emailservices/nmailagent/src/nmmailagent.cpp
changeset 43 99bcbff212ad
parent 40 2c62ef3caffd
child 44 c2d07d913565
--- a/emailservices/nmailagent/src/nmmailagent.cpp	Mon May 24 21:02:02 2010 +0300
+++ b/emailservices/nmailagent/src/nmmailagent.cpp	Fri May 28 13:56:43 2010 +0300
@@ -15,12 +15,21 @@
 *
 */
 
+#include <xqservicerequest.h>
+#include <email_services_api.h>
+
+#include "emailtrace.h"
 #include "nmmailagentheaders.h"
 #include "nmmailagent.h"
 
+#include <xqsystemtoneservice.h>
+
 // CONSTS
 const int NmAgentIndicatorNotSet = -1;
 const int NmAgentAlertToneTimer = 60000; // 60s
+static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
+static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
+static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
 
 
 /*!
@@ -31,6 +40,8 @@
 
 NmMailboxInfo::NmMailboxInfo()
 {
+    NM_FUNCTION;
+
     mId = 0;
     mIndicatorIndex = NmAgentIndicatorNotSet;
     mSyncState = SyncComplete;
@@ -45,11 +56,14 @@
 }
 
 NmMailAgent::NmMailAgent() :
+ mIndicator(NULL),
+ mSystemTone(NULL),
  mPluginFactory(NULL),
- mSendingState(false),
- mAlertToneAllowed(true)
+ mAlertToneAllowed(true),
+ mLastOutboxCount(0),
+ mUnreadIndicatorActive(false)
 {
-    NMLOG("NmMailAgent::NmMailAgent");
+    NM_FUNCTION;
 }
 
 /*!
@@ -57,7 +71,8 @@
 */
 void NmMailAgent::delayedStart()
 {
-	NMLOG("NmMailAgent::delayedStart");
+    NM_FUNCTION;
+
 	if (!init()) {
 		// Initialisation failed. Quit the agent.
 		QCoreApplication::exit(1);
@@ -69,13 +84,20 @@
 */
 bool NmMailAgent::init()
 {
-	NMLOG("NmMailAgent::init");
+    NM_FUNCTION;
+
     mPluginFactory = NmDataPluginFactory::instance();
     if (!mPluginFactory) {
-        NMLOG("NmMailAgent::init PluginFactory not created");
+        NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
         return false;
     }
 
+    //mSystemTone = new XQSystemToneService();
+    
+    mIndicator = new HbIndicator();
+    connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
+        this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
+
     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
 
     foreach (QObject *plugin, *plugins) {
@@ -109,7 +131,10 @@
 
 NmMailAgent::~NmMailAgent()
 {
+    NM_FUNCTION;
+
     qDeleteAll(mMailboxes);
+    //delete mSystemTone;
     NmDataPluginFactory::releaseInstance(mPluginFactory);
 }
 
@@ -118,7 +143,8 @@
 */
 void NmMailAgent::initMailboxStatus()
 {
-    NMLOG("NmMailAgent::initMailboxStatus");
+    NM_FUNCTION;
+
     QList<NmMailbox*> mailboxes;
     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
 
@@ -153,6 +179,7 @@
         }
         qDeleteAll(mailboxes);
     }
+    updateSendIndicator();
 }
 
 /*!
@@ -163,7 +190,8 @@
 */
 bool NmMailAgent::updateUnreadCount(const NmId &mailboxId, NmMailboxInfo &mailboxInfo)
 {
-    NMLOG("NmMailAgent::getUnreadCount");
+    NM_FUNCTION;
+
     int newUnreadMessages(0);
 
     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
@@ -202,8 +230,8 @@
 		// Save updated list of unread message IDs
         mailboxInfo.mUnreadMailIdList = newUnreadMessageIdList;
     }
-	NMLOG(QString("NmMailAgent::getUnreadCount count=%1 new=%2").
-	    arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages));
+    NM_COMMENT(QString("NmMailAgent::getUnreadCount(): count=%1, new=%2").
+        arg(mailboxInfo.mUnreadMailIdList.count()).arg(newUnreadMessages));
 
     return (newUnreadMessages > 0);
 }
@@ -216,7 +244,8 @@
 */
 int NmMailAgent::getOutboxCount(const NmId &mailboxId)
 {
-    NMLOG("NmMailAgent::getOutboxCount");
+    NM_FUNCTION;
+
     int count(0);
 
     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
@@ -231,12 +260,37 @@
 		count = messageList.count();
 		qDeleteAll(messageList);
     }
