homescreenapp/hsapplication/src/hshomescreen.cpp
changeset 35 f9ce957a272c
child 36 cdae8c6c3876
equal deleted inserted replaced
5:c743ef5928ba 35:f9ce957a272c
       
     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:  Homescreen application main class.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QStateMachine>
       
    19 #include <QDir>
       
    20 
       
    21 #include <qservicemanager.h>
       
    22 #include <qservicefilter.h>
       
    23 #include <qserviceinterfacedescriptor.h>
       
    24 
       
    25 #include <HbInstance>
       
    26 
       
    27 #include "hshomescreen.h"
       
    28 #include "hstest_global.h"
       
    29 
       
    30 QTM_USE_NAMESPACE
       
    31 
       
    32 /*!
       
    33     \class HsHomeScreen
       
    34     \ingroup group_hsapplication
       
    35     \brief Homescreen application main class.
       
    36     Loads a runtime from a runtime provider plugin. Manages
       
    37     the state machine execution.
       
    38 */
       
    39 
       
    40 /*!
       
    41     Constructor.
       
    42     \a parent Parent object.
       
    43 */
       
    44 HsHomeScreen::HsHomeScreen(QObject *parent)
       
    45   : QObject(parent),
       
    46     mRuntime(0)
       
    47 {
       
    48     HSTEST_FUNC_ENTRY("HS::HsHomeScreen::HsHomeScreen");
       
    49 
       
    50     registerServicePlugins();
       
    51 
       
    52     QServiceManager manager;
       
    53     QServiceFilter filter("com.nokia.homescreen.runtime.HsRuntime");
       
    54     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
       
    55     QObject *interface = manager.loadInterface(interfaces.first().interfaceName());
       
    56     mRuntime = qobject_cast<QStateMachine*>(interface);
       
    57     if (mRuntime) {
       
    58         mRuntime->setParent(this);
       
    59         connect(mRuntime, SIGNAL(started()), SLOT(onRuntimeStarted()));
       
    60         connect(mRuntime, SIGNAL(stopped()), SLOT(onRuntimeStopped()));
       
    61         hbInstance->allMainWindows().at(0)->installEventFilter(this);
       
    62     } else {
       
    63         emit exit();
       
    64     }
       
    65     HSTEST_FUNC_EXIT("HS::HsHomeScreen::HsHomeScreen");
       
    66 }
       
    67 
       
    68 /*!
       
    69     Destructor.
       
    70 */
       
    71 HsHomeScreen::~HsHomeScreen()
       
    72 {
       
    73 }
       
    74 
       
    75 /*!
       
    76     \fn void HsHomeScreen::exit()
       
    77     Emitted when the home screen application needs to exit.
       
    78 */
       
    79 
       
    80 /*!
       
    81     Starts the runtime.
       
    82 */
       
    83 void HsHomeScreen::start()
       
    84 {
       
    85     HSTEST_FUNC_ENTRY("HS::HsHomeScreen::start");
       
    86     if (mRuntime) {
       
    87         mRuntime->start();
       
    88     } else {
       
    89         HSTEST_FUNC_EXIT("HS::HsHomeScreen::start, mRuntime not created, exit application");
       
    90         emit exit();
       
    91     }
       
    92     HSTEST_FUNC_EXIT("HS::HsHomeScreen::start");
       
    93 }
       
    94 
       
    95 /*!
       
    96     Stops the runtime.
       
    97 */
       
    98 void HsHomeScreen::stop()
       
    99 {
       
   100 	if (mRuntime && mRuntime->isRunning()) {
       
   101 		QMetaObject::invokeMethod(mRuntime, "event_exit");
       
   102 	}
       
   103 }
       
   104 
       
   105 /*!
       
   106     \copydoc QObject::eventFilter(QObject *watched, QEvent *event)
       
   107 */
       
   108 bool HsHomeScreen::eventFilter(QObject *watched, QEvent *event)
       
   109 {
       
   110     Q_UNUSED(watched);
       
   111     if (event->type() == QEvent::Close) {
       
   112         return true;
       
   113     }
       
   114     return false;
       
   115 }
       
   116 
       
   117 /*!
       
   118     Called after the runtime has started.
       
   119 */
       
   120 void HsHomeScreen::onRuntimeStarted()
       
   121 {
       
   122 }
       
   123 
       
   124 /*!
       
   125     Called after the runtime has stopped.
       
   126 */
       
   127 void HsHomeScreen::onRuntimeStopped()
       
   128 {
       
   129     emit exit();
       
   130 }
       
   131 
       
   132 /*!
       
   133     Registers service plugins pre-installed on the device.
       
   134 */
       
   135 void HsHomeScreen::registerServicePlugins()
       
   136 {
       
   137     HSTEST_FUNC_ENTRY("HS::HsHomeScreen::registerServicePlugins()");
       
   138     QStringList pluginPaths;
       
   139 
       
   140     pluginPaths << "hsresources/plugins";
       
   141 #ifdef Q_OS_SYMBIAN
       
   142     pluginPaths << "private/20022F35/import/widgetregistry";
       
   143 #else
       
   144     pluginPaths << "hsresources/import/widgetregistry";
       
   145 #endif
       
   146 
       
   147     QFileInfoList drives = QDir::drives();
       
   148     foreach(const QString pluginPath, pluginPaths) {
       
   149 #ifdef Q_OS_SYMBIAN
       
   150         //Check plugin dirs from root of different drives
       
   151         foreach(QFileInfo drive, drives) {
       
   152             QString driveLetter = drive.absolutePath();
       
   153             QString path = driveLetter + pluginPath;
       
   154             if(QDir(path).exists()) {
       
   155                 registerServicePlugins(path);
       
   156             }
       
   157         }
       
   158 #endif
       
   159         //Check plugin path relative to current dir
       
   160         if(QDir(pluginPath).exists()) {
       
   161             registerServicePlugins(pluginPath);
       
   162         }
       
   163     }
       
   164     HSTEST_FUNC_EXIT("HS::HsHomeScreen::registerServicePlugins()");
       
   165 }
       
   166 
       
   167 /*!
       
   168     Recursively registers service plugins starting from given /a root
       
   169     directory. All directories containing plugins are added to
       
   170     application's library paths at the same time.
       
   171 */
       
   172 void HsHomeScreen::registerServicePlugins(const QString &root)
       
   173 {
       
   174     HSTEST_FUNC_ENTRY("HS::HsHomeScreen::registerServicePlugins(const QString &)");
       
   175     QDir dir = QDir(root);
       
   176     QFileInfoList fileInfos = dir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
       
   177 
       
   178     foreach (QFileInfo fileInfo, fileInfos) {
       
   179         registerServicePlugins(fileInfo.absoluteFilePath());
       
   180     }
       
   181 
       
   182     fileInfos = dir.entryInfoList(QStringList("*.xml"));
       
   183 
       
   184     if (!fileInfos.isEmpty()) {
       
   185         //Plugin dll and xml are in the same directory
       
   186         QApplication::addLibraryPath(root);
       
   187         qDebug() << QString("HS::HsHomeScreen::registerServicePlugins - Directory added to application's library paths: ")
       
   188                  << root;
       
   189         QServiceManager manager;
       
   190         foreach(QFileInfo fileInfo, fileInfos) {
       
   191             manager.addService(fileInfo.absoluteFilePath());
       
   192             qDebug() << QString("HS::HsHomeScreen::registerServicePlugins - Plugin registered: ") + fileInfo.fileName();
       
   193         }
       
   194     }
       
   195     HSTEST_FUNC_EXIT("HS::HsHomeScreen::registerServicePlugins(const QString &)");
       
   196 }