• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.13.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 #include "gid/gidextractor_p.h"
30 
31 #include <QtCore/QDateTime>
32 
33 #include <kdebug.h>
34 
35 using namespace Akonadi;
36 
37 class Akonadi::ItemCreateJobPrivate : public JobPrivate
38 {
39  public:
40  ItemCreateJobPrivate( ItemCreateJob *parent )
41  : JobPrivate( parent )
42  {
43  }
44 
45  QByteArray nextPartHeader();
46 
47  Collection mCollection;
48  Item mItem;
49  QSet<QByteArray> mParts;
50  Item::Id mUid;
51  QDateTime mDatetime;
52  QByteArray mPendingData;
53 };
54 
55 QByteArray ItemCreateJobPrivate::nextPartHeader()
56 {
57  QByteArray command;
58  if ( !mParts.isEmpty() ) {
59  QSetIterator<QByteArray> it( mParts );
60  const QByteArray label = it.next();
61  mParts.remove( label );
62 
63  mPendingData.clear();
64  int version = 0;
65  ItemSerializer::serialize( mItem, label, mPendingData, version );
66  command += ' ' + ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartPayload, label, version );
67  if ( mPendingData.size() > 0 ) {
68  command += " {" + QByteArray::number( mPendingData.size() ) + "}\n";
69  } else {
70  if ( mPendingData.isNull() )
71  command += " NIL";
72  else
73  command += " \"\"";
74  command += nextPartHeader();
75  }
76  } else {
77  command += ")\n";
78  }
79  return command;
80 }
81 
82 ItemCreateJob::ItemCreateJob( const Item &item, const Collection &collection, QObject * parent )
83  : Job( new ItemCreateJobPrivate( this ), parent )
84 {
85  Q_D( ItemCreateJob );
86 
87  Q_ASSERT( !item.mimeType().isEmpty() );
88  d->mItem = item;
89  d->mParts = d->mItem.loadedPayloadParts();
90  d->mCollection = collection;
91 }
92 
93 ItemCreateJob::~ItemCreateJob()
94 {
95 }
96 
97 void ItemCreateJob::doStart()
98 {
99  Q_D( ItemCreateJob );
100 
101  QByteArray remoteId;
102 
103  QList<QByteArray> flags;
104  flags.append( "\\MimeType[" + d->mItem.mimeType().toLatin1() + ']' );
105  const QString gid = GidExtractor::getGid( d->mItem );
106  if ( !gid.isNull() ) {
107  flags.append( ImapParser::quote( "\\Gid[" + gid.toUtf8() + ']' ) );
108  }
109  if ( !d->mItem.remoteId().isEmpty() )
110  flags.append( ImapParser::quote( "\\RemoteId[" + d->mItem.remoteId().toUtf8() + ']' ) );
111  if ( !d->mItem.remoteRevision().isEmpty() )
112  flags.append( ImapParser::quote( "\\RemoteRevision[" + d->mItem.remoteRevision().toUtf8() + ']' ) );
113  flags += d->mItem.flags().toList();
114 
115  QByteArray command = d->newTag() + " X-AKAPPEND " + QByteArray::number( d->mCollection.id() )
116  + ' ' + QByteArray::number( d->mItem.size() )
117  + " (" + ImapParser::join( flags, " " ) + ")"
118  + " ("; // list of parts
119  const QByteArray attrs = ProtocolHelper::attributesToByteArray( d->mItem, true );
120  if ( !attrs.isEmpty() ) {
121  command += attrs;
122  }
123 
124  command += d->nextPartHeader();
125 
126  d->writeData( command );
127 }
128 
129 void ItemCreateJob::doHandleResponse( const QByteArray & tag, const QByteArray & data )
130 {
131  Q_D( ItemCreateJob );
132 
133  if ( tag == "+" ) { // ready for literal data
134  d->writeData( d->mPendingData );
135  d->writeData( d->nextPartHeader() );
136  return;
137  }
138  if ( tag == d->tag() ) {
139  int uidNextPos = data.indexOf( "UIDNEXT" );
140  if ( uidNextPos != -1 ) {
141  bool ok = false;
142  ImapParser::parseNumber( data, d->mUid, &ok, uidNextPos + 7 );
143  if ( !ok ) {
144  kDebug() << "Invalid UIDNEXT response to APPEND command: "
145  << tag << data;
146  }
147  }
148  int dateTimePos = data.indexOf( "DATETIME" );
149  if ( dateTimePos != -1 ) {
150  int resultPos = ImapParser::parseDateTime( data, d->mDatetime, dateTimePos + 8 );
151  if ( resultPos == (dateTimePos + 8) ) {
152  kDebug() << "Invalid DATETIME response to APPEND command: "
153  << tag << data;
154  }
155  }
156  }
157 }
158 
159 Item ItemCreateJob::item() const
160 {
161  Q_D( const ItemCreateJob );
162 
163  if ( d->mUid == 0 )
164  return Item();
165 
166  Item item( d->mItem );
167  item.setId( d->mUid );
168  item.setRevision( 0 );
169  item.setModificationTime( d->mDatetime );
170  item.setParentCollection( d->mCollection );
171  item.setStorageCollectionId( d->mCollection.id() );
172 
173  return item;
174 }
175 
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:159
Akonadi::Collection
Represents a collection of PIM items.
Definition: collection.h:75
Akonadi::ProtocolHelper::attributesToByteArray
static QByteArray attributesToByteArray(const Entity &entity, bool ns=false)
Convert attributes to their protocol representation.
Definition: protocolhelper.cpp:206
Akonadi::Job
Base class for all actions in the Akonadi storage.
Definition: job.h:86
Akonadi::ProtocolHelper::encodePartIdentifier
static QByteArray encodePartIdentifier(PartNamespace ns, const QByteArray &label, int version=0)
Encodes part label and namespace.
Definition: protocolhelper.cpp:226
Akonadi::ItemCreateJob
Job that creates a new item in the Akonadi storage.
Definition: itemcreatejob.h:73
Akonadi::GidExtractor::getGid
static QString getGid(const Item &item)
Extracts the gid from item.
Definition: gidextractor.cpp:39
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:129
Akonadi::ItemCreateJob::doStart
virtual void doStart()
This method must be reimplemented in the concrete jobs.
Definition: itemcreatejob.cpp:97
Akonadi::ItemCreateJob::ItemCreateJob
ItemCreateJob(const Item &item, const Collection &collection, QObject *parent=0)
Creates a new item create job.
Definition: itemcreatejob.cpp:82
Akonadi::JobPrivate
Definition: job_p.h:31
Akonadi::ItemSerializer::serialize
static void serialize(const Item &item, const QByteArray &label, QByteArray &data, int &version)
throws ItemSerializerException on failure
Definition: itemserializer.cpp:127
Akonadi::ItemCreateJob::~ItemCreateJob
~ItemCreateJob()
Destroys the item create job.
Definition: itemcreatejob.cpp:93
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:53 by doxygen 1.8.6 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.13.3 API Reference

Skip menu "kdepimlibs-4.13.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