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