homescreenapp/stateplugins/hsapplibrarystateplugin/src/hscollectionstate.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 collection state.
       
    15  *
       
    16  */
       
    17 
       
    18 #include <qstatemachine.h>
       
    19 #include <hbview.h>
       
    20 #include <hbaction.h>
       
    21 #include <hbabstractviewitem.h>
       
    22 #include <hsmenueventfactory.h>
       
    23 #include <hbmenu.h>
       
    24 #include <hbinstance.h>
       
    25 #include <hbsearchpanel.h>
       
    26 
       
    27 #include "hsmenuevent.h"
       
    28 #include "hsmenuservice.h"
       
    29 #include "hsmenuitemmodel.h"
       
    30 #include "hsmenuview.h"
       
    31 #include "cadefs.h"
       
    32 #include "caentry.h"
       
    33 #include "hscollectionstate.h"
       
    34 #include "hsaddappstocollectionstate.h"
       
    35 #include "hsapp_defs.h"
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 HsCollectionState::HsCollectionState(HsMenuView &menuView, QState *parent) :
       
    41     QState(parent),
       
    42     mSortAttribute(LatestOnTopHsSortAttribute),
       
    43     mCollectionsSortAttribute(CustomHsSortAttribute),
       
    44     mCollectionId(-1), mMenuView(menuView),
       
    45     mCollectionModel(0), mOptions(0)
       
    46 {
       
    47     construct();
       
    48 }
       
    49 
       
    50 // ---------------------------------------------------------------------------
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 void HsCollectionState::construct()
       
    54 {
       
    55     HSMENUTEST_FUNC_ENTRY("HsCollectionState::construct");
       
    56     setObjectName(this->parent()->objectName() + "/collectionstate");
       
    57 
       
    58     //new action for backstepping
       
    59     mSecondarySoftkeyAction = new HbAction(Hb::BackAction, this);
       
    60     connect(this, SIGNAL(entered()),SLOT(stateEntered()));
       
    61     connect(this, SIGNAL(exited()),SLOT(stateExited()));
       
    62     HSMENUTEST_FUNC_EXIT("HsCollectionState::construct");
       
    63 }
       
    64 
       
    65 // ---------------------------------------------------------------------------
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 HsCollectionState::~HsCollectionState()
       
    69 {
       
    70     delete mCollectionModel;
       
    71 }
       
    72 
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // ---------------------------------------------------------------------------
       
    76 //
       
    77 void HsCollectionState::onEntry(QEvent *event)
       
    78 {
       
    79     qDebug("HsCollectionState::onEntry()");
       
    80     HSMENUTEST_FUNC_ENTRY("HsCollectionState::onEntry");
       
    81     QState::onEntry(event);
       
    82 
       
    83     if (event->type() == HsMenuEvent::eventType()) {
       
    84         HsMenuEvent *menuEvent = static_cast<HsMenuEvent *>(event);
       
    85         QVariantMap data = menuEvent->data();
       
    86         mCollectionId = data.value(itemIdKey()).toInt();
       
    87         mCollectionType = data.value(entryTypeNameKey()).toString();
       
    88     }
       
    89 
       
    90     HSMENUTEST_FUNC_EXIT("HsCollectionState::onEntry");
       
    91 }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 void HsCollectionState::stateEntered()
       
    97 {
       
    98     HSMENUTEST_FUNC_ENTRY("HsCollectionState::stateEntered");
       
    99     if (!mCollectionModel) {
       
   100         mCollectionModel = HsMenuService::getCollectionModel(mCollectionId,
       
   101                            mSortAttribute, mCollectionType);
       
   102     } else {
       
   103         if (mCollectionType == collectionDownloadedTypeName()) {
       
   104             mCollectionModel->setFlagsOn(RemovableEntryFlag | VisibleEntryFlag);
       
   105         } else {
       
   106             mCollectionModel->setFlagsOn(VisibleEntryFlag);
       
   107         }
       
   108 
       
   109         mCollectionModel->setParentId(mCollectionId);
       
   110         mCollectionModel->setSort(
       
   111             LatestOnTopHsSortAttribute);
       
   112     }
       
   113 
       
   114     mMenuView.setModel(mCollectionModel);
       
   115     mMenuView.scrollTo(mCollectionModel->index(0),
       
   116                        HbAbstractItemView::PositionAtTop);
       
   117 
       
   118     mMenuView.setLabelVisible(true);
       
   119     mMenuView.setLabel(
       
   120         mCollectionModel->root().data(Qt::DisplayRole).toString());
       
   121 
       
   122 
       
   123     if (!mOptions) {
       
   124         mOptions = new HbMenu();
       
   125 
       
   126         EntryFlags flags =
       
   127             mCollectionModel->root().data(CaItemModel::FlagsRole).value<
       
   128             EntryFlags> ();
       
   129 
       
   130         if (flags & RemovableEntryFlag) {
       
   131             mOptions->addAction(hbTrId("txt_applib_opt_add_content"), this,
       
   132                                 SLOT(addAppsAction()));
       
   133         }
       
   134 
       
   135         mOptions->addAction(hbTrId("txt_applib_opt_add_to_home_screen"),
       
   136                             this, SLOT(addCollectionShortcutToHomeScreenAction()));
       
   137 
       
   138         if (flags & RemovableEntryFlag) {
       
   139             mOptions->addAction(
       
   140                 hbTrId("txt_common_opt_rename_item"),
       
   141                 this,
       
   142                 SLOT(renameAction()));
       
   143             mOptions->addAction(
       
   144                 hbTrId("txt_common_opt_delete"),
       
   145                 this,
       
   146                 SLOT(deleteAction()));
       
   147         }
       
   148         if (mCollectionType == collectionDownloadedTypeName()) {
       
   149             HbMenu *sortMenu = mOptions->addMenu(
       
   150                                    hbTrId("txt_applib_opt_sort_by"));
       
   151             //Grouped options are exclusive by default.
       
   152             QActionGroup *sortGroup = new QActionGroup(mOptions);
       
   153             sortGroup->addAction(
       
   154                 sortMenu->addAction(
       
   155                     hbTrId("txt_applib_opt_sort_by_sub_latest_on_top"),
       
   156                     this,
       
   157                     SLOT(latestOnTopMenuAction())));
       
   158             sortGroup->addAction(
       
   159                 sortMenu->addAction(
       
   160                     hbTrId("txt_applib_opt_sort_by_sub_oldest_on_top"),
       
   161                     this,
       
   162                     SLOT(oldestOnTopMenuAction())));
       
   163             foreach(QAction *action, sortMenu->actions()) {
       
   164                 action->setCheckable(true);
       
   165             }
       
   166             static const int defaultSortingPosition = 0;
       
   167             sortGroup->actions().at(defaultSortingPosition)->setChecked(true);
       
   168         }
       
   169 
       
   170         mOptions->setParent(this);
       
   171         mMenuView.view()->setMenu(mOptions);
       
   172     }
       
   173 
       
   174     HbMainWindow *hbMainWindow =
       
   175         HbInstance::instance()->allMainWindows().value(0);
       
   176     // add BackStepping action
       
   177 
       
   178     hbMainWindow->addSoftKeyAction(Hb::SecondarySoftKey,
       
   179                                    mSecondarySoftkeyAction);
       
   180     makeConnect();
       
   181     HSMENUTEST_FUNC_EXIT("HsCollectionState::stateEntered");
       
   182 }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 void HsCollectionState::stateExited()
       
   188 {
       
   189     HSMENUTEST_FUNC_ENTRY("HsCollectionState::stateExited");
       
   190     makeDisconnect();
       
   191 
       
   192     mOptions = NULL;
       
   193 
       
   194     mMenuView.view()->setMenu(NULL);
       
   195     mMenuView.setLabelVisible(false);
       
   196 
       
   197     HbMainWindow *hbW = HbInstance::instance()->allMainWindows().value(0);
       
   198     if (hbW) {
       
   199         //remove BackStepping action
       
   200         hbW->removeSoftKeyAction(Hb::SecondarySoftKey,
       
   201                                  mSecondarySoftkeyAction);
       
   202     }
       
   203     HSMENUTEST_FUNC_EXIT("HsCollectionState::stateExited");
       
   204     qDebug("CollectionState::stateExited()");
       
   205 }
       
   206 
       
   207 // ---------------------------------------------------------------------------
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 void HsCollectionState::makeConnect()
       
   211 {
       
   212     connect(mSecondarySoftkeyAction, SIGNAL(triggered()),
       
   213             SLOT(backSteppingAction()));
       
   214     connect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   215             SLOT(listItemActivated(QModelIndex)));
       
   216     connect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   217             SLOT(listItemLongPressed(HbAbstractViewItem *, QPointF)));
       
   218     connect(mCollectionModel, SIGNAL(modelReset()),SLOT(updateLabel()));
       
   219 }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // ---------------------------------------------------------------------------
       
   223 //
       
   224 void HsCollectionState::makeDisconnect()
       
   225 {
       
   226     disconnect(mSecondarySoftkeyAction, SIGNAL(triggered()),
       
   227                this, SLOT(backSteppingAction()));
       
   228 
       
   229     disconnect(mCollectionModel, SIGNAL(modelReset()),
       
   230                this, SLOT(updateLabel()));
       
   231 
       
   232     disconnect(&mMenuView, SIGNAL(activated(QModelIndex)),
       
   233                this, SLOT(listItemActivated(QModelIndex)));
       
   234 
       
   235     disconnect(&mMenuView, SIGNAL(longPressed(HbAbstractViewItem *, QPointF)),
       
   236                this, SLOT(listItemLongPressed(HbAbstractViewItem *, QPointF)));
       
   237 }
       
   238 
       
   239 // ---------------------------------------------------------------------------
       
   240 // ---------------------------------------------------------------------------
       
   241 //
       
   242 void HsCollectionState::listItemActivated(const QModelIndex &index)
       
   243 {
       
   244     HSMENUTEST_FUNC_ENTRY("HsCollectionState::listItemActivated");
       
   245 
       
   246     const CaEntry *entry = mCollectionModel->entry(index);
       
   247 
       
   248     if (entry->entryTypeName() == widgetTypeName()) {
       
   249         machine()->postEvent(
       
   250             HsMenuEventFactory::createPreviewHSWidgetEvent(
       
   251                 entry->id(), entry->entryTypeName(), entry->attribute(
       
   252                     widgetUriAttributeName()), entry->attribute(
       
   253                     widgetLibraryAttributeName())));
       
   254 
       
   255         const int itemId = index.data(CaItemModel::IdRole).toInt();
       
   256         HsMenuService::touch(itemId);
       
   257     } else {
       
   258         QVariant data = mCollectionModel->data(index, CaItemModel::IdRole);
       
   259         HsMenuService::executeAction(data.toInt());
       
   260     }
       
   261 
       
   262     mMenuView.setSearchPanelVisible(false);
       
   263     HSMENUTEST_FUNC_EXIT("HsCollectionState::listItemActivated");
       
   264 }
       
   265 
       
   266 /*!
       
   267  Handles long-item-pressed event in all apps view by showing context menu
       
   268  \param item the event pertains to
       
   269  \param position at which context menu is shown
       
   270  \retval void
       
   271  */
       
   272 void HsCollectionState::listItemLongPressed(HbAbstractViewItem *item,
       
   273         const QPointF &coords)
       
   274 {
       
   275     Q_UNUSED(item);
       
   276     HSMENUTEST_FUNC_ENTRY("HsCollectionState::listItemLongPressed");
       
   277     mMenuView.setSearchPanelVisible(false);
       
   278     int itemId = item->modelIndex().data(CaItemModel::IdRole).toInt();
       
   279     // create context menu
       
   280     HbMenu *menu = new HbMenu();
       
   281 
       
   282     HbAction *addShortcutAction = menu->addAction(hbTrId(
       
   283                                       "txt_applib_menu_add_to_home_screen"));
       
   284     HbAction *addToCollection = menu->addAction(hbTrId(
       
   285                                     "txt_applib_menu_add_to_collection"));
       
   286     HbAction *removeAction(NULL);
       
   287     HbAction *uninstallAction(NULL);
       
   288     // we do not add remove option in collection download menu
       
   289     // check conditions and hide irrelevant menu items
       
   290     if (mCollectionType != collectionDownloadedTypeName()) {
       
   291         removeAction = menu->addAction(
       
   292                            hbTrId("txt_applib_menu_remove_from_collection"));
       
   293     }
       
   294 
       
   295     EntryFlags flags = item->modelIndex().data(CaItemModel::FlagsRole).value<
       
   296                        EntryFlags> ();
       
   297 
       
   298     if ((flags & RemovableEntryFlag)) {
       
   299         uninstallAction = menu->addAction(hbTrId("txt_common_menu_delete"));
       
   300     }
       
   301 
       
   302     // choose proper action
       
   303     if (HbAction *selectedAction = menu->exec(coords)) {
       
   304         if (selectedAction == addShortcutAction) {
       
   305             addElementToHomeScreen(item->modelIndex());
       
   306         } else if (selectedAction == addToCollection) {
       
   307             machine()->postEvent(
       
   308                 HsMenuEventFactory::createAddAppsFromCallectionViewEvent(
       
   309                     mCollectionId, itemId, mCollectionsSortAttribute));
       
   310         } else if (selectedAction == uninstallAction) {
       
   311             HsMenuService::executeAction(itemId, removeActionIdentifier());
       
   312         } else if (selectedAction == removeAction) {
       
   313             machine()->postEvent(
       
   314                 HsMenuEventFactory::createRemoveAppFromCollectionEvent(
       
   315                     itemId, mCollectionId));
       
   316         }
       
   317     }
       
   318     delete menu;
       
   319     HSMENUTEST_FUNC_EXIT("HsCollectionState::listItemLongPressed");
       
   320 }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 void HsCollectionState::addAppsAction()
       
   326 {
       
   327     // Add applications
       
   328     machine()->postEvent(
       
   329         HsMenuEventFactory::createAddAppsFromCallectionViewEvent(
       
   330             mCollectionId));
       
   331 }
       
   332 
       
   333 // ---------------------------------------------------------------------------
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 void HsCollectionState::addCollectionShortcutToHomeScreenAction()
       
   337 {
       
   338     machine()->postEvent(HsMenuEventFactory::createAddToHomeScreenEvent(
       
   339                              mCollectionId));
       
   340 }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // ---------------------------------------------------------------------------
       
   344 //
       
   345 void HsCollectionState::renameAction()
       
   346 {
       
   347     machine()->postEvent(HsMenuEventFactory::createRenameCollectionEvent(
       
   348                              mCollectionId));
       
   349 }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // ---------------------------------------------------------------------------
       
   353 //
       
   354 void HsCollectionState::deleteAction()
       
   355 {
       
   356     machine()->postEvent(HsMenuEventFactory::createDeleteCollectionEvent(
       
   357                              mCollectionId));
       
   358 }
       
   359 
       
   360 // ---------------------------------------------------------------------------
       
   361 // ---------------------------------------------------------------------------
       
   362 //
       
   363 void HsCollectionState::backSteppingAction()
       
   364 {
       
   365     machine()->postEvent(HsMenuEventFactory::createOpenAppLibraryEvent());
       
   366 }
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // ---------------------------------------------------------------------------
       
   370 //
       
   371 void HsCollectionState::updateLabel()
       
   372 {
       
   373     HSMENUTEST_FUNC_ENTRY("HsCollectionState::updateLabel");
       
   374     if (mCollectionModel) {
       
   375         mMenuView.setLabel(
       
   376             mCollectionModel->root().data(Qt::DisplayRole).toString());
       
   377     }
       
   378     HSMENUTEST_FUNC_EXIT("HsCollectionState::updateLabel");
       
   379 }
       
   380 
       
   381 /*!
       
   382   Triggers event so that a state adding to Home Screen is reached
       
   383   \param index of an item to be added to homescreen
       
   384   \retval void
       
   385  */
       
   386 void HsCollectionState::addElementToHomeScreen(const QModelIndex &index)
       
   387 {
       
   388     const CaEntry *entry = mCollectionModel->entry(index);
       
   389 
       
   390     machine()->
       
   391     postEvent(
       
   392         HsMenuEventFactory::createAddToHomeScreenEvent(
       
   393             entry->id(),
       
   394             entry->entryTypeName(),
       
   395             entry->attribute(widgetUriAttributeName()),
       
   396             entry->attribute(widgetLibraryAttributeName())));
       
   397 }
       
   398 
       
   399 /*!
       
   400   A Slot called when an action for sorting (latest
       
   401   on top) is invoked for Downloaded collection.
       
   402  */
       
   403 void HsCollectionState::latestOnTopMenuAction()
       
   404 {
       
   405     mSortAttribute = LatestOnTopHsSortAttribute;
       
   406     mCollectionModel->setSort(mSortAttribute);
       
   407     emit sortOrderChanged(mSortAttribute);
       
   408 }
       
   409 
       
   410 /*!
       
   411   A Slot called when an action for sorting (oldest
       
   412   on top) is invoked for Downloaded collection.
       
   413  */
       
   414 void HsCollectionState::oldestOnTopMenuAction()
       
   415 {
       
   416     mSortAttribute = OldestOnTopHsSortAttribute;
       
   417     mCollectionModel->setSort(mSortAttribute);
       
   418     emit sortOrderChanged(mSortAttribute);
       
   419 }
       
   420 
       
   421 /*!
       
   422   Slot for setting all collections sort order
       
   423   \param sortOrder all collections sort order
       
   424  */
       
   425 void HsCollectionState::collectionsSortOrder(
       
   426     HsSortAttribute sortOrder)
       
   427 {
       
   428     mCollectionsSortAttribute = sortOrder;
       
   429 }
       
   430 
       
   431 /*!
       
   432   \fn void HsCollectionState::sortOrderChanged(SHsSortAttribute sortOrder);
       
   433 
       
   434   This signal is emitted when sort order is changed to \a sortOrder.
       
   435  */