taskswitcherapp/tsdevicedialogplugin/src/tsmodel.cpp
changeset 62 341166945d65
parent 57 2e2dc3d30ca8
child 63 52b0f64eeb51
equal deleted inserted replaced
57:2e2dc3d30ca8 62:341166945d65
     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 <hsactivitydbclientinterface.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             if (entry->data(TsDataRoles::Visible).toBool()) { //visible activity filtering
       
   190                 mEntries.append(entry);
       
   191             } else {
       
   192                 delete entry;
       
   193             }
       
   194         }
       
   195     }
       
   196 }
       
   197 
       
   198 /*!
       
   199     Modify activity entry replacing application id with name
       
   200 */
       
   201 void TsModel::prepareActivityEntry(QVariantHash &activity)
       
   202 {
       
   203     activity.insert(TsActivityModelItem::applicationKeyword(),
       
   204                     activity.find(ActivityActivityKeyword) == activity.end() ?
       
   205                     QString::null :
       
   206                     getApplicationName(activity[ActivityApplicationKeyword].toInt()));
       
   207 }
       
   208 
       
   209 /*!
       
   210     Return application name
       
   211     \param id - reqiested application identyfier
       
   212 */
       
   213 QString TsModel::getApplicationName(int id)
       
   214 {
       
   215     QString retVal;
       
   216 #ifdef Q_OS_SYMBIAN
       
   217     TApaAppInfo info;
       
   218     iAppArcSession.GetAppInfo( info, TUid::Uid(id));
       
   219     retVal = QString::fromUtf16(info.iShortCaption.Ptr(),
       
   220                                 info.iShortCaption.Length());
       
   221 #endif
       
   222     return retVal;
       
   223 }
       
   224 
       
   225 /*!
       
   226     Called when some item was changed
       
   227     \param itemPtr - address of updated item
       
   228 */
       
   229 void TsModel::entryChanged(TsModelItem *itemPtr)
       
   230 {
       
   231     QList<TsModelItem *>::const_iterator iter(mEntries.constBegin());
       
   232     for (int offset(0); iter != mEntries.constEnd(); ++iter, ++offset) {
       
   233         if ((*iter) == itemPtr) {
       
   234             emit dataChanged(index(offset, 0), index(offset, 0));
       
   235             break;
       
   236         }
       
   237     }
       
   238 }
       
   239 
       
   240 /*!
       
   241     Returns an entry from model
       
   242     \param index of entry in model
       
   243     \retval pointer to an entry
       
   244 */
       
   245 TsModelItem *TsModel::entry(const QModelIndex &index) const
       
   246 {
       
   247     return mEntries.at(index.row());
       
   248 }
       
   249