tsdevicedialog/tsdevicedialogplugin/src/tsmodel.cpp
author hgs
Fri, 09 Jul 2010 14:19:08 +0300
changeset 102 8b8b34fa9751
parent 99 7aaf39b772ac
child 107 b34d53f6acdf
permissions -rw-r--r--
201027
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
99
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: tsmodel.cpp
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
#include "tsmodel.h"
hgs
parents:
diff changeset
    18
#include "tsmodelitem.h"
hgs
parents:
diff changeset
    19
#include "tsentrymodelitem.h"
hgs
parents:
diff changeset
    20
#include "tsactivitymodelitem.h"
hgs
parents:
diff changeset
    21
#include "tsdataroles.h"
hgs
parents:
diff changeset
    22
hgs
parents:
diff changeset
    23
#include <HbIcon>
hgs
parents:
diff changeset
    24
#include <qvariant.h>
hgs
parents:
diff changeset
    25
#include <qlist.h>
hgs
parents:
diff changeset
    26
#include <afstorageglobals.h>
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
#ifdef Q_OS_SYMBIAN
hgs
parents:
diff changeset
    29
#include <XQSettingsManager>
hgs
parents:
diff changeset
    30
#include <apaid.h>
hgs
parents:
diff changeset
    31
hgs
parents:
diff changeset
    32
const int TSDeviceDialogUid = 0x2002677F;
hgs
parents:
diff changeset
    33
const int ItemsLimit = 0x00000001;
hgs
parents:
diff changeset
    34
#endif
hgs
parents:
diff changeset
    35
hgs
parents:
diff changeset
    36
const int maxItems(10);
hgs
parents:
diff changeset
    37
/*!
hgs
parents:
diff changeset
    38
    \class TsModel
hgs
parents:
diff changeset
    39
    \ingroup group_tsdevicedialogplugin
hgs
parents:
diff changeset
    40
    \brief Model storing running application and activieties.
hgs
parents:
diff changeset
    41
*/
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
/*!
hgs
parents:
diff changeset
    44
    Constructor
hgs
parents:
diff changeset
    45
    \param query used to create model
hgs
parents:
diff changeset
    46
    \param pointer to parent object
hgs
parents:
diff changeset
    47
*/
hgs
parents:
diff changeset
    48
TsModel::TsModel(TsTaskMonitor &applicationSrv, QObject &activitySrv, QObject *parent) :
hgs
parents:
diff changeset
    49
    QAbstractListModel(parent),
hgs
parents:
diff changeset
    50
    mEntries(),
hgs
parents:
diff changeset
    51
    mApplicationService(applicationSrv),
hgs
parents:
diff changeset
    52
    mActivityService(activitySrv),
hgs
parents:
diff changeset
    53
    mSize(240, 240),
hgs
parents:
diff changeset
    54
    mMaxItems(maxItems)
hgs
parents:
diff changeset
    55
{
hgs
parents:
diff changeset
    56
    
hgs
parents:
diff changeset
    57
#ifdef Q_OS_SYMBIAN
hgs
parents:
diff changeset
    58
    XQSettingsManager *crManager = new XQSettingsManager;
hgs
parents:
diff changeset
    59
    XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit);
hgs
parents:
diff changeset
    60
    QVariant itemsNumberVariant = crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt);
hgs
parents:
diff changeset
    61
    if (!itemsNumberVariant.isNull()) {
hgs
parents:
diff changeset
    62
        int number = itemsNumberVariant.toInt();
hgs
parents:
diff changeset
    63
        if (number > 0) {
hgs
parents:
diff changeset
    64
            mMaxItems = number;
hgs
parents:
diff changeset
    65
        }
hgs
parents:
diff changeset
    66
    }
hgs
parents:
diff changeset
    67
    iAppArcSession.Connect();
hgs
parents:
diff changeset
    68
#endif
hgs
parents:
diff changeset
    69
hgs
parents:
diff changeset
    70
    connect(&activitySrv, SIGNAL(dataChanged()), this, SLOT(updateModel()));
hgs
parents:
diff changeset
    71
    connect(&applicationSrv, SIGNAL(taskListChanged()), this, SLOT(updateModel()));
hgs
parents:
diff changeset
    72
    updateModel();
hgs
parents:
diff changeset
    73
}
hgs
parents:
diff changeset
    74
hgs
parents:
diff changeset
    75
/*!
hgs
parents:
diff changeset
    76
    Destructor
hgs
parents:
diff changeset
    77
*/
hgs
parents:
diff changeset
    78
TsModel::~TsModel()
hgs
parents:
diff changeset
    79
{
hgs
parents:
diff changeset
    80
#ifdef Q_OS_SYMBIAN
hgs
parents:
diff changeset
    81
    iAppArcSession.Close();
hgs
parents:
diff changeset
    82
#endif
hgs
parents:
diff changeset
    83
    qDeleteAll(mEntries);
hgs
parents:
diff changeset
    84
}
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
/*!
hgs
parents:
diff changeset
    87
    Returns count of rows in model
hgs
parents:
diff changeset
    88
    \retval number of rows
hgs
parents:
diff changeset
    89
*/
hgs
parents:
diff changeset
    90
