• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
itemcreatejob.cpp
1 /*
2  Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
3  Copyright (c) 2007 Robert Zwerus <arzie@dds.nl>
4 
5  This library is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or (at your
8  option) any later version.
9 
10  This library is distributed in the hope that it will be useful, but WITHOUT
11  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to the
17  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "itemcreatejob.h"
22 
23 #include "collection.h"
24 #include "imapparser_p.h"
25 #include "item.h"
26 #include "itemserializer_p.h"
27 #include "job_p.h"
28 #include "protocolhelper_p.h"
29 
30 #include <QtCore/QDateTime>
31 
32 #include <kdebug.h>
33 
34 using namespace Akonadi;
35 
36 class Akonadi::ItemCreateJobPrivate : public JobPrivate
37 {
38  public:
39  ItemCreateJobPrivate( ItemCreateJob *parent )
40  : JobPrivate( parent )
41  {
42  }
43 
44  Collection mCollection;
45  Item mItem;
46  QSet<QByteArray> mParts;
47  Item::Id mUid;
48  QDateTime mDatetime;
49  QByteArray mData;
50 };
51 
52 ItemCreateJob::ItemCreateJob( const Item &item, const Collection &collection, QObject * parent )
53  : Job( new ItemCreateJobPrivate( this ), parent )
54 {
55  Q_D( ItemCreateJob );
56 
57  Q_ASSERT( !item.mimeType().isEmpty() );
58  d->mItem = item;
59  d->mParts = d->mItem.loadedPayloadParts();
60  d->mCollection = collection;
61 }
62 
63 ItemCreateJob::~ItemCreateJob()
64 {
65 }
66 
67 void ItemCreateJob::doStart()
68 {
69  Q_D( ItemCreateJob );
70 
71  QByteArray remoteId;
72 
73  QList<QByteArray> flags;
74  flags.append( "\\MimeType[" + d->mItem.mimeType().toLatin1() + ']' );
75  if ( !d->mItem.remoteId().isEmpty() )
76  flags.append( ImapParser::quote( "\\RemoteId[" + d->mItem.remoteId().toUtf8() + ']' ) );
77  if ( !d->mItem.remoteRevision().isEmpty() )
78  flags.append( ImapParser::quote( "\\RemoteRevision[" + d->mItem.remoteRevision().toUtf8() + ']' ) );
79  flags += d->mItem.flags().toList();
80 
81  // switch between a normal APPEND and a multipart X-AKAPPEND, based on the number of parts
82  if ( d->mItem.attributes().isEmpty() && ( d->mParts.isEmpty() || (d->mParts.size() == 1 && d->mParts.contains( Item::FullPayload )) ) ) {
83  if ( d->mItem.hasPayload() ) {
84  int version = 0;
85  ItemSerializer::serialize( d->mItem, Item::FullPayload, d->mData, version );
86  }
87  int dataSize = d->mData.size();
88 
89  d->writeData( d->newTag() + " APPEND " + QByteArray::number( d->mCollection.id() )
90  + ' ' + QByteArray::number( d->mItem.size() )
91  + " (" + ImapParser::join( flags, " " ) + ") {"
92  + QByteArray::number( dataSize ) + "}\n" );
93  }
94  else { // do a multipart X-AKAPPEND
95  QByteArray command = d->newTag() + " X-AKAPPEND " + QByteArray::number( d->mCollection.id() )
96  + ' ' + QByteArray::number( d->mItem.size() )
97  + " (" + ImapParser::join( flags, " " ) + ") ";
98 
99  QList<QByteArray> partSpecs;
100  int totalSize = 0;
101  foreach ( const QByteArray &partName, d->mParts ) {
102  QByteArray partData;
103  int version = 0;
104  ItemSerializer::serialize( d->mItem, partName, partData, version );
105  totalSize += partData.size();
106  const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartPayload, partName, version );
107  partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( partData.size() ) );
108  d->mData += partData;
109  }
110  foreach ( const Attribute* attr, d->mItem.attributes() ) {
111  const QByteArray data = attr->serialized();
112  totalSize += data.size();
113  const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartAttribute, attr->type() );
114  partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( data.size() ) );
115  d->mData += data;
116  }
117  command += '(' + ImapParser::join( partSpecs, "," ) + ") " +
118  '{' + QByteArray::number( totalSize ) + "}\n";
119 
120  d->writeData( command );
121  }
122 }
123 
124 void ItemCreateJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
125 {
126  Q_D( ItemCreateJob );
127 
128  if ( tag == "+" ) { // ready for literal data
129  d->writeData( d->mData );
130  if ( !d->mData.endsWith( '\n' ) )
131  d->writeData( "\n" );
132  return;
133  }
134  if ( tag == d->tag() ) {
135  int uidNextPos = data.indexOf( "UIDNEXT" );
136  if ( uidNextPos != -1 ) {
137  bool ok = false;
138  ImapParser::parseNumber( data, d->mUid, &ok, uidNextPos + 7 );
139  if ( !ok ) {
140  kDebug() << "Invalid UIDNEXT response to APPEND command: "
141  << tag << data;
142  }
143  }
144  int dateTimePos = data.indexOf( "DATETIME" );
145  if ( dateTimePos != -1 ) {
146  int resultPos = ImapParser::parseDateTime( data, d->mDatetime, dateTimePos + 8 );
147  if ( resultPos == (dateTimePos + 8) ) {
148  kDebug() << "Invalid DATETIME response to APPEND command: "
149  << tag << data;
150  }
151  }
152  }
153 }
154 
155 Item ItemCreateJob::item() const
156 {
157  Q_D( const ItemCreateJob );
158 
159  if ( d->mUid == 0 )
160  return Item();
161 
162  Item item( d->mItem );
163  item.setId( d->mUid );
164  item.setRevision( 0 );
165  item.setModificationTime( d->mDatetime );
166  item.setParentCollection( d->mCollection );
167  item.setStorageCollectionId( d->mCollection.id() );
168 
169  return item;
170 }
171 
Akonadi::ItemCreateJob::item
Item item() const
Returns the created item with the new unique id, or an invalid item if the job failed.
Definition: itemcreatejob.cpp:155
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::Attribute
Provides interface for custom attributes for Entity.
Definition: attribute.h:138
Akonadi::ProtocolHelper::encodePartIdentifier
static QByteArray encodePartIdentifier(PartNamespace ns, const QByteArray &label, int version=0)
Encodes part label and namespace.
Definition: protocolhelper.cpp:216
Akonadi::ItemCreateJob
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:73
Akonadi::ItemCreateJob::doHandleResponse
virtual void doHandleResponse(const QByteArray &tag, const QByteArray &data)
This method should be reimplemented in the concrete jobs in case you want to handle incoming data...
Definition: itemcreatejob.cpp:124
Akonadi::ItemCreateJob::doStart
virtual void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemcreatejob.cpp:67
Akonadi::ItemCreateJob::ItemCreateJob
ItemCreateJob(const Item &item, const Collection &collection, QObject *parent=0)
Creates a new item create job.
Definition: itemcreatejob.cpp:52
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::Attribute::serialized
virtual QByteArray serialized() const =0
Returns a QByteArray representation of the attribute which will be storaged.
Akonadi::ItemSerializer::serialize
static void serialize(const Item &item, const QByteArray &label, QByteArray &data, int &version)
throws ItemSerializerException on failure
Definition: itemserializer.cpp:110
Akonadi::ItemCreateJob::~ItemCreateJob
~ItemCreateJob()
Destroys the item create job.
Definition: itemcreatejob.cpp:63
Akonadi::Attribute::type
virtual QByteArray type() const =0
Returns the type of the attribute.
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:17 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

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

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal