appfoundation/statemodel/src/hsstatefactory.cpp
branchGCC_SURGE
changeset 105 e7325f632549
parent 81 5ef31a21fdd5
parent 104 9b022b1f357c
equal deleted inserted replaced
81:5ef31a21fdd5 105:e7325f632549
     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:  State factory.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QStringList>
       
    19 #include <QDir>
       
    20 #include <QPluginLoader>
       
    21 #include <QState>
       
    22 
       
    23 #include "hsstatefactory.h"
       
    24 #include "hsstatefactory_p.h"
       
    25 #include "hsstateprovidermanifest.h"
       
    26 #include "hstest_global.h"
       
    27 
       
    28 HsStateFactoryPrivate::HsStateFactoryPrivate(HsStateFactory* aPublic)
       
    29     : QObject(aPublic),
       
    30       mQ(aPublic)
       
    31 {
       
    32 
       
    33 }
       
    34 
       
    35 HsStateFactoryPrivate::~HsStateFactoryPrivate()
       
    36 {
       
    37 
       
    38 }
       
    39 
       
    40 QList<HsStateToken> HsStateFactoryPrivate::states()
       
    41 {
       
    42     QStringList pluginPaths;
       
    43 
       
    44     //Check plugin dirs from root of different drives
       
    45     QFileInfoList drives = QDir::drives();
       
    46     for(int i=0; i < drives.count(); i++)
       
    47     {
       
    48         QFileInfo drive = drives.at(i);
       
    49         QString driveLetter = drive.absolutePath();
       
    50         QString path = driveLetter + mPluginManifestDirectory;
       
    51         if(QDir(path).exists())
       
    52         {
       
    53             pluginPaths << path;
       
    54         }
       
    55     }
       
    56 
       
    57     //Check plugin dir relative to current dir
       
    58     if(QDir(mPluginManifestDirectory).exists() && !pluginPaths.contains(QDir(mPluginManifestDirectory).absolutePath()))
       
    59     {
       
    60         pluginPaths << mPluginManifestDirectory;
       
    61     }
       
    62 
       
    63     QList<HsStateToken> states;
       
    64 
       
    65     for(int h=0; h < pluginPaths.count(); h++)
       
    66     {
       
    67         QString path = pluginPaths.at(h);
       
    68         QDir dir(path);
       
    69         QStringList filters("*.manifest");
       
    70 
       
    71         for(int i=0; i < dir.entryList(filters, QDir::Files).count(); ++i)
       
    72         {
       
    73             QString fileName = dir.entryList(filters, QDir::Files).at(i);
       
    74 
       
    75             HsStateProviderManifest manifest;
       
    76             manifest.loadFromXml(dir.absoluteFilePath(fileName));
       
    77 
       
    78             if(manifest.loadOnQuery())
       
    79             {
       
    80                 QList<HsStateToken> tokens = manifest.states();
       
    81                 for(int j=0; j < tokens.count(); ++j)
       
    82                 {
       
    83                     HsStateToken token = tokens.at(j);
       
    84                     IHsStateProvider* provider = loadProviderFromPlugin(token.mLibrary);
       
    85                     if(provider)
       
    86                     {
       
    87                         states << provider->states();
       
    88                         delete provider;
       
    89                     }
       
    90                 }
       
    91             }
       
    92             else
       
    93             {
       
    94                 states << manifest.states();
       
    95             }
       
    96         }
       
    97     }
       
    98     return states;
       
    99 }
       
   100 
       
   101 QState* HsStateFactoryPrivate::createState(const HsStateToken& aToken)
       
   102 {
       
   103     IHsStateProvider* provider = loadProviderFromPlugin(aToken.mLibrary);
       
   104     if(!provider)
       
   105     {
       
   106         HSDEBUG("Widget creation failed - No provider.")
       
   107         return 0;
       
   108     }
       
   109 
       
   110     QState* state = provider->createState(aToken);
       
   111     delete provider;
       
   112     if(!state)
       
   113     {
       
   114         HSDEBUG("State creation failed.")
       
   115     }
       
   116     return state;
       
   117 }
       
   118 
       
   119 IHsStateProvider* HsStateFactoryPrivate::loadProviderFromPlugin(const QString& aPluginName)
       
   120 {
       
   121     QStringList pluginPaths;
       
   122 
       
   123     //Check plugin dirs from root of different drives
       
   124     QFileInfoList drives = QDir::drives();
       
   125     for(int i=0; i < drives.count(); i++)
       
   126     {
       
   127         QFileInfo drive = drives.at(i);
       
   128         QString driveLetter = drive.absolutePath();
       
   129         QString path = driveLetter + mPluginDirectory;
       
   130         if(QDir(path).exists())
       
   131         {
       
   132             pluginPaths << path;
       
   133         }
       
   134     }
       
   135 
       
   136 
       
   137     //Check plugin dir relative to current dir
       
   138     if(QDir(mPluginManifestDirectory).exists() && !pluginPaths.contains(QDir(mPluginDirectory).absolutePath()))
       
   139     {
       
   140         pluginPaths << mPluginDirectory;
       
   141     }
       
   142 
       
   143     IHsStateProvider* provider = 0;
       
   144     QPluginLoader loader;
       
   145     QObject* plugin = 0;
       
   146 
       
   147     for(int i=0; i < pluginPaths.count(); i++)
       
   148     {
       
   149         QString path = pluginPaths.at(i);
       
   150         QString fileName = QDir(path).absoluteFilePath(aPluginName);
       
   151 
       
   152         loader.setFileName(fileName);
       
   153         plugin = loader.instance();
       
   154         provider = qobject_cast<IHsStateProvider*>(plugin);
       
   155         if(provider)
       
   156         {
       
   157             return provider;
       
   158         }
       
   159 
       
   160         //Don't leak memory if provider not IHsStateProvider
       
   161         if(plugin)
       
   162         {
       
   163             HSDEBUG("State provider load - !provider, deleting plugin.")
       
   164             delete plugin;
       
   165         }
       
   166     }
       
   167 
       
   168     HSDEBUG("State provider load failed - Not found.")
       
   169     return 0;
       
   170 }
       
   171 
       
   172 /*!
       
   173     \class HsStateFactory
       
   174     \ingroup group_hsstatemodel
       
   175     \brief Finds and creates home screen states.
       
   176     State factory finds home screen states from state provider
       
   177     plugins. The search is done based on given plugin manifest
       
   178     and plugin binary directories. Found states are returned as
       
   179     a list of state tokens. State factory creates an instance of
       
   180     a state base on a state token that is given to it.
       
   181 */
       
   182 
       
   183 /*!
       
   184     Constructor.
       
   185     \a aPluginManifestDirectory Directory that contains plugin manifests.
       
   186     \a aPluginDirectory Directory that contains plugin binaries.
       
   187     \a aParent Parent object.
       
   188 */
       
   189 HsStateFactory::HsStateFactory(const QString& aPluginManifestDirectory,
       
   190                            const QString& aPluginDirectory,
       
   191                            QObject* aParent)
       
   192     : QObject(aParent)
       
   193 {
       
   194     mD = new HsStateFactoryPrivate(this);
       
   195     mD->mPluginManifestDirectory = aPluginManifestDirectory;
       
   196     mD->mPluginDirectory = aPluginDirectory;
       
   197 }
       
   198 
       
   199 
       
   200 /*!
       
   201     Destructor.
       
   202 */
       
   203 HsStateFactory::~HsStateFactory()
       
   204 {
       
   205 
       
   206 }
       
   207 
       
   208 /*!
       
   209     Returns found states as a list of state tokens.
       
   210 */
       
   211 QList<HsStateToken> HsStateFactory::states()
       
   212 {
       
   213     return mD->states();
       
   214 }
       
   215 
       
   216 /*!
       
   217     Creates and returns a state based on the given token.
       
   218     \a aToken Identifies the state to be created.
       
   219 
       
   220     Returns The created state.
       
   221 */
       
   222 QState* HsStateFactory::createState(const HsStateToken& aToken)
       
   223 {
       
   224     return mD->createState(aToken);
       
   225 }