homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsbaseviewstate.cpp
branchRCL_3
changeset 82 5f0182e07bfb
equal deleted inserted replaced
79:f00a6757af32 82:5f0182e07bfb
       
     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: Base for menu view states.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QScopedPointer>
       
    19 #include <QStateMachine>
       
    20 #include <HbMessageBox>
       
    21 #include <HbParameterLengthLimiter>
       
    22 #include <HbAction>
       
    23 #include <HbMenu>
       
    24 #include <HbAbstractViewItem>
       
    25 #include <HbView>
       
    26 #include <HbListView>
       
    27 
       
    28 #include <canotifier.h>
       
    29 
       
    30 #include "hsbaseviewstate.h"
       
    31 #include "hsmenueventfactory.h"
       
    32 #include "hsmenudialogfactory.h"
       
    33 #include "hsmenuitemmodel.h"
       
    34 #include "hsmenumodetransition.h"
       
    35 #include "hsmenuentryremovedhandler.h"
       
    36 #include "hsmainwindow.h"
       
    37 #include "hsmenumodewrapper.h"
       
    38 
       
    39 
       
    40 /*!
       
    41  Constructor.
       
    42  \param mainWindow main window wrapper object.
       
    43  \param parent Owner.
       
    44  */
       
    45 HsBaseViewState::HsBaseViewState(HsMainWindow &mainWindow, QState *parent):
       
    46     QState(parent),
       
    47     mApplicationLaunchFailMessage(0),
       
    48     mModel(0),
       
    49     mContextMenu(0),
       
    50     mContextModelIndex(),
       
    51     mBackKeyAction(0),
       
    52     mMenuView(0),
       
    53     mMenuMode(0),
       
    54     mMainWindow(mainWindow)
       
    55 {
       
    56     mBackKeyAction = new HbAction(Hb::BackNaviAction, this);
       
    57     mViewOptions = new HbMenu;
       
    58 }
       
    59 
       
    60 /*!
       
    61  Constructor.
       
    62  \param mainWindow main window wrapper object.
       
    63  \param menuMode menu mode object.
       
    64  \param parent Owner.
       
    65  */
       
    66 HsBaseViewState::HsBaseViewState(HsMainWindow &mainWindow,
       
    67     HsMenuModeWrapper& menuMode,
       
    68     QState *parent):
       
    69     QState(parent),
       
    70     mApplicationLaunchFailMessage(0),
       
    71     mModel(0),
       
    72     mContextMenu(0),
       
    73     mContextModelIndex(),
       
    74     mBackKeyAction(0),
       
    75     mMenuView(0),
       
    76     mMenuMode(&menuMode),
       
    77     mMainWindow(mainWindow)
       
    78 {
       
    79     mBackKeyAction = new HbAction(Hb::BackNaviAction, this);
       
    80     mViewOptions = new HbMenu;
       
    81 }
       
    82 /*!
       
    83  Initialize contained objects.
       
    84  \param menuViewBuilder object providing widgets for menu view.
       
    85  \param stateContext state context of the view the builder
       
    86      is to provide widgets for.
       
    87  */
       
    88 void HsBaseViewState::initialize(HsMenuViewBuilder &menuViewBuilder,
       
    89     HsStateContext stateContext)
       
    90 {
       
    91     mMenuView.reset(new HsMenuView(menuViewBuilder, stateContext, mMainWindow));
       
    92     mMenuView->view()->setNavigationAction(mBackKeyAction);
       
    93     mMenuView->view()->setMenu(mViewOptions);
       
    94 
       
    95     connect(this, SIGNAL(entered()),SLOT(stateEntered()));
       
    96     connect(this, SIGNAL(exited()),SLOT(stateExited()));
       
    97 }
       
    98 
       
    99 /*!
       
   100  Creates and open application launch fail message.
       
   101  \param errorCode eroor code to display.
       
   102  \param itemId id of the launched item.
       
   103  */
       
   104 void HsBaseViewState::createApplicationLaunchFailMessage(int errorCode,
       
   105     int itemId)
       
   106 {
       
   107     QString message;
       
   108     message.append(
       
   109         HbParameterLengthLimiter("txt_applib_info_launching_the_application_failed").arg(
       
   110             errorCode));
       
   111 
       
   112     // create and show message box
       
   113     mApplicationLaunchFailMessage = HsMenuDialogFactory().create(
       
   114             message, HsMenuDialogFactory::Close);
       
   115 
       
   116     QScopedPointer<HsMenuEntryRemovedHandler> entryObserver(
       
   117         new HsMenuEntryRemovedHandler(itemId, mApplicationLaunchFailMessage.data(), SLOT(close())));
       
   118 
       
   119     entryObserver.take()->setParent(mApplicationLaunchFailMessage.data());
       
   120 
       
   121     mApplicationLaunchFailMessage->open();
       
   122 }
       
   123 
       
   124 /*!
       
   125  Slot invoked when a state is entered.
       
   126  */
       
   127 void HsBaseViewState::stateEntered()
       
   128 {
       
   129     qDebug("HsBaseViewState::stateEntered()");
       
   130     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::stateEntered");
       
   131     mMenuView->activate();
       
   132     connect(mMenuView.data(), SIGNAL(listViewChange()),
       
   133             this, SLOT(closeContextMenu()));
       
   134     HSMENUTEST_FUNC_EXIT("HsBaseViewState::stateEntered");
       
   135 }
       
   136 
       
   137 /*!
       
   138  Slot invoked when a state is exited.
       
   139  */
       
   140 void HsBaseViewState::stateExited()
       
   141 {
       
   142     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::stateExited");
       
   143 
       
   144     mMenuView->hideSearchPanel();
       
   145     mMenuView->disconnect(this);
       
   146 
       
   147     mMenuView->inactivate();
       
   148     if (!mApplicationLaunchFailMessage.isNull()) {
       
   149         mApplicationLaunchFailMessage->close();
       
   150     }
       
   151     closeContextMenu();
       
   152     HSMENUTEST_FUNC_EXIT("HsBaseViewState::stateExited");
       
   153 }
       
   154 
       
   155 /*!
       
   156  Add mode entered.
       
   157  */
       
   158 void HsBaseViewState::addModeEntered()
       
   159 {
       
   160     mViewOptions = mMenuView->view()->takeMenu();
       
   161     connect(mMenuView.data(),
       
   162             SIGNAL(activated(QModelIndex)),
       
   163             SLOT(addActivated(QModelIndex)));
       
   164     connect(mMenuView.data(),
       
   165             SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   166             SLOT(addLongPressed(HbAbstractViewItem *, QPointF)));
       
   167 }
       
   168 
       
   169 /*!
       
   170  Add mode exited.
       
   171  */
       
   172 void HsBaseViewState::addModeExited()
       
   173 {
       
   174     mMenuView->view()->setMenu(mViewOptions);
       
   175 }
       
   176 
       
   177 /*!
       
   178  Slot invoked when normal mode entered.
       
   179  */
       
   180 void HsBaseViewState::normalModeEntered()
       
   181 {
       
   182     setMenuOptions();
       
   183     connect(mMenuView.data(),
       
   184             SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   185             SLOT(showContextMenu(HbAbstractViewItem *, QPointF)));
       
   186 }
       
   187 
       
   188 /*!
       
   189  Destructor.
       
   190  */
       
   191 HsBaseViewState::~HsBaseViewState()
       
   192 {
       
   193     delete mModel;
       
   194     mViewOptions = mMenuView->view()->takeMenu();
       
   195     delete mViewOptions;
       
   196 }
       
   197 
       
   198 /*!
       
   199  Slot connected to List widget in normal mode.
       
   200  \param index Model index of the activated item.
       
   201  */
       
   202 void HsBaseViewState::launchItem(const QModelIndex &index)
       
   203 {
       
   204     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::launchItem");
       
   205 
       
   206     QSharedPointer<const CaEntry> entry = mModel->entry(index);
       
   207     if (!entry.isNull() && !(entry->flags() & UninstallEntryFlag)) {
       
   208         if (entry->entryTypeName() == Hs::widgetTypeName) {
       
   209             machine()->postEvent(HsMenuEventFactory::createPreviewHSWidgetEvent(entry->id(),
       
   210                 entry->entryTypeName(), entry->attribute(Hs::widgetUriAttributeName),
       
   211                 entry->attribute(Hs::widgetLibraryAttributeName)));
       
   212             HsMenuService::touch(entry->id());
       
   213         }
       
   214         else {
       
   215             int errCode = HsMenuService::executeAction(entry->id());
       
   216             if (errCode != 0) {
       
   217                 createApplicationLaunchFailMessage(errCode, entry->id());
       
   218             }
       
   219         }
       
   220     }
       
   221     HSMENUTEST_FUNC_EXIT("HsBaseViewState::launchItem");
       
   222 }
       
   223 
       
   224 /*!
       
   225  Slot connected to List widget in normal mode.
       
   226  \param index Model index of the activated item.
       
   227  */
       
   228 void HsBaseViewState::openCollection(const QModelIndex &index)
       
   229 {
       
   230     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::openCollection");
       
   231     QVariant data = mModel->data(index, CaItemModel::IdRole);
       
   232     int id = data.toInt();
       
   233     QString collectionType = mModel->data(
       
   234             index, CaItemModel::TypeRole).toString();
       
   235     qDebug("HsBaseViewState::openCollection - MCS ID: %d", data.toInt());
       
   236 
       
   237     machine()->postEvent(
       
   238             HsMenuEventFactory::createOpenCollectionFromAppLibraryEvent(
       
   239                     id, collectionType));
       
   240     HSMENUTEST_FUNC_EXIT("HsBaseViewState::openCollection");
       
   241 }
       
   242 
       
   243 /*!
       
   244  Slot connected to List widget in normal mode.
       
   245  \param index Model index of the activated item.
       
   246  */
       
   247 void HsBaseViewState::showContextMenu(HbAbstractViewItem *item, const QPointF &coords)
       
   248 {
       
   249     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::showContextMenu");
       
   250 
       
   251     EntryFlags flags = item->modelIndex().data(
       
   252             CaItemModel::FlagsRole).value<EntryFlags> ();
       
   253 
       
   254     if (!(flags & UninstallEntryFlag)) {
       
   255         mContextMenu = new HbMenu;
       
   256         setContextMenuOptions(item,flags);
       
   257         mContextModelIndex = item->modelIndex();
       
   258         mContextMenu->setPreferredPos(coords);
       
   259         mContextMenu->setAttribute(Qt::WA_DeleteOnClose);
       
   260         mContextMenu->open(this, SLOT(contextMenuAction(HbAction*)));
       
   261     }
       
   262     HSMENUTEST_FUNC_EXIT("HsBaseViewState::showContextMenu");
       
   263 
       
   264 }
       
   265 
       
   266 /*!
       
   267  Open task switcher.
       
   268  \retval true if operation is successful.
       
   269  */
       
   270 bool HsBaseViewState::openTaskSwitcher()
       
   271 {
       
   272     return HsMenuService::launchTaskSwitcher();
       
   273 }
       
   274 
       
   275 /*!
       
   276  Menu softkey back action slot
       
   277  */
       
   278 void HsBaseViewState::openAppLibrary()
       
   279 {
       
   280     machine()->postEvent(HsMenuEventFactory::createOpenAppLibraryEvent());
       
   281 }
       
   282 
       
   283 /*!
       
   284  Triggers event so that a state adding to Home Screen is reached
       
   285  \param entryId of an item to be added to homescreen.
       
   286  \retval void
       
   287  */
       
   288 void HsBaseViewState::addToHomeScreen(const int entryId)
       
   289 {
       
   290     HSMENUTEST_FUNC_ENTRY("HsBaseViewState::addToHomeScreen");
       
   291     machine()->postEvent(
       
   292         HsMenuEventFactory::createAddToHomeScreenEvent(
       
   293             entryId, mMenuMode->getHsMenuMode(), mMenuMode->getHsToken()));
       
   294     HSMENUTEST_FUNC_EXIT("HsBaseViewState::addToHomeScreen");
       
   295 }
       
   296 
       
   297 /*!
       
   298  Check software updates.
       
   299  \retval 0 if operation is successful.
       
   300  */
       
   301 int HsBaseViewState::checkSoftwareUpdates()
       
   302 {
       
   303     int errCode = HsMenuService::launchSoftwareUpdate();
       
   304     if (errCode != 0){
       
   305         createApplicationLaunchFailMessage(errCode,0);
       
   306     }
       
   307     return errCode;
       
   308 }
       
   309 
       
   310 /*!
       
   311  close context menu
       
   312  */
       
   313 void HsBaseViewState::closeContextMenu()
       
   314 {
       
   315     if (mContextMenu) {
       
   316         mContextMenu->close();
       
   317     }    
       
   318 }
       
   319 
       
   320 /*!
       
   321  Handles context menu actions
       
   322  \param action action taken in context menu
       
   323  */
       
   324 void HsBaseViewState::contextMenuAction(HbAction *action)
       
   325 {
       
   326     Hs::HsContextAction command =
       
   327         static_cast<Hs::HsContextAction>(action->data().toInt());
       
   328 
       
   329     const int itemId = mContextModelIndex.data(CaItemModel::IdRole).toInt();
       
   330 
       
   331     switch (command) {
       
   332         case Hs::AddToHomeScreenContextAction:
       
   333             addToHomeScreen(itemId);
       
   334             break;
       
   335         case Hs::AddToCollectionFromApplicationsViewContextAction:
       
   336             // Addding a specific application to
       
   337             // an existing collection via item specific menu.
       
   338             machine()->postEvent(
       
   339                 HsMenuEventFactory::createAddAppsFromApplicationsViewEvent(
       
   340                     Hs::NoHsSortAttribute, itemId));
       
   341             break;
       
   342         case Hs::AddToCollectionFromCollectionViewContextAction:
       
   343             machine()->postEvent(
       
   344                 HsMenuEventFactory::createAddAppsFromCollectionViewEvent(
       
   345                     -1, itemId));
       
   346             break;
       
   347         case Hs::UninstallContextAction:
       
   348             machine()->postEvent(
       
   349                 HsMenuEventFactory::createUninstallApplicationEvent(
       
   350                     itemId));
       
   351             break;
       
   352         case Hs::AppSettingContextAction:
       
   353             machine()->postEvent(
       
   354                 HsMenuEventFactory::createAppSettingsViewEvent(itemId));
       
   355             break;
       
   356         case Hs::AppDetailsContextAction:
       
   357             machine()->postEvent(
       
   358                 HsMenuEventFactory::createAppDetailsViewEvent(itemId));
       
   359             break;
       
   360         case Hs::OpenAppContextAction:
       
   361             launchItem(mContextModelIndex);
       
   362             break;
       
   363         case Hs::RenameCollectionContextAction:
       
   364             machine()->postEvent(
       
   365                 HsMenuEventFactory::createRenameCollectionEvent(itemId));
       
   366             break;
       
   367         case Hs::DeleteCollectionContextAction:
       
   368              machine()->postEvent(
       
   369                 HsMenuEventFactory::createDeleteCollectionEvent(itemId));
       
   370             break;
       
   371         case Hs::OpenCollectionContextAction:
       
   372             openCollection(mContextModelIndex);
       
   373             break;
       
   374         default:
       
   375             break;
       
   376     }
       
   377     
       
   378     HsMenuService::touch(itemId);
       
   379 }
       
   380 
       
   381 /*!
       
   382  Slot connected to List widget in add mode.
       
   383  \param index Model index of the activated item.
       
   384  */
       
   385 void HsBaseViewState::addActivated(const QModelIndex &index)
       
   386 {
       
   387     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::addActivated");
       
   388     mMenuView->disconnect(this);
       
   389     const int itemId = index.data(CaItemModel::IdRole).toInt();
       
   390     addToHomeScreen(itemId);
       
   391     HsMenuService::touch(itemId);
       
   392     HSMENUTEST_FUNC_EXIT("HsAllAppsState::addActivated");
       
   393 }
       
   394 
       
   395 /*!
       
   396  Slot connected to List widget in add mode.
       
   397  Called when item long pressed.
       
   398  \param item View item.
       
   399  \param coords Press point coordinates.
       
   400  */
       
   401 void HsBaseViewState::addLongPressed(HbAbstractViewItem *item,
       
   402                                     const QPointF &coords)
       
   403 {
       
   404     Q_UNUSED(coords);
       
   405     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::addLongPressed");
       
   406     addActivated(item->modelIndex());
       
   407     HSMENUTEST_FUNC_EXIT("HsAllAppsState::addLongPressed");
       
   408 }
       
   409 
       
   410 /*!
       
   411  Scrolls view to first item at top
       
   412  */
       
   413 void HsBaseViewState::scrollToBeginning()
       
   414 {
       
   415     mMenuView->listView()->scrollTo(
       
   416             mModel->index(0), HbAbstractItemView::PositionAtTop);
       
   417 }
       
   418 
       
   419 /*!
       
   420  Normal mode exited dummy implementation.
       
   421  */
       
   422 void HsBaseViewState::normalModeExited()
       
   423 {
       
   424 }
       
   425 
       
   426 /*!
       
   427  Defines transitions
       
   428  */
       
   429 void HsBaseViewState::defineTransitions()
       
   430 {
       
   431     QState *initialState = new QState(this);
       
   432     setInitialState(initialState);
       
   433 
       
   434     QState *addModeState = new QState(this);
       
   435     connect(addModeState, SIGNAL(entered()),SLOT(addModeEntered()));
       
   436     connect(addModeState, SIGNAL(exited()),SLOT(addModeExited()));
       
   437 
       
   438     QState *normalModeState = new QState(this);
       
   439     connect(normalModeState, SIGNAL(entered()),SLOT(normalModeEntered()));
       
   440     connect(normalModeState, SIGNAL(exited()),SLOT(normalModeExited()));
       
   441 
       
   442     initialState->addTransition(new HsMenuModeTransition(
       
   443             *mMenuMode, Hs::NormalHsMenuMode, normalModeState));
       
   444     initialState->addTransition(new HsMenuModeTransition(
       
   445             *mMenuMode, Hs::AddHsMenuMode, addModeState));
       
   446 }
       
   447