emailservices/nmailagent/src/nmmailagent.cpp
changeset 65 478bc57ad291
parent 59 16ed8d08d0b1
child 72 64e38f08e49c
equal deleted inserted replaced
59:16ed8d08d0b1 65:478bc57ad291
    56     return ((settingKey.target() == cenrepSettingKey.target()) &&
    56     return ((settingKey.target() == cenrepSettingKey.target()) &&
    57         (settingKey.uid() == cenrepSettingKey.uid()) &&
    57         (settingKey.uid() == cenrepSettingKey.uid()) &&
    58         (settingKey.key() == cenrepSettingKey.key()));
    58         (settingKey.key() == cenrepSettingKey.key()));
    59 }
    59 }
    60 
    60 
    61 
       
    62 
       
    63 /*!
    61 /*!
    64     \class NmMailboxInfo
    62     \class NmMailboxInfo
    65 
    63 
    66     \brief Main class for storing mailbox data.
    64     \brief Main class for storing mailbox data.
    67  */
    65  */
    74   mConnectState(Disconnected),
    72   mConnectState(Disconnected),
    75   mInboxCreatedMessages(0),
    73   mInboxCreatedMessages(0),
    76   mInboxChangedMessages(0),
    74   mInboxChangedMessages(0),
    77   mInboxDeletedMessages(0),
    75   mInboxDeletedMessages(0),
    78   mOutboxMails(0),
    76   mOutboxMails(0),
    79   mActive(false),
    77   mActive(false)
    80   mInboxActive(false)
       
    81   {
    78   {
    82       NM_FUNCTION;
    79       NM_FUNCTION;
    83   }
    80   }
    84 
    81 
       
    82 
    85 /*!
    83 /*!
    86     \class NmMailAgent
    84     \class NmMailAgent
    87 
    85 
    88     \brief Main class for receiving email events. Activates and deactivates indicator.
    86     \brief Main class for receiving email events. Activates and deactivates
    89  */
    87            the indicator.
    90 NmMailAgent::NmMailAgent() :
    88 */
    91      mIndicator(NULL),
    89 NmMailAgent::NmMailAgent()
    92      mSystemTone(NULL),
    90 : mIndicator(NULL),
    93      mPluginFactory(NULL),
    91   mPluginFactory(NULL),
    94      mVibra(NULL),
    92   mVibra(NULL),
    95      mAlertToneAllowed(true),
    93   mSettingManager(NULL),
    96      mLastOutboxCount(0),
    94   mUiEventsNotifier(NULL),
    97      mUnreadIndicatorActive(false),
    95   mAlertToneAllowed(true),
    98      mSettingManager(NULL),
    96   mLastOutboxCount(0),
    99      mSilenceMode(NmSilenceModeOn)  // by default silent mode is on
    97   mUnreadIndicatorActive(false),
   100      {
    98   mSilenceMode(NmSilenceModeOn)  // by default silent mode is on
   101      NM_FUNCTION;
    99 {
   102      }
   100     NM_FUNCTION;
       
   101 }
       
   102 
   103 
   103 
   104 /*!
   104 /*!
   105     Delayed start.
   105     Delayed start.
   106  */
   106  */
   107 void NmMailAgent::delayedStart()
   107 void NmMailAgent::delayedStart()
   116 
   116 
   117 /*!
   117 /*!
   118     Initialise the agent.
   118     Initialise the agent.
   119 
   119 
   120     \return true if succesfully started.
   120     \return true if succesfully started.
   121  */
   121 */
   122 bool NmMailAgent::init()
   122 bool NmMailAgent::init()
   123 {
   123 {
   124     NM_FUNCTION;
   124     NM_FUNCTION;
   125 
   125 
   126     mPluginFactory = NmDataPluginFactory::instance();
   126     mPluginFactory = NmDataPluginFactory::instance();
   149     // that no tone is played if silence mode is turned on at somepoint.
   149     // that no tone is played if silence mode is turned on at somepoint.
   150     if (!monitoring) {
   150     if (!monitoring) {
   151         mSilenceMode = NmSilenceModeOn;
   151         mSilenceMode = NmSilenceModeOn;
   152     }
   152     }
   153 
   153 
   154     delete mSystemTone;
       
   155     mSystemTone = NULL;
       
   156     mSystemTone = new XQSystemToneService();
       
   157 
       
   158     delete mIndicator;
   154     delete mIndicator;
   159     mIndicator = NULL;
   155     mIndicator = NULL;
   160     mIndicator = new HbIndicator();
   156     mIndicator = new HbIndicator();
   161     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap &)),
   157     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap &)),
   162         this, SLOT(indicatorActivated(const QString &, const QVariantMap &)));
   158         this, SLOT(indicatorActivated(const QString &, const QVariantMap &)));
   189     // Construct the vibra interface instance.
   185     // Construct the vibra interface instance.
   190     delete mVibra;
   186     delete mVibra;
   191     mVibra = NULL;
   187     mVibra = NULL;
   192     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
   188     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
   193 
   189 
       
   190     // Start monitoring the UI events.
       
   191     delete mUiEventsNotifier;
       
   192     mUiEventsNotifier = NULL;
       
   193     mUiEventsNotifier = new NmUiEventsNotifier(this);
       
   194 
       
   195     connect(mUiEventsNotifier,
       
   196             SIGNAL(viewStateChanged(NmUiEventsNotifier::NmUiEventType,
       
   197                                     NmUiViewId, NmId)),
       
   198             this,
       
   199             SLOT(handleViewStateChangedEvent(const NmUiEventsNotifier::NmUiEventType,
       
   200                                              const NmUiViewId, const NmId)));
       
   201 
   194     // load all current mailboxes
   202     // load all current mailboxes
   195     initMailboxStatus();
   203     initMailboxStatus();
   196 
   204 
   197     return true;
   205     return true;
   198 }
   206 }
   199 
   207 
   200 /*!
   208 /*!
   201     Destructor of NmMailAgent.
   209     Destructor of NmMailAgent.
   202  */
   210 */
   203 NmMailAgent::~NmMailAgent()
   211 NmMailAgent::~NmMailAgent()
   204 {
   212 {
   205     NM_FUNCTION;
   213     NM_FUNCTION;
   206 
   214 
   207     delete mIndicator;
   215     delete mIndicator;
   208     delete mVibra;
   216     delete mVibra;
   209     delete mSystemTone;
   217 
   210     if (mSettingManager) {
   218     if (mSettingManager) {
   211         mSettingManager->stopMonitoring(NmSilenceModeKey);
   219         mSettingManager->stopMonitoring(NmSilenceModeKey);
   212         delete mSettingManager;
   220         delete mSettingManager;
   213     }
   221     }
       
   222 
   214     qDeleteAll(mMailboxes);
   223     qDeleteAll(mMailboxes);
       
   224 
       
   225     delete mUiEventsNotifier;
       
   226     mUiEventsNotifier = NULL;
   215 
   227 
   216     NmDataPluginFactory::releaseInstance(mPluginFactory);
   228     NmDataPluginFactory::releaseInstance(mPluginFactory);
   217 }
   229 }
   218 
   230 
   219 /*!
   231 /*!
   242             const NmMailbox *mailbox = i.previous();
   254             const NmMailbox *mailbox = i.previous();
   243             if (mailbox) {
   255             if (mailbox) {
   244                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox, plugin);
   256                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox, plugin);
   245                 if (mailboxInfo) {
   257                 if (mailboxInfo) {
   246                     bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo);
   258                     bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo);
       
   259                     NM_COMMENT(QString("Mailbox %1 initial state: newUnread=%2 total=%3").
       
   260                         arg(mailboxInfo->mName).
       
   261                         arg(mailboxInfo->mNewUnreadMailIdList.count()).
       
   262                         arg(mailboxInfo->mMailIdList.count()));
       
   263 
   247                     bool wasActive = isMailboxActive(mailbox->id());
   264                     bool wasActive = isMailboxActive(mailbox->id());
   248                     if (activate) {
   265                     if (!wasActive) {
   249                         if (!wasActive) {
   266                         // 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
   267                         activate = false;
   251                             activate = false;
   268                         mailboxInfo->mNewUnreadMailIdList.clear();
   252                         }
       
   253                         else {
       
   254                             // otherwise, activate the mailbox and show inbox state
       
   255                             mailboxInfo->mInboxActive = true;
       
   256                         }
       
   257                     }
   269                     }
   258 
   270 
   259                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id(),
   271                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id(),
   260                         mailboxInfo->mOutboxFolderId);
   272                         mailboxInfo->mOutboxFolderId);
   261                     if (mailboxInfo->mOutboxMails > 0 && wasActive) {
   273                     if (mailboxInfo->mOutboxMails > 0 && wasActive) {
   298 
   310 
   299         // get list of messages in inbox
   311         // get list of messages in inbox
   300         QList<NmMessageEnvelope*> messageList;
   312         QList<NmMessageEnvelope*> messageList;
   301         plugin->listMessages(mailboxId, inboxId, messageList);
   313         plugin->listMessages(mailboxId, inboxId, messageList);
   302 
   314 
   303         QList<NmId> newUnreadMessageIdList;
   315         QList<NmId> newMessageIdList;
       
   316 
       
   317         int unreadCount(0);
   304         foreach (const NmMessageEnvelope* envelope, messageList) {
   318         foreach (const NmMessageEnvelope* envelope, messageList) {
   305             // if the message is not read, it is "unread"
   319             // if the message is not read, it is "unread"
   306             if (!envelope->isRead()) {
   320             quint64 messageId = envelope->messageId().id();
   307                 quint64 messageId = envelope->messageId().id();
   321             newMessageIdList.append(envelope->messageId());
   308                 newUnreadMessageIdList.append(envelope->messageId());
   322             bool read = envelope->isRead();
       
   323 
       
   324             // This is a new unread mail
       
   325             if (!read) {
       
   326                 unreadCount++;
       
   327 
       
   328                 // Iterate through all known ids. If the id can't be found the mail is new.
   309                 bool found(false);
   329                 bool found(false);
   310                 // Iterate through all known ids. If the id can't be found the mail is new.
   330                 foreach (const NmId id, mailboxInfo.mMailIdList) {
   311                 foreach (const NmId id, mailboxInfo.mUnreadMailIdList) {
       
   312                     if (id.id() == messageId) {
   331                     if (id.id() == messageId) {
   313                         found = true;
   332                         found = true;
   314                         break;
   333                         break;
   315                     }
   334                     }
   316                 }
   335                 }
   317 
   336 
       
   337                 // it was new unread message
   318                 if (!found) {
   338                 if (!found) {
   319                     newUnreadMessages++;
   339 					if (mailboxInfo.mNewUnreadMailIdList.indexOf(messageId)<0) {
       
   340                         mailboxInfo.mNewUnreadMailIdList.append(messageId);
       
   341                         newUnreadMessages++;
       
   342 					}
   320                 }
   343                 }
   321             }
   344             }
       
   345             else {
       
   346                 // message is now 'read' - make sure it is no longer in list new unread mails
       
   347                 mailboxInfo.mNewUnreadMailIdList.removeAll(messageId);
       
   348             }
   322         }
   349         }
   323         qDeleteAll(messageList);
   350         qDeleteAll(messageList);
   324 
   351 
   325         // Save updated list of unread message IDs
   352         // Save updated list of unread message IDs
   326         mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList;
   353         mailboxInfo.mMailIdList = newMessageIdList;
   327     }
   354 
   328     NM_COMMENT(QString("NmMailAgent::getUnreadCount(): count=%1, new=%2").
   355         NM_COMMENT(QString("NmMailAgent::getUnreadCount(): totalCount=%1, newUnread=%2").
   329         arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages));
   356             arg(mailboxInfo.mMailIdList.count()).
       
   357             arg(newUnreadMessages));
       
   358     }
   330 
   359 
   331     return (newUnreadMessages > 0);
   360     return (newUnreadMessages > 0);
   332 }
   361 }
   333 
   362 
   334 /*!
   363 /*!
   356 
   385 
   357     return count;
   386     return count;
   358 }
   387 }
   359 
   388 
   360 /*!
   389 /*!
   361     Get list of unread counts in active mailboxes.
   390     Get list of unread counts in all active mailboxes.
   362 
   391 
   363 	\returns total number of unread mails
   392 	\returns total number of unread mails
   364  */
   393  */
   365 int NmMailAgent::getTotalUnreadCount() const
   394 int NmMailAgent::getTotalUnreadCount() const
   366 {
   395 {
   367     NM_FUNCTION;
   396     NM_FUNCTION;
   368 
   397 
   369     int unreads = 0;
   398     int unreads = 0;
   370     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   399     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   371         if (mailbox->mActive && mailbox->mInboxActive) {
   400         if (mailbox->mActive) {
   372             unreads += mailbox->mUnreadMailIdList.count();
   401             unreads += mailbox->mNewUnreadMailIdList.count();
   373         }
   402         }
   374     }
   403     }
   375     return unreads;
   404     return unreads;
   376 }
   405 }
   377 
   406 
   385     NM_FUNCTION;
   414     NM_FUNCTION;
   386 
   415 
   387     int unreads = getTotalUnreadCount();
   416     int unreads = getTotalUnreadCount();
   388     return updateUnreadIndicator(unreads>0);
   417     return updateUnreadIndicator(unreads>0);
   389 }
   418 }
       
   419 
       
   420 /*!
       
   421     Update mailbox visibility according to current status
       
   422 */
       
   423 bool NmMailAgent::updateMailboxState(const NmId &mailboxId) 
       
   424 {
       
   425     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   426     bool shown = false;
       
   427     
       
   428     if( mailboxInfo->mActive && 
       
   429         (mailboxInfo->mNewUnreadMailIdList.count() > 0 ||
       
   430          mailboxInfo->mOutboxMails>0)) {
       
   431         shown = true;
       
   432     }
       
   433     
       
   434     return updateMailboxState(mailboxId,shown,false);
       
   435 }
       
   436 
   390 
   437 
   391 /*!
   438 /*!
   392     Update the mailbox visibility and status.
   439     Update the mailbox visibility and status.
   393 
   440 
   394     \param mailboxId id of the mailbox
   441     \param mailboxId id of the mailbox
   450         arg(mailboxInfo.mIndicatorIndex);
   497         arg(mailboxInfo.mIndicatorIndex);
   451 
   498 
   452     QList<QVariant> list;
   499     QList<QVariant> list;
   453     list.append(mailboxInfo.mId.id());
   500     list.append(mailboxInfo.mId.id());
   454     list.append(mailboxInfo.mName);
   501     list.append(mailboxInfo.mName);
   455     list.append(mailboxInfo.mUnreadMailIdList.count());
   502     list.append(mailboxInfo.mNewUnreadMailIdList.count());
   456     list.append(mailboxInfo.mSyncState);
   503     list.append(mailboxInfo.mSyncState);
   457     list.append(mailboxInfo.mConnectState);
   504     list.append(mailboxInfo.mConnectState);
   458     list.append(mailboxInfo.mOutboxMails);
   505     list.append(mailboxInfo.mOutboxMails);
   459     list.append(mailboxInfo.mIconName);
   506     list.append(mailboxInfo.mIconName);
   460     list.append(mLastOutboxCount);
   507     list.append(mLastOutboxCount);
   480 bool NmMailAgent::updateUnreadIndicator(bool active)
   527 bool NmMailAgent::updateUnreadIndicator(bool active)
   481 {
   528 {
   482     NM_FUNCTION;
   529     NM_FUNCTION;
   483 
   530 
   484     bool activated = false;
   531     bool activated = false;
   485     if (active != mUnreadIndicatorActive) {
   532     bool ok;
   486         if (active) {
   533     if (active) {
   487             mIndicator->activate(NmUnreadIndicatorName);
   534         ok = mIndicator->activate(NmUnreadIndicatorName);
   488             activated = true;
   535         activated = true;
   489         }
   536     }
   490         else {
   537     else {
   491             mIndicator->deactivate(NmUnreadIndicatorName);
   538         ok = mIndicator->deactivate(NmUnreadIndicatorName);
   492         }
   539     }
       
   540 
       
   541     // update the state only if the activation/deactivation was successful
       
   542     if (ok) {
   493         mUnreadIndicatorActive = active;
   543         mUnreadIndicatorActive = active;
   494     }
   544     }
   495 
   545 
   496     return activated;
   546     return activated;
   497 }
   547 }
   526     Handles message created event.
   576     Handles message created event.
   527 
   577 
   528     \param folderId Id of the folder that includes the message
   578     \param folderId Id of the folder that includes the message
   529     \param messageIds Message ids that are checked
   579     \param messageIds Message ids that are checked
   530     \param mailboxId Id of the mailbox that includes the message
   580     \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  */
   581  */
   534 void NmMailAgent::handleMessageCreatedEvent(const NmId &folderId, const QList<NmId> &messageIds,
   582 void NmMailAgent::handleMessageCreatedEvent(const NmId &folderId, const QList<NmId> &messageIds,
   535     const NmId &mailboxId, bool &updateNeeded, bool &activate)
   583     const NmId &mailboxId)
   536 {
   584 {
   537     NM_FUNCTION;
   585     NM_FUNCTION;
   538 
   586 
   539     // Check the new messages to make the indicator appear earlier
   587     // Check the new messages to make the indicator appear earlier
   540     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   588     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   549                 plugin->getStandardFolderId(mailboxId, NmFolderInbox);
   597                 plugin->getStandardFolderId(mailboxId, NmFolderInbox);
   550         }
   598         }
   551     }
   599     }
   552 
   600 
   553     if (folderId == mailboxInfo->mInboxFolderId) {
   601     if (folderId == mailboxInfo->mInboxFolderId) {
       
   602         mailboxInfo->mInboxCreatedMessages += messageIds.count();
       
   603 
   554         foreach (NmId messageId, messageIds) {
   604         foreach (NmId messageId, messageIds) {
   555             bool messageUnread = false;
   605             bool messageUnread = false;
   556 
   606             mailboxInfo->mMailIdList.append(messageId);
   557             // Check the message if we can either play a tone or if the "@" is
   607 
   558             // not visible at the moment
   608             // double check that the message ID is really new
   559             if (mAlertToneAllowed || !mUnreadIndicatorActive) {
   609             bool newMessage = true;
       
   610             if (mailboxInfo->mNewUnreadMailIdList.indexOf(messageId)>=0) {
       
   611                 newMessage = false;
       
   612             }
       
   613 
       
   614             // If it is a new message, update the mailbox status
       
   615             if (newMessage) {
   560                 if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   616                 if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   561                     if (messageUnread) {
   617                     if (messageUnread) {
   562                         mailboxInfo->mUnreadMailIdList.append(messageId);
   618                         mailboxInfo->mNewUnreadMailIdList.append(messageId);
   563                         mailboxInfo->mInboxActive = true;
   619                         NM_COMMENT(QString(" new unread messages: count=%1").
       
   620                             arg(mailboxInfo->mNewUnreadMailIdList.count()));
       
   621 
       
   622                         if (!mUnreadIndicatorActive) {
       
   623                             // make the "@" appear immediatelly
       
   624                             updateUnreadIndicator(true);
       
   625                         }
   564                         updateMailboxState(mailboxId, true, false);
   626                         updateMailboxState(mailboxId, true, false);
   565 
       
   566                         // make the "@" appear immediatelly
       
   567                         updateUnreadIndicator(true);
       
   568 
   627 
   569                         // Play the tone as well
   628                         // Play the tone as well
   570                         playAlertTone();
   629                         playAlertTone();
   571                     }
   630                     }
   572                 }
   631                 }
   573             }
   632             }
   574         }
   633         }
   575     }
       
   576 
       
   577     if (folderId==mailboxInfo->mInboxFolderId) {
       
   578         mailboxInfo->mInboxCreatedMessages += messageIds.count();
       
   579     }
   634     }
   580 
   635 
   581     // When created a new mail in the outbox, we are in sending state
   636     // When created a new mail in the outbox, we are in sending state
   582     if (mailboxInfo->mOutboxFolderId == folderId) {
   637     if (mailboxInfo->mOutboxFolderId == folderId) {
   583         // The first mail created in the outbox
   638         // The first mail created in the outbox
   584         if (mailboxInfo->mOutboxMails <= 0) {
   639         if (mailboxInfo->mOutboxMails <= 0) {
   585             NM_COMMENT("NmMailAgent: first mail in outbox");
   640             NM_COMMENT("NmMailAgent: first mail in outbox");
   586         }
   641         }
   587         // Always activate the indicator
       
   588         activate = true;
       
   589         updateNeeded = true;
       
   590 
       
   591         mailboxInfo->mOutboxMails += messageIds.count();
   642         mailboxInfo->mOutboxMails += messageIds.count();
       
   643         
       
   644         updateMailboxState(mailboxId,true,true);
   592         updateSendIndicator();
   645         updateSendIndicator();
   593     }
   646     }
   594 }
   647 }
   595 
   648 
   596 
   649 
   599     \sa updateUnreadCount
   652     \sa updateUnreadCount
   600 
   653 
   601     \param folderId Id of the folder that includes the message
   654     \param folderId Id of the folder that includes the message
   602     \param messageIds Message ids that are checked
   655     \param messageIds Message ids that are checked
   603     \param mailboxId Id of the mailbox that includes the message
   656     \param mailboxId Id of the mailbox that includes the message
   604     \param updateNeeded Set to <code>true</code> if update needed otherwise not touched
   657  */
   605     \param activate Set result value of updateUnreadCount method
   658 void NmMailAgent::handleMessageChangedEvent(const NmId &folderId,
   606  */
       
   607 void NmMailAgent::handleMessageChangedEvent(const NmId &folderId, 
       
   608     const QList<NmId> &messageIds,
   659     const QList<NmId> &messageIds,
   609     const NmId &mailboxId,
   660     const NmId &mailboxId)
   610     bool &updateNeeded, bool &activate)
   661 {
   611 {
   662     NM_FUNCTION;
       
   663 
   612     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   664     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   613 
   665 
   614     // we are interested only about changes in the inbox
   666     // we are interested only about changes in the inbox
   615     if (folderId == mailboxInfo->mInboxFolderId) {
   667     if (mailboxInfo && folderId == mailboxInfo->mInboxFolderId) {
   616         mailboxInfo->mInboxChangedMessages++;
   668         mailboxInfo->mInboxChangedMessages++;
   617 
   669 
   618         // If not currently syncronizing the mailbox, this may mean
   670         if (mailboxInfo->mNewUnreadMailIdList.count()>0) {
   619         // that a message was read/unread
   671             bool updateMailbox = false;
   620         if (mailboxInfo && mailboxInfo->mSyncState == SyncComplete) {
   672 
   621             // If there was no unread mails in the list, do nothing
   673             // Check how many messages was in the list of new unread mails
   622             int oldCount(mailboxInfo->mUnreadMailIdList.count());
   674             foreach (NmId messageId, messageIds) {
   623             if (oldCount>0) {
   675                 if (mailboxInfo->mNewUnreadMailIdList.indexOf(messageId)>=0) {
   624                 // Check how many messages were unread earlier
   676                     bool messageUnread(false);
   625                 int unreadMessages = 0;
   677                     if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   626                 foreach (NmId messageId, messageIds) {
   678                         // Message is no longer unread
   627                     if (mailboxInfo->mUnreadMailIdList.indexOf(messageId)>=0) {
   679                         if (!messageUnread) {
   628                         unreadMessages++;
   680                             mailboxInfo->mNewUnreadMailIdList.removeAll(messageId);
       
   681                             updateMailbox = true;
       
   682                         }
   629                     }
   683                     }
   630                 }
   684                 }
   631 
   685             }
   632                 // Could lead the count drop to 0 => mailbox will be hidden
   686             
   633                 if (unreadMessages > 0) {
   687             if (updateMailbox) {
   634                     // check the unread status again
   688                 updateMailboxState(mailboxId);
   635                     activate = updateUnreadCount(mailboxId, *mailboxInfo);
   689                 updateUnreadIndicator();
   636 
       
   637                     // no more unread mails in the inbox
       
   638                     if (mailboxInfo->mUnreadMailIdList.count()==0) {
       
   639                         NM_COMMENT("NmMailAgent:handleMessageChangedEvent - no more unread msgs");
       
   640                         updateNeeded = true;
       
   641                     }
       
   642                 }
       
   643             }
   690             }
   644         }
   691         }
   645     }
   692     }
   646 }
   693 }
   647 
   694 
   649     Handles message deleted event.
   696     Handles message deleted event.
   650 
   697 
   651     \param folderId Id of the folder that includes the message
   698     \param folderId Id of the folder that includes the message
   652     \param messageIds Message ids that are checked
   699     \param messageIds Message ids that are checked
   653     \param mailboxId Id of the mailbox that includes the message
   700     \param mailboxId Id of the mailbox that includes the message
   654     \param updateNeeded Set to <code>true</code> if update needed otherwise not touched
       
   655     \param activate Set to <code>true</code> if activation needed otherwise not touched
       
   656  */
   701  */
   657 void NmMailAgent::handleMessageDeletedEvent(const NmId &folderId, const QList<NmId> &messageIds,
   702 void NmMailAgent::handleMessageDeletedEvent(const NmId &folderId, const QList<NmId> &messageIds,
   658     const NmId &mailboxId, bool &updateNeeded, bool &activate)
   703     const NmId &mailboxId)
   659 {
   704 {
       
   705     NM_FUNCTION;
       
   706     
   660     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   707     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   661 
   708 
   662     if (folderId == mailboxInfo->mInboxFolderId) {
   709     if (mailboxInfo && folderId == mailboxInfo->mInboxFolderId) {
   663         mailboxInfo->mInboxDeletedMessages++;
   710         mailboxInfo->mInboxDeletedMessages++;
       
   711 
       
   712         // Clear the IDs from 'new unread messages' list
       
   713         foreach (NmId messageId, messageIds) {
       
   714             mailboxInfo->mNewUnreadMailIdList.removeAll(messageId);
       
   715         }
       
   716 
       
   717         // All new unread messages are now deleted
       
   718         if (mailboxInfo->mNewUnreadMailIdList.count()==0) {
       
   719             NM_COMMENT(" No more new unread messages");
       
   720             updateUnreadIndicator();
       
   721             updateMailboxState(mailboxId);
       
   722         }
   664     }
   723     }
   665 
   724 
   666     // Deleted mails from the outbox
   725     // Deleted mails from the outbox
   667     if (mailboxInfo->mOutboxFolderId == folderId) {
   726     if (mailboxInfo->mOutboxFolderId == folderId) {
   668         mailboxInfo->mOutboxMails -= messageIds.count();
   727         mailboxInfo->mOutboxMails -= messageIds.count();
   670         // Sanity check for the outbox count
   729         // Sanity check for the outbox count
   671         if (mailboxInfo->mOutboxMails < 0) {
   730         if (mailboxInfo->mOutboxMails < 0) {
   672             mailboxInfo->mOutboxMails = 0;
   731             mailboxInfo->mOutboxMails = 0;
   673         }
   732         }
   674 
   733 
   675         // The last mail was now deleted
   734         // The last mail was now deleted from outbox
   676         if (mailboxInfo->mOutboxMails == 0) {
   735         if (mailboxInfo->mOutboxMails == 0) {
   677             NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   736             NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   678             updateNeeded = true;
   737             updateSendIndicator();
   679 
   738         }
   680             // Keep it active if there is unread mails and inbox is still active
   739         updateMailboxState(mailboxId);
   681             if (mailboxInfo->mInboxActive &&
       
   682                 mailboxInfo->mUnreadMailIdList.count() > 0) {
       
   683                 activate = true;
       
   684             }
       
   685         }
       
   686         else {
       
   687             // Also update the indicator status if it is already shown
       
   688             if (mailboxInfo->mActive) {
       
   689                 activate = true;
       
   690                 updateNeeded = true;
       
   691             }
       
   692         }
       
   693         updateSendIndicator();
       
   694     }
   740     }
   695 }
   741 }
   696 
   742 
   697 /*!
   743 /*!
   698     Get next free indicator index, starting from 0.
   744     Get next free indicator index, starting from 0.
   798 
   844 
   799     // create and subscribe to the mailbox, if not done earlier
   845     // create and subscribe to the mailbox, if not done earlier
   800     getMailboxInfo(mailboxId);
   846     getMailboxInfo(mailboxId);
   801 }
   847 }
   802 
   848 
       
   849 
       
   850 /*!
       
   851     Handles the view state changed UI event. The event notification is received
       
   852     via NmUiEventsNotifier.
       
   853 
       
   854     \param eventType The type of the UI event.
       
   855     \param viewId The ID of the view the event concerns.
       
   856     \param mailboxId The ID of the mailbox related to the UI event.
       
   857 */
       
   858 void NmMailAgent::handleViewStateChangedEvent(
       
   859     const NmUiEventsNotifier::NmUiEventType eventType,
       
   860     const NmUiViewId viewId,
       
   861     const NmId mailboxId)
       
   862 {
       
   863     if (eventType == NmUiEventsNotifier::NmViewShownEvent &&
       
   864         viewId == NmUiViewMessageList) {
       
   865         // Get the mailbox info.
       
   866         NmMailboxInfo *info = getMailboxInfo(mailboxId);
       
   867 
       
   868         if (info) {
       
   869             // The message list view was shown. If the indicator of the mailbox
       
   870             // in question is active, deactivate it.
       
   871             updateMailboxState(info->mId, false, false);  
       
   872             resetMailboxState(info);
       
   873         }
       
   874     }
       
   875 }
       
   876 
       
   877 
   803 /*!
   878 /*!
   804    Map the type name to mailbox info.
   879    Map the type name to mailbox info.
   805 
   880 
   806    \param type Indicator type name
   881    \param type Indicator type name
   807    \return NULL if no mailbox match the type
   882    \return NULL if no mailbox match the type
   825     }
   900     }
   826     return foundMailbox;
   901     return foundMailbox;
   827 }
   902 }
   828 
   903 
   829 /*!
   904 /*!
       
   905     Clear the mailbox state to be 'seen'
       
   906     \param info mailbox that will be reseted
       
   907  */
       
   908 void NmMailAgent::resetMailboxState(NmMailboxInfo *info)
       
   909 {
       
   910     NM_FUNCTION;
       
   911     
       
   912     info->mActive = false; // indicator is no longer active
       
   913     info->mNewUnreadMailIdList.clear(); // no mails are no longer 'new'
       
   914     storeMailboxActive(info->mId, false);
       
   915     updateUnreadIndicator();
       
   916 }
       
   917 
       
   918 /*!
   830     Called when indicator is clicked from the indicator menu
   919     Called when indicator is clicked from the indicator menu
   831     - indicator will be hide from the menu
   920     - indicator will be hide from the menu
   832     - mailbox will be launched
   921     - mailbox will be launched
   833 
   922 
   834     \param type Indicator type name
   923     \param type Indicator type name
   840     Q_UNUSED(data);
   929     Q_UNUSED(data);
   841 
   930 
   842     // map the indicator type to mailbox
   931     // map the indicator type to mailbox
   843     NmMailboxInfo *info = getMailboxByType(type);
   932     NmMailboxInfo *info = getMailboxByType(type);
   844     if (info) {
   933     if (info) {
   845         info->mActive = false; // indicator is no longer active
   934         resetMailboxState(info);
   846         info->mInboxActive = false; // also inbox does not activate the indicator
       
   847         storeMailboxActive(info->mId, false);
       
   848         updateUnreadIndicator();
       
   849 
   935 
   850         launchMailbox(info->mId.id());
   936         launchMailbox(info->mId.id());
   851     }
   937     }
   852 }
   938 }
   853 
   939 
   884 {
   970 {
   885     NM_FUNCTION;
   971     NM_FUNCTION;
   886     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
   972     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
   887         arg(event).arg(mailboxId.id()));
   973         arg(event).arg(mailboxId.id()));
   888 
   974 
   889     bool updateNeeded = false;
       
   890     bool activate = false;
       
   891 
       
   892     switch (event) {
   975     switch (event) {
   893         case NmMessageCreated: {
   976         case NmMessageCreated: {
   894             handleMessageCreatedEvent(folderId, messageIds, mailboxId, updateNeeded, activate);
   977             handleMessageCreatedEvent(folderId, messageIds, mailboxId);
   895             break;
   978             break;
   896         }
   979         }
   897         case NmMessageChanged: {
   980         case NmMessageChanged: {
   898             handleMessageChangedEvent(folderId, messageIds, mailboxId, updateNeeded, activate);
   981             handleMessageChangedEvent(folderId, messageIds, mailboxId);
   899             break;
   982             break;
   900         }
   983         }
   901         case NmMessageDeleted: {
   984         case NmMessageDeleted: {
   902             handleMessageDeletedEvent(folderId, messageIds, mailboxId, updateNeeded, activate);
   985             handleMessageDeletedEvent(folderId, messageIds, mailboxId);
   903             break;
   986             break;
   904         }
   987         }
   905         default:
   988         default:
   906             break;
   989             break;
   907     }
       
   908 
       
   909     if (updateNeeded) {
       
   910         updateUnreadIndicator();
       
   911 
       
   912         NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   913         updateMailboxState(mailboxId,
       
   914             activate, true /* force refresh */);
       
   915     }
   990     }
   916 }
   991 }
   917 
   992 
   918 /*!
   993 /*!
   919     Received from NmFrameworkAdapter syncStateEvent signal.
   994     Received from NmFrameworkAdapter syncStateEvent signal.
   930         arg(state).arg(event.mMailboxId.id()));
  1005         arg(state).arg(event.mMailboxId.id()));
   931 
  1006 
   932     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
  1007     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
   933     if (info) {
  1008     if (info) {
   934         info->mSyncState = state;
  1009         info->mSyncState = state;
   935 
       
   936         if (state==Synchronizing) {
       
   937             // Reset counters when sync is started
       
   938             info->mInboxCreatedMessages = 0;
       
   939             info->mInboxChangedMessages = 0;
       
   940             info->mInboxDeletedMessages = 0;
       
   941         }
       
   942         else if (state==SyncComplete) {
       
   943             // Check the unread status here again
       
   944             bool updateMailbox = updateUnreadCount(event.mMailboxId, *info);
       
   945             int oldOutboxCount = info->mOutboxMails;
       
   946             info->mOutboxMails = getOutboxCount(event.mMailboxId, info->mOutboxFolderId);
       
   947             int unreadMailCount(info->mUnreadMailIdList.count());
       
   948             if (info->mOutboxMails > oldOutboxCount || unreadMailCount == 0) {
       
   949                 // new mails in outbox
       
   950                 updateMailbox = true;
       
   951             }
       
   952             bool active = unreadMailCount || info->mOutboxMails;
       
   953 
       
   954             // Refresh the indicator if messages created or changed
       
   955             NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): "
       
   956                 "created=%1, changed=%2, deleted=%3").
       
   957                 arg(info->mInboxCreatedMessages).
       
   958                 arg(info->mInboxChangedMessages).
       
   959                 arg(info->mInboxDeletedMessages));
       
   960             bool refresh = (info->mInboxCreatedMessages > 0) || (info->mInboxChangedMessages > 0);
       
   961 
       
   962             updateUnreadIndicator();
       
   963 
       
   964             if (updateMailbox) {
       
   965                 updateMailboxState(event.mMailboxId, active, refresh);
       
   966             }
       
   967         }
       
   968     }
  1010     }
   969 }
  1011 }
   970 
  1012 
   971 /*!
  1013 /*!
   972     Received from NmFrameworkAdapter connectionState signal.
  1014     Received from NmFrameworkAdapter connectionState signal.
  1139 {
  1181 {
  1140     NM_FUNCTION;
  1182     NM_FUNCTION;
  1141     bool played(false);
  1183     bool played(false);
  1142 
  1184 
  1143     if (mAlertToneAllowed) {
  1185     if (mAlertToneAllowed) {
  1144         // Play tone only if system tone service is available and
  1186         // Play tone only when phone is not in silence mode.
  1145         // phone is not in silence mode.
  1187         if (!mSilenceMode) {
  1146         if (mSystemTone && !mSilenceMode) {
  1188             // Must instantiate it again to make sure correct tone is played  
  1147             mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
  1189             XQSystemToneService systemTone;
       
  1190             systemTone.playTone(XQSystemToneService::EmailAlertTone);
  1148         }
  1191         }
  1149 
  1192 
  1150         // Execute the vibra effect.
  1193         // Execute the vibra effect.
  1151         if (mVibra) {
  1194         if (mVibra) {
  1152             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));
  1195             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));