-    NMLOG(QString("NmMailAgent::getOutboxCount count=%1").arg(count));
+    NM_COMMENT(QString("NmMailAgent::getOutboxCount(): count=%1").arg(count));
 
     return count;
 }
 
 /*!
+    Get list of unread counts in active mailboxes
+	\returns total number of unread mails
+*/
+int NmMailAgent::getTotalUnreadCount() const
+{
+    int unreads = 0;
+    foreach (const NmMailboxInfo *mailbox, mMailboxes) {
+        if (mailbox->mActive) {
+            unreads += mailbox->mUnreadMailIdList.count();
+        }
+    }
+    return unreads;
+}
+
+/*!
+    Update the "@" indicator state according to unread state
+    \return true if the indicator was activated
+*/
+bool NmMailAgent::updateUnreadIndicator()
+{
+    int unreads = getTotalUnreadCount();
+    return updateUnreadIndicator(unreads>0);
+}
+
+/*!
     Update the mailbox visibility and status
     \param mailboxId id of the mailbox
     \param active visibility state of the mailbox
@@ -246,14 +300,7 @@
 bool NmMailAgent::updateMailboxState(const NmId &mailboxId,
     bool active, bool refreshAlways)
 {
-    // Update the global sending state
-    mSendingState = false;
-    foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
-        if (mailboxInfo->mOutboxMails>0) {
-            mSendingState = true;
-            break;
-        }
-    }
+    NM_FUNCTION;
 
     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
     bool changed = false;
@@ -264,7 +311,7 @@
         if (active) {
             // Mailbox is not yet assigned to any indicator
             if (mailboxInfo->mIndicatorIndex < 0) {
-                mailboxInfo->mIndicatorIndex = getIndicatorIndex();
+                mailboxInfo->mIndicatorIndex = getFreeIndicatorIndex();
             }
 
             updateIndicator(true,*mailboxInfo);
@@ -290,11 +337,12 @@
 bool NmMailAgent::updateIndicator(bool active,
     const NmMailboxInfo& mailboxInfo)
 {
-    NMLOG(QString("NmMailAgent::updateIndicator index=%1 active=%2").
+    NM_FUNCTION;
+    NM_COMMENT(QString("NmMailAgent::updateIndicator(): index=%1, active=%2").
         arg(mailboxInfo.mIndicatorIndex).arg(active));
 
     bool ok = false;
-    QString name = QString("com.nokia.nmail.indicatorplugin_%1/1.0").
+    QString name = QString(NmMailboxIndicatorType).
         arg(mailboxInfo.mIndicatorIndex);
 
     QList<QVariant> list;
@@ -305,15 +353,62 @@
     list.append(mailboxInfo.mConnectState);
     list.append(mailboxInfo.mOutboxMails);
     list.append(mailboxInfo.mIconName);
-    list.append(mSendingState);
+    list.append(mLastOutboxCount);
 
-    HbIndicator indicator;
     if (active) {
-        ok = indicator.activate(name,list);
+        ok = mIndicator->activate(name,list);
     }
     else {
-        ok = indicator.deactivate(name,list);
+        ok = mIndicator->deactivate(name,list);
+    }
+
+    updateUnreadIndicator();
+
+    return ok;
+}
+
+/*!
+    update the unread indicator state
+    \return true if the indicator was activated
+*/
+bool NmMailAgent::updateUnreadIndicator(bool active)
+{
+    NM_FUNCTION;
+    bool activated = false;
+
+    if (active) {
+        mIndicator->activate(NmUnreadIndicatorName);
+        if (!mUnreadIndicatorActive) {
+            activated = true;
+        }
     }
+    else {
+        mIndicator->deactivate(NmUnreadIndicatorName);
+    }
+    mUnreadIndicatorActive = active;
+    
+    return activated;
+}
+
+/*!
+    Opens inbox view to specific mailbox
+    \return true if inbox is succesfully opened
+*/
+bool NmMailAgent::launchMailbox(quint64 mailboxId)
+{
+    NM_FUNCTION;
+
+    XQServiceRequest request(
+        emailFullServiceNameMailbox,
+        emailOperationViewInbox,
+        true);
+
+    QList<QVariant> list;
+    list.append(QVariant(mailboxId));
+
+    request.setArguments(list);
+    bool ok = request.send();
+    NM_COMMENT(QString("Launch ok=%1 error=%2").arg(ok).arg(request.latestError()));
     return ok;
 }
 
@@ -321,8 +416,10 @@
     Get next free indicator index, starting from 0
     @return index of the indicator that is available
  */
