taskswitcher/tsdevicedialogplugin/src/tsmodel.cpp
changeset 125 26079c1bb561
parent 119 50e220be30d1
child 127 7b66bc3c6dc9
equal deleted inserted replaced
123:d1dadafc5584 125:26079c1bb561
       
     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 #include "tsmodelitem.h"
       
    27 #include "tsdataroles.h"
       
    28 #include "tstaskchangeinfo.h"
       
    29 
       
    30 /*!
       
    31     \class TsModel
       
    32     \ingroup group_tsdevicedialogplugin
       
    33     \brief Model storing running tasks.
       
    34 */
       
    35 
       
    36 /*!
       
    37     Constructor
       
    38     \param query used to create model
       
    39     \param pointer to parent object
       
    40 */
       
    41 TsModel::TsModel(TsTaskMonitor &applicationSrv, QObject *parent) :
       
    42     QAbstractListModel(parent),
       
    43     mEntries(),
       
    44     mApplicationService(applicationSrv)
       
    45 {
       
    46     connect(&applicationSrv,
       
    47             SIGNAL(taskListChanged()),
       
    48             this,
       
    49             SLOT(updateApplications()));
       
    50 
       
    51     fullUpdate();
       
    52 }
       
    53 
       
    54 /*!
       
    55     Destructor
       
    56 */
       
    57 TsModel::~TsModel()
       
    58 {
       
    59     qDeleteAll(mEntries);
       
    60 }
       
    61 
       
    62 /*!
       
    63     Returns count of rows in model
       
    64     \retval number of rows
       
    65 */
       
    66 int TsModel::rowCount(const QModelIndex &parent) const
       
    67 {
       
    68     Q_UNUSED(parent);
       
    69     return mEntries.count();
       
    70 }
       
    71 
       
    72 bool TsModel::insertRows(int row, int count, TsModelItem *item, const QModelIndex &parent)
       
    73 {
       
    74     beginInsertRows(parent, row, row+count-1);
       
    75     mEntries.insert(row, item);
       
    76     endInsertRows();
       
    77     return true;
       
    78 }
       
    79 
       
    80 bool TsModel::removeRows(int row, int count, const QModelIndex &parent)
       
    81 {
       
    82     TsModelItem *oldItem = mEntries.at(row);
       
    83     beginRemoveRows(parent, row, row + count - 1);
       
    84     mEntries.removeAt(row);
       
    85     delete oldItem;
       
    86     endRemoveRows();
       
    87     return true;
       
    88 }
       
    89 
       
    90 
       
    91 bool TsModel::moveRows(int oldPosition, int newPosition, const QModelIndex &parent)
       
    92 {
       
    93     beginMoveRows(parent, oldPosition, oldPosition, parent, newPosition);
       
    94     mEntries.move(oldPosition, newPosition);
       
    95     endMoveRows();
       
    96     return true;
       
    97 }
       
    98 
       
    99 
       
   100 bool TsModel::updateRows(int row, TsModelItem *item)
       
   101 {
       
   102     TsModelItem *oldItem = mEntries.at(row);
       
   103     mEntries[row] = item;
       
   104     delete oldItem;
       
   105 
       
   106     emit dataChanged(index(row),index(row));
       
   107     return true;
       
   108 }
       
   109 
       
   110 /*!
       
   111     Returns appropiate model's data
       
   112     \param index model index
       
   113     \param role which data role to return
       
   114     \retval models data
       
   115 */
       
   116 QVariant TsModel::data(const QModelIndex &index, int role) const
       
   117 {
       
   118     return index.isValid() ? entry(index)->data(role) : QVariant();
       
   119 }
       
   120 
       
   121 /*!
       
   122     Activate one of model entries
       
   123 */
       
   124 void TsModel::openApplication(const QModelIndex &index)
       
   125 {
       
   126     if (!index.isValid()) {
       
   127         return;
       
   128     }
       
   129     entry(index)->open();
       
   130 }
       
   131 
       
   132 /*!
       
   133     Close one of moder entries
       
   134 */
       
   135 void TsModel::closeApplication(const QModelIndex &index)
       
   136 {
       
   137     if (!index.isValid() ||
       
   138             !entry(index)->data(TsDataRoles::Closable).toBool()) {
       
   139         return;
       
   140     }
       
   141     entry(index)->close();
       
   142 }
       
   143 
       
   144 /*!
       
   145     Updates model with fresh entries
       
   146 */
       
   147 void TsModel::updateApplications()
       
   148 {
       
   149     QList<TsTaskChange> changes(mApplicationService.changeList());
       
   150 
       
   151     if (changes.count() == 0) {
       
   152         return;
       
   153     }
       
   154     //check 1st item whether we have cancel change - if so reset model
       
   155     if (changes[0].first.changeType() == TsTaskChangeInfo::EChangeCancel) {
       
   156         fullUpdate();
       
   157         return;
       
   158     }
       
   159     for (int iter(0); iter < changes.count(); iter++) {
       
   160         switch (changes[iter].first.changeType()) {
       
   161             case TsTaskChangeInfo::EChangeDelete :
       
   162                 removeRows(changes[iter].first.oldOffset(), 1);
       
   163                 break;
       
   164             case TsTaskChangeInfo::EChangeInsert :
       
   165                 insertRows(changes[iter].first.newOffset(), 1,
       
   166                            new TsModelItem(changes[iter].second));
       
   167                 break;
       
   168             case TsTaskChangeInfo::EChangeMove :
       
   169                 moveRows(changes[iter].first.oldOffset(), changes[iter].first.newOffset());
       
   170                 break;
       
   171             case TsTaskChangeInfo::EChangeUpdate :
       
   172                 updateRows(changes[iter].first.oldOffset(),
       
   173                            new TsModelItem(changes[iter].second));
       
   174                 break;
       
   175             default:
       
   176                 break;
       
   177         }
       
   178     }
       
   179 }
       
   180 
       
   181 /*!
       
   182     reset model
       
   183 */
       
   184 void TsModel::fullUpdate()
       
   185 {
       
   186     beginResetModel();
       
   187     qDeleteAll(mEntries);
       
   188     mEntries.clear();
       
   189     getApplications();
       
   190     endResetModel();
       
   191 
       
   192 }
       
   193 
       
   194 /*!
       
   195     Read list of running applications
       
   196 */
       
   197 void TsModel::getApplications()
       
   198 {
       
   199     //get all running applications and append to entries list
       
   200     QList<TsTaskChange> tasks(mApplicationService.changeList(true));
       
   201     foreach(TsTaskChange taskData, tasks) {
       
   202         if (!taskData.second.isNull()) {
       
   203             mEntries.append(new TsModelItem(taskData.second));
       
   204         }
       
   205     }
       
   206 }
       
   207 
       
   208 /*!
       
   209     Returns an entry from model
       
   210     \param index of entry in model
       
   211     \retval pointer to an entry
       
   212 */
       
   213 TsModelItem *TsModel::entry(const QModelIndex &index) const
       
   214 {
       
   215     return mEntries.at(index.row());
       
   216 }