emailuis/nmhswidget/src/nmhswidgetemailengine.cpp
changeset 76 38bf5461e270
parent 74 6c59112cfd31
equal deleted inserted replaced
74:6c59112cfd31 76:38bf5461e270
    24 #include "nmcommon.h"
    24 #include "nmcommon.h"
    25 #include "nmmessageenvelope.h"
    25 #include "nmmessageenvelope.h"
    26 #include "nmhswidgetemailengine.h"
    26 #include "nmhswidgetemailengine.h"
    27 #include "nmdataplugininterface.h"
    27 #include "nmdataplugininterface.h"
    28 #include "nmmailbox.h"
    28 #include "nmmailbox.h"
       
    29 #include "nmmessage.h"
    29 #include "nmfolder.h"
    30 #include "nmfolder.h"
    30 #include "nmdatapluginfactory.h"
    31 #include "nmdatapluginfactory.h"
    31 #include "nmhswidgetconsts.h"
    32 #include "nmhswidgetconsts.h"
    32 #include "emailtrace.h"
    33 #include "emailtrace.h"
       
    34 #include "nmhswidgetlistmodel.h"
    33 
    35 
    34 /*!
    36 /*!
    35  Constructor
    37  Constructor
    36  */
    38  */
    37 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) :
    39 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) :
    39     mFolderId(0),
    41     mFolderId(0),
    40     mAccountName(),
    42     mAccountName(),
    41     mUnreadCount(-1),
    43     mUnreadCount(-1),
    42     mEmailInterface(0),
    44     mEmailInterface(0),
    43     mFactory(0),
    45     mFactory(0),
    44     mAccountEventReceivedWhenSuspended(false),
    46     mAccountDataNeedsUpdate(false),
    45     mMessageEventReceivedWhenSuspended(false),
    47     mMessageDataNeedsUpdate(false),
    46     mSuspended(false),
    48     mSuspended(false),
    47     mUpdateTimer(0),
       
    48     mAiwRequest(0)
    49     mAiwRequest(0)
    49 {
    50 {
    50     NM_FUNCTION;
    51     NM_FUNCTION;
    51 }
    52 }
    52 
    53 
    63     if (!constructNmPlugin()) {
    64     if (!constructNmPlugin()) {
    64         //if plugin connection fails, there's no reason to proceed
    65         //if plugin connection fails, there's no reason to proceed
    65         return false;
    66         return false;
    66     }
    67     }
    67     updateData();
    68     updateData();
       
    69     updateUnreadCount();
    68     updateAccount();
    70     updateAccount();
    69 
       
    70     mUpdateTimer = new QTimer(this);
       
    71     mUpdateTimer->setInterval(NmHsWidgetEmailEngineUpdateTimerValue);
       
    72     connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(handleUpdateTimeout()) );
       
    73 
    71 
    74     return true;
    72     return true;
    75 }
    73 }
    76 
    74 
    77 /*!
    75 /*!
   138         SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) ));
   136         SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) ));
   139 
   137 
   140     return true;
   138     return true;
   141 }
   139 }
   142 
   140 
   143 /*!
       
   144  Reset envelope list
       
   145  \post mEnvelopeList.isEmpty() == true && all contained objects are deleted
       
   146  */
       
   147 void NmHsWidgetEmailEngine::resetEnvelopeList()
       
   148 {
       
   149     NM_FUNCTION;
       
   150 
       
   151     while (!mEnvelopeList.isEmpty()) {
       
   152         delete mEnvelopeList.takeFirst();
       
   153     }
       
   154 }
       
   155 
   141 
   156 /*!
   142 /*!
   157  Destructor
   143  Destructor
   158  */
   144  */
   159 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine()
   145 NmHsWidgetEmailEngine::~NmHsWidgetEmailEngine()
   160 {
   146 {
   161     NM_FUNCTION;
   147     NM_FUNCTION;
   162 
   148 
   163     resetEnvelopeList();
       
   164     if (mFactory) {
   149     if (mFactory) {
   165         NmDataPluginFactory::releaseInstance(mFactory);
   150         NmDataPluginFactory::releaseInstance(mFactory);
   166     }
   151     }
   167 
   152 
   168     if (mUpdateTimer){
   153 }
   169         mUpdateTimer->stop();
   154 
   170         delete mUpdateTimer;
       
   171     }
       
   172 }
       
   173 
       
   174 /*!
       
   175  getEnvelopes() provides message envelopes as a list of stack objects
       
   176  Amount of message envelopes in the list parameter is the smallest of the following factors:
       
   177  'KMaxNumberOfEnvelopesProvided', 'maxEnvelopeAmount', 'amount of available envelopes'.
       
   178 
       
   179  \param list list to be filled with message envelopes
       
   180  \param maxEnvelopeAmount Client side limit for amount of message envelope count.
       
   181  \return count of envelopes added to list
       
   182  */
       
   183 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope*> &list, int maxEnvelopeAmount)
       
   184 {
       
   185     NM_FUNCTION;
       
   186     list.clear(); //Reset the parameter list to avoid side effects
       
   187     list.append(mEnvelopeList.mid(0, maxEnvelopeAmount));
       
   188     return list.count();
       
   189 }
       
   190 
   155 
   191 /*!
   156 /*!
   192  UnreadCount
   157  UnreadCount
   193 
   158 
   194  \return count of unread mails, or -1 if there are no mails at all in the mailbox.
   159  \return count of unread mails, or -1 if there are no mails at all in the mailbox.
   208 {
   173 {
   209     NM_FUNCTION;
   174     NM_FUNCTION;
   210     return mAccountName;
   175     return mAccountName;
   211 }
   176 }
   212 
   177 
   213 /*!
   178 
   214  Refresh email data.
   179 bool NmHsWidgetEmailEngine::updateUnreadCount()
   215  \post mEnvelopeList is refreshed with valid content so that it has
   180 {
   216        valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes.
       
   217 
       
   218  - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs.
       
   219  - emits mailDataChanged() if new mail data is set into mEnvelopeList
       
   220  - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated
       
   221 
       
   222  \return true if everything succeeded, otherwise false
       
   223  */
       
   224 bool NmHsWidgetEmailEngine::updateData()
       
   225 {
       
   226     NM_FUNCTION;
       
   227     if (!mEmailInterface) {
       
   228         NM_ERROR(1,"NmHsWidgetEmailEngine::updateData() -- Interface missing");
       
   229         emit exceptionOccured(NmEngineExcFailure); //fatal error
       
   230         return false; //if interface is missing there's nothing to do
       
   231     }
       
   232 
       
   233     //reset envelope list before retrieving new items
       
   234     resetEnvelopeList();
       
   235 
       
   236     //get messages from inbox
       
   237     int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, mEnvelopeList,
       
   238         KMaxNumberOfEnvelopesProvided);
       
   239     if (msgErr) {
       
   240         //retrieval of messages failed.
       
   241         return false;
       
   242     }
       
   243     //emit signal about new message data right away
       
   244     emit mailDataChanged();
       
   245     //retrieve new unread count to mUnreadCount
   181     //retrieve new unread count to mUnreadCount
   246     NmFolder* folder = NULL;
   182     NmFolder* folder = NULL;
   247     int folderErr = mEmailInterface->getFolderById(mMailboxId, mFolderId, folder);
   183     int folderErr = mEmailInterface->getFolderById(mMailboxId, mFolderId, folder);
   248     if (folderErr) {
   184     if (folderErr) {
   249         //retrieval of valid folder failed.
   185         //retrieval of valid folder failed.
   250         return false;
   186         return false;
   251     }
   187     }
   252     if (folder) {
   188     if (folder) {
   253         //If messageCount in the folder is zero we must indicate unread count to be -1
   189         //If messageCount in the folder is zero we must indicate unread count to be -1
   254         if (mEnvelopeList.count() == 0) {
   190         if (folder->messageCount() == 0) {
   255             mUnreadCount = -1;
   191             mUnreadCount = -1;
   256         }
   192         }
   257         else {
   193         else {
   258             mUnreadCount = folder->unreadMessageCount();
   194             mUnreadCount = folder->unreadMessageCount();
   259         }
   195         }
   269         return false;
   205         return false;
   270     }
   206     }
   271     return true;
   207     return true;
   272 }
   208 }
   273 
   209 
       
   210 
       
   211 /*!
       
   212  * TODO: UPDATE THIS
       
   213  Refresh email data. 
       
   214  \post mEnvelopeList is refreshed with valid content so that it has
       
   215        valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes.
       
   216 
       
   217  - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs.
       
   218  - emits mailDataChanged() if new mail data is set into mEnvelopeList
       
   219  - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated
       
   220 
       
   221  \return true if everything succeeded, otherwise false
       
   222  */
       
   223 bool NmHsWidgetEmailEngine::updateData()
       
   224 {
       
   225     NM_FUNCTION;
       
   226     if (!mEmailInterface) {
       
   227         NM_ERROR(1,"NmHsWidgetEmailEngine::updateData() -- Interface missing");
       
   228         emit exceptionOccured(NmEngineExcFailure); //fatal error
       
   229         return false; //if interface is missing there's nothing to do
       
   230     }
       
   231 
       
   232     QList<NmMessageEnvelope*> envelopeList;
       
   233     //get messages from inbox
       
   234     int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, envelopeList,
       
   235         KMaxNumberOfEnvelopesProvided);
       
   236     if (msgErr) {
       
   237         //retrieval of messages failed.
       
   238         return false;
       
   239     }
       
   240     //emit signal about new message data right away
       
   241 	emit mailDataRefreshed(envelopeList);
       
   242 
       
   243     return true;
       
   244 }
       
   245 
   274 /*!
   246 /*!
   275  handleMessageEvent slot.
   247  handleMessageEvent slot.
   276  */
   248  */
   277 void NmHsWidgetEmailEngine::handleMessageEvent(
   249 void NmHsWidgetEmailEngine::handleMessageEvent(
   278     NmMessageEvent event,
   250     NmMessageEvent event,
   279     const NmId &folderId,
   251     const NmId &folderId,
   280     const QList<NmId> &messageIds,
   252     const QList<NmId> &messageIds,
   281     const NmId& mailboxId)
   253     const NmId& mailboxId)
   282 {
   254 {
   283     NM_FUNCTION;
   255     NM_FUNCTION;
   284     Q_UNUSED(event);
       
   285     Q_UNUSED(messageIds);
       
   286 
   256 
   287     if (!mEmailInterface) {
   257     if (!mEmailInterface) {
   288         NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing");
   258         NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing");
   289         emit exceptionOccured(NmEngineExcFailure); //fatal error
   259         emit exceptionOccured(NmEngineExcFailure); //fatal error
   290         return; //if interface is missing there's nothing to do
   260         return; //if interface is missing there's nothing to do
   295         //so let's get the id now as we received first mail event
   265         //so let's get the id now as we received first mail event
   296         mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   266         mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   297     }
   267     }
   298     if ((folderId == mFolderId) && (mailboxId == mMailboxId)) {
   268     if ((folderId == mFolderId) && (mailboxId == mMailboxId)) {
   299         //Data is updated only if the engine is not suspended
   269         //Data is updated only if the engine is not suspended
       
   270 
   300         if (mSuspended) {
   271         if (mSuspended) {
   301             mMessageEventReceivedWhenSuspended = true;
   272             mMessageDataNeedsUpdate = true;
   302         }
   273             return; //no further actions
   303         else {
   274         }
   304             //start or restart the timer. Update is started when timer expires
   275         updateUnreadCount();        
   305             mUpdateTimer->start();
   276         switch(event){
   306         }
   277             case(NmMessageCreated):
   307     }
   278                     emit mailsReceived(getEnvelopesFromIds(messageIds));
   308 }
   279                     break;
       
   280             case(NmMessageChanged):
       
   281                     emit mailsUpdated(getEnvelopesFromIds(messageIds));
       
   282                     break;
       
   283             case(NmMessageDeleted):
       
   284                     emit mailsDeleted(messageIds);
       
   285                     break;
       
   286         }
       
   287 
       
   288     }
       
   289 }
       
   290 
       
   291 QList<NmMessageEnvelope*> NmHsWidgetEmailEngine::getEnvelopesFromIds(const QList<NmId> messageIds)
       
   292 {
       
   293     NM_FUNCTION;
       
   294     QList<NmMessageEnvelope*> ret;
       
   295     foreach(NmId id, messageIds){
       
   296         NmMessageEnvelope *msgEnvelope = NULL;
       
   297         msgEnvelope = envelopeById(id);
       
   298         if(msgEnvelope){
       
   299             ret.append(msgEnvelope);
       
   300         }
       
   301     }
       
   302     return ret;
       
   303 }
       
   304 
       
   305 /*!
       
   306     Get new NmMessageEnvelope object by id \a messageId from any folder.
       
   307     Ownership is transferred to the caller. NULL pointer is returned if
       
   308     message id is not found.
       
   309  */
       
   310 NmMessageEnvelope *NmHsWidgetEmailEngine::envelopeById(const NmId &messageId)
       
   311 {
       
   312     NM_FUNCTION;
       
   313     
       
   314     NmMessageEnvelope *msgEnvelope(NULL);
       
   315     NmMessage *newMessageObject(NULL);
       
   316     int retVal(NmNotFoundError);
       
   317     
       
   318     retVal = mEmailInterface->getMessageById(mMailboxId, mFolderId, messageId, newMessageObject);
       
   319     if (retVal < NmNoError || !newMessageObject) {
       
   320         // Return null object if fetching failed
       
   321         delete newMessageObject;
       
   322         newMessageObject = NULL;
       
   323         return NULL;
       
   324     }
       
   325 
       
   326     msgEnvelope = new NmMessageEnvelope(newMessageObject->envelope());
       
   327 
       
   328     delete newMessageObject;
       
   329     newMessageObject = NULL;
       
   330     return msgEnvelope;
       
   331 }
       
   332 
   309 
   333 
   310 /*!
   334 /*!
   311  handleFolderEvent slot.
   335  handleFolderEvent slot.
   312  If the monitored folder is deleted, clear the envelope list and unread count.
   336  If the monitored folder is deleted, clear the envelope list and unread count.
   313  Also the mFolderId is set to 0 to indicate the current state so that in device boot up
   337  Also the mFolderId is set to 0 to indicate the current state so that in device boot up
   319 {
   343 {
   320     NM_FUNCTION;
   344     NM_FUNCTION;
   321     //react only if the monitored folder for monitored account is deleted
   345     //react only if the monitored folder for monitored account is deleted
   322     if (event == NmFolderIsDeleted && mailboxId == mMailboxId && folderIds.contains(mFolderId) )
   346     if (event == NmFolderIsDeleted && mailboxId == mMailboxId && folderIds.contains(mFolderId) )
   323     {
   347     {
   324         resetEnvelopeList();    //cached envelopes to be cleared
   348         emit mailDataCleared(); //emit mail data clearance
   325         mUnreadCount = 0;       //unread count to 0
   349         mUnreadCount = 0;       //unread count to 0
   326         mFolderId = NmId(0);    //folder id to zero (indicates the situation where folder is not available)
   350         mFolderId = NmId(0);    //folder id to zero (indicates the situation where folder is not available)
   327         emit mailDataChanged(); //emit data change for UI
       
   328         emit unreadCountChanged(mUnreadCount); // emit unread count change to UI
   351         emit unreadCountChanged(mUnreadCount); // emit unread count change to UI
   329     }
   352     }
   330 }
   353 }
   331 
   354 
   332 
   355 
   338     NM_FUNCTION;
   361     NM_FUNCTION;
   339     if (mailboxIds.contains(mMailboxId)) {
   362     if (mailboxIds.contains(mMailboxId)) {
   340         switch (event) {
   363         switch (event) {
   341             case (NmMailboxChanged): {
   364             case (NmMailboxChanged): {
   342                 if (mSuspended) {
   365                 if (mSuspended) {
   343                     mAccountEventReceivedWhenSuspended = true;
   366                     mAccountDataNeedsUpdate = true;
   344                 }
   367                 }
   345                 else {
   368                 else {
   346                     updateAccount();
   369                     updateAccount();
   347                 }
   370                 }
   348                 break;
   371                 break;
   356                 break;
   379                 break;
   357         }
   380         }
   358     }
   381     }
   359 }
   382 }
   360 
   383 
   361 /*!
       
   362  * handleUpdateTimeout slot
       
   363  */
       
   364 void NmHsWidgetEmailEngine::handleUpdateTimeout()
       
   365 {
       
   366     NM_FUNCTION;
       
   367     if (mUpdateTimer){
       
   368         mUpdateTimer->stop();
       
   369     }
       
   370     updateData();
       
   371 }
       
   372 
   384 
   373 /*!
   385 /*!
   374  Update Account data
   386  Update Account data
   375  \post if mEmailInterface exists, the mAccountName is refreshed from adapter
   387  \post if mEmailInterface exists, the mAccountName is refreshed from adapter
   376  and accountNameChanged signal is emitted.
   388  and accountNameChanged signal is emitted.
   394         emit accountNameChanged(mAccountName);
   406         emit accountNameChanged(mAccountName);
   395     }
   407     }
   396     return true;
   408     return true;
   397 }
   409 }
   398 
   410 
       
   411 
       
   412 /*!
       
   413  forceUpdate slot.
       
   414  \post If engine is not suspended, this will force full update immediately.
       
   415  */
       
   416 void NmHsWidgetEmailEngine::forceUpdate()
       
   417 {
       
   418     NM_FUNCTION;
       
   419     if(mSuspended){
       
   420         mMessageDataNeedsUpdate = true; //this is enough to force update when activated
       
   421     }else{
       
   422         updateData();
       
   423         updateUnreadCount();
       
   424     }
       
   425 }
       
   426 
   399 /*!
   427 /*!
   400  suspend slot.
   428  suspend slot.
   401  \post engine will not emit signals or refresh its data during suspension.
   429  \post engine will not emit signals or refresh its data during suspension.
   402  */
   430  */
   403 void NmHsWidgetEmailEngine::suspend()
   431 void NmHsWidgetEmailEngine::suspend()
   413  */
   441  */
   414 void NmHsWidgetEmailEngine::activate()
   442 void NmHsWidgetEmailEngine::activate()
   415 {
   443 {
   416     NM_FUNCTION;
   444     NM_FUNCTION;
   417     mSuspended = false;
   445     mSuspended = false;
   418     if (mAccountEventReceivedWhenSuspended) {
   446     if (mAccountDataNeedsUpdate) {
   419         mAccountEventReceivedWhenSuspended = false;
   447         mAccountDataNeedsUpdate = false;
   420         updateAccount();
   448         updateAccount();
   421     }
   449     }
   422     if (mMessageEventReceivedWhenSuspended) {
   450     if (mMessageDataNeedsUpdate) {
   423         mMessageEventReceivedWhenSuspended = false;
   451         mMessageDataNeedsUpdate = false;
   424         updateData();
   452         updateData();
   425     }
   453         updateUnreadCount();
   426 }
   454     }
       
   455 }
       
   456 
   427 
   457 
   428 /*!
   458 /*!
   429  launchMailAppInboxView slot.
   459  launchMailAppInboxView slot.
   430  \post Mail application is launched to inbox view corresponding widget's mailbox id
   460  \post Mail application is launched to inbox view corresponding widget's mailbox id
   431  */
   461  */