emailservices/nmclientapi/src/nmapidatapluginfactory.cpp
changeset 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
4:e7aa27f58ae1 18:578830873419
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:
       
    15  *
       
    16  */
       
    17 
       
    18 #include "nmapidataplugininterface.h"
       
    19 #include "nmapidatapluginfactory.h"
       
    20 // Qt
       
    21 #include <QList>
       
    22 #include <QPluginLoader>
       
    23 #include <QDir>
       
    24 #include <QApplication>
       
    25 #include <QLocale>
       
    26 
       
    27 // nmailbase
       
    28 #include "nmcommon.h"
       
    29 #include "nmmailbox.h"
       
    30 #include "../../../inc/nmmessageenvelope.h"
       
    31 #include "nmmessage.h"
       
    32 #include "nmmessagepart.h"
       
    33 #include "nmfolder.h"
       
    34 
       
    35 /*!
       
    36  \class NmDataPluginFactory
       
    37  \brief The NmDataPluginFactory class creates NmDataPlugin instance.
       
    38 
       
    39  */
       
    40 NmDataPluginFactory *NmDataPluginFactory::mInstance = NULL;
       
    41 int NmDataPluginFactory::mReferenceCount = 0;
       
    42 QList<QObject*> *NmDataPluginFactory::mPluginArray = NULL;
       
    43 QList<QPluginLoader*> NmDataPluginFactory::mPluginLoaderArray = QList<QPluginLoader*> ();
       
    44 const int INTERFACEPLUGININDEX = 0;
       
    45 
       
    46 /*!
       
    47 
       
    48  */
       
    49 NmDataPluginFactory::NmDataPluginFactory()
       
    50 {
       
    51 
       
    52 }
       
    53 
       
    54 /*!
       
    55 
       
    56  */
       
    57 NmDataPluginFactory::~NmDataPluginFactory()
       
    58 {
       
    59     if (mPluginArray) {
       
    60         qDeleteAll(mPluginArray->begin(), mPluginArray->end());
       
    61         mPluginArray->clear();
       
    62         delete mPluginArray;
       
    63         mPluginArray = NULL;
       
    64     }
       
    65 
       
    66     qDeleteAll(mPluginLoaderArray.constBegin(), mPluginLoaderArray.constEnd());
       
    67     mPluginLoaderArray.clear();
       
    68 }
       
    69 
       
    70 /*!
       
    71 
       
    72  */
       
    73 NmDataPluginFactory *NmDataPluginFactory::instance()
       
    74 {
       
    75     if (!mInstance) {
       
    76         mInstance = new NmDataPluginFactory();
       
    77     }
       
    78     mReferenceCount++;
       
    79     return mInstance;
       
    80 }
       
    81 
       
    82 /*!
       
    83 
       
    84  */
       
    85 void NmDataPluginFactory::releaseInstance(NmDataPluginFactory *&instance)
       
    86 {
       
    87     //can't have passed out instances if we don't have any
       
    88     if (mInstance) {
       
    89         if (instance == mInstance) {
       
    90             instance = NULL;
       
    91             mReferenceCount--;
       
    92         }
       
    93         if (0 >= mReferenceCount) {
       
    94             delete mInstance;
       
    95             mInstance = NULL;
       
    96         }
       
    97     }
       
    98 }
       
    99 
       
   100 /*!
       
   101 
       
   102  */
       
   103 NmDataPluginInterface *NmDataPluginFactory::interfaceInstance(QObject *plugin)
       
   104 {
       
   105     NmDataPluginInterface *pluginInterface = NULL;
       
   106     if (plugin) {
       
   107         pluginInterface = qobject_cast<NmDataPluginInterface*> (plugin);
       
   108     }
       
   109     return pluginInterface;
       
   110 }
       
   111 
       
   112 /*!
       
   113 
       
   114  */
       
   115 NmDataPluginInterface *NmDataPluginFactory::interfaceInstance(NmId mailboxId)
       
   116 {
       
   117     return interfaceInstance(pluginInstance(mailboxId));
       
   118 }
       
   119 
       
   120 /*!
       
   121 
       
   122  */
       
   123 QObject *NmDataPluginFactory::pluginInstance(NmId mailboxId)
       
   124 {
       
   125     Q_UNUSED(mailboxId);
       
   126     return pluginInstances()->at(INTERFACEPLUGININDEX);
       
   127 }
       
   128 
       
   129 /*!
       
   130 
       
   131  */
       
   132 QList<QObject*> *NmDataPluginFactory::pluginInstances()
       
   133 {
       
   134     //if plugins have not been already created, do it now.
       
   135     if (!mPluginArray) {
       
   136         mPluginArray = new QList<QObject*> ();
       
   137     }
       
   138     if (mPluginArray->count() == 0) {
       
   139 #ifdef Q_OS_SYMBIAN
       
   140 
       
   141         const QString KPluginDirectory = "c:\\resource\\plugins";
       
   142         QDir pluginDir = QDir(KPluginDirectory);
       
   143         const QString KFrameworkPluginName = "nmframeworkadapter.qtplugin";
       
   144         QObject *frameworkPlugin = loadPlugin(pluginDir, KFrameworkPluginName);
       
   145         mPluginArray->append(frameworkPlugin);
       
   146 
       
   147 #endif
       
   148     }
       
   149 
       
   150     return mPluginArray;
       
   151 }
       
   152 
       
   153 /*!
       
   154 
       
   155  */
       
   156 QObject *NmDataPluginFactory::loadPlugin(const QDir& pluginDir, const QString& pluginName)
       
   157 {
       
   158     /*!
       
   159      This creates plugin entity.
       
   160      */
       
   161     QPluginLoader *loader = new QPluginLoader(pluginDir.absoluteFilePath(pluginName));
       
   162 
       
   163     QObject *pluginInstance = loader->instance();
       
   164 
       
   165     if (pluginInstance) {
       
   166         // Store QPluginLoader instances to keep plugins alive.
       
   167         // If QPluginLoader instance for a plugin is deleted, then there
       
   168         // is a risk that some component outside will load and unload
       
   169         // plugin, which will lead into situation where plugin is deleted.
       
   170         // This means that instance in mPluginArray will be invalid.
       
   171         mPluginLoaderArray.append(loader);
       
   172     }
       
   173     else {
       
   174         // We don't have proper plugin instance, so we don't need it's loader.
       
   175         delete loader;
       
   176         loader = NULL;
       
   177     }
       
   178     return pluginInstance;
       
   179 }