00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "itemserializer_p.h"
00022 #include "item.h"
00023 #include "itemserializerplugin.h"
00024 #include "typepluginloader_p.h"
00025
00026
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
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
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
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
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
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 }