tsdevicedialog/tsdevicedialogplugin/src/tsmodel.cpp
changeset 124 e36b2f4799c0
parent 121 0b3699f6c654
equal deleted inserted replaced
121:0b3699f6c654 124:e36b2f4799c0
    21 
    21 
    22 #include <HbIcon>
    22 #include <HbIcon>
    23 
    23 
    24 #include <afstorageglobals.h>
    24 #include <afstorageglobals.h>
    25 
    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"
    26 #include "tsmodelitem.h"
    35 #include "tsentrymodelitem.h"
       
    36 #include "tsactivitymodelitem.h"
       
    37 #include "tsdataroles.h"
    27 #include "tsdataroles.h"
    38 #include "tstaskchangeinfo.h"
    28 #include "tstaskchangeinfo.h"
    39 
    29 
    40 const int maxItems(10);
       
    41 /*!
    30 /*!
    42     \class TsModel
    31     \class TsModel
    43     \ingroup group_tsdevicedialogplugin
    32     \ingroup group_tsdevicedialogplugin
    44     \brief Model storing running application and activieties.
    33     \brief Model storing running tasks.
    45 */
    34 */
    46 
    35 
    47 /*!
    36 /*!
    48     Constructor
    37     Constructor
    49     \param query used to create model
    38     \param query used to create model
    50     \param pointer to parent object
    39     \param pointer to parent object
    51 */
    40 */
    52 TsModel::TsModel(TsTaskMonitor &applicationSrv,
    41 TsModel::TsModel(TsTaskMonitor &applicationSrv, QObject *parent) :
    53                  QObject &activitySrv,
       
    54                  QObject *parent) :
       
    55     QAbstractListModel(parent),
    42     QAbstractListModel(parent),
    56     mEntries(),
    43     mEntries(),
    57     mApplicationService(applicationSrv),
    44     mApplicationService(applicationSrv)
    58     mActivityService(activitySrv),
    45 {
    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 #endif
       
    74 
       
    75     connect(&activitySrv,
       
    76             SIGNAL(dataChanged()),
       
    77             this,
       
    78             SLOT(updateActivities()));
       
    79     connect(&applicationSrv,
    46     connect(&applicationSrv,
    80             SIGNAL(taskListChanged()),
    47             SIGNAL(taskListChanged()),
    81             this,
    48             this,
    82             SLOT(updateApplications()));
    49             SLOT(updateApplications()));
    83 
    50 
    94 
    61 
    95 /*!
    62 /*!
    96     Returns count of rows in model
    63     Returns count of rows in model
    97     \retval number of rows
    64     \retval number of rows
    98 */
    65 */
    99 int TsModel::rowCount(
    66 int TsModel::rowCount(const QModelIndex &parent) const
   100     const QModelIndex &parent) const
       
   101 {
    67 {
   102     Q_UNUSED(parent);
    68     Q_UNUSED(parent);
   103     return mEntries.count();
    69     return mEntries.count();
   104 }
    70 }
   105 
    71 
   145     Returns appropiate model's data
   111     Returns appropiate model's data
   146     \param index model index
   112     \param index model index
   147     \param role which data role to return
   113     \param role which data role to return
   148     \retval models data
   114     \retval models data
   149 */
   115 */
   150 QVariant TsModel::data(const QModelIndex &index,
   116 QVariant TsModel::data(const QModelIndex &index, int role) const
   151                        int role) const
       
   152 {
   117 {
   153     return index.isValid() ? entry(index)->data(role) : QVariant();
   118     return index.isValid() ? entry(index)->data(role) : QVariant();
   154 }
       
   155 
       
   156 /*!
       
   157     Returns maximum anount of data allowed for model
       
   158     \retval maximum data count
       
   159 */
       
   160 
       
   161 int TsModel::maxRowCount()const
       
   162 {
       
   163     return mMaxItems;
       
   164 }
   119 }
   165 
   120 
   166 /*!
   121 /*!
   167     Activate one of model entries
   122     Activate one of model entries
   168 */
   123 */
   189 /*!
   144 /*!
   190     Updates model with fresh entries
   145     Updates model with fresh entries
   191 */
   146 */
   192 void TsModel::updateApplications()
   147 void TsModel::updateApplications()
   193 {
   148 {
   194     QList<TsTaskChange>  changes(mApplicationService.changeList());
   149     QList<TsTaskChange> changes(mApplicationService.changeList());
   195 
   150 
   196     if (changes.count() == 0) {
   151     if (changes.count() == 0) {
   197         //no applications - only activities on list
       
   198         return;
   152         return;
   199     }
   153     }
   200     //check 1st item whether we have cancel change - if so reset model
   154     //check 1st item whether we have cancel change - if so reset model
   201     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
   155     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
   202         fullUpdate();
   156         fullUpdate();
   204     }
   158     }
   205     for (int iter(0); iter < changes.count(); iter++) {
   159     for (int iter(0); iter < changes.count(); iter++) {
   206         switch (changes[iter].first.changeType()) {
   160         switch (changes[iter].first.changeType()) {
   207             case TsTaskChangeInfo::EChangeDelete :
   161             case TsTaskChangeInfo::EChangeDelete :
   208                 removeRows(changes[iter].first.oldOffset(), 1);
   162                 removeRows(changes[iter].first.oldOffset(), 1);
   209                 //we had max rows before delete - so there is possibility to add 
       
   210                 //one activity
       
   211                 if(mEntries.count() + 1 == maxRowCount()) {
       
   212                     beginInsertRows(QModelIndex(), mEntries.count(), mEntries.count());
       
   213                     getActivities(false);
       
   214                     endInsertRows();
       
   215                 }
       
   216                 break;
   163                 break;
   217             case TsTaskChangeInfo::EChangeInsert :
   164             case TsTaskChangeInfo::EChangeInsert :
   218                 insertRows(changes[iter].first.newOffset(), 1,
   165                 insertRows(changes[iter].first.newOffset(), 1,
   219                            new TsEntryModelItem(changes[iter].second));
   166                            new TsModelItem(changes[iter].second));
   220                 //we have too many items - delete some activities if we can
       
   221                 while(mEntries.count() > maxRowCount() && mActivitiesCount > 0) {
       
   222                     removeRows(mEntries.count()-1, 1);
       
   223                     mActivitiesCount--;
       
   224                 }
       
   225                 break;
   167                 break;
   226             case TsTaskChangeInfo::EChangeMove :
   168             case TsTaskChangeInfo::EChangeMove :
   227                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
   169                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
   228                 break;
   170                 break;
   229             case TsTaskChangeInfo::EChangeUpdate :
   171             case TsTaskChangeInfo::EChangeUpdate :
   230                 updateRows(changes[iter].first.oldOffset(),
   172                 updateRows(changes[iter].first.oldOffset(),
   231                            new TsEntryModelItem(changes[iter].second));
   173                            new TsModelItem(changes[iter].second));
   232                 break;
   174                 break;
   233             default:
   175             default:
   234                 break;
   176                 break;
   235         }
   177         }
   236     }
   178     }
   237 
   179 }
   238     //because delete entries are at end of changelist - iterate backwards
   180 
   239 
   181 /*!
   240 }
   182     reset model
   241 
       
   242 /*!
       
   243     Updates model with fresh entries
       
   244 */
       
   245 void TsModel::updateActivities()
       
   246 {
       
   247     //as for now we need full update when activities change
       
   248     fullUpdate();
       
   249 }
       
   250 
       
   251 /*!
       
   252     reset model using full application and activities lists
       
   253 */
   183 */
   254 void TsModel::fullUpdate()
   184 void TsModel::fullUpdate()
   255 {
   185 {
   256     beginResetModel();
   186     beginResetModel();
   257     qDeleteAll(mEntries);
   187     qDeleteAll(mEntries);
   258     mEntries.clear();
   188     mEntries.clear();
   259     mActivitiesCount = 0;
       
   260     getApplications();
   189     getApplications();
   261     getActivities();
       
   262     endResetModel();
   190     endResetModel();
   263 
   191 
   264 }
   192 }
   265 
   193 
   266 /*!
   194 /*!
   267     Read list of running applications
   195     Read list of running applications
   268 */
   196 */
   269 void TsModel::getApplications()
   197 void TsModel::getApplications()
   270 {
   198 {
   271     //get all running applications and append to entries list
   199     //get all running applications and append to entries list
   272     TsModelItem *entry(0);
   200     QList<TsTaskChange> tasks(mApplicationService.changeList(true));
   273     QList< TsTaskChange> tasks(mApplicationService.changeList(true));
       
   274     foreach(TsTaskChange taskData, tasks) {
   201     foreach(TsTaskChange taskData, tasks) {
   275         if (!taskData.second.isNull()) {
   202         if (!taskData.second.isNull()) {
   276             entry = new TsEntryModelItem(taskData.second);
   203             mEntries.append(new TsModelItem(taskData.second));
   277             if (entry) {
       
   278                 mEntries.append(entry);
       
   279             }
       
   280         }
   204         }
   281     }
       
   282 }
       
   283 
       
   284 /*!
       
   285     Read current activities
       
   286 */
       
   287 void TsModel::getActivities(bool fullUpdate)
       
   288 {
       
   289     //get activities
       
   290     int maxActivitiesCount = maxRowCount() - mEntries.count() + mActivitiesCount;
       
   291     if (maxActivitiesCount > 0) {
       
   292         QList<QVariantHash> activities;
       
   293         QMetaObject::invokeMethod(&mActivityService,
       
   294                                   "activitiesList",
       
   295                                   Q_RETURN_ARG(QList<QVariantHash>, activities),
       
   296                                   Q_ARG(int, maxActivitiesCount));
       
   297         int iterPos = fullUpdate ? 0 : mActivitiesCount;
       
   298         for(int iter(iterPos); iter< activities.count(); iter++) {
       
   299             mEntries.append(new TsActivityModelItem(*this, mActivityService, activities[iter]));
       
   300             mActivitiesCount++;
       
   301         }
       
   302     }
       
   303 }
       
   304 
       
   305 /*!
       
   306     Called when some item was changed
       
   307     \param itemPtr - address of updated item
       
   308 */
       
   309 void TsModel::entryChanged(TsModelItem *itemPtr)
       
   310 {
       
   311     const int itemIndex = mEntries.indexOf(itemPtr);
       
   312     if (itemIndex != -1) {
       
   313         emit dataChanged(index(itemIndex, 0), index(itemIndex, 0));
       
   314     }
   205     }
   315 }
   206 }
   316 
   207 
   317 /*!
   208 /*!
   318     Returns an entry from model
   209     Returns an entry from model