int TsModel::rowCount(
hgs
parents:
diff changeset
    91
    const QModelIndex &parent) const
hgs
parents:
diff changeset
    92
{
hgs
parents:
diff changeset
    93
    Q_UNUSED(parent);
hgs
parents:
diff changeset
    94
    return mEntries.count();
hgs
parents:
diff changeset
    95
}
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97
/*!
hgs
parents:
diff changeset
    98
    Returns appropiate model's data
hgs
parents:
diff changeset
    99
    \param index model index
hgs
parents:
diff changeset
   100
    \param role which data role to return
hgs
parents:
diff changeset
   101
    \retval models data
hgs
parents:
diff changeset
   102
*/
hgs
parents:
diff changeset
   103
QVariant TsModel::data(const QModelIndex &index,
hgs
parents:
diff changeset
   104
                       int role) const
hgs
parents:
diff changeset
   105
{
hgs
parents:
diff changeset
   106
    return index.isValid() ? entry(index)->data(role) : QVariant();
hgs
parents:
diff changeset
   107
}
hgs
parents:
diff changeset
   108
hgs
parents:
diff changeset
   109
/*!
hgs
parents:
diff changeset
   110
    Returns maximum anount of data allowed for model
hgs
parents:
diff changeset
   111
    \retval maximum data count
hgs
parents:
diff changeset
   112
*/
hgs
parents:
diff changeset
   113
hgs
parents:
diff changeset
   114
int TsModel::maxRowCount()const
hgs
parents:
diff changeset
   115
{
hgs
parents:
diff changeset
   116
    return mMaxItems;
hgs
parents:
diff changeset
   117
}
hgs
parents:
diff changeset
   118
hgs
parents:
diff changeset
   119
/*!
hgs
parents:
diff changeset
   120
    Activate one of model entries
hgs
parents:
diff changeset
   121
*/
hgs
parents:
diff changeset
   122
void TsModel::openApplication(const QModelIndex &index)
hgs
parents:
diff changeset
   123
{
hgs
parents:
diff changeset
   124
    if (!index.isValid()) {
hgs
parents:
diff changeset
   125
        return;
hgs
parents:
diff changeset
   126
    }
hgs
parents:
diff changeset
   127
    entry(index)->open();
hgs
parents:
diff changeset
   128
}
hgs
parents:
diff changeset
   129
hgs
parents:
diff changeset
   130
/*!
hgs
parents:
diff changeset
   131
    Close one of moder entries
hgs
parents:
diff changeset
   132
*/
hgs
parents:
diff changeset
   133
void TsModel::closeApplication(const QModelIndex &index)
hgs
parents:
diff changeset
   134
{
hgs
parents:
diff changeset
   135
    if (!index.isValid() || !entry(index)->data(TsDataRoles::Closable).toBool()) {
hgs
parents:
diff changeset
   136
        return;
hgs
parents:
diff changeset
   137
    }
hgs
parents:
diff changeset
   138
    entry(index)->close();
hgs
parents:
diff changeset
   139
}
hgs
parents:
diff changeset
   140
hgs
parents:
diff changeset
   141
/*!
hgs
parents:
diff changeset
   142
    Updates model with fresh entries
hgs
parents:
diff changeset
   143
*/
hgs
parents:
diff changeset
   144
void TsModel::updateModel()
hgs
parents:
diff changeset
   145
{
hgs
parents:
diff changeset
   146
    //clear current data
hgs
parents:
diff changeset
   147
    qDeleteAll(mEntries);
hgs
parents:
diff changeset
   148
    mEntries.clear();
hgs
parents:
diff changeset
   149
hgs
parents:
diff changeset
   150
    beginResetModel();
hgs
parents:
diff changeset
   151
    getApplications();
hgs
parents:
diff changeset
   152
    getActivities();
hgs
parents:
diff changeset
   153
    endResetModel();
hgs
parents:
diff changeset
   154
hgs
parents:
diff changeset
   155
}
hgs
parents:
diff changeset
   156
hgs
parents:
diff changeset
   157
/*!
hgs
parents:
diff changeset
   158
    Read list of running applications
hgs
parents:
diff changeset
   159
*/
hgs
parents:
diff changeset
   160
void TsModel::getApplications()
hgs
parents:
diff changeset
   161
{
hgs
parents:
diff changeset
   162
    //get running applications
hgs
parents:
diff changeset
   163
    TsModelItem *entry(0);
hgs
parents:
diff changeset
   164
    QList< QSharedPointer<TsTask> > tasks(mApplicationService.taskList());
hgs
parents:
diff changeset
   165
    foreach (QSharedPointer<TsTask> taskData, tasks) {
hgs
parents:
diff changeset
   166
        entry = new TsEntryModelItem(taskData);
hgs
parents:
diff changeset
   167
        if (0 != entry) {
hgs
parents:
diff changeset
   168
            mEntries.append(entry);
hgs
parents:
diff changeset
   169
        }
hgs
parents:
diff changeset
   170
    }
hgs
parents:
diff changeset
   171
}
hgs
parents:
diff changeset
   172
