tsdevicedialog/tsdevicedialogplugin/src/tsmodel.cpp
changeset 121 0b3699f6c654
parent 115 3ab5c078b490
child 119 50e220be30d1
equal deleted inserted replaced
115:3ab5c078b490 121:0b3699f6c654
    47 /*!
    47 /*!
    48     Constructor
    48     Constructor
    49     \param query used to create model
    49     \param query used to create model
    50     \param pointer to parent object
    50     \param pointer to parent object
    51 */
    51 */
    52 TsModel::TsModel(TsTaskMonitor &applicationSrv, 
    52 TsModel::TsModel(TsTaskMonitor &applicationSrv,
    53                  QObject &activitySrv, 
    53                  QObject &activitySrv,
    54                  QObject *parent) :
    54                  QObject *parent) :
    55     QAbstractListModel(parent),
    55     QAbstractListModel(parent),
    56     mEntries(),
    56     mEntries(),
    57     mApplicationService(applicationSrv),
    57     mApplicationService(applicationSrv),
    58     mActivityService(activitySrv),
    58     mActivityService(activitySrv),
    59     mMaxItems(maxItems)
    59     mMaxItems(maxItems)
    60 {
    60 {
    61     
    61 
    62 #ifdef Q_OS_SYMBIAN
    62 #ifdef Q_OS_SYMBIAN
    63     XQSettingsManager *crManager = new XQSettingsManager;
    63     XQSettingsManager *crManager = new XQSettingsManager;
    64     XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit);
    64     XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit);
    65     QVariant itemsNumberVariant = 
    65     QVariant itemsNumberVariant =
    66         crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt);
    66         crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt);
    67     if (!itemsNumberVariant.isNull()) {
    67     if (!itemsNumberVariant.isNull()) {
    68         int number = itemsNumberVariant.toInt();
    68         int number = itemsNumberVariant.toInt();
    69         if (number > 0) {
    69         if (number > 0) {
    70             mMaxItems = number;
    70             mMaxItems = number;
    71         }
    71         }
    72     }
    72     }
    73     iAppArcSession.Connect();
       
    74 #endif
    73 #endif
    75 
    74 
    76     connect(&activitySrv,
    75     connect(&activitySrv,
    77 	SIGNAL(dataChanged()),
    76             SIGNAL(dataChanged()),
    78 			 this,
    77             this,
    79 			 SLOT(updateActivities()));
    78             SLOT(updateActivities()));
    80     connect(&applicationSrv,
    79     connect(&applicationSrv,
    81 	        SIGNAL(taskListChanged()),
    80             SIGNAL(taskListChanged()),
    82 			this,
    81             this,
    83 			SLOT(updateApplications()));
    82             SLOT(updateApplications()));
    84 
    83 
    85     fullUpdate();    
    84     fullUpdate();
    86 }
    85 }
    87 
    86 
    88 /*!
    87 /*!
    89     Destructor
    88     Destructor
    90 */
    89 */
    91 TsModel::~TsModel()
    90 TsModel::~TsModel()
    92 {
    91 {
    93 #ifdef Q_OS_SYMBIAN
       
    94     iAppArcSession.Close();
       
    95 #endif
       
    96     qDeleteAll(mEntries);
    92     qDeleteAll(mEntries);
    97 }
    93 }
    98 
    94 
    99 /*!
    95 /*!
   100     Returns count of rows in model
    96     Returns count of rows in model
   105 {
   101 {
   106     Q_UNUSED(parent);
   102     Q_UNUSED(parent);
   107     return mEntries.count();
   103     return mEntries.count();
   108 }
   104 }
   109 
   105 
   110 bool TsModel::insertRows(int row, int count, TsModelItem* item, const QModelIndex & parent)
   106 bool TsModel::insertRows(int row, int count, TsModelItem *item, const QModelIndex &parent)
   111 {
   107 {
   112     beginInsertRows(parent, row, row+count-1);
   108     beginInsertRows(parent, row, row+count-1);
   113     mEntries.insert(row, item);
   109     mEntries.insert(row, item);
   114     endInsertRows();
   110     endInsertRows();
   115     return true;
   111     return true;
   116 }
   112 }
   117 
   113 
   118 bool TsModel::removeRows(int row, int count, const QModelIndex & parent)
   114 bool TsModel::removeRows(int row, int count, const QModelIndex &parent)
   119 {
   115 {
       
   116     TsModelItem *oldItem = mEntries.at(row);
   120     beginRemoveRows(parent, row, row + count - 1);
   117     beginRemoveRows(parent, row, row + count - 1);
   121     mEntries.removeAt(row);
   118     mEntries.removeAt(row);
       
   119     delete oldItem;
   122     endRemoveRows();
   120     endRemoveRows();
   123     return true;
   121     return true;
   124 }
   122 }
   125 
   123 
   126 
   124 
   127 bool TsModel::moveRows(int oldPosition, int newPosition, const QModelIndex & parent)
   125 bool TsModel::moveRows(int oldPosition, int newPosition, const QModelIndex &parent)
   128 {
   126 {
   129    beginMoveRows(parent, oldPosition, oldPosition, parent, newPosition);
   127     beginMoveRows(parent, oldPosition, oldPosition, parent, newPosition);
   130    mEntries.move(oldPosition, newPosition);
   128     mEntries.move(oldPosition, newPosition);
   131    endMoveRows();
   129     endMoveRows();
   132    return true;
   130     return true;
   133 }
   131 }
   134 
   132 
   135 
   133 
   136 bool TsModel::updateRows(int row, TsModelItem* item)
   134 bool TsModel::updateRows(int row, TsModelItem *item)
   137 {
   135 {
   138     TsModelItem *oldItem = mEntries.at(row);
   136     TsModelItem *oldItem = mEntries.at(row);
   139     mEntries[row] = item;
   137     mEntries[row] = item;
   140     delete oldItem;
   138     delete oldItem;
   141 
   139 
   179 /*!
   177 /*!
   180     Close one of moder entries
   178     Close one of moder entries
   181 */
   179 */
   182 void TsModel::closeApplication(const QModelIndex &index)
   180 void TsModel::closeApplication(const QModelIndex &index)
   183 {
   181 {
   184     if (!index.isValid() || 
   182     if (!index.isValid() ||
   185         !entry(index)->data(TsDataRoles::Closable).toBool()) {
   183             !entry(index)->data(TsDataRoles::Closable).toBool()) {
   186         return;
   184         return;
   187     }
   185     }
   188     entry(index)->close();
   186     entry(index)->close();
   189 }
   187 }
   190 
   188 
   191 /*!
   189 /*!
   192     Updates model with fresh entries
   190     Updates model with fresh entries
   193 */
   191 */
   194 void TsModel::updateApplications()
   192 void TsModel::updateApplications()
   195 {
   193 {
   196     RDebug::Printf( "TsModel::updateApps \n");
       
   197     RDebug::Printf(" from %d \n: ",this);
       
   198     QList<TsTaskChange>  changes(mApplicationService.changeList());
   194     QList<TsTaskChange>  changes(mApplicationService.changeList());
   199     
   195 
   200     if(changes.count() == 0)
   196     if (changes.count() == 0) {
   201     {
       
   202         //no applications - only activities on list
   197         //no applications - only activities on list
   203         return;
   198         return;
   204     }
   199     }
   205     //check 1st item whether we have cancel change - if so reset model
   200     //check 1st item whether we have cancel change - if so reset model
   206     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
   201     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
   209     }
   204     }
   210     for (int iter(0); iter < changes.count(); iter++) {
   205     for (int iter(0); iter < changes.count(); iter++) {
   211         switch (changes[iter].first.changeType()) {
   206         switch (changes[iter].first.changeType()) {
   212             case TsTaskChangeInfo::EChangeDelete :
   207             case TsTaskChangeInfo::EChangeDelete :
   213                 removeRows(changes[iter].first.oldOffset(), 1);
   208                 removeRows(changes[iter].first.oldOffset(), 1);
   214             break;
   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;
   215             case TsTaskChangeInfo::EChangeInsert :
   217             case TsTaskChangeInfo::EChangeInsert :
   216                 insertRows(changes[iter].first.newOffset(), 1,
   218                 insertRows(changes[iter].first.newOffset(), 1,
   217                     new TsEntryModelItem(changes[iter].second));
   219                            new TsEntryModelItem(changes[iter].second));
   218             break;
   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;
   219             case TsTaskChangeInfo::EChangeMove :
   226             case TsTaskChangeInfo::EChangeMove :
   220                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
   227                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
   221             break;            
   228                 break;
   222             case TsTaskChangeInfo::EChangeUpdate :
   229             case TsTaskChangeInfo::EChangeUpdate :
   223                 updateRows(changes[iter].first.oldOffset(),
   230                 updateRows(changes[iter].first.oldOffset(),
   224                            new TsEntryModelItem(changes[iter].second));
   231                            new TsEntryModelItem(changes[iter].second));
   225             break;
   232                 break;
   226             default:
   233             default:
   227             break;
   234                 break;
   228         }
   235         }
   229     }
   236     }
   230 
   237 
   231     //because delete entries are at end of changelist - iterate backwards
   238     //because delete entries are at end of changelist - iterate backwards
   232 
   239 
   244 /*!
   251 /*!
   245     reset model using full application and activities lists
   252     reset model using full application and activities lists
   246 */
   253 */
   247 void TsModel::fullUpdate()
   254 void TsModel::fullUpdate()
   248 {
   255 {
       
   256     beginResetModel();
   249     qDeleteAll(mEntries);
   257     qDeleteAll(mEntries);
   250     mEntries.clear();
   258     mEntries.clear();
       
   259     mActivitiesCount = 0;
   251     getApplications();
   260     getApplications();
   252     getActivities();
   261     getActivities();
   253     endResetModel();
   262     endResetModel();
   254 
   263 
   255 }
   264 }
   257 /*!
   266 /*!
   258     Read list of running applications
   267     Read list of running applications
   259 */
   268 */
   260 void TsModel::getApplications()
   269 void TsModel::getApplications()
   261 {
   270 {
   262     RDebug::Printf( "CTsTaskMonitorClientImpl::RunL \n");
       
   263     //get all running applications and append to entries list
   271     //get all running applications and append to entries list
   264     TsModelItem *entry(0);
   272     TsModelItem *entry(0);
   265     QList< TsTaskChange> tasks(mApplicationService.changeList(true));
   273     QList< TsTaskChange> tasks(mApplicationService.changeList(true));
   266     foreach (TsTaskChange taskData, tasks) {
   274     foreach(TsTaskChange taskData, tasks) {
   267         if (!taskData.second.isNull()) {
   275         if (!taskData.second.isNull()) {
   268             entry = new TsEntryModelItem(taskData.second);
   276             entry = new TsEntryModelItem(taskData.second);
   269             if (entry) {
   277             if (entry) {
   270                 mEntries.append(entry);
   278                 mEntries.append(entry);
   271             }
   279             }
   274 }
   282 }
   275 
   283 
   276 /*!
   284 /*!
   277     Read current activities
   285     Read current activities
   278 */
   286 */
   279 void TsModel::getActivities()
   287 void TsModel::getActivities(bool fullUpdate)
   280 {
   288 {
   281     //get activities
   289     //get activities
   282     TsModelItem *entry(0);
   290     int maxActivitiesCount = maxRowCount() - mEntries.count() + mActivitiesCount;
   283     QList<QVariantHash> activities;
   291     if (maxActivitiesCount > 0) {
   284     QMetaObject::invokeMethod(&mActivityService, 
   292         QList<QVariantHash> activities;
   285                               "activitiesList", 
   293         QMetaObject::invokeMethod(&mActivityService,
   286                               Q_RETURN_ARG(QList<QVariantHash>, 
   294                                   "activitiesList",
   287                               activities));
   295                                   Q_RETURN_ARG(QList<QVariantHash>, activities),
   288     foreach(QVariantHash activity, activities) {
   296                                   Q_ARG(int, maxActivitiesCount));
   289         prepareActivityEntry(activity);
   297         int iterPos = fullUpdate ? 0 : mActivitiesCount;
   290         entry = new TsActivityModelItem(*this, mActivityService, activity);
   298         for(int iter(iterPos); iter< activities.count(); iter++) {
   291         if (maxRowCount() <= mEntries.count()) {
   299             mEntries.append(new TsActivityModelItem(*this, mActivityService, activities[iter]));
   292             delete entry;
   300             mActivitiesCount++;
   293             break;
       
   294         }
   301         }
   295         mEntries.append(entry);
   302     }
   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 }
   303 }
   327 
   304 
   328 /*!
   305 /*!
   329     Called when some item was changed
   306     Called when some item was changed
   330     \param itemPtr - address of updated item
   307     \param itemPtr - address of updated item
   344 */
   321 */
   345 TsModelItem *TsModel::entry(const QModelIndex &index) const
   322 TsModelItem *TsModel::entry(const QModelIndex &index) const
   346 {
   323 {
   347     return mEntries.at(index.row());
   324     return mEntries.at(index.row());
   348 }
   325 }
   349