emailuis/nmhswidget/src/nmhswidgetemailengine.cpp
changeset 68 83cc6bae1de8
parent 66 084b5b1f02a7
child 74 6c59112cfd31
equal deleted inserted replaced
67:459da34cdb45 68:83cc6bae1de8
     9  * Initial Contributors:
     9  * Initial Contributors:
    10  * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11  *
    11  *
    12  * Contributors:
    12  * Contributors:
    13  *
    13  *
    14  * Description: 
    14  * Description:
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 #include <QTimer>
    18 #include <QTimer>
    19 #include <QDir>
    19 #include <QDir>
    20 #include <qpluginloader.h>
    20 #include <qpluginloader.h>
    21 #include <xqapplicationmanager.h>
    21 #include <xqapplicationmanager.h>
    22 #include <xqaiwdeclplat.h>
    22 #include <xqaiwdeclplat.h>
       
    23 #include <email_services_api.h>
    23 #include "nmcommon.h"
    24 #include "nmcommon.h"
    24 #include "nmmessageenvelope.h"
    25 #include "nmmessageenvelope.h"
    25 #include "nmhswidgetemailengine.h"
    26 #include "nmhswidgetemailengine.h"
    26 #include "nmdataplugininterface.h"
    27 #include "nmdataplugininterface.h"
    27 #include "nmmailbox.h"
    28 #include "nmmailbox.h"
    32 
    33 
    33 /*!
    34 /*!
    34  Constructor
    35  Constructor
    35  */
    36  */
    36 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) :
    37 NmHsWidgetEmailEngine::NmHsWidgetEmailEngine(const NmId& monitoredMailboxId) :
    37     mMailboxId(monitoredMailboxId), 
    38     mMailboxId(monitoredMailboxId),
    38     mFolderId(0), 
    39     mFolderId(0),
    39     mAccountName(), 
    40     mAccountName(),
    40     mUnreadCount(-1),
    41     mUnreadCount(-1),
    41     mEmailInterface(0), 
    42     mEmailInterface(0),
    42     mFactory(0), 
    43     mFactory(0),
    43     mAccountEventReceivedWhenSuspended(false),
    44     mAccountEventReceivedWhenSuspended(false),
    44     mMessageEventReceivedWhenSuspended(false), 
    45     mMessageEventReceivedWhenSuspended(false),
    45     mSuspended(false),
    46     mSuspended(false),
    46     mUpdateTimer(0),
    47     mUpdateTimer(0),
    47     mAiwRequest(0)
    48     mAiwRequest(0)
    48 {
    49 {
    49     NM_FUNCTION;
    50     NM_FUNCTION;
    56  /return true if initialize succeedes otherwise false. If false the object is unusable.
    57  /return true if initialize succeedes otherwise false. If false the object is unusable.
    57  */
    58  */
    58 bool NmHsWidgetEmailEngine::initialize()
    59 bool NmHsWidgetEmailEngine::initialize()
    59 {
    60 {
    60     NM_FUNCTION;
    61     NM_FUNCTION;
    61         
    62 
    62     if (!constructNmPlugin()) {
    63     if (!constructNmPlugin()) {
    63         //if plugin connection fails, there's no reason to proceed
    64         //if plugin connection fails, there's no reason to proceed
    64         return false;
    65         return false;
    65     }
    66     }
    66     updateData();
    67     updateData();
    67     updateAccount();
    68     updateAccount();
    68     
    69 
    69     mUpdateTimer = new QTimer(this);
    70     mUpdateTimer = new QTimer(this);
    70     mUpdateTimer->setInterval(NmHsWidgetEmailEngineUpdateTimerValue);
    71     mUpdateTimer->setInterval(NmHsWidgetEmailEngineUpdateTimerValue);
    71     connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(handleUpdateTimeout()) );
    72     connect(mUpdateTimer, SIGNAL(timeout()), this, SLOT(handleUpdateTimeout()) );
    72     
    73 
    73     return true;
    74     return true;
    74 }
    75 }
    75 
    76 
    76 /*!
    77 /*!
    77  constructNmPlugin
    78  constructNmPlugin
   100     //get interface instance
   101     //get interface instance
   101     mEmailInterface = mFactory->interfaceInstance(pluginInstance);
   102     mEmailInterface = mFactory->interfaceInstance(pluginInstance);
   102 
   103 
   103     if (!mEmailInterface) {
   104     if (!mEmailInterface) {
   104         NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- mEmailInterface FAILED");
   105         NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- mEmailInterface FAILED");
   105         return false;       
   106         return false;
   106     }
   107     }
   107     //Verify that the mailbox we are interested actually exists.
   108     //Verify that the mailbox we are interested actually exists.
   108     //Otherwise emit account deleted event (instead of just returning)
   109     //Otherwise emit account deleted event (instead of just returning)
   109     QList<NmId> ids; 
   110     QList<NmId> ids;
   110     mEmailInterface->listMailboxIds(ids);
   111     mEmailInterface->listMailboxIds(ids);
   111     if(!ids.contains(mMailboxId)){
   112     if(!ids.contains(mMailboxId)){
   112         NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- !ids.contains(mMailboxId) FAILED");
   113         NM_ERROR(1,"NmHsWidgetEmailEngine::constructNmPlugin() -- !ids.contains(mMailboxId) FAILED");
   113         emit exceptionOccured(NmEngineExcAccountDeleted);
   114         emit exceptionOccured(NmEngineExcAccountDeleted);
   114         return false;
   115         return false;
   115     }
   116     }
   116     
   117 
   117     //retrieve folderID for this mailbox's inbox
   118     //retrieve folderID for this mailbox's inbox
   118     //If NmId equals zero, we don't have the Initial sync done for the account
   119     //If NmId equals zero, we don't have the Initial sync done for the account
   119     //This is valid at least for IMAP accounts. 
   120     //This is valid at least for IMAP accounts.
   120     //Folder ID is then retrieved later when first message event is received
   121     //Folder ID is then retrieved later when first message event is received
   121     mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   122     mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   122 
   123 
   123     //Subscription is needed - otherwise the signals will not be received
   124     //Subscription is needed - otherwise the signals will not be received
   124     mEmailInterface->subscribeMailboxEvents(mMailboxId);
   125     mEmailInterface->subscribeMailboxEvents(mMailboxId);
   127     connect(pluginInstance, SIGNAL( messageEvent(NmMessageEvent, const NmId&,
   128     connect(pluginInstance, SIGNAL( messageEvent(NmMessageEvent, const NmId&,
   128             const QList<NmId>&, const NmId&) ), this,
   129             const QList<NmId>&, const NmId&) ), this,
   129         SLOT( handleMessageEvent(NmMessageEvent, const NmId&,
   130         SLOT( handleMessageEvent(NmMessageEvent, const NmId&,
   130                 const QList<NmId>&, const NmId&) ));
   131                 const QList<NmId>&, const NmId&) ));
   131 
   132 
       
   133     connect( pluginInstance, SIGNAL( void folderEvent(
       
   134              NmFolderEvent, const QList<NmId>, const NmId&) ), this,
       
   135              SLOT( void handleFolderEvent( NmFolderEvent, const QList<NmId>, const NmId&)));
       
   136 
   132     connect(pluginInstance, SIGNAL( mailboxEvent(NmMailboxEvent, const QList<NmId>& ) ), this,
   137     connect(pluginInstance, SIGNAL( mailboxEvent(NmMailboxEvent, const QList<NmId>& ) ), this,
   133         SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) ));
   138         SLOT( handleMailboxEvent(NmMailboxEvent, const QList<NmId>&) ));
   134 
   139 
   135     return true;
   140     return true;
   136 }
   141 }
   157 
   162 
   158     resetEnvelopeList();
   163     resetEnvelopeList();
   159     if (mFactory) {
   164     if (mFactory) {
   160         NmDataPluginFactory::releaseInstance(mFactory);
   165         NmDataPluginFactory::releaseInstance(mFactory);
   161     }
   166     }
   162     
   167 
   163     if (mUpdateTimer){
   168     if (mUpdateTimer){
   164         mUpdateTimer->stop();
   169         mUpdateTimer->stop();
   165         delete mUpdateTimer;
   170         delete mUpdateTimer;
   166     }
   171     }
   167 }
   172 }
   168 
   173 
   169 /*!
   174 /*!
   170  getEnvelopes() provides message envelopes as a list of stack objects
   175  getEnvelopes() provides message envelopes as a list of stack objects
   171  Amount of message envelopes in the list parameter is the smallest of the following factors:
   176  Amount of message envelopes in the list parameter is the smallest of the following factors:
   172  'KMaxNumberOfEnvelopesProvided', 'maxEnvelopeAmount', 'amount of available envelopes'. 
   177  'KMaxNumberOfEnvelopesProvided', 'maxEnvelopeAmount', 'amount of available envelopes'.
   173  
   178 
   174  \param list list to be filled with message envelopes
   179  \param list list to be filled with message envelopes
   175  \param maxEnvelopeAmount Client side limit for amount of message envelope count. 
   180  \param maxEnvelopeAmount Client side limit for amount of message envelope count.
   176  \return count of envelopes added to list
   181  \return count of envelopes added to list
   177  */
   182  */
   178 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope> &list, int maxEnvelopeAmount)
   183 int NmHsWidgetEmailEngine::getEnvelopes(QList<NmMessageEnvelope*> &list, int maxEnvelopeAmount)
   179 {
   184 {
   180     NM_FUNCTION;
   185     NM_FUNCTION;
   181     list.clear(); //Reset the parameter list to avoid side effects
   186     list.clear(); //Reset the parameter list to avoid side effects
   182     int i = 0;
   187     list.append(mEnvelopeList.mid(0, maxEnvelopeAmount));
   183     for (; i < mEnvelopeList.count() && i < maxEnvelopeAmount; i++) {
   188     return list.count();
   184         NmMessageEnvelope env(*mEnvelopeList.at(i));
       
   185         list.append(env);
       
   186     }
       
   187     return i;
       
   188 }
   189 }
   189 
   190 
   190 /*!
   191 /*!
   191  UnreadCount
   192  UnreadCount
   192 
   193 
   210 }
   211 }
   211 
   212 
   212 /*!
   213 /*!
   213  Refresh email data.
   214  Refresh email data.
   214  \post mEnvelopeList is refreshed with valid content so that it has
   215  \post mEnvelopeList is refreshed with valid content so that it has
   215        valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes. 
   216        valid data with maximum of KMaxNumberOfEnvelopesProvided envelopes.
   216        
   217 
   217  - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs.
   218  - emits exceptionOccured(NmEngineExcFailure) if fatal error occurs.
   218  - emits mailDataChanged() if new mail data is set into mEnvelopeList
   219  - emits mailDataChanged() if new mail data is set into mEnvelopeList
   219  - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated
   220  - emits unreadCountChanged(mUnreadCount), if mUnreadCount is updated
   220  
   221 
   221  \return true if everything succeeded, otherwise false
   222  \return true if everything succeeded, otherwise false
   222  */
   223  */
   223 bool NmHsWidgetEmailEngine::updateData()
   224 bool NmHsWidgetEmailEngine::updateData()
   224 {
   225 {
   225     NM_FUNCTION;
   226     NM_FUNCTION;
   234 
   235 
   235     //get messages from inbox
   236     //get messages from inbox
   236     int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, mEnvelopeList,
   237     int msgErr = mEmailInterface->listMessages(mMailboxId, mFolderId, mEnvelopeList,
   237         KMaxNumberOfEnvelopesProvided);
   238         KMaxNumberOfEnvelopesProvided);
   238     if (msgErr) {
   239     if (msgErr) {
   239         //retrieval of messages failed.  
   240         //retrieval of messages failed.
   240         return false;
   241         return false;
   241     }
   242     }
   242     //emit signal about new message data right away
   243     //emit signal about new message data right away
   243     emit mailDataChanged();
   244     emit mailDataChanged();
   244     //retrieve new unread count to mUnreadCount
   245     //retrieve new unread count to mUnreadCount
   280     const NmId& mailboxId)
   281     const NmId& mailboxId)
   281 {
   282 {
   282     NM_FUNCTION;
   283     NM_FUNCTION;
   283     Q_UNUSED(event);
   284     Q_UNUSED(event);
   284     Q_UNUSED(messageIds);
   285     Q_UNUSED(messageIds);
   285     
   286 
   286     if (!mEmailInterface) {
   287     if (!mEmailInterface) {
   287         NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing");
   288         NM_ERROR(1,"NmHsWidgetEmailEngine::handleMessageEvent() -- Interface missing");
   288         emit exceptionOccured(NmEngineExcFailure); //fatal error
   289         emit exceptionOccured(NmEngineExcFailure); //fatal error
   289         return; //if interface is missing there's nothing to do
   290         return; //if interface is missing there's nothing to do
   290     }
   291     }
   291     
   292 
   292     if (mFolderId == NmId(0)) {
   293     if (mFolderId == NmId(0)) {
   293         // inbox was not created in construction phase, so let's
   294         // inbox was not created in construction phase or the folder was deleted in some point,
   294         // get id now as received first mail event
   295         //so let's get the id now as we received first mail event
   295         mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   296         mFolderId = mEmailInterface->getStandardFolderId(mMailboxId, NmFolderInbox);
   296     }
   297     }
   297     if ((folderId == mFolderId) && (mailboxId == mMailboxId)) {
   298     if ((folderId == mFolderId) && (mailboxId == mMailboxId)) {
   298         //Data is updated only if the engine is not suspended
   299         //Data is updated only if the engine is not suspended
   299         if (mSuspended) {
   300         if (mSuspended) {
   303             //start or restart the timer. Update is started when timer expires
   304             //start or restart the timer. Update is started when timer expires
   304             mUpdateTimer->start();
   305             mUpdateTimer->start();
   305         }
   306         }
   306     }
   307     }
   307 }
   308 }
       
   309 
       
   310 /*!
       
   311  handleFolderEvent slot.
       
   312  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
       
   314  we don't query any messages. When the first message event arrives, the folder ID must be
       
   315  retrieved again.
       
   316  */
       
   317 void NmHsWidgetEmailEngine::handleFolderEvent( NmFolderEvent event,
       
   318     const QList<NmId> &folderIds, const NmId& mailboxId)
       
   319 {
       
   320     NM_FUNCTION;
       
   321     //react only if the monitored folder for monitored account is deleted
       
   322     if (event == NmFolderIsDeleted && mailboxId == mMailboxId && folderIds.contains(mFolderId) )
       
   323     {
       
   324         resetEnvelopeList();    //cached envelopes to be cleared
       
   325         mUnreadCount = 0;       //unread count to 0
       
   326         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
       
   329     }
       
   330 }
       
   331 
   308 
   332 
   309 /*!
   333 /*!
   310  handleMailboxEvent slot.
   334  handleMailboxEvent slot.
   311  */
   335  */
   312 void NmHsWidgetEmailEngine::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   336 void NmHsWidgetEmailEngine::handleMailboxEvent(NmMailboxEvent event, const QList<NmId> &mailboxIds)
   346     updateData();
   370     updateData();
   347 }
   371 }
   348 
   372 
   349 /*!
   373 /*!
   350  Update Account data
   374  Update Account data
   351  \post if mEmailInterface exists, the mAccountName is refreshed from adapter 
   375  \post if mEmailInterface exists, the mAccountName is refreshed from adapter
   352  and accountNameChanged signal is emitted.
   376  and accountNameChanged signal is emitted.
   353  */
   377  */
   354 bool NmHsWidgetEmailEngine::updateAccount()
   378 bool NmHsWidgetEmailEngine::updateAccount()
   355 {
   379 {
   356     NM_FUNCTION;
   380     NM_FUNCTION;
   406  \post Mail application is launched to inbox view corresponding widget's mailbox id
   430  \post Mail application is launched to inbox view corresponding widget's mailbox id
   407  */
   431  */
   408 void NmHsWidgetEmailEngine::launchMailAppInboxView()
   432 void NmHsWidgetEmailEngine::launchMailAppInboxView()
   409 {
   433 {
   410     NM_FUNCTION;
   434     NM_FUNCTION;
   411     
   435     NM_TIMESTAMP("Launch mail application from widget.");
   412     QT_TRY{ 
   436 
       
   437     QT_TRY{
   413         if (!mAiwRequest) {
   438         if (!mAiwRequest) {
   414             XQApplicationManager appManager;
   439             XQApplicationManager appManager;
   415             mAiwRequest = appManager.create(
   440             mAiwRequest = appManager.create(
   416                     XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW,
   441                     XQI_EMAIL_INBOX_VIEW, XQOP_EMAIL_INBOX_VIEW,
   417                     false);
   442                     false);
   418             
   443 
   419             if (mAiwRequest) {
   444             if (mAiwRequest) {
   420                 connect(mAiwRequest, SIGNAL( requestError(int, const QString&) ), 
   445                 connect(mAiwRequest, SIGNAL( requestError(int, const QString&) ),
   421                         this, SLOT( aiwRequestError(int, const QString&) ));
   446                         this, SLOT( aiwRequestError(int, const QString&) ));
   422                 connect(mAiwRequest, SIGNAL( requestOk(const QVariant&) ), 
   447                 connect(mAiwRequest, SIGNAL( requestOk(const QVariant&) ),
   423                         this, SLOT( aiwRequestOk(const QVariant&) ));
   448                         this, SLOT( aiwRequestOk(const QVariant&) ));
   424                 QList<QVariant> list;
   449                 QList<QVariant> list;
   425                 list.append(QVariant(mMailboxId.id()));
   450                 list.append(QVariant(mMailboxId.id()));
   426             
   451 
   427                 mAiwRequest->setSynchronous(false);
   452                 mAiwRequest->setSynchronous(false);
   428                 mAiwRequest->setArguments(list);
   453                 mAiwRequest->setArguments(list);
   429                 mAiwRequest->send();
   454                 mAiwRequest->send();
   430             }
   455             }
   431         }
   456         }
   443  \post Mail application is launched and viewing mail specified by
   468  \post Mail application is launched and viewing mail specified by
   444  */
   469  */
   445 void NmHsWidgetEmailEngine::launchMailAppMailViewer(const NmId &messageId)
   470 void NmHsWidgetEmailEngine::launchMailAppMailViewer(const NmId &messageId)
   446 {
   471 {
   447     NM_FUNCTION;
   472     NM_FUNCTION;
       
   473     NM_TIMESTAMP("Launch mail viewer from widget.");
   448 
   474 
   449     QT_TRY{
   475     QT_TRY{
   450         if (!mAiwRequest) {
   476         if (!mAiwRequest) {
   451             XQApplicationManager appManager;
   477             XQApplicationManager appManager;
   452             mAiwRequest = appManager.create(
   478             mAiwRequest = appManager.create(
   453                     XQI_EMAIL_MESSAGE_VIEW, XQOP_EMAIL_MESSAGE_VIEW,
   479                     XQI_EMAIL_MESSAGE_VIEW, "viewMessage(QVariant,QVariant)",
       
   480 					//XQOP_EMAIL_MESSAGE_VIEW,
   454                     false);
   481                     false);
   455             
   482 
   456             if (mAiwRequest) {
   483             if (mAiwRequest) {
   457                 connect(mAiwRequest, SIGNAL( requestError(int, const QString&) ), 
   484                 connect(mAiwRequest, SIGNAL( requestError(int, const QString&) ),
   458                         this, SLOT( aiwRequestError(int, const QString&) ));
   485                         this, SLOT( aiwRequestError(int, const QString&) ));
   459                 connect(mAiwRequest, SIGNAL( requestOk(const QVariant&) ), 
   486                 connect(mAiwRequest, SIGNAL( requestOk(const QVariant&) ),
   460                         this, SLOT( aiwRequestOk(const QVariant&) ));
   487                         this, SLOT( aiwRequestOk(const QVariant&) ));
   461 
   488 
       
   489                 QVariantList messageIdList;
       
   490                 messageIdList.append(mMailboxId.id());
       
   491                 messageIdList.append(mFolderId.id());
       
   492                 messageIdList.append(messageId.id());
       
   493 
   462                 QList<QVariant> argList;
   494                 QList<QVariant> argList;
   463                 QList<QVariant> messageIdList;
   495                 argList.append(QVariant(messageIdList));
   464                 messageIdList.append(QVariant(mMailboxId.id()));
   496                 argList.append(quint64(EmailBackReturnsToMessageList));
   465                 messageIdList.append(QVariant(mFolderId.id()));
   497 
   466                 messageIdList.append(QVariant(messageId.id()));
       
   467                 argList.append(messageIdList);
       
   468             
       
   469                 mAiwRequest->setSynchronous(false);
   498                 mAiwRequest->setSynchronous(false);
   470                 mAiwRequest->setArguments(argList);
   499                 mAiwRequest->setArguments(argList);
   471                 mAiwRequest->send();
   500                 mAiwRequest->send();
   472             }
   501             }
   473         }
   502         }
   485 void NmHsWidgetEmailEngine::aiwRequestOk(const QVariant& result)
   514 void NmHsWidgetEmailEngine::aiwRequestOk(const QVariant& result)
   486 {
   515 {
   487     NM_FUNCTION;
   516     NM_FUNCTION;
   488 
   517 
   489     Q_UNUSED(result);
   518     Q_UNUSED(result);
   490     
   519 
   491     deleteAiwRequest();
   520     deleteAiwRequest();
   492 }
   521 }
   493 
   522 
   494 /*!
   523 /*!
   495  aiwRequestError slot.
   524  aiwRequestError slot.
   498 {
   527 {
   499     NM_FUNCTION;
   528     NM_FUNCTION;
   500 
   529 
   501     Q_UNUSED(errorCode);
   530     Q_UNUSED(errorCode);
   502     Q_UNUSED(errorMessage);
   531     Q_UNUSED(errorMessage);
   503     
   532 
   504     deleteAiwRequest();
   533     deleteAiwRequest();
   505 }
   534 }
   506 
   535 
   507 /*!
   536 /*!
   508  deleteAiwRequest function.
   537  deleteAiwRequest function.
   510 void NmHsWidgetEmailEngine::deleteAiwRequest()
   539 void NmHsWidgetEmailEngine::deleteAiwRequest()
   511 {
   540 {
   512     NM_FUNCTION;
   541     NM_FUNCTION;
   513 
   542 
   514     delete mAiwRequest;
   543     delete mAiwRequest;
   515     mAiwRequest = NULL;    
   544     mAiwRequest = NULL;
   516 }
   545 }
   517 
   546