hgs
parents:
diff changeset
   173
/*!
hgs
parents:
diff changeset
   174
    Read current activities
hgs
parents:
diff changeset
   175
*/
hgs
parents:
diff changeset
   176
void TsModel::getActivities()
hgs
parents:
diff changeset
   177
{
hgs
parents:
diff changeset
   178
    //get activities
hgs
parents:
diff changeset
   179
    TsModelItem *entry(0);
hgs
parents:
diff changeset
   180
    QList<QVariantHash> activities;
hgs
parents:
diff changeset
   181
    QMetaObject::invokeMethod(&mActivityService, "activitiesList", Q_RETURN_ARG(QList<QVariantHash>, activities));
hgs
parents:
diff changeset
   182
    foreach(QVariantHash activity, activities) {
hgs
parents:
diff changeset
   183
        prepareActivityEntry(activity);
hgs
parents:
diff changeset
   184
        entry = new TsActivityModelItem(*this, mActivityService, activity);
hgs
parents:
diff changeset
   185
        if (entry) {
hgs
parents:
diff changeset
   186
            if (maxRowCount() <= mEntries.count()) {
hgs
parents:
diff changeset
   187
                break;
hgs
parents:
diff changeset
   188
            }
hgs
parents:
diff changeset
   189
            mEntries.append(entry);
hgs
parents:
diff changeset
   190
        }
hgs
parents:
diff changeset
   191
    }
hgs
parents:
diff changeset
   192
}
hgs
parents:
diff changeset
   193
hgs
parents:
diff changeset
   194
/*!
hgs
parents:
diff changeset
   195
    Modify activity entry replacing application id with name
hgs
parents:
diff changeset
   196
*/
hgs
parents:
diff changeset
   197
void TsModel::prepareActivityEntry(QVariantHash &activity)
hgs
parents:
diff changeset
   198
{
hgs
parents:
diff changeset
   199
    if (!activity.contains(TsActivityModelItem::applicationKeyword())) {
hgs
parents:
diff changeset
   200
        activity.insert(TsActivityModelItem::applicationKeyword(),
hgs
parents:
diff changeset
   201
                    activity.contains(ActivityApplicationKeyword) ?
hgs
parents:
diff changeset
   202
                    getApplicationName(activity[ActivityApplicationKeyword].toInt()) :
hgs
parents:
diff changeset
   203
                    QString::null);
hgs
parents:
diff changeset
   204
    }
hgs
parents:
diff changeset
   205
}
hgs
parents:
diff changeset
   206
hgs
parents:
diff changeset
   207
/*!
hgs
parents:
diff changeset
   208
    Return application name
hgs
parents:
diff changeset
   209
    \param id - reqiested application identyfier
hgs
parents:
diff changeset
   210
*/
hgs
parents:
diff changeset
   211
QString TsModel::getApplicationName(int id)
hgs
parents:
diff changeset
   212
{
hgs
parents:
diff changeset
   213
    QString retVal;
hgs
parents:
diff changeset
   214
#ifdef Q_OS_SYMBIAN
hgs
parents:
diff changeset
   215
    TApaAppInfo info;
hgs
parents:
diff changeset
   216
    iAppArcSession.GetAppInfo( info, TUid::Uid(id));
hgs
parents:
diff changeset
   217
    retVal = QString::fromUtf16(info.iShortCaption.Ptr(),
hgs
parents:
diff changeset
   218
                                info.iShortCaption.Length());
hgs
parents:
diff changeset
   219
#endif
hgs
parents:
diff changeset
   220
    return retVal;
hgs
parents:
diff changeset
   221
}
hgs
parents:
diff changeset
   222
hgs
parents:
diff changeset
   223
/*!
hgs
parents:
diff changeset
   224
    Called when some item was changed
hgs
parents:
diff changeset
   225
    \param itemPtr - address of updated item
hgs
parents:
diff changeset
   226
*/
hgs
parents:
diff changeset
   227
void TsModel::entryChanged(TsModelItem *itemPtr)
hgs
parents:
diff changeset
   228
{
hgs
parents:
diff changeset
   229
    const int itemIndex = mEntries.indexOf(itemPtr);
hgs
parents:
diff changeset
   230
    if (itemIndex != -1) {
hgs
parents:
diff changeset
   231
        emit dataChanged(index(itemIndex, 0), index(itemIndex, 0));
hgs
parents:
diff changeset
   232
    }
hgs
parents:
diff changeset
   233
}
hgs
parents:
diff changeset
   234
hgs
parents:
diff changeset
   235
/*!
hgs
parents:
diff changeset
   236
    Returns an entry from model
hgs
parents:
diff changeset
   237
    \param index of entry in model
hgs
parents:
diff changeset
   238
    \retval pointer to an entry
hgs
parents:
diff changeset
   239
*/
hgs
parents:
diff changeset
   240
TsModelItem *TsModel::entry(const QModelIndex &index) const
hgs
parents:
diff changeset
   241
{
hgs
parents:
diff changeset
   242
    return mEntries.at(index.row());
hgs
parents:
diff changeset
   243
}
hgs
parents:
diff changeset
   244