emailservices/nmailagent/src/nmmailagent.cpp
changeset 47 f83bd4ae1fe3
parent 44 c2d07d913565
child 48 10eaf342f539
equal deleted inserted replaced
45:780f926bc26c 47:f83bd4ae1fe3
    29 
    29 
    30 // CONSTS
    30 // CONSTS
    31 const int NmAgentIndicatorNotSet = -1;
    31 const int NmAgentIndicatorNotSet = -1;
    32 const int NmAgentAlertToneTimer = 60000; // 60s
    32 const int NmAgentAlertToneTimer = 60000; // 60s
    33 const int NmAgentDefaultVibraDuration = 1000; // 1 second
    33 const int NmAgentDefaultVibraDuration = 1000; // 1 second
    34 static const quint32 NmRepositoryId = 0x2002C326; 
    34 static const quint32 NmRepositoryId = 0x2002C326;
    35 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    35 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    36 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    36 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    37 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    37 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    38 
    38 
    39 
    39 
    97     if (!mPluginFactory) {
    97     if (!mPluginFactory) {
    98         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
    98         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
    99         return false;
    99         return false;
   100     }
   100     }
   101 
   101 
   102     //mSystemTone = new XQSystemToneService();
   102     mSystemTone = new XQSystemToneService();
   103     
   103 
   104     mIndicator = new HbIndicator();
   104     mIndicator = new HbIndicator();
   105     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
   105     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
   106         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
   106         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
   107 
   107 
   108     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   108     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   142 NmMailAgent::~NmMailAgent()
   142 NmMailAgent::~NmMailAgent()
   143 {
   143 {
   144     NM_FUNCTION;
   144     NM_FUNCTION;
   145 
   145 
   146     delete mVibra;
   146     delete mVibra;
   147     //delete mSystemTone;
   147     delete mSystemTone;
   148 
   148 
   149     qDeleteAll(mMailboxes);
   149     qDeleteAll(mMailboxes);
   150 
   150 
   151     //delete mSystemTone;
       
   152     NmDataPluginFactory::releaseInstance(mPluginFactory);
   151     NmDataPluginFactory::releaseInstance(mPluginFactory);
   153 }
   152 }
   154 
   153 
   155 /*!
   154 /*!
   156     Initialize the mailbox list with the current state
   155     Initialize the mailbox list with the current state
   330 
   329 
   331     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   330     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   332     bool changed = false;
   331     bool changed = false;
   333     if (mailboxInfo->mActive != active ||
   332     if (mailboxInfo->mActive != active ||
   334         refreshAlways) {
   333         refreshAlways) {
   335         
   334 
   336 		// store the new state to permanent storage
   335 		// store the new state to permanent storage
   337         storeMailboxActive(mailboxId, active);
   336         storeMailboxActive(mailboxId, active);
   338         
   337 
   339         mailboxInfo->mActive = active;
   338         mailboxInfo->mActive = active;
   340         changed = true;
   339         changed = true;
   341         if (active) {
   340         if (active) {
   342             // Mailbox is not yet assigned to any indicator
   341             // Mailbox is not yet assigned to any indicator
   343             if (mailboxInfo->mIndicatorIndex < 0) {
   342             if (mailboxInfo->mIndicatorIndex < 0) {
   426 */
   425 */
   427 bool NmMailAgent::launchMailbox(quint64 mailboxId)
   426 bool NmMailAgent::launchMailbox(quint64 mailboxId)
   428 {
   427 {
   429     NM_FUNCTION;
   428     NM_FUNCTION;
   430 
   429 
       
   430     bool ok(false);
   431     XQApplicationManager appManager;
   431     XQApplicationManager appManager;
   432     XQAiwRequest *request = appManager.create(
   432     XQAiwRequest *request = appManager.create(
   433         XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW, false);
   433         XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW, false);
   434     
   434     // Instance might be NULL if the service is not available.
   435     QList<QVariant> list;
   435     if (request) {
   436     list.append(QVariant(mailboxId));
   436         QList<QVariant> list;
   437     request->setArguments(list);
   437         list.append(QVariant(mailboxId));
   438     
   438         request->setArguments(list);
   439     bool ok = request->send();
   439 
   440     NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError()));
   440         ok = request->send();
   441     delete request;
   441         NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request->lastError()));
       
   442         delete request;
       
   443     }
   442     return ok;
   444     return ok;
   443 }
   445 }
   444 
   446 
   445 /*!
   447 /*!
   446     Get next free indicator index, starting from 0
   448     Get next free indicator index, starting from 0
   477 
   479 
   478     switch(event) {
   480     switch(event) {
   479         case NmMailboxCreated:
   481         case NmMailboxCreated:
   480             foreach (NmId mailboxId, mailboxIds) {
   482             foreach (NmId mailboxId, mailboxIds) {
   481                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   483                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   482                 
   484 
   483                 // make sure the mailbox activity data is reseted
   485                 // make sure the mailbox activity data is reseted
   484                 deleteStoredMailboxActivity(mailboxId);
   486                 deleteStoredMailboxActivity(mailboxId);
   485             }
   487             }
   486             break;
   488             break;
   487         case NmMailboxChanged:
   489         case NmMailboxChanged:
   509             break;
   511             break;
   510         case NmMailboxDeleted:
   512         case NmMailboxDeleted:
   511             foreach (NmId mailboxId, mailboxIds) {
   513             foreach (NmId mailboxId, mailboxIds) {
   512                 // Will hide also the indicator
   514                 // Will hide also the indicator
   513                 removeMailboxInfo(mailboxId);
   515                 removeMailboxInfo(mailboxId);
   514                 
   516 
   515                 // make sure the mailbox activity data is deleted
   517                 // make sure the mailbox activity data is deleted
   516                 deleteStoredMailboxActivity(mailboxId);
   518                 deleteStoredMailboxActivity(mailboxId);
   517             }
   519             }
   518             updateUnreadIndicator();
   520             updateUnreadIndicator();
   519             break;
   521             break;
   549 */
   551 */
   550 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data)
   552 void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data)
   551 {
   553 {
   552     NM_FUNCTION;
   554     NM_FUNCTION;
   553     Q_UNUSED(data);
   555     Q_UNUSED(data);
   554     
   556 
   555     // map the indicator type to mailbox
   557     // map the indicator type to mailbox
   556     NmMailboxInfo *info = getMailboxByType(type);
   558     NmMailboxInfo *info = getMailboxByType(type);
   557     if (info) {
   559     if (info) {
   558         info->mActive = false; // indicator is no longer active
   560         info->mActive = false; // indicator is no longer active
   559         info->mInboxActive = false; // also inbox does not activate the indicator
   561         info->mInboxActive = false; // also inbox does not activate the indicator
   581     bool updateNeeded = false;
   583     bool updateNeeded = false;
   582     bool activate = false;
   584     bool activate = false;
   583 
   585 
   584     switch (event) {
   586     switch (event) {
   585         case NmMessageCreated: {
   587         case NmMessageCreated: {
       
   588             // Check the new messages to make the indicator appear earlier
   586             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   589             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   587 
   590             
   588             // Check the new messages to make the indicator appear earlier
   591             // Inbox folder ID may be still unknown
   589             if (mailboxInfo->mSyncState == Synchronizing) {
   592             if (mailboxInfo->mInboxFolderId.id() == 0) {
   590                 
   593                 NmDataPluginInterface *plugin =
   591                 // Inbox folder ID may be still unknown
   594                     mPluginFactory->interfaceInstance(mailboxId);
   592                 if (mailboxInfo->mInboxFolderId.id()==0) {
   595 
   593                     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   596                 if (plugin) {
   594                     if (plugin) {
   597                     mailboxInfo->mInboxFolderId =
   595                         mailboxInfo->mInboxFolderId =
   598                         plugin->getStandardFolderId(mailboxId, NmFolderInbox);
   596                             plugin->getStandardFolderId(mailboxId, NmFolderInbox);
       
   597                     }
       
   598                 }
   599                 }
   599                 
   600             }
   600                 if (folderId == mailboxInfo->mInboxFolderId) {
   601 
   601                     foreach (NmId messageId, messageIds) {
   602             if (folderId == mailboxInfo->mInboxFolderId) {
   602                         bool messageUnread = false;
   603                 foreach (NmId messageId, messageIds) {
   603 
   604                     bool messageUnread = false;
   604                         // Check the message if we can either play a tone or if the "@" is 
   605 
   605                         // not visible at the moment
   606                     // Check the message if we can either play a tone or if the "@" is
   606                         if (mAlertToneAllowed || !mUnreadIndicatorActive) {
   607                     // not visible at the moment
   607                             if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   608                     if (mAlertToneAllowed || !mUnreadIndicatorActive) {
   608                                 if (messageUnread) {
   609                         if (getMessageUnreadInfo(folderId, messageId, mailboxId, messageUnread)) {
   609                                     mailboxInfo->mUnreadMailIdList.append(messageId);
   610                             if (messageUnread) {
   610                                     mailboxInfo->mInboxActive = true;
   611                                 mailboxInfo->mUnreadMailIdList.append(messageId);
   611                                     updateMailboxState(mailboxId, true, false);
   612                                 mailboxInfo->mInboxActive = true;
   612 
   613                                 updateMailboxState(mailboxId, true, false);
   613                                     // make the "@" appear immediatelly
   614 
   614                                     updateUnreadIndicator(true);
   615                                 // make the "@" appear immediatelly
   615 
   616                                 updateUnreadIndicator(true);
   616                                     // Play the tone as well
   617 
   617                                     playAlertTone();
   618                                 // Play the tone as well
   618                                 }
   619                                 playAlertTone();
   619                             }
   620                             }
   620                         }
   621                         }
   621                     }
   622                     }
   622                 }
   623                 }
   623             }
   624             }
   624             
   625 
   625             if (folderId==mailboxInfo->mInboxFolderId) {
   626             if (folderId==mailboxInfo->mInboxFolderId) {
   626                 mailboxInfo->mInboxCreatedMessages += messageIds.count();
   627                 mailboxInfo->mInboxCreatedMessages += messageIds.count();
   627             }
   628             }
   628 
   629 
   629             // When created a new mail in the outbox, we are in sending state
   630             // When created a new mail in the outbox, we are in sending state
   633 					NM_COMMENT("NmMailAgent: first mail in outbox");
   634 					NM_COMMENT("NmMailAgent: first mail in outbox");
   634                 }
   635                 }
   635                 // Always activate the indicator
   636                 // Always activate the indicator
   636                 activate = true;
   637                 activate = true;
   637                 updateNeeded = true;
   638                 updateNeeded = true;
   638                 
   639 
   639                 mailboxInfo->mOutboxMails += messageIds.count();
   640                 mailboxInfo->mOutboxMails += messageIds.count();
   640 				updateSendIndicator();
   641 				updateSendIndicator();
   641             }
   642             }
   642             break;
   643             break;
   643         }
   644         }
   679                 }
   680                 }
   680 
   681 
   681                 // The last mail was now deleted
   682                 // The last mail was now deleted
   682                 if (mailboxInfo->mOutboxMails == 0) {
   683                 if (mailboxInfo->mOutboxMails == 0) {
   683 					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   684 					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
   684                     // Keep it active if there is unread mails and inbox is still active
   685 					updateNeeded = true;
   685 					if (mailboxInfo->mInboxActive &&
   686 
       
   687 	                // Keep it active if there is unread mails and inbox is still active
       
   688  				    if (mailboxInfo->mInboxActive &&
   686 					    mailboxInfo->mUnreadMailIdList.count() > 0) {
   689 					    mailboxInfo->mUnreadMailIdList.count() > 0) {
   687 					    activate = true;
   690 					    activate = true;
   688                     }
   691 	                }
   689                     updateNeeded = true;
   692 				}
   690                 }
   693 				else {
       
   694 	                // Also update the indicator status if it is already shown
       
   695     	            if (mailboxInfo->mActive) {
       
   696 						activate = true;
       
   697             	        updateNeeded = true;
       
   698 					}
       
   699 				}
   691 				updateSendIndicator();
   700 				updateSendIndicator();
   692             }
   701             }
   693             break;
   702             break;
   694         }
   703         }
   695         default:
   704         default:
   911 {
   920 {
   912     NM_FUNCTION;
   921     NM_FUNCTION;
   913 	bool played = false;
   922 	bool played = false;
   914 
   923 
   915     if (mAlertToneAllowed) {
   924     if (mAlertToneAllowed) {
   916         //mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
   925         if (mSystemTone) {
   917         
   926             mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
       
   927         }
       
   928 
   918         // Execute the vibra effect.
   929         // Execute the vibra effect.
   919         if (mVibra) {
   930         if (mVibra) {
   920             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));
   931             TRAP_IGNORE(mVibra->StartVibraL(NmAgentDefaultVibraDuration));
   921         }
   932         }
   922         
   933 
   923         // play alert only once per minute
   934         // play alert only once per minute
   924         mAlertToneAllowed = false;
   935         mAlertToneAllowed = false;
   925         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   936         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
   926 		played = true;
   937 		played = true;
   927     }
   938     }
   971 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
   982 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
   972 {
   983 {
   973     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
   984     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
   974     XQSettingsManager mgr;
   985     XQSettingsManager mgr;
   975     XQCentralRepositoryUtils utils(mgr);
   986     XQCentralRepositoryUtils utils(mgr);
   976     
   987 
   977     if (active) {
   988     if (active) {
   978         // when mailbox is active, key can be deleted
   989         // when mailbox is active, key can be deleted
   979         utils.deleteKey(key);
   990         utils.deleteKey(key);
   980     }
   991     }
   981     else {
   992     else {
   982         utils.createKey(key,(int)active);
   993         utils.createKey(key,(int)active);
   983     }
   994     }
   984 }
   995 }
   985 
   996 
   986 /*!
   997 /*!
   987     Get the mailbox activity state. 
   998     Get the mailbox activity state.
   988     \param mailboxId id of the mailbox
   999     \param mailboxId id of the mailbox
   989     \return true if the mailbox is active or no information was stored earlier
  1000     \return true if the mailbox is active or no information was stored earlier
   990 */
  1001 */
   991 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
  1002 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
   992 {
  1003 {