• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

akonadi

itemcreatejob.cpp

00001 /*
00002     Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
00003     Copyright (c) 2007        Robert Zwerus <arzie@dds.nl>
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
00019 */
00020 
00021 #include "itemcreatejob.h"
00022 
00023 #include "collection.h"
00024 #include "imapparser_p.h"
00025 #include "item.h"
00026 #include "itemserializer.h"
00027 #include "job_p.h"
00028 #include "protocolhelper.h"
00029 
00030 #include <kdebug.h>
00031 
00032 using namespace Akonadi;
00033 
00034 class Akonadi::ItemCreateJobPrivate : public JobPrivate
00035 {
00036   public:
00037     ItemCreateJobPrivate( ItemCreateJob *parent )
00038       : JobPrivate( parent )
00039     {
00040     }
00041 
00042     Collection mCollection;
00043     Item mItem;
00044     QSet<QByteArray> mParts;
00045     Item::Id mUid;
00046     QByteArray mData;
00047 };
00048 
00049 ItemCreateJob::ItemCreateJob( const Item &item, const Collection &collection, QObject * parent )
00050   : Job( new ItemCreateJobPrivate( this ), parent )
00051 {
00052   Q_D( ItemCreateJob );
00053 
00054   Q_ASSERT( !item.mimeType().isEmpty() );
00055   d->mItem = item;
00056   d->mParts = d->mItem.loadedPayloadParts();
00057   d->mCollection = collection;
00058 }
00059 
00060 ItemCreateJob::~ItemCreateJob()
00061 {
00062 }
00063 
00064 void ItemCreateJob::doStart()
00065 {
00066   Q_D( ItemCreateJob );
00067 
00068   QByteArray remoteId;
00069 
00070   if ( !d->mItem.remoteId().isEmpty() )
00071     remoteId = ' ' + ImapParser::quote( "\\RemoteId[" + d->mItem.remoteId().toUtf8() + ']' );
00072   // switch between a normal APPEND and a multipart X-AKAPPEND, based on the number of parts
00073   if ( d->mItem.attributes().isEmpty() && ( d->mParts.isEmpty() || (d->mParts.size() == 1 && d->mParts.contains( Item::FullPayload )) ) ) {
00074     if ( d->mItem.hasPayload() ) {
00075       int version = 0;
00076       ItemSerializer::serialize( d->mItem, Item::FullPayload, d->mData, version );
00077     }
00078     int dataSize = d->mData.size();
00079 
00080     d->writeData( d->newTag() + " APPEND " + QByteArray::number( d->mCollection.id() )
00081         + " (\\MimeType[" + d->mItem.mimeType().toLatin1() + ']' + remoteId + ") {"
00082         + QByteArray::number( dataSize ) + "}\n" );
00083   }
00084   else { // do a multipart X-AKAPPEND
00085     QByteArray command = d->newTag() + " X-AKAPPEND " + QByteArray::number( d->mCollection.id() )
00086         + " (\\MimeType[" + d->mItem.mimeType().toLatin1() + ']' + remoteId + ") ";
00087 
00088     QList<QByteArray> partSpecs;
00089     int totalSize = 0;
00090     foreach( const QByteArray &partName, d->mParts ) {
00091       QByteArray partData;
00092       int version = 0;
00093       ItemSerializer::serialize( d->mItem, partName, partData, version );
00094       totalSize += partData.size();
00095       const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartPayload, partName, version );
00096       partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( partData.size() ) );
00097       d->mData += partData;
00098     }
00099     foreach ( const Attribute* attr, d->mItem.attributes() ) {
00100       const QByteArray data = attr->serialized();
00101       totalSize += data.size();
00102       const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartAttribute, attr->type() );
00103       partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( data.size() ) );
00104       d->mData += data;
00105     }
00106     command += '(' + ImapParser::join( partSpecs, "," ) + ") " +
00107       '{' + QByteArray::number( totalSize ) + "}\n";
00108 
00109     d->writeData( command );
00110   }
00111 }
00112 
00113 void ItemCreateJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
00114 {
00115   Q_D( ItemCreateJob );
00116 
00117   if ( tag == "+" ) { // ready for literal data
00118     d->writeData( d->mData );
00119     if ( !d->mData.endsWith( '\n' ) )
00120       d->writeData( "\n" );
00121     return;
00122   }
00123   if ( tag == d->tag() ) {
00124     if ( int pos = data.indexOf( "UIDNEXT" ) ) {
00125       bool ok = false;
00126       ImapParser::parseNumber( data, d->mUid, &ok, pos + 7 );
00127       if ( !ok ) {
00128         kDebug( 5250 ) << "Invalid UIDNEXT response to APPEND command: "
00129                        << tag << data;
00130       }
00131     }
00132   }
00133 }
00134 
00135 Item ItemCreateJob::item() const
00136 {
00137   Q_D( const ItemCreateJob );
00138 
00139   if ( d->mUid == 0 )
00140     return Item();
00141 
00142   Item item( d->mItem );
00143   item.setId( d->mUid );
00144 
00145   return item;
00146 }
00147 
00148 #include "itemcreatejob.moc"

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal