screensavermodel/src/screensaverfactory.cpp
changeset 73 4bc7b118b3df
parent 66 32469d7d46ff
child 80 397d00875918
child 81 5ef31a21fdd5
equal deleted inserted replaced
66:32469d7d46ff 73:4bc7b118b3df
     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:  Screensaver factory.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QStringList>
       
    20 #include <QDir>
       
    21 #include <QPluginLoader>
       
    22 
       
    23 #include "screensaverfactory.h"
       
    24 #include "screensaverfactory_p.h"
       
    25 #include "screensaver.h"
       
    26 
       
    27 /*!
       
    28     \class ScreensaverPluginUnloader
       
    29     \brief Unloads plugin and deletes plugin loader.
       
    30     
       
    31     Holds plugin loader instance and unloads and destroys plugin
       
    32     on it's destructor.
       
    33  */
       
    34 
       
    35 /*!
       
    36     Constructs a new SnsrPluginUnloader with \a pluginLoader and \a parent.
       
    37  */
       
    38 ScreensaverPluginUnloader::ScreensaverPluginUnloader(
       
    39             QPluginLoader *pluginLoader, QObject *parent) :
       
    40     QObject(parent), mPluginLoader(pluginLoader)
       
    41 {
       
    42 }
       
    43 
       
    44 /*!
       
    45     Destructs the class.
       
    46  */
       
    47 ScreensaverPluginUnloader::~ScreensaverPluginUnloader()
       
    48 {
       
    49     if (mPluginLoader) {
       
    50         mPluginLoader->unload();
       
    51         delete mPluginLoader;
       
    52     }
       
    53 }
       
    54 
       
    55 /*!
       
    56     \class ScreensaverFactoryPrivate
       
    57     \brief The private implementation of the ScreensaverFactory.
       
    58  */
       
    59 
       
    60 /*!
       
    61     Constructs a new ScreensaverFactoryPrivate with \a pluginDirectory and \a screensaverFactoryPublic.
       
    62  */
       
    63 ScreensaverFactoryPrivate::ScreensaverFactoryPrivate(const QString& pluginDirectory,
       
    64                                     ScreensaverFactory *screensaverFactoryPublic) :
       
    65     m_q(screensaverFactoryPublic), mPluginDirectory(pluginDirectory)
       
    66 {
       
    67 }
       
    68 
       
    69 /*!
       
    70     Destructs the class.
       
    71  */
       
    72 ScreensaverFactoryPrivate::~ScreensaverFactoryPrivate()
       
    73 {
       
    74 }
       
    75 
       
    76 /*!
       
    77     Creates and returns a screensaver on the given token.
       
    78     \param token Identifies the screensaver to be created.
       
    79     \return The created state.
       
    80  */
       
    81 Screensaver* ScreensaverFactoryPrivate::createScreensaver(const ScreensaverToken &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     IScreensaverProvider *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<IScreensaverProvider*>(plugin);
       
   113         if (provider) {
       
   114             break;
       
   115         }
       
   116     }
       
   117 
       
   118     Screensaver *screensaver(0);
       
   119 
       
   120     if (provider) {
       
   121         screensaver = provider->createScreensaver(token);
       
   122         if (!screensaver) {
       
   123             qWarning() << "Screensaver creation failed.";
       
   124             qWarning() << token.mLibrary << "cannot provide" << token.mUri;
       
   125             loader->unload();
       
   126             delete loader;
       
   127         } else {
       
   128             // unload plugin once screensaver gets deleted
       
   129             ScreensaverPluginUnloader *unloader = new ScreensaverPluginUnloader(loader);
       
   130             unloader->connect(screensaver, SIGNAL(destroyed()), SLOT(deleteLater()));
       
   131         }
       
   132     } else {
       
   133         qDebug() << "Screensaver creation failed.";
       
   134         qWarning() << token.mLibrary << "- provider not found";
       
   135         loader->unload();
       
   136         delete loader;
       
   137     }
       
   138 
       
   139     return screensaver;
       
   140 }
       
   141 
       
   142 /*!
       
   143     \class ScreensaverFactory
       
   144     \brief Finds and creates screensavers.
       
   145 
       
   146     Screensaver factory creates an instance of a screensaver
       
   147     on a screensaver token that is given to it.
       
   148  */
       
   149 
       
   150 /*!
       
   151     Constructs a new ScreensaverFactory with \a pluginDirectory and \a parent.
       
   152  */
       
   153 ScreensaverFactory::ScreensaverFactory(const QString& pluginDirectory, 
       
   154                                         QObject *parent) :
       
   155     QObject(parent), m_d(new ScreensaverFactoryPrivate(pluginDirectory, this))
       
   156 {
       
   157 }
       
   158 
       
   159 /*!
       
   160     Destructs the class.
       
   161  */
       
   162 ScreensaverFactory::~ScreensaverFactory()
       
   163 {
       
   164     delete m_d;
       
   165 }
       
   166 
       
   167 /*!
       
   168     Creates and returns a screensaver on the given token.
       
   169     \param token Identifies the screensaver to be created.
       
   170     \return The created screensaver.
       
   171  */
       
   172 Screensaver* ScreensaverFactory::createScreensaver(const ScreensaverToken &token)
       
   173 {
       
   174     return m_d->createScreensaver(token);
       
   175 }