emailservices/nmailagent/src/nmmailagent.cpp
branchRCL_3
changeset 63 d189ee25cf9d
equal deleted inserted replaced
61:dcf0eedfc1a3 63:d189ee25cf9d
       
     1 /*
       
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: Mail agent is responsible of monitoring mail accounts. It activates
       
    15  *              and deactivates indicator depending on received events. Mail agent is
       
    16  *              also responsible of monitoring silence mode, playing alert tone and vibrating
       
    17  *              phone when new mail is received.
       
    18  *
       
    19  */
       
    20 
       
    21 #include "emailtrace.h"
       
    22 #include "nmmailagentheaders.h"
       
    23 #include "nmmailagent.h"
       
    24 
       
    25 #include <xqaiwdeclplat.h>
       
    26 #include <xqapplicationmanager.h>
       
    27 #include <xqaiwrequest.h>
       
    28 
       
    29 #include <xqsettingsmanager.h>
       
    30 #include <xqcentralrepositoryutils.h>
       
    31 #include <xqsystemtoneservice.h>
       
    32 #include <xqsettingskey.h>
       
    33 #include <ProfileEngineInternalCRKeys.h>
       
    34 
       
    35 // CONSTANTS
       
    36 const int NmAgentIndicatorNotSet = -1;
       
    37 const int NmAgentAlertToneTimer = 60000; // 60s
       
    38 const int NmAgentDefaultVibraDuration = 1000; // 1 second
       
    39 static const quint32 NmRepositoryId = 0x2002C326;
       
    40 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
       
    41 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
       
    42 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
       
    43 const XQCentralRepositorySettingsKey NmSilenceModeKey(KCRUidProfileEngine.iUid, KProEngSilenceMode);
       
    44 const int NmSilenceModeOn = 1;
       
    45 
       
    46 /*!
       
    47     Helper method for finding out if XQSettingsKey and XQCentralRepositorySettingsKey points to
       
    48     same key.
       
    49 
       
    50     \param settingKey XQSettingsKey
       
    51     \param cenrepSettingKey XQCentralRepositorySettingsKey
       
    52     \return <code>true</code> if target, uid and key matches otherwise returns <code>false</code>
       
    53  */
       
    54 bool keysEqual(const XQSettingsKey& settingKey, const XQCentralRepositorySettingsKey& cenrepSettingKey)
       
    55 {
       
    56     return ((settingKey.target() == cenrepSettingKey.target()) &&
       
    57         (settingKey.uid() == cenrepSettingKey.uid()) &&
       
    58         (settingKey.key() == cenrepSettingKey.key()));
       
    59 }
       
    60 
       
    61 /*!
       
    62     \class NmMailboxInfo
       
    63 
       
    64     \brief Main class for storing mailbox data.
       
    65  */
       
    66 NmMailboxInfo::NmMailboxInfo()
       
    67 : mId(0),
       
    68   mIndicatorIndex(NmAgentIndicatorNotSet),
       
    69   mInboxFolderId(0),
       
    70   mOutboxFolderId(0),
       
    71   mSyncState(SyncComplete),
       
    72   mConnectState(Disconnected),
       
    73   mInboxCreatedMessages(0),
       
    74   mInboxChangedMessages(0),
       
    75   mInboxDeletedMessages(0),
       
    76   mOutboxMails(0),
       
    77   mActive(false)
       
    78   {
       
    79       NM_FUNCTION;
       
    80   }
       
    81 
       
    82 
       
    83 /*!
       
    84     \class NmMailAgent
       
    85 
       
    86     \brief Main class for receiving email events. Activates and deactivates
       
    87            the indicator.
       
    88 */
       
    89 NmMailAgent::NmMailAgent()
       
    90 : mIndicator(NULL),
       
    91   mPluginFactory(NULL),
       
    92   mVibra(NULL),
       
    93   mSettingManager(NULL),
       
    94   mUiEventsNotifier(NULL),
       
    95   mAlertToneAllowed(true),
       
    96   mLastOutboxCount(0),
       
    97   mUnreadIndicatorActive(false),
       
    98   mSilenceMode(NmSilenceModeOn)  // by default silent mode is on
       
    99 {
       
   100     NM_FUNCTION;
       
   101 }
       
   102 
       
   103 
       
   104 /*!
       
   105     Delayed start.
       
   106  */
       
   107 void NmMailAgent::delayedStart()
       
   108 {
       
   109     NM_FUNCTION;
       
   110 
       
   111     if (!init()) {
       
   112         // Initialisation failed. Quit the agent.
       
   113         QCoreApplication::exit(1);
       
   114     }
       
   115 }
       
   116 
       
   117 /*!
       
   118     Initialise the agent.
       
   119 
       
   120     \return true if succesfully started.
       
   121 */
       
   122 bool NmMailAgent::init()
       
   123 {
       
   124     NM_FUNCTION;
       
   125 
       
   126     mPluginFactory = NmDataPluginFactory::instance();
       
   127     if (!mPluginFactory) {
       
   128         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
       
   129         return false;
       
   130     }
       
   131 
       
   132     // Check status of silent mode.
       
   133     delete mSettingManager;
       
   134     mSettingManager = NULL;
       
   135     mSettingManager = new XQSettingsManager();
       
   136     QVariant silenceMode = mSettingManager->readItemValue(NmSilenceModeKey,
       
   137         XQSettingsManager::TypeInt);
       
   138     mSilenceMode = silenceMode.toInt();
       
   139 
       
   140     // Start monitoring silence mode key.
       
   141     bool monitoring(mSettingManager->startMonitoring(NmSilenceModeKey, XQSettingsManager::TypeInt));
       
   142     monitoring &= connect(mSettingManager,
       
   143                           SIGNAL(valueChanged(const XQSettingsKey &, const QVariant &)),
       
   144                           this,
       
   145                           SLOT(valueChanged(const XQSettingsKey &, const QVariant &)),
       
   146                           Qt::UniqueConnection);
       
   147 
       
   148     // If silence mode monitoring can't be started, then change silence mode on to be sure
       
   149     // that no tone is played if silence mode is turned on at somepoint.
       
   150     if (!monitoring) {
       
   151         mSilenceMode = NmSilenceModeOn;
       
   152     }
       
   153 
       
   154     delete mIndicator;
       
   155     mIndicator = NULL;
       
   156     mIndicator = new HbIndicator();
       
   157     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap &)),
       
   158         this, SLOT(indicatorActivated(const QString &, const QVariantMap &)));
       
   159 
       
   160     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
       
   161 
       
   162     foreach (QObject *plugin, *plugins) {
       
   163         if (plugin) {
       
   164             // Start listening events
       
   165             connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, const QList<NmId> &)),
       
   166                     this, SLOT(handleMailboxEvent(NmMailboxEvent, const QList<NmId> &)),
       
   167                     Qt::UniqueConnection);
       
   168 
       
   169             connect(plugin, SIGNAL(messageEvent(
       
   170                         NmMessageEvent, const NmId &, const QList<NmId> &, const NmId &)),
       
   171                     this, SLOT(handleMessageEvent(
       
   172                         NmMessageEvent, const NmId &, const QList<NmId> &, const NmId &)),
       
   173                     Qt::UniqueConnection);
       
   174 
       
   175             connect(plugin, SIGNAL(syncStateEvent(NmSyncState, const NmOperationCompletionEvent &)),
       
   176                     this, SLOT(handleSyncStateEvent(NmSyncState, const NmOperationCompletionEvent &)),
       
   177                 Qt::UniqueConnection);
       
   178 
       
   179             connect(plugin, SIGNAL(connectionEvent(NmConnectState, const NmId, int)),
       
   180                     this, SLOT(handleConnectionEvent(NmConnectState, const NmId, int)),
       
   181                     Qt::UniqueConnection);
       
   182         }
       
   183     }
       
   184 
       
   185     // Construct the vibra interface instance.
       
   186     delete mVibra;
       
   187     mVibra = NULL;
       
   188     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
       
   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 
       
   202     // load all current mailboxes
       
   203     initMailboxStatus();
       
   204 
       
   205     return true;
       
   206 }
       
   207 
       
   208 /*!
       
   209     Destructor of NmMailAgent.
       
   210 */
       
   211 NmMailAgent::~NmMailAgent()
       
   212 {
       
   213     NM_FUNCTION;
       
   214 
       
   215     delete mIndicator;
       
   216     delete mVibra;
       
   217 
       
   218     if (mSettingManager) {
       
   219         mSettingManager->stopMonitoring(NmSilenceModeKey);
       
   220         delete mSettingManager;
       
   221     }
       
   222 
       
   223     qDeleteAll(mMailboxes);
       
   224 
       
   225     delete mUiEventsNotifier;
       
   226     mUiEventsNotifier = NULL;
       
   227 
       
   228     NmDataPluginFactory::releaseInstance(mPluginFactory);
       
   229 }
       
   230 
       
   231 /*!
       
   232     Initialize the mailbox list with the current state.
       
   233  */
       
   234 void NmMailAgent::initMailboxStatus()
       
   235 {
       
   236     NM_FUNCTION;
       
   237 
       
   238     QList<NmMailbox*> mailboxes;
       
   239     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
       
   240 
       
   241     foreach(QObject* pluginObject, *plugins) {
       
   242         NmDataPluginInterface *plugin =
       
   243             mPluginFactory->interfaceInstance(pluginObject);
       
   244         if (plugin) {
       
   245             plugin->listMailboxes(mailboxes);
       
   246         }
       
   247 
       
   248         // Add the indicators
       
   249         // Must be made in reverse order to show them properly in
       
   250         // HbIndicator menu
       
   251         QListIterator<NmMailbox *> i(mailboxes);
       
   252         i.toBack();
       
   253         while (i.hasPrevious()) {
       
   254             const NmMailbox *mailbox = i.previous();
       
   255             if (mailbox) {
       
   256                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox, plugin);
       
   257                 if (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 
       
   264                     bool wasActive = isMailboxActive(mailbox->id());
       
   265                     if (!wasActive) {
       
   266                         // do not activate the mailbox if it was left as hidden last time
       
   267                         activate = false;
       
   268                         mailboxInfo->mNewUnreadMailIdList.clear();
       
   269                     }
       
   270 
       
   271                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id(),
       
   272                         mailboxInfo->mOutboxFolderId);
       
   273                     if (mailboxInfo->mOutboxMails > 0 && wasActive) {
       
   274                         activate = true;
       
   275                     }
       
   276 
       
   277                     // Create indicator for visible mailboxes
       
   278                     updateMailboxState(mailbox->id(), activate, false);
       
   279                 }
       
   280             }
       
   281         }
       
   282         qDeleteAll(mailboxes);
       
   283     }
       
   284     updateUnreadIndicator();
       
   285     updateSendIndicator();
       
   286 }
       
   287 
       
   288 /*!
       
   289     Get mailbox unread count in inbox folder.
       
   290 
       
   291     \param mailboxId id of the mailbox
       
   292     \param mailboxInfo contains the list of unread messages
       
   293     \return true if new unread mails was found
       
   294  */
       
   295 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo)
       
   296 {
       
   297     NM_FUNCTION;
       
   298 
       
   299     int newUnreadMessages(0);
       
   300     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
       
   301 
       
   302     if (plugin) {
       
   303         NmId inboxId = mailboxInfo.mInboxFolderId.id();
       
   304 
       
   305         // Inbox folder ID may be still unknown
       
   306         if (inboxId == 0) {
       
   307             mailboxInfo.mInboxFolderId = plugin->getStandardFolderId(mailboxId, NmFolderInbox);
       
   308             inboxId = mailboxInfo.mInboxFolderId.id();
       
   309         }
       
   310 
       
   311         // get list of messages in inbox
       
   312         QList<NmMessageEnvelope*> messageList;
       
   313         plugin->listMessages(mailboxId, inboxId, messageList);
       
   314 
       
   315         QList<NmId> newMessageIdList;
       
   316 
       
   317         int unreadCount(0);
       
   318         foreach (const NmMessageEnvelope* envelope, messageList) {
       
   319             // if the message is not read, it is "unread"
       
   320             quint64 messageId = envelope->messageId().id();
       
   321             newMessageIdList.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.
       
   329                 bool found(false);
       
   330                 foreach (const NmId id, mailboxInfo.mMailIdList) {
       
   331                     if (id.id() == messageId) {
       
   332                         found = true;
       
   333                         break;
       
   334                     }
       
   335                 }
       
   336 
       
   337                 // it was new unread message
       
   338                 if (!found) {
       
   339 					if (mailboxInfo.mNewUnreadMailIdList.indexOf(messageId)<0) {
       
   340                         mailboxInfo.mNewUnreadMailIdList.append(messageId);
       
   341                         newUnreadMessages++;
       
   342 					}
       
   343                 }
       
   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             }
       
   349         }
       
   350         qDeleteAll(messageList);
       
   351 
       
   352         // Save updated list of unread message IDs
       
   353         mailboxInfo.mMailIdList = newMessageIdList;
       
   354 
       
   355         NM_COMMENT(QString("NmMailAgent::getUnreadCount(): totalCount=%1, newUnread=%2").
       
   356             arg(mailboxInfo.mMailIdList.count()).
       
   357             arg(newUnreadMessages));
       
   358     }
       
   359 
       
   360     return (newUnreadMessages > 0);
       
   361 }
       
   362 
       
   363 /*!
       
   364     Get mailbox count in outbox folder.
       
   365 
       
   366     \param mailboxId id of the mailbox
       
   367     \param outboxId Outbox folder Id
       
   368     \return number of mails in the outbox
       
   369  */
       
   370 int NmMailAgent::getOutboxCount(const NmId &mailboxId, const NmId &outboxId)
       
   371 {
       
   372     NM_FUNCTION;
       
   373 
       
   374     int count(0);
       
   375     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
       
   376 
       
   377     if (plugin) {
       
   378         // get list of messages in outbox
       
   379         QList<NmMessageEnvelope*> messageList;
       
   380         plugin->listMessages(mailboxId, outboxId, messageList);
       
   381         count = messageList.count();
       
   382         qDeleteAll(messageList);
       
   383     }
       
   384     NM_COMMENT(QString("NmMailAgent::getOutboxCount(): count=%1").arg(count));
       
   385 
       
   386     return count;
       
   387 }
       
   388 
       
   389 /*!
       
   390     Get list of unread counts in all active mailboxes.
       
   391 
       
   392 	\returns total number of unread mails
       
   393  */
       
   394 int NmMailAgent::getTotalUnreadCount() const
       
   395 {
       
   396     NM_FUNCTION;
       
   397 
       
   398     int unreads = 0;
       
   399     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
       
   400         if (mailbox->mActive) {
       
   401             unreads += mailbox->mNewUnreadMailIdList.count();
       
   402         }
       
   403     }
       
   404     return unreads;
       
   405 }
       
   406 
       
   407 /*!
       
   408     Update the "@" indicator state according to unread state.
       
   409 
       
   410     \return true if the indicator was activated
       
   411  */
       
   412 bool NmMailAgent::updateUnreadIndicator()
       
   413 {
       
   414     NM_FUNCTION;
       
   415 
       
   416     int unreads = getTotalUnreadCount();
       
   417     return updateUnreadIndicator(unreads>0);
       
   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 
       
   437 
       
   438 /*!
       
   439     Update the mailbox visibility and status.
       
   440 
       
   441     \param mailboxId id of the mailbox
       
   442     \param active visibility state of the mailbox
       
   443     \param refreshAlways true when the indicator should be always updated
       
   444     \return true if the mailbox state was changed
       
   445  */
       
   446 bool NmMailAgent::updateMailboxState(const NmId &mailboxId,
       
   447     bool active, bool refreshAlways)
       
   448 {
       
   449     NM_FUNCTION;
       
   450 
       
   451     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   452     bool changed = false;
       
   453     if (mailboxInfo->mActive != active ||
       
   454         refreshAlways) {
       
   455 
       
   456         // store the new state to permanent storage
       
   457         storeMailboxActive(mailboxId, active);
       
   458 
       
   459         mailboxInfo->mActive = active;
       
   460         changed = true;
       
   461         if (active) {
       
   462             // Mailbox is not yet assigned to any indicator
       
   463             if (mailboxInfo->mIndicatorIndex < 0) {
       
   464                 mailboxInfo->mIndicatorIndex = getFreeIndicatorIndex();
       
   465             }
       
   466 
       
   467             updateIndicator(true,*mailboxInfo);
       
   468         }
       
   469         else {
       
   470             // Indicator not anymore active. Release it.
       
   471             if (mailboxInfo->mIndicatorIndex>=0) {
       
   472                 updateIndicator(false,*mailboxInfo);
       
   473                 mailboxInfo->mIndicatorIndex = NmAgentIndicatorNotSet;
       
   474             }
       
   475         }
       
   476     }
       
   477     return changed;
       
   478 }
       
   479 
       
   480 /*!
       
   481     Updates indicator status.
       
   482 
       
   483     \param mailboxIndex index of the item shown in indicator menu
       
   484     \param active indicator visibility state
       
   485     \param mailboxInfo information of the mailbox
       
   486     \return true if indicator was updated with no errors
       
   487  */
       
   488 bool NmMailAgent::updateIndicator(bool active,
       
   489     const NmMailboxInfo& mailboxInfo)
       
   490 {
       
   491     NM_FUNCTION;
       
   492     NM_COMMENT(QString("NmMailAgent::updateIndicator(): index=%1, active=%2").
       
   493         arg(mailboxInfo.mIndicatorIndex).arg(active));
       
   494 
       
   495     bool ok = false;
       
   496     QString name = QString(NmMailboxIndicatorType).
       
   497         arg(mailboxInfo.mIndicatorIndex);
       
   498 
       
   499     QList<QVariant> list;
       
   500     list.append(mailboxInfo.mId.id());
       
   501     list.append(mailboxInfo.mName);
       
   502     list.append(mailboxInfo.mNewUnreadMailIdList.count());
       
   503     list.append(mailboxInfo.mSyncState);
       
   504     list.append(mailboxInfo.mConnectState);
       
   505     list.append(mailboxInfo.mOutboxMails);
       
   506     list.append(mailboxInfo.mIconName);
       
   507     list.append(mLastOutboxCount);
       
   508 
       
   509     if (active) {
       
   510         ok = mIndicator->activate(name,list);
       
   511     }
       
   512     else {
       
   513         ok = mIndicator->deactivate(name,list);
       
   514     }
       
   515 
       
   516     updateUnreadIndicator();
       
   517 
       
   518     return ok;
       
   519 }
       
   520 
       
   521 /*!
       
   522     Update the unread indicator state.
       
   523 
       
   524     \param active if true unread indicator is activated otherwise deactivated
       
   525     \return true if the indicator was activated
       
   526  */
       
   527 bool NmMailAgent::updateUnreadIndicator(bool active)
       
   528 {
       
   529     NM_FUNCTION;
       
   530 
       
   531     bool activated = false;
       
   532     bool ok;
       
   533     if (active) {
       
   534         ok = mIndicator->activate(NmUnreadIndicatorName);
       
   535         activated = true;
       
   536     }
       
   537     else {
       
   538         ok = mIndicator->deactivate(NmUnreadIndicatorName);
       
   539     }
       
   540 
       
   541     // update the state only if the activation/deactivation was successful
       
   542     if (ok) {
       
   543         mUnreadIndicatorActive = active;
       
   544     }
       
   545 
       
   546     return activated;
       
   547 }
       
   548 
       
   549 /*!
       
   550     Opens inbox view to specific mailbox.
       
   551 
       
   552     \param mailboxId Id of mailbox
       
   553     \return true if inbox is succesfully opened
       
   554  */
       
   555 bool NmMailAgent::launchMailbox(quint64 mailboxId)
       
   556 {
       
   557     NM_FUNCTION;
       
   558 
       
   559     bool ok(false);
       
   560     XQApplicationManager appManager;
       
   561     XQAiwRequest *request = appManager.create(
       
   562         XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW, false);
       
   563     // Instance might be NULL if the service is not available.
       
   564     if (request) {
       
   565         QList<QVariant> list;
       
   566         list.append(QVariant(mailboxId));
       
   567         request->setArguments(list);
       
   568         ok = request->send();
       
   569         NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError()));
       
   570         delete request;
       
   571     }
       
   572     return ok;
       
   573 }
       
   574 
       
   575 /*!
       
   576     Handles message created event.
       
   577 
       
   578     \param folderId Id of the folder that includes the message
       
   579     \param messageIds Message ids that are checked
       
   580     \param mailboxId Id of the mailbox that includes the message
       
   581  */
       
   582 void NmMailAgent::handleMessageCreatedEvent(const NmId &folderId, const QList<NmId> &messageIds,
       
   583     const NmId &mailboxId)
       
   584 {
       
   585     NM_FUNCTION;
       
   586 
       
   587     // Check the new messages to make the indicator appear earlier
       
   588     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   589 
       
   590     // Inbox folder ID may be still unknown
       
   591     if (mailboxInfo->mInboxFolderId.id() == 0) {
       
   592         NmDataPluginInterface *plugin =
       
   593             mPluginFactory->interfaceInstance(mailboxId);
       
   594 
       
   595         if (plugin) {
       
   596             mailboxInfo->mInboxFolderId =
       
   597                 plugin->getStandardFolderId(mailboxId, NmFolderInbox);
       
   598         }
       
   599     }
       
   600 
       
   601     if (folderId == mailboxInfo->mInboxFolderId) {
       
   602         mailboxInfo->mInboxCreatedMessages += messageIds.count();
       
   603 
       
   604         foreach (NmId messageId, messageIds) {
       
   605             bool messageUnread = false;
       
   606             mailboxInfo->mMailIdList.append(messageId);
       
   607 
       
   608             // double check that the message ID is really new
       
   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) {
       
   616                 if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
       
   617                     if (messageUnread) {
       
   618                         mailboxInfo->mNewUnreadMailIdList.append(messageId);
       
   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                         }
       
   626                         updateMailboxState(mailboxId, true, false);
       
   627 
       
   628                         // Play the tone as well
       
   629                         playAlertTone();
       
   630                     }
       
   631                 }
       
   632             }
       
   633         }
       
   634     }
       
   635 
       
   636     // When created a new mail in the outbox, we are in sending state
       
   637     if (mailboxInfo->mOutboxFolderId == folderId) {
       
   638         // The first mail created in the outbox
       
   639         if (mailboxInfo->mOutboxMails <= 0) {
       
   640             NM_COMMENT("NmMailAgent: first mail in outbox");
       
   641         }
       
   642         mailboxInfo->mOutboxMails += messageIds.count();
       
   643         
       
   644         updateMailboxState(mailboxId,true,true);
       
   645         updateSendIndicator();
       
   646     }
       
   647 }
       
   648 
       
   649 
       
   650 /*!
       
   651     Handles message changed event.
       
   652     \sa updateUnreadCount
       
   653 
       
   654     \param folderId Id of the folder that includes the message
       
   655     \param messageIds Message ids that are checked
       
   656     \param mailboxId Id of the mailbox that includes the message
       
   657  */
       
   658 void NmMailAgent::handleMessageChangedEvent(const NmId &folderId,
       
   659     const QList<NmId> &messageIds,
       
   660     const NmId &mailboxId)
       
   661 {
       
   662     NM_FUNCTION;
       
   663 
       
   664     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   665 
       
   666     // we are interested only about changes in the inbox
       
   667     if (mailboxInfo && folderId == mailboxInfo->mInboxFolderId) {
       
   668         mailboxInfo->mInboxChangedMessages++;
       
   669 
       
   670         if (mailboxInfo->mNewUnreadMailIdList.count()>0) {
       
   671             bool updateMailbox = false;
       
   672 
       
   673             // Check how many messages was in the list of new unread mails
       
   674             foreach (NmId messageId, messageIds) {
       
   675                 if (mailboxInfo->mNewUnreadMailIdList.indexOf(messageId)>=0) {
       
   676                     bool messageUnread(false);
       
   677                     if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
       
   678                         // Message is no longer unread
       
   679                         if (!messageUnread) {
       
   680                             mailboxInfo->mNewUnreadMailIdList.removeAll(messageId);
       
   681                             updateMailbox = true;
       
   682                         }
       
   683                     }
       
   684                 }
       
   685             }
       
   686             
       
   687             if (updateMailbox) {
       
   688                 updateMailboxState(mailboxId);
       
   689                 updateUnreadIndicator();
       
   690             }
       
   691         }
       
   692     }
       
   693 }
       
   694 
       
   695 /*!
       
   696     Handles message deleted event.
       
   697 
       
   698     \param folderId Id of the folder that includes the message
       
   699     \param messageIds Message ids that are checked
       
   700     \param mailboxId Id of the mailbox that includes the message
       
   701  */
       
   702 void NmMailAgent::handleMessageDeletedEvent(const NmId &folderId, const QList<NmId> &messageIds,
       
   703     const NmId &mailboxId)
       
   704 {
       
   705     NM_FUNCTION;
       
   706     
       
   707     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   708 
       
   709     if (mailboxInfo && folderId == mailboxInfo->mInboxFolderId) {
       
   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         }
       
   723     }
       
   724 
       
   725     // Deleted mails from the outbox
       
   726     if (mailboxInfo->mOutboxFolderId == folderId) {
       
   727         mailboxInfo->mOutboxMails -= messageIds.count();
       
   728 
       
   729         // Sanity check for the outbox count
       
   730         if (mailboxInfo->mOutboxMails < 0) {
       
   731             mailboxInfo->mOutboxMails = 0;
       
   732         }
       
   733 
       
   734         // The last mail was now deleted from outbox
       
   735         if (mailboxInfo->mOutboxMails == 0) {
       
   736             NM_COMMENT("NmMailAgent: last mail deleted from outbox");
       
   737             updateSendIndicator();
       
   738         }
       
   739         updateMailboxState(mailboxId);
       
   740     }
       
   741 }
       
   742 
       
   743 /*!
       
   744     Get next free indicator index, starting from 0.
       
   745 
       
   746     \return index of the indicator that is available
       
   747  */
       
   748 int NmMailAgent::getFreeIndicatorIndex()
       
   749 {
       
   750     NM_FUNCTION;
       
   751 
       
   752     int index = 0;
       
   753     bool found(false);
       
   754     do {
       
   755         found = false;
       
   756         foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
   757             if (mailbox->mIndicatorIndex == index &&
       
   758                 mailbox->mActive) {
       
   759                 found = true;
       
   760                 index++;
       
   761             }
       
   762         }
       
   763     }
       
   764     while( found );
       
   765     return index;
       
   766 }
       
   767 
       
   768 /*!
       
   769     Received from NmFrameworkAdapter mailboxEvent signal
       
   770     \sa NmFrameworkAdapter
       
   771 
       
   772     \param event Mailbox event type
       
   773     \param mailboxIds Mailbox ids that are checked
       
   774  */
       
   775 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
       
   776 {
       
   777     NM_FUNCTION;
       
   778     NM_COMMENT(QString("NmMailAgent::handleMailboxEvent(): event=%1").arg(event));
       
   779 
       
   780     switch(event) {
       
   781         case NmMailboxCreated:
       
   782             foreach (NmId mailboxId, mailboxIds) {
       
   783                 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId); // create a new mailbox if needed
       
   784                 if (!mailboxInfo) {
       
   785                     // Unable to initialise the mailbox. Try again later.
       
   786                     NM_COMMENT("Cannot initialise mailbox");
       
   787 
       
   788                     qRegisterMetaType<NmId>("NmId");
       
   789                     QMetaObject::invokeMethod(this, "delayedMailboxCreated",
       
   790                         Qt::QueuedConnection, Q_ARG(NmId,mailboxId));
       
   791                 }
       
   792 
       
   793                 // make sure the mailbox activity data is reseted
       
   794                 deleteStoredMailboxActivity(mailboxId);
       
   795             }
       
   796             break;
       
   797         case NmMailboxChanged:
       
   798 
       
   799             // Mailbox name may have been changed
       
   800             foreach (NmId mailboxId, mailboxIds) {
       
   801                 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   802                 NmMailbox *mailbox(NULL);
       
   803                 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
       
   804                 if (plugin) {
       
   805                     plugin->getMailboxById(mailboxId,mailbox);
       
   806                 }
       
   807                 if (mailbox && mailboxInfo) {
       
   808                     if(mailbox->name() != mailboxInfo->mName) {
       
   809                         mailboxInfo->mName = mailbox->name();
       
   810 
       
   811                         if (mailboxInfo->mActive) {
       
   812                             // Update the status of the mailbox
       
   813                             updateMailboxState(mailboxId, true, true);
       
   814                         }
       
   815                     }
       
   816                 }
       
   817                 delete mailbox;
       
   818             }
       
   819             break;
       
   820         case NmMailboxDeleted:
       
   821             foreach (NmId mailboxId, mailboxIds) {
       
   822                 // Will hide also the indicator
       
   823                 removeMailboxInfo(mailboxId);
       
   824 
       
   825                 // make sure the mailbox activity data is deleted
       
   826                 deleteStoredMailboxActivity(mailboxId);
       
   827             }
       
   828             updateUnreadIndicator();
       
   829             break;
       
   830         default:
       
   831             break;
       
   832     }
       
   833 }
       
   834 
       
   835 /*!
       
   836     Called when mailbox is initialised with a delay. This may happen at least when
       
   837     NmMailboxCreated event is received.
       
   838 
       
   839     \param mailboxId id of the mailbox
       
   840 */
       
   841 void NmMailAgent::delayedMailboxCreated(const NmId mailboxId)
       
   842 {
       
   843     NM_FUNCTION;
       
   844 
       
   845     // create and subscribe to the mailbox, if not done earlier
       
   846     getMailboxInfo(mailboxId);
       
   847 }
       
   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 
       
   878 /*!
       
   879    Map the type name to mailbox info.
       
   880 
       
   881    \param type Indicator type name
       
   882    \return NULL if no mailbox match the type
       
   883  */
       
   884 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
       
   885 {
       
   886     NM_FUNCTION;
       
   887 
       
   888     NmMailboxInfo *foundMailbox = NULL;
       
   889     foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
   890         // mailbox is shown in indicators
       
   891         if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
       
   892             QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex);
       
   893 
       
   894             // type names match(!)
       
   895             if (type == typeName) {
       
   896                 foundMailbox = mailbox;
       
   897                 break;
       
   898             }
       
   899         }
       
   900     }
       
   901     return foundMailbox;
       
   902 }
       
   903 
       
   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 /*!
       
   919     Called when indicator is clicked from the indicator menu
       
   920     - indicator will be hide from the menu
       
   921     - mailbox will be launched
       
   922 
       
   923     \param type Indicator type name
       
   924     \param data Data sent by indicator
       
   925  */
       
   926 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data)
       
   927 {
       
   928     NM_FUNCTION;
       
   929     Q_UNUSED(data);
       
   930 
       
   931     // map the indicator type to mailbox
       
   932     NmMailboxInfo *info = getMailboxByType(type);
       
   933     if (info) {
       
   934         resetMailboxState(info);
       
   935 
       
   936         launchMailbox(info->mId.id());
       
   937     }
       
   938 }
       
   939 
       
   940 /*!
       
   941     Called when cenrep key value has been changed.
       
   942     - only silence mode key handled
       
   943 
       
   944     \param key changed key
       
   945     \param value value for a key
       
   946  */
       
   947 void NmMailAgent::valueChanged(const XQSettingsKey &key, const QVariant &value)
       
   948 {
       
   949     NM_FUNCTION;
       
   950 
       
   951     if(keysEqual(key, NmSilenceModeKey)) {
       
   952         mSilenceMode = value.toInt();
       
   953     }
       
   954 }
       
   955 
       
   956 /*!
       
   957     Received from NmFrameworkAdapter messageEvent signal
       
   958     \sa NmFrameworkAdapter
       
   959 
       
   960     \param event Message event
       
   961     \param folderId Folder Id
       
   962     \param messageIds List of message Ids
       
   963     \param mailboxId Id of the mailbox
       
   964  */
       
   965 void NmMailAgent::handleMessageEvent(
       
   966     NmMessageEvent event,
       
   967     const NmId &folderId,
       
   968     const QList<NmId> &messageIds,
       
   969     const NmId &mailboxId)
       
   970 {
       
   971     NM_FUNCTION;
       
   972     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
       
   973         arg(event).arg(mailboxId.id()));
       
   974 
       
   975     switch (event) {
       
   976         case NmMessageCreated: {
       
   977             handleMessageCreatedEvent(folderId, messageIds, mailboxId);
       
   978             break;
       
   979         }
       
   980         case NmMessageChanged: {
       
   981             handleMessageChangedEvent(folderId, messageIds, mailboxId);
       
   982             break;
       
   983         }
       
   984         case NmMessageDeleted: {
       
   985             handleMessageDeletedEvent(folderId, messageIds, mailboxId);
       
   986             break;
       
   987         }
       
   988         default:
       
   989             break;
       
   990     }
       
   991 }
       
   992 
       
   993 /*!
       
   994     Received from NmFrameworkAdapter syncStateEvent signal.
       
   995     \sa NmFrameworkAdapter
       
   996 
       
   997     \param state state of synchronization
       
   998     \param event Information related to asynchronous operation
       
   999 
       
  1000  */
       
  1001 void NmMailAgent::handleSyncStateEvent(NmSyncState state, const NmOperationCompletionEvent &event)
       
  1002 {
       
  1003     NM_FUNCTION;
       
  1004     NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): state=%1, id=%2").
       
  1005         arg(state).arg(event.mMailboxId.id()));
       
  1006 
       
  1007     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
       
  1008     if (info) {
       
  1009         info->mSyncState = state;
       
  1010     }
       
  1011 }
       
  1012 
       
  1013 /*!
       
  1014     Received from NmFrameworkAdapter connectionState signal.
       
  1015     \sa NmFrameworkAdapter
       
  1016 
       
  1017     \param state Connection state
       
  1018     \param mailboxId Id of the mailbox
       
  1019     \parma errorcode Error code
       
  1020  */
       
  1021 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId, int errorcode)
       
  1022 {
       
  1023     NM_FUNCTION;
       
  1024     NM_COMMENT(QString("NmMailAgent::handleConnectionEvent(): state=%1, id=%2").
       
  1025         arg(state).arg(mailboxId.id()));
       
  1026     Q_UNUSED(errorcode);
       
  1027 
       
  1028     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
  1029     if (mailboxInfo) {
       
  1030         // Connecting, Connected, Disconnecting, Disconnected
       
  1031         mailboxInfo->mConnectState = state;
       
  1032     }
       
  1033 }
       
  1034 
       
  1035 /*!
       
  1036     Remove a mailbox info entry.
       
  1037 
       
  1038     \param id Id of the mailbox
       
  1039     \return true if mailbox info was found.
       
  1040  */
       
  1041 bool NmMailAgent::removeMailboxInfo(const NmId &id)
       
  1042 {
       
  1043     NM_FUNCTION;
       
  1044 
       
  1045     bool found = false;
       
  1046     foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
  1047         if (mailbox->mId == id) {
       
  1048             // Hide the indicator too
       
  1049             if(mailbox->mIndicatorIndex>=0) {
       
  1050                 updateIndicator(false,*mailbox);
       
  1051             }
       
  1052 
       
  1053             found = true;
       
  1054             mMailboxes.removeAll(mailbox);
       
  1055         }
       
  1056     }
       
  1057     return found;
       
  1058 }
       
  1059 
       
  1060 /*!
       
  1061     Create a new mailbox info entry.
       
  1062 
       
  1063     \param id Id of the mailbox
       
  1064     \return new mailbox info object
       
  1065  */
       
  1066 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
       
  1067 {
       
  1068     NM_FUNCTION;
       
  1069 
       
  1070     // get information of the mailbox
       
  1071     NmMailbox *mailbox = NULL;
       
  1072     NmMailboxInfo *info = NULL;
       
  1073     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(id);
       
  1074     if (plugin) {
       
  1075         plugin->getMailboxById(id, mailbox);
       
  1076         if (mailbox) {
       
  1077             info = createMailboxInfo(*mailbox,plugin);
       
  1078         }
       
  1079     }
       
  1080 
       
  1081     return info;
       
  1082 }
       
  1083 
       
  1084 /*!
       
  1085     Create a new mailbox info with given parameters
       
  1086 
       
  1087     \param mailbox Mailbox which mailbox info will be created
       
  1088     \param plugin Interface to access email data
       
  1089     \return new mailbox info object
       
  1090  */
       
  1091 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox &mailbox, NmDataPluginInterface *plugin)
       
  1092 {
       
  1093     NM_FUNCTION;
       
  1094 
       
  1095     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
       
  1096     mailboxInfo->mId = mailbox.id();
       
  1097     mailboxInfo->mName = mailbox.name();
       
  1098 
       
  1099     mMailboxes.append(mailboxInfo);
       
  1100 
       
  1101     // Subscribe to get all mailbox events
       
  1102     plugin->subscribeMailboxEvents(mailboxInfo->mId);
       
  1103 
       
  1104     // get inbox folder ID. It might be still unknown (=0).
       
  1105     mailboxInfo->mInboxFolderId = plugin->getStandardFolderId(
       
  1106         mailbox.id(), NmFolderInbox );
       
  1107 
       
  1108     // get outbox folder ID
       
  1109     mailboxInfo->mOutboxFolderId = plugin->getStandardFolderId(
       
  1110         mailbox.id(), NmFolderOutbox );
       
  1111 
       
  1112     // Get branded mailbox icon
       
  1113     NmMailbox mailbox2( mailbox );
       
  1114     QString domainName = mailbox2.address().address();
       
  1115     EmailMailboxInfo emailMailboxInfo;
       
  1116     mailboxInfo->mIconName =
       
  1117         emailMailboxInfo.mailboxIcon(domainName);
       
  1118 
       
  1119     return mailboxInfo;
       
  1120 }
       
  1121 
       
  1122 /*!
       
  1123     Return mailbox info class with mailbox id. If no class is found,
       
  1124     create a new instance with given id.
       
  1125 
       
  1126     \param id Id of the mailbox
       
  1127     \return mailbox info object
       
  1128  */
       
  1129 NmMailboxInfo *NmMailAgent::getMailboxInfo(const NmId &id)
       
  1130 {
       
  1131     NM_FUNCTION;
       
  1132 
       
  1133     foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
  1134         if (mailbox->mId == id) {
       
  1135             return mailbox;
       
  1136         }
       
  1137     }
       
  1138 
       
  1139     // Not found. Create a new mailbox info.
       
  1140     return createMailboxInfo(id);
       
  1141 }
       
  1142 
       
  1143 /*!
       
  1144     Finds out if the message is unread.
       
  1145 
       
  1146     \param folderId the id of the folder that includes the message
       
  1147     \param messageIds the message ids that are checked
       
  1148     \param mailboxId the id of the mailbox that includes the message
       
  1149     \param unreadMessage true if there was unread messages
       
  1150     \return true if info fetching was successful
       
  1151  */
       
  1152 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId,
       
  1153     const NmId &messageId, const NmId &mailboxId, bool &unreadMessage)
       
  1154 {
       
  1155     NM_FUNCTION;
       
  1156 
       
  1157     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
       
  1158     bool ok(false);
       
  1159 
       
  1160     if (plugin) {
       
  1161         NmMessage *message=NULL;
       
  1162         plugin->getMessageById(mailboxId, folderId, messageId, message);
       
  1163         if (message) {
       
  1164             ok = true;
       
  1165             NmMessageEnvelope envelope = message->envelope();
       
  1166             if (!envelope.isRead()) {
       
  1167                 unreadMessage = true;
       
  1168             }
       
  1169             delete message;
       
  1170         }
       
  1171     }
       
  1172     return ok;
       
  1173 }
       
  1174 
       
  1175 /*!
       
  1176     Plays email alert tone when new messages arrive.
       
  1177 
       
  1178 	\returns true if the tone was played
       
  1179  */
       
  1180 bool NmMailAgent::playAlertTone()
       
  1181 {
       
  1182     NM_FUNCTION;
       
  1183     bool played(false);
       
  1184 
       
  1185     if (mAlertToneAllowed) {
       
  1186         // Play tone only when phone is not in silence mode.
       
  1187         if (!mSilenceMode) {
       
  1188             // Must instantiate it again to make sure correct tone is played  
       
  1189             XQSystemToneService systemTone;
       
  1190             systemTone.playTone(XQSystemToneService::EmailAlertTone);
       
  1191         }
       
  1192 
       
  1193         // Execute the vibra effect.
       
  1194         if (mVibra) {
       
  1195             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));
       
  1196         }
       
  1197 
       
  1198         // play alert only once per minute
       
  1199         mAlertToneAllowed = false;
       
  1200         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
       
  1201         played = true;
       
  1202     }
       
  1203 
       
  1204     return played;
       
  1205 }
       
  1206 
       
  1207 /*!
       
  1208     Allows alert tune to be played again.
       
  1209  */
       
  1210 void NmMailAgent::enableAlertTone()
       
  1211 {
       
  1212     NM_FUNCTION;
       
  1213 
       
  1214     mAlertToneAllowed = true;
       
  1215 }
       
  1216 
       
  1217 /*!
       
  1218     Update send indicator according to outbox state.
       
  1219  */
       
  1220 void NmMailAgent::updateSendIndicator()
       
  1221 {
       
  1222     NM_FUNCTION;
       
  1223 
       
  1224     // Get number of mails in outboxes
       
  1225     int outboxMails(0);
       
  1226     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
       
  1227         outboxMails += mailboxInfo->mOutboxMails;
       
  1228     }
       
  1229 
       
  1230     if( outboxMails == 0 ) {
       
  1231         mIndicator->deactivate(NmSendIndicatorName);
       
  1232     }
       
  1233     else if (outboxMails > mLastOutboxCount) {
       
  1234         // New mails detected in outbox
       
  1235 
       
  1236         // indicator will disappear automatically after a delay
       
  1237         mIndicator->activate(NmSendIndicatorName);
       
  1238     }
       
  1239 
       
  1240     mLastOutboxCount = outboxMails;
       
  1241 }
       
  1242 
       
  1243 /*!
       
  1244     Store the mailbox active information to permanent storage.
       
  1245 
       
  1246     \param mailboxId Id of the mailbox
       
  1247     \param active true if the mailbox is active
       
  1248  */
       
  1249 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
       
  1250 {
       
  1251     NM_FUNCTION;
       
  1252 
       
  1253     if (mSettingManager) {
       
  1254         XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
       
  1255         XQCentralRepositoryUtils utils(*mSettingManager);
       
  1256 
       
  1257         if (active) {
       
  1258             // when mailbox is active, key can be deleted
       
  1259             utils.deleteKey(key);
       
  1260         }
       
  1261         else {
       
  1262             utils.createKey(key,(int)active);
       
  1263         }
       
  1264     }
       
  1265 }
       
  1266 
       
  1267 /*!
       
  1268     Get the mailbox activity state.
       
  1269 
       
  1270     \param mailboxId id of the mailbox
       
  1271     \return true if the mailbox is active or no information was stored earlier
       
  1272  */
       
  1273 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
       
  1274 {
       
  1275     NM_FUNCTION;
       
  1276 
       
  1277     bool mailboxActive(true);
       
  1278     if (mSettingManager) {
       
  1279         XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
       
  1280         QVariant value = mSettingManager->readItemValue(key, XQSettingsManager::TypeInt);
       
  1281         if (value.isValid()) {
       
  1282             NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
       
  1283             mailboxActive = value.toInt();
       
  1284         }
       
  1285     }
       
  1286     return mailboxActive;
       
  1287 }
       
  1288 
       
  1289 /*!
       
  1290     Delete all stored activity information for the mailbox id.
       
  1291 
       
  1292     \param mailboxId id of the mailbox
       
  1293  */
       
  1294 void NmMailAgent::deleteStoredMailboxActivity(const NmId &mailboxId)
       
  1295 {
       
  1296     NM_FUNCTION;
       
  1297 
       
  1298     // deactivation delete the key too
       
  1299     storeMailboxActive(mailboxId, false);
       
  1300 }
       
  1301 
       
  1302 // End of file