tsdevicedialog/tsdevicedialogplugin/src/tsmodel.cpp
branchRCL_3
changeset 113 0efa10d348c0
equal deleted inserted replaced
111:053c6c7c14f3 113:0efa10d348c0
       
     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 
       
    19 #include <QVariant>
       
    20 #include <QList>
       
    21 
       
    22 #include <HbIcon>
       
    23 
       
    24 #include <afstorageglobals.h>
       
    25 
       
    26 #ifdef Q_OS_SYMBIAN
       
    27 #include <XQSettingsManager>
       
    28 #include <apaid.h>
       
    29 
       
    30 const int TSDeviceDialogUid = 0x2002677F;
       
    31 const int ItemsLimit = 0x00000001;
       
    32 #endif
       
    33 
       
    34 #include "tsmodelitem.h"
       
    35 #include "tsentrymodelitem.h"
       
    36 #include "tsactivitymodelitem.h"
       
    37 #include "tsdataroles.h"
       
    38 #include "tstaskchangeinfo.h"
       
    39 
       
    40 const int maxItems(10);
       
    41 /*!
       
    42     \class TsModel
       
    43     \ingroup group_tsdevicedialogplugin
       
    44     \brief Model storing running application and activieties.
       
    45 */
       
    46 
       
    47 /*!
       
    48     Constructor
       
    49     \param query used to create model
       
    50     \param pointer to parent object
       
    51 */
       
    52 TsModel::TsModel(TsTaskMonitor &applicationSrv, 
       
    53                  QObject &activitySrv, 
       
    54                  QObject *parent) :
       
    55     QAbstractListModel(parent),
       
    56     mEntries(),
       
    57     mApplicationService(applicationSrv),
       
    58     mActivityService(activitySrv),
       
    59     mMaxItems(maxItems)
       
    60 {
       
    61     
       
    62 #ifdef Q_OS_SYMBIAN
       
    63     XQSettingsManager *crManager = new XQSettingsManager;
       
    64     XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit);
       
    65     QVariant itemsNumberVariant = 
       
    66         crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt);
       
    67     if (!itemsNumberVariant.isNull()) {
       
    68         int number = itemsNumberVariant.toInt();
       
    69         if (number > 0) {
       
    70             mMaxItems = number;
       
    71         }
       
    72     }
       
    73     iAppArcSession.Connect();
       
    74 #endif
       
    75 
       
    76     connect(&activitySrv,
       
    77 	SIGNAL(dataChanged()),
       
    78 			 this,
       
    79 			 SLOT(updateActivities()));
       
    80     connect(&applicationSrv,
       
    81 	        SIGNAL(taskListChanged()),
       
    82 			this,
       
    83 			SLOT(updateApplications()));
       
    84 
       
    85     fullUpdate();    
       
    86 }
       
    87 
       
    88 /*!
       
    89     Destructor
       
    90 */
       
    91 TsModel::~TsModel()
       
    92 {
       
    93 #ifdef Q_OS_SYMBIAN
       
    94     iAppArcSession.Close();
       
    95 #endif
       
    96     qDeleteAll(mEntries);
       
    97 }
       
    98 
       
    99 /*!
       
   100     Returns count of rows in model
       
   101     \retval number of rows
       
   102 */
       
   103 int TsModel::rowCount(
       
   104     const QModelIndex &parent) const
       
   105 {
       
   106     Q_UNUSED(parent);
       
   107     return mEntries.count();
       
   108 }
       
   109 
       
   110 bool TsModel::insertRows(int row, int count, TsModelItem* item, const QModelIndex & parent)
       
   111 {
       
   112     beginInsertRows(parent, row, row+count-1);
       
   113     mEntries.insert(row, item);
       
   114     endInsertRows();
       
   115     return true;
       
   116 }
       
   117 
       
   118 bool TsModel::removeRows(int row, int count, const QModelIndex & parent)
       
   119 {
       
   120     beginRemoveRows(parent, row, row + count - 1);
       
   121     mEntries.removeAt(row);
       
   122     endRemoveRows();
       
   123     return true;
       
   124 }
       
   125 
       
   126 
       
   127 bool TsModel::moveRows(int oldPosition, int newPosition, const QModelIndex & parent)
       
   128 {
       
   129    beginMoveRows(parent, oldPosition, oldPosition, parent, newPosition);
       
   130    mEntries.move(oldPosition, newPosition);
       
   131    endMoveRows();
       
   132    return true;
       
   133 }
       
   134 
       
   135 
       
   136 bool TsModel::updateRows(int row, TsModelItem* item)
       
   137 {
       
   138     TsModelItem *oldItem = mEntries.at(row);
       
   139     mEntries[row] = item;
       
   140     delete oldItem;
       
   141 
       
   142     emit dataChanged(index(row),index(row));
       
   143     return true;
       
   144 }
       
   145 
       
   146 /*!
       
   147     Returns appropiate model's data
       
   148     \param index model index
       
   149     \param role which data role to return
       
   150     \retval models data
       
   151 */
       
   152 QVariant TsModel::data(const QModelIndex &index,
       
   153                        int role) const
       
   154 {
       
   155     return index.isValid() ? entry(index)->data(role) : QVariant();
       
   156 }
       
   157 
       
   158 /*!
       
   159     Returns maximum anount of data allowed for model
       
   160     \retval maximum data count
       
   161 */
       
   162 
       
   163 int TsModel::maxRowCount()const
       
   164 {
       
   165     return mMaxItems;
       
   166 }
       
   167 
       
   168 /*!
       
   169     Activate one of model entries
       
   170 */
       
   171 void TsModel::openApplication(const QModelIndex &index)
       
   172 {
       
   173     if (!index.isValid()) {
       
   174         return;
       
   175     }
       
   176     entry(index)->open();
       
   177 }
       
   178 
       
   179 /*!
       
   180     Close one of moder entries
       
   181 */
       
   182 void TsModel::closeApplication(const QModelIndex &index)
       
   183 {
       
   184     if (!index.isValid() || 
       
   185         !entry(index)->data(TsDataRoles::Closable).toBool()) {
       
   186         return;
       
   187     }
       
   188     entry(index)->close();
       
   189 }
       
   190 
       
   191 /*!
       
   192     Updates model with fresh entries
       
   193 */
       
   194 void TsModel::updateApplications()
       
   195 {
       
   196     RDebug::Printf( "TsModel::updateApps \n");
       
   197     RDebug::Printf(" from %d \n: ",this);
       
   198     QList<TsTaskChange>  changes(mApplicationService.changeList());
       
   199     
       
   200     if(changes.count() == 0)
       
   201     {
       
   202         //no applications - only activities on list
       
   203         return;
       
   204     }
       
   205     //check 1st item whether we have cancel change - if so reset model
       
   206     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
       
   207         fullUpdate();
       
   208         return;
       
   209     }
       
   210     for (int iter(0); iter < changes.count(); iter++) {
       
   211         switch (changes[iter].first.changeType()) {
       
   212             case TsTaskChangeInfo::EChangeDelete :
       
   213                 removeRows(changes[iter].first.oldOffset(), 1);
       
   214             break;
       
   215             case TsTaskChangeInfo::EChangeInsert :
       
   216                 insertRows(changes[iter].first.newOffset(), 1,
       
   217                     new TsEntryModelItem(changes[iter].second));
       
   218             break;
       
   219             case TsTaskChangeInfo::EChangeMove :
       
   220                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
       
   221             break;            
       
   222             case TsTaskChangeInfo::EChangeUpdate :
       
   223                 updateRows(changes[iter].first.oldOffset(),
       
   224                            new TsEntryModelItem(changes[iter].second));
       
   225             break;
       
   226             default:
       
   227             break;
       
   228         }
       
   229     }
       
   230 
       
   231     //because delete entries are at end of changelist - iterate backwards
       
   232 
       
   233 }
       
   234 
       
   235 /*!
       
   236     Updates model with fresh entries
       
   237 */
       
   238 void TsModel::updateActivities()
       
   239 {
       
   240     //as for now we need full update when activities change
       
   241     fullUpdate();
       
   242 }
       
   243 
       
   244 /*!
       
   245     reset model using full application and activities lists
       
   246 */
       
   247 void TsModel::fullUpdate()
       
   248 {
       
   249     qDeleteAll(mEntries);
       
   250     mEntries.clear();
       
   251     getApplications();
       
   252     getActivities();
       
   253     endResetModel();
       
   254 
       
   255 }
       
   256 
       
   257 /*!
       
   258     Read list of running applications
       
   259 */
       
   260 void TsModel::getApplications()
       
   261 {
       
   262     RDebug::Printf( "CTsTaskMonitorClientImpl::RunL \n");
       
   263     //get all running applications and append to entries list
       
   264     TsModelItem *entry(0);
       
   265     QList< TsTaskChange> tasks(mApplicationService.changeList(true));
       
   266     foreach (TsTaskChange taskData, tasks) {
       
   267         if (!taskData.second.isNull()) {
       
   268             entry = new TsEntryModelItem(taskData.second);
       
   269             if (entry) {
       
   270                 mEntries.append(entry);
       
   271             }
       
   272         }
       
   273     }
       
   274 }
       
   275 
       
   276 /*!
       
   277     Read current activities
       
   278 */
       
   279 void TsModel::getActivities()
       
   280 {
       
   281     //get activities
       
   282     TsModelItem *entry(0);
       
   283     QList<QVariantHash> activities;
       
   284     QMetaObject::invokeMethod(&mActivityService, 
       
   285                               "activitiesList", 
       
   286                               Q_RETURN_ARG(QList<QVariantHash>, 
       
   287                               activities));
       
   288     foreach(QVariantHash activity, activities) {
       
   289         prepareActivityEntry(activity);
       
   290         entry = new TsActivityModelItem(*this, mActivityService, activity);
       
   291         if (maxRowCount() <= mEntries.count()) {
       
   292             delete entry;
       
   293             break;
       
   294         }
       
   295         mEntries.append(entry);
       
   296     }
       
   297 }
       
   298 
       
   299 /*!
       
   300     Modify activity entry replacing application id with name
       
   301 */
       
   302 void TsModel::prepareActivityEntry(QVariantHash &activity)
       
   303 {
       
   304     if (!activity.contains(TsActivityModelItem::applicationKeyword())) {
       
   305         activity.insert(TsActivityModelItem::applicationKeyword(),
       
   306                     activity.contains(ActivityApplicationKeyword) ?
       
   307                     getApplicationName(activity[ActivityApplicationKeyword].toInt()) :
       
   308                     QString::null);
       
   309     }
       
   310 }
       
   311 
       
   312 /*!
       
   313     Return application name
       
   314     \param id - reqiested application identyfier
       
   315 */
       
   316 QString TsModel::getApplicationName(int id)
       
   317 {
       
   318     QString retVal;
       
   319 #ifdef Q_OS_SYMBIAN
       
   320     TApaAppInfo info;
       
   321     iAppArcSession.GetAppInfo( info, TUid::Uid(id));
       
   322     retVal = QString::fromUtf16(info.iShortCaption.Ptr(),
       
   323                                 info.iShortCaption.Length());
       
   324 #endif
       
   325     return retVal;
       
   326 }
       
   327 
       
   328 /*!
       
   329     Called when some item was changed
       
   330     \param itemPtr - address of updated item
       
   331 */
       
   332 void TsModel::entryChanged(TsModelItem *itemPtr)
       
   333 {
       
   334     const int itemIndex = mEntries.indexOf(itemPtr);
       
   335     if (itemIndex != -1) {
       
   336         emit dataChanged(index(itemIndex, 0), index(itemIndex, 0));
       
   337     }
       
   338 }
       
   339 
       
   340 /*!
       
   341     Returns an entry from model
       
   342     \param index of entry in model
       
   343     \retval pointer to an entry
       
   344 */
       
   345 TsModelItem *TsModel::entry(const QModelIndex &index) const
       
   346 {
       
   347     return mEntries.at(index.row());
       
   348 }
       
   349