homescreenapp/serviceproviders/hsmenuserviceprovider/src/hsmenuservice.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: hsmenuservice.cpp
       
    15  *
       
    16  */
       
    17 
       
    18 #include <QDebug>
       
    19 #include <QStandardItem>
       
    20 
       
    21 #include "hsapp_defs.h"
       
    22 #include "hsmenuservice.h"
       
    23 #include "hsmenuserviceutils.h"
       
    24 #include "caquery.h"
       
    25 #include "caitemmodel.h"
       
    26 #include "hsmenuitemmodel.h"
       
    27 #include "hsmenucollectionsitemmodel.h"
       
    28 
       
    29 // ======== MEMBER FUNCTIONS ========
       
    30 
       
    31 // Initialization of a static member variable.
       
    32 int HsMenuService::mAllCollectionsId = 0;
       
    33 
       
    34 /*!
       
    35  Returns all applications model
       
    36  \param sortAttribute ::  SortAttribute
       
    37  \retval HsMenuItemModel: AllApplicationsModel
       
    38  */
       
    39 HsMenuItemModel *HsMenuService::getAllApplicationsModel(
       
    40     HsSortAttribute sortAttribute)
       
    41 {
       
    42     qDebug() << "HsMenuService::getAllApplicationsModel sortAttribute:"
       
    43              << sortAttribute;
       
    44     HSMENUTEST_FUNC_ENTRY("HsMenuService::getAllApplicationsModel");
       
    45     CaQuery query;
       
    46     query.setEntryRoles(ItemEntryRole);
       
    47     query.addEntryTypeName(applicationTypeName());
       
    48     query.addEntryTypeName(urlTypeName());
       
    49     query.addEntryTypeName(widgetTypeName());
       
    50     query.setFlagsOn(VisibleEntryFlag);
       
    51     query.setFlagsOff(MissingEntryFlag);
       
    52     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
    53                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
    54     HsMenuItemModel *model = new HsMenuItemModel(query);
       
    55     HSMENUTEST_FUNC_EXIT("HsMenuService::getAllApplicationsModel");
       
    56     return model;
       
    57 }
       
    58 
       
    59 /*!
       
    60  Returns all collections model
       
    61  \param sortAttribute ::  SortAttribute
       
    62  \param details : switch to return details or not
       
    63  \retval HsMenuItemModel: all collections model
       
    64  */
       
    65 HsMenuItemModel *HsMenuService::getAllCollectionsModel(
       
    66     HsSortAttribute sortAttribute)
       
    67 {
       
    68     qDebug() << "HsMenuService::getAllCollectionsModel" << "sortAttribute:"
       
    69              << sortAttribute;
       
    70     HSMENUTEST_FUNC_ENTRY("HsMenuService::getAllCollectionsModel");
       
    71     CaQuery query;
       
    72     query.setParentId(allCollectionsId());
       
    73     query.setFlagsOn(VisibleEntryFlag);
       
    74     query.setFlagsOff(MissingEntryFlag);
       
    75     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
    76                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
    77     HsMenuItemModel *model = new HsMenuCollectionsItemModel(query);
       
    78     HSMENUTEST_FUNC_EXIT("HsMenuService::getAllCollectionsModel");
       
    79     return model;
       
    80 }
       
    81 
       
    82 /*!
       
    83  Returns collection model
       
    84  \param collectionId ::  id of this collection
       
    85  \param sortAttribute ::  SortAttribute
       
    86  \retval HsMenuItemModel: collection model
       
    87  */
       
    88 HsMenuItemModel *HsMenuService::getCollectionModel(int collectionId,
       
    89         HsSortAttribute sortAttribute, const QString &collectionType)
       
    90 {
       
    91     qDebug() << "HsMenuService::getCollectionModel sortAttribute:"
       
    92              << sortAttribute;
       
    93     HSMENUTEST_FUNC_ENTRY("HsMenuService::getCollectionModel");
       
    94     CaQuery query;
       
    95     query.setFlagsOff(MissingEntryFlag);
       
    96     if (collectionType == collectionDownloadedTypeName()) {
       
    97         query.setFlagsOn(RemovableEntryFlag | VisibleEntryFlag);
       
    98     } else {
       
    99         query.setFlagsOn(VisibleEntryFlag);
       
   100     }
       
   101     query.setParentId(collectionId);
       
   102     query.setSort(HsMenuServiceUtils::sortBy(sortAttribute),
       
   103                   HsMenuServiceUtils::sortOrder(sortAttribute));
       
   104     HsMenuItemModel *model = new HsMenuItemModel(query);
       
   105     HSMENUTEST_FUNC_EXIT("HsMenuService::getCollectionModel");
       
   106     return model;
       
   107 }
       
   108 
       
   109 /*!
       
   110  Returns all collections names
       
   111  \retval CaItemModel: list with collections names
       
   112  */
       
   113 QStringList HsMenuService::getCollectionNames()
       
   114 {
       
   115     qDebug() << "HsMenuService::getCollectionNames";
       
   116     HSMENUTEST_FUNC_ENTRY("HsMenuService::getCollectionNames");
       
   117     CaQuery query;
       
   118     query.setParentId(allCollectionsId());
       
   119     query.setFlagsOn(VisibleEntryFlag);
       
   120     query.setFlagsOff(MissingEntryFlag);
       
   121     QList<CaEntry *> collections = CaService::instance()->getEntries(query);
       
   122     QStringList resultList;
       
   123     while (!collections.isEmpty()) {
       
   124         CaEntry *entry = collections.takeFirst();
       
   125         resultList << entry->text();
       
   126         delete entry;
       
   127     }
       
   128 
       
   129     qDebug() << "HsMenuService::getCollectionNames resultList:"
       
   130              << resultList;
       
   131     HSMENUTEST_FUNC_EXIT("HsMenuService::getCollectionNames");
       
   132     return resultList;
       
   133 }
       
   134 
       
   135 /*!
       
   136  Returns name of an entry
       
   137  \param id of this entry
       
   138  \retval QString with name of the entry
       
   139  */
       
   140 QString HsMenuService::getName(int entryId)
       
   141 {
       
   142     qDebug() << "HsMenuService::getName entryId:" << entryId;
       
   143     HSMENUTEST_FUNC_ENTRY("HsMenuService::getName");
       
   144     CaEntry *entry = CaService::instance()->getEntry(entryId);
       
   145 
       
   146     QString name;
       
   147     if (entry) {
       
   148         name = entry->text();
       
   149         delete entry;
       
   150     }
       
   151     qDebug() << "HsMenuService::getName name: " << name;
       
   152     HSMENUTEST_FUNC_EXIT("HsMenuService::getName");
       
   153     return name;
       
   154 }
       
   155 
       
   156 /*!
       
   157  Executes action on an item
       
   158  \param entryId of this item
       
   159  \param actionName string with action name
       
   160  \retval boolean error code
       
   161  */
       
   162 bool HsMenuService::executeAction(int entryId, const QString &actionName)
       
   163 {
       
   164     qDebug() << "HsMenuService::executeAction entryId:" << entryId
       
   165              << "actionName:" << actionName;
       
   166 
       
   167     return CaService::instance()->executeCommand(entryId, actionName);
       
   168 }
       
   169 
       
   170 /*!
       
   171  Adds new collection
       
   172  \param name of the collection
       
   173  \retval entryId of new collection
       
   174  */
       
   175 int HsMenuService::createCollection(const QString &name)
       
   176 {
       
   177     qDebug() << "HsMenuService::addCollection name: " << name;
       
   178     HSMENUTEST_FUNC_ENTRY("HsMenuService::createCollection");
       
   179     int entryId = 0;
       
   180     CaEntry collection(GroupEntryRole);
       
   181     collection.setEntryTypeName(collectionTypeName());
       
   182     collection.setText(name);
       
   183     CaIconDescription iconDescription;
       
   184     iconDescription.setFilename(defaultCollectionIconId());
       
   185     collection.setIconDescription(iconDescription);
       
   186     CaEntry *entry = CaService::instance()->createEntry(collection);
       
   187     if (entry) {
       
   188         qDebug() << "HsMenuService::addCollection entry" << entry;
       
   189         entryId = entry->id();
       
   190         delete entry;
       
   191         CaService::instance()->appendEntryToGroup(allCollectionsId(),
       
   192                 entryId);
       
   193     }
       
   194     HSMENUTEST_FUNC_EXIT("HsMenuService::createCollection");
       
   195     return entryId;
       
   196 }
       
   197 
       
   198 /*!
       
   199  Renames a collection
       
   200  \param id of this collection
       
   201  \param new name for collection
       
   202  \retval boolean error code
       
   203  */
       
   204 bool HsMenuService::renameCollection(int collectionId,
       
   205                                      const QString &newCollectionName)
       
   206 {
       
   207     qDebug() << "HsMenuService::renameCollection collectionId:"
       
   208              << collectionId << "newCollectionName" << newCollectionName;
       
   209     HSMENUTEST_FUNC_ENTRY("HsMenuService::renameCollection");
       
   210     bool result(false);
       
   211     CaEntry *collection = CaService::instance()->getEntry(collectionId);
       
   212     if (collection) {
       
   213         qDebug() << "HsMenuService::renameCollection collection"
       
   214                  << collection;
       
   215 
       
   216         collection->setText(newCollectionName);
       
   217         result = CaService::instance()->updateEntry(*collection);
       
   218         delete collection;
       
   219     }
       
   220     HSMENUTEST_FUNC_EXIT("HsMenuService::renameCollection");
       
   221     return result;
       
   222 }
       
   223 
       
   224 /*!
       
   225  Removes a collection
       
   226  \param id of this collection
       
   227  \retval boolean error code
       
   228  */
       
   229 bool HsMenuService::removeCollection(int collectionId)
       
   230 {
       
   231     qDebug() << "HsMenuService::removeCollection collectionId:"
       
   232              << collectionId;
       
   233 
       
   234     return CaService::instance()->removeEntry(collectionId);
       
   235 }
       
   236 
       
   237 /*!
       
   238  Adds applications to collection
       
   239  \param list with applications entries ids
       
   240  \param collection id
       
   241  \retval boolean error code
       
   242  */
       
   243 bool HsMenuService::addApplicationsToCollection(
       
   244     const QList<int> &applicationList, int collectionId)
       
   245 {
       
   246     qDebug() << "HsMenuService::addApplicationsToCollection"
       
   247              << "applicationList ids:" << applicationList << "collectionId:"
       
   248              << collectionId;
       
   249 
       
   250     return CaService::instance()->appendEntriesToGroup(collectionId,
       
   251             applicationList);
       
   252 }
       
   253 
       
   254 /*!
       
   255  Removes application from collection
       
   256  \param application id
       
   257  \param collection id
       
   258  \retval boolean error code
       
   259  */
       
   260 bool HsMenuService::removeApplicationFromCollection(int applicationId,
       
   261         int collectionId)
       
   262 {
       
   263     qDebug() << "HsMenuService::removeApplicationFromCollection"
       
   264              << "applicationId:" << applicationId << "collectionId:"
       
   265              << collectionId;
       
   266 
       
   267     return CaService::instance()->removeEntryFromGroup(collectionId,
       
   268             applicationId);
       
   269 }
       
   270 
       
   271 /*!
       
   272  Organizes collection's entries.
       
   273  \param groupId Group id.
       
   274  \param entryIdList Group's entries' ids list.
       
   275  \retval Return true if organize collection is done correctly,
       
   276   otherwise return false.
       
   277  */
       
   278 bool HsMenuService::organizeCollection(int groupId, QList<int> &entryIdList)
       
   279 {
       
   280     qDebug() << "HsMenuService::organizeCollection"
       
   281              << "groupId:" << groupId
       
   282              << "collectionIds:" << entryIdList;
       
   283     return CaService::instance()->customSort(groupId, entryIdList);
       
   284 }
       
   285 
       
   286 /*!
       
   287  Retrives the all collections entry id
       
   288  \retval all collections id
       
   289  */
       
   290 int HsMenuService::allCollectionsId()
       
   291 {
       
   292     HSMENUTEST_FUNC_ENTRY("HsMenuService::allCollectionsId");
       
   293     if (mAllCollectionsId <= 0) {
       
   294         CaQuery collectionsQuery;
       
   295         collectionsQuery.setEntryRoles(GroupEntryRole);
       
   296         //sorting is set to (default, ascending) to assure that
       
   297         //proper entry is fetched, somebody can add item with
       
   298         //"menucollections" typename to the storage, but even if he or she
       
   299         //do this we fetch entry that we wanted
       
   300         collectionsQuery.setSort(DefaultSortAttribute, Qt::AscendingOrder);
       
   301         collectionsQuery.addEntryTypeName(menuCollectionsTypeName());
       
   302         QList<int> ids = CaService::instance()->getEntryIds(
       
   303                              collectionsQuery);
       
   304         Q_ASSERT(ids.count() > 0);
       
   305         mAllCollectionsId = ids.at(0);
       
   306         qDebug() << "HsMenuService::HsMenuService mAllCollectionsId"
       
   307                  << mAllCollectionsId;
       
   308     }
       
   309     HSMENUTEST_FUNC_EXIT("HsMenuService::allCollectionsId");
       
   310     return mAllCollectionsId;
       
   311 }
       
   312 
       
   313 /*!
       
   314  Touch action on an entry.
       
   315  \param entryId of this entry.
       
   316  \retval boolean error code.
       
   317  */
       
   318 bool HsMenuService::touch(int entryId)
       
   319 {
       
   320     CaEntry *entry = CaService::instance()->getEntry(entryId);
       
   321     return CaService::instance()->touch(* entry);
       
   322 }