42 |
45 |
43 /*! |
46 /*! |
44 Helper method for finding out if XQSettingsKey and XQCentralRepositorySettingsKey points to |
47 Helper method for finding out if XQSettingsKey and XQCentralRepositorySettingsKey points to |
45 same key. |
48 same key. |
46 |
49 |
47 @param settingKey XQSettingsKey |
50 \param settingKey XQSettingsKey |
48 @param cenrepSettingKey XQCentralRepositorySettingsKey |
51 \param cenrepSettingKey XQCentralRepositorySettingsKey |
49 @return <code>true</code> if target, uid and key matches otherwise returns <code>false</code> |
52 \return <code>true</code> if target, uid and key matches otherwise returns <code>false</code> |
50 */ |
53 */ |
51 bool keysEqual(const XQSettingsKey& settingKey, const XQCentralRepositorySettingsKey& cenrepSettingKey) |
54 bool keysEqual(const XQSettingsKey& settingKey, const XQCentralRepositorySettingsKey& cenrepSettingKey) |
52 { |
55 { |
53 return ((settingKey.target() == cenrepSettingKey.target()) && |
56 return ((settingKey.target() == cenrepSettingKey.target()) && |
54 (settingKey.uid() == cenrepSettingKey.uid()) && |
57 (settingKey.uid() == cenrepSettingKey.uid()) && |
55 (settingKey.key() == cenrepSettingKey.key())); |
58 (settingKey.key() == cenrepSettingKey.key())); |
56 } |
59 } |
57 |
60 |
|
61 |
|
62 |
|
63 /*! |
|
64 \class NmMailboxInfo |
|
65 |
|
66 \brief Main class for storing mailbox data. |
|
67 */ |
|
68 NmMailboxInfo::NmMailboxInfo() |
|
69 : mId(0), |
|
70 mIndicatorIndex(NmAgentIndicatorNotSet), |
|
71 mInboxFolderId(0), |
|
72 mOutboxFolderId(0), |
|
73 mSyncState(SyncComplete), |
|
74 mConnectState(Disconnected), |
|
75 mInboxCreatedMessages(0), |
|
76 mInboxChangedMessages(0), |
|
77 mInboxDeletedMessages(0), |
|
78 mOutboxMails(0), |
|
79 mActive(false), |
|
80 mInboxActive(false) |
|
81 { |
|
82 NM_FUNCTION; |
|
83 } |
58 |
84 |
59 /*! |
85 /*! |
60 \class NmMailAgent |
86 \class NmMailAgent |
61 |
87 |
62 \brief Main class for receiving email events and passing them to the HbIndicator |
88 \brief Main class for receiving email events. Activates and deactivates indicator. |
63 */ |
89 */ |
64 |
|
65 NmMailboxInfo::NmMailboxInfo() |
|
66 { |
|
67 NM_FUNCTION; |
|
68 |
|
69 mId = 0; |
|
70 mIndicatorIndex = NmAgentIndicatorNotSet; |
|
71 mSyncState = SyncComplete; |
|
72 mConnectState = Disconnected; |
|
73 mOutboxMails = 0; |
|
74 mInboxFolderId = 0; |
|
75 mOutboxFolderId = 0; |
|
76 mInboxCreatedMessages = 0; |
|
77 mInboxChangedMessages = 0; |
|
78 mInboxDeletedMessages = 0; |
|
79 mActive = false; |
|
80 mInboxActive = false; |
|
81 } |
|
82 |
|
83 NmMailAgent::NmMailAgent() : |
90 NmMailAgent::NmMailAgent() : |
84 mIndicator(NULL), |
91 mIndicator(NULL), |
85 mSystemTone(NULL), |
92 mSystemTone(NULL), |
86 mPluginFactory(NULL), |
93 mPluginFactory(NULL), |
87 mVibra(NULL), |
94 mVibra(NULL), |
88 mAlertToneAllowed(true), |
95 mAlertToneAllowed(true), |
89 mLastOutboxCount(0), |
96 mLastOutboxCount(0), |
90 mUnreadIndicatorActive(false), |
97 mUnreadIndicatorActive(false), |
91 mSettingManager(NULL), |
98 mSettingManager(NULL), |
92 mSilenceMode(NmSilenceModeOn) // by default silent mode is on |
99 mSilenceMode(NmSilenceModeOn) // by default silent mode is on |
93 { |
100 { |
94 NM_FUNCTION; |
101 NM_FUNCTION; |
95 } |
102 } |
96 |
103 |
97 /*! |
104 /*! |
98 Delayed start |
105 Delayed start. |
99 */ |
106 */ |
100 void NmMailAgent::delayedStart() |
107 void NmMailAgent::delayedStart() |
101 { |
108 { |
102 NM_FUNCTION; |
109 NM_FUNCTION; |
103 |
110 |
104 if (!init()) { |
111 if (!init()) { |
105 // Initialisation failed. Quit the agent. |
112 // Initialisation failed. Quit the agent. |
106 QCoreApplication::exit(1); |
113 QCoreApplication::exit(1); |
107 } |
114 } |
108 } |
115 } |
109 |
116 |
110 /*! |
117 /*! |
111 Initialise the agent. \return true if succesfully started. |
118 Initialise the agent. |
112 */ |
119 |
|
120 \return true if succesfully started. |
|
121 */ |
113 bool NmMailAgent::init() |
122 bool NmMailAgent::init() |
114 { |
123 { |
115 NM_FUNCTION; |
124 NM_FUNCTION; |
116 |
125 |
117 mPluginFactory = NmDataPluginFactory::instance(); |
126 mPluginFactory = NmDataPluginFactory::instance(); |
147 mSystemTone = new XQSystemToneService(); |
156 mSystemTone = new XQSystemToneService(); |
148 |
157 |
149 delete mIndicator; |
158 delete mIndicator; |
150 mIndicator = NULL; |
159 mIndicator = NULL; |
151 mIndicator = new HbIndicator(); |
160 mIndicator = new HbIndicator(); |
152 connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)), |
161 connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap &)), |
153 this, SLOT(indicatorActivated(const QString&, const QVariantMap&))); |
162 this, SLOT(indicatorActivated(const QString &, const QVariantMap &))); |
154 |
163 |
155 QList<QObject*> *plugins = mPluginFactory->pluginInstances(); |
164 QList<QObject*> *plugins = mPluginFactory->pluginInstances(); |
156 |
165 |
157 foreach (QObject *plugin, *plugins) { |
166 foreach (QObject *plugin, *plugins) { |
158 if (plugin) { |
167 if (plugin) { |
159 // Start listening events |
168 // Start listening events |
160 connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, const QList<NmId>&)), |
169 connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, const QList<NmId> &)), |
161 this, SLOT(handleMailboxEvent(NmMailboxEvent, const QList<NmId> &)), |
170 this, SLOT(handleMailboxEvent(NmMailboxEvent, const QList<NmId> &)), |
162 Qt::UniqueConnection); |
171 Qt::UniqueConnection); |
163 |
172 |
164 connect(plugin, SIGNAL(messageEvent( |
173 connect(plugin, SIGNAL(messageEvent( |
165 NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)), |
174 NmMessageEvent, const NmId &, const QList<NmId> &, const NmId &)), |
166 this, SLOT(handleMessageEvent( |
175 this, SLOT(handleMessageEvent( |
167 NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)), |
176 NmMessageEvent, const NmId &, const QList<NmId> &, const NmId &)), |
168 Qt::UniqueConnection); |
177 Qt::UniqueConnection); |
169 |
178 |
170 connect(plugin, SIGNAL(syncStateEvent(NmSyncState, const NmOperationCompletionEvent&)), |
179 connect(plugin, SIGNAL(syncStateEvent(NmSyncState, const NmOperationCompletionEvent &)), |
171 this, SLOT(handleSyncStateEvent(NmSyncState, const NmOperationCompletionEvent&)), |
180 this, SLOT(handleSyncStateEvent(NmSyncState, const NmOperationCompletionEvent &)), |
172 Qt::UniqueConnection); |
181 Qt::UniqueConnection); |
173 |
182 |
174 connect(plugin, SIGNAL(connectionEvent(NmConnectState, const NmId, int)), |
183 connect(plugin, SIGNAL(connectionEvent(NmConnectState, const NmId, int)), |
175 this, SLOT(handleConnectionEvent(NmConnectState, const NmId, int)), |
184 this, SLOT(handleConnectionEvent(NmConnectState, const NmId, int)), |
176 Qt::UniqueConnection); |
185 Qt::UniqueConnection); |
177 } |
186 } |
178 } |
187 } |
179 |
188 |
180 // Construct the vibra interface instance. |
189 // Construct the vibra interface instance. |
181 delete mVibra; |
190 delete mVibra; |
182 mVibra = NULL; |
191 mVibra = NULL; |
226 QListIterator<NmMailbox *> i(mailboxes); |
239 QListIterator<NmMailbox *> i(mailboxes); |
227 i.toBack(); |
240 i.toBack(); |
228 while (i.hasPrevious()) { |
241 while (i.hasPrevious()) { |
229 const NmMailbox *mailbox = i.previous(); |
242 const NmMailbox *mailbox = i.previous(); |
230 if (mailbox) { |
243 if (mailbox) { |
231 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox,plugin); |
244 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox, plugin); |
232 if (mailboxInfo) { |
245 if (mailboxInfo) { |
233 bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo); |
246 bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo); |
234 bool wasActive = isMailboxActive(mailbox->id()); |
247 bool wasActive = isMailboxActive(mailbox->id()); |
235 if (activate) { |
248 if (activate) { |
236 if(!wasActive) { |
249 if (!wasActive) { |
237 // do not activate the mailbox if it was left as hidden last time |
250 // do not activate the mailbox if it was left as hidden last time |
238 activate = false; |
251 activate = false; |
239 } |
252 } |
240 else { |
253 else { |
241 // otherwise, activate the mailbox and show inbox state |
254 // otherwise, activate the mailbox and show inbox state |
242 mailboxInfo->mInboxActive = true; |
255 mailboxInfo->mInboxActive = true; |
243 } |
256 } |
244 } |
257 } |
245 |
258 |
246 mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id()); |
259 mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id(), |
|
260 mailboxInfo->mOutboxFolderId); |
247 if (mailboxInfo->mOutboxMails > 0 && wasActive) { |
261 if (mailboxInfo->mOutboxMails > 0 && wasActive) { |
248 activate = true; |
262 activate = true; |
249 } |
263 } |
250 |
264 |
251 // Create indicator for visible mailboxes |
265 // Create indicator for visible mailboxes |
252 updateMailboxState(mailbox->id(), activate, false); |
266 updateMailboxState(mailbox->id(), activate, false); |
253 } |
267 } |
254 } |
268 } |
255 } |
269 } |
256 qDeleteAll(mailboxes); |
270 qDeleteAll(mailboxes); |
257 } |
271 } |
258 updateUnreadIndicator(); |
272 updateUnreadIndicator(); |
259 updateSendIndicator(); |
273 updateSendIndicator(); |
260 } |
274 } |
261 |
275 |
262 /*! |
276 /*! |
263 Get mailbox unread count in inbox folder |
277 Get mailbox unread count in inbox folder. |
|
278 |
264 \param mailboxId id of the mailbox |
279 \param mailboxId id of the mailbox |
265 \param mailboxInfo contains the list of unread messages |
280 \param mailboxInfo contains the list of unread messages |
266 \return true if new unread mails was found |
281 \return true if new unread mails was found |
267 */ |
282 */ |
268 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo) |
283 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo) |
269 { |
284 { |
270 NM_FUNCTION; |
285 NM_FUNCTION; |
271 |
286 |
272 int newUnreadMessages(0); |
287 int newUnreadMessages(0); |
273 |
|
274 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
288 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
275 |
289 |
276 if (plugin) { |
290 if (plugin) { |
277 // get inbox folder ID |
291 NmId inboxId = mailboxInfo.mInboxFolderId.id(); |
278 NmId inboxId = plugin->getStandardFolderId( |
292 |
279 mailboxId, NmFolderInbox ); |
293 // Inbox folder ID may be still unknown |
280 |
294 if (inboxId == 0) { |
281 // get list of messages in inbox |
295 mailboxInfo.mInboxFolderId = plugin->getStandardFolderId(mailboxId, NmFolderInbox); |
282 QList<NmMessageEnvelope*> messageList; |
296 inboxId = mailboxInfo.mInboxFolderId.id(); |
283 plugin->listMessages(mailboxId, inboxId, messageList); |
297 } |
284 |
298 |
285 QList<NmId> newUnreadMessageIdList; |
299 // get list of messages in inbox |
286 foreach (const NmMessageEnvelope* envelope, messageList) { |
300 QList<NmMessageEnvelope*> messageList; |
287 // if the message is not read, it is "unread" |
301 plugin->listMessages(mailboxId, inboxId, messageList); |
288 if (!envelope->isRead()) { |
302 |
289 quint64 messageId = envelope->messageId().id(); |
303 QList<NmId> newUnreadMessageIdList; |
290 newUnreadMessageIdList.append(envelope->messageId()); |
304 foreach (const NmMessageEnvelope* envelope, messageList) { |
291 bool found(false); |
305 // if the message is not read, it is "unread" |
292 // Iterate through all known ids. If the id can't be found the mail is new. |
306 if (!envelope->isRead()) { |
293 foreach (const NmId id, mailboxInfo.mUnreadMailIdList) { |
307 quint64 messageId = envelope->messageId().id(); |
294 if (id.id() == messageId) { |
308 newUnreadMessageIdList.append(envelope->messageId()); |
295 found = true; |
309 bool found(false); |
296 break; |
310 // Iterate through all known ids. If the id can't be found the mail is new. |
297 } |
311 foreach (const NmId id, mailboxInfo.mUnreadMailIdList) { |
298 } |
312 if (id.id() == messageId) { |
299 |
313 found = true; |
300 if (!found) { |
314 break; |
301 newUnreadMessages++; |
315 } |
302 } |
316 } |
303 } |
317 |
304 } |
318 if (!found) { |
305 qDeleteAll(messageList); |
319 newUnreadMessages++; |
306 |
320 } |
307 // Save updated list of unread message IDs |
321 } |
|
322 } |
|
323 qDeleteAll(messageList); |
|
324 |
|
325 // Save updated list of unread message IDs |
308 mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList; |
326 mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList; |
309 } |
327 } |
310 NM_COMMENT(QString("NmMailAgent::getUnreadCount(): count=%1, new=%2"). |
328 NM_COMMENT(QString("NmMailAgent::getUnreadCount(): count=%1, new=%2"). |
311 arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages)); |
329 arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages)); |
312 |
330 |
313 return (newUnreadMessages > 0); |
331 return (newUnreadMessages > 0); |
314 } |
332 } |
315 |
333 |
316 /*! |
334 /*! |
317 Get mailbox count in outbox folder |
335 Get mailbox count in outbox folder. |
|
336 |
318 \param mailboxId id of the mailbox |
337 \param mailboxId id of the mailbox |
319 \param maxCount max number of outbox mails that is needed |
338 \param outboxId Outbox folder Id |
320 \return number of mails in the outbox |
339 \return number of mails in the outbox |
321 */ |
340 */ |
322 int NmMailAgent::getOutboxCount(const NmId &mailboxId) |
341 int NmMailAgent::getOutboxCount(const NmId &mailboxId, const NmId &outboxId) |
323 { |
342 { |
324 NM_FUNCTION; |
343 NM_FUNCTION; |
325 |
344 |
326 int count(0); |
345 int count(0); |
327 |
|
328 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
346 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
329 |
347 |
330 if (plugin) { |
348 if (plugin) { |
331 // get outbox folder ID |
349 // get list of messages in outbox |
332 NmId outboxId = plugin->getStandardFolderId( mailboxId, NmFolderOutbox ); |
350 QList<NmMessageEnvelope*> messageList; |
333 |
351 plugin->listMessages(mailboxId, outboxId, messageList); |
334 // get list of messages in outbox |
352 count = messageList.count(); |
335 QList<NmMessageEnvelope*> messageList; |
353 qDeleteAll(messageList); |
336 plugin->listMessages(mailboxId, outboxId, messageList); |
|
337 count = messageList.count(); |
|
338 qDeleteAll(messageList); |
|
339 } |
354 } |
340 NM_COMMENT(QString("NmMailAgent::getOutboxCount(): count=%1").arg(count)); |
355 NM_COMMENT(QString("NmMailAgent::getOutboxCount(): count=%1").arg(count)); |
341 |
356 |
342 return count; |
357 return count; |
343 } |
358 } |
344 |
359 |
345 /*! |
360 /*! |
346 Get list of unread counts in active mailboxes |
361 Get list of unread counts in active mailboxes. |
|
362 |
347 \returns total number of unread mails |
363 \returns total number of unread mails |
348 */ |
364 */ |
349 int NmMailAgent::getTotalUnreadCount() const |
365 int NmMailAgent::getTotalUnreadCount() const |
350 { |
366 { |
351 NM_FUNCTION; |
367 NM_FUNCTION; |
352 |
368 |
353 int unreads = 0; |
369 int unreads = 0; |
358 } |
374 } |
359 return unreads; |
375 return unreads; |
360 } |
376 } |
361 |
377 |
362 /*! |
378 /*! |
363 Update the "@" indicator state according to unread state |
379 Update the "@" indicator state according to unread state. |
|
380 |
364 \return true if the indicator was activated |
381 \return true if the indicator was activated |
365 */ |
382 */ |
366 bool NmMailAgent::updateUnreadIndicator() |
383 bool NmMailAgent::updateUnreadIndicator() |
367 { |
384 { |
368 NM_FUNCTION; |
385 NM_FUNCTION; |
369 |
386 |
370 int unreads = getTotalUnreadCount(); |
387 int unreads = getTotalUnreadCount(); |
371 return updateUnreadIndicator(unreads>0); |
388 return updateUnreadIndicator(unreads>0); |
372 } |
389 } |
373 |
390 |
374 /*! |
391 /*! |
375 Update the mailbox visibility and status |
392 Update the mailbox visibility and status. |
|
393 |
376 \param mailboxId id of the mailbox |
394 \param mailboxId id of the mailbox |
377 \param active visibility state of the mailbox |
395 \param active visibility state of the mailbox |
378 \param refreshAlways true when the indicator should be always updated |
396 \param refreshAlways true when the indicator should be always updated |
379 \return true if the mailbox state was changed |
397 \return true if the mailbox state was changed |
380 */ |
398 */ |
381 bool NmMailAgent::updateMailboxState(const NmId &mailboxId, |
399 bool NmMailAgent::updateMailboxState(const NmId &mailboxId, |
382 bool active, bool refreshAlways) |
400 bool active, bool refreshAlways) |
383 { |
401 { |
384 NM_FUNCTION; |
402 NM_FUNCTION; |
385 |
403 |
386 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
404 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
387 bool changed = false; |
405 bool changed = false; |
388 if (mailboxInfo->mActive != active || |
406 if (mailboxInfo->mActive != active || |
389 refreshAlways) { |
407 refreshAlways) { |
390 |
408 |
391 // store the new state to permanent storage |
409 // store the new state to permanent storage |
392 storeMailboxActive(mailboxId, active); |
410 storeMailboxActive(mailboxId, active); |
393 |
411 |
394 mailboxInfo->mActive = active; |
412 mailboxInfo->mActive = active; |
395 changed = true; |
413 changed = true; |
396 if (active) { |
414 if (active) { |
490 // Instance might be NULL if the service is not available. |
513 // Instance might be NULL if the service is not available. |
491 if (request) { |
514 if (request) { |
492 QList<QVariant> list; |
515 QList<QVariant> list; |
493 list.append(QVariant(mailboxId)); |
516 list.append(QVariant(mailboxId)); |
494 request->setArguments(list); |
517 request->setArguments(list); |
495 |
|
496 ok = request->send(); |
518 ok = request->send(); |
497 NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError())); |
519 NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError())); |
498 delete request; |
520 delete request; |
499 } |
521 } |
500 return ok; |
522 return ok; |
501 } |
523 } |
502 |
524 |
503 /*! |
525 /*! |
504 Get next free indicator index, starting from 0 |
526 Handles message created event. |
505 @return index of the indicator that is available |
527 |
|
528 \param folderId Id of the folder that includes the message |
|
529 \param messageIds Message ids that are checked |
|
530 \param mailboxId Id of the mailbox that includes the message |
|
531 \param updateNeeded Set to <code>true</code> if update needed otherwise not touched |
|
532 \param activate Set to <code>true</code> if activation needed otherwise not touched |
|
533 */ |
|
534 void NmMailAgent::handleMessageCreatedEvent(const NmId &folderId, const QList<NmId> &messageIds, |
|
535 const NmId &mailboxId, bool &updateNeeded, bool &activate) |
|
536 { |
|
537 NM_FUNCTION; |
|
538 |
|
539 // Check the new messages to make the indicator appear earlier |
|
540 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
|
541 |
|
542 // Inbox folder ID may be still unknown |
|
543 if (mailboxInfo->mInboxFolderId.id() == 0) { |
|
544 NmDataPluginInterface *plugin = |
|
545 mPluginFactory->interfaceInstance(mailboxId); |
|
546 |
|
547 if (plugin) { |
|
548 mailboxInfo->mInboxFolderId = |
|
549 plugin->getStandardFolderId(mailboxId, NmFolderInbox); |
|
550 } |
|
551 } |
|
552 |
|
553 if (folderId == mailboxInfo->mInboxFolderId) { |
|
554 foreach (NmId messageId, messageIds) { |
|
555 bool messageUnread = false; |
|
556 |
|
557 // Check the message if we can either play a tone or if the "@" is |
|
558 // not visible at the moment |
|
559 if (mAlertToneAllowed || !mUnreadIndicatorActive) { |
|
560 if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) { |
|
561 if (messageUnread) { |
|
562 mailboxInfo->mUnreadMailIdList.append(messageId); |
|
563 mailboxInfo->mInboxActive = true; |
|
564 updateMailboxState(mailboxId, true, false); |
|
565 |
|
566 // make the "@" appear immediatelly |
|
567 updateUnreadIndicator(true); |
|
568 |
|
569 // Play the tone as well |
|
570 playAlertTone(); |
|
571 } |
|
572 } |
|
573 } |
|
574 } |
|
575 } |
|
576 |
|
577 if (folderId==mailboxInfo->mInboxFolderId) { |
|
578 mailboxInfo->mInboxCreatedMessages += messageIds.count(); |
|
579 } |
|
580 |
|
581 // When created a new mail in the outbox, we are in sending state |
|
582 if (mailboxInfo->mOutboxFolderId == folderId) { |
|
583 // The first mail created in the outbox |
|
584 if (mailboxInfo->mOutboxMails <= 0) { |
|
585 NM_COMMENT("NmMailAgent: first mail in outbox"); |
|
586 } |
|
587 // Always activate the indicator |
|
588 activate = true; |
|
589 updateNeeded = true; |
|
590 |
|
591 mailboxInfo->mOutboxMails += messageIds.count(); |
|
592 updateSendIndicator(); |
|
593 } |
|
594 } |
|
595 |
|
596 |
|
597 /*! |
|
598 Handles message changed event. |
|
599 \sa updateUnreadCount |
|
600 |
|
601 \param folderId Id of the folder that includes the message |
|
602 \param mailboxId Id of the mailbox that includes the message |
|
603 \param updateNeeded Set to <code>true</code> if update needed otherwise not touched |
|
604 \param activate Set result value of updateUnreadCount method |
|
605 */ |
|
606 void NmMailAgent::handleMessageChangedEvent(const NmId &folderId, const NmId &mailboxId, |
|
607 bool &updateNeeded, bool &activate) |
|
608 { |
|
609 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
|
610 |
|
611 if (folderId == mailboxInfo->mInboxFolderId) { |
|
612 mailboxInfo->mInboxChangedMessages++; |
|
613 } |
|
614 |
|
615 // If not currently syncronizing the mailbox, this may mean |
|
616 // that a message was read/unread |
|
617 if (mailboxInfo && mailboxInfo->mSyncState == SyncComplete) { |
|
618 |
|
619 // check the unread status again |
|
620 int oldCount(mailboxInfo->mUnreadMailIdList.count()); |
|
621 activate = updateUnreadCount(mailboxId, *mailboxInfo); |
|
622 |
|
623 // new unread mails found or no more unread mails in the inbox |
|
624 if (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0) { |
|
625 updateNeeded = true; |
|
626 } |
|
627 } |
|
628 } |
|
629 |
|
630 /*! |
|
631 Handles message deleted event. |
|
632 |
|
633 \param folderId Id of the folder that includes the message |
|
634 \param messageIds Message ids that are checked |
|
635 \param mailboxId Id of the mailbox that includes the message |
|
636 \param updateNeeded Set to <code>true</code> if update needed otherwise not touched |
|
637 \param activate Set to <code>true</code> if activation needed otherwise not touched |
|
638 */ |
|
639 void NmMailAgent::handleMessageDeletedEvent(const NmId &folderId, const QList<NmId> &messageIds, |
|
640 const NmId &mailboxId, bool &updateNeeded, bool &activate) |
|
641 { |
|
642 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
|
643 |
|
644 if (folderId == mailboxInfo->mInboxFolderId) { |
|
645 mailboxInfo->mInboxDeletedMessages++; |
|
646 } |
|
647 |
|
648 // Deleted mails from the outbox |
|
649 if (mailboxInfo->mOutboxFolderId == folderId) { |
|
650 mailboxInfo->mOutboxMails -= messageIds.count(); |
|
651 |
|
652 // Sanity check for the outbox count |
|
653 if (mailboxInfo->mOutboxMails < 0) { |
|
654 mailboxInfo->mOutboxMails = 0; |
|
655 } |
|
656 |
|
657 // The last mail was now deleted |
|
658 if (mailboxInfo->mOutboxMails == 0) { |
|
659 NM_COMMENT("NmMailAgent: last mail deleted from outbox"); |
|
660 updateNeeded = true; |
|
661 |
|
662 // Keep it active if there is unread mails and inbox is still active |
|
663 if (mailboxInfo->mInboxActive && |
|
664 mailboxInfo->mUnreadMailIdList.count() > 0) { |
|
665 activate = true; |
|
666 } |
|
667 } |
|
668 else { |
|
669 // Also update the indicator status if it is already shown |
|
670 if (mailboxInfo->mActive) { |
|
671 activate = true; |
|
672 updateNeeded = true; |
|
673 } |
|
674 } |
|
675 updateSendIndicator(); |
|
676 } |
|
677 } |
|
678 |
|
679 /*! |
|
680 Get next free indicator index, starting from 0. |
|
681 |
|
682 \return index of the indicator that is available |
506 */ |
683 */ |
507 int NmMailAgent::getFreeIndicatorIndex() |
684 int NmMailAgent::getFreeIndicatorIndex() |
508 { |
685 { |
509 NM_FUNCTION; |
686 NM_FUNCTION; |
510 |
687 |
525 } |
702 } |
526 |
703 |
527 /*! |
704 /*! |
528 Received from NmFrameworkAdapter mailboxEvent signal |
705 Received from NmFrameworkAdapter mailboxEvent signal |
529 \sa NmFrameworkAdapter |
706 \sa NmFrameworkAdapter |
530 */ |
707 |
|
708 \param event Mailbox event type |
|
709 \param mailboxIds Mailbox ids that are checked |
|
710 */ |
531 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds) |
711 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds) |
532 { |
712 { |
533 NM_FUNCTION; |
713 NM_FUNCTION; |
534 NM_COMMENT(QString("NmMailAgent::handleMailboxEvent(): event=%1").arg(event)); |
714 NM_COMMENT(QString("NmMailAgent::handleMailboxEvent(): event=%1").arg(event)); |
535 |
715 |
536 switch(event) { |
716 switch(event) { |
537 case NmMailboxCreated: |
717 case NmMailboxCreated: |
538 foreach (NmId mailboxId, mailboxIds) { |
718 foreach (NmId mailboxId, mailboxIds) { |
539 getMailboxInfo(mailboxId); // create a new mailbox if needed |
719 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); // create a new mailbox if needed |
|
720 if (!mailboxInfo) { |
|
721 // Unable to initialise the mailbox. Try again later. |
|
722 NM_COMMENT("Cannot initialise mailbox"); |
|
723 |
|
724 qRegisterMetaType<NmId>("NmId"); |
|
725 QMetaObject::invokeMethod(this, "delayedMailboxCreated", |
|
726 Qt::QueuedConnection, Q_ARG(NmId,mailboxId)); |
|
727 } |
540 |
728 |
541 // make sure the mailbox activity data is reseted |
729 // make sure the mailbox activity data is reseted |
542 deleteStoredMailboxActivity(mailboxId); |
730 deleteStoredMailboxActivity(mailboxId); |
543 } |
731 } |
544 break; |
732 break; |
579 break; |
767 break; |
580 } |
768 } |
581 } |
769 } |
582 |
770 |
583 /*! |
771 /*! |
584 Map the type name to mailbox info |
772 Called when mailbox is initialised with a delay. This may happen at least when |
585 \return NULL if no mailbox match the type |
773 NmMailboxCreated event is received. |
|
774 |
|
775 \param mailboxId id of the mailbox |
586 */ |
776 */ |
|
777 void NmMailAgent::delayedMailboxCreated(const NmId mailboxId) |
|
778 { |
|
779 NM_FUNCTION; |
|
780 |
|
781 // create and subscribe to the mailbox, if not done earlier |
|
782 getMailboxInfo(mailboxId); |
|
783 } |
|
784 |
|
785 /*! |
|
786 Map the type name to mailbox info. |
|
787 |
|
788 \param type Indicator type name |
|
789 \return NULL if no mailbox match the type |
|
790 */ |
587 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type) |
791 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type) |
588 { |
792 { |
589 NM_FUNCTION; |
793 NM_FUNCTION; |
590 |
794 |
|
795 NmMailboxInfo *foundMailbox = NULL; |
591 foreach (NmMailboxInfo *mailbox, mMailboxes) { |
796 foreach (NmMailboxInfo *mailbox, mMailboxes) { |
592 // mailbox is shown in indicators |
797 // mailbox is shown in indicators |
593 if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) { |
798 if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) { |
594 QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex); |
799 QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex); |
595 |
800 |
596 // type names match(!) |
801 // type names match(!) |
597 if(type==typeName) { |
802 if (type == typeName) { |
598 return mailbox; |
803 foundMailbox = mailbox; |
599 } |
804 break; |
600 } |
805 } |
601 } |
806 } |
602 return NULL; |
807 } |
|
808 return foundMailbox; |
603 } |
809 } |
604 |
810 |
605 /*! |
811 /*! |
606 Called when indicator is clicked from the indicator menu |
812 Called when indicator is clicked from the indicator menu |
607 - indicator will be hide from the menu |
813 - indicator will be hide from the menu |
608 - mailbox will be launched |
814 - mailbox will be launched |
609 */ |
815 |
|
816 \param type Indicator type name |
|
817 \param data Data sent by indicator |
|
818 */ |
610 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data) |
819 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data) |
611 { |
820 { |
612 NM_FUNCTION; |
821 NM_FUNCTION; |
613 Q_UNUSED(data); |
822 Q_UNUSED(data); |
614 |
823 |
641 } |
850 } |
642 |
851 |
643 /*! |
852 /*! |
644 Received from NmFrameworkAdapter messageEvent signal |
853 Received from NmFrameworkAdapter messageEvent signal |
645 \sa NmFrameworkAdapter |
854 \sa NmFrameworkAdapter |
646 */ |
855 |
|
856 \param event Message event |
|
857 \param folderId Folder Id |
|
858 \param messageIds List of message Ids |
|
859 \param mailboxId Id of the mailbox |
|
860 */ |
647 void NmMailAgent::handleMessageEvent( |
861 void NmMailAgent::handleMessageEvent( |
648 NmMessageEvent event, |
862 NmMessageEvent event, |
649 const NmId &folderId, |
863 const NmId &folderId, |
650 const QList<NmId> &messageIds, |
864 const QList<NmId> &messageIds, |
651 const NmId& mailboxId) |
865 const NmId &mailboxId) |
652 { |
866 { |
653 NM_FUNCTION; |
867 NM_FUNCTION; |
654 |
|
655 NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2"). |
868 NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2"). |
656 arg(event).arg(mailboxId.id())); |
869 arg(event).arg(mailboxId.id())); |
657 |
870 |
658 bool updateNeeded = false; |
871 bool updateNeeded = false; |
659 bool activate = false; |
872 bool activate = false; |
660 |
873 |
661 switch (event) { |
874 switch (event) { |
662 case NmMessageCreated: { |
875 case NmMessageCreated: { |
663 // Check the new messages to make the indicator appear earlier |
876 handleMessageCreatedEvent(folderId, messageIds, mailboxId, updateNeeded, activate); |
664 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
|
665 |
|
666 // Inbox folder ID may be still unknown |
|
667 if (mailboxInfo->mInboxFolderId.id() == 0) { |
|
668 NmDataPluginInterface *plugin = |
|
669 mPluginFactory->interfaceInstance(mailboxId); |
|
670 |
|
671 if (plugin) { |
|
672 mailboxInfo->mInboxFolderId = |
|
673 plugin->getStandardFolderId(mailboxId, NmFolderInbox); |
|
674 } |
|
675 } |
|
676 |
|
677 if (folderId == mailboxInfo->mInboxFolderId) { |
|
678 foreach (NmId messageId, messageIds) { |
|
679 bool messageUnread = false; |
|
680 |
|
681 // Check the message if we can either play a tone or if the "@" is |
|
682 // not visible at the moment |
|
683 if (mAlertToneAllowed || !mUnreadIndicatorActive) { |
|
684 if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) { |
|
685 if (messageUnread) { |
|
686 mailboxInfo->mUnreadMailIdList.append(messageId); |
|
687 mailboxInfo->mInboxActive = true; |
|
688 updateMailboxState(mailboxId, true, false); |
|
689 |
|
690 // make the "@" appear immediatelly |
|
691 updateUnreadIndicator(true); |
|
692 |
|
693 // Play the tone as well |
|
694 playAlertTone(); |
|
695 } |
|
696 } |
|
697 } |
|
698 } |
|
699 } |
|
700 |
|
701 if (folderId==mailboxInfo->mInboxFolderId) { |
|
702 mailboxInfo->mInboxCreatedMessages += messageIds.count(); |
|
703 } |
|
704 |
|
705 // When created a new mail in the outbox, we are in sending state |
|
706 if (mailboxInfo->mOutboxFolderId == folderId) { |
|
707 // The first mail created in the outbox |
|
708 if (mailboxInfo->mOutboxMails <= 0) { |
|
709 NM_COMMENT("NmMailAgent: first mail in outbox"); |
|
710 } |
|
711 // Always activate the indicator |
|
712 activate = true; |
|
713 updateNeeded = true; |
|
714 |
|
715 mailboxInfo->mOutboxMails += messageIds.count(); |
|
716 updateSendIndicator(); |
|
717 } |
|
718 break; |
877 break; |
719 } |
878 } |
720 case NmMessageChanged: { |
879 case NmMessageChanged: { |
721 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
880 handleMessageChangedEvent(folderId, mailboxId, updateNeeded, activate); |
722 |
881 break; |
723 if (folderId==mailboxInfo->mInboxFolderId) { |
882 } |
724 mailboxInfo->mInboxChangedMessages++; |
|
725 } |
|
726 |
|
727 // If not currently syncronizing the mailbox, this may mean |
|
728 // that a message was read/unread |
|
729 if (mailboxInfo && mailboxInfo->mSyncState==SyncComplete) { |
|
730 // check the unread status again |
|
731 int oldCount = mailboxInfo->mUnreadMailIdList.count(); |
|
732 activate = updateUnreadCount(mailboxId, *mailboxInfo); |
|
733 |
|
734 // new unread mails found or no more unread mails in the inbox |
|
735 if (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0) { |
|
736 updateNeeded = true; |
|
737 } |
|
738 } |
|
739 break; |
|
740 } |
|
741 case NmMessageDeleted: { |
883 case NmMessageDeleted: { |
742 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); |
884 handleMessageDeletedEvent(folderId, messageIds, mailboxId, updateNeeded, activate); |
743 |
|
744 if (folderId==mailboxInfo->mInboxFolderId) { |
|
745 mailboxInfo->mInboxDeletedMessages++; |
|
746 } |
|
747 |
|
748 // Deleted mails from the outbox |
|
749 if (mailboxInfo->mOutboxFolderId == folderId) { |
|
750 mailboxInfo->mOutboxMails -= messageIds.count(); |
|
751 |
|
752 // Sanity check for the outbox count |
|
753 if (mailboxInfo->mOutboxMails < 0) { |
|
754 mailboxInfo->mOutboxMails = 0; |
|
755 } |
|
756 |
|
757 // The last mail was now deleted |
|
758 if (mailboxInfo->mOutboxMails == 0) { |
|
759 NM_COMMENT("NmMailAgent: last mail deleted from outbox"); |
|
760 updateNeeded = true; |
|
761 |
|
762 // Keep it active if there is unread mails and inbox is still active |
|
763 if (mailboxInfo->mInboxActive && |
|
764 mailboxInfo->mUnreadMailIdList.count() > 0) { |
|
765 activate = true; |
|
766 } |
|
767 } |
|
768 else { |
|
769 // Also update the indicator status if it is already shown |
|
770 if (mailboxInfo->mActive) { |
|
771 activate = true; |
|
772 updateNeeded = true; |
|
773 } |
|
774 } |
|
775 updateSendIndicator(); |
|
776 } |
|
777 break; |
885 break; |
778 } |
886 } |
779 default: |
887 default: |
780 break; |
888 break; |
781 } |
889 } |
811 info->mInboxChangedMessages = 0; |
921 info->mInboxChangedMessages = 0; |
812 info->mInboxDeletedMessages = 0; |
922 info->mInboxDeletedMessages = 0; |
813 } |
923 } |
814 else if (state==SyncComplete) { |
924 else if (state==SyncComplete) { |
815 // Check the unread status here again |
925 // Check the unread status here again |
816 bool activate = updateUnreadCount(event.mMailboxId, *info); |
926 bool updateMailbox = updateUnreadCount(event.mMailboxId, *info); |
817 int oldOutboxCount = info->mOutboxMails; |
927 int oldOutboxCount = info->mOutboxMails; |
818 info->mOutboxMails = getOutboxCount(event.mMailboxId); |
928 info->mOutboxMails = getOutboxCount(event.mMailboxId, info->mOutboxFolderId); |
819 if (info->mOutboxMails > oldOutboxCount) { |
929 int unreadMailCount(info->mUnreadMailIdList.count()); |
|
930 if (info->mOutboxMails > oldOutboxCount || unreadMailCount == 0) { |
820 // new mails in outbox |
931 // new mails in outbox |
821 activate = true; |
932 updateMailbox = true; |
822 } |
933 } |
823 bool active = info->mUnreadMailIdList.count() || |
934 bool active = unreadMailCount || info->mOutboxMails; |
824 info->mOutboxMails; |
|
825 |
935 |
826 // Refresh the indicator if messages created or changed |
936 // Refresh the indicator if messages created or changed |
827 NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): " |
937 NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): " |
828 "created=%1, changed=%2, deleted=%3"). |
938 "created=%1, changed=%2, deleted=%3"). |
829 arg(info->mInboxCreatedMessages). |
939 arg(info->mInboxCreatedMessages). |
956 return createMailboxInfo(id); |
1080 return createMailboxInfo(id); |
957 } |
1081 } |
958 |
1082 |
959 /*! |
1083 /*! |
960 Finds out if the message is unread. |
1084 Finds out if the message is unread. |
|
1085 |
961 \param folderId the id of the folder that includes the message |
1086 \param folderId the id of the folder that includes the message |
962 \param messageIds the message ids that are checked |
1087 \param messageIds the message ids that are checked |
963 \param mailboxId the id of the mailbox that includes the message |
1088 \param mailboxId the id of the mailbox that includes the message |
964 \param unreadMessage true if there was unread messages |
1089 \param unreadMessage true if there was unread messages |
965 \return true if info fetching was successful |
1090 \return true if info fetching was successful |
966 */ |
1091 */ |
967 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId, |
1092 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId, |
968 const NmId &messageId, const NmId &mailboxId, bool &unreadMessage) |
1093 const NmId &messageId, const NmId &mailboxId, bool &unreadMessage) |
969 { |
1094 { |
970 NM_FUNCTION; |
1095 NM_FUNCTION; |
971 |
1096 |
972 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
1097 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId); |
973 bool ok = false; |
1098 bool ok(false); |
974 |
1099 |
975 if (plugin) { |
1100 if (plugin) { |
976 NmMessage *message=NULL; |
1101 NmMessage *message=NULL; |
977 plugin->getMessageById(mailboxId, folderId, messageId, message); |
1102 plugin->getMessageById(mailboxId, folderId, messageId, message); |
978 if (message) { |
1103 if (message) { |
1009 } |
1135 } |
1010 |
1136 |
1011 // play alert only once per minute |
1137 // play alert only once per minute |
1012 mAlertToneAllowed = false; |
1138 mAlertToneAllowed = false; |
1013 QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone())); |
1139 QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone())); |
1014 played = true; |
1140 played = true; |
1015 } |
1141 } |
1016 |
1142 |
1017 return played; |
1143 return played; |
1018 } |
1144 } |
1019 |
1145 |
1020 /*! |
1146 /*! |
1021 Allows alert tune to be played again |
1147 Allows alert tune to be played again. |
1022 */ |
1148 */ |
1023 void NmMailAgent::enableAlertTone() |
1149 void NmMailAgent::enableAlertTone() |
1024 { |
1150 { |
1025 NM_FUNCTION; |
1151 NM_FUNCTION; |
1026 |
1152 |
1027 mAlertToneAllowed = true; |
1153 mAlertToneAllowed = true; |
1028 } |
1154 } |
1029 |
1155 |
1030 /*! |
1156 /*! |
1031 update send indicator according to outbox state |
1157 Update send indicator according to outbox state. |
1032 */ |
1158 */ |
1033 void NmMailAgent::updateSendIndicator() |
1159 void NmMailAgent::updateSendIndicator() |
1034 { |
1160 { |
1035 NM_FUNCTION; |
1161 NM_FUNCTION; |
1036 |
1162 |
1037 // Get number of mails in outboxes |
1163 // Get number of mails in outboxes |
1038 int outboxMails = 0; |
1164 int outboxMails(0); |
1039 foreach (NmMailboxInfo *mailboxInfo, mMailboxes) { |
1165 foreach (NmMailboxInfo *mailboxInfo, mMailboxes) { |
1040 outboxMails += mailboxInfo->mOutboxMails; |
1166 outboxMails += mailboxInfo->mOutboxMails; |
1041 } |
1167 } |
1042 |
1168 |
1043 if( outboxMails == 0 ) { |
1169 if( outboxMails == 0 ) { |
1044 mIndicator->deactivate(NmSendIndicatorName); |
1170 mIndicator->deactivate(NmSendIndicatorName); |
1045 } |
1171 } |
1046 else if (outboxMails > mLastOutboxCount) { |
1172 else if (outboxMails > mLastOutboxCount) { |
1047 // New mails detected in outbox |
1173 // New mails detected in outbox |
1048 |
1174 |
1049 // indicator will disappear automatically after a delay |
1175 // indicator will disappear automatically after a delay |
1050 mIndicator->activate(NmSendIndicatorName); |
1176 mIndicator->activate(NmSendIndicatorName); |
1051 } |
1177 } |
1052 |
1178 |
1053 mLastOutboxCount = outboxMails; |
1179 mLastOutboxCount = outboxMails; |
1054 } |
1180 } |
1055 |
1181 |
1056 /*! |
1182 /*! |
1057 Store the mailbox active information to permanent storage |
1183 Store the mailbox active information to permanent storage. |
1058 \param mailboxId id of the mailbox |
1184 |
|
1185 \param mailboxId Id of the mailbox |
1059 \param active true if the mailbox is active |
1186 \param active true if the mailbox is active |
1060 */ |
1187 */ |
1061 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active) |
1188 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active) |
1062 { |
1189 { |
1063 NM_FUNCTION; |
1190 NM_FUNCTION; |
1064 |
1191 |
1065 if (mSettingManager) { |
1192 if (mSettingManager) { |