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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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  Collection mCollection;
46  Item mItem;
47  QSet<QByteArray> mParts;
48  Item::Id mUid;
49  QDateTime mDatetime;
50  QByteArray mData;
51 };
52 
53 ItemCreateJob::ItemCreateJob( const Item &item, const Collection &collection, QObject * parent )
54  : Job( new ItemCreateJobPrivate( this ), parent )
55 {
56  Q_D( ItemCreateJob );
57 
58  Q_ASSERT( !item.mimeType().isEmpty() );
59  d->mItem = item;
60  d->mParts = d->mItem.loadedPayloadParts();
61  d->mCollection = collection;
62 }
63 
64 ItemCreateJob::~ItemCreateJob()
65 {
66 }
67 
68 void ItemCreateJob::doStart()
69 {
70  Q_D( ItemCreateJob );
71 
72  QByteArray remoteId;
73 
74  QList<QByteArray> flags;
75  flags.append( "\\MimeType[" + d->mItem.mimeType().toLatin1() + ']' );
76  const QString gid = GidExtractor::getGid( d->mItem );
77  if ( !gid.isNull() ) {
78  flags.append( ImapParser::quote( "\\Gid[" + gid.toUtf8() + ']' ) );
79  }
80  if ( !d->mItem.remoteId().isEmpty() )
81  flags.append( ImapParser::quote( "\\RemoteId[" + d->mItem.remoteId().toUtf8() + ']' ) );
82  if ( !d->mItem.remoteRevision().isEmpty() )
83  flags.append( ImapParser::quote( "\\RemoteRevision[" + d->mItem.remoteRevision().toUtf8() + ']' ) );
84  flags += d->mItem.flags().toList();
85 
86  // switch between a normal APPEND and a multipart X-AKAPPEND, based on the number of parts
87  if ( d->mItem.attributes().isEmpty() && ( d->mParts.isEmpty() || (d->mParts.size() == 1 && d->mParts.contains( Item::FullPayload )) ) ) {
88  if ( d->mItem.hasPayload() ) {
89  int version = 0;
90  ItemSerializer::serialize( d->mItem, Item::FullPayload, d->mData, version );
91  }
92  int dataSize = d->mData.size();
93 
94  d->writeData( d->newTag() + " APPEND " + QByteArray::number( d->mCollection.id() )
95  + ' ' + QByteArray::number( d->mItem.size() )
96  + " (" + ImapParser::join( flags, " " ) + ") {"
97  + QByteArray::number( dataSize ) + "}\n" );
98  }
99  else { // do a multipart X-AKAPPEND
100  QByteArray command = d->newTag() + " X-AKAPPEND " + QByteArray::number( d->mCollection.id() )
101  + ' ' + QByteArray::number( d->mItem.size() )
102  + " (" + ImapParser::join( flags, " " ) + ") ";
103 
104  QList<QByteArray> partSpecs;
105  int totalSize = 0;
106  foreach ( const QByteArray &partName, d->mParts ) {
107  QByteArray partData;
108  int version = 0;
109  ItemSerializer::serialize( d->mItem, partName, partData, version );
110  totalSize += partData.size();
111  const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartPayload, partName, version );
112  partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( partData.size() ) );
113  d->mData += partData;
114  }
115  foreach ( const Attribute* attr, d->mItem.attributes() ) {
116  const QByteArray data = attr->serialized();
117  totalSize += data.size();
118  const QByteArray partId = ProtocolHelper::encodePartIdentifier( ProtocolHelper::PartAttribute, attr->type() );
119  partSpecs.append( ImapParser::quote( partId ) + ':' + QByteArray::number( data.size() ) );
120  d->mData += data;
121  }
122  command += '(' + ImapParser::join( partSpecs, "," ) + ") " +
123  '{' + QByteArray::number( totalSize ) + "}\n";
124 
125  d->writeData( command );
126  }
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->mData );
135  if ( !d->mData.endsWith( '\n' ) )
136  d->writeData( "\n" );
137  return;
138  }
139  if ( tag == d->tag() ) {
140  int uidNextPos = data.indexOf( "UIDNEXT" );
141  if ( uidNextPos != -1 ) {
142  bool ok = false;
143  ImapParser::parseNumber( data, d->mUid, &ok, uidNextPos + 7 );
144  if ( !ok ) {
145  kDebug() << "Invalid UIDNEXT response to APPEND command: "
146  << tag << data;
147  }
148  }
149  int dateTimePos = data.indexOf( "DATETIME" );
150  if ( dateTimePos != -1 ) {
151  int resultPos = ImapParser::parseDateTime( data, d->mDatetime, dateTimePos + 8 );
152  if ( resultPos == (dateTimePos + 8) ) {
153  kDebug() << "Invalid DATETIME response to APPEND command: "
154  << tag << data;
155  }
156  }
157  }
158 }
159 
160 Item ItemCreateJob::item() const
161 {
162  Q_D( const ItemCreateJob );
163 
164  if ( d->mUid == 0 )
165  return Item();
166 
167  Item item( d->mItem );
168  item.setId( d->mUid );
169  item.setRevision( 0 );
170  item.setModificationTime( d->mDatetime );
171  item.setParentCollection( d->mCollection );
172  item.setStorageCollectionId( d->mCollection.id() );
173 
174  return item;
175 }
176 
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:160
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::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:68
Akonadi::ItemCreateJob::ItemCreateJob
ItemCreateJob(const Item &item, const Collection &collection, QObject *parent=0)
Creates a new item create job.
Definition: itemcreatejob.cpp:53
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:127
Akonadi::ItemCreateJob::~ItemCreateJob
~ItemCreateJob()
Destroys the item create job.
Definition: itemcreatejob.cpp:64
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-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



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