homescreenapp/stateplugins/hsmenuworkerstateplugin/src/hsaddtohomescreenstate.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:  Menu Application Library state.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <hblabel.h>
       
    20 #include <hsiwidgetprovider.h>
       
    21 #include <hsshortcutservice.h>
       
    22 #include <hscontentservice.h>
       
    23 #include <hbmessagebox.h>
       
    24 
       
    25 #include "hsaddtohomescreenstate.h"
       
    26 #include "hsmenuevent.h"
       
    27 #include "hsapp_defs.h"
       
    28 #include "caservice.h"
       
    29 #include "caquery.h"
       
    30 #include "hsmenuservice.h"
       
    31 
       
    32 const char SHORTCUT_WIDGET_URI[] = "hsshortcutwidgetplugin";
       
    33 const char SHORTCUT_ID[] = "mcsId";
       
    34 
       
    35 /*!
       
    36  \class HsAddToHomeScreenState
       
    37  \ingroup group_hsmenustateplugin
       
    38  \brief Application Library State.
       
    39  Parent state for Application Library functionality
       
    40  (browsing applications and collections)
       
    41  */
       
    42 
       
    43 /*!
       
    44  Constructor
       
    45  \param parent: parent state
       
    46  \retval void
       
    47  */
       
    48 HsAddToHomeScreenState::HsAddToHomeScreenState(QState *parent) :
       
    49     HsMenuBaseState("AddToHomeScreenState", parent)
       
    50 {
       
    51     requestServices(QList<QVariant> () << SHORTCUT_SERVICE_KEY
       
    52                     << CONTENT_SERVICE_KEY);
       
    53 }
       
    54 
       
    55 /*!
       
    56  Destructor. Empty one.
       
    57  */
       
    58 HsAddToHomeScreenState::~HsAddToHomeScreenState()
       
    59 {
       
    60 }
       
    61 
       
    62 /*!
       
    63  onEntry method invoked on entering the state
       
    64  Adds a widget or shortcut to the active page of home screen.
       
    65  It reads "data" property which value is a string map.
       
    66  The map can keep either data specific for shortcut (item id)
       
    67  or widget (entry type name, widget library name, widget uri).
       
    68  Depending on the property values it calls method for adding widget
       
    69  or shortcut.
       
    70  \param contentService: service for adding widgets
       
    71  \retval void
       
    72  */
       
    73 void HsAddToHomeScreenState::onEntry(QEvent *event)
       
    74 {
       
    75     HSMENUTEST_FUNC_ENTRY("HsAddToHomeScreenState::onEntry");
       
    76     HsMenuBaseState::onEntry(event);
       
    77     HsMenuEvent *menuEvent = static_cast<HsMenuEvent *>(event);
       
    78     QVariantMap data = menuEvent->data();
       
    79 
       
    80     const QString entryTypeName = data.value(entryTypeNameKey()).toString();
       
    81 
       
    82     const int entryId = data.value(itemIdKey()).toInt();
       
    83 
       
    84     if (entryTypeName == widgetTypeName()) {
       
    85         const QString library =
       
    86             data.value(widgetLibraryAttributeName()).toString();
       
    87 
       
    88         const QString uri = data.value(widgetUriAttributeName()).toString();
       
    89 
       
    90         addWidget(*contentService(), library, uri, entryId);
       
    91         HsMenuService::touch(entryId);
       
    92     } else {
       
    93         addShortcut(*contentService(), entryId);
       
    94     }
       
    95     HSMENUTEST_FUNC_EXIT("HsAddToHomeScreenState::onEntry");
       
    96 }
       
    97 
       
    98 /*!
       
    99  Adds a widget to active page of home screen
       
   100  \param contentService: service for adding widgets
       
   101  \param library: widget library path and file name
       
   102  \param uri: widget uri
       
   103  \param entryId: widget entry id (ignored)
       
   104  \param activePage: active page of home screen
       
   105  \retval void
       
   106  */
       
   107 void HsAddToHomeScreenState::addWidget(HsContentService &contentService,
       
   108                                        const QString &library, const QString &uri, int entryId)
       
   109 {
       
   110     HSMENUTEST_FUNC_ENTRY("HsAddToHomeScreenState::addWidget");
       
   111     QVariantMap params;
       
   112     params["library"] = library;
       
   113     params["uri"] = uri;
       
   114     bool ok = contentService.createWidget(params);
       
   115     if (!ok) {
       
   116         showMessageWidgetCorrupted(entryId);
       
   117     }
       
   118     logActionResult("Adding widget", entryId, ok);
       
   119     HSMENUTEST_FUNC_EXIT("HsAddToHomeScreenState::addWidget");
       
   120 }
       
   121 
       
   122 /*!
       
   123  Shows message about corrupted widget library. Deletes widget eventually
       
   124  \param itemId entryId of widget (needed to delete it)
       
   125  \retval void
       
   126  */
       
   127 #ifdef COVERAGE_MEASUREMENT
       
   128 #pragma CTC SKIP
       
   129 #endif //COVERAGE_MEASUREMENT
       
   130 void HsAddToHomeScreenState::showMessageWidgetCorrupted(int itemId)
       
   131 {
       
   132     HSMENUTEST_FUNC_ENTRY("HsCollectionState::showMessageWidgetCorrupted");
       
   133     QString message(hbTrId(
       
   134                         "txt_applib_dialog_file_corrupted_unable_to_use_wi"));
       
   135     if (HbMessageBox::question(message, hbTrId(
       
   136                                    "txt_common_button_ok"), hbTrId("txt_common_button_cancel"))) {
       
   137         HsMenuService::executeAction(itemId, removeActionIdentifier());
       
   138     }
       
   139     HSMENUTEST_FUNC_EXIT("HsCollectionState::showMessageWidgetCorrupted");
       
   140 }
       
   141 #ifdef COVERAGE_MEASUREMENT
       
   142 #pragma CTC ENDSKIP
       
   143 #endif //COVERAGE_MEASUREMENT
       
   144 /*!
       
   145  Adds a shortcut to active page of home screen
       
   146  \param shortcutService: service for adding shortcuts
       
   147  \param entryId: menu entry id
       
   148  \param activePage: active page of home screen
       
   149  \retval void
       
   150  */
       
   151 void HsAddToHomeScreenState::addShortcut(HsContentService &contentService,
       
   152         int entryId)
       
   153 {
       
   154     //ShortcutData item_id;
       
   155     //item_id.mShortcutId = entryId;
       
   156 
       
   157     HSMENUTEST_FUNC_ENTRY("HsAddToHomeScreenState::addShortcut");
       
   158     CaQuery query;
       
   159     query.setEntryTypeNames(QStringList(widgetTypeName()));
       
   160     QList<CaEntry *> entries = CaService::instance()->getEntries(query);
       
   161 
       
   162     if (mLibraryPath.isEmpty()) {
       
   163         foreach(CaEntry *entry,entries) {
       
   164             QString uri = entry->attribute(widgetUriAttributeName());
       
   165             if (uri == SHORTCUT_WIDGET_URI) {
       
   166                 mLibraryPath = entry->attribute(
       
   167                                    widgetLibraryAttributeName());
       
   168                 break;
       
   169             }
       
   170         }
       
   171     }
       
   172     if (!mLibraryPath.isEmpty()) {
       
   173         QVariantMap params;
       
   174         params["library"] = mLibraryPath;
       
   175         params["uri"] = SHORTCUT_WIDGET_URI;
       
   176         QVariantMap preferences;
       
   177         preferences[SHORTCUT_ID] = QString::number(entryId);
       
   178         params["preferences"] = preferences;
       
   179         const bool result = contentService.createWidget(params);
       
   180         logActionResult("Adding shortcut", entryId, result);
       
   181     }
       
   182     HSMENUTEST_FUNC_EXIT("HsAddToHomeScreenState::addShortcut");
       
   183 }
       
   184 
       
   185 /*!
       
   186  Provides debug logging of add widget/shortcut action
       
   187  \param operation name: results of which operation is logged
       
   188  \param entryId: menu entry id that was added to home screen
       
   189  \param activePage: active page of home screen
       
   190  \param operationSucceded: true always and only when
       
   191  logged operation succeded
       
   192  \retval void
       
   193  */
       
   194 void HsAddToHomeScreenState::logActionResult(QString operationName,
       
   195         int entryId, bool operationSucceded)
       
   196 {
       
   197     const QString
       
   198     message =
       
   199         QString("\t%1 %2 item ID: %3, active page index: %4") .arg(
       
   200             operationName) .arg(operationSucceded ? "succeded"
       
   201                                 : "failed") .arg(entryId);
       
   202 
       
   203     if (operationSucceded) {
       
   204         qDebug() << message;
       
   205     } else {
       
   206         qWarning() << message;
       
   207     }
       
   208 }