bagetmodel/src/bagetfactory.cpp
author hgs
Wed, 24 Mar 2010 13:06:03 +0200
changeset 90 0e41c72311af
parent 85 7feec50967db
permissions -rw-r--r--
201011_02
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
85
7feec50967db 201003_02
hgs
parents:
diff changeset
     1
/*
7feec50967db 201003_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7feec50967db 201003_02
hgs
parents:
diff changeset
     3
* All rights reserved.
7feec50967db 201003_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
7feec50967db 201003_02
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
7feec50967db 201003_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
7feec50967db 201003_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
7feec50967db 201003_02
hgs
parents:
diff changeset
     8
*
7feec50967db 201003_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
7feec50967db 201003_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
7feec50967db 201003_02
hgs
parents:
diff changeset
    11
*
7feec50967db 201003_02
hgs
parents:
diff changeset
    12
* Contributors:
7feec50967db 201003_02
hgs
parents:
diff changeset
    13
* 
7feec50967db 201003_02
hgs
parents:
diff changeset
    14
* Description:  Widget factory.
7feec50967db 201003_02
hgs
parents:
diff changeset
    15
*
7feec50967db 201003_02
hgs
parents:
diff changeset
    16
*/
7feec50967db 201003_02
hgs
parents:
diff changeset
    17
7feec50967db 201003_02
hgs
parents:
diff changeset
    18
#include <QDebug>
7feec50967db 201003_02
hgs
parents:
diff changeset
    19
#include <QStringList>
7feec50967db 201003_02
hgs
parents:
diff changeset
    20
#include <QDir>
7feec50967db 201003_02
hgs
parents:
diff changeset
    21
#include <QPluginLoader>
7feec50967db 201003_02
hgs
parents:
diff changeset
    22
7feec50967db 201003_02
hgs
parents:
diff changeset
    23
#include "bagetfactory.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    24
#include "bagetfactory_p.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    25
#include "baget.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    26
#include "ibagetprovider.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    27
7feec50967db 201003_02
hgs
parents:
diff changeset
    28
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    29
    \class BagetPluginUnloader
7feec50967db 201003_02
hgs
parents:
diff changeset
    30
    \brief Unloads plugin and deletes plugin loader.
7feec50967db 201003_02
hgs
parents:
diff changeset
    31
    
7feec50967db 201003_02
hgs
parents:
diff changeset
    32
    Holds plugin loader instance and unloads and destroys plugin
7feec50967db 201003_02
hgs
parents:
diff changeset
    33
    on it's destructor.
7feec50967db 201003_02
hgs
parents:
diff changeset
    34
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    35
7feec50967db 201003_02
hgs
parents:
diff changeset
    36
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    37
    Constructs a new BagetPluginUnloader with \a pluginLoader and \a parent.
7feec50967db 201003_02
hgs
parents:
diff changeset
    38
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    39
BagetPluginUnloader::BagetPluginUnloader(QPluginLoader *pluginLoader, QObject *parent) :
7feec50967db 201003_02
hgs
parents:
diff changeset
    40
    QObject(parent), mPluginLoader(pluginLoader)
7feec50967db 201003_02
hgs
parents:
diff changeset
    41
{
7feec50967db 201003_02
hgs
parents:
diff changeset
    42
}
7feec50967db 201003_02
hgs
parents:
diff changeset
    43
7feec50967db 201003_02
hgs
parents:
diff changeset
    44
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    45
    Destructs the class.
7feec50967db 201003_02
hgs
parents:
diff changeset
    46
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    47
BagetPluginUnloader::~BagetPluginUnloader()
7feec50967db 201003_02
hgs
parents:
diff changeset
    48
{
7feec50967db 201003_02
hgs
parents:
diff changeset
    49
    if (mPluginLoader) {
7feec50967db 201003_02
hgs
parents:
diff changeset
    50
        mPluginLoader->unload();
7feec50967db 201003_02
hgs
parents:
diff changeset
    51
        delete mPluginLoader;
7feec50967db 201003_02
hgs
parents:
diff changeset
    52
    }
7feec50967db 201003_02
hgs
parents:
diff changeset
    53
}
7feec50967db 201003_02
hgs
parents:
diff changeset
    54
7feec50967db 201003_02
hgs
parents:
diff changeset
    55
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    56
    \class BagetFactoryPrivate
7feec50967db 201003_02
hgs
parents:
diff changeset
    57
    \brief Private implementation of the BagetFactory.
7feec50967db 201003_02
hgs
parents:
diff changeset
    58
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    59
7feec50967db 201003_02
hgs
parents:
diff changeset
    60
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    61
    Constructs a new BagetFactoryPrivate with \a pluginDirectory and \a bagetFactoryPublic.
7feec50967db 201003_02
hgs
parents:
diff changeset
    62
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    63
BagetFactoryPrivate::BagetFactoryPrivate(const QString& pluginDirectory, 
7feec50967db 201003_02
hgs
parents:
diff changeset
    64
                                         BagetFactory *bagetFactoryPublic) :
