• Skip to content
  • Skip to link menu
KDE 4.1 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.h"
00022 #include "item.h"
00023 #include "itemserializerplugin.h"
00024 #include "attributefactory.h"
00025 
00026 // KDE core
00027 #include <kdebug.h>
00028 #include <kmimetype.h>
00029 #include <kglobal.h>
00030 
00031 // Qt
00032 #include <QtCore/QBuffer>
00033 #include <QtCore/QIODevice>
00034 #include <QtCore/QHash>
00035 #include <QtCore/QString>
00036 #include <QtCore/QStringList>
00037 
00038 // temporary
00039 #include "pluginloader.h"
00040 
00041 
00042 
00043 namespace Akonadi {
00044 
00045 class DefaultItemSerializerPlugin;
00046 
00047 class DefaultItemSerializerPlugin : public ItemSerializerPlugin
00048 {
00049 public:
00050     DefaultItemSerializerPlugin() { }
00051 
00052     bool deserialize( Item& item, const QByteArray& label, QIODevice& data, int )
00053     {
00054         if ( label != Item::FullPayload )
00055           return false;
00056         item.setPayload( data.readAll() );
00057         return true;
00058     }
00059 
00060     void serialize( const Item& item, const QByteArray& label, QIODevice& data, int& )
00061     {
00062         Q_ASSERT( label == Item::FullPayload );
00063         if ( item.hasPayload<QByteArray>() )
00064             data.write( item.payload<QByteArray>() );
00065     }
00066 
00067 };
00068 
00069 K_GLOBAL_STATIC( DefaultItemSerializerPlugin, s_defaultItemSerializerPlugin )
00070 
00071 }
00072 
00073 
00074 using namespace Akonadi;
00075 
00076 class PluginEntry
00077 {
00078   public:
00079     PluginEntry()
00080       : mPlugin( 0 )
00081     {
00082     }
00083 
00084     PluginEntry( const QString &identifier )
00085       : mIdentifier( identifier ), mPlugin( 0 )
00086     {
00087     }
00088 
00089     inline ItemSerializerPlugin* plugin() const
00090     {
00091       if ( mPlugin )
00092         return mPlugin;
00093 
00094       QObject *object = PluginLoader::self()->createForName( mIdentifier );
00095       if ( !object ) {
00096         kWarning( 5250 ) << "ItemSerializerPluginLoader: "
00097                          << "plugin" << mIdentifier << "is not valid!" << endl;
00098 
00099         // we try to use the default in that case
00100         mPlugin = s_defaultItemSerializerPlugin;
00101       }
00102 
00103       mPlugin = qobject_cast<ItemSerializerPlugin*>( object );
00104       if ( !mPlugin ) {
00105         kWarning( 5250 ) << "ItemSerializerPluginLoader: "
00106                          << "plugin" << mIdentifier << "doesn't provide interface ItemSerializerPlugin!" << endl;
00107 
00108         // we try to use the default in that case
00109         mPlugin = s_defaultItemSerializerPlugin;
00110       }
00111 
00112       Q_ASSERT( mPlugin );
00113 
00114       return mPlugin;
00115     }
00116 
00117   private:
00118     QString mIdentifier;
00119     mutable ItemSerializerPlugin *mPlugin;
00120 };
00121 
00122 static QHash<QString, PluginEntry> * all = 0;
00123 
00124 static void loadPlugins() {
00125   const PluginLoader* pl = PluginLoader::self();
00126   if ( !pl ) {
00127     kWarning( 5250 ) << "Cannot instantiate plugin loader!" << endl;
00128     return;
00129   }
00130   const QStringList types = pl->types();
00131   kDebug( 5250 ) << "ItemSerializerPluginLoader: "
00132                  << "found" << types.size() << "plugins." << endl;
00133   for ( QStringList::const_iterator it = types.begin() ; it != types.end() ; ++it ) {
00134     all->insert( *it, PluginEntry( *it ) );
00135   }
00136 }
00137 
00138 static void setup()
00139 {
00140     if (!all) {
00141         all = new QHash<QString, PluginEntry>();
00142         loadPlugins();
00143     }
00144 }
00145 
00146 /*static*/
00147 void ItemSerializer::deserialize( Item& item, const QByteArray& label, const QByteArray& data, int version )
00148 {
00149     QBuffer buffer;
00150     buffer.setData( data );
00151     buffer.open( QIODevice::ReadOnly );
00152     buffer.seek( 0 );
00153     deserialize( item, label, buffer, version );
00154     buffer.close();
00155 }
00156 
00157 /*static*/
00158 void ItemSerializer::deserialize( Item& item, const QByteArray& label, QIODevice& data, int version )
00159 {
00160   setup();
00161   if ( !ItemSerializer::pluginForMimeType( item.mimeType() ).deserialize( item, label, data, version ) )
00162     kWarning() << "Unable to deserialize payload part:" << label;
00163 }
00164 
00165 /*static*/
00166 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QByteArray& data, int &version )
00167 {
00168     QBuffer buffer;
00169     buffer.setBuffer( &data );
00170     buffer.open( QIODevice::WriteOnly );
00171     buffer.seek( 0 );
00172     serialize( item, label, buffer, version );
00173     buffer.close();
00174 }
00175 
00176 /*static*/
00177 void ItemSerializer::serialize( const Item& item, const QByteArray& label, QIODevice& data, int &version )
00178 {
00179   if ( !item.hasPayload() )
00180     return;
00181   setup();
00182   ItemSerializerPlugin& plugin = pluginForMimeType( item.mimeType() );
00183   plugin.serialize( item, label, data, version );
00184 }
00185 
00186 QSet<QByteArray> ItemSerializer::parts(const Item & item)
00187 {
00188   if ( !item.hasPayload() )
00189     return QSet<QByteArray>();
00190   setup();
00191   return pluginForMimeType( item.mimeType() ).parts( item );
00192 }
00193 
00194 /*static*/
00195 ItemSerializerPlugin& ItemSerializer::pluginForMimeType( const QString & mimetype )
00196 {
00197     if ( all->contains( mimetype ) )
00198         return *(all->value( mimetype ).plugin());
00199 
00200     KMimeType::Ptr mimeType = KMimeType::mimeType( mimetype, KMimeType::ResolveAliases );
00201     if ( !mimeType.isNull() ) {
00202       foreach ( const QString &type, all->keys() ) {
00203         if ( mimeType->is( type ) ) {
00204           return *(all->value( type ).plugin() );
00205         }
00206       }
00207     }
00208 
00209     kDebug( 5250 ) << "No plugin for mimetype " << mimetype << " found!";
00210     kDebug( 5250 ) << "Available plugins are: " << all->keys();
00211 
00212     ItemSerializerPlugin *plugin = s_defaultItemSerializerPlugin;
00213     Q_ASSERT(plugin);
00214     return *plugin;
00215 }

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
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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