• 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
itemserializer.cpp
1 /*
2  Copyright (c) 2007 Till Adam <adam@kde.org>
3  Copyright (c) 2007 Volker Krause <vkrause@kde.org>
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 "itemserializer_p.h"
22 #include "item.h"
23 #include "itemserializerplugin.h"
24 #include "typepluginloader_p.h"
25 #include "servermanager.h"
26 #include <akonadi/private/xdgbasedirs_p.h>
27 
28 // Qt
29 #include <QtCore/QBuffer>
30 #include <QtCore/QFile>
31 #include <QtCore/QIODevice>
32 #include <QtCore/QString>
33 #include <QtCore/QDir>
34 
35 #include <string>
36 
37 Q_DECLARE_METATYPE( std::string )
38 
39 namespace Akonadi {
40 
41 DefaultItemSerializerPlugin::DefaultItemSerializerPlugin()
42 {
43  Item::addToLegacyMapping<QByteArray>( QLatin1String( "application/octet-stream" ) );
44 }
45 
46 bool DefaultItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
47 {
48  if ( label != Item::FullPayload )
49  return false;
50 
51  item.setPayload( data.readAll() );
52  return true;
53 }
54 
55 void DefaultItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
56 {
57  Q_ASSERT( label == Item::FullPayload );
58  Q_UNUSED( label );
59  data.write( item.payload<QByteArray>() );
60 }
61 
62 bool StdStringItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
63 {
64  if ( label != Item::FullPayload )
65  return false;
66  std::string str;
67  {
68  const QByteArray ba = data.readAll();
69  str.assign( ba.data(), ba.size() );
70  }
71  item.setPayload( str );
72  return true;
73 }
74 
75 void StdStringItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
76 {
77  Q_ASSERT( label == Item::FullPayload );
78  Q_UNUSED( label );
79  const std::string str = item.payload<std::string>();
80  data.write( QByteArray::fromRawData( str.data(), str.size() ) );
81 }
82 
83 /*static*/
84 void ItemSerializer::deserialize( Item& item, const QByteArray& label, const QByteArray& data, int version, bool external )
85 {
86  if ( external ) {
87  QString path;
88  QString fileName = QString::fromUtf8( data );
89  QFileInfo fi( fileName );
90  if ( !fi.isAbsolute() ) {
91  QString fullRelPath = QLatin1String( "akonadi" );
92  if ( Akonadi::ServerManager::hasInstanceIdentifier() ) {
93  fullRelPath += QDir::separator() + QLatin1String( "instance" ) + QDir::separator() + Akonadi::ServerManager::instanceIdentifier();
94  }
95  fullRelPath += QDir::separator() + QLatin1String( "file_db_data" );
96  fileName = XdgBaseDirs::saveDir( "data", fullRelPath ) + QDir::separator() + fileName;
97  }
98 
99  QFile file( fileName );
100  if ( file.open( QIODevice::ReadOnly ) ) {
101  deserialize( item, label, file, version );
102  file.close();
103  } else {
104  kWarning() << "Failed to open external payload:" << fileName << file.errorString();
105  }
106  } else {
107  QBuffer buffer;
108  buffer.setData( data );
109  buffer.open( QIODevice::ReadOnly );
110  buffer.seek( 0 );
111  deserialize( item, label, buffer, version );
112  buffer.close();
113  }
114 }
115 
116 /*static*/
117 void ItemSerializer::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
118 {
119  if ( !TypePluginLoader::defaultPluginForMimeType( item.mimeType() )->deserialize( item, label, data, version ) ) {
120  kWarning() << "Unable to deserialize payload part:" << label;
121  data.seek( 0 );
122  kWarning() << "Payload data was: " << data.readAll();
123  }
124 }
125 
126 /*static*/
127 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QByteArray& data, int &version )
128 {
129  QBuffer buffer;
130  buffer.setBuffer( &data );
131  buffer.open( QIODevice::WriteOnly );
132  buffer.seek( 0 );
133  serialize( item, label, buffer, version );
134  buffer.close();
135 }
136 
137 /*static*/
138 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
139 {
140  if ( !item.hasPayload() )
141  return;
142  ItemSerializerPlugin * plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
143  plugin->serialize( item, label, data, version );
144 }
145 
146 void ItemSerializer::apply( Item &item, const Item &other )
147 {
148  if ( !other.hasPayload() )
149  return;
150 
151  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
152 
153  ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
154  if ( pluginV2 ) {
155  pluginV2->apply( item, other );
156  return;
157  }
158 
159  // Old-school merge:
160  foreach ( const QByteArray &part, other.loadedPayloadParts() ) {
161  QByteArray partData;
162  QBuffer buffer;
163  buffer.setBuffer( &partData );
164  buffer.open( QIODevice::ReadWrite );
165  buffer.seek( 0 );
166  int version;
167  serialize( other, part, buffer, version );
168  buffer.seek( 0 );
169  deserialize( item, part, buffer, version );
170  }
171 }
172 
173 QSet<QByteArray> ItemSerializer::parts( const Item & item )
174 {
175  if ( !item.hasPayload() )
176  return QSet<QByteArray>();
177  return TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() )->parts( item );
178 }
179 
180 QSet<QByteArray> ItemSerializer::availableParts( const Item & item )
181 {
182  if ( !item.hasPayload() )
183  return QSet<QByteArray>();
184  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
185  ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
186 
187  if ( pluginV2 )
188  return pluginV2->availableParts( item );
189 
190  if (item.hasPayload())
191  return QSet<QByteArray>();
192 
193  return QSet<QByteArray>() << Item::FullPayload;
194 }
195 
196 Item ItemSerializer::convert( const Item & item, int mtid )
197 {
198 // kDebug() << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
199  if ( !item.hasPayload() ) {
200  kDebug() << " -> but item has no payload!";
201  return Item();
202  }
203 
204  if ( ItemSerializerPlugin * const plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), QVector<int>( 1, mtid ), TypePluginLoader::NoDefault ) ) {
205  kDebug() << " -> found a plugin that feels responsible, trying serialising the payload";
206  QBuffer buffer;
207  buffer.open( QIODevice::ReadWrite );
208  int version;
209  serialize( item, Item::FullPayload, buffer, version );
210  buffer.seek( 0 );
211  kDebug() << " -> serialized payload into" << buffer.size() << "bytes" << endl
212  << " -> going to deserialize";
213  Item newItem;
214  if ( plugin->deserialize( newItem, Item::FullPayload, buffer, version ) ) {
215  kDebug() << " -> conversion successful";
216  return newItem;
217  } else {
218  kDebug() << " -> conversion FAILED";
219  }
220  } else {
221 // kDebug() << " -> found NO plugin that feels responsible";
222  }
223  return Item();
224 }
225 
226 void ItemSerializer::overridePluginLookup( QObject *p )
227 {
228  TypePluginLoader::overridePluginLookup(p);
229 }
230 
231 }
Akonadi::ServerManager::instanceIdentifier
static QString instanceIdentifier()
Returns the identifier of the Akonadi instance we are connected to.
Definition: servermanager.cpp:275
Akonadi::ItemSerializerPluginV2::availableParts
virtual QSet< QByteArray > availableParts(const Item &item) const
Returns the parts available in the item item.
Definition: itemserializerplugin.cpp:51
Akonadi::ItemSerializerPluginV2::apply
virtual void apply(Item &item, const Item &other)
Merges the payload parts in other into item.
Definition: itemserializerplugin.cpp:59
Akonadi::ItemSerializerPlugin::serialize
virtual void serialize(const Item &item, const QByteArray &label, QIODevice &data, int &version)=0
Convert the payload object provided in item into its serialzed form into data.
Akonadi::ItemSerializerPluginV2
The extended base class for item type serializer plugins.
Definition: itemserializerplugin.h:193
Akonadi::ItemSerializerPlugin
The base class for item type serializer plugins.
Definition: itemserializerplugin.h:119
Akonadi::ServerManager::hasInstanceIdentifier
static bool hasInstanceIdentifier()
Returns true if we are connected to a non-default Akonadi server instance.
Definition: servermanager.cpp:280
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