• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • 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 namespace Akonadi {
00033 
00034 DefaultItemSerializerPlugin::DefaultItemSerializerPlugin()
00035 {
00036 }
00037 
00038 bool DefaultItemSerializerPlugin::deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
00039 {
00040   if ( label != Item::FullPayload )
00041     return false;
00042 
00043   item.setPayload( data.readAll() );
00044   return true;
00045 }
00046 
00047 void DefaultItemSerializerPlugin::serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
00048 {
00049   Q_ASSERT( label == Item::FullPayload );
00050   Q_UNUSED( label );
00051   if ( item.hasPayload<QByteArray>() )
00052     data.write( item.payload<QByteArray>() );
00053 }
00054 
00055 /*static*/
00056 void ItemSerializer::deserialize( Item& item, const QByteArray& label, const QByteArray& data, int version, bool external )
00057 {
00058   if ( external ) {
00059     QFile file( QString::fromUtf8(data) );
00060     if ( file.open( QIODevice::ReadOnly ) ) {
00061       deserialize( item, label, file, version );
00062       file.close();
00063     }
00064   } else {
00065     QBuffer buffer;
00066     buffer.setData( data );
00067     buffer.open( QIODevice::ReadOnly );
00068     buffer.seek( 0 );
00069     deserialize( item, label, buffer, version );
00070     buffer.close();
00071   }
00072 }
00073 
00074 /*static*/
00075 void ItemSerializer::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
00076 {
00077   if ( !TypePluginLoader::pluginForMimeType( item.mimeType() )->deserialize( item, label, data, version ) ) {
00078     kWarning() << "Unable to deserialize payload part:" << label;
00079     data.seek( 0 );
00080     kWarning() << "Payload data was: " << data.readAll();
00081   }
00082 }
00083 
00084 /*static*/
00085 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QByteArray& data, int &version )
00086 {
00087   QBuffer buffer;
00088   buffer.setBuffer( &data );
00089   buffer.open( QIODevice::WriteOnly );
00090   buffer.seek( 0 );
00091   serialize( item, label, buffer, version );
00092   buffer.close();
00093 }
00094 
00095 /*static*/
00096 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
00097 {
00098   if ( !item.hasPayload() )
00099     return;
00100   ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeType( item.mimeType() );
00101   plugin->serialize( item, label, data, version );
00102 }
00103 
00104 void ItemSerializer::apply( Item &item, const Item &other )
00105 {
00106   if ( !other.hasPayload() )
00107     return;
00108 
00109   ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeType( item.mimeType() );
00110 
00111   ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
00112   if ( pluginV2 ) {
00113     pluginV2->apply( item, other );
00114     return;
00115   }
00116 
00117   // Old-school merge:
00118   foreach ( const QByteArray &part, other.loadedPayloadParts() ) {
00119     QByteArray partData;
00120     QBuffer buffer;
00121     buffer.setBuffer( &partData );
00122     buffer.open( QIODevice::ReadWrite );
00123     buffer.seek( 0 );
00124     int version;
00125     serialize( other, part, buffer, version );
00126     buffer.seek( 0 );
00127     deserialize( item, part, buffer, version );
00128   }
00129 }
00130 
00131 QSet<QByteArray> ItemSerializer::parts( const Item & item )
00132 {
00133   if ( !item.hasPayload() )
00134     return QSet<QByteArray>();
00135   return TypePluginLoader::pluginForMimeType( item.mimeType() )->parts( item );
00136 }
00137 
00138 QSet<QByteArray> ItemSerializer::availableParts( const Item & item )
00139 {
00140   if ( !item.hasPayload() )
00141     return QSet<QByteArray>();
00142   ItemSerializerPlugin *plugin = TypePluginLoader::pluginForMimeType( item.mimeType() );
00143   ItemSerializerPluginV2 *pluginV2 = dynamic_cast<ItemSerializerPluginV2*>( plugin );
00144 
00145   if ( pluginV2 )
00146     return pluginV2->availableParts( item );
00147 
00148   if (item.hasPayload())
00149     return QSet<QByteArray>();
00150 
00151   return QSet<QByteArray>() << Item::FullPayload;
00152 }
00153 
00154 }

akonadi

Skip menu "akonadi"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kblog
  • kcal
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.7.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal