emailuis/nmailuiengine/src/nmuiengine.cpp
changeset 20 ecc8def7944a
parent 18 578830873419
child 23 2dc6caa42ec3
equal deleted inserted replaced
18:578830873419 20:ecc8def7944a
    31     Constructor
    31     Constructor
    32 */
    32 */
    33 NmUiEngine::NmUiEngine() 
    33 NmUiEngine::NmUiEngine() 
    34 :mMailboxListModel(NULL),
    34 :mMailboxListModel(NULL),
    35 mMessageListModel(NULL),
    35 mMessageListModel(NULL),
    36 mSendOperation(NULL),
    36 mSendOperation(NULL)
    37 mSaveOperation(NULL),
       
    38 mMessageToBeSent(false),
       
    39 mMessage(NULL)
       
    40 {
    37 {
    41     mPluginFactory = NmDataPluginFactory::instance();
    38     mPluginFactory = NmDataPluginFactory::instance();
    42     mDataManager = new NmDataManager();
    39     mDataManager = new NmDataManager();
    43 }
    40 }
    44 
    41 
    62     while (!mOperations.isEmpty()) {
    59     while (!mOperations.isEmpty()) {
    63         delete mOperations.takeLast();
    60         delete mOperations.takeLast();
    64     }
    61     }
    65     
    62     
    66     delete mSendOperation;
    63     delete mSendOperation;
    67     delete mSaveOperation;
       
    68 }
    64 }
    69 
    65 
    70 /*!
    66 /*!
    71 
    67 
    72 */
    68 */
   158         QObject::connect(plugin,
   154         QObject::connect(plugin,
   159             SIGNAL(messageEvent(NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)),
   155             SIGNAL(messageEvent(NmMessageEvent, const NmId &, const QList<NmId> &, const NmId&)),
   160         mMessageListModel,
   156         mMessageListModel,
   161             SLOT(handleMessageEvent(NmMessageEvent, const NmId &, const QList<NmId> &)),
   157             SLOT(handleMessageEvent(NmMessageEvent, const NmId &, const QList<NmId> &)),
   162             Qt::UniqueConnection );
   158             Qt::UniqueConnection );
   163         
   159 
   164         QObject::connect(plugin,
   160         QObject::connect(
   165             SIGNAL(syncStateEvent(NmSyncState, const NmId &)),
   161             plugin, SIGNAL(syncStateEvent(NmSyncState, const NmOperationCompletionEvent &)),
   166             this,
   162             this, SLOT(handleSyncStateEvent(NmSyncState, const NmOperationCompletionEvent &)),
   167             SIGNAL(syncStateEvent(NmSyncState, const NmId &)),
       
   168             Qt::UniqueConnection);
   163             Qt::UniqueConnection);
   169         
   164         
   170         QObject::connect(plugin,
   165         QObject::connect(plugin,
   171             SIGNAL(connectionEvent(NmConnectState, const NmId &)),
   166             SIGNAL(connectionEvent(NmConnectState, const NmId &)),
   172             this,
   167             this,
   253 */
   248 */
   254 NmOperation *NmUiEngine::fetchMessagePart(
   249 NmOperation *NmUiEngine::fetchMessagePart(
   255     const NmId &mailboxId,
   250     const NmId &mailboxId,
   256     const NmId &folderId,
   251     const NmId &folderId,
   257     const NmId &messageId,
   252     const NmId &messageId,
   258     const NmId& messagePartId)
   253     const NmId &messagePartId)
   259 {
   254 {
   260     NmOperation *value(NULL);
   255     NmOperation *value(NULL);
   261     NmDataPluginInterface *plugin =
   256     NmDataPluginInterface *plugin =
   262         mPluginFactory->interfaceInstance(mailboxId);
   257         mPluginFactory->interfaceInstance(mailboxId);
   263     if (plugin) {
   258     if (plugin) {
   264         value = plugin->fetchMessagePart(mailboxId, folderId, messageId, messagePartId);
   259         value = plugin->fetchMessagePart(mailboxId, folderId, messageId, messagePartId);
   265     }
   260     }
   266     return value;
   261     return value;
   267 }
   262 }
   268 
   263 
       
   264 /*!
       
   265 
       
   266 */
       
   267 XQSharableFile NmUiEngine::messagePartFile(
       
   268         const NmId &mailboxId,
       
   269         const NmId &folderId,
       
   270         const NmId &messageId,
       
   271         const NmId &messagePartId)
       
   272 {
       
   273     NmDataPluginInterface *plugin =
       
   274         mPluginFactory->interfaceInstance(mailboxId);
       
   275     if (plugin) {
       
   276         return plugin->messagePartFile(mailboxId, folderId, messageId, messagePartId);
       
   277     }
       
   278     else {
       
   279         // empty file handle
       
   280         return XQSharableFile();
       
   281     }
       
   282 }
   269 /*!
   283 /*!
   270     Get content to message part
   284     Get content to message part
   271 */
   285 */
   272 int NmUiEngine::contentToMessagePart(
   286 int NmUiEngine::contentToMessagePart(
   273     const NmId &mailboxId,
   287     const NmId &mailboxId,
   422     }
   436     }
   423     return ret;
   437     return ret;
   424 }
   438 }
   425 
   439 
   426 /*!
   440 /*!
   427     Saves a message with its subparts (into message store).
       
   428     Ownership of operation object is transferred to the caller.
       
   429 */
       
   430 NmOperation *NmUiEngine::saveMessageWithSubparts(const NmMessage &message)
       
   431 {
       
   432     NmOperation *value(NULL);
       
   433     const NmId &mailboxId = message.mailboxId();
       
   434     
       
   435     NmDataPluginInterface *plugin =
       
   436         mPluginFactory->interfaceInstance(mailboxId);
       
   437 
       
   438     if (plugin) {
       
   439         value = plugin->saveMessageWithSubparts(message);
       
   440     }
       
   441     return value;
       
   442 }
       
   443 
       
   444 /*!
       
   445     Refreshes mailbox.
   441     Refreshes mailbox.
   446 */
   442 */
   447 int NmUiEngine::refreshMailbox(const NmId &mailboxId )
   443 int NmUiEngine::refreshMailbox(const NmId &mailboxId )
   448 {
   444 {
   449     int ret(NmNotFoundError);
   445     int ret(NmNotFoundError);
   450     NmDataPluginInterface *plugin =
   446     NmDataPluginInterface *plugin =
   451         mPluginFactory->interfaceInstance(mailboxId);
   447         mPluginFactory->interfaceInstance(mailboxId);
   452     if (plugin) {
   448     if (plugin) {
   453         ret = plugin->refreshMailbox(mailboxId);
   449         ret = plugin->refreshMailbox(mailboxId);
       
   450     }
       
   451     return ret;
       
   452 }
       
   453 
       
   454 /*!
       
   455     Online mailbox.
       
   456 */
       
   457 int NmUiEngine::goOnline(const NmId &mailboxId )
       
   458 {
       
   459     int ret(NmNotFoundError);
       
   460     NmDataPluginInterface *plugin =
       
   461         mPluginFactory->interfaceInstance(mailboxId);
       
   462     if (plugin) {
       
   463         ret = plugin->goOnline(mailboxId);
       
   464     }
       
   465     return ret;
       
   466 }
       
   467 
       
   468 /*!
       
   469     Offline mailbox.
       
   470 */
       
   471 int NmUiEngine::goOffline(const NmId &mailboxId )
       
   472 {
       
   473     int ret(NmNotFoundError);
       
   474     NmDataPluginInterface *plugin =
       
   475         mPluginFactory->interfaceInstance(mailboxId);
       
   476     if (plugin) {
       
   477         ret = plugin->goOffline(mailboxId);
   454     }
   478     }
   455     return ret;
   479     return ret;
   456 }
   480 }
   457 
   481 
   458 /*!
   482 /*!
   488 }
   512 }
   489 
   513 
   490 /*!
   514 /*!
   491     Sends the given message.
   515     Sends the given message.
   492  */
   516  */
   493 void NmUiEngine::sendMessage(NmMessage *message)
   517 void NmUiEngine::sendMessage(NmMessage *message, const QList<NmOperation *> &preliminaryOperations)
   494 {
   518 {
   495     //First trigger message storing
   519     //First trigger message storing
   496     if (message) {
   520     if (message) {
   497 
   521         NmDataPluginInterface *plugin =
   498         if (mMessage) {
   522             mPluginFactory->interfaceInstance(message->mailboxId());
   499             delete mMessage;
   523         
   500             mMessage = NULL;
   524         if (plugin) {
   501         }
   525             // to be on the safer side:
   502         mMessage = message;
   526             // we shouldn't even be here if mSendOperation != NULL
   503 
   527             delete mSendOperation;
   504         if (mSaveOperation) {
   528             mSendOperation = NULL;
   505             delete mSaveOperation;
   529             // ownership of message changes
   506             mSaveOperation = NULL;
   530             mSendOperation = plugin->sendMessage(message);
   507         }
   531             // don't put this to mOperations as we need to handle this
   508         mSaveOperation = this->saveMessageWithSubparts(*message);
   532             // operation separately
   509 
   533             if (mSendOperation) {
   510         if (mSaveOperation) {
   534                 foreach (NmOperation *op, preliminaryOperations) {
   511             connect(mSaveOperation, SIGNAL(operationCompleted(int)), this,
   535                     // ownership is transferred
   512                 SLOT(handleCompletedSaveOperation(int)));
   536                     mSendOperation->addPreliminaryOperation(op);
   513         }
   537                 }
   514         mMessageToBeSent = true;
   538                 
       
   539                 connect(mSendOperation, 
       
   540                         SIGNAL(operationCompleted(int)), 
       
   541                         this, 
       
   542                         SLOT(handleCompletedSendOperation()));
       
   543             }
       
   544         }
   515     }
   545     }
   516 }
   546 }
   517 
   547 
   518 /*!
   548 /*!
   519     Is sending operation in progress.
   549     Is sending operation in progress.
   528 }
   558 }
   529 
   559 
   530 /*!
   560 /*!
   531    Returns a pointer to the message that is being sent. Returns NULL if not sending.
   561    Returns a pointer to the message that is being sent. Returns NULL if not sending.
   532  */
   562  */
   533 const NmMessage *NmUiEngine::messageBeingSent()
   563 const NmMessage *NmUiEngine::messageBeingSent() const
   534 {
   564 {
   535     const NmMessage *message = NULL;
   565     const NmMessage *message = NULL;
   536     
   566     
   537     if (mSendOperation != NULL) {
   567     if (mSendOperation != NULL) {
   538         message = mSendOperation->getMessage();
   568         message = mSendOperation->getMessage();
   646         }
   676         }
   647     NMLOG("NmUiEngine::handleCompletedOperation() --->");
   677     NMLOG("NmUiEngine::handleCompletedOperation() --->");
   648 }
   678 }
   649     
   679     
   650 /*!
   680 /*!
   651     Handle completed send operation
   681     Handle completed send operation.
   652  */
   682 */
   653 void NmUiEngine::handleCompletedSendOperation()
   683 void NmUiEngine::handleCompletedSendOperation()
   654 { // let the callback method finish until cleaning the operation
   684 {
       
   685     // Let the callback method finish until cleaning the operation.
   655     QTimer::singleShot(1, this, SLOT(cleanupSendOperation()));
   686     QTimer::singleShot(1, this, SLOT(cleanupSendOperation()));
       
   687     emit sendOperationCompleted();
   656 }
   688 }
   657 
   689 
   658 /*!
   690 /*!
   659     Cleanup the send operation
   691     Cleanup the send operation
   660  */
   692 */
   661 void NmUiEngine::cleanupSendOperation()
   693 void NmUiEngine::cleanupSendOperation()
   662 {
   694 {
   663     delete mSendOperation;
   695     delete mSendOperation;
   664     mSendOperation = NULL;
   696     mSendOperation = NULL;
   665     // delete the sent messages from the store if necessary
   697     // delete the sent messages from the store if necessary
   666     // ...
   698     // ...
   667 }
   699 }
   668  
   700 
   669 /*!
   701 /*!
   670     Handle completed store message operation
   702     Handles synch operation related events
   671  */
   703  */
   672 void NmUiEngine::handleCompletedSaveOperation(int error)
   704 void NmUiEngine::handleSyncStateEvent(NmSyncState syncState, const NmOperationCompletionEvent &event)
   673 {
   705 {
   674 	if (mMessage && mMessageToBeSent && error == NmNoError)
   706     NMLOG("NmUiEngine::handleSyncStateEvent()");
   675 	{
   707 
   676 	    mMessageToBeSent = false;
   708     if ( syncState == SyncComplete ) {
   677 	    
   709         // signal for reporting about (sync) operation completion status
   678 	    NmDataPluginInterface *plugin =
   710         emit operationCompleted(event);
   679             mPluginFactory->interfaceInstance(mMessage->mailboxId());
   711     }
   680         
   712 
   681         if (plugin) {
   713     // signal for handling sync state icons
   682             // to be on the safer side:
   714     emit syncStateEvent(syncState, event.mMailboxId);
   683             // we shouldn't even be here if mSendOperation != NULL
   715 }
   684 			delete mSendOperation;
   716 
   685             mSendOperation = NULL;
   717 
   686             // ownership of mMessage changes
       
   687             mSendOperation = plugin->sendMessage(mMessage);
       
   688             mMessage = NULL;
       
   689             // don't put this to mOperations as we need to handle this
       
   690             // operation separately
       
   691 			if (mSendOperation) {
       
   692 			    connect(mSendOperation, 
       
   693 			            SIGNAL(operationCompleted(int)), 
       
   694 			            this, 
       
   695 			            SLOT(handleCompletedSendOperation()));
       
   696 			}
       
   697         }
       
   698 	}
       
   699 }