-int NmMailAgent::getIndicatorIndex()
+int NmMailAgent::getFreeIndicatorIndex()
 {
+    NM_FUNCTION;
+
     int index = 0;
     bool found(false);
     do {
@@ -345,7 +442,8 @@
 */
 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
 {
-    NMLOG(QString("NmMailAgent::handleMailboxEvent %1").arg(event));
+    NM_FUNCTION;
+    NM_COMMENT(QString("NmMailAgent::handleMailboxEvent(): event=%1").arg(event));
 
     switch(event) {
         case NmMailboxCreated:
@@ -381,6 +479,7 @@
                 // Will hide also the indicator
                 removeMailboxInfo(mailboxId);
             }
+            updateUnreadIndicator();
             break;
         default:
             break;
@@ -388,6 +487,47 @@
 }
 
 /*!
+   Map the type name to mailbox info
+    \return NULL if no mailbox match the type
+*/
+NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
+{
+    foreach (NmMailboxInfo *mailbox, mMailboxes) {
+        // mailbox is shown in indicators
+        if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
+            QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex);
+
+            // type names match(!)
+            if(type==typeName) {
+                return mailbox;
+            }
+        }
+    }
+    return NULL;
+}
+
+/*!
+    Called when indicator is clicked from the indicator menu
+    - indicator will be hide from the menu
+    - mailbox will be launched
+*/
+void NmMailAgent::indicatorActivated(const QString &type, const QVariantMap &data)
+{
+    NM_FUNCTION;
+    Q_UNUSED(data);
+    
+    // map the indicator type to mailbox
+    NmMailboxInfo *info = getMailboxByType(type);
+    if (info) {
+        info->mLastSeenTime = QDateTime::currentDateTime();
+        info->mActive = false; // indicator is no longer active
+        updateUnreadIndicator();
+
+        launchMailbox(info->mId.id());
+    }
+}
+
+/*!
     Received from NmFrameworkAdapter messageEvent signal
     \sa NmFrameworkAdapter
 */
@@ -397,7 +537,10 @@
             const QList<NmId> &messageIds,
             const NmId& mailboxId)
 {
-    NMLOG(QString("NmMailAgent::handleMessageEvent %1 %2").arg(event).arg(mailboxId.id()));
+    NM_FUNCTION;
+    NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
+        arg(event).arg(mailboxId.id()));
+
     bool updateNeeded = false;
     bool activate = false;
 
@@ -425,6 +568,12 @@
                             if (messageUnread) {
                                 mailboxInfo->mUnreadMailIdList.append(messageId);
                                 updateMailboxState(mailboxId, true, false);
+
+                                // make the "@" appear immediatelly
+                                updateUnreadIndicator(true);
+								
+                                // Play the tone as well
+                                playAlertTone();
                             }
                         }
                     }
@@ -438,11 +587,12 @@
             if (mailboxInfo->mOutboxFolderId == folderId) {
                 // The first mail created in the outbox
                 if (mailboxInfo->mOutboxMails <= 0) {
-					NMLOG("NmMailAgent: first mail in outbox");
+					NM_COMMENT("NmMailAgent: first mail in outbox");
 					activate = true;
                     updateNeeded = true;
                 }
                 mailboxInfo->mOutboxMails += messageIds.count();
+				updateSendIndicator();
             }
             break;
         }
@@ -460,8 +610,8 @@
                 int oldCount = mailboxInfo->mUnreadMailIdList.count();
                 activate = updateUnreadCount(mailboxId, *mailboxInfo);
 
-                // new unread mails found or no more unread mails in the mailbox
-                if (activate || (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0)) {
+                // new unread mails found or no more unread mails in the inbox
+                if (oldCount>0 && mailboxInfo->mUnreadMailIdList.count()==0) {
                     updateNeeded = true;
                 }
             }
@@ -485,12 +635,12 @@
 
                 // The last mail was now deleted
                 if (mailboxInfo->mOutboxMails == 0) {
-					NMLOG("NmMailAgent: last mail deleted from outbox");
-
+					NM_COMMENT("NmMailAgent: last mail deleted from outbox");
                     // Keep it active if there is unread mails
                     activate = mailboxInfo->mUnreadMailIdList.count() > 0;
                     updateNeeded = true;
                 }
+				updateSendIndicator();
             }
             break;
         }
