emailservices/nmailagent/src/nmmailagent.cpp
changeset 20 ecc8def7944a
parent 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
18:578830873419 20:ecc8def7944a
    17 
    17 
    18 #include "nmmailagentheaders.h"
    18 #include "nmmailagentheaders.h"
    19 #include "nmmailagent.h"
    19 #include "nmmailagent.h"
    20 
    20 
    21 // CONSTS
    21 // CONSTS
    22 const int maxUnreadCount = 1; // 1 is enough
    22 const int NmAgentMaxUnreadCount = 1; // 1 is enough
       
    23 const int NmAgentIndicatorNotSet = -1;
    23 
    24 
    24 
    25 
    25 /*!
    26 /*!
    26     \class NmMailAgent
    27     \class NmMailAgent
    27 
    28 
    28     \brief Main class for receiving email events and passing them to the HbIndicator
    29     \brief Main class for receiving email events and passing them to the HbIndicator
    29 */
    30 */
    30 
    31 
    31 /*!
       
    32     Creates list of folder paths where plug-ins can be loaded from.
       
    33     \return folder path list.
       
    34 */
       
    35 QStringList NmMailAgent::pluginFolders()
       
    36 {
       
    37     const QString nmPluginPath("resource/plugins");
       
    38     QStringList pluginDirectories;
       
    39     QFileInfoList driveList = QDir::drives();
       
    40 
       
    41     foreach(const QFileInfo &driveInfo, driveList) {
       
    42         QString pluginDirectory =
       
    43             driveInfo.absolutePath() + nmPluginPath;
       
    44 
       
    45         if (QFileInfo(pluginDirectory).exists()) {
       
    46             pluginDirectories.append(pluginDirectory);
       
    47         }
       
    48     }
       
    49 
       
    50     return pluginDirectories;
       
    51 }
       
    52 
       
    53 NmMailboxInfo::NmMailboxInfo()
    32 NmMailboxInfo::NmMailboxInfo()
    54 {
    33 {
    55     mId = 0;
    34     mId = 0;
       
    35     mIndicatorIndex = NmAgentIndicatorNotSet;
    56     mSyncState = SyncComplete;
    36     mSyncState = SyncComplete;
    57     mConnectState = Disconnected;
    37     mConnectState = Disconnected;
    58     mUnreadMails = 0;
    38     mUnreadMails = 0;
       
    39     mOutboxMails = 0;
       
    40     mInboxFolderId = 0;
       
    41     mOutboxFolderId = 0;
       
    42     mInboxCreatedMessages = 0;
       
    43     mInboxChangedMessages = 0;
       
    44     mInboxDeletedMessages = 0;
    59     mActive = false;
    45     mActive = false;
    60 }
    46 }
    61 
    47 
    62 NmMailAgent::NmMailAgent() :
    48 NmMailAgent::NmMailAgent() :
    63  mAdapter(NULL),
    49  mPluginFactory(NULL),
    64  mActiveIndicators(0)
    50  mSendingState(false)
    65 {
    51 {
    66     NMLOG("NmMailAgent::NmMailAgent");
    52     NMLOG("NmMailAgent::NmMailAgent");
    67 }
    53 }
    68 
    54 
    69 /*!
    55 /*!
       
    56     Delayed start
       
    57 */
       
    58 void NmMailAgent::delayedStart()
       
    59 {
       
    60 	NMLOG("NmMailAgent::delayedStart");
       
    61 	if (!init()) {
       
    62 		// Initialisation failed. Quit the agent.
       
    63 		QCoreApplication::exit(1);
       
    64 	}
       
    65 }
       
    66 
       
    67 /*!
    70     Initialise the agent. \return true if succesfully started.
    68     Initialise the agent. \return true if succesfully started.
    71 */
    69 */
    72 bool NmMailAgent::init()
    70 bool NmMailAgent::init()
    73 {
    71 {
    74     if (!loadAdapter()) {
    72 	NMLOG("NmMailAgent::init");
    75         // Failed to load NmFrameworkAdapter
    73     mPluginFactory = NmDataPluginFactory::instance();
       
    74     if (!mPluginFactory) {
       
    75         NMLOG("NmMailAgent::init PluginFactory not created");
    76         return false;
    76         return false;
    77     }
    77     }
    78 
    78 
    79     // Start listening events
    79     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
    80     connect(mAdapter, SIGNAL(mailboxEvent(NmMailboxEvent, const QList<NmId>&)),
    80 
    81         this, SLOT(handleMailboxEvent(NmMailboxEvent, const QList<NmId> &)));
    81     foreach (QObject *plugin, *plugins) {
    82 
    82     	if (plugin) {
    83     connect(mAdapter, SIGNAL(messageEvent(
    83 			// Start listening events
    84             NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)),
    84 			connect(plugin, SIGNAL(mailboxEvent(NmMailboxEvent, const QList<NmId>&)),
    85         this, SLOT(handleMessageEvent(
    85 				this, SLOT(handleMailboxEvent(NmMailboxEvent, const QList<NmId> &)),
    86             NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)));
    86 				Qt::UniqueConnection);
    87 
    87 
    88     connect(mAdapter, SIGNAL(syncStateEvent(NmSyncState, const NmId)),
    88 			connect(plugin, SIGNAL(messageEvent(
    89         this, SLOT(handleSyncStateEvent(NmSyncState, const NmId)));
    89 					NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)),
    90 
    90 				this, SLOT(handleMessageEvent(
    91     connect(mAdapter, SIGNAL(connectionEvent(NmConnectState, const NmId)),
    91 					NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)),
    92         this, SLOT(handleConnectionEvent(NmConnectState, const NmId)));
    92 				Qt::UniqueConnection);
       
    93 
       
    94 			connect(plugin, SIGNAL(syncStateEvent(NmSyncState, const NmOperationCompletionEvent&)),
       
    95 				this, SLOT(handleSyncStateEvent(NmSyncState, const NmOperationCompletionEvent&)),
       
    96 				Qt::UniqueConnection);
       
    97 
       
    98 			connect(plugin, SIGNAL(connectionEvent(NmConnectState, const NmId)),
       
    99 				this, SLOT(handleConnectionEvent(NmConnectState, const NmId)),
       
   100 				Qt::UniqueConnection);
       
   101     	}
       
   102     }
    93 
   103 
    94     // load all current mailboxes
   104     // load all current mailboxes
    95     initMailboxStatus();
   105     initMailboxStatus();
    96 
   106 
    97     updateStatus();
       
    98     return true;
   107     return true;
    99 }
   108 }
   100 
   109 
   101 NmMailAgent::~NmMailAgent()
   110 NmMailAgent::~NmMailAgent()
   102 {
   111 {
   103     delete mAdapter;
       
   104     qDeleteAll(mMailboxes);
   112     qDeleteAll(mMailboxes);
       
   113     NmDataPluginFactory::releaseInstance(mPluginFactory);
   105 }
   114 }
   106 
   115 
   107 /*!
   116 /*!
   108     Initialize the mailbox list with the current state
   117     Initialize the mailbox list with the current state
   109 */
   118 */
   110 void NmMailAgent::initMailboxStatus()
   119 void NmMailAgent::initMailboxStatus()
   111 {
   120 {
   112     NMLOG("NmMailAgent::initMailboxStatus");
   121     NMLOG("NmMailAgent::initMailboxStatus");
   113     QList<NmMailbox*> mailboxes;
   122     QList<NmMailbox*> mailboxes;
   114     mAdapter->listMailboxes(mailboxes);
   123     QList<QObject*> *plugins = mPluginFactory->pluginInstances();
   115     foreach (const NmMailbox* mailbox, mailboxes) {
   124 
   116         if (mailbox) {
   125     foreach(QObject* pluginObject, *plugins) {
   117             NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox);
   126         NmDataPluginInterface *plugin =
   118             if (mailboxInfo) {
   127             mPluginFactory->interfaceInstance(pluginObject);
   119                 mailboxInfo->mUnreadMails = getUnreadCount(mailbox->id(),maxUnreadCount);
   128         if (plugin) {
   120                 updateMailboxActivity(mailbox->id(), isMailboxActive(*mailboxInfo));
   129             plugin->listMailboxes(mailboxes);
   121             }
   130         }
   122         }
   131 
   123     }
   132         // Add the indicators
   124     qDeleteAll(mailboxes);
   133         // Must be made in reverse order to show them properly in
       
   134         // HbIndicator menu
       
   135         QListIterator<NmMailbox *> i(mailboxes);
       
   136         i.toBack();
       
   137         while (i.hasPrevious()) {
       
   138             const NmMailbox *mailbox = i.previous();
       
   139             if (mailbox) {
       
   140                 NmMailboxInfo *mailboxInfo = createMailboxInfo(*mailbox,plugin);
       
   141                 if (mailboxInfo) {
       
   142                     mailboxInfo->mUnreadMails = getUnreadCount(mailbox->id(),NmAgentMaxUnreadCount);
       
   143                     mailboxInfo->mOutboxMails = getOutboxCount(mailbox->id());
       
   144 
       
   145                     // Create indicator for visible mailboxes
       
   146                     updateMailboxState(mailbox->id(),
       
   147                         isMailboxActive(*mailboxInfo),
       
   148                         false);
       
   149                 }
       
   150             }
       
   151         }
       
   152         qDeleteAll(mailboxes);
       
   153     }
   125 }
   154 }
   126 
   155 
   127 /*!
   156 /*!
   128     Get mailbox unread count in inbox folder
   157     Get mailbox unread count in inbox folder
   129     \param mailboxId id of the mailbox
   158     \param mailboxId id of the mailbox
   133 int NmMailAgent::getUnreadCount(const NmId &mailboxId, int maxCount)
   162 int NmMailAgent::getUnreadCount(const NmId &mailboxId, int maxCount)
   134 {
   163 {
   135     NMLOG("NmMailAgent::getUnreadCount");
   164     NMLOG("NmMailAgent::getUnreadCount");
   136     int count(0);
   165     int count(0);
   137 
   166 
   138     // get inbox folder ID
   167     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   139     NmId inboxId = mAdapter->getStandardFolderId(
   168 
   140             mailboxId, NmFolderInbox );
   169     if (plugin) {
   141 
   170 		// get inbox folder ID
   142     // get list of messages in inbox
   171 		NmId inboxId = plugin->getStandardFolderId(
   143     QList<NmMessageEnvelope*> messageList;
   172 				mailboxId, NmFolderInbox );
   144     mAdapter->listMessages(mailboxId, inboxId, messageList);
   173 
   145 
   174 		// get list of messages in inbox
   146     foreach (const NmMessageEnvelope* envelope, messageList) {
   175 		QList<NmMessageEnvelope*> messageList;
   147         // if the message is not read, it is "unread"
   176 		plugin->listMessages(mailboxId, inboxId, messageList);
   148         if (!envelope->isRead()) {
   177 
   149             count++;
   178 		foreach (const NmMessageEnvelope* envelope, messageList) {
   150 
   179 			// if the message is not read, it is "unread"
   151             // No more unread mails are needed
   180 			if (!envelope->isRead()) {
   152             if (count >= maxCount) {
   181 				count++;
   153                 break;
   182 
   154             }
   183 				// No more unread mails are needed
   155         }
   184 				if (count >= maxCount) {
   156     }
   185 					break;
   157 	qDeleteAll(messageList);
   186 				}
       
   187 			}
       
   188 		}
       
   189 		qDeleteAll(messageList);
       
   190     }
   158 	NMLOG(QString("NmMailAgent::getUnreadCount count=%1").arg(count));
   191 	NMLOG(QString("NmMailAgent::getUnreadCount count=%1").arg(count));
   159 
   192 
   160     return count;
   193     return count;
   161 }
   194 }
   162 
   195 
   163 /*!
   196 /*!
   164     Load NmFrameworkAdapter from plugins.
   197     Get mailbox count in outbox folder
   165     \return true if adapter is loaded succesfully.
   198     \param mailboxId id of the mailbox
   166 */
   199     \param maxCount max number of outbox mails that is needed
   167 bool NmMailAgent::loadAdapter()
   200     \return number of mails in the outbox
   168 {
   201 */
   169     QStringList directories(pluginFolders());
   202 int NmMailAgent::getOutboxCount(const NmId &mailboxId)
   170 
   203 {
   171      foreach (const QString &pluginPath, directories) {
   204     NMLOG("NmMailAgent::getOutboxCount");
   172          QPluginLoader *loader =
   205     int count(0);
   173              new QPluginLoader(pluginPath + "/nmframeworkadapter.qtplugin");
   206 
   174          if (loader) {
   207     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
   175              mAdapter = static_cast<NmFrameworkAdapter *>(loader->instance());
   208 
   176              if (mAdapter) {
   209     if (plugin) {
   177                  return true;
   210 		// get outbox folder ID
   178              }
   211 		NmId outboxId = plugin->getStandardFolderId( mailboxId, NmFolderOutbox );
   179          }
   212 
   180      }
   213 		// get list of messages in outbox
   181      NMLOG("NmMailAgent::loadAdapter failed");
   214 		QList<NmMessageEnvelope*> messageList;
   182      return false;
   215 		plugin->listMessages(mailboxId, outboxId, messageList);
   183 }
   216 		count = messageList.count();
   184 
   217 		qDeleteAll(messageList);
   185 /*!
   218     }
   186     Update the mailbox visibility
   219     NMLOG(QString("NmMailAgent::getOutboxCount count=%1").arg(count));
       
   220 
       
   221     return count;
       
   222 }
       
   223 
       
   224 /*!
       
   225     Update the mailbox visibility and status
   187     \param mailboxId id of the mailbox
   226     \param mailboxId id of the mailbox
   188     \param active visibility state of the mailbox
   227     \param active visibility state of the mailbox
       
   228     \param refreshAlways true when the indicator should be always updated
   189     \return true if the mailbox state was changed
   229     \return true if the mailbox state was changed
   190 */
   230 */
   191 bool NmMailAgent::updateMailboxActivity(const NmId &mailboxId, bool active)
   231 bool NmMailAgent::updateMailboxState(const NmId &mailboxId,
   192 {
   232     bool active, bool refreshAlways)
       
   233 {
       
   234     // Update the global sending state
       
   235     mSendingState = false;
       
   236     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
       
   237         if (mailboxInfo->mOutboxMails>0) {
       
   238             mSendingState = true;
       
   239             break;
       
   240         }
       
   241     }
       
   242 
   193     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   243     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   194     bool changed = false;
   244     bool changed = false;
   195     if (mailboxInfo->mActive != active) {
   245     if (mailboxInfo->mActive != active ||
       
   246         refreshAlways) {
   196         mailboxInfo->mActive = active;
   247         mailboxInfo->mActive = active;
   197         changed = true;
   248         changed = true;
   198         if (active) {
   249         if (active) {
   199             // Mailbox becomes active again. Move to the bottom of the list.
   250             // Mailbox is not yet assigned to any indicator
   200             mMailboxes.removeAll(mailboxInfo);
   251             if (mailboxInfo->mIndicatorIndex < 0) {
   201             mMailboxes.append(mailboxInfo);
   252                 mailboxInfo->mIndicatorIndex = getIndicatorIndex();
       
   253             }
       
   254 
       
   255             updateIndicator(true,*mailboxInfo);
       
   256         }
       
   257         else {
       
   258             // Indicator not anymore active. Release it.
       
   259             if (mailboxInfo->mIndicatorIndex>=0) {
       
   260                 updateIndicator(false,*mailboxInfo);
       
   261                 mailboxInfo->mIndicatorIndex = NmAgentIndicatorNotSet;
       
   262             }
   202         }
   263         }
   203     }
   264     }
   204     return changed;
   265     return changed;
   205 }
       
   206 
       
   207 /*!
       
   208     Updates status according to current information
       
   209 */
       
   210 void NmMailAgent::updateStatus()
       
   211 {
       
   212     NMLOG("NmMailAgent::updateStatus");
       
   213 
       
   214     int activeIndicators = 0;
       
   215 
       
   216     // Update the indicators
       
   217     foreach (NmMailboxInfo *mailboxInfo, mMailboxes) {
       
   218         // Show only active mailboxes
       
   219         if (mailboxInfo->mActive) {
       
   220             updateIndicator(activeIndicators,true,*mailboxInfo);
       
   221             activeIndicators++;
       
   222         }
       
   223     }
       
   224 
       
   225     // Hide the indicator that are not needed anymore
       
   226     for (int i=activeIndicators;i<mActiveIndicators;i++) {
       
   227         NmMailboxInfo mailboxInfo;
       
   228         updateIndicator(i,false,mailboxInfo);
       
   229     }
       
   230     mActiveIndicators = activeIndicators;
       
   231 }
   266 }
   232 
   267 
   233 /*!
   268 /*!
   234     Check if the mailbox indicator should be active, according to current state
   269     Check if the mailbox indicator should be active, according to current state
   235     \param mailboxInfo information of the mailbox
   270     \param mailboxInfo information of the mailbox
   236     \return true if indicator should be now active
   271     \return true if indicator should be now active
   237 */
   272 */
   238 bool NmMailAgent::isMailboxActive(const NmMailboxInfo& mailboxInfo)
   273 bool NmMailAgent::isMailboxActive(const NmMailboxInfo& mailboxInfo)
   239 {
   274 {
   240     if (mailboxInfo.mUnreadMails>0) {
   275     if (mailboxInfo.mUnreadMails>0 || mailboxInfo.mOutboxMails>0) {
   241         return true;
   276         return true;
   242     }
   277     }
   243     return false;
   278     return false;
   244 }
   279 }
   245 
   280 
   248     \param mailboxIndex index of the item shown in indicator menu
   283     \param mailboxIndex index of the item shown in indicator menu
   249     \param active indicator visibility state
   284     \param active indicator visibility state
   250     \param mailboxInfo information of the mailbox
   285     \param mailboxInfo information of the mailbox
   251     \return true if indicator was updated with no errors
   286     \return true if indicator was updated with no errors
   252 */
   287 */
   253 bool NmMailAgent::updateIndicator(int mailboxIndex, bool active,
   288 bool NmMailAgent::updateIndicator(bool active,
   254     const NmMailboxInfo& mailboxInfo)
   289     const NmMailboxInfo& mailboxInfo)
   255 {
   290 {
   256     NMLOG(QString("NmMailAgent::updateIndicator index=%1 active=%2 unread=%3").
   291     NMLOG(QString("NmMailAgent::updateIndicator index=%1 active=%2 unread=%3").
   257         arg(mailboxIndex).arg(active).arg(mailboxInfo.mUnreadMails));
   292         arg(mailboxInfo.mIndicatorIndex).arg(active).arg(mailboxInfo.mUnreadMails));
   258 
   293 
   259     bool ok = false;
   294     bool ok = false;
   260     QString name = QString("com.nokia.nmail.indicatorplugin_")+mailboxIndex+"/1.0";
   295     QString name = QString("com.nokia.nmail.indicatorplugin_%1/1.0").
       
   296         arg(mailboxInfo.mIndicatorIndex);
   261 
   297 
   262     QList<QVariant> list;
   298     QList<QVariant> list;
   263     list.append(mailboxInfo.mId.id());
   299     list.append(mailboxInfo.mId.id());
   264     list.append(mailboxInfo.mName);
   300     list.append(mailboxInfo.mName);
   265     list.append(mailboxInfo.mUnreadMails);
   301     list.append(mailboxInfo.mUnreadMails);
   266     list.append(mailboxInfo.mSyncState);
   302     list.append(mailboxInfo.mSyncState);
   267     list.append(mailboxInfo.mConnectState);
   303     list.append(mailboxInfo.mConnectState);
       
   304     list.append(mailboxInfo.mOutboxMails);
       
   305     list.append(mSendingState);
   268 
   306 
   269     HbIndicator indicator;
   307     HbIndicator indicator;
   270     if (active) {
   308     if (active) {
   271         ok = indicator.activate(name,list);
   309         ok = indicator.activate(name,list);
   272     }
   310     }
   275     }
   313     }
   276     return ok;
   314     return ok;
   277 }
   315 }
   278 
   316 
   279 /*!
   317 /*!
       
   318     Get next free indicator index, starting from 0
       
   319     @return index of the indicator that is available
       
   320  */
       
   321 int NmMailAgent::getIndicatorIndex()
       
   322 {
       
   323     int index = 0;
       
   324     bool found(false);
       
   325     do {
       
   326         found = false;
       
   327         foreach (NmMailboxInfo *mailbox, mMailboxes) {
       
   328             if (mailbox->mIndicatorIndex == index &&
       
   329                 mailbox->mActive) {
       
   330                 found = true;
       
   331                 index++;
       
   332             }
       
   333         }
       
   334     }
       
   335     while( found );
       
   336     return index;
       
   337 }
       
   338 
       
   339 /*!
   280     Received from NmFrameworkAdapter mailboxEvent signal
   340     Received from NmFrameworkAdapter mailboxEvent signal
   281     \sa NmFrameworkAdapter
   341     \sa NmFrameworkAdapter
   282 */
   342 */
   283 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   343 void NmMailAgent::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   284 {
   344 {
   285     NMLOG(QString("NmMailAgent::handleMailboxEvent %1").arg(event));
   345     NMLOG(QString("NmMailAgent::handleMailboxEvent %1").arg(event));
   286     bool updateNeeded(false);
       
   287 
   346 
   288     switch(event) {
   347     switch(event) {
   289         case NmMailboxCreated:
   348         case NmMailboxCreated:
   290             foreach (NmId mailboxId, mailboxIds) {
   349             foreach (NmId mailboxId, mailboxIds) {
   291                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   350                 getMailboxInfo(mailboxId); // create a new mailbox if needed
   292                 updateNeeded = true;
       
   293             }
   351             }
   294             break;
   352             break;
   295         case NmMailboxChanged:
   353         case NmMailboxChanged:
       
   354 
   296             // Mailbox name may have been changed
   355             // Mailbox name may have been changed
   297             foreach (NmId mailboxId, mailboxIds) {
   356             foreach (NmId mailboxId, mailboxIds) {
   298                 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   357                 NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   299                 NmMailbox *mailbox(NULL);
   358                 NmMailbox *mailbox(NULL);
   300                 mAdapter->getMailboxById(mailboxId,mailbox);
   359                 NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(mailboxId);
       
   360                 if (plugin) {
       
   361 					plugin->getMailboxById(mailboxId,mailbox);
       
   362                 }
   301                 if (mailbox && mailboxInfo) {
   363                 if (mailbox && mailboxInfo) {
   302                     if(mailbox->name() != mailboxInfo->mName) {
   364                     if(mailbox->name() != mailboxInfo->mName) {
   303                         mailboxInfo->mName = mailbox->name();
   365                         mailboxInfo->mName = mailbox->name();
   304                         updateNeeded = true;
   366 
       
   367                         if (mailboxInfo->mActive) {
       
   368                             // Update the status of the mailbox
       
   369                             updateMailboxState(mailboxId, true, true);
       
   370                         }
   305                     }
   371                     }
   306                 }
   372                 }
   307                 delete mailbox;
   373                 delete mailbox;
   308             }
   374             }
   309             break;
   375             break;
   310         case NmMailboxDeleted:
   376         case NmMailboxDeleted:
   311             foreach (NmId mailboxId, mailboxIds) {
   377             foreach (NmId mailboxId, mailboxIds) {
   312                 if (removeMailboxInfo(mailboxId)) {
   378                 // Will hide also the indicator
   313                     updateNeeded = true;
   379                 removeMailboxInfo(mailboxId);
   314                 }
       
   315             }
   380             }
   316             break;
   381             break;
   317         default:
   382         default:
   318             break;
   383             break;
   319     }
   384     }
   320 
       
   321     if (updateNeeded) {
       
   322         updateStatus();
       
   323     }
       
   324 }
   385 }
   325 
   386 
   326 /*!
   387 /*!
   327     Received from NmFrameworkAdapter messageEvent signal
   388     Received from NmFrameworkAdapter messageEvent signal
   328     \sa NmFrameWorkAdapter
   389     \sa NmFrameworkAdapter
   329 */
   390 */
   330 void NmMailAgent::handleMessageEvent(
   391 void NmMailAgent::handleMessageEvent(
   331             NmMessageEvent event,
   392             NmMessageEvent event,
   332             const NmId &folderId,
   393             const NmId &folderId,
   333             const QList<NmId> &messageIds,
   394             const QList<NmId> &messageIds,
   334             const NmId& mailboxId)
   395             const NmId& mailboxId)
   335 {
   396 {
   336     NMLOG(QString("NmMailAgent::handleMessageEvent %1 %2").arg(event).arg(mailboxId.id()));
   397     NMLOG(QString("NmMailAgent::handleMessageEvent %1 %2").arg(event).arg(mailboxId.id()));
   337     Q_UNUSED(folderId);
   398     bool updateNeeded = false;
   338     Q_UNUSED(messageIds);
       
   339 
   399 
   340     switch (event) {
   400     switch (event) {
       
   401         case NmMessageCreated: {
       
   402             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   403             
       
   404             if (folderId==mailboxInfo->mInboxFolderId) {
       
   405                 mailboxInfo->mInboxCreatedMessages++;
       
   406             }
       
   407             
       
   408             // When created a new mail in the outbox, we are in sending state
       
   409             if (mailboxInfo->mOutboxFolderId == folderId) {
       
   410                 // The first mail created in the outbox
       
   411                 if (mailboxInfo->mOutboxMails <= 0) {
       
   412                     updateNeeded = true;
       
   413                 }
       
   414                 mailboxInfo->mOutboxMails += messageIds.count();
       
   415             }
       
   416             break;
       
   417         }
   341         case NmMessageChanged: {
   418         case NmMessageChanged: {
   342             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   419             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   420             
       
   421             if (folderId==mailboxInfo->mInboxFolderId) {
       
   422                 mailboxInfo->mInboxChangedMessages++;
       
   423             }
       
   424             
   343             // If not currently syncronizing the mailbox, this may mean
   425             // If not currently syncronizing the mailbox, this may mean
   344             // that a message was read/unread
   426             // that a message was read/unread
   345             if (mailboxInfo && mailboxInfo->mSyncState==SyncComplete) {
   427             if (mailboxInfo && mailboxInfo->mSyncState==SyncComplete) {
   346                 // check the unread status here again
   428                 // check the unread status again
   347                 mailboxInfo->mUnreadMails = getUnreadCount(mailboxId,maxUnreadCount);
   429                 mailboxInfo->mUnreadMails = getUnreadCount(mailboxId,NmAgentMaxUnreadCount);
   348                 if(updateMailboxActivity(mailboxId, isMailboxActive(*mailboxInfo))) {
   430                 updateNeeded = true;
   349                     updateStatus();
       
   350                 }
       
   351             }
   431             }
   352 			break;
   432 			break;
   353 		}
   433 		}
       
   434         case NmMessageDeleted: {
       
   435             NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   436 
       
   437             if (folderId==mailboxInfo->mInboxFolderId) {
       
   438                 mailboxInfo->mInboxDeletedMessages++;
       
   439             }
       
   440             
       
   441             // Deleted mails from the outbox
       
   442             if (mailboxInfo->mOutboxFolderId == folderId) {
       
   443                 mailboxInfo->mOutboxMails -= messageIds.count();
       
   444 
       
   445                 // Sanity check for the outbox count
       
   446                 if (mailboxInfo->mOutboxMails < 0) {
       
   447                     mailboxInfo->mOutboxMails = 0;
       
   448                 }
       
   449 
       
   450                 // The last mail was now deleted
       
   451                 if (mailboxInfo->mOutboxMails == 0) {
       
   452                     updateNeeded = true;
       
   453                 }
       
   454             }
       
   455             break;
       
   456         }
   354         default:
   457         default:
   355             break;
   458             break;
   356     }
   459     }
   357 
   460 
   358     // Do not perform an update here, just in handleSyncState
   461     if (updateNeeded) {
       
   462         NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
       
   463         updateMailboxState(mailboxId,
       
   464             isMailboxActive(*mailboxInfo), true /* force refresh */);
       
   465     }
   359 }
   466 }
   360 
   467 
   361 /*!
   468 /*!
   362     Received from NmFrameworkAdapter syncStateEvent signal
   469     Received from NmFrameworkAdapter syncStateEvent signal
   363     \sa NmFrameWorkAdapter
   470     \sa NmFrameworkAdapter
   364 */
   471 */
   365 void NmMailAgent::handleSyncStateEvent(
   472 void NmMailAgent::handleSyncStateEvent(
   366             NmSyncState state,
   473             NmSyncState state,
   367             const NmId mailboxId)
   474             const NmOperationCompletionEvent &event)
   368 {
   475 {
   369     NMLOG(QString("NmMailAgent::handleSyncStateEvent %1 %2").arg(state).arg(mailboxId.id()));
   476     NMLOG(QString("NmMailAgent::handleSyncStateEvent %1 %2").arg(state).arg(event.mMailboxId.id()));
   370     NmMailboxInfo *info = getMailboxInfo(mailboxId);
   477     NmMailboxInfo *info = getMailboxInfo(event.mMailboxId);
   371     if (info) {
   478     if (info) {
   372         info->mSyncState = state;
   479         info->mSyncState = state;
   373 
   480         
   374         if (state==SyncComplete) {
   481         if (state==Synchronizing) {
   375             // check the unread status here again
   482             // Reset counters when sync is started
   376             info->mUnreadMails = getUnreadCount(mailboxId,maxUnreadCount);
   483             info->mInboxCreatedMessages = 0;
   377             if(updateMailboxActivity(mailboxId, isMailboxActive(*info))) {
   484             info->mInboxChangedMessages = 0;
   378                 updateStatus();
   485             info->mInboxDeletedMessages = 0;
   379             }
   486         } 
       
   487         else if (state==SyncComplete) {
       
   488             // Check the unread status here again
       
   489             info->mUnreadMails = getUnreadCount(event.mMailboxId,NmAgentMaxUnreadCount);
       
   490 
       
   491             // Refresh the indicator if messages created or changed
       
   492             NMLOG(QString(" created=%1, changed=%1, deleted=%1").
       
   493                 arg(info->mInboxCreatedMessages).
       
   494                 arg(info->mInboxChangedMessages).
       
   495                 arg(info->mInboxDeletedMessages));
       
   496             bool refresh = (info->mInboxCreatedMessages > 0) || (info->mInboxChangedMessages > 0);
       
   497 
       
   498             updateMailboxState(event.mMailboxId, isMailboxActive(*info), refresh);
   380         }
   499         }
   381     }
   500     }
   382 }
   501 }
   383 
   502 
   384 /*!
   503 /*!
   385     Received from NmFrameworkAdapter connectionState signal
   504     Received from NmFrameworkAdapter connectionState signal
   386     \sa NmFrameWorkAdapter
   505     \sa NmFrameworkAdapter
   387 */
   506 */
   388 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId)
   507 void NmMailAgent::handleConnectionEvent(NmConnectState state, const NmId mailboxId)
   389 {
   508 {
   390     NMLOG(QString("NmMailAgent::handleConnectionEvent %1 %2").arg(state).arg(mailboxId.id()));
   509     NMLOG(QString("NmMailAgent::handleConnectionEvent %1 %2").arg(state).arg(mailboxId.id()));
   391     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   510     NmMailboxInfo *mailboxInfo = getMailboxInfo(mailboxId);
   392     if (mailboxInfo) {
   511     if (mailboxInfo) {
   393         // Connecting, Connected, Disconnecting, Disconnected
   512         // Connecting, Connected, Disconnecting, Disconnected
   394         mailboxInfo->mConnectState = state;
   513         mailboxInfo->mConnectState = state;
   395     }
   514     }
   396     updateStatus();
       
   397 }
   515 }
   398 
   516 
   399 /*!
   517 /*!
   400     Remove a mailbox info entry
   518     Remove a mailbox info entry
   401     \return true if mailbox info was found
   519     \return true if mailbox info was found
   403 bool NmMailAgent::removeMailboxInfo(const NmId &id)
   521 bool NmMailAgent::removeMailboxInfo(const NmId &id)
   404 {
   522 {
   405     bool found = false;
   523     bool found = false;
   406     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   524     foreach (NmMailboxInfo *mailbox, mMailboxes) {
   407         if (mailbox->mId == id) {
   525         if (mailbox->mId == id) {
       
   526             // Hide the indicator too
       
   527             if(mailbox->mIndicatorIndex>=0) {
       
   528                 updateIndicator(false,*mailbox);
       
   529             }
       
   530 
   408             found = true;
   531             found = true;
   409             mMailboxes.removeAll(mailbox);
   532             mMailboxes.removeAll(mailbox);
   410         }
   533         }
   411     }
   534     }
   412     return found;
   535     return found;
   418 */
   541 */
   419 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
   542 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmId &id)
   420 {
   543 {
   421     // get information of the mailbox
   544     // get information of the mailbox
   422     NmMailbox *mailbox = NULL;
   545     NmMailbox *mailbox = NULL;
   423     mAdapter->getMailboxById(id, mailbox);
   546     NmMailboxInfo *info = NULL;
   424     if (mailbox) {
   547     NmDataPluginInterface *plugin = mPluginFactory->interfaceInstance(id);
   425         return createMailboxInfo(*mailbox);
   548     if (plugin) {
   426     }
   549         plugin->getMailboxById(id, mailbox);
   427     return NULL;
   550         if (mailbox) {
       
   551             info = createMailboxInfo(*mailbox,plugin);
       
   552         }
       
   553     }
       
   554 
       
   555     return info;
   428 }
   556 }
   429 
   557 
   430 /*!
   558 /*!
   431     Create a new mailbox info with given parameters
   559     Create a new mailbox info with given parameters
   432     \return new mailbox info object
   560     \return new mailbox info object
   433 */
   561 */
   434 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox& mailbox)
   562 NmMailboxInfo *NmMailAgent::createMailboxInfo(const NmMailbox& mailbox,NmDataPluginInterface *plugin)
   435 {
   563 {
   436     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
   564     NmMailboxInfo *mailboxInfo = new NmMailboxInfo();
   437     mailboxInfo->mId = mailbox.id();
   565     mailboxInfo->mId = mailbox.id();
   438     mailboxInfo->mName = mailbox.name();
   566     mailboxInfo->mName = mailbox.name();
   439 
   567 
   440     mMailboxes.append(mailboxInfo);
   568     mMailboxes.append(mailboxInfo);
   441 
   569 
   442     // Subscribe to get all mailbox events
   570     // Subscribe to get all mailbox events
   443     mAdapter->subscribeMailboxEvents(mailboxInfo->mId);
   571     plugin->subscribeMailboxEvents(mailboxInfo->mId);
       
   572 
       
   573     // get inbox folder ID
       
   574     mailboxInfo->mInboxFolderId = plugin->getStandardFolderId(
       
   575         mailbox.id(), NmFolderInbox );
       
   576 
       
   577     // get outbox folder ID
       
   578     mailboxInfo->mOutboxFolderId = plugin->getStandardFolderId(
       
   579         mailbox.id(), NmFolderOutbox );
       
   580 
   444     return mailboxInfo;
   581     return mailboxInfo;
   445 }
   582 }
   446 
   583 
   447 /*!
   584 /*!
   448     Return mailbox info class with mailbox id. If no class is found, create a new instance with given id.
   585     Return mailbox info class with mailbox id. If no class is found, create a new instance with given id.