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 #include "tsmodelitem.h" |
|
19 #include "tsentrymodelitem.h" |
|
20 #include "tsactivitymodelitem.h" |
|
21 #include "tsdataroles.h" |
|
22 |
|
23 #include <HbIcon> |
|
24 #include <qvariant.h> |
|
25 #include <qlist.h> |
|
26 #include <cadefs.h> |
|
27 |
|
28 #include <canotifier.h> |
|
29 #include <canotifierfilter.h> |
|
30 #include <hsactivitydbclientinterface.h> |
|
31 |
|
32 #ifdef Q_OS_SYMBIAN |
|
33 #include <XQSettingsManager> |
|
34 |
|
35 const int TSDeviceDialogUid = 0x2002677F; |
|
36 const int ItemsLimit = 0x00000001; |
|
37 #endif |
|
38 |
|
39 const char entryTypeKeyword[] = "application"; |
|
40 const char applicationUidKeyword [] = "application:uid"; |
|
41 const int maxItems(10); |
|
42 /*! |
|
43 \class TsModel |
|
44 \ingroup group_tsdevicedialogplugin |
|
45 \brief Model storing running application and activieties. |
|
46 */ |
|
47 |
|
48 /*! |
|
49 Constructor |
|
50 \param query used to create model |
|
51 \param pointer to parent object |
|
52 */ |
|
53 TsModel::TsModel(CaService &applicationSrv, QObject &activitySrv, QObject *parent) : |
|
54 QAbstractListModel(parent), |
|
55 mEntries(), |
|
56 mApplicationService(applicationSrv), |
|
57 mActivityService(activitySrv), |
|
58 mNotifier(NULL), |
|
59 mSize(240, 240), |
|
60 mMaxItems(maxItems) |
|
61 { |
|
62 // creating query |
|
63 mQuery.setEntryRoles(ItemEntryRole); |
|
64 mQuery.addEntryTypeName(entryTypeKeyword); |
|
65 mQuery.setFlagsOn(VisibleEntryFlag); |
|
66 mQuery.setFlagsOff(MissingEntryFlag); |
|
67 mQuery.setFlagsOn(UsedEntryFlag); |
|
68 mQuery.setSort(LastUsedSortAttribute, Qt::DescendingOrder); |
|
69 // requesting new data |
|
70 updateModel(); |
|
71 // creating notyfication filter |
|
72 CaNotifierFilter filter(mQuery); |
|
73 mNotifier = mApplicationService.createNotifier(filter); |
|
74 connect(mNotifier, SIGNAL(entryChanged(CaEntry,ChangeType)), this, SLOT(entryChanged(CaEntry, ChangeType))); |
|
75 connect(mNotifier, SIGNAL(entryTouched(int)), this, SLOT(updateModel())); |
|
76 if (mQuery.parentId() > 0) { |
|
77 connect(mNotifier, SIGNAL(groupContentChanged(int)), this, SLOT(updateModel())); |
|
78 } |
|
79 |
|
80 #ifdef Q_OS_SYMBIAN |
|
81 XQSettingsManager *crManager = new XQSettingsManager; |
|
82 XQCentralRepositorySettingsKey itemsNumberKey(TSDeviceDialogUid, ItemsLimit); |
|
83 QVariant itemsNumberVariant = crManager->readItemValue(itemsNumberKey, XQSettingsManager::TypeInt); |
|
84 if (!itemsNumberVariant.isNull()) { |
|
85 int number = itemsNumberVariant.toInt(); |
|
86 if (number > 0) { |
|
87 mMaxItems = number; |
|
88 } |
|
89 } |
|
90 #endif |
|
91 } |
|
92 |
|
93 /*! |
|
94 Destructor |
|
95 */ |
|
96 TsModel::~TsModel() |
|
97 { |
|
98 qDeleteAll(mEntries); |
|
99 delete mNotifier; |
|
100 } |
|
101 |
|
102 /*! |
|
103 Returns count of rows in model |
|
104 \retval number of rows |
|
105 */ |
|
106 int TsModel::rowCount( |
|
107 const QModelIndex &parent) const |
|
108 { |
|
109 Q_UNUSED(parent); |
|
110 return mEntries.count(); |
|
111 } |
|
112 |
|
113 /*! |
|
114 Returns appropiate model's data |
|
115 \param index model index |
|
116 \param role which data role to return |
|
117 \retval models data |
|
118 */ |
|
119 QVariant TsModel::data(const QModelIndex &index, |
|
120 int role) const |
|
121 { |
|
122 return index.isValid() ? entry(index)->data(role) : QVariant(); |
|
123 } |
|
124 |
|
125 /*! |
|
126 Returns maximum anount of data allowed for model |
|
127 \retval maximum data count |
|
128 */ |
|
129 |
|
130 int TsModel::maxRowCount()const |
|
131 { |
|
132 return mMaxItems; |
|
133 } |
|
134 |
|
135 /*! |
|
136 Activate one of model entries |
|
137 */ |
|
138 void TsModel::openApplication(const QModelIndex &index) |
|
139 { |
|
140 if (!index.isValid()) { |
|
141 return; |
|
142 } |
|
143 entry(index)->open(); |
|
144 } |
|
145 |
|
146 /*! |
|
147 Close one of moder entries |
|
148 */ |
|
149 void TsModel::closeApplication(const QModelIndex &index) |
|
150 { |
|
151 if (!index.isValid() || !entry(index)->data(TsDataRoles::Closable).toBool()) { |
|
152 return; |
|
153 } |
|
154 entry(index)->close(); |
|
155 } |
|
156 |
|
157 /*! |
|
158 Updates model with fresh entries |
|
159 */ |
|
160 void TsModel::updateModel() |
|
161 { |
|
162 //clear current data |
|
163 qDeleteAll(mEntries); |
|
164 mEntries.clear(); |
|
165 |
|
166 beginResetModel(); |
|
167 getApplications(); |
|
168 getActivities(); |
|
169 endResetModel(); |
|
170 |
|
171 } |
|
172 |
|
173 /*! |
|
174 Read list of running applications |
|
175 */ |
|
176 void TsModel::getApplications() |
|
177 { |
|
178 //get running applications |
|
179 TsModelItem *entry(0); |
|
180 QList< QSharedPointer<CaEntry> > applications(mApplicationService.getEntries(mQuery)); |
|
181 foreach(QSharedPointer<CaEntry> application, applications) { |
|
182 //CA entry ownership is transfered to model item |
|
183 entry = new TsEntryModelItem(mApplicationService, application, mSize); |
|
184 if (entry) { |
|
185 //add running application filtering |
|
186 if (entry->data(TsDataRoles::Closable).toBool() && //running application filtering |
|
187 entry->data(TsDataRoles::Visible).toBool()) { //visible applications filtering |
|
188 mEntries.append(entry); |
|
189 } else { |
|
190 delete entry; |
|
191 } |
|
192 } |
|
193 } |
|
194 } |
|
195 |
|
196 /*! |
|
197 Read current activities |
|
198 */ |
|
199 void TsModel::getActivities() |
|
200 { |
|
201 //get activities |
|
202 TsModelItem *entry(0); |
|
203 QList<QVariantHash> activities; |
|
204 QMetaObject::invokeMethod(&mActivityService, "activitiesList", Q_RETURN_ARG(QList<QVariantHash>, activities)); |
|
205 foreach(QVariantHash activity, activities) { |
|
206 prepareActivityEntry(activity); |
|
207 entry = new TsActivityModelItem(*this, mActivityService, activity); |
|
208 if (entry) { |
|
209 if (maxRowCount() <= mEntries.count()) { |
|
210 break; |
|
211 } |
|
212 if (entry->data(TsDataRoles::Visible).toBool()) { //visible activity filtering |
|
213 mEntries.append(entry); |
|
214 } else { |
|
215 delete entry; |
|
216 } |
|
217 } |
|
218 } |
|
219 } |
|
220 |
|
221 /*! |
|
222 Modify activity entry replacing application id with name |
|
223 */ |
|
224 void TsModel::prepareActivityEntry(QVariantHash &activity) |
|
225 { |
|
226 activity.insert(TsActivityModelItem::applicationKeyword(), |
|
227 activity.find(ActivityActivityKeyword) == activity.end() ? |
|
228 QString::null : |
|
229 getApplicationName(activity[ActivityApplicationKeyword].toInt())); |
|
230 } |
|
231 |
|
232 /*! |
|
233 Return application name |
|
234 \param id - reqiested application identyfier |
|
235 */ |
|
236 QString TsModel::getApplicationName(int id) |
|
237 { |
|
238 CaQuery query; |
|
239 QString retVal; |
|
240 query.setEntryRoles(ItemEntryRole); |
|
241 query.addEntryTypeName(entryTypeKeyword); |
|
242 query.setAttribute(applicationUidKeyword, QString::number(id)); |
|
243 QList< QSharedPointer<CaEntry> > applications(mApplicationService.getEntries(query)); |
|
244 if (applications.begin() != applications.end()) { |
|
245 retVal = (*applications.begin())->text(); |
|
246 } |
|
247 return retVal; |
|
248 } |
|
249 |
|
250 /*! |
|
251 Called when some entry was changed |
|
252 \param updatedEntry entry that was changed |
|
253 \param change change type |
|
254 */ |
|
255 void TsModel::entryChanged(CaEntry updatedEntry, |
|
256 ChangeType change) |
|
257 { |
|
258 Q_UNUSED(updatedEntry); |
|
259 Q_UNUSED(change) |
|
260 updateModel(); |
|
261 } |
|
262 |
|
263 /*! |
|
264 Called when some item was changed |
|
265 \param itemPtr - address of updated item |
|
266 */ |
|
267 void TsModel::entryChanged(TsModelItem *itemPtr) |
|
268 { |
|
269 QList<TsModelItem *>::const_iterator iter(mEntries.constBegin()); |
|
270 for (int offset(0); iter != mEntries.constEnd(); ++iter, ++offset) { |
|
271 if ((*iter) == itemPtr) { |
|
272 emit dataChanged(index(offset, 0), index(offset, 0)); |
|
273 break; |
|
274 } |
|
275 } |
|
276 } |
|
277 |
|
278 /*! |
|
279 Returns an entry from model |
|
280 \param index of entry in model |
|
281 \retval pointer to an entry |
|
282 */ |
|
283 TsModelItem *TsModel::entry(const QModelIndex &index) const |
|
284 { |
|
285 return mEntries.at(index.row()); |
|
286 } |
|
287 |
|