7feec50967db 201003_02
hgs
parents:
diff changeset
    65
    m_q(bagetFactoryPublic), mPluginDirectory(pluginDirectory)
7feec50967db 201003_02
hgs
parents:
diff changeset
    66
{
7feec50967db 201003_02
hgs
parents:
diff changeset
    67
}
7feec50967db 201003_02
hgs
parents:
diff changeset
    68
7feec50967db 201003_02
hgs
parents:
diff changeset
    69
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    70
    Destructs the class.
7feec50967db 201003_02
hgs
parents:
diff changeset
    71
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    72
BagetFactoryPrivate::~BagetFactoryPrivate()
7feec50967db 201003_02
hgs
parents:
diff changeset
    73
{
7feec50967db 201003_02
hgs
parents:
diff changeset
    74
}
7feec50967db 201003_02
hgs
parents:
diff changeset
    75
7feec50967db 201003_02
hgs
parents:
diff changeset
    76
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    77
    Creates and returns a Baget based on the given token.
7feec50967db 201003_02
hgs
parents:
diff changeset
    78
    \param token Identifies the Baget to be created.
7feec50967db 201003_02
hgs
parents:
diff changeset
    79
    \return The created Baget.
7feec50967db 201003_02
hgs
parents:
diff changeset
    80
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    81
Baget *BagetFactoryPrivate::createBaget(const BagetToken &token)
7feec50967db 201003_02
hgs
parents:
diff changeset
    82
{
7feec50967db 201003_02
hgs
parents:
diff changeset
    83
    QStringList pluginPaths;
7feec50967db 201003_02
hgs
parents:
diff changeset
    84
7feec50967db 201003_02
hgs
parents:
diff changeset
    85
    // check plugin dirs from root of different drives
7feec50967db 201003_02
hgs
parents:
diff changeset
    86
    QFileInfoList drives = QDir::drives();
7feec50967db 201003_02
hgs
parents:
diff changeset
    87
    for(int i=0; i < drives.count(); i++) {
7feec50967db 201003_02
hgs
parents:
diff changeset
    88
        QFileInfo drive = drives.at(i);
7feec50967db 201003_02
hgs
parents:
diff changeset
    89
        QString driveLetter = drive.absolutePath();
7feec50967db 201003_02
hgs
parents:
diff changeset
    90
        QString path = driveLetter + mPluginDirectory;
7feec50967db 201003_02
hgs
parents:
diff changeset
    91
        if (QDir(path).exists()) {
7feec50967db 201003_02
hgs
parents:
diff changeset
    92
            pluginPaths << path;
7feec50967db 201003_02
hgs
parents:
diff changeset
    93
        }
7feec50967db 201003_02
hgs
parents:
diff changeset
    94
    }
7feec50967db 201003_02
hgs
parents:
diff changeset
    95
7feec50967db 201003_02
hgs
parents:
diff changeset
    96
    // check plugin dir relative to current dir
7feec50967db 201003_02
hgs
parents:
diff changeset
    97
    if (QDir(mPluginDirectory).exists() && 
7feec50967db 201003_02
hgs
parents:
diff changeset
    98
        !pluginPaths.contains(QDir(mPluginDirectory).absolutePath())) {
7feec50967db 201003_02
hgs
parents:
diff changeset
    99
        pluginPaths << mPluginDirectory;
7feec50967db 201003_02
hgs
parents:
diff changeset
   100
    }
7feec50967db 201003_02
hgs
parents:
diff changeset
   101
7feec50967db 201003_02
hgs
parents:
diff changeset
   102
    IBagetProvider *provider(0);
7feec50967db 201003_02
hgs
parents:
diff changeset
   103
    QPluginLoader *loader = new QPluginLoader();
7feec50967db 201003_02
hgs
parents:
diff changeset
   104
    QObject *plugin(0);
7feec50967db 201003_02
hgs
parents:
diff changeset
   105
7feec50967db 201003_02
hgs
parents:
diff changeset
   106
    for(int i=0; i < pluginPaths.count(); i++) {
7feec50967db 201003_02
hgs
parents:
diff changeset
   107
        QString path = pluginPaths.at(i);
7feec50967db 201003_02
hgs
parents:
diff changeset
   108
        QString fileName = QDir(path).absoluteFilePath(token.mLibrary);
7feec50967db 201003_02
hgs
parents:
diff changeset
   109
7feec50967db 201003_02
hgs
parents:
diff changeset
   110
        loader->setFileName(fileName);
7feec50967db 201003_02
hgs
parents:
diff changeset
   111
        plugin = loader->instance();
7feec50967db 201003_02
hgs
parents:
diff changeset
   112
        provider = qobject_cast<IBagetProvider*>(plugin);
7feec50967db 201003_02
hgs
parents:
diff changeset
   113
        if (provider) {
7feec50967db 201003_02
hgs
parents:
diff changeset
   114
            break;
7feec50967db 201003_02
hgs
parents:
diff changeset
   115
        }
7feec50967db 201003_02
hgs
parents:
diff changeset
   116
    }
7feec50967db 201003_02
hgs
parents:
diff changeset
   117
7feec50967db 201003_02
hgs
parents:
diff changeset
   118
    Baget *baget(0);
7feec50967db 201003_02
hgs
parents:
diff changeset
   119
7feec50967db 201003_02
hgs
parents:
diff changeset
   120
    if (provider) {
7feec50967db 201003_02
hgs
parents:
diff changeset
   121
        baget = provider->createBaget(token);
7feec50967db 201003_02
hgs
parents:
diff changeset
   122
        if (!baget) {
7feec50967db 201003_02
hgs
parents:
diff changeset
   123
            qWarning() << "Baget creation failed.";
7feec50967db 201003_02
hgs
parents:
diff changeset
   124
            qWarning() << token.mLibrary << "cannot provide" << token.mUri;
7feec50967db 201003_02
hgs
parents:
diff changeset
   125
            loader->unload();
7feec50967db 201003_02
hgs
parents:
diff changeset
   126
            delete loader;
7feec50967db 201003_02
hgs
parents:
diff changeset
   127
        } else {
7feec50967db 201003_02
hgs
parents:
diff changeset
   128
            // unload plugin once baget gets deleted
7feec50967db 201003_02
hgs
parents:
diff changeset
   129
            BagetPluginUnloader *unloader = new BagetPluginUnloader(loader);
7feec50967db 201003_02
hgs
parents:
diff changeset
   130
            unloader->connect(baget, SIGNAL(destroyed()), SLOT(deleteLater()));
7feec50967db 201003_02
hgs
parents:
diff changeset
   131
        }
7feec50967db 201003_02
hgs
parents:
diff changeset
   132
    } else {
7feec50967db 201003_02
hgs
parents:
diff changeset
   133
        qDebug() << "Baget creation failed.";
7feec50967db 201003_02
hgs
parents:
diff changeset
   134
        qWarning() << token.mLibrary << "- provider not found";
7feec50967db 201003_02
hgs
parents:
diff changeset
   135
        loader->unload();
7feec50967db 201003_02
hgs
parents:
diff changeset
   136
        delete loader;
7feec50967db 201003_02
hgs
parents:
diff changeset
   137
    }
7feec50967db 201003_02
hgs
parents:
diff changeset
   138
7feec50967db 201003_02
hgs
parents:
diff changeset
   139
    return baget;
7feec50967db 201003_02
hgs
parents:
diff changeset
   140
}
7feec50967db 201003_02
hgs
parents:
diff changeset
   141
