emailservices/nmailagent/src/nmmailagent.cpp
changeset 43 99bcbff212ad
parent 40 2c62ef3caffd
child 44 c2d07d913565
equal deleted inserted replaced
42:139d4b7b2938 43:99bcbff212ad
    13 *
    13 *
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 
    17 
       
    18 #include <xqservicerequest.h>
       
    19 #include <email_services_api.h>
       
    20 
       
    21 #include "emailtrace.h"
    18 #include "nmmailagentheaders.h"
    22 #include "nmmailagentheaders.h"
    19 #include "nmmailagent.h"
    23 #include "nmmailagent.h"
       
    24 
       
    25 #include <xqsystemtoneservice.h>
    20 
    26 
    21 // CONSTS
    27 // CONSTS
    22 const int NmAgentIndicatorNotSet = -1;
    28 const int NmAgentIndicatorNotSet = -1;
    23 const int NmAgentAlertToneTimer = 60000; // 60s
    29 const int NmAgentAlertToneTimer = 60000; // 60s
       
    30 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
       
    31 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
       
    32 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    24 
    33 
    25 
    34 
    26 /*!
    35 /*!
    27     \class NmMailAgent
    36     \class NmMailAgent
    28 
    37 
    29     \brief Main class for receiving email events and passing them to the HbIndicator
    38     \brief Main class for receiving email events and passing them to the HbIndicator
    30 */
    39 */
    31 
    40 
    32 NmMailboxInfo::NmMailboxInfo()
    41 NmMailboxInfo::NmMailboxInfo()
    33 {
    42 {
       
    43     NM_FUNCTION;
       
    44 
    34     mId = 0;
    45     mId = 0;
    35     mIndicatorIndex = NmAgentIndicatorNotSet;
    46     mIndicatorIndex = NmAgentIndicatorNotSet;
    36     mSyncState = SyncComplete;
    47     mSyncState = SyncComplete;
    37     mConnectState = Disconnected;
    48     mConnectState = Disconnected;
    38     mOutboxMails = 0;
    49     mOutboxMails = 0;
    43     mInboxDeletedMessages = 0;
    54     mInboxDeletedMessages = 0;
    44     mActive = false;
    55     mActive = false;
    45 }
    56 }
    46 
    57 
    47 NmMailAgent::NmMailAgent() :
    58 NmMailAgent::NmMailAgent() :
       
    59  mIndicator(NULL),
       
    60  mSystemTone(NULL),
    48  mPluginFactory(NULL),
    61  mPluginFactory(NULL),
    49  mSendingState(false),
    62  mAlertToneAllowed(true),
    50  mAlertToneAllowed(true)
    63  mLastOutboxCount(0),
    51 {
    64  mUnreadIndicatorActive(false)
    52     NMLOG("NmMailAgent::NmMailAgent");
    65 {
       
    66     NM_FUNCTION;
    53 }
    67 }
    54 
    68 
    55 /*!
    69 /*!
    56     Delayed start
    70     Delayed start
    57 */
    71 */
    58 void NmMailAgent::delayedStart()
    72 void NmMailAgent::delayedStart()
    59 {
    73 {
    60 	NMLOG("NmMailAgent::delayedStart");
    74     NM_FUNCTION;
       
    75 
    61 	if (!init()) {
    76 	if (!init()) {
    62 		// Initialisation failed. Quit the agent.
    77 		// Initialisation failed. Quit the agent.
    63 		QCoreApplication::exit(1);
    78 		QCoreApplication::exit(1);
    64 	}
    79 	}
    65 }
    80 }
    67 /*!
    82 /*!
    68     Initialise the agent. \return true if succesfully started.
    83     Initialise the agent. \return true if succesfully started.
    69 */
    84 */
    70 bool NmMailAgent::init()
    85 bool NmMailAgent::init()
    71 {
    86 {
    72 	NMLOG("NmMailAgent::init");
    87     NM_FUNCTION;
       
    88 
    73     mPluginFactory = NmDataPluginFactory::instance();
    89     mPluginFactory = NmDataPluginFactory::instance();
    74     if (!mPluginFactory) {
    90     if (!mPluginFactory) {
    75         NMLOG("NmMailAgent::init PluginFactory not created");
    91         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
    76         return false;
    92         return false;
    77     }
    93     }
       
    94 
       
    95     //mSystemTone = new XQSystemToneService();
       
    96     
       
    97     mIndicator = new HbIndicator();
       
    98     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
       
    99         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
    78 
   100 
    79     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   101     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
    80 
   102 
    81     foreach (QObject *plugin, *plugins) {
   103     foreach (QObject *plugin, *plugins) {
    82     	if (plugin) {
   104     	if (plugin) {
   107     return true;
   129     return true;
   108 }
   130 }
   109 
   131 
   110 NmMailAgent::~NmMailAgent()
   132 NmMailAgent::~NmMailAgent()
   111 {
   133 {
       
   134     NM_FUNCTION;
       
   135 
   112     qDeleteAll(mMailboxes);
   136     qDeleteAll(mMailboxes);
       
   137     //delete mSystemTone;
   113     NmDataPluginFactory::releaseInstance(mPluginFactory);
   138     NmDataPluginFactory::releaseInstance(mPluginFactory);
   114 }
   139 }
   115 
   140 
   116 /*!
   141 /*!
   117     Initialize the mailbox list with the current state
   142     Initialize the mailbox list with the current state
   118 */
   143 */
   119 void NmMailAgent::initMailboxStatus()
   144 void NmMailAgent::initMailboxStatus()
   120 {
   145 {
   121     NMLOG("NmMailAgent::initMailboxStatus");
   146     NM_FUNCTION;
       
   147 
   122     QList<NmMailbox*> mailboxes;
   148     QList<NmMailbox*> mailboxes;
   123     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   149     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   124 
   150 
   125     foreach(QObject* pluginObject, *plugins) {
   151     foreach(QObject* pluginObject, *plugins) {
   126         NmDataPluginInterface *plugin =
   152         NmDataPluginInterface *plugin =
   151                 }
   177                 }
   152             }
   178             }
   153         }
   179         }
   154         qDeleteAll(mailboxes);
   180         qDeleteAll(mailboxes);
   155     }
   181     }
       
   182     updateSendIndicator();
   156 }
   183 }
   157 
   184 
   158 /*!
   185 /*!
   159     Get mailbox unread count in inbox folder
   186     Get mailbox unread count in inbox folder
   160     \param mailboxId id of the mailbox
   187     \param mailboxId id of the mailbox
   161     \param mailboxInfo contains the list of unread messages
   188     \param mailboxInfo contains the list of unread messages
   162     \return true if new unread mails was found
   189     \return true if new unread mails was found
   163 */
   190 */
   164 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo)
   191 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo)
   165 {
   192 {
   166     NMLOG("NmMailAgent::getUnreadCount");
   193     NM_FUNCTION;
       
   194 
   167     int newUnreadMessages(0);
   195     int newUnreadMessages(0);
   168 
   196 
   169     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   197     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   170 
   198 
   171     if (plugin) {
   199     if (plugin) {
   200 		qDeleteAll(messageList);
   228 		qDeleteAll(messageList);
   201 
   229 
   202 		// Save updated list of unread message IDs
   230 		// Save updated list of unread message IDs
   203         mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList;
   231         mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList;
   204     }
   232     }
   205 	NMLOG(QString("NmMailAgent::getUnreadCount count=%1 new=%2").
   233     NM_COMMENT(QString("NmMailAgent::getUnreadCount(): count=%1, new=%2").
   206 	    arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages));
   234         arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages));
   207 
   235 
   208     return (newUnreadMessages > 0);
   236     return (newUnreadMessages > 0);
   209 }
   237 }
   210 
   238 
   211 /*!
   239 /*!
   214     \param maxCount max number of outbox mails that is needed
   242     \param maxCount max number of outbox mails that is needed
   215     \return number of mails in the outbox
   243     \return number of mails in the outbox
   216 */
   244 */
   217 int NmMailAgent::getOutboxCount(const NmId &mailboxId)
   245 int NmMailAgent::getOutboxCount(const NmId &mailboxId)
   218 {
   246 {
   219     NMLOG("NmMailAgent::getOutboxCount");
   247     NM_FUNCTION;
       
   248 
   220     int count(0);
   249     int count(0);
   221 
   250 
   222     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   251     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   223 
   252 
   224     if (plugin) {
   253     if (plugin) {
   229 		QList<NmMessageEnvelope*> messageList;
   258 		QList<NmMessageEnvelope*> messageList;
   230 		plugin->listMessages(mailboxId, outboxId, messageList);
   259 		plugin->listMessages(mailboxId, outboxId, messageList);
   231 		count = messageList.count();
   260 		count = messageList.count();
   232 		qDeleteAll(messageList);
   261 		qDeleteAll(messageList);
   233     }
   262     }
   234     NMLOG(QString("NmMailAgent::getOutboxCount count=%1").arg(count));
   263     NM_COMMENT(QString("NmMailAgent::getOutboxCount(): count=%1").arg(count));
   235 
   264 
   236     return count;
   265     return count;
       
   266 }
       
   267 
       
   268 /*!
       
   269     Get list of unread counts in active mailboxes
       
   270 	\returns total number of unread mails
       
   271 */
       
   272 int NmMailAgent::getTotalUnreadCount() const
       
   273 {
       
   274     int unreads = 0;
       
   275     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
       
   276         if (mailbox->mActive) {
       
   277             unreads += mailbox->mUnreadMailIdList.count();
       
   278         }
       
   279     }
       
   280     return unreads;
       
   281 }
       
   282 
       
   283 /*!
       
   284     Update the "@" indicator state according to unread state
       
   285     \return true if the indicator was activated
       
   286 */
       
   287 bool NmMailAgent::updateUnreadIndicator()
       
   288 {
       
   289     int unreads = getTotalUnreadCount();
       
   290     return updateUnreadIndicator(unreads>0);
   237 }
   291 }
   238 
   292 
   239 /*!
   293 /*!
   240     Update the mailbox visibility and status
   294     Update the mailbox visibility and status
   241     \param mailboxId id of the mailbox
   295     \param mailboxId id of the mailbox
   244     \return true if the mailbox state was changed
   298     \return true if the mailbox state was changed
   245 */
   299 */
   246 bool NmMailAgent::updateMailboxState(const NmId &mailboxId,
   300 bool NmMailAgent::updateMailboxState(const NmId &mailboxId,
   247     bool active, bool refreshAlways)
   301     bool active, bool refreshAlways)
   248 {
   302 {
   249     // Update the global sending state
   303     NM_FUNCTION;
   250     mSendingState = false;
       
   251     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
       
   252         if (mailboxInfo->mOutboxMails>0) {
       
   253             mSendingState = true;
       
   254             break;
       
   255         }
       
   256     }
       
   257 
   304 
   258     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   305     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   259     bool changed = false;
   306     bool changed = false;
   260     if (mailboxInfo->mActive != active ||
   307     if (mailboxInfo->mActive != active ||
   261         refreshAlways) {
   308         refreshAlways) {
   262         mailboxInfo->mActive = active;
   309         mailboxInfo->mActive = active;
   263         changed = true;
   310         changed = true;
   264         if (active) {
   311         if (active) {
   265             // Mailbox is not yet assigned to any indicator
   312             // Mailbox is not yet assigned to any indicator
   266             if (mailboxInfo->mIndicatorIndex < 0) {
   313             if (mailboxInfo->mIndicatorIndex < 0) {
   267                 mailboxInfo->mIndicatorIndex = getIndicatorIndex();
   314                 mailboxInfo->mIndicatorIndex = getFreeIndicatorIndex();
   268             }
   315             }
   269 
   316 
   270             updateIndicator(true,*mailboxInfo);
   317             updateIndicator(true,*mailboxInfo);
   271         }
   318         }
   272         else {
   319         else {
   288     \return true if indicator was updated with no errors
   335     \return true if indicator was updated with no errors
   289 */
   336 */
   290 bool NmMailAgent::updateIndicator(bool active,
   337 bool NmMailAgent::updateIndicator(bool active,
   291     const NmMailboxInfo& mailboxInfo)
   338     const NmMailboxInfo& mailboxInfo)
   292 {
   339 {
   293     NMLOG(QString("NmMailAgent::updateIndicator index=%1 active=%2").
   340     NM_FUNCTION;
       
   341     NM_COMMENT(QString("NmMailAgent::updateIndicator(): index=%1, active=%2").
   294         arg(mailboxInfo.mIndicatorIndex).arg(active));
   342         arg(mailboxInfo.mIndicatorIndex).arg(active));
   295 
   343 
   296     bool ok = false;
   344     bool ok = false;
   297     QString name = QString("com.nokia.nmail.indicatorplugin_%1/1.0").
   345     QString name = QString(NmMailboxIndicatorType).
   298         arg(mailboxInfo.mIndicatorIndex);
   346         arg(mailboxInfo.mIndicatorIndex);
   299 
   347 
   300     QList<QVariant> list;
   348     QList<QVariant> list;
   301     list.append(mailboxInfo.mId.id());
   349     list.append(mailboxInfo.mId.id());
   302     list.append(mailboxInfo.mName);
   350     list.append(mailboxInfo.mName);
   303     list.append(mailboxInfo.mUnreadMailIdList.count());
   351     list.append(mailboxInfo.mUnreadMailIdList.count());
   304     list.append(mailboxInfo.mSyncState);
   352     list.append(mailboxInfo.mSyncState);
   305     list.append(mailboxInfo.mConnectState);
   353     list.append(mailboxInfo.mConnectState);
   306     list.append(mailboxInfo.mOutboxMails);
   354     list.append(mailboxInfo.mOutboxMails);
   307     list.append(mailboxInfo.mIconName);
   355     list.append(mailboxInfo.mIconName);
   308     list.append(mSendingState);
   356     list.append(mLastOutboxCount);
   309 
   357 
   310     HbIndicator indicator;
       
   311     if (active) {
   358     if (active) {
   312         ok = indicator.activate(name,list);
   359         ok = mIndicator->activate(name,list);
   313     }
   360     }
   314     else {
   361     else {
   315         ok = indicator.deactivate(name,list);
   362         ok = mIndicator->deactivate(name,list);
   316     }
   363     }
       
   364 
       
   365     updateUnreadIndicator();
       
   366 
       
   367     return ok;
       
   368 }
       
   369 
       
   370 /*!
       
   371     update the unread indicator state
       
   372     \return true if the indicator was activated
       
   373 */
       
   374 bool NmMailAgent::updateUnreadIndicator(bool active)
       
   375 {
       
   376     NM_FUNCTION;
       
   377     bool activated = false;
       
   378 
       
   379     if (active) {
       
   380         mIndicator->activate(NmUnreadIndicatorName);
       
   381         if (!mUnreadIndicatorActive) {
       
   382             activated = true;
       
   383         }
       
   384     }
       
   385     else {
       
   386         mIndicator->deactivate(NmUnreadIndicatorName);
       
   387     }
       
   388     mUnreadIndicatorActive = active;
       
   389     
       
   390     return activated;
       
   391 }
       
   392 
       
   393 /*!
       
   394     Opens inbox view to specific mailbox
       
   395     \return true if inbox is succesfully opened
       
   396 */
       
   397 bool NmMailAgent::launchMailbox(quint64 mailboxId)
       
   398 {
       
   399     NM_FUNCTION;
       
   400 
       
   401     XQServiceRequest request(
       
   402         emailFullServiceNameMailbox,
       
   403         emailOperationViewInbox,
       
   404         true);
       
   405 
       
   406     QList<QVariant> list;
       
   407     list.append(QVariant(mailboxId));
       
   408 
       
   409     request.setArguments(list);
       
   410     bool ok = request.send();
       
   411     NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request.latestError()));
   317     return ok;
   412     return ok;
   318 }
   413 }
   319 
   414 
   320 /*!
   415 /*!
   321     Get next free indicator index, starting from 0
   416     Get next free indicator index, starting from 0
   322     @return index of the indicator that is available
   417     @return index of the indicator that is available
   323  */
   418  */
   324 int NmMailAgent::getIndicatorIndex()
   419 int NmMailAgent::getFreeIndicatorIndex()
   325 {
   420 {
       
   421     NM_FUNCTION;
       
   422 
   326     int index = 0;
   423     int index = 0;
   327     bool found(false);
   424     bool found(false);
   328     do {
   425     do {
   329         found = false;
   426         found = false;
   330         foreach (NmMailboxInfo *mailbox, mMailboxes) {
   427         foreach (NmMailboxInfo *mailbox, mMailboxes) {
   343     Received from NmFrameworkAdapter mailboxEvent signal
   440     Received from NmFrameworkAdapter mailboxEvent signal
   344     \sa NmFrameworkAdapter
   441     \sa NmFrameworkAdapter
   345 */
   442 */
   346 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   443 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   347 {
   444 {
   348     NMLOG(QString("NmMailAgent::handleMailboxEvent %1").arg(event));
   445     NM_FUNCTION;
       
   446     NM_COMMENT(QString("NmMailAgent::handleMailboxEvent(): event=%1").arg(event));
   349 
   447 
   350     switch(event) {
   448     switch(event) {
   351         case NmMailboxCreated:
   449         case NmMailboxCreated:
   352             foreach (NmId mailboxId, mailboxIds) {
   450             foreach (NmId mailboxId, mailboxIds) {
   353                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   451                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   379         case NmMailboxDeleted:
   477         case NmMailboxDeleted:
   380             foreach (NmId mailboxId, mailboxIds) {
   478             foreach (NmId mailboxId, mailboxIds) {
   381                 // Will hide also the indicator
   479                 // Will hide also the indicator
   382                 removeMailboxInfo(mailboxId);
   480                 removeMailboxInfo(mailboxId);
   383             }
   481             }
       
   482             updateUnreadIndicator();
   384             break;
   483             break;
   385         default:
   484         default:
   386             break;
   485             break;
       
   486     }
       
   487 }
       
   488 
       
   489 /*!
       
   490    Map the type name to mailbox info
       
   491     \return NULL if no mailbox match the type
       
   492 */
       
   493 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
       
   494 {
       
   495     foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
   496         // mailbox is shown in indicators
       
   497         if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
       
   498             QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex);
       
   499 
       
   500             // type names match(!)
       
   501             if(type==typeName) {
       
   502                 return mailbox;
       
   503             }
       
   504         }
       
   505     }
       
   506     return NULL;
       
   507 }
       
   508 
       
   509 /*!
       
   510     Called when indicator is clicked from the indicator menu
       
   511     - indicator will be hide from the menu
       
   512     - mailbox will be launched
       
   513 */
       
   514 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data)
       
   515 {
       
   516     NM_FUNCTION;
       
   517     Q_UNUSED(data);
       
   518     
       
   519     // map the indicator type to mailbox
       
   520     NmMailboxInfo *info = getMailboxByType(type);
       
   521     if (info) {
       
   522         info->mLastSeenTime = QDateTime::currentDateTime();
       
   523         info->mActive = false; // indicator is no longer active
       
   524         updateUnreadIndicator();
       
   525 
       
   526         launchMailbox(info->mId.id());
   387     }
   527     }
   388 }
   528 }
   389 
   529 
   390 /*!
   530 /*!
   391     Received from NmFrameworkAdapter messageEvent signal
   531     Received from NmFrameworkAdapter messageEvent signal
   395             NmMessageEvent event,
   535             NmMessageEvent event,
   396             const NmId &folderId,
   536             const NmId &folderId,
   397             const QList<NmId> &messageIds,
   537             const QList<NmId> &messageIds,
   398             const NmId& mailboxId)
   538             const NmId& mailboxId)
   399 {
   539 {
   400     NMLOG(QString("NmMailAgent::handleMessageEvent %1 %2").arg(event).arg(mailboxId.id()));
   540     NM_FUNCTION;
       
   541     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
       
   542         arg(event).arg(mailboxId.id()));
       
   543 
   401     bool updateNeeded = false;
   544     bool updateNeeded = false;
   402     bool activate = false;
   545     bool activate = false;
   403 
   546 
   404     switch (event) {
   547     switch (event) {
   405         case NmMessageCreated: {
   548         case NmMessageCreated: {
   423                     foreach (NmId messageId, messageIds) {
   566                     foreach (NmId messageId, messageIds) {
   424                         if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   567                         if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   425                             if (messageUnread) {
   568                             if (messageUnread) {
   426                                 mailboxInfo->mUnreadMailIdList.append(messageId);
   569                                 mailboxInfo->mUnreadMailIdList.append(messageId);
   427                                 updateMailboxState(mailboxId, true, false);
   570                                 updateMailboxState(mailboxId, true, false);
       
   571 
       
   572                                 // make the "@" appear immediatelly
       
   573                                 updateUnreadIndicator(true);
       
   574 								
       
   575                                 // Play the tone as well
       
   576                                 playAlertTone();
   428                             }
   577                             }
   429                         }
   578                         }
   430                     }
   579                     }
   431                 }
   580                 }
   432             }
   581             }
   436 
   585 
   437             // When created a new mail in the outbox, we are in sending state
   586             // When created a new mail in the outbox, we are in sending state
   438             if (mailboxInfo->mOutboxFolderId == folderId) {
   587             if (mailboxInfo->mOutboxFolderId == folderId) {
   439                 // The first mail created in the outbox
   588                 // The first mail created in the outbox
   440                 if (mailboxInfo->mOutboxMails <= 0) {
   589                 if (mailboxInfo->mOutboxMails <= 0) {
   441 					NMLOG("NmMailAgent: first mail in outbox");
   590 					NM_COMMENT("NmMailAgent: first mail in outbox");
   442 					activate = true;
   591 					activate = true;
   443                     updateNeeded = true;
   592                     updateNeeded = true;
   444                 }
   593                 }
   445                 mailboxInfo->mOutboxMails += messageIds.count();
   594                 mailboxInfo->mOutboxMails += messageIds.count();
       
   595 				updateSendIndicator();
   446             }
   596             }
   447             break;
   597             break;
   448         }
   598         }
   449         case NmMessageChanged: {
   599         case NmMessageChanged: {
   450             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   600             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   458             if (mailboxInfo && mailboxInfo->mSyncState==SyncComplete) {
   608             if (mailboxInfo && mailboxInfo->mSyncState==SyncComplete) {
   459                 // check the unread status again
   609                 // check the unread status again
   460                 int oldCount = mailboxInfo->mUnreadMailIdList.count();
   610                 int oldCount = mailboxInfo->mUnreadMailIdList.count();
   461                 activate = updateUnreadCount(mailboxId, *mailboxInfo);
   611                 activate = updateUnreadCount(mailboxId, *mailboxInfo);
   462 
   612 
   463                 // new unread mails found or no more unread mails in the mailbox
   613                 // new unread mails found or no more unread mails in the inbox
   464                 if (activate || (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0)) {
   614                 if (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0) {
   465                     updateNeeded = true;
   615                     updateNeeded = true;
   466                 }
   616                 }
   467             }
   617             }
   468 			break;
   618 			break;
   469 		}
   619 		}
   483                     mailboxInfo->mOutboxMails = 0;
   633                     mailboxInfo->mOutboxMails = 0;
   484                 }
   634                 }
   485 
   635 
   486                 // The last mail was now deleted
   636                 // The last mail was now deleted
   487                 if (mailboxInfo->mOutboxMails == 0) {
   637                 if (mailboxInfo->mOutboxMails == 0) {
   488 					NMLOG("NmMailAgent: last mail deleted from outbox");
   638 					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   489 
       
   490                     // Keep it active if there is unread mails
   639                     // Keep it active if there is unread mails
   491                     activate = mailboxInfo->mUnreadMailIdList.count() > 0;
   640                     activate = mailboxInfo->mUnreadMailIdList.count() > 0;
   492                     updateNeeded = true;
   641                     updateNeeded = true;
   493                 }
   642                 }
       
   643 				updateSendIndicator();
   494             }
   644             }
   495             break;
   645             break;
   496         }
   646         }
   497         default:
   647         default:
   498             break;
   648             break;
   499     }
   649     }
   500 
   650 
   501     if (updateNeeded) {
   651     if (updateNeeded) {
       
   652         updateUnreadIndicator();
       
   653 
   502         NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   654         NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   503         updateMailboxState(mailboxId,
   655         updateMailboxState(mailboxId,
   504             activate, true /* force refresh */);
   656             activate, true /* force refresh */);
   505     }
   657     }
   506 }
   658 }
   511 */
   663 */
   512 void NmMailAgent::handleSyncStateEvent(
   664 void NmMailAgent::handleSyncStateEvent(
   513             NmSyncState state,
   665             NmSyncState state,
   514             const NmOperationCompletionEvent &event)
   666             const NmOperationCompletionEvent &event)
   515 {
   667 {
   516     NMLOG(QString("NmMailAgent::handleSyncStateEvent %1 %2").arg(state).arg(event.mMailboxId.id()));
   668     NM_FUNCTION;
       
   669     NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): state=%1, id=%2").
       
   670         arg(state).arg(event.mMailboxId.id()));
       
   671 
   517     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
   672     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
   518     if (info) {
   673     if (info) {
   519         info->mSyncState = state;
   674         info->mSyncState = state;
   520 
   675 
   521         if (state==Synchronizing) {
   676         if (state==Synchronizing) {
   535             }
   690             }
   536             bool active = info->mUnreadMailIdList.count() ||
   691             bool active = info->mUnreadMailIdList.count() ||
   537                 info->mOutboxMails;
   692                 info->mOutboxMails;
   538 
   693 
   539             // Refresh the indicator if messages created or changed
   694             // Refresh the indicator if messages created or changed
   540             NMLOG(QString(" created=%1, changed=%2, deleted=%3").
   695             NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): "
       
   696                 "created=%1, changed=%2, deleted=%3").
   541                 arg(info->mInboxCreatedMessages).
   697                 arg(info->mInboxCreatedMessages).
   542                 arg(info->mInboxChangedMessages).
   698                 arg(info->mInboxChangedMessages).
   543                 arg(info->mInboxDeletedMessages));
   699                 arg(info->mInboxDeletedMessages));
   544             bool refresh = (info->mInboxCreatedMessages > 0) || (info->mInboxChangedMessages > 0);
   700             bool refresh = (info->mInboxCreatedMessages > 0) || (info->mInboxChangedMessages > 0);
   545 
   701 
       
   702             updateUnreadIndicator();
       
   703 
   546             if (activate) {
   704             if (activate) {
   547                 updateMailboxState(event.mMailboxId, active, refresh);
   705                 updateMailboxState(event.mMailboxId, active, refresh);
   548             }
   706             }
   549         }
   707         }
   550     }
   708     }
   554     Received from NmFrameworkAdapter connectionState signal
   712     Received from NmFrameworkAdapter connectionState signal
   555     \sa NmFrameworkAdapter
   713     \sa NmFrameworkAdapter
   556 */
   714 */
   557 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId, int errorcode)
   715 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId, int errorcode)
   558 {
   716 {
   559 	Q_UNUSED(errorcode);
   717     NM_FUNCTION;
   560     NMLOG(QString("NmMailAgent::handleConnectionEvent %1 %2 %3").arg(state).arg(mailboxId.id()).arg(errorcode));
   718     NM_COMMENT(QString("NmMailAgent::handleConnectionEvent(): state=%1, id=%2").
       
   719         arg(state).arg(mailboxId.id()));
       
   720     Q_UNUSED(errorcode);
       
   721 
   561     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   722     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   562     if (mailboxInfo) {
   723     if (mailboxInfo) {
   563         // Connecting, Connected, Disconnecting, Disconnected
   724         // Connecting, Connected, Disconnecting, Disconnected
   564         mailboxInfo->mConnectState = state;
   725         mailboxInfo->mConnectState = state;
   565     }
   726     }
   569     Remove a mailbox info entry
   730     Remove a mailbox info entry
   570     \return true if mailbox info was found
   731     \return true if mailbox info was found
   571 */
   732 */
   572 bool NmMailAgent::removeMailboxInfo(const NmId &id)
   733 bool NmMailAgent::removeMailboxInfo(const NmId &id)
   573 {
   734 {
       
   735     NM_FUNCTION;
       
   736 
   574     bool found = false;
   737     bool found = false;
   575     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   738     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   576         if (mailbox->mId == id) {
   739         if (mailbox->mId == id) {
   577             // Hide the indicator too
   740             // Hide the indicator too
   578             if(mailbox->mIndicatorIndex>=0) {
   741             if(mailbox->mIndicatorIndex>=0) {
   590     Create a new mailbox info entry
   753     Create a new mailbox info entry
   591     \return new mailbox info object
   754     \return new mailbox info object
   592 */
   755 */
   593 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
   756 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
   594 {
   757 {
       
   758     NM_FUNCTION;
       
   759 
   595     // get information of the mailbox
   760     // get information of the mailbox
   596     NmMailbox *mailbox = NULL;
   761     NmMailbox *mailbox = NULL;
   597     NmMailboxInfo *info = NULL;
   762     NmMailboxInfo *info = NULL;
   598     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(id);
   763     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(id);
   599     if (plugin) {
   764     if (plugin) {
   610     Create a new mailbox info with given parameters
   775     Create a new mailbox info with given parameters
   611     \return new mailbox info object
   776     \return new mailbox info object
   612 */
   777 */
   613 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox &mailbox, NmDataPluginInterface *plugin)
   778 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox &mailbox, NmDataPluginInterface *plugin)
   614 {
   779 {
       
   780     NM_FUNCTION;
       
   781 
   615     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
   782     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
   616     mailboxInfo->mId = mailbox.id();
   783     mailboxInfo->mId = mailbox.id();
   617     mailboxInfo->mName = mailbox.name();
   784     mailboxInfo->mName = mailbox.name();
   618 
   785 
   619     mMailboxes.append(mailboxInfo);
   786     mMailboxes.append(mailboxInfo);
   643     Return mailbox info class with mailbox id. If no class is found, create a new instance with given id.
   810     Return mailbox info class with mailbox id. If no class is found, create a new instance with given id.
   644     \return mailbox info object
   811     \return mailbox info object
   645 */
   812 */
   646 NmMailboxInfo *NmMailAgent::getMailboxInfo(const NmId &id)
   813 NmMailboxInfo *NmMailAgent::getMailboxInfo(const NmId &id)
   647 {
   814 {
       
   815     NM_FUNCTION;
       
   816 
   648     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   817     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   649         if (mailbox->mId == id) {
   818         if (mailbox->mId == id) {
   650             return mailbox;
   819             return mailbox;
   651         }
   820         }
   652     }
   821     }
   664     \return true if info fetching was successful
   833     \return true if info fetching was successful
   665 */
   834 */
   666 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId,
   835 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId,
   667     const NmId &messageId, const NmId &mailboxId, bool &unreadMessage)
   836     const NmId &messageId, const NmId &mailboxId, bool &unreadMessage)
   668 {
   837 {
   669     NMLOG("NmMailAgent::messageInfo");
   838     NM_FUNCTION;
   670 
   839 
   671     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   840     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   672     bool ok = false;
   841     bool ok = false;
   673 
   842 
   674     if (plugin) {
   843     if (plugin) {
   685     }
   854     }
   686     return ok;
   855     return ok;
   687 }
   856 }
   688 
   857 
   689 /*!
   858 /*!
   690     Plays email alert tune when new messages arrive
   859     Plays email alert tone when new messages arrive
   691 */
   860 	\returns true if the tone was played
   692 void NmMailAgent::playAlertTone()
   861 */
   693 {
   862 bool NmMailAgent::playAlertTone()
       
   863 {
       
   864     NM_FUNCTION;
       
   865 	bool played = false;
       
   866 
   694     if (mAlertToneAllowed) {
   867     if (mAlertToneAllowed) {
   695         // play alert
   868         //mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
       
   869         
       
   870         // play alert only once per minute
   696         mAlertToneAllowed = false;
   871         mAlertToneAllowed = false;
   697         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   872         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   698     }
   873 		played = true;
       
   874     }
       
   875 	return played;
   699 }
   876 }
   700 
   877 
   701 /*!
   878 /*!
   702     Allows alert tune to be played again
   879     Allows alert tune to be played again
   703 */
   880 */
   704 void NmMailAgent::enableAlertTone()
   881 void NmMailAgent::enableAlertTone()
   705 {
   882 {
       
   883     NM_FUNCTION;
       
   884 
   706     mAlertToneAllowed = true;
   885     mAlertToneAllowed = true;
   707 }
   886 }
   708 
   887 
       
   888 /*!
       
   889     update send indicator according to outbox state
       
   890 */
       
   891 void NmMailAgent::updateSendIndicator()
       
   892 {
       
   893     // Get number of mails in outboxes
       
   894     int outboxMails = 0;
       
   895     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
       
   896         outboxMails += mailboxInfo->mOutboxMails;
       
   897     }
       
   898 
       
   899     if( outboxMails == 0 ) {
       
   900         mIndicator->deactivate(NmSendIndicatorName);
       
   901     }
       
   902     else if (outboxMails > mLastOutboxCount) {
       
   903         // New mails detected in outbox
       
   904 
       
   905     	// indicator will disappear automatically after a delay
       
   906         mIndicator->activate(NmSendIndicatorName);
       
   907 	}
       
   908 
       
   909 	mLastOutboxCount = outboxMails;
       
   910 }
       
   911 
   709 // End of file
   912 // End of file
   710 
   913