homescreenapp/stateplugins/hsmenuworkerstateplugin/src/hspreviewhswidgetstate.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 <hbmessagebox.h>
       
    22 #include <qstatemachine.h>
       
    23 #include <hbaction.h>
       
    24 #include <hbdialog.h>
       
    25 #include <hbwidget.h>
       
    26 #include <hbscrollarea.h>
       
    27 #include <hbscrollbar.h>
       
    28 
       
    29 #include "hsmenueventfactory.h"
       
    30 #include "hsmenuservice.h"
       
    31 #include "hswidgetdata.h"
       
    32 #include "hswidgethost.h"
       
    33 #include "hspreviewhswidgetstate.h"
       
    34 #include "hsmenuevent.h"
       
    35 #include "hsapp_defs.h"
       
    36 #include "hsscene.h"
       
    37 #include "hspage.h"
       
    38 
       
    39 #include "canotifier.h"
       
    40 #include "canotifierfilter.h"
       
    41 #include "caservice.h"
       
    42 
       
    43 #include "hsdomainmodel_global.h"
       
    44 #include <hscontentservice.h>
       
    45 
       
    46 /*!
       
    47  Constructor
       
    48  \param parent: parent state
       
    49  \retval void
       
    50  */
       
    51 HsPreviewHSWidgetState::HsPreviewHSWidgetState(QState *parent) :
       
    52     HsMenuBaseState("HsPreviewHSWidgetState", parent),
       
    53     mPopupDialog(0),
       
    54     mNotifier(0)
       
    55 {
       
    56     requestServices(QList<QVariant> () << CONTENT_SERVICE_KEY);
       
    57     connect(this, SIGNAL(exited()),SLOT(stateExited()));
       
    58 }
       
    59 
       
    60 /*!
       
    61  Destructor.
       
    62  */
       
    63 HsPreviewHSWidgetState::~HsPreviewHSWidgetState()
       
    64 {
       
    65     if (mNotifier) {
       
    66         delete mNotifier;
       
    67     }
       
    68     if (mPopupDialog) {
       
    69         delete mPopupDialog;
       
    70     }
       
    71 }
       
    72 
       
    73 /*!
       
    74  onEntry method invoked on entering the state
       
    75  Adds a widget or shortcut to the active page of home screen.
       
    76  It reads "data" property which value is a string map.
       
    77  The map can keep either data specific for shortcut (item id)
       
    78  or widget (entry type name, widget library name, widget uri).
       
    79  Depending on the property values it calls method for adding widget
       
    80  or shortcut.
       
    81  \param contentService: service for adding widgets
       
    82  \param shortcutService: service for adding shortcuts
       
    83  \retval void
       
    84  */
       
    85 #ifdef COVERAGE_MEASUREMENT
       
    86 #pragma CTC SKIP
       
    87 #endif //COVERAGE_MEASUREMENT
       
    88 void HsPreviewHSWidgetState::onEntry(QEvent *event)
       
    89 {
       
    90     HSMENUTEST_FUNC_ENTRY("HsPreviewHSWidgetState::onEntry");
       
    91     HsMenuBaseState::onEntry(event);
       
    92     HsMenuEvent *menuEvent = static_cast<HsMenuEvent *>(event);
       
    93     QVariantMap data = menuEvent->data();
       
    94 
       
    95     const int entryId = data.value(itemIdKey()).toInt();
       
    96 
       
    97     QVariantMap widgetData;
       
    98     widgetData.insert("library", data.value(widgetLibraryAttributeName()).toString());
       
    99     widgetData.insert("uri", data.value(widgetUriAttributeName()).toString());
       
   100     HsWidgetHost *widget = contentService()->createWidgetForPreview(widgetData);
       
   101 
       
   102     if (widget) {
       
   103         widget->setMinimumSize(widget->preferredWidth(),widget->preferredHeight());
       
   104         HbScrollArea *scrollArea = new HbScrollArea();
       
   105         scrollArea->setClampingStyle(HbScrollArea::StrictClamping);
       
   106         scrollArea->setScrollingStyle(HbScrollArea::Pan);
       
   107         scrollArea->setHorizontalScrollBarPolicy(HbScrollArea::ScrollBarAsNeeded);
       
   108         scrollArea->setHorizontalScrollBarPolicy(HbScrollArea::ScrollBarAutoHide);//
       
   109         scrollArea->setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAsNeeded);
       
   110         scrollArea->setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAutoHide);//
       
   111         scrollArea->verticalScrollBar()->setInteractive(true);
       
   112         scrollArea->horizontalScrollBar()->setInteractive(true);
       
   113         // this sets up the scroll area to scroll in both directions
       
   114         scrollArea->setScrollDirections(Qt::Vertical | Qt::Horizontal);
       
   115         scrollArea->setContentWidget(widget);   //ownership transferred
       
   116         scrollArea->setAlignment(Qt::AlignCenter);
       
   117 
       
   118         // Instantiate a popup
       
   119         mPopupDialog = new HbDialog();
       
   120 
       
   121         // Set dismiss policy that determines what tap events will cause the dialog
       
   122         // to be dismissed
       
   123         mPopupDialog->setDismissPolicy(HbDialog::NoDismiss);
       
   124         mPopupDialog->setTimeout(HbDialog::NoTimeout);
       
   125 
       
   126         // Set content widget
       
   127         mPopupDialog->setContentWidget(scrollArea); //ownership transferred
       
   128 
       
   129         // Sets the primary action and secondary action
       
   130         mPopupDialog->setPrimaryAction(new HbAction(hbTrId("txt_applib_button_add_to_homescreen"),mPopupDialog));
       
   131         mPopupDialog->setSecondaryAction(new HbAction(hbTrId("txt_common_button_close"),mPopupDialog));
       
   132 
       
   133         subscribeForMemoryCardRemove(entryId);
       
   134         widget->initializeWidget();
       
   135         widget->showWidget();
       
   136         // Launch popup syncronously
       
   137         HbAction *result = mPopupDialog->exec();
       
   138 
       
   139         disconnect(mNotifier,
       
   140                    SIGNAL(entryChanged(const CaEntry &, ChangeType)),
       
   141                    this, SLOT(memoryCardRemoved()));
       
   142 
       
   143         if (result == mPopupDialog->primaryAction()) {
       
   144             // take it back from scrollarea
       
   145             scrollArea->takeContentWidget();
       
   146             widget->hideWidget();
       
   147             widget->uninitializeWidget();
       
   148             HsScene::instance()->activePage()->addNewWidget(widget);
       
   149         } else {
       
   150             widget->deleteFromDatabase();
       
   151         }
       
   152 
       
   153         delete result;
       
   154         result = NULL;
       
   155 
       
   156     } else {
       
   157         showMessageWidgetCorrupted(entryId);
       
   158     }
       
   159 
       
   160     HSMENUTEST_FUNC_EXIT("HsPreviewHSWidgetState::onEntry");
       
   161 }
       
   162 #ifdef COVERAGE_MEASUREMENT
       
   163 #pragma CTC ENDSKIP
       
   164 #endif //COVERAGE_MEASUREMENT
       
   165 
       
   166 /*!
       
   167  State exited.
       
   168  \retval void
       
   169  */
       
   170 void HsPreviewHSWidgetState::stateExited()
       
   171 {
       
   172     HSMENUTEST_FUNC_ENTRY("HsPreviewHSWidgetState::stateExited");
       
   173 
       
   174     if (mNotifier) {
       
   175         delete mNotifier;
       
   176         mNotifier = NULL;
       
   177     }
       
   178 
       
   179     if (mPopupDialog) {
       
   180         delete mPopupDialog;
       
   181         mPopupDialog = NULL;
       
   182     }
       
   183 
       
   184     HSMENUTEST_FUNC_EXIT("HsPreviewHSWidgetState::stateExited");
       
   185     qDebug("HsPreviewHSWidgetState::stateExited()");
       
   186 }
       
   187 
       
   188 /*!
       
   189  Memory card with instaled widget was removed.
       
   190  \retval void
       
   191  */
       
   192 void HsPreviewHSWidgetState::memoryCardRemoved()
       
   193 {
       
   194     if (mPopupDialog) {
       
   195         mPopupDialog->close();
       
   196     }
       
   197 }
       
   198 
       
   199 /*!
       
   200  Subscribe for memory card remove.
       
   201  \param entryId: HSWidget id.
       
   202  \retval void
       
   203  */
       
   204 void HsPreviewHSWidgetState::subscribeForMemoryCardRemove(int entryId)
       
   205 {
       
   206     CaNotifierFilter filter;
       
   207     QList<int> entryIds;
       
   208     entryIds.append(entryId);
       
   209     filter.setIds(entryIds);
       
   210     mNotifier = CaService::instance()->createNotifier(filter);
       
   211     mNotifier->setParent(this);
       
   212     connect(mNotifier,
       
   213             SIGNAL(entryChanged(const CaEntry &, ChangeType)),
       
   214             SLOT(memoryCardRemoved()));
       
   215 }
       
   216 
       
   217 
       
   218 /*!
       
   219  Shows message about corrupted widget library. Deletes widget eventually
       
   220  \param itemId entryId of widget (needed to delete it)
       
   221  \retval void
       
   222  */
       
   223 #ifdef COVERAGE_MEASUREMENT
       
   224 #pragma CTC SKIP
       
   225 #endif //COVERAGE_MEASUREMENT
       
   226 void HsPreviewHSWidgetState::showMessageWidgetCorrupted(int itemId)
       
   227 {
       
   228     HSMENUTEST_FUNC_ENTRY("HsCollectionState::showMessageWidgetCorrupted");
       
   229     QString message(hbTrId("txt_applib_dialog_file_corrupted_unable_to_use_wi"));
       
   230     if (HbMessageBox::question(message,hbTrId(
       
   231                                    "txt_common_button_ok"), hbTrId("txt_common_button_cancel"))) {
       
   232         HsMenuService::executeAction(itemId, removeActionIdentifier());
       
   233     }
       
   234     HSMENUTEST_FUNC_EXIT("HsCollectionState::showMessageWidgetCorrupted");
       
   235 }
       
   236 #ifdef COVERAGE_MEASUREMENT
       
   237 #pragma CTC ENDSKIP
       
   238 #endif //COVERAGE_MEASUREMENT
       
   239