homescreenapp/hsdomainmodel/src/hscontentservice.cpp
changeset 62 341166945d65
parent 55 03646e8da489
child 81 7dd137878ff8
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
    19 #include "hsdatabase.h"
    19 #include "hsdatabase.h"
    20 #include "hsscene.h"
    20 #include "hsscene.h"
    21 #include "hspage.h"
    21 #include "hspage.h"
    22 #include "hsdomainmodeldatastructures.h"
    22 #include "hsdomainmodeldatastructures.h"
    23 #include "hswidgethost.h"
    23 #include "hswidgethost.h"
       
    24 #include "hsapp_defs.h"
    24 
    25 
    25 
    26 
       
    27 /*!
       
    28     \class HsContentService
       
    29     \ingroup group_hsdomainmodel
       
    30     \brief Service for creating widget to Home Screen and make query for widget instances. 
       
    31     
       
    32 */
       
    33 
       
    34 /*!
       
    35     Constructor.
       
    36     
       
    37     \a parent Owner.
       
    38 */
    26 HsContentService::HsContentService(QObject *parent)
    39 HsContentService::HsContentService(QObject *parent)
    27     : QObject(parent)
    40     : QObject(parent), mWidgetStartFaulted(false)
    28 {
    41 {
    29    
    42    
    30 }
    43 }
    31 
    44 
       
    45 /*!
       
    46     Destructor.
       
    47 */
    32 HsContentService::~HsContentService()
    48 HsContentService::~HsContentService()
    33 {
    49 {
    34 }
    50 }
    35 
    51 
    36 
    52 /*!
       
    53     Creates widget. \a params must include 'uri' for the desired widget type.
       
    54     'preferences' is optional.
       
    55 */
    37 bool HsContentService::createWidget(const QVariantHash &params)
    56 bool HsContentService::createWidget(const QVariantHash &params)
    38 {
    57 {
    39     HsWidgetHost *widget = createWidgetForPreview(params);
    58     return addWidget(params.value(URI).toString(),
    40     if (!widget) {
    59                      params.value(PREFERENCES).toHash(),
    41         return false;
    60                      params.value(HOMESCREENDATA));
    42     }     
       
    43     return HsScene::instance()->activePage()->addNewWidget(widget);
       
    44 }
    61 }
    45 
    62 
       
    63 // This method will be removed.
       
    64 #ifdef COVERAGE_MEASUREMENT
       
    65 #pragma CTC SKIP
       
    66 #endif //COVERAGE_MEASUREMENT
    46 HsWidgetHost *HsContentService::createWidgetForPreview(const QVariantHash &params)
    67 HsWidgetHost *HsContentService::createWidgetForPreview(const QVariantHash &params)
    47 {
    68 {
    48     HsWidgetData widgetData;
    69     HsWidgetData widgetData;
    49     widgetData.uri = params.value("uri").toString();
    70     widgetData.uri = params.value(URI).toString();
    50 
    71 
    51     return HsWidgetHost::createInstance(
    72     return HsWidgetHost::createInstance(
    52         widgetData, params.value("preferences").toHash());
    73         widgetData, params.value(PREFERENCES).toHash());
    53 }
    74 }
       
    75 #ifdef COVERAGE_MEASUREMENT
       
    76 #pragma CTC ENDSKIP
       
    77 #endif //COVERAGE_MEASUREMENT
    54 
    78 
    55 /*!
    79 /*!
    56 
    80 
    57 */
    81 */
    58 bool HsContentService::addWidget(const QString &uri, const QVariantHash &preferences)
    82 bool HsContentService::addWidget(const QString &uri, const QVariantHash &preferences,
       
    83                                  const QVariant &homescreenData)
    59 {
    84 {
    60     HsWidgetData data;
    85     HsWidgetData data;
    61     data.uri = uri;
    86     data.uri = uri;
    62 
    87 
    63     HsWidgetHost *widget = HsWidgetHost::createInstance(data, preferences);    
    88     HsWidgetHost *widget = HsWidgetHost::createInstance(data, preferences);    
    64     if (!widget) {
    89     if (!widget) {
    65         return false;
    90         return false;
    66     }
    91     }
    67 
    92 
    68     HsPage *page = HsScene::instance()->activePage();
    93     HsPage *page = HsScene::instance()->activePage();
    69     if (!page->addNewWidget(widget)) {
    94     QPointF touchPoint = homescreenData.toPointF();
       
    95     if (!page->addNewWidget(widget, touchPoint)) {
    70         widget->remove();
    96         widget->remove();
    71         return false;
    97         return false;
    72     }
    98     }
    73 
    99     connect(widget,SIGNAL(event_faulted()),SLOT(widgetStartFaulted()));
    74     widget->startWidget();
   100     mWidgetStartFaulted = false; 
    75     page->layoutNewWidgets();
   101     widget->startWidget(); // synchronous operation
       
   102     if (mWidgetStartFaulted) {
       
   103         // page will destroy widget instance
       
   104         return false;
       
   105     }
       
   106     widget->disconnect(this);
       
   107     emit widgetAdded(uri, preferences);
    76     return true;
   108     return true;
    77 }
   109 }
    78 
   110 
    79 /*!
   111 /*!
       
   112     Returns false if database query fails. If returns true then 
       
   113     number of widget instances of the given \a uri and \a preferences is stored to \a count.
       
   114     If \a preferences is empty then returns number of widget instances with given uri.
       
   115 */
       
   116 bool HsContentService::widgets(const QString &uri, const QVariantHash &preferences, int &count)
       
   117 {
       
   118     return HsDatabase::instance()->widgets(uri, preferences, count);
       
   119 }
    80 
   120 
       
   121 /*!
    81 */
   122 */
    82 HsContentService *HsContentService::instance()
   123 HsContentService *HsContentService::instance()
    83 {
   124 {
    84     if (!mInstance) {
   125     if (!mInstance) {
    85         mInstance = new HsContentService();
   126         mInstance = new HsContentService();
    86     }
   127     }
    87     return mInstance;
   128     return mInstance;
    88 }
   129 }
    89 
   130 
    90 /*!
   131 /*!
       
   132 
       
   133 */
       
   134 void HsContentService::emitWidgetRemoved(const QString &uri, const QVariantHash &preferences)
       
   135 {
       
   136     emit widgetRemoved(uri, preferences);
       
   137 }
       
   138 
       
   139 /*!
       
   140 
       
   141 */
       
   142 void HsContentService::widgetStartFaulted()
       
   143 {
       
   144     mWidgetStartFaulted = true;
       
   145 }
       
   146 
       
   147 /*!
    91     Points to the content service instance.
   148     Points to the content service instance.
    92 */
   149 */
    93 HsContentService *HsContentService::mInstance(0);
   150 HsContentService *HsContentService::mInstance(0);
       
   151 
       
   152 /*!
       
   153     \fn HsContentService::widgetAdded(const QString &uri, const QVariantHash &preferences);
       
   154     
       
   155     Emited when widget is added.
       
   156 
       
   157 */
       
   158 /*!
       
   159     \fn HsContentService::widgetRemoved(const QString &uri, const QVariantHash &preferences);
       
   160     
       
   161     Emited when widget is removed.
       
   162 
       
   163 */
       
   164