00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectioncreatejob.h"
00021 #include "imapparser_p.h"
00022 #include "protocolhelper_p.h"
00023
00024 #include "job_p.h"
00025
00026 #include <kdebug.h>
00027 #include <KLocale>
00028 using namespace Akonadi;
00029
00030 class Akonadi::CollectionCreateJobPrivate : public JobPrivate
00031 {
00032 public:
00033 CollectionCreateJobPrivate( CollectionCreateJob *parent )
00034 : JobPrivate( parent )
00035 {
00036 }
00037
00038 Collection mCollection;
00039 };
00040
00041 CollectionCreateJob::CollectionCreateJob( const Collection &collection, QObject * parent )
00042 : Job( new CollectionCreateJobPrivate( this ), parent )
00043 {
00044 Q_D( CollectionCreateJob );
00045
00046 d->mCollection = collection;
00047 }
00048
00049 CollectionCreateJob::~CollectionCreateJob( )
00050 {
00051 }
00052
00053 void CollectionCreateJob::doStart( )
00054 {
00055 Q_D( CollectionCreateJob );
00056 if ( d->mCollection.parentCollection().id() < 0 && d->mCollection.parentCollection().remoteId().isEmpty() ) {
00057 setError( Unknown );
00058 setErrorText( i18n( "Invalid parent" ) );
00059 emitResult();
00060 return;
00061 }
00062
00063 QByteArray command = d->newTag();
00064 if ( d->mCollection.parentCollection().id() < 0 )
00065 command += " RID";
00066 command += " CREATE " + ImapParser::quote( d->mCollection.name().toUtf8() ) + ' ';
00067 if ( d->mCollection.parentCollection().id() >= 0 )
00068 command += QByteArray::number( d->mCollection.parentCollection().id() );
00069 else
00070 command += ImapParser::quote( d->mCollection.parentCollection().remoteId().toUtf8() );
00071 command += " (";
00072 if ( !d->mCollection.contentMimeTypes().isEmpty() )
00073 {
00074 QList<QByteArray> cList;
00075 foreach ( const QString &s, d->mCollection.contentMimeTypes() ) cList << s.toLatin1();
00076 command += "MIMETYPE (" + ImapParser::join( cList, QByteArray(" ") ) + ')';
00077 }
00078 command += " REMOTEID " + ImapParser::quote( d->mCollection.remoteId().toUtf8() );
00079 command += " REMOTEREVISION " + ImapParser::quote( d->mCollection.remoteRevision().toUtf8() );
00080 foreach ( Attribute* attr, d->mCollection.attributes() )
00081 command += ' ' + attr->type() + ' ' + ImapParser::quote( attr->serialized() );
00082 command += ' ' + ProtocolHelper::cachePolicyToByteArray( d->mCollection.cachePolicy() );
00083 command += ")\n";
00084 d->writeData( command );
00085 emitWriteFinished();
00086 }
00087
00088 Collection CollectionCreateJob::collection() const
00089 {
00090 Q_D( const CollectionCreateJob );
00091
00092 return d->mCollection;
00093 }
00094
00095 void CollectionCreateJob::doHandleResponse(const QByteArray & tag, const QByteArray & data)
00096 {
00097 Q_D( CollectionCreateJob );
00098
00099 if ( tag == "*" ) {
00100 Collection col;
00101 ProtocolHelper::parseCollection( data, col );
00102 if ( !col.isValid() )
00103 return;
00104
00105 col.setParentCollection( d->mCollection.parentCollection() );
00106 col.setName( d->mCollection.name() );
00107 col.setRemoteId( d->mCollection.remoteId() );
00108 col.setRemoteRevision( d->mCollection.remoteRevision() );
00109 d->mCollection = col;
00110 } else {
00111 Job::doHandleResponse( tag, data );
00112 }
00113 }
00114
00115 #include "collectioncreatejob.moc"