homescreenapp/stateplugins/hsapplibrarystateplugin/src/hsallappsstate.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 All Applications state.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <qstatemachine.h>
       
    19 #include <hbview.h>
       
    20 #include <hblistview.h>
       
    21 #include <hbaction.h>
       
    22 #include <hbmenu.h>
       
    23 #include <hbabstractviewitem.h>
       
    24 #include <hsmenueventfactory.h>
       
    25 #include <hbinstance.h>
       
    26 #include <caentry.h>
       
    27 #include <hbsearchpanel.h>
       
    28 
       
    29 #include "hsmenuitemmodel.h"
       
    30 #include "hsmenuview.h"
       
    31 #include "cadefs.h"
       
    32 #include "hsallappsstate.h"
       
    33 #include "hsaddappstocollectionstate.h"
       
    34 #include "hsapp_defs.h"
       
    35 #include "hsmenumodetransition.h"
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 HsAllAppsState::HsAllAppsState(HsMenuView &menuView, QState *parent) :
       
    41     QState(parent), mSortAttribute(AscendingNameHsSortAttribute),
       
    42     mCollectionsSortAttribute(LatestOnTopHsSortAttribute),
       
    43     mMenuView(menuView), mAllAppsModel(0)
       
    44 {
       
    45     construct();
       
    46 }
       
    47 
       
    48 // ---------------------------------------------------------------------------
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 void HsAllAppsState::construct()
       
    52 {
       
    53     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::construct");
       
    54 
       
    55     QState *initialState = new QState(this);
       
    56     setInitialState(initialState);
       
    57 
       
    58     QState *addModeState = new QState(this);
       
    59     connect(addModeState, SIGNAL(entered()),SLOT(addModeEntered()));
       
    60 
       
    61     QState *normalModeState = new QState(this);
       
    62     connect(normalModeState, SIGNAL(entered()),SLOT(normalModeEntered()));
       
    63 
       
    64     initialState->addTransition(new HsMenuModeTransition(
       
    65                                     mMenuView, NormalHsMenuMode, normalModeState));
       
    66     initialState->addTransition(new HsMenuModeTransition(
       
    67                                     mMenuView, AddHsMenuMode, addModeState));
       
    68 
       
    69     setObjectName(this->parent()->objectName() + "/allappsstate");
       
    70     connect(this, SIGNAL(entered()),SLOT(stateEntered()));
       
    71     connect(this, SIGNAL(exited()),SLOT(stateExited()));
       
    72     mAllAppsModel = HsMenuService::getAllApplicationsModel(mSortAttribute);
       
    73     HSMENUTEST_FUNC_EXIT("HsAllAppsState::construct");
       
    74 }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 void HsAllAppsState::setMenuOptions()
       
    80 {
       
    81     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::setMenuOptions");
       
    82     HbMenu *const mOptions = new HbMenu();
       
    83     mOptions->addAction(hbTrId("txt_applib_opt_add_to_collection"),
       
    84                         this,
       
    85                         SLOT(addToCollection()));
       
    86 
       
    87     HbMenu *const sortMenu = mOptions->addMenu(hbTrId(
       
    88                                  "txt_applib_opt_sort_by"));
       
    89     //Grouped options are exclusive by default.
       
    90     QActionGroup *sortGroup = new QActionGroup(mOptions);
       
    91 
       
    92     sortGroup->addAction(
       
    93         sortMenu->addAction(hbTrId("txt_applib_opt_sub_ascending"),
       
    94                             this,
       
    95                             SLOT(ascendingMenuAction())));
       
    96     sortGroup->addAction(
       
    97         sortMenu->addAction(hbTrId("txt_applib_opt_sub_descending"),
       
    98                             this,
       
    99                             SLOT(descendingMenuAction())));
       
   100     mOptions->setParent(this);
       
   101     foreach(QAction *action, sortMenu->actions()) {
       
   102         action->setCheckable(true);
       
   103     }
       
   104     int currentSortingPosition(-1);
       
   105     switch (mSortAttribute) {
       
   106     case AscendingNameHsSortAttribute:
       
   107         currentSortingPosition = 0;
       
   108         break;
       
   109     case DescendingNameHsSortAttribute:
       
   110         currentSortingPosition = 1;
       
   111         break;
       
   112     default:
       
   113         break;
       
   114     }
       
   115     if (currentSortingPosition >= 0) {
       
   116         sortGroup->actions().at(currentSortingPosition)->setChecked(true);
       
   117     }
       
   118     mMenuView.view()->setMenu(mOptions);
       
   119     HSMENUTEST_FUNC_EXIT("HsAllAppsState::setMenuOptions");
       
   120 }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 HsAllAppsState::~HsAllAppsState()
       
   126 {
       
   127     delete mAllAppsModel;
       
   128 }
       
   129 
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // ---------------------------------------------------------------------------
       
   133 //
       
   134 void HsAllAppsState::scrollToBeginning()
       
   135 {
       
   136     mBookmark = mAllAppsModel->index(0);
       
   137 }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 void HsAllAppsState::stateEntered()
       
   143 {
       
   144     qDebug("AllAppsState::stateEntered()");
       
   145     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::stateEntered");
       
   146 
       
   147     mMenuView.setModel(mAllAppsModel);
       
   148     mMenuView.scrollTo(mBookmark, HbAbstractItemView::PositionAtTop);
       
   149     HSMENUTEST_FUNC_EXIT("HsAllAppsState::stateEntered");
       
   150 }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void HsAllAppsState::normalModeEntered()
       
   156 {
       
   157     setMenuOptions();
       
   158 
       
   159     connect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   160             SLOT(listItemActivated(QModelIndex)));
       
   161     connect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   162             SLOT(listItemLongPressed(HbAbstractViewItem *, QPointF)));
       
   163 }
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 void HsAllAppsState::addModeEntered()
       
   169 {
       
   170     connect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   171             SLOT(addActivated(QModelIndex)));
       
   172     connect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   173             SLOT(addLongPressed(HbAbstractViewItem *, QPointF)));
       
   174 }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // ---------------------------------------------------------------------------
       
   178 //
       
   179 void HsAllAppsState::stateExited()
       
   180 {
       
   181     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::stateExited");
       
   182     mBookmark = mMenuView.firstVisibleItemIndex();
       
   183 
       
   184     mMenuView.disconnect(this);
       
   185     mMenuView.view()->setMenu(NULL);
       
   186 
       
   187     HSMENUTEST_FUNC_EXIT("HsAllAppsState::stateExited");
       
   188     qDebug("AllAppsState::stateExited()");
       
   189 }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // ---------------------------------------------------------------------------
       
   193 //
       
   194 void HsAllAppsState::listItemActivated(const QModelIndex &index)
       
   195 {
       
   196     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::listItemActivated");
       
   197 
       
   198     const CaEntry *entry = mAllAppsModel->entry(index);
       
   199 
       
   200     if (entry->entryTypeName() == widgetTypeName()) {
       
   201         machine()->postEvent(
       
   202             HsMenuEventFactory::createPreviewHSWidgetEvent(
       
   203                 entry->id(), entry->entryTypeName(), entry->attribute(
       
   204                     widgetUriAttributeName()), entry->attribute(
       
   205                     widgetLibraryAttributeName())));
       
   206 
       
   207         const int itemId = index.data(CaItemModel::IdRole).toInt();
       
   208         HsMenuService::touch(itemId);
       
   209     } else {
       
   210         QVariant data = mAllAppsModel->data(index, CaItemModel::IdRole);
       
   211         HsMenuService::executeAction(data.toInt());
       
   212     }
       
   213 
       
   214     mMenuView.setSearchPanelVisible(false);
       
   215 
       
   216     HSMENUTEST_FUNC_EXIT("HsAllAppsState::listItemActivated");
       
   217 }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // ---------------------------------------------------------------------------
       
   221 //
       
   222 void HsAllAppsState::addActivated(const QModelIndex &index)
       
   223 {
       
   224     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::addActivated");
       
   225     mMenuView.setSearchPanelVisible(false);
       
   226     addToHomeScreen(index);
       
   227     machine()->postEvent(
       
   228         HsMenuEventFactory::createOpenHomeScreenEvent());
       
   229     HSMENUTEST_FUNC_EXIT("HsAllAppsState::addActivated");
       
   230 }
       
   231 
       
   232 /*!
       
   233  Handles long-item-pressed event in all apps view by showing context menu
       
   234  \param item the event pertains to
       
   235  \param position at which context menu is shown
       
   236  \retval void
       
   237  */
       
   238 void HsAllAppsState::listItemLongPressed(HbAbstractViewItem *item,
       
   239         const QPointF &coords)
       
   240 {
       
   241     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::listItemLongPressed");
       
   242 
       
   243     mMenuView.setSearchPanelVisible(false);
       
   244 
       
   245     // create context menu
       
   246     HbMenu *menu = new HbMenu();
       
   247 
       
   248     HbAction *addToHomeScreenAction = menu->addAction(hbTrId(
       
   249                                           "txt_applib_menu_add_to_home_screen"));
       
   250 
       
   251     HbAction *addToCollectionAction = menu->addAction(hbTrId(
       
   252                                           "txt_applib_menu_add_to_collection"));
       
   253 
       
   254     HbAction *uninstallAction = menu->addAction(hbTrId(
       
   255                                     "txt_common_menu_delete"));
       
   256 
       
   257     // check conditions and hide irrelevant menu items
       
   258 
       
   259     EntryFlags flags = item->modelIndex().data(
       
   260                            CaItemModel::FlagsRole).value<EntryFlags> ();
       
   261 
       
   262     if (!(flags & RemovableEntryFlag)) {
       
   263         uninstallAction->setVisible(false);
       
   264     }
       
   265     // choose proper action
       
   266     if (HbAction *selectedAction = menu->exec(coords)) {
       
   267         if (selectedAction == addToHomeScreenAction) {
       
   268             addToHomeScreen(item->modelIndex());
       
   269         } else if (selectedAction == addToCollectionAction) {
       
   270             // Addding a specific application to
       
   271             // an existing collection via item specific menu.
       
   272             const int itemId =
       
   273                 item->modelIndex().data(CaItemModel::IdRole).toInt();
       
   274             machine()->postEvent(
       
   275                 HsMenuEventFactory::createAddAppsFromApplicationsViewEvent(
       
   276                     mSortAttribute, mCollectionsSortAttribute, itemId));
       
   277 
       
   278         } else if (selectedAction == uninstallAction) {
       
   279             const int itemId =
       
   280                 item->modelIndex().data(CaItemModel::IdRole).toInt();
       
   281             HsMenuService::executeAction(itemId, removeActionIdentifier());
       
   282         }
       
   283     }
       
   284     delete menu;
       
   285     HSMENUTEST_FUNC_EXIT("HsAllAppsState::listItemLongPressed");
       
   286 }
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // ---------------------------------------------------------------------------
       
   290 //
       
   291 void HsAllAppsState::addLongPressed(HbAbstractViewItem *item,
       
   292                                     const QPointF &coords)
       
   293 {
       
   294     Q_UNUSED(coords);
       
   295     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::addLongPressed");
       
   296     mMenuView.setSearchPanelVisible(false);
       
   297     addToHomeScreen(item->modelIndex());
       
   298     machine()->postEvent(
       
   299         HsMenuEventFactory::createOpenHomeScreenEvent());
       
   300     HSMENUTEST_FUNC_EXIT("HsAllAppsState::addLongPressed");
       
   301 
       
   302 }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 void HsAllAppsState::addToCollection()
       
   308 {
       
   309     // Add one/many applications to
       
   310     // a new/an existing collection via the All view
       
   311     machine()->postEvent(
       
   312         HsMenuEventFactory::createAddAppsFromApplicationsViewEvent(
       
   313             mSortAttribute, mCollectionsSortAttribute));
       
   314 }
       
   315 
       
   316 /*!
       
   317  Triggers event so that a state adding to Home Screen is reached
       
   318  \param index of an item to be added to homescreen
       
   319  \retval void
       
   320  */
       
   321 void HsAllAppsState::addToHomeScreen(const QModelIndex &index)
       
   322 {
       
   323     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::addToHomeScreen");
       
   324     const CaEntry *entry = mAllAppsModel->entry(index);
       
   325 
       
   326     machine()-> postEvent(HsMenuEventFactory::createAddToHomeScreenEvent(
       
   327                               entry->id(), entry->entryTypeName(), entry->attribute(
       
   328                                   widgetUriAttributeName()), entry->attribute(
       
   329                                   widgetLibraryAttributeName())));
       
   330     HSMENUTEST_FUNC_EXIT("HsAllAppsState::addToHomeScreen");
       
   331 }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void HsAllAppsState::ascendingMenuAction()
       
   337 {
       
   338     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::ascendingMenuAction");
       
   339     mSortAttribute = AscendingNameHsSortAttribute;
       
   340     mAllAppsModel->setSort(mSortAttribute);
       
   341     HSMENUTEST_FUNC_EXIT("HsAllAppsState::ascendingMenuAction");
       
   342 }
       
   343 
       
   344 // ---------------------------------------------------------------------------
       
   345 // ---------------------------------------------------------------------------
       
   346 //
       
   347 void HsAllAppsState::collectionsSortOrder(
       
   348     HsSortAttribute sortOrder)
       
   349 {
       
   350     mCollectionsSortAttribute = sortOrder;
       
   351 }
       
   352 
       
   353 // ---------------------------------------------------------------------------
       
   354 // ---------------------------------------------------------------------------
       
   355 //
       
   356 void HsAllAppsState::descendingMenuAction()
       
   357 {
       
   358     HSMENUTEST_FUNC_ENTRY("HsAllAppsState::descendingMenuAction");
       
   359     mSortAttribute = DescendingNameHsSortAttribute;
       
   360     mAllAppsModel->setSort(mSortAttribute);
       
   361     HSMENUTEST_FUNC_EXIT("HsAllAppsState::descendingMenuAction");
       
   362 
       
   363 }