emailservices/nmailagent/src/nmmailagent.cpp
changeset 44 c2d07d913565
parent 43 99bcbff212ad
child 47 f83bd4ae1fe3
child 54 997a02608b3a
equal deleted inserted replaced
43:99bcbff212ad 44:c2d07d913565
    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 "emailtrace.h"
    22 #include "nmmailagentheaders.h"
    19 #include "nmmailagentheaders.h"
    23 #include "nmmailagent.h"
    20 #include "nmmailagent.h"
    24 
    21 
       
    22 #include <xqaiwdeclplat.h>
       
    23 #include <xqapplicationmanager.h>
       
    24 #include <xqaiwrequest.h>
       
    25 
       
    26 #include <xqsettingsmanager.h>
       
    27 #include <xqcentralrepositoryutils.h>
    25 #include <xqsystemtoneservice.h>
    28 #include <xqsystemtoneservice.h>
    26 
    29 
    27 // CONSTS
    30 // CONSTS
    28 const int NmAgentIndicatorNotSet = -1;
    31 const int NmAgentIndicatorNotSet = -1;
    29 const int NmAgentAlertToneTimer = 60000; // 60s
    32 const int NmAgentAlertToneTimer = 60000; // 60s
       
    33 const int NmAgentDefaultVibraDuration = 1000; // 1 second
       
    34 static const quint32 NmRepositoryId = 0x2002C326; 
    30 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    35 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    31 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    36 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    32 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    37 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    33 
    38 
    34 
    39 
    51     mOutboxFolderId = 0;
    56     mOutboxFolderId = 0;
    52     mInboxCreatedMessages = 0;
    57     mInboxCreatedMessages = 0;
    53     mInboxChangedMessages = 0;
    58     mInboxChangedMessages = 0;
    54     mInboxDeletedMessages = 0;
    59     mInboxDeletedMessages = 0;
    55     mActive = false;
    60     mActive = false;
       
    61     mInboxActive = false;
    56 }
    62 }
    57 
    63 
    58 NmMailAgent::NmMailAgent() :
    64 NmMailAgent::NmMailAgent() :
    59  mIndicator(NULL),
    65  mIndicator(NULL),
    60  mSystemTone(NULL),
    66  mSystemTone(NULL),
    61  mPluginFactory(NULL),
    67  mPluginFactory(NULL),
       
    68  mVibra(NULL),
    62  mAlertToneAllowed(true),
    69  mAlertToneAllowed(true),
    63  mLastOutboxCount(0),
    70  mLastOutboxCount(0),
    64  mUnreadIndicatorActive(false)
    71  mUnreadIndicatorActive(false)
    65 {
    72 {
    66     NM_FUNCTION;
    73     NM_FUNCTION;
   121 				this, SLOT(handleConnectionEvent(NmConnectState, const NmId, int)),
   128 				this, SLOT(handleConnectionEvent(NmConnectState, const NmId, int)),
   122 				Qt::UniqueConnection);
   129 				Qt::UniqueConnection);
   123     	}
   130     	}
   124     }
   131     }
   125 
   132 
       
   133     // Construct the vibra interface instance.
       
   134     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
       
   135 
   126     // load all current mailboxes
   136     // load all current mailboxes
   127     initMailboxStatus();
   137     initMailboxStatus();
   128 
   138 
   129     return true;
   139     return true;
   130 }
   140 }
   131 
   141 
   132 NmMailAgent::~NmMailAgent()
   142 NmMailAgent::~NmMailAgent()
   133 {
   143 {
   134     NM_FUNCTION;
   144     NM_FUNCTION;
   135 
   145 
       
   146     delete mVibra;
       
   147     //delete mSystemTone;
       
   148 
   136     qDeleteAll(mMailboxes);
   149     qDeleteAll(mMailboxes);
       
   150 
   137     //delete mSystemTone;
   151     //delete mSystemTone;
   138     NmDataPluginFactory::releaseInstance(mPluginFactory);
   152     NmDataPluginFactory::releaseInstance(mPluginFactory);
   139 }
   153 }
   140 
   154 
   141 /*!
   155 /*!
   164             const NmMailbox *mailbox = i.previous();
   178             const NmMailbox *mailbox = i.previous();
   165             if (mailbox) {
   179             if (mailbox) {
   166                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox,plugin);
   180                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox,plugin);
   167                 if (mailboxInfo) {
   181                 if (mailboxInfo) {
   168                     bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo);
   182                     bool activate = updateUnreadCount(mailbox->id(), *mailboxInfo);
       
   183                     bool wasActive = isMailboxActive(mailbox->id());
       
   184                     if (activate) {
       
   185                         if(!wasActive) {
       
   186                             // do not activate the mailbox if it was left as hidden last time
       
   187                             activate = false;
       
   188                         }
       
   189                         else {
       
   190                             // otherwise, activate the mailbox and show inbox state
       
   191                             mailboxInfo->mInboxActive = true;
       
   192                         }
       
   193                     }
       
   194 
   169                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id());
   195                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id());
   170                     if (mailboxInfo->mOutboxMails > 0) {
   196                     if (mailboxInfo->mOutboxMails > 0 && wasActive) {
   171                         activate = true;
   197                         activate = true;
   172                     }
   198                     }
   173 
   199 
   174                     // Create indicator for visible mailboxes
   200 	                // Create indicator for visible mailboxes
   175                     updateMailboxState(mailbox->id(),
   201     	            updateMailboxState(mailbox->id(), activate, false);
   176                         activate, false);
       
   177                 }
   202                 }
   178             }
   203             }
   179         }
   204         }
   180         qDeleteAll(mailboxes);
   205         qDeleteAll(mailboxes);
   181     }
   206     }
       
   207     updateUnreadIndicator();
   182     updateSendIndicator();
   208     updateSendIndicator();
   183 }
   209 }
   184 
   210 
   185 /*!
   211 /*!
   186     Get mailbox unread count in inbox folder
   212     Get mailbox unread count in inbox folder
   271 */
   297 */
   272 int NmMailAgent::getTotalUnreadCount() const
   298 int NmMailAgent::getTotalUnreadCount() const
   273 {
   299 {
   274     int unreads = 0;
   300     int unreads = 0;
   275     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   301     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   276         if (mailbox->mActive) {
   302         if (mailbox->mActive && mailbox->mInboxActive) {
   277             unreads += mailbox->mUnreadMailIdList.count();
   303             unreads += mailbox->mUnreadMailIdList.count();
   278         }
   304         }
   279     }
   305     }
   280     return unreads;
   306     return unreads;
   281 }
   307 }
   304 
   330 
   305     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   331     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   306     bool changed = false;
   332     bool changed = false;
   307     if (mailboxInfo->mActive != active ||
   333     if (mailboxInfo->mActive != active ||
   308         refreshAlways) {
   334         refreshAlways) {
       
   335         
       
   336 		// store the new state to permanent storage
       
   337         storeMailboxActive(mailboxId, active);
       
   338         
   309         mailboxInfo->mActive = active;
   339         mailboxInfo->mActive = active;
   310         changed = true;
   340         changed = true;
   311         if (active) {
   341         if (active) {
   312             // Mailbox is not yet assigned to any indicator
   342             // Mailbox is not yet assigned to any indicator
   313             if (mailboxInfo->mIndicatorIndex < 0) {
   343             if (mailboxInfo->mIndicatorIndex < 0) {
   374 bool NmMailAgent::updateUnreadIndicator(bool active)
   404 bool NmMailAgent::updateUnreadIndicator(bool active)
   375 {
   405 {
   376     NM_FUNCTION;
   406     NM_FUNCTION;
   377     bool activated = false;
   407     bool activated = false;
   378 
   408 
   379     if (active) {
   409     if (active != mUnreadIndicatorActive) {
   380         mIndicator->activate(NmUnreadIndicatorName);
   410         if (active) {
   381         if (!mUnreadIndicatorActive) {
   411             mIndicator->activate(NmUnreadIndicatorName);
   382             activated = true;
   412             activated = true;
   383         }
   413         }
   384     }
   414         else {
   385     else {
   415             mIndicator->deactivate(NmUnreadIndicatorName);
   386         mIndicator->deactivate(NmUnreadIndicatorName);
   416         }
   387     }
   417         mUnreadIndicatorActive = active;
   388     mUnreadIndicatorActive = active;
   418     }
   389     
   419 
   390     return activated;
   420     return activated;
   391 }
   421 }
   392 
   422 
   393 /*!
   423 /*!
   394     Opens inbox view to specific mailbox
   424     Opens inbox view to specific mailbox
   396 */
   426 */
   397 bool NmMailAgent::launchMailbox(quint64 mailboxId)
   427 bool NmMailAgent::launchMailbox(quint64 mailboxId)
   398 {
   428 {
   399     NM_FUNCTION;
   429     NM_FUNCTION;
   400 
   430 
   401     XQServiceRequest request(
   431     XQApplicationManager appManager;
   402         emailFullServiceNameMailbox,
   432     XQAiwRequest *request = appManager.create(
   403         emailOperationViewInbox,
   433         XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW, false);
   404         true);
   434     
   405 
       
   406     QList<QVariant> list;
   435     QList<QVariant> list;
   407     list.append(QVariant(mailboxId));
   436     list.append(QVariant(mailboxId));
   408 
   437     request->setArguments(list);
   409     request.setArguments(list);
   438     
   410     bool ok = request.send();
   439     bool ok = request->send();
   411     NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request.latestError()));
   440     NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError()));
       
   441     delete request;
   412     return ok;
   442     return ok;
   413 }
   443 }
   414 
   444 
   415 /*!
   445 /*!
   416     Get next free indicator index, starting from 0
   446     Get next free indicator index, starting from 0
   447 
   477 
   448     switch(event) {
   478     switch(event) {
   449         case NmMailboxCreated:
   479         case NmMailboxCreated:
   450             foreach (NmId mailboxId, mailboxIds) {
   480             foreach (NmId mailboxId, mailboxIds) {
   451                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   481                 getMailboxInfo(mailboxId); // create a new mailbox if needed
       
   482                 
       
   483                 // make sure the mailbox activity data is reseted
       
   484                 deleteStoredMailboxActivity(mailboxId);
   452             }
   485             }
   453             break;
   486             break;
   454         case NmMailboxChanged:
   487         case NmMailboxChanged:
   455 
   488 
   456             // Mailbox name may have been changed
   489             // Mailbox name may have been changed
   476             break;
   509             break;
   477         case NmMailboxDeleted:
   510         case NmMailboxDeleted:
   478             foreach (NmId mailboxId, mailboxIds) {
   511             foreach (NmId mailboxId, mailboxIds) {
   479                 // Will hide also the indicator
   512                 // Will hide also the indicator
   480                 removeMailboxInfo(mailboxId);
   513                 removeMailboxInfo(mailboxId);
       
   514                 
       
   515                 // make sure the mailbox activity data is deleted
       
   516                 deleteStoredMailboxActivity(mailboxId);
   481             }
   517             }
   482             updateUnreadIndicator();
   518             updateUnreadIndicator();
   483             break;
   519             break;
   484         default:
   520         default:
   485             break;
   521             break;
   517     Q_UNUSED(data);
   553     Q_UNUSED(data);
   518     
   554     
   519     // map the indicator type to mailbox
   555     // map the indicator type to mailbox
   520     NmMailboxInfo *info = getMailboxByType(type);
   556     NmMailboxInfo *info = getMailboxByType(type);
   521     if (info) {
   557     if (info) {
   522         info->mLastSeenTime = QDateTime::currentDateTime();
       
   523         info->mActive = false; // indicator is no longer active
   558         info->mActive = false; // indicator is no longer active
       
   559         info->mInboxActive = false; // also inbox does not activate the indicator
       
   560         storeMailboxActive(info->mId, false);
   524         updateUnreadIndicator();
   561         updateUnreadIndicator();
   525 
   562 
   526         launchMailbox(info->mId.id());
   563         launchMailbox(info->mId.id());
   527     }
   564     }
   528 }
   565 }
   547     switch (event) {
   584     switch (event) {
   548         case NmMessageCreated: {
   585         case NmMessageCreated: {
   549             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   586             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   550 
   587 
   551             // Check the new messages to make the indicator appear earlier
   588             // Check the new messages to make the indicator appear earlier
   552             if (mailboxInfo->mSyncState == Synchronizing &&
   589             if (mailboxInfo->mSyncState == Synchronizing) {
   553                 mailboxInfo->mUnreadMailIdList.count()==0) {
   590                 
   554 
       
   555                 // Inbox folder ID may be still unknown
   591                 // Inbox folder ID may be still unknown
   556                 if (mailboxInfo->mInboxFolderId.id()==0) {
   592                 if (mailboxInfo->mInboxFolderId.id()==0) {
   557                     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   593                     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   558                     if (plugin) {
   594                     if (plugin) {
   559                         mailboxInfo->mInboxFolderId =
   595                         mailboxInfo->mInboxFolderId =
   560                             plugin->getStandardFolderId(mailboxId, NmFolderInbox);
   596                             plugin->getStandardFolderId(mailboxId, NmFolderInbox);
   561                     }
   597                     }
   562                 }
   598                 }
   563 
   599                 
   564                 if (folderId == mailboxInfo->mInboxFolderId) {
   600                 if (folderId == mailboxInfo->mInboxFolderId) {
   565                     bool messageUnread = false;
       
   566                     foreach (NmId messageId, messageIds) {
   601                     foreach (NmId messageId, messageIds) {
   567                         if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   602                         bool messageUnread = false;
   568                             if (messageUnread) {
   603 
   569                                 mailboxInfo->mUnreadMailIdList.append(messageId);
   604                         // Check the message if we can either play a tone or if the "@" is 
   570                                 updateMailboxState(mailboxId, true, false);
   605                         // not visible at the moment
   571 
   606                         if (mAlertToneAllowed || !mUnreadIndicatorActive) {
   572                                 // make the "@" appear immediatelly
   607                             if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   573                                 updateUnreadIndicator(true);
   608                                 if (messageUnread) {
   574 								
   609                                     mailboxInfo->mUnreadMailIdList.append(messageId);
   575                                 // Play the tone as well
   610                                     mailboxInfo->mInboxActive = true;
   576                                 playAlertTone();
   611                                     updateMailboxState(mailboxId, true, false);
       
   612 
       
   613                                     // make the "@" appear immediatelly
       
   614                                     updateUnreadIndicator(true);
       
   615 
       
   616                                     // Play the tone as well
       
   617                                     playAlertTone();
       
   618                                 }
   577                             }
   619                             }
   578                         }
   620                         }
   579                     }
   621                     }
   580                 }
   622                 }
   581             }
   623             }
       
   624             
   582             if (folderId==mailboxInfo->mInboxFolderId) {
   625             if (folderId==mailboxInfo->mInboxFolderId) {
   583                 mailboxInfo->mInboxCreatedMessages++;
   626                 mailboxInfo->mInboxCreatedMessages += messageIds.count();
   584             }
   627             }
   585 
   628 
   586             // When created a new mail in the outbox, we are in sending state
   629             // When created a new mail in the outbox, we are in sending state
   587             if (mailboxInfo->mOutboxFolderId == folderId) {
   630             if (mailboxInfo->mOutboxFolderId == folderId) {
   588                 // The first mail created in the outbox
   631                 // The first mail created in the outbox
   589                 if (mailboxInfo->mOutboxMails <= 0) {
   632                 if (mailboxInfo->mOutboxMails <= 0) {
   590 					NM_COMMENT("NmMailAgent: first mail in outbox");
   633 					NM_COMMENT("NmMailAgent: first mail in outbox");
   591 					activate = true;
       
   592                     updateNeeded = true;
       
   593                 }
   634                 }
       
   635                 // Always activate the indicator
       
   636                 activate = true;
       
   637                 updateNeeded = true;
       
   638                 
   594                 mailboxInfo->mOutboxMails += messageIds.count();
   639                 mailboxInfo->mOutboxMails += messageIds.count();
   595 				updateSendIndicator();
   640 				updateSendIndicator();
   596             }
   641             }
   597             break;
   642             break;
   598         }
   643         }
   634                 }
   679                 }
   635 
   680 
   636                 // The last mail was now deleted
   681                 // The last mail was now deleted
   637                 if (mailboxInfo->mOutboxMails == 0) {
   682                 if (mailboxInfo->mOutboxMails == 0) {
   638 					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   683 					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   639                     // Keep it active if there is unread mails
   684                     // Keep it active if there is unread mails and inbox is still active
   640                     activate = mailboxInfo->mUnreadMailIdList.count() > 0;
   685 					if (mailboxInfo->mInboxActive &&
       
   686 					    mailboxInfo->mUnreadMailIdList.count() > 0) {
       
   687 					    activate = true;
       
   688                     }
   641                     updateNeeded = true;
   689                     updateNeeded = true;
   642                 }
   690                 }
   643 				updateSendIndicator();
   691 				updateSendIndicator();
   644             }
   692             }
   645             break;
   693             break;
   865 	bool played = false;
   913 	bool played = false;
   866 
   914 
   867     if (mAlertToneAllowed) {
   915     if (mAlertToneAllowed) {
   868         //mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
   916         //mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
   869         
   917         
       
   918         // Execute the vibra effect.
       
   919         if (mVibra) {
       
   920             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));
       
   921         }
       
   922         
   870         // play alert only once per minute
   923         // play alert only once per minute
   871         mAlertToneAllowed = false;
   924         mAlertToneAllowed = false;
   872         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   925         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   873 		played = true;
   926 		played = true;
   874     }
   927     }
       
   928 
   875 	return played;
   929 	return played;
   876 }
   930 }
   877 
   931 
   878 /*!
   932 /*!
   879     Allows alert tune to be played again
   933     Allows alert tune to be played again
   907 	}
   961 	}
   908 
   962 
   909 	mLastOutboxCount = outboxMails;
   963 	mLastOutboxCount = outboxMails;
   910 }
   964 }
   911 
   965 
       
   966 /*!
       
   967     Store the mailbox active information to permanent storage
       
   968     \param mailboxId id of the mailbox
       
   969     \param active true if the mailbox is active
       
   970 */
       
   971 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
       
   972 {
       
   973     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
       
   974     XQSettingsManager mgr;
       
   975     XQCentralRepositoryUtils utils(mgr);
       
   976     
       
   977     if (active) {
       
   978         // when mailbox is active, key can be deleted
       
   979         utils.deleteKey(key);
       
   980     }
       
   981     else {
       
   982         utils.createKey(key,(int)active);
       
   983     }
       
   984 }
       
   985 
       
   986 /*!
       
   987     Get the mailbox activity state. 
       
   988     \param mailboxId id of the mailbox
       
   989     \return true if the mailbox is active or no information was stored earlier
       
   990 */
       
   991 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
       
   992 {
       
   993     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
       
   994     XQSettingsManager mgr;
       
   995     QVariant value = mgr.readItemValue(key,XQSettingsManager::TypeInt);
       
   996     if (!value.isValid()) {
       
   997         // no valid value found, key missing?
       
   998         NM_COMMENT("NmMailAgent::isMailboxActive - value not valid");
       
   999         return true;
       
  1000     }
       
  1001     NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
       
  1002     return value.toInt();
       
  1003 }
       
  1004 
       
  1005 /*!
       
  1006     Delete all stored activity information for the mailbox id
       
  1007     \param mailboxId id of the mailbox
       
  1008 */
       
  1009 void NmMailAgent::deleteStoredMailboxActivity(const NmId &mailboxId)
       
  1010 {
       
  1011     // deactivation delete the key too
       
  1012     storeMailboxActive(mailboxId,false);
       
  1013 }
       
  1014 
   912 // End of file
  1015 // End of file
   913 
  1016