screensavermodel/src/screensaverfactory.cpp
author hgs
Fri, 16 Apr 2010 14:45:49 +0300
changeset 92 782e3408c2ab
parent 85 7feec50967db
permissions -rw-r--r--
201015
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:  Screensaver 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 "screensaverfactory.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    24
#include "screensaverfactory_p.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    25
#include "screensaver.h"
7feec50967db 201003_02
hgs
parents:
diff changeset
    26
7feec50967db 201003_02
hgs
parents:
diff changeset
    27
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    28
    \class ScreensaverPluginUnloader
7feec50967db 201003_02
hgs
parents:
diff changeset
    29
    \brief Unloads plugin and deletes plugin loader.
7feec50967db 201003_02
hgs
parents:
diff changeset
    30
    
7feec50967db 201003_02
hgs
parents:
diff changeset
    31
    Holds plugin loader instance and unloads and destroys plugin
7feec50967db 201003_02
hgs
parents:
diff changeset
    32
    on it's destructor.
7feec50967db 201003_02
hgs
parents:
diff changeset
    33
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    34
7feec50967db 201003_02
hgs
parents:
diff changeset
    35
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
    36
    Constructs a new SnsrPluginUnloader with \a pluginLoader and \a parent.
7feec50967db 201003_02
hgs
parents:
diff changeset
    37
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    38
ScreensaverPluginUnloader::ScreensaverPluginUnloader(
7feec50967db 201003_02
hgs
parents:
diff changeset
    39
            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
ScreensaverPluginUnloader::~ScreensaverPluginUnloader()
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 ScreensaverFactoryPrivate
7feec50967db 201003_02
hgs
parents:
diff changeset
    57
    \brief The private implementation of the ScreensaverFactory.
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 ScreensaverFactoryPrivate with \a pluginDirectory and \a screensaverFactoryPublic.
7feec50967db 201003_02
hgs
parents:
diff changeset
    62
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    63
ScreensaverFactoryPrivate::ScreensaverFactoryPrivate(const QString& pluginDirectory,
7feec50967db 201003_02
hgs
parents:
diff changeset
    64
                                    ScreensaverFactory *screensaverFactoryPublic) :
7feec50967db 201003_02
hgs
parents:
diff changeset
    65
    m_q(screensaverFactoryPublic), 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
ScreensaverFactoryPrivate::~ScreensaverFactoryPrivate()
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 screensaver on the given token.
7feec50967db 201003_02
hgs
parents:
diff changeset
    78
    \param token Identifies the screensaver to be created.
7feec50967db 201003_02
hgs
parents:
diff changeset
    79
    \return The created state.
7feec50967db 201003_02
hgs
parents:
diff changeset
    80
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
    81
Screensaver* ScreensaverFactoryPrivate::createScreensaver(const ScreensaverToken &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
    IScreensaverProvider *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<IScreensaverProvider*>(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
    Screensaver *screensaver(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
        screensaver = provider->createScreensaver(token);
7feec50967db 201003_02
hgs
parents:
diff changeset
   122
        if (!screensaver) {
7feec50967db 201003_02
hgs
parents:
diff changeset
   123
            qWarning() << "Screensaver 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 screensaver gets deleted
7feec50967db 201003_02
hgs
parents:
diff changeset
   129
            ScreensaverPluginUnloader *unloader = new ScreensaverPluginUnloader(loader);
7feec50967db 201003_02
hgs
parents:
diff changeset
   130
            unloader->connect(screensaver, 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() << "Screensaver 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 screensaver;
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 ScreensaverFactory
7feec50967db 201003_02
hgs
parents:
diff changeset
   144
    \brief Finds and creates screensavers.
7feec50967db 201003_02
hgs
parents:
diff changeset
   145
7feec50967db 201003_02
hgs
parents:
diff changeset
   146
    Screensaver factory creates an instance of a screensaver
7feec50967db 201003_02
hgs
parents:
diff changeset
   147
    on a screensaver token 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 ScreensaverFactory with \a pluginDirectory and \a parent.
7feec50967db 201003_02
hgs
parents:
diff changeset
   152
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   153
ScreensaverFactory::ScreensaverFactory(const QString& pluginDirectory, 
7feec50967db 201003_02
hgs
parents:
diff changeset
   154
                                        QObject *parent) :
7feec50967db 201003_02
hgs
parents:
diff changeset
   155
    QObject(parent), m_d(new ScreensaverFactoryPrivate(pluginDirectory, this))
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
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   160
    Destructs the class.
7feec50967db 201003_02
hgs
parents:
diff changeset
   161
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   162
ScreensaverFactory::~ScreensaverFactory()
7feec50967db 201003_02
hgs
parents:
diff changeset
   163
{
7feec50967db 201003_02
hgs
parents:
diff changeset
   164
    delete m_d;
7feec50967db 201003_02
hgs
parents:
diff changeset
   165
}
7feec50967db 201003_02
hgs
parents:
diff changeset
   166
7feec50967db 201003_02
hgs
parents:
diff changeset
   167
/*!
7feec50967db 201003_02
hgs
parents:
diff changeset
   168
    Creates and returns a screensaver on the given token.
7feec50967db 201003_02
hgs
parents:
diff changeset
   169
    \param token Identifies the screensaver to be created.
7feec50967db 201003_02
hgs
parents:
diff changeset
   170
    \return The created screensaver.
7feec50967db 201003_02
hgs
parents:
diff changeset
   171
 */
7feec50967db 201003_02
hgs
parents:
diff changeset
   172
Screensaver* ScreensaverFactory::createScreensaver(const ScreensaverToken &token)
7feec50967db 201003_02
hgs
parents:
diff changeset
   173
{
7feec50967db 201003_02
hgs
parents:
diff changeset
   174
    return m_d->createScreensaver(token);
7feec50967db 201003_02
hgs
parents:
diff changeset
   175
}