tsdevicedialog/tsdevicedialogplugin/src/tsmodel.cpp
changeset 104 9b022b1f357c
child 107 b34d53f6acdf
equal deleted inserted replaced
103:b99b84bcd2d1 104:9b022b1f357c
       
     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: tsmodel.cpp
       
    15 *
       
    16 */
       
    17 #include "tsmodel.h"
       
    18 #include "tsmodelitem.h"
       
    19 #include "tsentrymodelitem.h"
       
    20 #include "tsactivitymodelitem.h"
       
    21 #include "tsdataroles.h"
       
    22 
       
    23 #include <HbIcon>
       
    24 #include <qvariant.h>
       
    25 #include <qlist.h>
       
    26 #include <afstorageglobals.h>
       
    27 
       
    28 #ifdef Q_OS_SYMBIAN
       
    29 #include <XQSettingsManager>
       
    30 #include <apaid.h>
       
    31 
       
    32 const int TSDeviceDialogUid = 0x2002677F;
       
    33 const int ItemsLimit = 0x00000001;
       
    34 #endif
       
    35 
       
    36 const int maxItems(10);
       
    37 /*!
       
    38     \class TsModel
       
    39     \ingroup group_tsdevicedialogplugin
       
    40     \brief Model storing running application and activieties.
       
    41 */
       
    42 
       
    43 /*!
       
    44     Constructor
       
    45     \param query used to create model
       
    46     \param pointer to parent object
       
    47 */
       
    48 TsModel::TsModel(TsTaskMonitor &applicationSrv, QObject &activitySrv, QObject *parent) :
       
    49     QAbstractListModel(parent),
       
    50     mEntries(),
       
    51     mApplicationService(applicationSrv),
       
    52     mActivityService(activitySrv),
       
    53     mSize(240, 240),
       
    54     mMaxItems(maxItems)
       
    55 {
       
    56     
       
    57 #ifdef Q_OS_SYMBIAN
       
    58     XQSettingsManager *crManager = new XQSettingsManager;
       
    59     XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit);
       
    60     QVariant itemsNumberVariant = crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt);
       
    61     if (!itemsNumberVariant.isNull()) {
       
    62         int number = itemsNumberVariant.toInt();
       
    63         if (number > 0) {
       
    64             mMaxItems = number;
       
    65         }
       
    66     }
       
    67     iAppArcSession.Connect();
       
    68 #endif
       
    69 
       
    70     connect(&activitySrv, SIGNAL(dataChanged()), this, SLOT(updateModel()));
       
    71     connect(&applicationSrv, SIGNAL(taskListChanged()), this, SLOT(updateModel()));
       
    72     updateModel();
       
    73 }
       
    74 
       
    75 /*!
       
    76     Destructor
       
    77 */
       
    78 TsModel::~TsModel()
       
    79 {
       
    80 #ifdef Q_OS_SYMBIAN
       
    81     iAppArcSession.Close();
       
    82 #endif
       
    83     qDeleteAll(mEntries);
       
    84 }
       
    85 
       
    86 /*!
       
    87     Returns count of rows in model
       
    88     \retval number of rows
       
    89 */
       
    90 int TsModel::rowCount(
       
    91     const QModelIndex &parent) const
       
    92 {
       
    93     Q_UNUSED(parent);
       
    94     return mEntries.count();
       
    95 }
       
    96 
       
    97 /*!
       
    98     Returns appropiate model's data
       
    99     \param index model index
       
   100     \param role which data role to return
       
   101     \retval models data
       
   102 */
       
   103 QVariant TsModel::data(const QModelIndex &index,
       
   104                        int role) const
       
   105 {
       
   106     return index.isValid() ? entry(index)->data(role) : QVariant();
       
   107 }
       
   108 
       
   109 /*!
       
   110     Returns maximum anount of data allowed for model
       
   111     \retval maximum data count
       
   112 */
       
   113 
       
   114 int TsModel::maxRowCount()const
       
   115 {
       
   116     return mMaxItems;
       
   117 }
       
   118 
       
   119 /*!
       
   120     Activate one of model entries
       
   121 */
       
   122 void TsModel::openApplication(const QModelIndex &index)
       
   123 {
       
   124     if (!index.isValid()) {
       
   125         return;
       
   126     }
       
   127     entry(index)->open();
       
   128 }
       
   129 
       
   130 /*!
       
   131     Close one of moder entries
       
   132 */
       
   133 void TsModel::closeApplication(const QModelIndex &index)
       
   134 {
       
   135     if (!index.isValid() || !entry(index)->data(TsDataRoles::Closable).toBool()) {
       
   136         return;
       
   137     }
       
   138     entry(index)->close();
       
   139 }
       
   140 
       
   141 /*!
       
   142     Updates model with fresh entries
       
   143 */
       
   144 void TsModel::updateModel()
       
   145 {
       
   146     //clear current data
       
   147     qDeleteAll(mEntries);
       
   148     mEntries.clear();
       
   149 
       
   150     beginResetModel();
       
   151     getApplications();
       
   152     getActivities();
       
   153     endResetModel();
       
   154 
       
   155 }
       
   156 
       
   157 /*!
       
   158     Read list of running applications
       
   159 */
       
   160 void TsModel::getApplications()
       
   161 {
       
   162     //get running applications
       
   163     TsModelItem *entry(0);
       
   164     QList< QSharedPointer<TsTask> > tasks(mApplicationService.taskList());
       
   165     foreach (QSharedPointer<TsTask> taskData, tasks) {
       
   166         entry = new TsEntryModelItem(taskData);
       
   167         if (0 != entry) {
       
   168             mEntries.append(entry);
       
   169         }
       
   170     }
       
   171 }
       
   172 
       
   173 /*!
       
   174     Read current activities
       
   175 */
       
   176 void TsModel::getActivities()
       
   177 {
       
   178     //get activities
       
   179     TsModelItem *entry(0);
       
   180     QList<QVariantHash> activities;
       
   181     QMetaObject::invokeMethod(&mActivityService, "activitiesList", Q_RETURN_ARG(QList<QVariantHash>, activities));
       
   182     foreach(QVariantHash activity, activities) {
       
   183         prepareActivityEntry(activity);
       
   184         entry = new TsActivityModelItem(*this, mActivityService, activity);
       
   185         if (entry) {
       
   186             if (maxRowCount() <= mEntries.count()) {
       
   187                 break;
       
   188             }
       
   189             mEntries.append(entry);
       
   190         }
       
   191     }
       
   192 }
       
   193 
       
   194 /*!
       
   195     Modify activity entry replacing application id with name
       
   196 */
       
   197 void TsModel::prepareActivityEntry(QVariantHash &activity)
       
   198 {
       
   199     if (!activity.contains(TsActivityModelItem::applicationKeyword())) {
       
   200         activity.insert(TsActivityModelItem::applicationKeyword(),
       
   201                     activity.contains(ActivityApplicationKeyword) ?
       
   202                     getApplicationName(activity[ActivityApplicationKeyword].toInt()) :
       
   203                     QString::null);
       
   204     }
       
   205 }
       
   206 
       
   207 /*!
       
   208     Return application name
       
   209     \param id - reqiested application identyfier
       
   210 */
       
   211 QString TsModel::getApplicationName(int id)
       
   212 {
       
   213     QString retVal;
       
   214 #ifdef Q_OS_SYMBIAN
       
   215     TApaAppInfo info;
       
   216     iAppArcSession.GetAppInfo( info, TUid::Uid(id));
       
   217     retVal = QString::fromUtf16(info.iShortCaption.Ptr(),
       
   218                                 info.iShortCaption.Length());
       
   219 #endif
       
   220     return retVal;
       
   221 }
       
   222 
       
   223 /*!
       
   224     Called when some item was changed
       
   225     \param itemPtr - address of updated item
       
   226 */
       
   227 void TsModel::entryChanged(TsModelItem *itemPtr)
       
   228 {
       
   229     const int itemIndex = mEntries.indexOf(itemPtr);
       
   230     if (itemIndex != -1) {
       
   231         emit dataChanged(index(itemIndex, 0), index(itemIndex, 0));
       
   232     }
       
   233 }
       
   234 
       
   235 /*!
       
   236     Returns an entry from model
       
   237     \param index of entry in model
       
   238     \retval pointer to an entry
       
   239 */
       
   240 TsModelItem *TsModel::entry(const QModelIndex &index) const
       
   241 {
       
   242     return mEntries.at(index.row());
       
   243 }
       
   244