68
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 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 <hbnamespace.h>
|
|
19 |
#include <nmmessageenvelope.h>
|
|
20 |
#include "nmhswidgetlistmodel.h"
|
|
21 |
#include "nmhswidgetlistmodelitem.h"
|
|
22 |
|
|
23 |
/*!
|
|
24 |
\class NmHsWidgetListModel
|
|
25 |
\brief The NmHsWidgetListModel class represents data model for message list.
|
|
26 |
@alpha
|
|
27 |
|
|
28 |
The NmHsWidgetListModel class uses NmHsWidgetListModelItem class to represent data returned in its'
|
|
29 |
data method to get all information needed for one list row for a widget by calling the method
|
|
30 |
once.
|
|
31 |
*/
|
|
32 |
|
|
33 |
/*!
|
|
34 |
Constructor
|
|
35 |
*/
|
|
36 |
NmHsWidgetListModel::NmHsWidgetListModel(QObject *parent)
|
|
37 |
:QStandardItemModel(parent)
|
|
38 |
{
|
|
39 |
NM_FUNCTION;
|
|
40 |
}
|
|
41 |
|
|
42 |
/*!
|
|
43 |
Destructor
|
|
44 |
*/
|
|
45 |
NmHsWidgetListModel::~NmHsWidgetListModel()
|
|
46 |
{
|
|
47 |
NM_FUNCTION;
|
|
48 |
|
|
49 |
clear();
|
|
50 |
}
|
|
51 |
|
|
52 |
/*!
|
|
53 |
Returns data specified by \a index. Only Qt::DisplayRole is supported in \a role.
|
|
54 |
The refresh method must have been called before this method can return any real data.
|
|
55 |
*/
|
|
56 |
QVariant NmHsWidgetListModel::data(const QModelIndex &index, int role) const
|
|
57 |
{
|
|
58 |
NM_FUNCTION;
|
|
59 |
|
|
60 |
QVariant qVariant;
|
|
61 |
if (index.isValid() && Qt::DisplayRole == role) {
|
|
62 |
NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
|
|
63 |
NmMessageEnvelope *mailbox = item->itemMetaData();
|
|
64 |
qVariant = QVariant::fromValue(mailbox);
|
|
65 |
}
|
|
66 |
return qVariant;
|
|
67 |
}
|
|
68 |
|
|
69 |
|
|
70 |
/*!
|
|
71 |
Create mailbox item
|
|
72 |
\param mailbox
|
|
73 |
*/
|
|
74 |
NmHsWidgetListModelItem* NmHsWidgetListModel::createMessageListModelItem(const NmMessageEnvelope* envelope)
|
|
75 |
{
|
|
76 |
NM_FUNCTION;
|
|
77 |
|
|
78 |
NmMessageEnvelope *newMeta = new NmMessageEnvelope(*envelope);
|
|
79 |
NmHsWidgetListModelItem *item = new NmHsWidgetListModelItem();
|
|
80 |
item->setItemMetaData(newMeta);
|
|
81 |
item->setData(Hb::StandardItem, Hb::ItemTypeRole);
|
|
82 |
return item;
|
|
83 |
}
|
76
|
84 |
|
|
85 |
|
|
86 |
/*!
|
|
87 |
Function determines model index in which the new message should be inserted.
|
|
88 |
*/
|
|
89 |
int NmHsWidgetListModel::getInsertionIndex(
|
|
90 |
const NmMessageEnvelope &envelope) const
|
|
91 |
{
|
|
92 |
NM_FUNCTION;
|
|
93 |
|
|
94 |
int ret(NmNotFoundError);
|
|
95 |
|
|
96 |
// Date+descending sort mode based comparison.
|
|
97 |
// Go through model and compare sent times with QDateTime >= comparison operation.
|
|
98 |
QList<QStandardItem*> items = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
|
|
99 |
int count(items.count());
|
|
100 |
bool found(false);
|
|
101 |
int i(0);
|
|
102 |
while (i < count && !found) {
|
|
103 |
QModelIndex index = items[i]->index();
|
|
104 |
NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
|
|
105 |
found = envelope.sentTime() >= item->itemMetaData()->sentTime();
|
|
106 |
if (found) {
|
|
107 |
ret = i;
|
|
108 |
}
|
|
109 |
i++;
|
|
110 |
}
|
|
111 |
if (0 == count) {
|
|
112 |
ret = NmNoError;
|
|
113 |
}
|
|
114 |
items.clear();
|
|
115 |
return ret;
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
/*!
|
|
120 |
Inserts given messages into the model
|
|
121 |
*/
|
|
122 |
void NmHsWidgetListModel::addMessages(const QList<NmMessageEnvelope*> &messageEnvs)
|
|
123 |
{
|
|
124 |
NM_FUNCTION;
|
|
125 |
int orig_count = rowCount();
|
|
126 |
foreach(NmMessageEnvelope* env, messageEnvs){
|
|
127 |
int insertionIndex = getInsertionIndex(*env);
|
|
128 |
NmHsWidgetListModelItem *newItem = createMessageListModelItem(env);
|
|
129 |
insertRow(insertionIndex, newItem);
|
|
130 |
}
|
|
131 |
|
|
132 |
if( !messageEnvs.isEmpty() ){
|
|
133 |
if (orig_count == 0){
|
|
134 |
emit modelIsEmpty(false);
|
|
135 |
}
|
|
136 |
emit messagesAddedToModel(); //emit messages added to model
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
/*!
|
|
141 |
This refreshes the data of the model.
|
|
142 |
NOTE: safe guard any call to this function with try-catch.
|
|
143 |
*/
|
|
144 |
void NmHsWidgetListModel::refresh(const QList<NmMessageEnvelope*> &envelopeList)
|
|
145 |
{
|
|
146 |
NM_FUNCTION;
|
|
147 |
|
|
148 |
clear();
|
|
149 |
foreach(NmMessageEnvelope *env, envelopeList){
|
|
150 |
NmHsWidgetListModelItem *item = createMessageListModelItem(env);
|
|
151 |
appendRow(item);
|
|
152 |
}
|
|
153 |
|
|
154 |
//As we refresh all data, emit signal in any case
|
|
155 |
if( rowCount() == 0 ){
|
|
156 |
emit modelIsEmpty(true);
|
|
157 |
}else{
|
|
158 |
emit modelIsEmpty(false);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
/*!
|
|
163 |
Updates existing messages in model with given envelopes
|
|
164 |
*/
|
|
165 |
void NmHsWidgetListModel::updateMessages(const QList<NmMessageEnvelope*> &messageEnvs)
|
|
166 |
{
|
|
167 |
NM_FUNCTION;
|
|
168 |
QList<QStandardItem*> modelItemList = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
|
|
169 |
foreach(NmMessageEnvelope *env, messageEnvs){
|
|
170 |
foreach (QStandardItem *it, modelItemList){
|
|
171 |
QModelIndex index = it->index();
|
|
172 |
NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
|
|
173 |
if (env->messageId() == item->itemMetaData()->messageId()) {
|
|
174 |
item->setItemMetaData(env);
|
|
175 |
break;
|
|
176 |
}
|
|
177 |
}
|
|
178 |
}
|
|
179 |
}
|
|
180 |
|
|
181 |
|
|
182 |
/*!
|
|
183 |
Removes the given messages from the model
|
|
184 |
*/
|
|
185 |
void NmHsWidgetListModel::removeMessages(const QList<NmId> &messageIds)
|
|
186 |
{
|
|
187 |
NM_FUNCTION;
|
|
188 |
foreach(NmId msgId, messageIds){
|
|
189 |
//lets refresh the item list every time, so that it really is up to date after each iteration
|
|
190 |
//(and the count stays valid!)
|
|
191 |
QList<QStandardItem*> modelItemList = findItems("*", Qt::MatchWildcard | Qt::MatchRecursive);
|
|
192 |
foreach(QStandardItem *it, modelItemList){
|
|
193 |
QModelIndex index = it->index();
|
|
194 |
NmHsWidgetListModelItem *item = static_cast<NmHsWidgetListModelItem*>(itemFromIndex(index));
|
|
195 |
if (msgId == item->itemMetaData()->messageId()) {
|
|
196 |
removeRow(index.row());
|
|
197 |
break;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
//if model is empty after removing messages, signal it.
|
|
203 |
if( rowCount() == 0 ){
|
|
204 |
emit modelIsEmpty(true);
|
|
205 |
}
|
|
206 |
|
|
207 |
}
|
|
208 |
|
|
209 |
/*!
|
|
210 |
Clears the model and emits modelIsEmpty signal
|
|
211 |
*/
|
|
212 |
void NmHsWidgetListModel::removeAllMessages()
|
|
213 |
{
|
|
214 |
clear();
|
|
215 |
emit modelIsEmpty(true);
|
|
216 |
}
|