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