emailservices/nmailagent/src/nmmailagent.cpp
changeset 48 10eaf342f539
parent 47 f83bd4ae1fe3
child 51 d845db10c0d4
--- a/emailservices/nmailagent/src/nmmailagent.cpp	Thu Jun 24 14:32:18 2010 +0300
+++ b/emailservices/nmailagent/src/nmmailagent.cpp	Tue Jun 29 17:12:28 2010 +0300
@@ -26,6 +26,8 @@
 #include <xqsettingsmanager.h>
 #include <xqcentralrepositoryutils.h>
 #include <xqsystemtoneservice.h>
+#include <xqsettingskey.h>
+#include <ProfileEngineInternalCRKeys.h>
 
 // CONSTS
 const int NmAgentIndicatorNotSet = -1;
@@ -35,6 +37,23 @@
 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";
+const XQCentralRepositorySettingsKey NmSilenceModeKey(KCRUidProfileEngine.iUid, KProEngSilenceMode);
+const int NmSilenceModeOn = 1;
+
+/*!
+    Helper method for finding out if XQSettingsKey and XQCentralRepositorySettingsKey points to
+    same key.
+
+    @param settingKey XQSettingsKey
+    @param cenrepSettingKey XQCentralRepositorySettingsKey
+    @return <code>true</code> if target, uid and key matches otherwise returns <code>false</code>
+*/
+bool keysEqual(const XQSettingsKey& settingKey, const XQCentralRepositorySettingsKey& cenrepSettingKey)
+{
+    return ((settingKey.target() == cenrepSettingKey.target()) &&
+            (settingKey.uid() == cenrepSettingKey.uid()) &&
+            (settingKey.key() == cenrepSettingKey.key()));
+}
 
 
 /*!
@@ -68,7 +87,9 @@
  mVibra(NULL),
  mAlertToneAllowed(true),
  mLastOutboxCount(0),
- mUnreadIndicatorActive(false)
+ mUnreadIndicatorActive(false),
+ mSettingManager(NULL),
+ mSilenceMode(NmSilenceModeOn)  // by default silent mode is on
 {
     NM_FUNCTION;
 }
@@ -99,8 +120,34 @@
         return false;
     }
 
+    // Check status of silent mode.
+    delete mSettingManager;
+    mSettingManager = NULL;
+    mSettingManager = new XQSettingsManager();
+    QVariant silenceMode = mSettingManager->readItemValue(NmSilenceModeKey,
+        XQSettingsManager::TypeInt);
+    mSilenceMode = silenceMode.toInt();
+
+    // Start monitoring silence mode key.
+    bool monitoring(mSettingManager->startMonitoring(NmSilenceModeKey, XQSettingsManager::TypeInt));
+    monitoring &= connect(mSettingManager,
+                          SIGNAL(valueChanged(const XQSettingsKey&, const QVariant&)),
+                          this,
+                          SLOT(valueChanged(const XQSettingsKey&, const QVariant&)),
+                          Qt::UniqueConnection);
+
+    // If silence mode monitoring can't be started, then change silence mode on to be sure
+    // that no tone is played if silence mode is turned on at somepoint.
+    if (!monitoring) {
+        mSilenceMode = NmSilenceModeOn;
+    }
+
+    delete mSystemTone;
+    mSystemTone = NULL;
     mSystemTone = new XQSystemToneService();
 
+    delete mIndicator;
+    mIndicator = NULL;
     mIndicator = new HbIndicator();
     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
@@ -131,6 +178,8 @@
     }
 
     // Construct the vibra interface instance.
+    delete mVibra;
+    mVibra = NULL;
     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
 
     // load all current mailboxes
@@ -145,7 +194,10 @@
 
     delete mVibra;
     delete mSystemTone;
-
+    if (mSettingManager) {
+        mSettingManager->stopMonitoring(NmSilenceModeKey);
+        delete mSettingManager;
+    }
     qDeleteAll(mMailboxes);
 
     NmDataPluginFactory::releaseInstance(mPluginFactory);
@@ -296,6 +348,8 @@
 */
 int NmMailAgent::getTotalUnreadCount() const
 {
+    NM_FUNCTION;
+
     int unreads = 0;
     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
         if (mailbox->mActive && mailbox->mInboxActive) {
@@ -311,6 +365,8 @@
 */
 bool NmMailAgent::updateUnreadIndicator()
 {
+    NM_FUNCTION;
+
     int unreads = getTotalUnreadCount();
     return updateUnreadIndicator(unreads>0);
 }
@@ -403,8 +459,8 @@
 bool NmMailAgent::updateUnreadIndicator(bool active)
 {
     NM_FUNCTION;
+
     bool activated = false;
-
     if (active != mUnreadIndicatorActive) {
         if (active) {
             mIndicator->activate(NmUnreadIndicatorName);
@@ -530,6 +586,8 @@
 */
 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
 {
+    NM_FUNCTION;
+
     foreach (NmMailboxInfo *mailbox, mMailboxes) {
         // mailbox is shown in indicators
         if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
@@ -567,6 +625,22 @@
 }
 
 /*!
+    Called when cenrep key value has been changed.
+    - only silence mode key handled
+
+    @param key changed key
+    @param value value for a key.
+*/
+void NmMailAgent::valueChanged(const XQSettingsKey& key, const QVariant& value)
+{
+    NM_FUNCTION;
+
+    if(keysEqual(key, NmSilenceModeKey)) {
+        mSilenceMode = value.toInt();
+    }
+}
+
+/*!
     Received from NmFrameworkAdapter messageEvent signal
     \sa NmFrameworkAdapter
 */
@@ -577,6 +651,7 @@
             const NmId& mailboxId)
 {
     NM_FUNCTION;
+
     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
         arg(event).arg(mailboxId.id()));
 
@@ -919,10 +994,12 @@
 bool NmMailAgent::playAlertTone()
 {
     NM_FUNCTION;
-	bool played = false;
+	bool played(false);
 
     if (mAlertToneAllowed) {
-        if (mSystemTone) {
+        // Play tone only if system tone service is available and
+        // phone is not in silence mode.
+        if (mSystemTone && !mSilenceMode) {
             mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
         }
 
@@ -955,6 +1032,8 @@
 */
 void NmMailAgent::updateSendIndicator()
 {
+    NM_FUNCTION;
+
     // Get number of mails in outboxes
     int outboxMails = 0;
     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
@@ -981,16 +1060,19 @@
 */
 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
 {
-    XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
-    XQSettingsManager mgr;
-    XQCentralRepositoryUtils utils(mgr);
+    NM_FUNCTION;
+
+    if (mSettingManager) {
+        XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
+        XQCentralRepositoryUtils utils(*mSettingManager);
 
-    if (active) {
-        // when mailbox is active, key can be deleted
-        utils.deleteKey(key);
-    }
-    else {
-        utils.createKey(key,(int)active);
+        if (active) {
+            // when mailbox is active, key can be deleted
+            utils.deleteKey(key);
+        }
+        else {
+            utils.createKey(key,(int)active);
+        }
     }
 }
 
@@ -1001,16 +1083,18 @@
 */
 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
 {
-    XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
-    XQSettingsManager mgr;
-    QVariant value = mgr.readItemValue(key,XQSettingsManager::TypeInt);
-    if (!value.isValid()) {
-        // no valid value found, key missing?
-        NM_COMMENT("NmMailAgent::isMailboxActive - value not valid");
-        return true;
+    NM_FUNCTION;
+
+    bool mailboxActive(true);
+    if (mSettingManager) {
+        XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
+        QVariant value = mSettingManager->readItemValue(key, XQSettingsManager::TypeInt);
+        if (value.isValid()) {
+            NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
+            mailboxActive = value.toInt();
+        }
     }
-    NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
-    return value.toInt();
+    return mailboxActive;
 }
 
 /*!
@@ -1019,9 +1103,10 @@
 */
 void NmMailAgent::deleteStoredMailboxActivity(const NmId &mailboxId)
 {
+    NM_FUNCTION;
+
     // deactivation delete the key too
     storeMailboxActive(mailboxId,false);
 }
 
 // End of file
-