7feec50967db 201003_02
hgs
parents:
diff changeset
   142
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   143
    \class BagetFactory
7feec50967db 201003_02
hgs
parents:
diff changeset
   144
    \brief Finds and creates baget widgets.
7feec50967db 201003_02
hgs
parents:
diff changeset
   145
7feec50967db 201003_02
hgs
parents:
diff changeset
   146
    Baget factory creates an instance of a Baget
7feec50967db 201003_02
hgs
parents:
diff changeset
   147
    based on a BagetToken that is given to it.
7feec50967db 201003_02
hgs
parents:
diff changeset
   148
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   149
7feec50967db 201003_02
hgs
parents:
diff changeset
   150
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   151
    Constructs a new BagetFactory with \a pluginDirectory and \a parent.
7feec50967db 201003_02
hgs
parents:
diff changeset
   152
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   153
BagetFactory::BagetFactory(const QString& pluginDirectory, QObject *parent) :
7feec50967db 201003_02
hgs
parents:
diff changeset
   154
    QObject(parent), m_d(new BagetFactoryPrivate(pluginDirectory, this))
7feec50967db 201003_02
hgs
parents:
diff changeset
   155
{
7feec50967db 201003_02
hgs
parents:
diff changeset
   156
}
7feec50967db 201003_02
hgs
parents:
diff changeset
   157
7feec50967db 201003_02
hgs
parents:
diff changeset
   158
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   159
    Destructs the class.
7feec50967db 201003_02
hgs
parents:
diff changeset
   160
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   161
BagetFactory::~BagetFactory()
7feec50967db 201003_02
hgs
parents:
diff changeset
   162
{
7feec50967db 201003_02
hgs
parents:
diff changeset
   163
    delete m_d;
7feec50967db 201003_02
hgs
parents:
diff changeset
   164
}
7feec50967db 201003_02
hgs
parents:
diff changeset
   165
7feec50967db 201003_02
hgs
parents:
diff changeset
   166
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   167
    Creates and returns a Baget based on the given token.
7feec50967db 201003_02
hgs
parents:
diff changeset
   168
    \param token Identifies the Baget to be created.
7feec50967db 201003_02
hgs
parents:
diff changeset
   169
    \return The created Baget.
7feec50967db 201003_02
hgs
parents:
diff changeset
   170
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   171
Baget* BagetFactory::createBaget(const BagetToken &token)
7feec50967db 201003_02
hgs
parents:
diff changeset
   172
{
7feec50967db 201003_02
hgs
parents:
diff changeset
   173
    return m_d->createBaget(token);
7feec50967db 201003_02
hgs
parents:
diff changeset
   174
}