@@ -499,6 +649,8 @@
     }
 
     if (updateNeeded) {
+        updateUnreadIndicator();
+
         NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
         updateMailboxState(mailboxId,
             activate, true /* force refresh */);
@@ -513,7 +665,10 @@
             NmSyncState state,
             const NmOperationCompletionEvent &event)
 {
-    NMLOG(QString("NmMailAgent::handleSyncStateEvent %1 %2").arg(state).arg(event.mMailboxId.id()));
+    NM_FUNCTION;
+    NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): state=%1, id=%2").
+        arg(state).arg(event.mMailboxId.id()));
+
     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
     if (info) {
         info->mSyncState = state;
@@ -537,12 +692,15 @@
                 info->mOutboxMails;
 
             // Refresh the indicator if messages created or changed
-            NMLOG(QString(" created=%1, changed=%2, deleted=%3").
+            NM_COMMENT(QString("NmMailAgent::handleSyncStateEvent(): "
+                "created=%1, changed=%2, deleted=%3").
                 arg(info->mInboxCreatedMessages).
                 arg(info->mInboxChangedMessages).
                 arg(info->mInboxDeletedMessages));
             bool refresh = (info->mInboxCreatedMessages > 0) || (info->mInboxChangedMessages > 0);
 
+            updateUnreadIndicator();
+
             if (activate) {
                 updateMailboxState(event.mMailboxId, active, refresh);
             }
@@ -556,8 +714,11 @@
 */
 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId, int errorcode)
 {
-	Q_UNUSED(errorcode);
-    NMLOG(QString("NmMailAgent::handleConnectionEvent %1 %2 %3").arg(state).arg(mailboxId.id()).arg(errorcode));
+    NM_FUNCTION;
+    NM_COMMENT(QString("NmMailAgent::handleConnectionEvent(): state=%1, id=%2").
+        arg(state).arg(mailboxId.id()));
+    Q_UNUSED(errorcode);
+
     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
     if (mailboxInfo) {
         // Connecting, Connected, Disconnecting, Disconnected
@@ -571,6 +732,8 @@
 */
 bool NmMailAgent::removeMailboxInfo(const NmId &id)
 {
+    NM_FUNCTION;
+
     bool found = false;
     foreach (NmMailboxInfo *mailbox, mMailboxes) {
         if (mailbox->mId == id) {
@@ -592,6 +755,8 @@
 */
 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
 {
+    NM_FUNCTION;
+
     // get information of the mailbox
     NmMailbox *mailbox = NULL;
     NmMailboxInfo *info = NULL;
@@ -612,6 +777,8 @@
 */
 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox &mailbox, NmDataPluginInterface *plugin)
 {
+    NM_FUNCTION;
+
     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
     mailboxInfo->mId = mailbox.id();
     mailboxInfo->mName = mailbox.name();
@@ -645,6 +812,8 @@
 */
 NmMailboxInfo *NmMailAgent::getMailboxInfo(const NmId &id)
 {
+    NM_FUNCTION;
+
     foreach (NmMailboxInfo *mailbox, mMailboxes) {
         if (mailbox->mId == id) {
             return mailbox;
@@ -666,7 +835,7 @@
 bool NmMailAgent::getMessageUnreadInfo(const NmId &folderId,
     const NmId &messageId, const NmId &mailboxId, bool &unreadMessage)
 {
-    NMLOG("NmMailAgent::messageInfo");
+    NM_FUNCTION;
 
     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
     bool ok = false;
@@ -687,15 +856,23 @@
 }
 
 /*!
-    Plays email alert tune when new messages arrive
+    Plays email alert tone when new messages arrive
+	\returns true if the tone was played
 */
-void NmMailAgent::playAlertTone()
+bool NmMailAgent::playAlertTone()
 {
+    NM_FUNCTION;
+	bool played = false;
+
     if (mAlertToneAllowed) {
-        // play alert
+        //mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
+        
+        // play alert only once per minute
         mAlertToneAllowed = false;
         QTimer::singleShot(NmAgentAlertToneTimer, this, SLOT(enableAlertTone()));
+		played = true;
     }
+	return played;
 }
 
 /*!
@@ -703,8 +880,34 @@
 */
 void NmMailAgent::enableAlertTone()
 {
+    NM_FUNCTION;
+
     mAlertToneAllowed = true;
 }
 
+/*!
+    update send indicator according to outbox state
+*/
+void NmMailAgent::updateSendIndicator()
+{
+    // Get number of mails in outboxes
+    int outboxMails = 0;
+    foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
+        outboxMails += mailboxInfo->mOutboxMails;
+    }
+
+    if( outboxMails == 0 ) {
+        mIndicator->deactivate(NmSendIndicatorName);
+    }
+    else if (outboxMails > mLastOutboxCount) {
+        // New mails detected in outbox
+
+    	// indicator will disappear automatically after a delay
+        mIndicator->activate(NmSendIndicatorName);
+	}
+
+	mLastOutboxCount = outboxMails;
+}
+
 // End of file