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

akonadi

  • 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 
26 // Qt
27 #include <QtCore/QBuffer>
28 #include <QtCore/QFile>
29 #include <QtCore/QIODevice>
30 #include <QtCore/QString>
31 
32 #include <string>
33 
34 Q_DECLARE_METATYPE( std::string )
35 
36 namespace Akonadi {
37 
38 DefaultItemSerializerPlugin::DefaultItemSerializerPlugin()
39 {
40  Item::addToLegacyMapping<QByteArray>( QLatin1String( "application/octet-stream" ) );
41 }
42 
43 bool DefaultItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
44 {
45  if ( label != Item::FullPayload )
46  return false;
47 
48  item.setPayload( data.readAll() );
49  return true;
50 }
51 
52 void DefaultItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
53 {
54  Q_ASSERT( label == Item::FullPayload );
55  Q_UNUSED( label );
56  data.write( item.payload<QByteArray>() );
57 }
58 
59 bool StdStringItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
60 {
61  if ( label != Item::FullPayload )
62  return false;
63  std::string str;
64  {
65  const QByteArray ba = data.readAll();
66  str.assign( ba.data(), ba.size() );
67  }
68  item.setPayload( str );
69  return true;
70 }
71 
72 void StdStringItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
73 {
74  Q_ASSERT( label == Item::FullPayload );
75  Q_UNUSED( label );
76  const std::string str = item.payload<std::string>();
77  data.write( QByteArray::fromRawData( str.data(), str.size() ) );
78 }
79 
80 /*static*/
81 void ItemSerializer::deserialize( Item& item, const QByteArray& label, const QByteArray& data, int version, bool external )
82 {
83  if ( external ) {
84  QFile file( QString::fromUtf8(data) );
85  if ( file.open( QIODevice::ReadOnly ) ) {
86  deserialize( item, label, file, version );
87  file.close();
88  }
89  } else {
90  QBuffer buffer;
91  buffer.setData( data );
92  buffer.open( QIODevice::ReadOnly );
93  buffer.seek( 0 );
94  deserialize( item, label, buffer, version );
95  buffer.close();
96  }
97 }
98 
99 /*static*/
100 void ItemSerializer::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
101 {
102  if ( !TypePluginLoader::defaultPluginForMimeType( item.mimeType() )->deserialize( item, label, data, version ) ) {
103  kWarning() << "Unable to deserialize payload part:" << label;
104  data.seek( 0 );
105  kWarning() << "Payload data was: " << data.readAll();
106  }
107 }
108 
109 /*static*/
110 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QByteArray& data, int &version )
111 {
112  QBuffer buffer;
113  buffer.setBuffer( &data );
114  buffer.open( QIODevice::WriteOnly );
115  buffer.seek( 0 );
116  serialize( item, label, buffer, version );
117  buffer.close();
118 }
119 
120 /*static*/
121 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
122 {
123  if ( !item.hasPayload() )
124  return;
125  ItemSerializerPlugin * plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
126  plugin->serialize( item, label, data, version );
127 }
128 
129 void ItemSerializer::apply( Item &item, const Item &other )
130 {
131  if ( !other.hasPayload() )
132  return;
133 
134  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
135 
136  ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
137  if ( pluginV2 ) {
138  pluginV2->apply( item, other );
139  return;
140  }
141 
142  // Old-school merge:
143  foreach ( const QByteArray &part, other.loadedPayloadParts() ) {
144  QByteArray partData;
145  QBuffer buffer;
146  buffer.setBuffer( &partData );
147  buffer.open( QIODevice::ReadWrite );
148  buffer.seek( 0 );
149  int version;
150  serialize( other, part, buffer, version );
151  buffer.seek( 0 );
152  deserialize( item, part, buffer, version );
153  }
154 }
155 
156 QSet<QByteArray> ItemSerializer::parts( const Item & item )
157 {
158  if ( !item.hasPayload() )
159  return QSet<QByteArray>();
160  return TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() )->parts( item );
161 }
162 
163 QSet<QByteArray> ItemSerializer::availableParts( const Item & item )
164 {
165  if ( !item.hasPayload() )
166  return QSet<QByteArray>();
167  ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
168  ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
169 
170  if ( pluginV2 )
171  return pluginV2->availableParts( item );
172 
173  if (item.hasPayload())
174  return QSet<QByteArray>();
175 
176  return QSet<QByteArray>() << Item::FullPayload;
177 }
178 
179 Item ItemSerializer::convert( const Item & item, int mtid )
180 {
181 // kDebug() << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
182  if ( !item.hasPayload() ) {
183  kDebug() << " -> but item has no payload!";
184  return Item();
185  }
186 
187  if ( ItemSerializerPlugin * const plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), QVector<int>( 1, mtid ), TypePluginLoader::NoDefault ) ) {
188  kDebug() << " -> found a plugin that feels responsible, trying serialising the payload";
189  QBuffer buffer;
190  buffer.open( QIODevice::ReadWrite );
191  int version;
192  serialize( item, Item::FullPayload, buffer, version );
193  buffer.seek( 0 );
194  kDebug() << " -> serialized payload into" << buffer.size() << "bytes" << endl
195  << " -> going to deserialize";
196  Item newItem;
197  if ( plugin->deserialize( newItem, Item::FullPayload, buffer, version ) ) {
198  kDebug() << " -> conversion successful";
199  return newItem;
200  } else {
201  kDebug() << " -> conversion FAILED";
202  }
203  } else {
204 // kDebug() << " -> found NO plugin that feels responsible";
205  }
206  return Item();
207 }
208 
209 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:27:38 by doxygen 1.8.3.1 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.10.5 API Reference

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