homescreenapp/hsdomainmodel/src/hswidgethost.cpp
changeset 36 cdae8c6c3876
parent 35 f9ce957a272c
child 39 4e8ebe173323
equal deleted inserted replaced
35:f9ce957a272c 36:cdae8c6c3876
    26 
    26 
    27 #include <HbInstance>
    27 #include <HbInstance>
    28 
    28 
    29 #include "hswidgethost.h"
    29 #include "hswidgethost.h"
    30 #include "hsdatabase.h"
    30 #include "hsdatabase.h"
    31 #include "hswidgetdata.h"
    31 #include "hsdomainmodeldatastructures.h"
    32 #include "hspage.h"
    32 #include "hspage.h"
    33 #include "hsapp_defs.h"
    33 #include "hsapp_defs.h"
    34 #include "hsscene.h"
    34 #include "hsscene.h"
    35 #include "cadefs.h"
    35 #include "cadefs.h"
    36 #include "canotifier.h"
    36 #include "canotifier.h"
    47     \brief Homescreen widget runner.
    47     \brief Homescreen widget runner.
    48     Is responsible of running a homescreen widget. Each 
    48     Is responsible of running a homescreen widget. Each 
    49     homescreen widget has its own host.
    49     homescreen widget has its own host.
    50 */
    50 */
    51 
    51 
    52 HsWidgetHost* HsWidgetHost::createInstance(const HsWidgetData &widgetData, 
    52 HsWidgetHost* HsWidgetHost::createInstance(HsWidgetData &widgetData, 
    53                                            const QVariantMap &preferences)
    53                                            const QVariantHash &preferences)
    54 {
    54 {
    55     HsWidgetHost *host = NULL;
       
    56     
       
    57     HsDatabase* db = HsDatabase::instance();
    55     HsDatabase* db = HsDatabase::instance();
    58     Q_ASSERT(db);
    56     Q_ASSERT(db);
    59 
    57 
    60     int databaseId = -1;
    58     if (db->insertWidget(widgetData)) {
    61     if (db->insertWidget(widgetData, databaseId)) {
    59         db->setWidgetPreferences(widgetData.id, preferences);
    62         db->setWidgetPreferences(databaseId, preferences);
    60         return new HsWidgetHost(widgetData.id);
    63         host = new HsWidgetHost(databaseId);
    61     }
    64     }
    62 
    65 
    63     return 0;
    66     return host;                
       
    67 }
    64 }
    68 /*!
    65 /*!
    69     Construct a widget host for the given \a databaseId. 
    66     Construct a widget host for the given \a databaseId. 
    70     \a parent becomes the parent item for the host.
    67     \a parent becomes the parent item for the host.
    71 */
    68 */
    72 HsWidgetHost::HsWidgetHost(int databaseId, QGraphicsItem *parent)
    69 HsWidgetHost::HsWidgetHost(int databaseId, QGraphicsItem *parent)
    73     : HbWidget(parent),
    70     : HbWidget(parent),
    74       mWidget(0),
    71       mWidget(0),
    75       mPage(0),
    72       mPage(0),
       
    73       mState(Constructed),
    76       mDatabaseId(databaseId)
    74       mDatabaseId(databaseId)
    77 {
    75 {
    78     CaQuery query;
    76     CaQuery query;
    79     query.setEntryTypeNames(QStringList(widgetTypeName()));
    77     query.setEntryTypeNames(QStringList(widgetTypeName()));
    80     CaNotifierFilter filter(query);    
    78     CaNotifierFilter filter(query);    
    81     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
    79     CaNotifier *notifier = CaService::instance()->createNotifier(filter);
    82     notifier->setParent(this);
    80     notifier->setParent(this);
    83     connect(notifier,
    81     connect(notifier,
    84             SIGNAL(entryChanged(const CaEntry&, ChangeType)),
    82             SIGNAL(entryChanged(CaEntry,ChangeType)),
    85             SLOT(onEntryChanged(const CaEntry&, ChangeType)), Qt::QueuedConnection);
    83             SLOT(onEntryChanged(CaEntry,ChangeType)), Qt::QueuedConnection);
    86 
    84 
    87     /* TODO: Uncomment after the Qt bug has been fixed.
    85     /* TODO: Uncomment after the Qt bug has been fixed.
    88     QGraphicsDropShadowEffect *effect = 
    86     QGraphicsDropShadowEffect *effect = 
    89         new QGraphicsDropShadowEffect(this);
    87         new QGraphicsDropShadowEffect(this);
    90     effect->setColor(QColor(0, 0, 0, 150));
    88     effect->setColor(QColor(0, 0, 0, 150));
   109     if (mWidget) {
   107     if (mWidget) {
   110         return false;
   108         return false;
   111     }
   109     }
   112 
   110 
   113     HsDatabase *db = HsDatabase::instance();
   111     HsDatabase *db = HsDatabase::instance();
   114     Q_ASSERT(db);
   112     
   115 
       
   116     // Find the widget data.
   113     // Find the widget data.
   117     HsWidgetData widgetData;
   114     HsWidgetData data;
   118     if (!db->widget(mDatabaseId, widgetData, false)) {
   115     data.id = mDatabaseId;
   119         return false;
   116     if (!db->widget(data)) {
   120     }
   117         return false;
   121 
   118     }
   122     mUri = widgetData.uri();
   119 
       
   120     mUri = data.uri;
   123 
   121 
   124     // Create the hosted widget.
   122     // Create the hosted widget.
   125     QServiceManager manager;
   123     QServiceManager manager;
   126     QServiceFilter filter("com.nokia.IHomeScreenWidget");
   124     QServiceFilter filter("com.nokia.symbian.IHomeScreenWidget");
   127     filter.setServiceName(widgetData.uri());
   125     filter.setServiceName(mUri);
   128     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
   126     QList<QServiceInterfaceDescriptor> interfaces = manager.findInterfaces(filter);
   129     if(interfaces.isEmpty()) {
   127     if (interfaces.isEmpty()) {
   130         return false;
   128         return false;
   131     }
   129     }
   132 
   130 
   133     QObject *widgetObject = manager.loadInterface(interfaces.first());
   131     QObject *widgetObject = manager.loadInterface(interfaces.first());
   134     mWidget = qobject_cast<QGraphicsWidget *>(widgetObject);
   132     mWidget = qobject_cast<QGraphicsWidget *>(widgetObject);
   172 {
   170 {
   173     HsDatabase* db = HsDatabase::instance();
   171     HsDatabase* db = HsDatabase::instance();
   174     Q_ASSERT(db);
   172     Q_ASSERT(db);
   175 
   173 
   176     HsWidgetData data;
   174     HsWidgetData data;
   177     if (db->widget(mDatabaseId, data, false)) {
   175     data.id = mDatabaseId;
       
   176     if (db->widget(data)) {
   178         if (!page) {
   177         if (!page) {
   179             data.setPageId(-1);
   178             data.pageId = -1;
   180         } else {
   179         } else {
   181             data.setPageId(page->databaseId());
   180             data.pageId = page->databaseId();
   182         }
   181         }
   183         if (!db->updateWidget(data, false)) {
   182         if (!db->updateWidget(data)) {
   184             return false;
   183             return false;
   185         }
   184         }
   186     } else {
   185     } else {
   187         return false;
   186         return false;
   188     }
   187     }
   217     Returns true if this the database operation succeeds,
   216     Returns true if this the database operation succeeds,
   218     false otherwise
   217     false otherwise
   219 */
   218 */
   220 bool HsWidgetHost::deleteFromDatabase()
   219 bool HsWidgetHost::deleteFromDatabase()
   221 {
   220 {
   222     if (HsDatabase::instance()->deleteWidget(mDatabaseId)) {
   221     HsDatabase *db = HsDatabase::instance();
   223         mDatabaseId = -1;
   222 
   224         return true;
   223     if (!db->deleteWidget(mDatabaseId)) {
   225     }
   224         return false;
   226     return false;
   225     }
       
   226 
       
   227     mDatabaseId = -1;
       
   228     return true;
   227 }
   229 }
   228 
   230 
   229 /*!
   231 /*!
   230     Set widget presentation by using current values.
   232     Set widget presentation by using current values.
   231     Return true if successfull.
   233     Return true if successfull.
   233 bool HsWidgetHost::setWidgetPresentation()
   235 bool HsWidgetHost::setWidgetPresentation()
   234 {
   236 {
   235     HsDatabase *db = HsDatabase::instance();
   237     HsDatabase *db = HsDatabase::instance();
   236     Q_ASSERT(db);
   238     Q_ASSERT(db);
   237 
   239 
   238     QString key = hbInstance->orientation() == Qt::Vertical ? 
   240     QString key = HsScene::orientation() == Qt::Vertical ? 
   239         "portrait" : "landscape";
   241         "portrait" : "landscape";
   240    
   242    
   241     HsWidgetPresentationData data;
   243     HsWidgetPresentationData data;    
   242     data.setWidgetId(databaseId());
   244     data.key      = key;
   243     data.setKey(key);
   245     data.setPos(pos());
   244     data.setPosition(pos());
       
   245     data.setSize(size());
   246     data.setSize(size());
   246     data.setZValue(zValue());
   247     data.zValue   = zValue();
       
   248     data.widgetId = databaseId();
   247     
   249     
   248     HsWidgetPresentationData temp;
   250     return db->setWidgetPresentation(data);
   249     if (!db->widgetPresentation(databaseId(), key, temp)) {        
       
   250         if (!db->insertWidgetPresentation(data)) {
       
   251             return false;
       
   252         }
       
   253     } else {
       
   254         data.setId(temp.id());
       
   255         if (!db->updateWidgetPresentation(data)) {
       
   256             return false;
       
   257         }
       
   258     }
       
   259 
       
   260     return true;
       
   261 }
   251 }
   262 
   252 
   263 /*!
   253 /*!
   264     Set widget presentation data. Return true if successfull
   254     Set widget presentation data. Return true if successfull
   265 
   255 
   266 */
   256 */
   267 bool HsWidgetHost::setWidgetPresentationData(HsWidgetPresentationData &presentationData)
   257 bool HsWidgetHost::setWidgetPresentationData(HsWidgetPresentationData &presentationData)
   268 {
   258 {
   269     presentationData.setWidgetId(mDatabaseId);
   259     HsDatabase *db = HsDatabase::instance();
   270     return HsDatabase::instance()->insertWidgetPresentation(presentationData);
   260     Q_ASSERT(db);
   271 }
   261 
       
   262     presentationData.widgetId = mDatabaseId;
       
   263     return db->setWidgetPresentation(presentationData);
       
   264 }
       
   265 
   272 /*!
   266 /*!
   273     Get widget presentation data matching given \a key. 
   267     Get widget presentation data matching given \a key. 
   274     Data is returned on given empty \a presentationData. Return true if successfull 
   268     Data is returned on given empty \a presentationData. Return true if successfull 
   275 */
   269 */
   276 bool HsWidgetHost::widgetPresentationData(
   270 bool HsWidgetHost::widgetPresentationData(const QString &key,
   277     const QString &key,
   271                                           HsWidgetPresentationData &presentationData)
   278     HsWidgetPresentationData &presentationData)
   272 {
   279 {
   273     HsDatabase *db = HsDatabase::instance();
   280     return HsDatabase::instance()->widgetPresentation(mDatabaseId,key,presentationData);
   274     Q_ASSERT(db);
       
   275 
       
   276     presentationData.key = key;
       
   277     presentationData.widgetId = mDatabaseId;
       
   278     return db->widgetPresentation(presentationData);
   281 }
   279 }
   282 
   280 
   283 /*!
   281 /*!
   284     Return HsWidgetPresentationData for given \a orientation
   282     Return HsWidgetPresentationData for given \a orientation
   285 */
   283 */
   290 
   288 
   291     QString key = orientation == Qt::Vertical ? 
   289     QString key = orientation == Qt::Vertical ? 
   292         "portrait" : "landscape";
   290         "portrait" : "landscape";
   293 
   291 
   294     HsWidgetPresentationData data;
   292     HsWidgetPresentationData data;
   295     if (db->widgetPresentation(databaseId(), key, data)) {
   293     data.key = key;
       
   294     data.widgetId = mDatabaseId;
       
   295     if (db->widgetPresentation(data)) {
   296         return data;
   296         return data;
   297     } else {
   297     } else {
   298         return HsWidgetPresentationData();
   298         return HsWidgetPresentationData();
   299     }
   299     }
   300 
       
   301 }
   300 }
   302 
   301 
   303 /*!
   302 /*!
   304     Load HsWidgetPresentationData for current orientation
   303     Load HsWidgetPresentationData for current orientation
   305 */
   304 */
   306 bool HsWidgetHost::loadWidgetPresentation()
   305 bool HsWidgetHost::loadWidgetPresentation()
   307 {
   306 {
   308     HsDatabase *db = HsDatabase::instance();
   307     HsDatabase *db = HsDatabase::instance();
   309     Q_ASSERT(db);
   308     
   310 
   309     QString key = HsScene::orientation() == Qt::Vertical ?
   311     QString key = hbInstance->orientation() == Qt::Vertical ? 
       
   312         "portrait" : "landscape";
   310         "portrait" : "landscape";
   313 
   311 
   314     HsWidgetPresentationData data;
   312     HsWidgetPresentationData data;
   315     if (!db->widgetPresentation(databaseId(), key, data)) {
   313     data.key = key;
   316         return false;
   314     data.widgetId = mDatabaseId;
   317     }
   315     if (!db->widgetPresentation(data)) {
   318 
   316         return false;
   319     setGeometry(QRectF(data.position(), data.size()));
   317     }
   320     setZValue(data.zValue());
   318 
       
   319     setGeometry(data.geometry());
       
   320     setZValue(data.zValue);
   321 
   321 
   322     return true;
   322     return true;
   323 }
   323 }
   324 
   324 
   325 /*!
   325 /*!
   331     HsDatabase *db = HsDatabase::instance();
   331     HsDatabase *db = HsDatabase::instance();
   332     Q_ASSERT(db);
   332     Q_ASSERT(db);
   333 
   333 
   334     QString key = orientation == Qt::Vertical ? 
   334     QString key = orientation == Qt::Vertical ? 
   335         "portrait" : "landscape";
   335         "portrait" : "landscape";
   336 
   336     
   337     HsWidgetPresentationData data;
   337     return db->deleteWidgetPresentation(mDatabaseId, key);
   338     if (!db->widgetPresentation(databaseId(), key, data)) {
       
   339         return true;
       
   340     } else {
       
   341         if (!db->deleteWidgetPresentation(data.id())) {
       
   342             return false;
       
   343         }
       
   344     }
       
   345 
       
   346     return true;
       
   347 }
   338 }
   348 
   339 
   349 /*!
   340 /*!
   350     \fn void HsWidgetHost::finished()
   341     \fn void HsWidgetHost::finished()
   351     This signal is emitten after the contained widget
   342     This signal is emitten after the contained widget
   361 /*!
   352 /*!
   362     Calls the widget's onInitialize() slot if the
   353     Calls the widget's onInitialize() slot if the
   363     widget defines it.
   354     widget defines it.
   364 */
   355 */
   365 void HsWidgetHost::initializeWidget()
   356 void HsWidgetHost::initializeWidget()
   366 {    
   357 {   
       
   358     if (mState != Constructed) {
       
   359         return;
       
   360     }
       
   361 
   367     setPreferencesToWidget();
   362     setPreferencesToWidget();
   368     setOnline(HsScene::instance()->isOnline());
   363     setOnline(HsScene::instance()->isOnline());
   369     mOnInitializeMethod.invoke(mWidget);
   364     mOnInitializeMethod.invoke(mWidget);
       
   365    
       
   366     mState = Initialized;
   370 }
   367 }
   371 
   368 
   372 /*!
   369 /*!
   373     Calls the widget's onShow() slot if the
   370     Calls the widget's onShow() slot if the
   374     widget defines it.
   371     widget defines it.
   375 */
   372 */
   376 void HsWidgetHost::showWidget()
   373 void HsWidgetHost::showWidget()
   377 {
   374 {
       
   375     if (mState != Initialized &&
       
   376         mState != Hidden) {
       
   377         return;
       
   378     }
       
   379 
   378     mOnShowMethod.invoke(mWidget);
   380     mOnShowMethod.invoke(mWidget);
       
   381     
       
   382     mState = Visible;
   379 }
   383 }
   380 
   384 
   381 /*!
   385 /*!
   382     Calls the widget's onHide() slot if the
   386     Calls the widget's onHide() slot if the
   383     widget defines it.
   387     widget defines it.
   384 */
   388 */
   385 void HsWidgetHost::hideWidget()
   389 void HsWidgetHost::hideWidget()
   386 {
   390 {
       
   391     if (mState != Initialized &&
       
   392         mState != Visible) {
       
   393         return;
       
   394     }
       
   395 
   387     mOnHideMethod.invoke(mWidget);
   396     mOnHideMethod.invoke(mWidget);
       
   397 
       
   398     mState = Hidden;
   388 }
   399 }
   389 
   400 
   390 /*!
   401 /*!
   391     Calls the widget's onUninitialize() slot if the
   402     Calls the widget's onUninitialize() slot if the
   392     widget defines it.
   403     widget defines it.
   393 */
   404 */
   394 void HsWidgetHost::uninitializeWidget()
   405 void HsWidgetHost::uninitializeWidget()
   395 {
   406 {
       
   407     if (mState != Visible &&
       
   408         mState != Hidden) {
       
   409         return;
       
   410     }
       
   411 
   396     mOnUninitializeMethod.invoke(mWidget);
   412     mOnUninitializeMethod.invoke(mWidget);
       
   413 
       
   414     mState = Uninitialized;
   397 }
   415 }
   398 
   416 
   399 /*!
   417 /*!
   400     Calls the widget's widgetOnlineState property if the
   418     Calls the widget's widgetOnlineState property if the
   401     widget defines it.
   419     widget defines it.
   509 bool HsWidgetHost::setPreferencesToWidget()
   527 bool HsWidgetHost::setPreferencesToWidget()
   510 {
   528 {
   511     HsDatabase *db = HsDatabase::instance();
   529     HsDatabase *db = HsDatabase::instance();
   512     Q_ASSERT(db);
   530     Q_ASSERT(db);
   513 
   531 
   514     QVariantMap preferences;
   532     QVariantHash preferences;
   515     if (!db->widgetPreferences(mDatabaseId, preferences)) {
   533     if (!db->widgetPreferences(mDatabaseId, preferences)) {
   516         return false;
   534         return false;
   517     }
   535     }
   518 
   536 
   519     QStringList names = preferences.keys();
   537     QStringList names = preferences.keys();
   537 {
   555 {
   538     if (names.isEmpty()) {
   556     if (names.isEmpty()) {
   539         return;
   557         return;
   540     }
   558     }
   541 
   559 
   542     QVariantMap preferences;
   560     QVariantHash preferences;
   543 
   561 
   544     foreach (QString name, names) {
   562     foreach (QString name, names) {
   545         QVariant value = mWidget->property(name.toLatin1());
   563         QVariant value = mWidget->property(name.toLatin1());
   546         preferences.insert(name, value);
   564         preferences.insert(name, value);
   547     }
   565     }
   560     when it has finished its execution and is ready for 
   578     when it has finished its execution and is ready for 
   561     removal from the homescreen.
   579     removal from the homescreen.
   562 */
   580 */
   563 void HsWidgetHost::onFinished()
   581 void HsWidgetHost::onFinished()
   564 {
   582 {
       
   583     mState = Finished;
   565     emit widgetFinished(this);
   584     emit widgetFinished(this);
   566 }
   585 }
   567 
   586 
   568 /*!
   587 /*!
   569     This slot reacts to the widgets error() signal, if it was 
   588     This slot reacts to the widgets error() signal, if it was 
   570     defined for the widget. The widget emits the signal in 
   589     defined for the widget. The widget emits the signal in 
   571     failure cases.
   590     failure cases.
   572 */
   591 */
   573 void HsWidgetHost::onError()
   592 void HsWidgetHost::onError()
   574 {
   593 {
       
   594     mState = Faulted;
   575     emit widgetError(this);
   595     emit widgetError(this);
   576 }
   596 }
   577 
   597 
   578 /*!
   598 /*!
   579     This slot reacts to \a entry change that is described with 
   599     This slot reacts to \a entry change that is described with