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

akonadi

itemserializer.cpp
00001 /*
00002     Copyright (c) 2007 Till Adam <adam@kde.org>
00003     Copyright (c) 2007 Volker Krause <vkrause@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU Library General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or (at your
00008     option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful, but WITHOUT
00011     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013     License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to the
00017     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018     02110-1301, USA.
00019 */
00020 
00021 #include "itemserializer_p.h"
00022 #include "item.h"
00023 #include "itemserializerplugin.h"
00024 #include "typepluginloader_p.h"
00025 
00026 // Qt
00027 #include <QtCore/QBuffer>
00028 #include <QtCore/QFile>
00029 #include <QtCore/QIODevice>
00030 #include <QtCore/QString>
00031 
00032 #include <string>
00033 
00034 Q_DECLARE_METATYPE( std::string )
00035 
00036 namespace Akonadi {
00037 
00038 DefaultItemSerializerPlugin::DefaultItemSerializerPlugin()
00039 {
00040   Item::addToLegacyMapping<QByteArray>( QLatin1String( "application/octet-stream" ) );
00041 }
00042 
00043 bool DefaultItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
00044 {
00045   if ( label != Item::FullPayload )
00046     return false;
00047 
00048   item.setPayload( data.readAll() );
00049   return true;
00050 }
00051 
00052 void DefaultItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
00053 {
00054   Q_ASSERT( label == Item::FullPayload );
00055   Q_UNUSED( label );
00056   data.write( item.payload<QByteArray>() );
00057 }
00058 
00059 bool StdStringItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
00060 {
00061   if ( label != Item::FullPayload )
00062     return false;
00063   std::string str;
00064   {
00065     const QByteArray ba = data.readAll();
00066     str.assign( ba.data(), ba.size() );
00067   }
00068   item.setPayload( str );
00069   return true;
00070 }
00071 
00072 void StdStringItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
00073 {
00074   Q_ASSERT( label == Item::FullPayload );
00075   Q_UNUSED( label );
00076   const std::string str = item.payload<std::string>();
00077   data.write( QByteArray::fromRawData( str.data(), str.size() ) );
00078 }
00079 
00080 /*static*/
00081 void ItemSerializer::deserialize( Item& item, const QByteArray& label, const QByteArray& data, int version, bool external )
00082 {
00083   if ( external ) {
00084     QFile file( QString::fromUtf8(data) );
00085     if ( file.open( QIODevice::ReadOnly ) ) {
00086       deserialize( item, label, file, version );
00087       file.close();
00088     }
00089   } else {
00090     QBuffer buffer;
00091     buffer.setData( data );
00092     buffer.open( QIODevice::ReadOnly );
00093     buffer.seek( 0 );
00094     deserialize( item, label, buffer, version );
00095     buffer.close();
00096   }
00097 }
00098 
00099 /*static*/
00100 void ItemSerializer::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
00101 {
00102   if ( !TypePluginLoader::defaultPluginForMimeType( item.mimeType() )->deserialize( item, label, data, version ) ) {
00103     kWarning() << "Unable to deserialize payload part:" << label;
00104     data.seek( 0 );
00105     kWarning() << "Payload data was: " << data.readAll();
00106   }
00107 }
00108 
00109 /*static*/
00110 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QByteArray& data, int &version )
00111 {
00112   QBuffer buffer;
00113   buffer.setBuffer( &data );
00114   buffer.open( QIODevice::WriteOnly );
00115   buffer.seek( 0 );
00116   serialize( item, label, buffer, version );
00117   buffer.close();
00118 }
00119 
00120 /*static*/
00121 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
00122 {
00123   if ( !item.hasPayload() )
00124     return;
00125   ItemSerializerPlugin * plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
00126   plugin->serialize( item, label, data, version );
00127 }
00128 
00129 void ItemSerializer::apply( Item &item, const Item &other )
00130 {
00131   if ( !other.hasPayload() )
00132     return;
00133 
00134   ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
00135 
00136   ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
00137   if ( pluginV2 ) {
00138     pluginV2->apply( item, other );
00139     return;
00140   }
00141 
00142   // Old-school merge:
00143   foreach ( const QByteArray &part, other.loadedPayloadParts() ) {
00144     QByteArray partData;
00145     QBuffer buffer;
00146     buffer.setBuffer( &partData );
00147     buffer.open( QIODevice::ReadWrite );
00148     buffer.seek( 0 );
00149     int version;
00150     serialize( other, part, buffer, version );
00151     buffer.seek( 0 );
00152     deserialize( item, part, buffer, version );
00153   }
00154 }
00155 
00156 QSet<QByteArray> ItemSerializer::parts( const Item & item )
00157 {
00158   if ( !item.hasPayload() )
00159     return QSet<QByteArray>();
00160   return TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() )->parts( item );
00161 }
00162 
00163 QSet<QByteArray> ItemSerializer::availableParts( const Item & item )
00164 {
00165   if ( !item.hasPayload() )
00166     return QSet<QByteArray>();
00167   ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), item.availablePayloadMetaTypeIds() );
00168   ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
00169 
00170   if ( pluginV2 )
00171     return pluginV2->availableParts( item );
00172 
00173   if (item.hasPayload())
00174     return QSet<QByteArray>();
00175 
00176   return QSet<QByteArray>() << Item::FullPayload;
00177 }
00178 
00179 Item ItemSerializer::convert( const Item & item, int mtid )
00180 {
00181 //   kDebug() << "asked to convert a" << item.mimeType() << "item to format" << ( mtid ? QMetaType::typeName( mtid ) : "<legacy>" );
00182   if ( !item.hasPayload() ) {
00183     kDebug() << "  -> but item has no payload!";
00184     return Item();
00185   }
00186 
00187   if ( ItemSerializerPlugin * const plugin = TypePluginLoader::pluginForMimeTypeAndClass( item.mimeType(), QVector<int>( 1, mtid ), TypePluginLoader::NoDefault ) ) {
00188     kDebug() << "  -> found a plugin that feels responsible, trying serialising the payload";
00189     QBuffer buffer;
00190     buffer.open( QIODevice::ReadWrite );
00191     int version;
00192     serialize( item, Item::FullPayload, buffer, version );
00193     buffer.seek( 0 );
00194     kDebug() << "    -> serialized payload into" << buffer.size() << "bytes" << endl
00195              << "  -> going to deserialize";
00196     Item newItem;
00197     if ( plugin->deserialize( newItem, Item::FullPayload, buffer, version ) ) {
00198       kDebug() << "    -> conversion successful";
00199       return newItem;
00200     } else {
00201       kDebug() << "    -> conversion FAILED";
00202     }
00203   } else {
00204 //     kDebug() << "  -> found NO plugin that feels responsible";
00205   }
00206   return Item();
00207 }
00208 
00209 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 22:18:34 by doxygen 1.8.0 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.8.3 API Reference

Skip menu "kdepimlibs-4.8.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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