|
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: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QStringList> |
|
19 #include <QtAlgorithms> |
|
20 #include <HbGlobal> |
|
21 |
|
22 #include "mobhistorymodel_p.h" |
|
23 #include "mobhistorymodel.h" |
|
24 |
|
25 // Unnamed namespace for helper functions |
|
26 namespace |
|
27 { |
|
28 bool greaterThan(const HistoryItem& t1, const HistoryItem& t2) |
|
29 { |
|
30 return t1.timeStamp > t2.timeStamp; |
|
31 } |
|
32 |
|
33 bool lessThan(const HistoryItem& t1, const HistoryItem& t2) |
|
34 { |
|
35 return t1.timeStamp < t2.timeStamp; |
|
36 } |
|
37 } |
|
38 |
|
39 Q_DECLARE_METATYPE(LogsEvent *) |
|
40 |
|
41 /*! |
|
42 * Construct a new MobHistoryModel object to communicate |
|
43 * with the conversations and logs databases. |
|
44 * |
|
45 * \param contactId History specific to this contact is cached. |
|
46 * If no contact is specified all the call logs and conversation |
|
47 * history from all contacts will be cached. |
|
48 */ |
|
49 MobHistoryModel::MobHistoryModel(QContactLocalId contactId, |
|
50 QContactManager* manager, |
|
51 QObject *parent) |
|
52 : QAbstractListModel(parent) |
|
53 { |
|
54 d = new MobHistoryModelData(contactId, manager); |
|
55 |
|
56 // Check if the contact is my card |
|
57 if (d->m_contactId == d->m_contactManager->selfContactId()) { |
|
58 d->m_isMyCard = true; |
|
59 } |
|
60 else { |
|
61 d->m_isMyCard = false; |
|
62 } |
|
63 // Create the model structure and cache history data from the databases |
|
64 initializeModel(); |
|
65 |
|
66 } |
|
67 |
|
68 MobHistoryModel::~MobHistoryModel() |
|
69 { |
|
70 } |
|
71 |
|
72 /*! |
|
73 * Return the data to be used by the view or delegates for a particular |
|
74 * item and role. |
|
75 * |
|
76 * \param index The index of the item to return data about. |
|
77 * \param role The data should be relevant to this particular purpose. |
|
78 * \return QVariant The data for the specified index and role. |
|
79 */ |
|
80 QVariant MobHistoryModel::data(const QModelIndex& index, int role) const |
|
81 { |
|
82 // Invalid index |
|
83 int row = index.row(); |
|
84 |
|
85 if ( !validateRowIndex(row) ) |
|
86 return QVariant(); |
|
87 |
|
88 HistoryItem item = d->m_table.at(row); |
|
89 |
|
90 switch( role ) |
|
91 { |
|
92 case Qt::DisplayRole: |
|
93 return displayRoleData(item); |
|
94 case Qt::DecorationRole: |
|
95 return QVariant(item.iconPath); |
|
96 case SeenStatusRole: |
|
97 return QVariant(item.seenStatus); |
|
98 case DirectionRole: |
|
99 return QVariant(item.direction); |
|
100 case ItemTypeRole: |
|
101 return QVariant(item.msgType); |
|
102 case PhoneNumberRole: |
|
103 return QVariant(item.number); |
|
104 default: |
|
105 return QVariant(); |
|
106 } |
|
107 } |
|
108 |
|
109 /*! |
|
110 * Return the data to be used by the view for a display role. |
|
111 * |
|
112 * \param column The column of the item to return data about. |
|
113 * return QVariant List of strings to be displayed on the view. |
|
114 * The stings can also be NULL |
|
115 * index 0 Title of the conversation item. |
|
116 * index 1 Body text |
|
117 * index 2 Time stamp |
|
118 */ |
|
119 QVariant MobHistoryModel::displayRoleData(const HistoryItem& item) const |
|
120 { |
|
121 QStringList list; |
|
122 |
|
123 list << item.title << item.message << item.timeStamp.toString(); |
|
124 |
|
125 return QVariant(list); |
|
126 } |
|
127 |
|
128 /*! |
|
129 * Get the number of rows (conversations) in this model. |
|
130 * |
|
131 * \param parent Optional parent index value. |
|
132 * \return Number of rows in this model. |
|
133 */ |
|
134 int MobHistoryModel::rowCount(const QModelIndex& /*parent*/) const |
|
135 { |
|
136 return d->m_table.size(); |
|
137 } |
|
138 |
|
139 /*! |
|
140 * Sort list items on the model. |
|
141 * |
|
142 * \param column Column to be sorted. It is not used. |
|
143 * \param order Order to sort the list items. |
|
144 */ |
|
145 void MobHistoryModel::sort(int /*column*/, Qt::SortOrder order) |
|
146 { |
|
147 if ( order == Qt::AscendingOrder ) |
|
148 qStableSort(d->m_table.begin(), d->m_table.end(), lessThan); |
|
149 else |
|
150 qStableSort(d->m_table.begin(), d->m_table.end(), greaterThan); |
|
151 } |
|
152 |
|
153 /*! |
|
154 * Clear history from the database. If the history cached |
|
155 * is specific to one contact, only that history is cleared. |
|
156 * |
|
157 */ |
|
158 void MobHistoryModel::clearHistory() |
|
159 { |
|
160 if ( d->m_table.isEmpty() ) |
|
161 return; |
|
162 |
|
163 // Call logs |
|
164 if ( !d->m_isMyCard && d->m_logsFilter ) { |
|
165 if ( !d->m_logsFilter->clearEvents() ) { |
|
166 // Operation not async |
|
167 int bottom = rowCount(); |
|
168 d->m_table.clear(); |
|
169 beginRemoveRows(QModelIndex(), rowCount(), bottom); |
|
170 endRemoveRows(); |
|
171 } |
|
172 } else if ( d->m_logsModel ) { |
|
173 if ( !d->m_logsModel->clearList(LogsModel::TypeLogsClearAll) ) { |
|
174 // Operation not async |
|
175 int bottom = rowCount(); |
|
176 d->m_table.clear(); |
|
177 beginRemoveRows(QModelIndex(), rowCount(), bottom); |
|
178 endRemoveRows(); |
|
179 } |
|
180 } |
|
181 } |
|
182 |
|
183 /*! |
|
184 * Mark all the conversations in the view as seen. |
|
185 * |
|
186 */ |
|
187 void MobHistoryModel::markAllAsSeen() |
|
188 { |
|
189 if ( d->m_isMarkedAsSeen ) |
|
190 return; |
|
191 |
|
192 // Call logs |
|
193 if ( !d->m_isMyCard && d->m_logsFilter ) { |
|
194 d->m_logsFilter->markEventsSeen(); |
|
195 } else if ( d->m_logsModel ) { |
|
196 d->m_logsModel->markEventsSeen(LogsModel::TypeLogsClearMissed); |
|
197 } |
|
198 d->m_isMarkedAsSeen = true; |
|
199 } |
|
200 |
|
201 /*! |
|
202 * Sort items in the model and refresh the view |
|
203 * |
|
204 */ |
|
205 void MobHistoryModel::sortAndRefresh(Qt::SortOrder order) |
|
206 { |
|
207 sort(0, order); |
|
208 emit layoutChanged(); |
|
209 } |
|
210 |
|
211 /*! |
|
212 * Create the model structure and cache history data from |
|
213 * conversations and call logs databases. |
|
214 * |
|
215 */ |
|
216 void MobHistoryModel::initializeModel() |
|
217 { |
|
218 initializeLogsModel(); |
|
219 } |
|
220 |
|
221 void MobHistoryModel::initializeLogsModel() |
|
222 { |
|
223 //populate model with call events |
|
224 d->m_logsModel = new LogsModel(LogsModel::LogsFullModel); |
|
225 if (!d->m_isMyCard) { |
|
226 //do the filtering to get call events for the target contact |
|
227 d->m_logsFilter = new LogsCustomFilter; |
|
228 d->m_logsFilter->setContactId(d->m_contactId); |
|
229 d->m_logsFilter->setSourceModel(d->m_logsModel); |
|
230 d->m_AbstractLogsModel = d->m_logsFilter; |
|
231 |
|
232 connect(d->m_logsFilter, SIGNAL(clearingCompleted(int)), |
|
233 this, SLOT(clearedCallLogs(int))); |
|
234 connect(d->m_logsFilter, SIGNAL(markingCompleted(int)), |
|
235 this, SLOT(markingCompleted(int))); |
|
236 } else { |
|
237 //get all call events |
|
238 d->m_AbstractLogsModel = d->m_logsModel; |
|
239 |
|
240 connect(d->m_logsModel, SIGNAL(clearingCompleted(int)), |
|
241 this, SLOT(clearedCallLogs(int))); |
|
242 connect(d->m_logsModel, SIGNAL(markingCompleted(int)), |
|
243 this, SLOT(markingCompleted(int))); |
|
244 } |
|
245 |
|
246 //read first call events and start listening for more |
|
247 for ( int i = 0; i < d->m_AbstractLogsModel->rowCount(); ++i ) { |
|
248 LogsEvent* event = qVariantValue<LogsEvent*>( |
|
249 d->m_AbstractLogsModel->data(d->m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) ); |
|
250 |
|
251 if ( event ) { |
|
252 HistoryItem item; |
|
253 readLogEvent(event, item); |
|
254 d->m_LogsMap.insert(i, d->m_table.size()); |
|
255 d->m_table.append( item ); |
|
256 } |
|
257 } |
|
258 |
|
259 connect(d->m_AbstractLogsModel, SIGNAL(rowsInserted(const QModelIndex &, int, int)), |
|
260 this, SLOT(logsRowsInserted(const QModelIndex &, int, int))); |
|
261 connect(d->m_AbstractLogsModel, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), |
|
262 this, SLOT(logsRowsRemoved(const QModelIndex &, int, int))); |
|
263 connect(d->m_AbstractLogsModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), |
|
264 this, SLOT(logsDataChanged(const QModelIndex &, const QModelIndex &))); |
|
265 |
|
266 } |
|
267 |
|
268 /*! |
|
269 * Read call event into a history item |
|
270 * |
|
271 * \param event Call log event |
|
272 * \param item Conversation history item |
|
273 */ |
|
274 void MobHistoryModel::readLogEvent(LogsEvent* event, HistoryItem& item) |
|
275 { |
|
276 QString bodyText; |
|
277 QString icon; |
|
278 QString title; |
|
279 |
|
280 if ( d->m_isMyCard ) { |
|
281 if ( event->remoteParty().length() > 0 ) { |
|
282 title = event->remoteParty(); |
|
283 } |
|
284 else { |
|
285 title = event->number(); |
|
286 } |
|
287 } else { |
|
288 if ( event->direction() == LogsEvent::DirIn ) { |
|
289 bodyText = QString("Received call"); |
|
290 icon = QString("qtg_small_received"); |
|
291 } else if ( event->direction() == LogsEvent::DirOut ) { |
|
292 bodyText = QString(hbTrId("txt_phob_list_dialled_call")); |
|
293 icon = QString("qtg_small_sent"); |
|
294 } else if ( event->direction() == LogsEvent::DirMissed ) { |
|
295 bodyText = QString(hbTrId("txt_phob_list_missed_call")); |
|
296 icon = QString("qtg_small_missed_call"); |
|
297 if ( !event->isRead() ) |
|
298 item.seenStatus = Unseen; |
|
299 } |
|
300 } |
|
301 |
|
302 if ( event->direction() == LogsEvent::DirOut ) |
|
303 item.direction = Outgoing; |
|
304 else |
|
305 item.direction = Incoming; |
|
306 |
|
307 item.message = bodyText; |
|
308 item.iconPath = icon; |
|
309 item.title = title; |
|
310 item.timeStamp = event->time(); |
|
311 item.msgType = CallLog; |
|
312 item.number = event->number(); |
|
313 } |
|
314 |
|
315 /*! |
|
316 * Slot used for receiving new rows from the LogsModel. |
|
317 * |
|
318 * \param parent Optional parent index value. |
|
319 * \param first The first row item to be received from the model. |
|
320 * \param last The last row item to be received from the model. |
|
321 */ |
|
322 void MobHistoryModel::logsRowsInserted(const QModelIndex& /*parent*/, int first, int last) |
|
323 { |
|
324 int oldRowCount = rowCount(); |
|
325 |
|
326 for ( int i = first; i < d->m_AbstractLogsModel->rowCount() && i <= last; ++i ) { |
|
327 LogsEvent* event = qVariantValue<LogsEvent*>( |
|
328 d->m_AbstractLogsModel->data(d->m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) ); |
|
329 |
|
330 if ( event ) { |
|
331 HistoryItem item; |
|
332 readLogEvent(event, item); |
|
333 d->m_LogsMap.insert(i, d->m_table.size()); |
|
334 d->m_table.append( item ); |
|
335 } |
|
336 } |
|
337 |
|
338 sort(); |
|
339 beginInsertRows(QModelIndex(), oldRowCount, rowCount()); |
|
340 endInsertRows(); |
|
341 } |
|
342 |
|
343 /*! |
|
344 * Slot used for receiving new rows from the LogsModel. |
|
345 * |
|
346 * \param parent Optional parent index value. |
|
347 * \param first The first row item to be received from the model. |
|
348 * \param last The last row item to be received from the model. |
|
349 */ |
|
350 void MobHistoryModel::logsRowsRemoved(const QModelIndex& /*parent*/, int first, int last) |
|
351 { |
|
352 QList< int > indices; |
|
353 |
|
354 for ( int i = first; i <= last; ++i ) { |
|
355 int index = d->m_LogsMap.value(i); |
|
356 d->m_table.removeAt(index); |
|
357 d->m_LogsMap.remove(i); |
|
358 indices.append(index); |
|
359 } |
|
360 |
|
361 // Remove list items in batches |
|
362 if ( !indices.isEmpty() ) { |
|
363 QList< QList<int> > batches = findIndices(indices); |
|
364 foreach( QList<int> l, batches ) { |
|
365 beginRemoveRows(QModelIndex(), l.first(), l.last()); |
|
366 endRemoveRows(); |
|
367 } |
|
368 } |
|
369 } |
|
370 |
|
371 /*! |
|
372 * Update events from logs model. Events are |
|
373 * received as a batch |
|
374 * |
|
375 * \param first First updated model index |
|
376 * \param last Last updated model index |
|
377 */ |
|
378 void MobHistoryModel::logsDataChanged(const QModelIndex& first, const QModelIndex& last) |
|
379 { |
|
380 int f = first.row(); |
|
381 int l = last.row(); |
|
382 QList< int > indices; |
|
383 |
|
384 for ( int i = f; i < d->m_AbstractLogsModel->rowCount() && i <= l; ++i ) { |
|
385 |
|
386 LogsEvent* event = qVariantValue<LogsEvent*>( |
|
387 d->m_AbstractLogsModel->data(d->m_AbstractLogsModel->index(i, 0), LogsModel::RoleFullEvent) ); |
|
388 |
|
389 // Fetch index from the mapped logs model indices |
|
390 int index = d->m_LogsMap.value(i); |
|
391 |
|
392 if ( event && validateRowIndex(index) ) { |
|
393 HistoryItem item; |
|
394 readLogEvent(event, item); |
|
395 d->m_table.replace( index, item ); |
|
396 indices.append(index); |
|
397 } |
|
398 } |
|
399 |
|
400 // Emit dataChanged signal only if there were updates |
|
401 if ( !indices.isEmpty() ) { |
|
402 QList< QList<int> > batches = findIndices(indices); |
|
403 foreach( QList<int> l, batches ) |
|
404 emit dataChanged(index(l.first(), 0), index(l.last(), 0)); |
|
405 } |
|
406 } |
|
407 |
|
408 /*! |
|
409 * Clear logs event slot received from logs model |
|
410 * |
|
411 * \param err Error of the clear logs request |
|
412 */ |
|
413 void MobHistoryModel::clearedCallLogs(int err) |
|
414 { |
|
415 if ( err == 0 ) { |
|
416 int bottom = rowCount(); |
|
417 d->m_table.clear(); |
|
418 beginRemoveRows(QModelIndex(), rowCount(), bottom); |
|
419 endRemoveRows(); |
|
420 } |
|
421 } |
|
422 |
|
423 /*! |
|
424 * Mark events as seen slot received from logs model |
|
425 * |
|
426 * \param err Error of the marking logs request |
|
427 */ |
|
428 void MobHistoryModel::markingCompleted(int err) |
|
429 { |
|
430 if ( err == 0 ) { |
|
431 d->m_isMarkedAsSeen = true; |
|
432 } |
|
433 } |
|
434 |
|
435 /*! |
|
436 * Check whether an idex is out of bound of our list |
|
437 * |
|
438 * \param index Index to be validated |
|
439 */ |
|
440 |
|
441 bool MobHistoryModel::validateRowIndex( const int index) const |
|
442 { |
|
443 return( index < rowCount() && index >= 0 ); |
|
444 } |
|
445 |
|
446 /*! |
|
447 * Find the sequences of indices for the given indices from the private list |
|
448 * |
|
449 * \param indices List of indices |
|
450 * \return sequences of indices |
|
451 */ |
|
452 QList< QList<int> > MobHistoryModel::findIndices( const QList< int >& indices ) |
|
453 { |
|
454 QList< QList<int> > sequences; |
|
455 QList<int> currSequence; |
|
456 int prevIndex = indices.at(0) - 1; |
|
457 |
|
458 for (int i = 0; i < indices.count(); i++) |
|
459 { |
|
460 int currIndex = indices.at(i); |
|
461 |
|
462 if (currIndex >= 0) |
|
463 { |
|
464 if (prevIndex+1 != currIndex) |
|
465 { |
|
466 sequences.append(currSequence); |
|
467 currSequence.clear(); |
|
468 } |
|
469 currSequence.append(currIndex); |
|
470 prevIndex = currIndex; |
|
471 } |
|
472 } |
|
473 |
|
474 if (!currSequence.isEmpty()) |
|
475 { |
|
476 // Add last sequence if such exist |
|
477 sequences.append(currSequence); |
|
478 } |
|
479 |
|
480 return sequences; |
|
481 } |
|
482 |