bagetmodel/src/bagetfactory.cpp
changeset 66 32469d7d46ff
parent 61 8e5041d13c84
child 73 4bc7b118b3df
equal deleted inserted replaced
61:8e5041d13c84 66:32469d7d46ff
     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:  Widget factory.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QStringList>
       
    20 #include <QDir>
       
    21 #include <QPluginLoader>
       
    22 
       
    23 #include "bagetfactory.h"
       
    24 #include "bagetfactory_p.h"
       
    25 #include "baget.h"
       
    26 #include "ibagetprovider.h"
       
    27 
       
    28 /*!
       
    29     \class BagetPluginUnloader
       
    30     \brief Unloads plugin and deletes plugin loader.
       
    31     
       
    32     Holds plugin loader instance and unloads and destroys plugin
       
    33     on it's destructor.
       
    34  */
       
    35 
       
    36 /*!
       
    37     Constructs a new BagetPluginUnloader with \a pluginLoader and \a parent.
       
    38  */
       
    39 BagetPluginUnloader::BagetPluginUnloader(QPluginLoader *pluginLoader, QObject *parent) :
       
    40     QObject(parent), mPluginLoader(pluginLoader)
       
    41 {
       
    42 }
       
    43 
       
    44 /*!
       
    45     Destructs the class.
       
    46  */
       
    47 BagetPluginUnloader::~BagetPluginUnloader()
       
    48 {
       
    49     if (mPluginLoader) {
       
    50         mPluginLoader->unload();
       
    51         delete mPluginLoader;
       
    52     }
       
    53 }
       
    54 
       
    55 /*!
       
    56     \class BagetFactoryPrivate
       
    57     \brief Private implementation of the BagetFactory.
       
    58  */
       
    59 
       
    60 /*!
       
    61     Constructs a new BagetFactoryPrivate with \a pluginDirectory and \a bagetFactoryPublic.
       
    62  */
       
    63 BagetFactoryPrivate::BagetFactoryPrivate(const QString& pluginDirectory, 
       
    64                                          BagetFactory *bagetFactoryPublic) :
       
    65     m_q(bagetFactoryPublic), mPluginDirectory(pluginDirectory)
       
    66 {
       
    67 }
       
    68 
       
    69 /*!
       
    70     Destructs the class.
       
    71  */
       
    72 BagetFactoryPrivate::~BagetFactoryPrivate()
       
    73 {
       
    74 }
       
    75 
       
    76 /*!
       
    77     Creates and returns a Baget based on the given token.
       
    78     \param token Identifies the Baget to be created.
       
    79     \return The created Baget.
       
    80  */
       
    81 Baget *BagetFactoryPrivate::createBaget(const BagetToken &token)
       
    82 {
       
    83     QStringList pluginPaths;
       
    84 
       
    85     // check plugin dirs from root of different drives
       
    86     QFileInfoList drives = QDir::drives();
       
    87     for(int i=0; i < drives.count(); i++) {
       
    88         QFileInfo drive = drives.at(i);
       
    89         QString driveLetter = drive.absolutePath();
       
    90         QString path = driveLetter + mPluginDirectory;
       
    91         if (QDir(path).exists()) {
       
    92             pluginPaths << path;
       
    93         }
       
    94     }
       
    95 
       
    96     // check plugin dir relative to current dir
       
    97     if (QDir(mPluginDirectory).exists() && 
       
    98         !pluginPaths.contains(QDir(mPluginDirectory).absolutePath())) {
       
    99         pluginPaths << mPluginDirectory;
       
   100     }
       
   101 
       
   102     IBagetProvider *provider(0);
       
   103     QPluginLoader *loader = new QPluginLoader();
       
   104     QObject *plugin(0);
       
   105 
       
   106     for(int i=0; i < pluginPaths.count(); i++) {
       
   107         QString path = pluginPaths.at(i);
       
   108         QString fileName = QDir(path).absoluteFilePath(token.mLibrary);
       
   109 
       
   110         loader->setFileName(fileName);
       
   111         plugin = loader->instance();
       
   112         provider = qobject_cast<IBagetProvider*>(plugin);
       
   113         if (provider) {
       
   114             break;
       
   115         }
       
   116     }
       
   117 
       
   118     Baget *baget(0);
       
   119 
       
   120     if (provider) {
       
   121         baget = provider->createBaget(token);
       
   122         if (!baget) {
       
   123             qWarning() << "Baget creation failed.";
       
   124             qWarning() << token.mLibrary << "cannot provide" << token.mUri;
       
   125             loader->unload();
       
   126             delete loader;
       
   127         } else {
       
   128             // unload plugin once baget gets deleted
       
   129             BagetPluginUnloader *unloader = new BagetPluginUnloader(loader);
       
   130             unloader->connect(baget, SIGNAL(destroyed()), SLOT(deleteLater()));
       
   131         }
       
   132     } else {
       
   133         qDebug() << "Baget creation failed.";
       
   134         qWarning() << token.mLibrary << "- provider not found";
       
   135         loader->unload();
       
   136         delete loader;
       
   137     }
       
   138 
       
   139     return baget;
       
   140 }
       
   141 
       
   142 /*!
       
   143     \class BagetFactory
       
   144     \brief Finds and creates baget widgets.
       
   145 
       
   146     Baget factory creates an instance of a Baget
       
   147     based on a BagetToken that is given to it.
       
   148  */
       
   149 
       
   150 /*!
       
   151     Constructs a new BagetFactory with \a pluginDirectory and \a parent.
       
   152  */
       
   153 BagetFactory::BagetFactory(const QString& pluginDirectory, QObject *parent) :
       
   154     QObject(parent), m_d(new BagetFactoryPrivate(pluginDirectory, this))
       
   155 {
       
   156 }
       
   157 
       
   158 /*!
       
   159     Destructs the class.
       
   160  */
       
   161 BagetFactory::~BagetFactory()
       
   162 {
       
   163     delete m_d;
       
   164 }
       
   165 
       
   166 /*!
       
   167     Creates and returns a Baget based on the given token.
       
   168     \param token Identifies the Baget to be created.
       
   169     \return The created Baget.
       
   170  */
       
   171 Baget* BagetFactory::createBaget(const BagetToken &token)
       
   172 {
       
   173     return m_d->createBaget(token);
       
   174 }