24 #include "nmcommon.h" |
24 #include "nmcommon.h" |
25 #include "nmmessageenvelope.h" |
25 #include "nmmessageenvelope.h" |
26 #include "nmhswidgetemailengine.h" |
26 #include "nmhswidgetemailengine.h" |
27 #include "nmdataplugininterface.h" |
27 #include "nmdataplugininterface.h" |
28 #include "nmmailbox.h" |
28 #include "nmmailbox.h" |
|
29 #include "nmmessage.h" |
29 #include "nmfolder.h" |
30 #include "nmfolder.h" |
30 #include "nmdatapluginfactory.h" |
31 #include "nmdatapluginfactory.h" |
31 #include "nmhswidgetconsts.h" |
32 #include "nmhswidgetconsts.h" |
32 #include "emailtrace.h" |
33 #include "emailtrace.h" |
|
34 #include "nmhswidgetlistmodel.h" |
33 |
35 |
34 /*! |
36 /*! |
35 Constructor |
37 Constructor |
36 */ |
38 */ |
37 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) : |
39 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) : |
138 SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) )); |
136 SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) )); |
139 |
137 |
140 return true; |
138 return true; |
141 } |
139 } |
142 |
140 |
143 /*! |
|
144 Reset envelope list |
|
145 \post mEnvelopeList.isEmpty() == true && all contained objects are deleted |
|
146 */ |
|
147 void NmHsWidgetEmailEngine::resetEnvelopeList() |
|
148 { |
|
149 NM_FUNCTION; |
|
150 |
|
151 while (!mEnvelopeList.isEmpty()) { |
|
152 delete mEnvelopeList.takeFirst(); |
|
153 } |
|
154 } |
|
155 |
141 |
156 /*! |
142 /*! |
157 Destructor |
143 Destructor |
158 */ |
144 */ |
159 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine() |
145 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine() |
160 { |
146 { |
161 NM_FUNCTION; |
147 NM_FUNCTION; |
162 |
148 |
163 resetEnvelopeList(); |
|
164 if (mFactory) { |
149 if (mFactory) { |
165 NmDataPluginFactory::releaseInstance(mFactory); |
150 NmDataPluginFactory::releaseInstance(mFactory); |
166 } |
151 } |
167 |
152 |
168 if (mUpdateTimer){ |
153 } |
169 mUpdateTimer->stop(); |
154 |
170 delete mUpdateTimer; |
|
171 } |
|
172 } |
|
173 |
|
174 /*! |
|
175 getEnvelopes() provides message envelopes as a list of stack objects |
|
176 Amount of message envelopes in the list parameter is the smallest of the following factors: |
|
177 'KMaxNumberOfEnvelopesProvided', 'maxEnvelopeAmount', 'amount of available envelopes'. |
|
178 |
|
179 \param list list to be filled with message envelopes |
|
180 \param maxEnvelopeAmount Client side limit for amount of message envelope count. |
|
181 \return count of envelopes added to list |
|
182 */ |
|
183 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope*> &list, int maxEnvelopeAmount) |
|
184 { |
|
185 NM_FUNCTION; |
|
186 list.clear(); //Reset the parameter list to avoid side effects |
|
187 list.append(mEnvelopeList.mid(0, maxEnvelopeAmount)); |
|
188 return list.count(); |
|
189 } |
|
190 |
155 |
191 /*! |
156 /*! |
192 UnreadCount |
157 UnreadCount |
193 |
158 |
194 \return count of unread mails, or -1 if there are no mails at all in the mailbox. |
159 \return count of unread mails, or -1 if there are no mails at all in the mailbox. |
208 { |
173 { |
209 NM_FUNCTION; |
174 NM_FUNCTION; |
210 return mAccountName; |
175 return mAccountName; |
211 } |
176 } |
212 |
177 |
213 /*! |
178 |
214 Refresh email data. |
179 bool NmHsWidgetEmailEngine::updateUnreadCount() |
215 \post mEnvelopeList is refreshed with valid content so that it has |
180 { |
216 valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes. |
|
217 |
|
218 - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs. |
|
219 - emits mailDataChanged() if new mail data is set into mEnvelopeList |
|
220 - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated |
|
221 |
|
222 \return true if everything succeeded, otherwise false |
|
223 */ |
|
224 bool NmHsWidgetEmailEngine::updateData() |
|
225 { |
|
226 NM_FUNCTION; |
|
227 if (!mEmailInterface) { |
|
228 NM_ERROR(1,"NmHsWidgetEmailEngine::updateData() -- Interface missing"); |
|
229 emit exceptionOccured(NmEngineExcFailure); //fatal error |
|
230 return false; //if interface is missing there's nothing to do |
|
231 } |
|
232 |
|
233 //reset envelope list before retrieving new items |
|
234 resetEnvelopeList(); |
|
235 |
|
236 //get messages from inbox |
|
237 int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, mEnvelopeList, |
|
238 KMaxNumberOfEnvelopesProvided); |
|
239 if (msgErr) { |
|
240 //retrieval of messages failed. |
|
241 return false; |
|
242 } |
|
243 //emit signal about new message data right away |
|
244 emit mailDataChanged(); |
|
245 //retrieve new unread count to mUnreadCount |
181 //retrieve new unread count to mUnreadCount |
246 NmFolder* folder = NULL; |
182 NmFolder* folder = NULL; |
247 int folderErr = mEmailInterface->getFolderById(mMailboxId, mFolderId, folder); |
183 int folderErr = mEmailInterface->getFolderById(mMailboxId, mFolderId, folder); |
248 if (folderErr) { |
184 if (folderErr) { |
249 //retrieval of valid folder failed. |
185 //retrieval of valid folder failed. |
250 return false; |
186 return false; |
251 } |
187 } |
252 if (folder) { |
188 if (folder) { |
253 //If messageCount in the folder is zero we must indicate unread count to be -1 |
189 //If messageCount in the folder is zero we must indicate unread count to be -1 |
254 if (mEnvelopeList.count() == 0) { |
190 if (folder->messageCount() == 0) { |
255 mUnreadCount = -1; |
191 mUnreadCount = -1; |
256 } |
192 } |
257 else { |
193 else { |
258 mUnreadCount = folder->unreadMessageCount(); |
194 mUnreadCount = folder->unreadMessageCount(); |
259 } |
195 } |
269 return false; |
205 return false; |
270 } |
206 } |
271 return true; |
207 return true; |
272 } |
208 } |
273 |
209 |
|
210 |
|
211 /*! |
|
212 * TODO: UPDATE THIS |
|
213 Refresh email data. |
|
214 \post mEnvelopeList is refreshed with valid content so that it has |
|
215 valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes. |
|
216 |
|
217 - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs. |
|
218 - emits mailDataChanged() if new mail data is set into mEnvelopeList |
|
219 - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated |
|
220 |
|
221 \return true if everything succeeded, otherwise false |
|
222 */ |
|
223 bool NmHsWidgetEmailEngine::updateData() |
|
224 { |
|
225 NM_FUNCTION; |
|
226 if (!mEmailInterface) { |
|
227 NM_ERROR(1,"NmHsWidgetEmailEngine::updateData() -- Interface missing"); |
|
228 emit exceptionOccured(NmEngineExcFailure); //fatal error |
|
229 return false; //if interface is missing there's nothing to do |
|
230 } |
|
231 |
|
232 QList<NmMessageEnvelope*> envelopeList; |
|
233 //get messages from inbox |
|
234 int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, envelopeList, |
|
235 KMaxNumberOfEnvelopesProvided); |
|
236 if (msgErr) { |
|
237 //retrieval of messages failed. |
|
238 return false; |
|
239 } |
|
240 //emit signal about new message data right away |
|
241 emit mailDataRefreshed(envelopeList); |
|
242 |
|
243 return true; |
|
244 } |
|
245 |
274 /*! |
246 /*! |
275 handleMessageEvent slot. |
247 handleMessageEvent slot. |
276 */ |
248 */ |
277 void NmHsWidgetEmailEngine::handleMessageEvent( |
249 void NmHsWidgetEmailEngine::handleMessageEvent( |
278 NmMessageEvent event, |
250 NmMessageEvent event, |
279 const NmId &folderId, |
251 const NmId &folderId, |
280 const QList<NmId> &messageIds, |
252 const QList<NmId> &messageIds, |
281 const NmId& mailboxId) |
253 const NmId& mailboxId) |
282 { |
254 { |
283 NM_FUNCTION; |
255 NM_FUNCTION; |
284 Q_UNUSED(event); |
|
285 Q_UNUSED(messageIds); |
|
286 |
256 |
287 if (!mEmailInterface) { |
257 if (!mEmailInterface) { |
288 NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing"); |
258 NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing"); |
289 emit exceptionOccured(NmEngineExcFailure); //fatal error |
259 emit exceptionOccured(NmEngineExcFailure); //fatal error |
290 return; //if interface is missing there's nothing to do |
260 return; //if interface is missing there's nothing to do |
295 //so let's get the id now as we received first mail event |
265 //so let's get the id now as we received first mail event |
296 mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox); |
266 mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox); |
297 } |
267 } |
298 if ((folderId == mFolderId) && (mailboxId == mMailboxId)) { |
268 if ((folderId == mFolderId) && (mailboxId == mMailboxId)) { |
299 //Data is updated only if the engine is not suspended |
269 //Data is updated only if the engine is not suspended |
|
270 |
300 if (mSuspended) { |
271 if (mSuspended) { |
301 mMessageEventReceivedWhenSuspended = true; |
272 mMessageDataNeedsUpdate = true; |
302 } |
273 return; //no further actions |
303 else { |
274 } |
304 //start or restart the timer. Update is started when timer expires |
275 updateUnreadCount(); |
305 mUpdateTimer->start(); |
276 switch(event){ |
306 } |
277 case(NmMessageCreated): |
307 } |
278 emit mailsReceived(getEnvelopesFromIds(messageIds)); |
308 } |
279 break; |
|
280 case(NmMessageChanged): |
|
281 emit mailsUpdated(getEnvelopesFromIds(messageIds)); |
|
282 break; |
|
283 case(NmMessageDeleted): |
|
284 emit mailsDeleted(messageIds); |
|
285 break; |
|
286 } |
|
287 |
|
288 } |
|
289 } |
|
290 |
|
291 QList<NmMessageEnvelope*> NmHsWidgetEmailEngine::getEnvelopesFromIds(const QList<NmId> messageIds) |
|
292 { |
|
293 NM_FUNCTION; |
|
294 QList<NmMessageEnvelope*> ret; |
|
295 foreach(NmId id, messageIds){ |
|
296 NmMessageEnvelope *msgEnvelope = NULL; |
|
297 msgEnvelope = envelopeById(id); |
|
298 if(msgEnvelope){ |
|
299 ret.append(msgEnvelope); |
|
300 } |
|
301 } |
|
302 return ret; |
|
303 } |
|
304 |
|
305 /*! |
|
306 Get new NmMessageEnvelope object by id \a messageId from any folder. |
|
307 Ownership is transferred to the caller. NULL pointer is returned if |
|
308 message id is not found. |
|
309 */ |
|
310 NmMessageEnvelope *NmHsWidgetEmailEngine::envelopeById(const NmId &messageId) |
|
311 { |
|
312 NM_FUNCTION; |
|
313 |
|
314 NmMessageEnvelope *msgEnvelope(NULL); |
|
315 NmMessage *newMessageObject(NULL); |
|
316 int retVal(NmNotFoundError); |
|
317 |
|
318 retVal = mEmailInterface->getMessageById(mMailboxId, mFolderId, messageId, newMessageObject); |
|
319 if (retVal < NmNoError || !newMessageObject) { |
|
320 // Return null object if fetching failed |
|
321 delete newMessageObject; |
|
322 newMessageObject = NULL; |
|
323 return NULL; |
|
324 } |
|
325 |
|
326 msgEnvelope = new NmMessageEnvelope(newMessageObject->envelope()); |
|
327 |
|
328 delete newMessageObject; |
|
329 newMessageObject = NULL; |
|
330 return msgEnvelope; |
|
331 } |
|
332 |
309 |
333 |
310 /*! |
334 /*! |
311 handleFolderEvent slot. |
335 handleFolderEvent slot. |
312 If the monitored folder is deleted, clear the envelope list and unread count. |
336 If the monitored folder is deleted, clear the envelope list and unread count. |
313 Also the mFolderId is set to 0 to indicate the current state so that in device boot up |
337 Also the mFolderId is set to 0 to indicate the current state so that in device boot up |
319 { |
343 { |
320 NM_FUNCTION; |
344 NM_FUNCTION; |
321 //react only if the monitored folder for monitored account is deleted |
345 //react only if the monitored folder for monitored account is deleted |
322 if (event == NmFolderIsDeleted && mailboxId == mMailboxId && folderIds.contains(mFolderId) ) |
346 if (event == NmFolderIsDeleted && mailboxId == mMailboxId && folderIds.contains(mFolderId) ) |
323 { |
347 { |
324 resetEnvelopeList(); //cached envelopes to be cleared |
348 emit mailDataCleared(); //emit mail data clearance |
325 mUnreadCount = 0; //unread count to 0 |
349 mUnreadCount = 0; //unread count to 0 |
326 mFolderId = NmId(0); //folder id to zero (indicates the situation where folder is not available) |
350 mFolderId = NmId(0); //folder id to zero (indicates the situation where folder is not available) |
327 emit mailDataChanged(); //emit data change for UI |
|
328 emit unreadCountChanged(mUnreadCount); // emit unread count change to UI |
351 emit unreadCountChanged(mUnreadCount); // emit unread count change to UI |
329 } |
352 } |
330 } |
353 } |
331 |
354 |
332 |
355 |
413 */ |
441 */ |
414 void NmHsWidgetEmailEngine::activate() |
442 void NmHsWidgetEmailEngine::activate() |
415 { |
443 { |
416 NM_FUNCTION; |
444 NM_FUNCTION; |
417 mSuspended = false; |
445 mSuspended = false; |
418 if (mAccountEventReceivedWhenSuspended) { |
446 if (mAccountDataNeedsUpdate) { |
419 mAccountEventReceivedWhenSuspended = false; |
447 mAccountDataNeedsUpdate = false; |
420 updateAccount(); |
448 updateAccount(); |
421 } |
449 } |
422 if (mMessageEventReceivedWhenSuspended) { |
450 if (mMessageDataNeedsUpdate) { |
423 mMessageEventReceivedWhenSuspended = false; |
451 mMessageDataNeedsUpdate = false; |
424 updateData(); |
452 updateData(); |
425 } |
453 updateUnreadCount(); |
426 } |
454 } |
|
455 } |
|
456 |
427 |
457 |
428 /*! |
458 /*! |
429 launchMailAppInboxView slot. |
459 launchMailAppInboxView slot. |
430 \post Mail application is launched to inbox view corresponding widget's mailbox id |
460 \post Mail application is launched to inbox view corresponding widget's mailbox id |
431 */ |
461 */ |