emailservices/nmailagent/src/nmmailagent.cpp
changeset 48 10eaf342f539
parent 47 f83bd4ae1fe3
child 51 d845db10c0d4
equal deleted inserted replaced
47:f83bd4ae1fe3 48:10eaf342f539
    24 #include <xqaiwrequest.h>
    24 #include <xqaiwrequest.h>
    25 
    25 
    26 #include <xqsettingsmanager.h>
    26 #include <xqsettingsmanager.h>
    27 #include <xqcentralrepositoryutils.h>
    27 #include <xqcentralrepositoryutils.h>
    28 #include <xqsystemtoneservice.h>
    28 #include <xqsystemtoneservice.h>
       
    29 #include <xqsettingskey.h>
       
    30 #include <ProfileEngineInternalCRKeys.h>
    29 
    31 
    30 // CONSTS
    32 // CONSTS
    31 const int NmAgentIndicatorNotSet = -1;
    33 const int NmAgentIndicatorNotSet = -1;
    32 const int NmAgentAlertToneTimer = 60000; // 60s
    34 const int NmAgentAlertToneTimer = 60000; // 60s
    33 const int NmAgentDefaultVibraDuration = 1000; // 1 second
    35 const int NmAgentDefaultVibraDuration = 1000; // 1 second
    34 static const quint32 NmRepositoryId = 0x2002C326;
    36 static const quint32 NmRepositoryId = 0x2002C326;
    35 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    37 static const QString NmMailboxIndicatorType = "com.nokia.nmail.indicatorplugin_%1/1.0";
    36 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    38 static const QString NmSendIndicatorName = "com.nokia.nmail.indicatorplugin.send/1.0";
    37 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
    39 static const QString NmUnreadIndicatorName = "com.nokia.nmail.indicatorplugin.unread/1.0";
       
    40 const XQCentralRepositorySettingsKey NmSilenceModeKey(KCRUidProfileEngine.iUid, KProEngSilenceMode);
       
    41 const int NmSilenceModeOn = 1;
       
    42 
       
    43 /*!
       
    44     Helper method for finding out if XQSettingsKey and XQCentralRepositorySettingsKey points to
       
    45     same key.
       
    46 
       
    47     @param settingKey XQSettingsKey
       
    48     @param cenrepSettingKey XQCentralRepositorySettingsKey
       
    49     @return <code>true</code> if target, uid and key matches otherwise returns <code>false</code>
       
    50 */
       
    51 bool keysEqual(const XQSettingsKey& settingKey, const XQCentralRepositorySettingsKey& cenrepSettingKey)
       
    52 {
       
    53     return ((settingKey.target() == cenrepSettingKey.target()) &&
       
    54             (settingKey.uid() == cenrepSettingKey.uid()) &&
       
    55             (settingKey.key() == cenrepSettingKey.key()));
       
    56 }
    38 
    57 
    39 
    58 
    40 /*!
    59 /*!
    41     \class NmMailAgent
    60     \class NmMailAgent
    42 
    61 
    66  mSystemTone(NULL),
    85  mSystemTone(NULL),
    67  mPluginFactory(NULL),
    86  mPluginFactory(NULL),
    68  mVibra(NULL),
    87  mVibra(NULL),
    69  mAlertToneAllowed(true),
    88  mAlertToneAllowed(true),
    70  mLastOutboxCount(0),
    89  mLastOutboxCount(0),
    71  mUnreadIndicatorActive(false)
    90  mUnreadIndicatorActive(false),
       
    91  mSettingManager(NULL),
       
    92  mSilenceMode(NmSilenceModeOn)  // by default silent mode is on
    72 {
    93 {
    73     NM_FUNCTION;
    94     NM_FUNCTION;
    74 }
    95 }
    75 
    96 
    76 /*!
    97 /*!
    97     if (!mPluginFactory) {
   118     if (!mPluginFactory) {
    98         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
   119         NM_ERROR(1,"NmMailAgent::init(): PluginFactory not created");
    99         return false;
   120         return false;
   100     }
   121     }
   101 
   122 
       
   123     // Check status of silent mode.
       
   124     delete mSettingManager;
       
   125     mSettingManager = NULL;
       
   126     mSettingManager = new XQSettingsManager();
       
   127     QVariant silenceMode = mSettingManager->readItemValue(NmSilenceModeKey,
       
   128         XQSettingsManager::TypeInt);
       
   129     mSilenceMode = silenceMode.toInt();
       
   130 
       
   131     // Start monitoring silence mode key.
       
   132     bool monitoring(mSettingManager->startMonitoring(NmSilenceModeKey, XQSettingsManager::TypeInt));
       
   133     monitoring &= connect(mSettingManager,
       
   134                           SIGNAL(valueChanged(const XQSettingsKey&, const QVariant&)),
       
   135                           this,
       
   136                           SLOT(valueChanged(const XQSettingsKey&, const QVariant&)),
       
   137                           Qt::UniqueConnection);
       
   138 
       
   139     // If silence mode monitoring can't be started, then change silence mode on to be sure
       
   140     // that no tone is played if silence mode is turned on at somepoint.
       
   141     if (!monitoring) {
       
   142         mSilenceMode = NmSilenceModeOn;
       
   143     }
       
   144 
       
   145     delete mSystemTone;
       
   146     mSystemTone = NULL;
   102     mSystemTone = new XQSystemToneService();
   147     mSystemTone = new XQSystemToneService();
   103 
   148 
       
   149     delete mIndicator;
       
   150     mIndicator = NULL;
   104     mIndicator = new HbIndicator();
   151     mIndicator = new HbIndicator();
   105     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
   152     connect(mIndicator,SIGNAL(userActivated(const QString &, const QVariantMap&)),
   106         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
   153         this, SLOT(indicatorActivated(const QString&, const QVariantMap&)));
   107 
   154 
   108     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   155     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   129 				Qt::UniqueConnection);
   176 				Qt::UniqueConnection);
   130     	}
   177     	}
   131     }
   178     }
   132 
   179 
   133     // Construct the vibra interface instance.
   180     // Construct the vibra interface instance.
       
   181     delete mVibra;
       
   182     mVibra = NULL;
   134     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
   183     TRAP_IGNORE(mVibra = CHWRMVibra::NewL());
   135 
   184 
   136     // load all current mailboxes
   185     // load all current mailboxes
   137     initMailboxStatus();
   186     initMailboxStatus();
   138 
   187 
   143 {
   192 {
   144     NM_FUNCTION;
   193     NM_FUNCTION;
   145 
   194 
   146     delete mVibra;
   195     delete mVibra;
   147     delete mSystemTone;
   196     delete mSystemTone;
   148 
   197     if (mSettingManager) {
       
   198         mSettingManager->stopMonitoring(NmSilenceModeKey);
       
   199         delete mSettingManager;
       
   200     }
   149     qDeleteAll(mMailboxes);
   201     qDeleteAll(mMailboxes);
   150 
   202 
   151     NmDataPluginFactory::releaseInstance(mPluginFactory);
   203     NmDataPluginFactory::releaseInstance(mPluginFactory);
   152 }
   204 }
   153 
   205 
   294     Get list of unread counts in active mailboxes
   346     Get list of unread counts in active mailboxes
   295 	\returns total number of unread mails
   347 	\returns total number of unread mails
   296 */
   348 */
   297 int NmMailAgent::getTotalUnreadCount() const
   349 int NmMailAgent::getTotalUnreadCount() const
   298 {
   350 {
       
   351     NM_FUNCTION;
       
   352 
   299     int unreads = 0;
   353     int unreads = 0;
   300     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   354     foreach (const NmMailboxInfo *mailbox, mMailboxes) {
   301         if (mailbox->mActive && mailbox->mInboxActive) {
   355         if (mailbox->mActive && mailbox->mInboxActive) {
   302             unreads += mailbox->mUnreadMailIdList.count();
   356             unreads += mailbox->mUnreadMailIdList.count();
   303         }
   357         }
   309     Update the "@" indicator state according to unread state
   363     Update the "@" indicator state according to unread state
   310     \return true if the indicator was activated
   364     \return true if the indicator was activated
   311 */
   365 */
   312 bool NmMailAgent::updateUnreadIndicator()
   366 bool NmMailAgent::updateUnreadIndicator()
   313 {
   367 {
       
   368     NM_FUNCTION;
       
   369 
   314     int unreads = getTotalUnreadCount();
   370     int unreads = getTotalUnreadCount();
   315     return updateUnreadIndicator(unreads>0);
   371     return updateUnreadIndicator(unreads>0);
   316 }
   372 }
   317 
   373 
   318 /*!
   374 /*!
   401     \return true if the indicator was activated
   457     \return true if the indicator was activated
   402 */
   458 */
   403 bool NmMailAgent::updateUnreadIndicator(bool active)
   459 bool NmMailAgent::updateUnreadIndicator(bool active)
   404 {
   460 {
   405     NM_FUNCTION;
   461     NM_FUNCTION;
       
   462 
   406     bool activated = false;
   463     bool activated = false;
   407 
       
   408     if (active != mUnreadIndicatorActive) {
   464     if (active != mUnreadIndicatorActive) {
   409         if (active) {
   465         if (active) {
   410             mIndicator->activate(NmUnreadIndicatorName);
   466             mIndicator->activate(NmUnreadIndicatorName);
   411             activated = true;
   467             activated = true;
   412         }
   468         }
   528    Map the type name to mailbox info
   584    Map the type name to mailbox info
   529     \return NULL if no mailbox match the type
   585     \return NULL if no mailbox match the type
   530 */
   586 */
   531 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
   587 NmMailboxInfo *NmMailAgent::getMailboxByType(const QString &type)
   532 {
   588 {
       
   589     NM_FUNCTION;
       
   590 
   533     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   591     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   534         // mailbox is shown in indicators
   592         // mailbox is shown in indicators
   535         if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
   593         if (mailbox->mIndicatorIndex >= 0 && mailbox->mActive) {
   536             QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex);
   594             QString typeName = QString(NmMailboxIndicatorType).arg(mailbox->mIndicatorIndex);
   537 
   595 
   565         launchMailbox(info->mId.id());
   623         launchMailbox(info->mId.id());
   566     }
   624     }
   567 }
   625 }
   568 
   626 
   569 /*!
   627 /*!
       
   628     Called when cenrep key value has been changed.
       
   629     - only silence mode key handled
       
   630 
       
   631     @param key changed key
       
   632     @param value value for a key.
       
   633 */
       
   634 void NmMailAgent::valueChanged(const XQSettingsKey& key, const QVariant& value)
       
   635 {
       
   636     NM_FUNCTION;
       
   637 
       
   638     if(keysEqual(key, NmSilenceModeKey)) {
       
   639         mSilenceMode = value.toInt();
       
   640     }
       
   641 }
       
   642 
       
   643 /*!
   570     Received from NmFrameworkAdapter messageEvent signal
   644     Received from NmFrameworkAdapter messageEvent signal
   571     \sa NmFrameworkAdapter
   645     \sa NmFrameworkAdapter
   572 */
   646 */
   573 void NmMailAgent::handleMessageEvent(
   647 void NmMailAgent::handleMessageEvent(
   574             NmMessageEvent event,
   648             NmMessageEvent event,
   575             const NmId &folderId,
   649             const NmId &folderId,
   576             const QList<NmId> &messageIds,
   650             const QList<NmId> &messageIds,
   577             const NmId& mailboxId)
   651             const NmId& mailboxId)
   578 {
   652 {
   579     NM_FUNCTION;
   653     NM_FUNCTION;
       
   654 
   580     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
   655     NM_COMMENT(QString("NmMailAgent::handleMessageEvent(): event=%1, id=%2").
   581         arg(event).arg(mailboxId.id()));
   656         arg(event).arg(mailboxId.id()));
   582 
   657 
   583     bool updateNeeded = false;
   658     bool updateNeeded = false;
   584     bool activate = false;
   659     bool activate = false;
   917 	\returns true if the tone was played
   992 	\returns true if the tone was played
   918 */
   993 */
   919 bool NmMailAgent::playAlertTone()
   994 bool NmMailAgent::playAlertTone()
   920 {
   995 {
   921     NM_FUNCTION;
   996     NM_FUNCTION;
   922 	bool played = false;
   997 	bool played(false);
   923 
   998 
   924     if (mAlertToneAllowed) {
   999     if (mAlertToneAllowed) {
   925         if (mSystemTone) {
  1000         // Play tone only if system tone service is available and
       
  1001         // phone is not in silence mode.
       
  1002         if (mSystemTone && !mSilenceMode) {
   926             mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
  1003             mSystemTone->playTone(XQSystemToneService::EmailAlertTone);
   927         }
  1004         }
   928 
  1005 
   929         // Execute the vibra effect.
  1006         // Execute the vibra effect.
   930         if (mVibra) {
  1007         if (mVibra) {
   953 /*!
  1030 /*!
   954     update send indicator according to outbox state
  1031     update send indicator according to outbox state
   955 */
  1032 */
   956 void NmMailAgent::updateSendIndicator()
  1033 void NmMailAgent::updateSendIndicator()
   957 {
  1034 {
       
  1035     NM_FUNCTION;
       
  1036 
   958     // Get number of mails in outboxes
  1037     // Get number of mails in outboxes
   959     int outboxMails = 0;
  1038     int outboxMails = 0;
   960     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
  1039     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
   961         outboxMails += mailboxInfo->mOutboxMails;
  1040         outboxMails += mailboxInfo->mOutboxMails;
   962     }
  1041     }
   979     \param mailboxId id of the mailbox
  1058     \param mailboxId id of the mailbox
   980     \param active true if the mailbox is active
  1059     \param active true if the mailbox is active
   981 */
  1060 */
   982 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
  1061 void NmMailAgent::storeMailboxActive(const NmId &mailboxId, bool active)
   983 {
  1062 {
   984     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
  1063     NM_FUNCTION;
   985     XQSettingsManager mgr;
  1064 
   986     XQCentralRepositoryUtils utils(mgr);
  1065     if (mSettingManager) {
   987 
  1066         XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
   988     if (active) {
  1067         XQCentralRepositoryUtils utils(*mSettingManager);
   989         // when mailbox is active, key can be deleted
  1068 
   990         utils.deleteKey(key);
  1069         if (active) {
   991     }
  1070             // when mailbox is active, key can be deleted
   992     else {
  1071             utils.deleteKey(key);
   993         utils.createKey(key,(int)active);
  1072         }
       
  1073         else {
       
  1074             utils.createKey(key,(int)active);
       
  1075         }
   994     }
  1076     }
   995 }
  1077 }
   996 
  1078 
   997 /*!
  1079 /*!
   998     Get the mailbox activity state.
  1080     Get the mailbox activity state.
   999     \param mailboxId id of the mailbox
  1081     \param mailboxId id of the mailbox
  1000     \return true if the mailbox is active or no information was stored earlier
  1082     \return true if the mailbox is active or no information was stored earlier
  1001 */
  1083 */
  1002 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
  1084 bool NmMailAgent::isMailboxActive(const NmId &mailboxId)
  1003 {
  1085 {
  1004     XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
  1086     NM_FUNCTION;
  1005     XQSettingsManager mgr;
  1087 
  1006     QVariant value = mgr.readItemValue(key,XQSettingsManager::TypeInt);
  1088     bool mailboxActive(true);
  1007     if (!value.isValid()) {
  1089     if (mSettingManager) {
  1008         // no valid value found, key missing?
  1090         XQCentralRepositorySettingsKey key(NmRepositoryId, mailboxId.id());
  1009         NM_COMMENT("NmMailAgent::isMailboxActive - value not valid");
  1091         QVariant value = mSettingManager->readItemValue(key, XQSettingsManager::TypeInt);
  1010         return true;
  1092         if (value.isValid()) {
  1011     }
  1093             NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
  1012     NM_COMMENT(QString("NmMailAgent::isMailboxActive - value=%1").arg(value.toInt()));
  1094             mailboxActive = value.toInt();
  1013     return value.toInt();
  1095         }
       
  1096     }
       
  1097     return mailboxActive;
  1014 }
  1098 }
  1015 
  1099 
  1016 /*!
  1100 /*!
  1017     Delete all stored activity information for the mailbox id
  1101     Delete all stored activity information for the mailbox id
  1018     \param mailboxId id of the mailbox
  1102     \param mailboxId id of the mailbox
  1019 */
  1103 */
  1020 void NmMailAgent::deleteStoredMailboxActivity(const NmId &mailboxId)
  1104 void NmMailAgent::deleteStoredMailboxActivity(const NmId &mailboxId)
  1021 {
  1105 {
       
  1106     NM_FUNCTION;
       
  1107 
  1022     // deactivation delete the key too
  1108     // deactivation delete the key too
  1023     storeMailboxActive(mailboxId,false);
  1109     storeMailboxActive(mailboxId,false);
  1024 }
  1110 }
  1025 
  1111 
  1026 // End of file
  1112 // End of file
  1027