contentstorage/caclient/stub/src/hswidgetregistryservice.cpp
changeset 60 f62f87b200ec
child 66 32469d7d46ff
equal deleted inserted replaced
4:1a2a00e78665 60:f62f87b200ec
       
     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:  Manages installed widgets information
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QDir>
       
    19 #include <QPluginLoader>
       
    20 #include <QDebug>
       
    21 
       
    22 #include "hswidgetregistryservice.h"
       
    23 #include "hswidgetregistryservice_p.h"
       
    24 #include "hswidgetprovidermanifest.h"
       
    25 #include "servicefinder.h"
       
    26 
       
    27 const char POSTERURI[] = "homescreen.nokia.com/widget/poster/";
       
    28 const char POSTERWIDGETNAME[] = "name";
       
    29 const char POSTERWIDGETID[] = "posterwidgetid";
       
    30 const char PUBLISHER[] = "publisher";
       
    31 const char TEMPLATETYPE[] = "templatetype";
       
    32 const char CONTENTID[] = "contentid";
       
    33 const char CONTENTTYPE[] = "contenttype";
       
    34 
       
    35 /*!
       
    36     ?Qt_style_documentation
       
    37 */
       
    38 HsWidgetRegistryServicePrivate::HsWidgetRegistryServicePrivate(
       
    39     const QString &installationPath, HsWidgetRegistryService *ptrToPublic,
       
    40     QObject *parent) : QObject(parent)
       
    41 {
       
    42     Q_UNUSED(ptrToPublic);
       
    43     QStringList manifestPaths;
       
    44 
       
    45     this->mInstallationPath = installationPath;
       
    46     QDir currentDir = QDir::current();
       
    47     QString currentPath = currentDir.absolutePath();
       
    48 
       
    49     //Check widget installation dirs from root of different drives
       
    50     QFileInfoList drives = QDir::drives();
       
    51 
       
    52     // ?
       
    53     for (int i=0; i < drives.count(); i++) {
       
    54         QFileInfo drive = drives.at(i);
       
    55         QString driveLetter = drive.absolutePath();
       
    56         QString path = currentPath + "/" + mInstallationPath;
       
    57         QDir registryDir(path);
       
    58 
       
    59         if (registryDir.exists()) {
       
    60             // ?
       
    61             mManifestDirectories[path] = readManifestDirectories(path);
       
    62         }
       
    63     }
       
    64 }
       
    65 
       
    66 /*!
       
    67     ?Qt_style_documentation
       
    68 */
       
    69 HsWidgetRegistryServicePrivate::~HsWidgetRegistryServicePrivate()
       
    70 {
       
    71 
       
    72 }
       
    73 
       
    74 /*!
       
    75     ?Qt_style_documentation
       
    76 */
       
    77 QList<HsWidgetToken> HsWidgetRegistryServicePrivate::widgets()
       
    78 {
       
    79     QList<HsWidgetToken> widgets;
       
    80     QMapIterator<QString, QStringList> i(mManifestDirectories);
       
    81 
       
    82     while (i.hasNext()) {
       
    83         i.next();
       
    84         QStringList manifestFileList = i.value();
       
    85         QDir manifestDir(i.key());
       
    86 
       
    87         // ?
       
    88         for (int h=0; h < manifestFileList.count(); h++) {
       
    89             widgets << readManifestFile(manifestDir.absoluteFilePath(manifestFileList.at(h)));
       
    90         }
       
    91     }
       
    92     return widgets;
       
    93 }
       
    94 
       
    95 /*!
       
    96     ?Qt_style_documentation
       
    97 */
       
    98 IHsWidgetProvider *HsWidgetRegistryServicePrivate::loadProviderFromPlugin(
       
    99     const QString &pluginName)
       
   100 {
       
   101     QPluginLoader loader(pluginName);
       
   102     QObject *plugin = loader.instance();
       
   103     IHsWidgetProvider *provider = qobject_cast<IHsWidgetProvider *>(plugin);
       
   104 
       
   105     if (provider) {
       
   106         // ?
       
   107         return provider;
       
   108     }
       
   109 
       
   110     // Don't leak memory if provider not IHsWidgetProvider
       
   111     if (plugin) {
       
   112         //qDebug("Widget provider load - !provider, deleting plugin.")
       
   113         delete plugin;
       
   114     }
       
   115 
       
   116     // qDebug("Widget provider load failed - Not found.")
       
   117     return 0;
       
   118 }
       
   119 
       
   120 /*!
       
   121     ?Qt_style_documentation
       
   122 */
       
   123 QStringList HsWidgetRegistryServicePrivate::readManifestDirectories(const QString &path)
       
   124 {
       
   125     QStringList widgetDirPaths;
       
   126     QDir registryDir(path);
       
   127     QStringList widgetDirs = registryDir.entryList(QDir::AllDirs);
       
   128 
       
   129     // ?
       
   130     for (int i=0; i < widgetDirs.count(); ++i) {
       
   131         widgetDirPaths << registryDir.absoluteFilePath(widgetDirs.at(i));
       
   132     }
       
   133     return widgetDirPaths;
       
   134 }
       
   135 
       
   136 /*!
       
   137     ?Qt_style_documentation
       
   138 */
       
   139 void HsWidgetRegistryServicePrivate::doWidgetRemove(const QString &path,
       
   140         const QStringList &originalList, const QStringList &currentList)
       
   141 {
       
   142     Q_UNUSED(path);
       
   143     const int originalCount = originalList.count();
       
   144 
       
   145     // ?
       
   146     for (int i=0; i<originalCount; i++) {
       
   147         QString widgetDir = originalList.at(i);
       
   148 
       
   149         if (!currentList.contains(widgetDir)) {
       
   150             // ?
       
   151             QDir dir(widgetDir);
       
   152             int widgetUid = dir.dirName().toUInt(0, 16);
       
   153             mPublic->emitWidgetRemovedFromRegistry(widgetUid);
       
   154         }
       
   155     }
       
   156 }
       
   157 
       
   158 /*!
       
   159     ?Qt_style_documentation
       
   160 */
       
   161 QList<HsWidgetToken> HsWidgetRegistryServicePrivate::readManifestFile(
       
   162     const QString &manifestFilePath)
       
   163 {
       
   164     QList<HsWidgetToken> widgets;
       
   165     HsWidgetProviderManifest manifest;
       
   166     QStringList filters("*.manifest");
       
   167     QDir dir(manifestFilePath);
       
   168     QStringList manifestDir = dir.entryList(filters, QDir::Files);
       
   169 
       
   170     if (!manifestDir.isEmpty()) {
       
   171         // ?
       
   172         QString fileName = manifestDir.first();
       
   173 
       
   174         if (fileName != "hsposterwidgetprovider.manifest") {
       
   175             // Directory differs from the poster widget's directory
       
   176             // which is not supported for the time being.
       
   177             manifest.loadFromXml(dir.absoluteFilePath(fileName));
       
   178             widgets = manifest.widgets();
       
   179             int widgetUid = dir.dirName().toUInt(0, 16);
       
   180 
       
   181             // ?
       
   182             for (int i=0; i<widgets.count(); i++) {
       
   183                 widgets[i].mUid = widgetUid;
       
   184                 widgets[i].mLibrary = manifestFilePath + "/" + widgets[i].mLibrary;
       
   185                 if (widgets[i].mIconUri != "") {
       
   186                     // ?
       
   187                     widgets[i].mIconUri = manifestFilePath + "/" + widgets[i].mIconUri;
       
   188                 }
       
   189             }
       
   190 
       
   191             qDebug() << "HsWidgetRegistryServicePrivate::readManifestFile - " \
       
   192                      "widget added: " << fileName;
       
   193         }
       
   194     }
       
   195     return widgets;
       
   196 }
       
   197 
       
   198 /*!
       
   199     ?Qt_style_documentation
       
   200 */
       
   201 void HsWidgetRegistryServicePrivate::ensureWidgetRegistryPaths()
       
   202 {
       
   203     /*
       
   204     mFileSystemWatcher.disconnect();
       
   205     QStringList pathList = mManifestDirectories.keys();
       
   206 
       
   207     // ?
       
   208     for(int i=0; i < pathList.count(); i++) {
       
   209         QDir registryDir(pathList.at(i));
       
   210 
       
   211     	if(!registryDir.exists()) {
       
   212     		// ?
       
   213             registryDir.mkpath(pathList.at(i));
       
   214         }
       
   215     }
       
   216 
       
   217     mFileSystemWatcher.removePaths(QStringList(mManifestDirectories.keys()));
       
   218     mFileSystemWatcher.addPaths(QStringList(mManifestDirectories.keys()));
       
   219     connect(&mFileSystemWatcher,SIGNAL(directoryChanged(const QString&)),SLOT(directoryChanged(const QString&)));
       
   220     */
       
   221 }
       
   222 
       
   223 /*!
       
   224     ?Qt_style_documentation
       
   225 */
       
   226 void HsWidgetRegistryServicePrivate::directoryChanged(const QString &path)
       
   227 {
       
   228     Q_UNUSED(path);
       
   229     /*
       
   230     int installationStatus = mInstallerObserver.value();
       
   231 
       
   232     if ((installationStatus & KSASwisOperationMask) == ESASwisNone) {
       
   233         // ?
       
   234         QStringList originalList = mManifestDirectories.value(path);
       
   235         QStringList currentList = readManifestDirectories(path);
       
   236         doWidgetAdd(path, originalList, currentList);
       
   237         doWidgetRemove(path, originalList, currentList);
       
   238         mManifestDirectories[path] = currentList;
       
   239         ensureWidgetRegistryPaths();
       
   240     } else {
       
   241         // ?
       
   242         mFileSystemWatcher.disconnect();
       
   243         connect(&mInstallerObserver,SIGNAL(valueChanged(int)),SLOT(installerStateChanged(int)));
       
   244         mInstallerObserver.subscribe();
       
   245         mLatestChangedDirectory = path;
       
   246     }
       
   247     */
       
   248 }
       
   249 
       
   250 /*!
       
   251     ?Qt_style_documentation
       
   252 */
       
   253 void HsWidgetRegistryServicePrivate::installerStateChanged(int newValue)
       
   254 {
       
   255     Q_UNUSED(newValue);
       
   256     /*
       
   257     if ((newValue & KSASwisOperationMask) == ESASwisNone) {
       
   258     	// ?
       
   259         mInstallerObserver.unSubscribe();
       
   260         mInstallerObserver.disconnect();
       
   261         QStringList originalList = mManifestDirectories.value(mLatestChangedDirectory);
       
   262         QStringList currentList = readManifestDirectories(mLatestChangedDirectory);
       
   263         doWidgetAdd(mLatestChangedDirectory, originalList, currentList);
       
   264         doWidgetRemove(mLatestChangedDirectory, originalList, currentList);
       
   265         connect(&mFileSystemWatcher,SIGNAL(directoryChanged(const QString&)),SLOT(directoryChanged(const QString&)));
       
   266         mManifestDirectories[mLatestChangedDirectory] = currentList;
       
   267         ensureWidgetRegistryPaths();
       
   268     }
       
   269     */
       
   270 }
       
   271 
       
   272 
       
   273 /*!
       
   274     \class HsWidgetRegistryService
       
   275     \ingroup group_hsruntimeservices
       
   276     \brief Manages installed widgets information
       
   277 
       
   278     Manages information on available widgets and inform observers on
       
   279     registry modifications, i.e installing and uninstalling widgets.
       
   280 
       
   281 */
       
   282 
       
   283 /*!
       
   284     Constructor.
       
   285     \a installationPath is the path where the widget is installed
       
   286     and the \a parent is the parent object.
       
   287 */
       
   288 HsWidgetRegistryService::HsWidgetRegistryService(
       
   289     const QString &installationPath,
       
   290     QObject *parent)
       
   291     :QObject(parent),
       
   292      mPrivate(new HsWidgetRegistryServicePrivate(installationPath, this, this))
       
   293 {
       
   294 
       
   295 }
       
   296 
       
   297 /*!
       
   298     Destructor.
       
   299 */
       
   300 HsWidgetRegistryService::~HsWidgetRegistryService()
       
   301 {
       
   302 
       
   303 }
       
   304 
       
   305 /*!
       
   306     Fetch available widgets information
       
   307     Return List of widget tokens.
       
   308 */
       
   309 QList<HsWidgetToken> HsWidgetRegistryService::widgets()
       
   310 {
       
   311     return mPrivate->widgets();
       
   312 }
       
   313 
       
   314 /*!
       
   315     \fn HsWidgetRegistryService::widgetAddedToRegistry(const QList<HsWidgetToken> &widgetTokenList);
       
   316     Emitted when new widgets are added to registry. \a widgetTokenList
       
   317     contains list of widget tokens.
       
   318 */
       
   319 
       
   320 /*!
       
   321     \fn HsWidgetRegistryService::widgetRemovedFromRegistry(int uid)
       
   322     Emitted when a widget is removed from registry
       
   323 
       
   324 */
       
   325 
       
   326 
       
   327 /*!
       
   328     \fn HsWidgetRegistryService::posterWidgetRemovedFromRegistry(int posterWidgetId)
       
   329     Emitted when a poster widget publisher is removed from cps
       
   330     \a posterWidgetId Poster widget id.
       
   331 */
       
   332 
       
   333 /*!
       
   334     Emits the widgetAddedToRegistry() signal
       
   335     \a widgetsAdded Identifies the added widgets.
       
   336 */
       
   337 void HsWidgetRegistryService::emitWidgetAddedToRegistry(const QList<HsWidgetToken> &widgetsAdded)
       
   338 {
       
   339     emit widgetAddedToRegistry(widgetsAdded);
       
   340 }
       
   341 
       
   342 
       
   343 /*!
       
   344     Emits the widgetRemovedFromRegistry() signal
       
   345     \a uid HsWidget uid.
       
   346 */
       
   347 void HsWidgetRegistryService::emitWidgetRemovedFromRegistry(int uid)
       
   348 {
       
   349     emit widgetRemovedFromRegistry(uid);
       
   350 }