homescreenapp/serviceproviders/hsmenuserviceprovider/src/hsmenuservice.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: hsmenuservice.cpp
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QStandardItem>
       
    20 #include <qvaluespacepublisher.h>
       
    21 #include <HbDeviceDialog>
       
    22 
       
    23 #include "hsapp_defs.h"
       
    24 #include "hsmenuservice.h"
       
    25 #include "hsmenuserviceutils.h"
       
    26 #include "caquery.h"
       
    27 #include "caitemmodel.h"
       
    28 #include "hsmenuitemmodel.h"
       
    29 #include "hsmenucollectionsitemmodel.h"
       
    30 
       
    31 QTM_USE_NAMESPACE
       
    32 
       
    33 // ======== MEMBER FUNCTIONS ========
       
    34 
       
    35 // Initialization of a static member variable.
       
    36 int HsMenuService::mAllCollectionsId = 0;
       
    37 const char COLLECTION_TITLE_NAME[] = "title_name";
       
    38 const char COLLECTION_SHORT_NAME[] = "short_name";
       
    39 /*!
       
    40  Returns all applications model
       
    41  \param sortAttribute ::  SortAttribute
       
    42  \retval HsMenuItemModel: AllApplicationsModel
       
    43  */
       
    44 HsMenuItemModel *HsMenuService::getAllApplicationsModel(
       
    45     Hs::HsSortAttribute sortAttribute)
       
    46 {
       
    47     qDebug() << "HsMenuService::getAllApplicationsModel sortAttribute:"
       
    48              << sortAttribute;
       
    49     HSMENUTEST_FUNC_ENTRY("HsMenuService::getAllApplicationsModel");
       
    50     CaQuery query;
       
    51     query.setEntryRoles(ItemEntryRole);
       
    52     query.addEntryTypeName(Hs::applicationTypeName);
       
    53     query.addEntryTypeName(Hs::urlTypeName);
       
    54     query.addEntryTypeName(Hs::widgetTypeName);
       
    55     query.addEntryTypeName(Hs::templatedApplicationTypeName);
       
    56     query.setFlagsOn(VisibleEntryFlag);
       
    57     query.setFlagsOff(MissingEntryFlag);
       
    58     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
    59                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
    60     HsMenuItemModel *model = new HsMenuItemModel(query);
       
    61     HSMENUTEST_FUNC_EXIT("HsMenuService::getAllApplicationsModel");
       
    62     return model;
       
    63 }
       
    64 
       
    65 /*!
       
    66  Returns all collections model
       
    67  \retval HsMenuItemModel: all collections model
       
    68  */
       
    69 HsMenuItemModel *HsMenuService::getAllCollectionsModel()
       
    70 {
       
    71     qDebug() << "HsMenuService::getAllCollectionsModel";
       
    72     HSMENUTEST_FUNC_ENTRY("HsMenuService::getAllCollectionsModel");
       
    73     CaQuery query;
       
    74     query.setParentId(allCollectionsId());
       
    75     query.setFlagsOn(VisibleEntryFlag);
       
    76     query.setFlagsOff(MissingEntryFlag);
       
    77     HsMenuItemModel *model = new HsMenuCollectionsItemModel(query);
       
    78     HSMENUTEST_FUNC_EXIT("HsMenuService::getAllCollectionsModel");
       
    79     return model;
       
    80 }
       
    81 
       
    82 /*!
       
    83  Returns native and java applications.
       
    84  \param sortAttribute :: SortAttribute
       
    85  \retval HsMenuItemModel: installed model
       
    86  */
       
    87 HsMenuItemModel *HsMenuService::getInstalledModel(
       
    88     Hs::HsSortAttribute sortAttribute)
       
    89 {
       
    90     //TODO get proper items
       
    91     qDebug() << "HsMenuService::getInstalledModel" << "sortAttribute:"
       
    92              << sortAttribute;
       
    93     HSMENUTEST_FUNC_ENTRY("HsMenuService::getInstalledModel");
       
    94     CaQuery query;
       
    95     query.addEntryTypeName(Hs::packageTypeName);
       
    96     query.setFlagsOn(VisibleEntryFlag | RemovableEntryFlag);
       
    97     query.setFlagsOff(MissingEntryFlag);
       
    98     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
    99                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
   100     HsMenuItemModel *model = new HsMenuItemModel(query);
       
   101     HSMENUTEST_FUNC_EXIT("HsMenuService::getInstalledModel");
       
   102     return model;
       
   103 }
       
   104 
       
   105 /*!
       
   106  Returns collection model
       
   107  \param collectionId ::  id of this collection
       
   108  \param sortAttribute ::  SortAttribute
       
   109  \retval HsMenuItemModel: collection model
       
   110  */
       
   111 HsMenuItemModel *HsMenuService::getCollectionModel(int collectionId,
       
   112         Hs::HsSortAttribute sortAttribute, const QString &collectionType)
       
   113 {
       
   114     qDebug() << "HsMenuService::getCollectionModel sortAttribute:"
       
   115              << sortAttribute;
       
   116     HSMENUTEST_FUNC_ENTRY("HsMenuService::getCollectionModel");
       
   117     CaQuery query;
       
   118     query.setFlagsOff(MissingEntryFlag);
       
   119     if (collectionType == Hs::collectionDownloadedTypeName) {
       
   120         query.setFlagsOn(RemovableEntryFlag | VisibleEntryFlag);
       
   121     } else {
       
   122         query.setFlagsOn(VisibleEntryFlag);
       
   123     }
       
   124     if(!collectionId) {
       
   125         collectionId = collectionIdByType(collectionType);
       
   126     }
       
   127     query.setParentId(collectionId);
       
   128     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
   129                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
   130     HsMenuItemModel *model = new HsMenuItemModel(query);
       
   131     HSMENUTEST_FUNC_EXIT("HsMenuService::getCollectionModel");
       
   132     return model;
       
   133 }
       
   134 
       
   135 /*!
       
   136  Returns all collections names
       
   137  \retval CaItemModel: list with collections names
       
   138  */
       
   139 QStringList HsMenuService::getCollectionNames()
       
   140 {
       
   141     qDebug() << "HsMenuService::getCollectionNames";
       
   142     HSMENUTEST_FUNC_ENTRY("HsMenuService::getCollectionNames");
       
   143     CaQuery query;
       
   144     query.setParentId(allCollectionsId());
       
   145     query.setFlagsOn(VisibleEntryFlag);
       
   146     query.setFlagsOff(MissingEntryFlag);
       
   147     QList< QSharedPointer<CaEntry> > collections = CaService::instance()->getEntries(query);
       
   148     QStringList resultList;
       
   149     foreach(QSharedPointer<CaEntry> entry, collections) {
       
   150         resultList << entry->text();
       
   151     }
       
   152 
       
   153     qDebug() << "HsMenuService::getCollectionNames resultList:"
       
   154              << resultList;
       
   155     HSMENUTEST_FUNC_EXIT("HsMenuService::getCollectionNames");
       
   156     return resultList;
       
   157 }
       
   158 
       
   159 /*!
       
   160  Returns name of an entry
       
   161  \param id of this entry
       
   162  \retval QString with name of the entry
       
   163  */
       
   164 QString HsMenuService::getName(int entryId)
       
   165 {
       
   166     qDebug() << "HsMenuService::getName entryId:" << entryId;
       
   167     HSMENUTEST_FUNC_ENTRY("HsMenuService::getName");
       
   168     QSharedPointer<CaEntry> entry = CaService::instance()->getEntry(entryId);
       
   169 
       
   170     QString name;
       
   171     if (!entry.isNull()) {
       
   172         name = entry->text();
       
   173     }
       
   174     qDebug() << "HsMenuService::getName name: " << name;
       
   175     HSMENUTEST_FUNC_EXIT("HsMenuService::getName");
       
   176     return name;
       
   177 }
       
   178 
       
   179 /*!
       
   180  Executes action on an item
       
   181  \param entryId of this item
       
   182  \param actionName string with action name
       
   183  \retval int error code, 0 if no error
       
   184  */
       
   185 int HsMenuService::executeAction(int entryId, const QString &actionName)
       
   186 {
       
   187     qDebug() << "HsMenuService::executeAction entryId:" << entryId
       
   188              << "actionName:" << actionName;
       
   189     return CaService::instance()->executeCommand(entryId, actionName);
       
   190 }
       
   191 
       
   192 /*!
       
   193  Launch task switcher
       
   194  \retval boolean launching status
       
   195  */
       
   196 bool HsMenuService::launchTaskSwitcher()
       
   197 {
       
   198     qDebug() << "HsMenuService::launchTaskSwitcher";
       
   199     HbDeviceDialog deviceDialog;
       
   200     QVariantMap params;
       
   201     return deviceDialog.show(Hs::tsDeviceDialogUri, params);
       
   202 }
       
   203 
       
   204 /*!
       
   205  Adds new collection
       
   206  \param name of the collection
       
   207  \retval entryId of new collection
       
   208  */
       
   209 int HsMenuService::createCollection(const QString &name)
       
   210 {
       
   211     qDebug() << "HsMenuService::addCollection name: " << name;
       
   212     HSMENUTEST_FUNC_ENTRY("HsMenuService::createCollection");
       
   213     int entryId = 0;
       
   214     CaEntry collection(GroupEntryRole);
       
   215     collection.setEntryTypeName(Hs::collectionTypeName);
       
   216     collection.setText(name);
       
   217     collection.setAttribute(Hs::groupNameAttributeName,name);
       
   218     CaIconDescription iconDescription;
       
   219     iconDescription.setFilename(Hs::defaultCollectionIconId);
       
   220     collection.setIconDescription(iconDescription);
       
   221     QSharedPointer<CaEntry> entry = CaService::instance()->createEntry(collection);
       
   222     if (!entry.isNull()) {
       
   223         qDebug() << "HsMenuService::addCollection entry" << entry;
       
   224         entryId = entry->id();
       
   225         CaService::instance()->appendEntryToGroup(allCollectionsId(),
       
   226                 entryId);
       
   227     }
       
   228     HSMENUTEST_FUNC_EXIT("HsMenuService::createCollection");
       
   229     return entryId;
       
   230 }
       
   231 
       
   232 /*!
       
   233  Renames a collection
       
   234  \param id of this collection
       
   235  \param new name for collection
       
   236  \retval boolean error code
       
   237  */
       
   238 bool HsMenuService::renameCollection(int collectionId,
       
   239                                      const QString &newCollectionName)
       
   240 {
       
   241     qDebug() << "HsMenuService::renameCollection collectionId:"
       
   242              << collectionId << "newCollectionName" << newCollectionName;
       
   243     HSMENUTEST_FUNC_ENTRY("HsMenuService::renameCollection");
       
   244     bool result(false);
       
   245     QSharedPointer<CaEntry> collection = CaService::instance()->getEntry(collectionId);
       
   246     if (collection) {
       
   247         qDebug() << "HsMenuService::renameCollection collection"
       
   248                  << collection;
       
   249 
       
   250         collection->setText(newCollectionName);
       
   251         collection->setAttribute(COLLECTION_TITLE_NAME, newCollectionName);
       
   252         collection->setAttribute(COLLECTION_SHORT_NAME, newCollectionName);
       
   253         result = CaService::instance()->updateEntry(*collection);
       
   254     }
       
   255     HSMENUTEST_FUNC_EXIT("HsMenuService::renameCollection");
       
   256     return result;
       
   257 }
       
   258 
       
   259 /*!
       
   260  Removes a collection
       
   261  \param id of this collection
       
   262  \retval boolean error code
       
   263  */
       
   264 bool HsMenuService::removeCollection(int collectionId)
       
   265 {
       
   266     qDebug() << "HsMenuService::removeCollection collectionId:"
       
   267              << collectionId;
       
   268 
       
   269     return CaService::instance()->removeEntry(collectionId);
       
   270 }
       
   271 
       
   272 /*!
       
   273  Adds applications to collection
       
   274  \param list with applications entries ids
       
   275  \param collection id
       
   276  \retval boolean error code
       
   277  */
       
   278 bool HsMenuService::addApplicationsToCollection(
       
   279     const QList<int> &applicationList, int collectionId)
       
   280 {
       
   281     qDebug() << "HsMenuService::addApplicationsToCollection"
       
   282              << "applicationList ids:" << applicationList << "collectionId:"
       
   283              << collectionId;
       
   284 
       
   285     return CaService::instance()->appendEntriesToGroup(collectionId,
       
   286             applicationList);
       
   287 }
       
   288 
       
   289 /*!
       
   290  Removes application from collection
       
   291  \param application id
       
   292  \param collection id
       
   293  \retval boolean error code
       
   294  */
       
   295 bool HsMenuService::removeApplicationFromCollection(int applicationId,
       
   296         int collectionId)
       
   297 {
       
   298     qDebug() << "HsMenuService::removeApplicationFromCollection"
       
   299              << "applicationId:" << applicationId << "collectionId:"
       
   300              << collectionId;
       
   301 
       
   302     return CaService::instance()->removeEntryFromGroup(collectionId,
       
   303             applicationId);
       
   304 }
       
   305 
       
   306 /*!
       
   307  Organizes collection's entries.
       
   308  \param groupId Group id.
       
   309  \param entryIdList Group's entries' ids list.
       
   310  \retval Return true if organize collection is done correctly,
       
   311   otherwise return false.
       
   312  */
       
   313 bool HsMenuService::organizeCollection(int groupId, QList<int> &entryIdList)
       
   314 {
       
   315     qDebug() << "HsMenuService::organizeCollection"
       
   316              << "groupId:" << groupId
       
   317              << "collectionIds:" << entryIdList;
       
   318     return CaService::instance()->customSort(groupId, entryIdList);
       
   319 }
       
   320 
       
   321 /*!
       
   322  Retrives the all collections entry id
       
   323  \retval all collections id
       
   324  */
       
   325 int HsMenuService::allCollectionsId()
       
   326 {
       
   327     HSMENUTEST_FUNC_ENTRY("HsMenuService::allCollectionsId");
       
   328     if (mAllCollectionsId <= 0) {
       
   329         CaQuery collectionsQuery;
       
   330         collectionsQuery.setEntryRoles(GroupEntryRole);
       
   331         //sorting is set to (default, ascending) to assure that
       
   332         //proper entry is fetched, somebody can add item with
       
   333         //"menucollections" typename to the storage, but even if he or she
       
   334         //do this we fetch entry that we wanted
       
   335         collectionsQuery.setSort(DefaultSortAttribute, Qt::AscendingOrder);
       
   336         collectionsQuery.addEntryTypeName(Hs::menuCollectionsTypeName);
       
   337         QList<int> ids = CaService::instance()->getEntryIds(
       
   338                              collectionsQuery);
       
   339         Q_ASSERT(ids.count() > 0);
       
   340         mAllCollectionsId = ids.at(0);
       
   341         qDebug() << "HsMenuService::HsMenuService mAllCollectionsId"
       
   342                  << mAllCollectionsId;
       
   343     }
       
   344     HSMENUTEST_FUNC_EXIT("HsMenuService::allCollectionsId");
       
   345     return mAllCollectionsId;
       
   346 }
       
   347 
       
   348 /*!
       
   349  Retrives the first found collection entry id
       
   350  \param collectionType collection type.
       
   351  \retval collectionType id
       
   352  */
       
   353 int HsMenuService::collectionIdByType(const QString& collectionType)
       
   354 {
       
   355     HSMENUTEST_FUNC_ENTRY("HsMenuService::collectionsIdByType");
       
   356     int collectionId;
       
   357     CaQuery collectionsQuery;
       
   358     collectionsQuery.setEntryRoles(GroupEntryRole);
       
   359     //sorting is set to (default, ascending) to assure that
       
   360     //proper entry is fetched, somebody can add item with
       
   361     //"menucollections" typename to the storage, but even if he or she
       
   362     //do this we fetch entry that we wanted
       
   363     collectionsQuery.setSort(DefaultSortAttribute, Qt::AscendingOrder);
       
   364     collectionsQuery.addEntryTypeName(collectionType);
       
   365     QList<int> ids = CaService::instance()->getEntryIds(
       
   366                          collectionsQuery);
       
   367     Q_ASSERT(ids.count() > 0);
       
   368     collectionId = ids.at(0);
       
   369     qDebug() << "HsMenuService::HsMenuService collectionsIdByType"
       
   370              << collectionId;
       
   371     HSMENUTEST_FUNC_EXIT("HsMenuService::collectionsIdByType");
       
   372     return collectionId;
       
   373 }
       
   374 
       
   375 /*!
       
   376  Touch action on an entry.
       
   377  \param entryId of this entry.
       
   378  \retval boolean error code.
       
   379  */
       
   380 void HsMenuService::touch(int entryId)
       
   381 {
       
   382     QSharedPointer<CaEntry> entry = CaService::instance()->getEntry(entryId);
       
   383     if (entry->flags() & RemovableEntryFlag &&
       
   384         (entry->flags() & UsedEntryFlag) == 0 &&
       
   385         entry->role() == ItemEntryRole &&
       
   386         entry->entryTypeName() != QString(Hs::packageTypeName)) {
       
   387             CaService::instance()->touch(* entry);
       
   388     }
       
   389 }
       
   390 
       
   391 /*!
       
   392  Touch action on an lists of entries.
       
   393  \param entryIdList of this entry.
       
   394  \retval boolean error code.
       
   395  */
       
   396 void HsMenuService::touch(const QList<int> &entryIdList)
       
   397 {
       
   398     CaQuery query;
       
   399     query.setFlagsOn(RemovableEntryFlag);
       
   400     query.setFlagsOff(UsedEntryFlag);
       
   401     query.setEntryRoles(ItemEntryRole);
       
   402     QList<int> removableUnTouchEntries =
       
   403         CaService::instance()->getEntryIds(query);
       
   404     foreach (int item, entryIdList) {
       
   405         if (removableUnTouchEntries.contains(item)) {
       
   406             touch(item);
       
   407         }
       
   408     }
       
   409 }
       
   410 
       
   411 /*!
       
   412  Launch SoftwareUpdateApplication
       
   413  \retval int launching status, 0 if no errors
       
   414  */
       
   415 int HsMenuService::launchSoftwareUpdate()
       
   416 {
       
   417     qDebug() << "HsMenuService::launchSoftwareUpdate";
       
   418     QScopedPointer<CaEntry> tsEntry(new CaEntry);
       
   419     tsEntry->setEntryTypeName(Hs::applicationTypeName);
       
   420     tsEntry->setAttribute(
       
   421             Hs::applicationUidEntryKey, QString::number(Hs::softwareUpdateApplicationUid));
       
   422     int retval = CaService::instance()->executeCommand(*tsEntry,
       
   423             Hs::openActionIdentifier);
       
   424     return retval;
       
   425 }