qtmobility/src/messaging/qmtmengine_symbian.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
    75 #include <eikenv.h>
    75 #include <eikenv.h>
    76 #include <smut.h>
    76 #include <smut.h>
    77 #include <smuthdr.h>
    77 #include <smuthdr.h>
    78 #include <mtuireg.h> // CMtmUiRegistry
    78 #include <mtuireg.h> // CMtmUiRegistry
    79 #include <mtmuibas.h> // CBaseMtmUi
    79 #include <mtmuibas.h> // CBaseMtmUi
    80 #include <SendUiConsts.h>
    80 #include <senduiconsts.h>
    81 #include <sendui.h>    // SendUi API
    81 #include <sendui.h>    // SendUi API
    82 #include <CMessageData.h> //CMessageData
    82 #include <cmessagedata.h> //CMessageData
    83 #include <apgcli.h>
    83 #include <apgcli.h>
    84 #include <rsendas.h>
    84 #include <rsendas.h>
    85 #include <rsendasmessage.h>
    85 #include <rsendasmessage.h>
    86 #include <cmsvrecipientlist.h>
    86 #include <cmsvrecipientlist.h>
    87 #include <imapset.h>
    87 #include <imapset.h>
   274                     accountIds.append(value.id());
   274                     accountIds.append(value.id());
   275                 }
   275                 }
   276             }
   276             }
   277         }
   277         }
   278     }
   278     }
   279     
   279 
   280     if (!sortOrder.isEmpty()) {
   280     if (!sortOrder.isEmpty()) {
   281         orderAccounts(accountIds, sortOrder);
   281         orderAccounts(accountIds, sortOrder);
   282     }
   282     }
   283     
   283 
   284     applyOffsetAndLimitToAccountIds(accountIds, offset, limit);
   284     applyOffsetAndLimitToAccountIds(accountIds, offset, limit);
   285         
   285     
   286     return accountIds;
   286     return accountIds;
   287 }
   287 }
   288 
   288 
   289 void CMTMEngine::applyOffsetAndLimitToAccountIds(QMessageAccountIdList& idList, int offset, int limit) const
   289 void CMTMEngine::applyOffsetAndLimitToAccountIds(QMessageAccountIdList& idList, int offset, int limit) const
   290 {
   290 {
   701 }
   701 }
   702 
   702 
   703 void CMTMEngine::showMessageL(const QMessageId &id)
   703 void CMTMEngine::showMessageL(const QMessageId &id)
   704 {
   704 {
   705     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
   705     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
   706     
   706 
   707     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
   707     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
   708     CleanupStack::PushL(pEntry);
   708     CleanupStack::PushL(pEntry);
   709     CBaseMtm* mtm = ipClientMtmReg->NewMtmL(pEntry->Entry().iMtm);
   709 
   710     CleanupStack::PushL(mtm);
   710     CBaseMtm* pMtm = ipClientMtmReg->NewMtmL(pEntry->Entry().iMtm);
   711     
   711     CleanupStack::PushL(pMtm);
   712     CMtmUiRegistry* mtmUiRegistry = CMtmUiRegistry::NewL(*ipMsvSession);
   712     
   713     CleanupStack::PushL(mtmUiRegistry); 
   713     CMtmUiRegistry* pMtmUiRegistry = CMtmUiRegistry::NewL(*ipMsvSession);
   714 
   714     CleanupStack::PushL(pMtmUiRegistry); 
   715     CBaseMtmUi* ui = mtmUiRegistry->NewMtmUiL(*mtm); 
   715 
   716     CleanupStack::PushL(ui);
   716     CBaseMtmUi* pMtmUi = pMtmUiRegistry->NewMtmUiL(*pMtm); 
   717     
   717     CleanupStack::PushL(pMtmUi);
   718     ui->BaseMtm().SwitchCurrentEntryL(messageId);
   718     
   719     CMsvOperationWait* waiter = CMsvOperationWait::NewLC();
   719     pMtmUi->BaseMtm().SwitchCurrentEntryL(messageId);
   720     waiter->Start(); 
   720 
   721     CMsvOperation* op = ui->OpenL(waiter->iStatus);
   721     QMTMWait mtmWait;
   722     CleanupStack::PushL(op);
   722     
   723     
   723     CMsvOperation* pMsvOperation = pMtmUi->OpenL(mtmWait.iStatus);
   724     CActiveScheduler::Start();
   724     
   725 
   725     mtmWait.start();
   726     CleanupStack::PopAndDestroy(6); // op,waiter,ui,mtmuiregistry,mtm,pEntry
   726 
       
   727     delete pMsvOperation;
       
   728     
       
   729     CleanupStack::PopAndDestroy(pMtmUi);
       
   730     CleanupStack::PopAndDestroy(pMtmUiRegistry);
       
   731     CleanupStack::PopAndDestroy(pMtm);
       
   732     CleanupStack::PopAndDestroy(pEntry);
   727 }
   733 }
   728 
   734 
   729 bool CMTMEngine::composeMessage(const QMessage &message)
   735 bool CMTMEngine::composeMessage(const QMessage &message)
   730 {
   736 {
   731     bool retVal = true;
   737     bool retVal = true;
   955     sendUi->CreateAndSendMessageL(KSenduiMtmSmtpUid, messageData, KNullUid, ETrue);
   961     sendUi->CreateAndSendMessageL(KSenduiMtmSmtpUid, messageData, KNullUid, ETrue);
   956     CleanupStack::PopAndDestroy(2); //messageData and sendUi
   962     CleanupStack::PopAndDestroy(2); //messageData and sendUi
   957     return true;
   963     return true;
   958 }
   964 }
   959 
   965 
   960 bool CMTMEngine::retrieve(const QMessageId &messageId, const QMessageContentContainerId& id)
   966 bool CMTMEngine::retrieve(QMessageServicePrivate& privateService, const QMessageId &messageId, const QMessageContentContainerId& id)
   961 {
   967 {
   962     TRAPD(err, retrieveL(messageId, id));
   968     TRAPD(err, retrieveL(privateService, messageId, id));
   963         if (err != KErrNone)
   969         if (err != KErrNone)
   964             return false;
   970             return false;
   965         else
   971         else
   966             return true;
   972             return true;
   967 }
   973 }
   968 
   974 
   969 void CMTMEngine::retrieveL(const QMessageId &messageId, const QMessageContentContainerId& id)
   975 void CMTMEngine::retrieveL(QMessageServicePrivate& privateService, const QMessageId &messageId, const QMessageContentContainerId& id)
   970 {
   976 {
   971     Q_UNUSED(id); // all attachments are retrieved (cannot retrieve only one)
   977     Q_UNUSED(id); // all attachments are retrieved (cannot retrieve only one)
   972     
   978     
   973     long int messId = SymbianHelpers::stripIdPrefix(messageId.toString()).toLong();
   979     long int msgId = SymbianHelpers::stripIdPrefix(messageId.toString()).toLong();
   974     
   980     
   975     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messId);
   981     CMsvEntry* pEntry = ipMsvSession->GetEntryL(msgId);
   976     CleanupStack::PushL(pEntry);
   982     CleanupStack::PushL(pEntry);
   977     
   983     
   978     CMsvEntrySelection* sel = new(ELeave) CMsvEntrySelection;
   984     CAsynchronousMTMOperation* pMTMOperation = NULL;
   979     CleanupStack::PushL(sel);
       
   980     
       
   981     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
   982 
       
   983     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
   985     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
   984         TPckgBuf<TInt> parameter;
   986         pMTMOperation = createAsynchronousMTMOperation(privateService,
   985         
   987                                                        ipImap4Mtm,
   986         ipImap4Mtm->SwitchCurrentEntryL(pEntry->OwningService());
   988                                                        pEntry->OwningService());
   987         
   989     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
   988         sel->AppendL(messId);
   990         pMTMOperation = createAsynchronousMTMOperation(privateService,
   989         
   991                                                        ipPop3Mtm,
   990         CMsvOperation* opConnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMConnect,
   992                                                        pEntry->OwningService());
   991                                                     *sel, parameter, wait->iStatus);
   993     }
   992         CleanupStack::PushL(opConnect);
   994 
   993         wait->Start();
   995     if (pMTMOperation) {
   994         CActiveScheduler::Start();    
   996         if (!pMTMOperation->retrieveMessageAttachments(msgId)) {
   995         
   997             User::Leave(KErrAccessDenied);
   996         TImImap4GetPartialMailInfo info;
   998         }
   997         info.iPartialMailOptions = EAttachmentsOnly;
   999     } else {
   998         TPckg<TImImap4GetPartialMailInfo> bodyInfo(info);
  1000         User::Leave(KErrArgument);
   999 
  1001     }
  1000         CMsvOperation* opPopulate = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
  1002     
  1001                                                     *sel, bodyInfo, wait->iStatus);
       
  1002         CleanupStack::PushL(opPopulate);
       
  1003         wait->Start();
       
  1004         CActiveScheduler::Start();
       
  1005         
       
  1006         if (wait->iStatus.Int() != KErrNone) { 
       
  1007             if (wait->iStatus.Int() == KErrNotFound){
       
  1008                 // TODO: set messagestatus removed
       
  1009             }
       
  1010         }
       
  1011         
       
  1012         CMsvOperation* opDisconnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,
       
  1013                                                     *sel, parameter, wait->iStatus);
       
  1014         CleanupStack::PushL(opDisconnect);
       
  1015         
       
  1016         wait->Start();
       
  1017         CActiveScheduler::Start();    
       
  1018         
       
  1019         CleanupStack::PopAndDestroy(3);
       
  1020     }
       
  1021     
       
  1022     if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
       
  1023         TPckgBuf<TInt> parameter;
       
  1024         
       
  1025         ipPop3Mtm->SwitchCurrentEntryL(pEntry->OwningService());    
       
  1026 
       
  1027         sel->AppendL(pEntry->EntryId());
       
  1028         
       
  1029         CMsvOperation* opConnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMConnect,
       
  1030                                                     *sel, parameter, wait->iStatus);
       
  1031         
       
  1032         CleanupStack::PushL(opConnect);
       
  1033         wait->Start();
       
  1034         CActiveScheduler::Start();    
       
  1035         
       
  1036         CImPop3Settings *popSettings = new (ELeave) CImPop3Settings;
       
  1037         CleanupStack::PushL(popSettings);
       
  1038         CEmailAccounts *emailAccounts = CEmailAccounts::NewLC();
       
  1039         TPopAccount account;
       
  1040         emailAccounts->GetPopAccountL(pEntry->OwningService(), account);
       
  1041         emailAccounts->LoadPopSettingsL(account, *popSettings);
       
  1042         // cannot retrieve only attachment, have to retrieve entire message
       
  1043         popSettings->SetGetMailOptions(EGetPop3EmailMessages);
       
  1044         emailAccounts->SavePopSettingsL(account,*popSettings);
       
  1045         CleanupStack::PopAndDestroy(emailAccounts);
       
  1046         CleanupStack::PopAndDestroy(popSettings);
       
  1047 
       
  1048         CMsvOperation* opPopulate = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  1049                                                     *sel, parameter, wait->iStatus);
       
  1050         CleanupStack::PushL(opPopulate);
       
  1051         wait->Start();
       
  1052         CActiveScheduler::Start();
       
  1053         
       
  1054         CMsvOperation* opDisconnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMDisconnect,
       
  1055                                                     *sel, parameter, wait->iStatus);
       
  1056         CleanupStack::PushL(opDisconnect);
       
  1057         
       
  1058         wait->Start();
       
  1059         CActiveScheduler::Start();    
       
  1060         CleanupStack::PopAndDestroy(3);
       
  1061         
       
  1062     }
       
  1063     
       
  1064     CleanupStack::PopAndDestroy(wait);
       
  1065     CleanupStack::PopAndDestroy(sel);    
       
  1066     CleanupStack::PopAndDestroy(pEntry);
  1003     CleanupStack::PopAndDestroy(pEntry);
  1067 }
  1004 }
  1068 
  1005 
  1069 bool CMTMEngine::retrieveBody(const QMessageId& id)
  1006 bool CMTMEngine::retrieveBody(QMessageServicePrivate& privateService, const QMessageId& id)
  1070 {
  1007 {
  1071     TRAPD(err, retrieveBodyL(id));
  1008     TRAPD(err, retrieveBodyL(privateService, id));
  1072         if (err != KErrNone)
  1009         if (err != KErrNone)
  1073             return false;
  1010             return false;
  1074         else
  1011         else
  1075             return true;
  1012             return true;
  1076 }
  1013 }
  1077 
  1014 
  1078 void CMTMEngine::retrieveBodyL(const QMessageId& id) const
  1015 void CMTMEngine::retrieveBodyL(QMessageServicePrivate& privateService, const QMessageId& id)
  1079 {
  1016 {
  1080     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1017     long int msgId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1081     
  1018     
  1082     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  1019     CMsvEntry* pEntry = ipMsvSession->GetEntryL(msgId);
  1083     CleanupStack::PushL(pEntry);
  1020     CleanupStack::PushL(pEntry);
  1084     
  1021     
  1085     CMsvEntrySelection* sel = new(ELeave) CMsvEntrySelection;
  1022     CAsynchronousMTMOperation* pMTMOperation = NULL;
  1086     CleanupStack::PushL(sel);
       
  1087     
       
  1088     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
  1089 
       
  1090     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
  1023     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
  1091         TPckgBuf<TInt> parameter;
  1024         pMTMOperation = createAsynchronousMTMOperation(privateService,
  1092         
  1025                                                        ipImap4Mtm,
  1093         ipImap4Mtm->SwitchCurrentEntryL(pEntry->OwningService());
  1026                                                        pEntry->OwningService());
  1094         
  1027     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
  1095         sel->AppendL(messageId);
  1028         pMTMOperation = createAsynchronousMTMOperation(privateService,
  1096         
  1029                                                        ipPop3Mtm,
  1097         CMsvOperation* opConnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMConnect,
  1030                                                        pEntry->OwningService());
  1098                                                     *sel, parameter, wait->iStatus);
  1031     }
  1099         CleanupStack::PushL(opConnect);
  1032 
  1100         wait->Start();
  1033     if (pMTMOperation) {
  1101         CActiveScheduler::Start();    
  1034         if (!pMTMOperation->retrieveMessageBody(msgId)) {
  1102         
  1035             User::Leave(KErrAccessDenied);
  1103         TImImap4GetPartialMailInfo info;
  1036         }
  1104         info.iPartialMailOptions = EBodyTextOnly;
  1037     } else {
  1105         TPckg<TImImap4GetPartialMailInfo> bodyInfo(info);
  1038         User::Leave(KErrArgument);
  1106 
  1039     }
  1107         CMsvOperation* opPopulate = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
  1040     
  1108                                                     *sel, bodyInfo, wait->iStatus);
       
  1109         CleanupStack::PushL(opPopulate);
       
  1110         wait->Start();
       
  1111         CActiveScheduler::Start();
       
  1112         
       
  1113         if (wait->iStatus.Int() != KErrNone) { 
       
  1114             if (wait->iStatus.Int() == KErrNotFound){
       
  1115                 // TODO: set messagestatus removed
       
  1116             }
       
  1117         }
       
  1118         
       
  1119         CMsvOperation* opDisconnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,
       
  1120                                                     *sel, parameter, wait->iStatus);
       
  1121         CleanupStack::PushL(opDisconnect);
       
  1122         
       
  1123         wait->Start();
       
  1124         CActiveScheduler::Start();    
       
  1125         
       
  1126         CleanupStack::PopAndDestroy(3);
       
  1127     }
       
  1128     
       
  1129     if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
       
  1130         TPckgBuf<TInt> parameter;
       
  1131         
       
  1132         ipPop3Mtm->SwitchCurrentEntryL(pEntry->OwningService());    
       
  1133 
       
  1134         sel->AppendL(pEntry->EntryId());
       
  1135         
       
  1136         CMsvOperation* opConnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMConnect,
       
  1137                                                     *sel, parameter, wait->iStatus);
       
  1138         
       
  1139         CleanupStack::PushL(opConnect);
       
  1140         wait->Start();
       
  1141         CActiveScheduler::Start();    
       
  1142         
       
  1143         CImPop3Settings *popSettings = new (ELeave) CImPop3Settings;
       
  1144         CleanupStack::PushL(popSettings);
       
  1145         CEmailAccounts *emailAccounts = CEmailAccounts::NewLC();
       
  1146         TPopAccount account;
       
  1147         emailAccounts->GetPopAccountL(pEntry->OwningService(), account);
       
  1148         emailAccounts->LoadPopSettingsL(account, *popSettings);
       
  1149         // cannot retrieve only body, have to retrieve entire message
       
  1150         popSettings->SetGetMailOptions(EGetPop3EmailMessages);
       
  1151         emailAccounts->SavePopSettingsL(account,*popSettings);
       
  1152         CleanupStack::PopAndDestroy(emailAccounts);
       
  1153         CleanupStack::PopAndDestroy(popSettings);
       
  1154 
       
  1155         CMsvOperation* opPopulate = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  1156                                                     *sel, parameter, wait->iStatus);
       
  1157         CleanupStack::PushL(opPopulate);
       
  1158         wait->Start();
       
  1159         CActiveScheduler::Start();
       
  1160         
       
  1161         CMsvOperation* opDisconnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMDisconnect,
       
  1162                                                     *sel, parameter, wait->iStatus);
       
  1163         CleanupStack::PushL(opDisconnect);
       
  1164         
       
  1165         wait->Start();
       
  1166         CActiveScheduler::Start();    
       
  1167         CleanupStack::PopAndDestroy(3);
       
  1168         
       
  1169     }
       
  1170     
       
  1171     CleanupStack::PopAndDestroy(wait);
       
  1172     CleanupStack::PopAndDestroy(sel);    
       
  1173     CleanupStack::PopAndDestroy(pEntry);
  1041     CleanupStack::PopAndDestroy(pEntry);
  1174 }
  1042 }
  1175 
  1043 
  1176 bool CMTMEngine::retrieveHeader(const QMessageId& id)
  1044 bool CMTMEngine::retrieveHeader(QMessageServicePrivate& privateService, const QMessageId& id)
  1177 {
  1045 {
  1178     TRAPD(err, retrieveHeaderL(id));
  1046     TRAPD(err, retrieveHeaderL(privateService, id));
  1179         if (err != KErrNone)
  1047         if (err != KErrNone)
  1180             return false;
  1048             return false;
  1181         else
  1049         else
  1182             return true;
  1050             return true;
  1183 }
  1051 }
  1184 
  1052 
  1185 void CMTMEngine::retrieveHeaderL(const QMessageId& id) const
  1053 void CMTMEngine::retrieveHeaderL(QMessageServicePrivate& privateService, const QMessageId& id)
  1186 {
  1054 {
  1187     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1055     long int msgId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1188     
  1056     
  1189     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  1057     CMsvEntry* pEntry = ipMsvSession->GetEntryL(msgId);
  1190     CleanupStack::PushL(pEntry);
  1058     CleanupStack::PushL(pEntry);
  1191     
  1059     
  1192     CMsvEntrySelection* sel = new(ELeave) CMsvEntrySelection;
  1060     CAsynchronousMTMOperation* pMTMOperation = NULL;
  1193     CleanupStack::PushL(sel);
       
  1194     
       
  1195     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
       
  1196 
       
  1197     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
  1061     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4){    
  1198         TPckgBuf<TInt> parameter;
  1062         pMTMOperation = createAsynchronousMTMOperation(privateService,
  1199         
  1063                                                        ipImap4Mtm,
  1200         ipImap4Mtm->SwitchCurrentEntryL(pEntry->OwningService());
  1064                                                        pEntry->OwningService());
  1201         
  1065     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
  1202         sel->AppendL(messageId);
  1066         pMTMOperation = createAsynchronousMTMOperation(privateService,
  1203         
  1067                                                        ipPop3Mtm,
  1204         CMsvOperation* opConnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMConnect,
  1068                                                        pEntry->OwningService());
  1205                                                     *sel, parameter, wait->iStatus);
  1069     }
  1206         CleanupStack::PushL(opConnect);
  1070     
  1207         wait->Start();
  1071     if (pMTMOperation) {
  1208         CActiveScheduler::Start();    
  1072         if (!pMTMOperation->retrieveMessageHeader(msgId)) {
  1209             
  1073             User::Leave(KErrAccessDenied);
  1210         TImImap4GetMailInfo info;
  1074         }
  1211         info.iGetMailBodyParts = EGetImap4EmailHeaders;
  1075     } else {
  1212         TPckg<TImImap4GetMailInfo> bodyInfo(info);
  1076         User::Leave(KErrArgument);
  1213         
  1077     }
  1214         CMsvOperation* opPopulate = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
  1078     
  1215                                                     *sel, bodyInfo, wait->iStatus);
       
  1216         CleanupStack::PushL(opPopulate);
       
  1217         wait->Start();
       
  1218         CActiveScheduler::Start();
       
  1219         
       
  1220         CMsvOperation* opDisconnect = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,
       
  1221                                                     *sel, parameter, wait->iStatus);
       
  1222         CleanupStack::PushL(opDisconnect);
       
  1223         
       
  1224         wait->Start();
       
  1225         CActiveScheduler::Start();    
       
  1226         
       
  1227         CleanupStack::PopAndDestroy(3);
       
  1228     }
       
  1229     
       
  1230     if (pEntry->Entry().iMtm == KUidMsgTypePOP3){
       
  1231         TPckgBuf<TInt> parameter;
       
  1232         
       
  1233         ipPop3Mtm->SwitchCurrentEntryL(pEntry->OwningService());    
       
  1234 
       
  1235         sel->AppendL(pEntry->EntryId());
       
  1236         
       
  1237         CMsvOperation* opConnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMConnect,
       
  1238                                                     *sel, parameter, wait->iStatus);
       
  1239         CleanupStack::PushL(opConnect);
       
  1240         wait->Start();
       
  1241         CActiveScheduler::Start();    
       
  1242         
       
  1243         CImPop3Settings *popSettings = new (ELeave) CImPop3Settings;
       
  1244         CleanupStack::PushL(popSettings);
       
  1245         CEmailAccounts *emailAccounts = CEmailAccounts::NewLC();
       
  1246         TPopAccount account;
       
  1247         emailAccounts->GetPopAccountL(pEntry->OwningService(), account);
       
  1248         emailAccounts->LoadPopSettingsL(account, *popSettings);
       
  1249         popSettings->SetGetMailOptions(EGetPop3EmailHeaders);
       
  1250         emailAccounts->SavePopSettingsL(account,*popSettings);
       
  1251         CleanupStack::PopAndDestroy(emailAccounts);
       
  1252         CleanupStack::PopAndDestroy(popSettings);
       
  1253 
       
  1254         CMsvOperation* opPopulate = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  1255                                                     *sel, parameter, wait->iStatus);
       
  1256         CleanupStack::PushL(opPopulate);
       
  1257         wait->Start();
       
  1258         CActiveScheduler::Start();
       
  1259         
       
  1260         CMsvOperation* opDisconnect = ipPop3Mtm->InvokeAsyncFunctionL(KPOP3MTMDisconnect,
       
  1261                                                     *sel, parameter, wait->iStatus);
       
  1262         CleanupStack::PushL(opDisconnect);
       
  1263         
       
  1264         wait->Start();
       
  1265         CActiveScheduler::Start();    
       
  1266         
       
  1267         CleanupStack::PopAndDestroy(3);
       
  1268         
       
  1269     }
       
  1270     
       
  1271     CleanupStack::PopAndDestroy(wait);
       
  1272     CleanupStack::PopAndDestroy(sel);    
       
  1273     CleanupStack::PopAndDestroy(pEntry);
  1079     CleanupStack::PopAndDestroy(pEntry);
  1274 }
  1080 }
  1275 
  1081 
  1276 bool CMTMEngine::exportUpdates(const QMessageAccountId &id)
  1082 bool CMTMEngine::exportUpdates(QMessageServicePrivate& privateService, const QMessageAccountId &id)
  1277 {
  1083 {
  1278     TRAPD(err, exportUpdatesL(id));
  1084     TRAPD(err, exportUpdatesL(privateService, id));
  1279     if (err != KErrNone) {
  1085     if (err != KErrNone) {
  1280         return false;
  1086         return false;
  1281     } else {
  1087     } else {
  1282         return true;
  1088         return true;
  1283     }
  1089     }
  1284 }
  1090 }
  1285 
  1091 
  1286 void CMTMEngine::exportUpdatesL(const QMessageAccountId &id) const
  1092 void CMTMEngine::exportUpdatesL(QMessageServicePrivate& privateService, const QMessageAccountId &id)
  1287 {
  1093 {
       
  1094     CAsynchronousMTMOperation* pMTMOperation = NULL;
  1288     QMessageAccount account = this->account(id);
  1095     QMessageAccount account = this->account(id);
  1289     CMsvEntry* pEntry = retrieveCMsvEntryAndPushToCleanupStack(account.d_ptr->_service1EntryId);
  1096     CMsvEntry* pEntry = retrieveCMsvEntryAndPushToCleanupStack(account.d_ptr->_service1EntryId);
  1290     if (!pEntry) {
  1097     if (!pEntry) {
  1291         User::Leave(KErrNotFound);
  1098         User::Leave(KErrNotFound);
  1292     }
  1099     }
  1293     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4) {
  1100     if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4) {
  1294         TPckgBuf<TInt> parameter;
  1101         pMTMOperation = createAsynchronousMTMOperation(privateService,
  1295         CMsvEntrySelection* pMsvEntrySelection = new(ELeave) CMsvEntrySelection;
  1102                                                        ipImap4Mtm,
  1296         CleanupStack::PushL(pMsvEntrySelection);
  1103                                                        account.d_ptr->_service1EntryId);
  1297         CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  1104         pMTMOperation->doFullSync();
  1298         
       
  1299         ipImap4Mtm->SwitchCurrentEntryL(account.d_ptr->_service1EntryId);
       
  1300         pMsvEntrySelection->AppendL(account.d_ptr->_service1EntryId);
       
  1301         
       
  1302         CMsvOperation* pMsvOperation = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMConnect, *pMsvEntrySelection,
       
  1303                                                                         parameter, pMsvOperationWait->iStatus);
       
  1304         CleanupStack::PushL(pMsvOperation);
       
  1305         pMsvOperationWait->Start();
       
  1306         CActiveScheduler::Start();
       
  1307         CleanupStack::PopAndDestroy(pMsvOperation);
       
  1308 
       
  1309         pMsvOperation = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMFullSync, *pMsvEntrySelection,
       
  1310                                                          parameter, pMsvOperationWait->iStatus);
       
  1311         CleanupStack::PushL(pMsvOperation);
       
  1312         pMsvOperationWait->Start();
       
  1313         CActiveScheduler::Start();
       
  1314         CleanupStack::PopAndDestroy(pMsvOperation);
       
  1315         
       
  1316         pMsvOperation = ipImap4Mtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect, *pMsvEntrySelection,
       
  1317                                                          parameter, pMsvOperationWait->iStatus);
       
  1318         CleanupStack::PushL(pMsvOperation);
       
  1319         pMsvOperationWait->Start();
       
  1320         CActiveScheduler::Start();
       
  1321         CleanupStack::PopAndDestroy(pMsvOperation);
       
  1322         
       
  1323         CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  1324         CleanupStack::PopAndDestroy(pMsvEntrySelection);
       
  1325     }
  1105     }
  1326     releaseCMsvEntryAndPopFromCleanupStack(pEntry);
  1106     releaseCMsvEntryAndPopFromCleanupStack(pEntry);
       
  1107     
       
  1108     if (!pMTMOperation) {
       
  1109         User::Leave(KErrArgument);
       
  1110     }
  1327 }
  1111 }
  1328 
  1112 
  1329 bool CMTMEngine::removeMessageL(const QMessageId &id, QMessageManager::RemovalOption /*option*/)
  1113 bool CMTMEngine::removeMessageL(const QMessageId &id, QMessageManager::RemovalOption /*option*/)
  1330 {
  1114 {
  1331     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1115     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  1357         if (!ipImap4Mtm)
  1141         if (!ipImap4Mtm)
  1358             return false;
  1142             return false;
  1359         ipImap4Mtm->SwitchCurrentEntryL(messageId);
  1143         ipImap4Mtm->SwitchCurrentEntryL(messageId);
  1360         TMsvId parent = ipImap4Mtm->Entry().Entry().Parent();
  1144         TMsvId parent = ipImap4Mtm->Entry().Entry().Parent();
  1361         ipImap4Mtm->SwitchCurrentEntryL(parent);
  1145         ipImap4Mtm->SwitchCurrentEntryL(parent);
  1362         CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  1146         QMTMWait mtmWait;
  1363         CMsvOperation* pMsvOperation = ipImap4Mtm->Entry().DeleteL(messageId, pMsvOperationWait->iStatus);
  1147         CMsvOperation* pMsvOperation = ipImap4Mtm->Entry().DeleteL(messageId, mtmWait.iStatus);
  1364         CleanupStack::PushL(pMsvOperation);
  1148         mtmWait.start();
  1365         pMsvOperationWait->Start();
  1149         delete pMsvOperation;
  1366         CActiveScheduler::Start();
       
  1367         CleanupStack::PopAndDestroy(pMsvOperation);
       
  1368         CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  1369     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3) {
  1150     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3) {
  1370         if (!ipPop3Mtm)
  1151         if (!ipPop3Mtm)
  1371             return false;
  1152             return false;
  1372         ipPop3Mtm->SwitchCurrentEntryL(messageId);
  1153         ipPop3Mtm->SwitchCurrentEntryL(messageId);
  1373         TMsvId parent = ipPop3Mtm->Entry().Entry().Parent();
  1154         TMsvId parent = ipPop3Mtm->Entry().Entry().Parent();
  1374         ipPop3Mtm->SwitchCurrentEntryL(parent);
  1155         ipPop3Mtm->SwitchCurrentEntryL(parent);
  1375         CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  1156         QMTMWait mtmWait;
  1376         CMsvOperation* pMsvOperation = ipPop3Mtm->Entry().DeleteL(messageId, pMsvOperationWait->iStatus);
  1157         CMsvOperation* pMsvOperation = ipPop3Mtm->Entry().DeleteL(messageId, mtmWait.iStatus);
  1377         CleanupStack::PushL(pMsvOperation);
  1158         mtmWait.start();
  1378         pMsvOperationWait->Start();
  1159         delete pMsvOperation;
  1379         CActiveScheduler::Start();
       
  1380         CleanupStack::PopAndDestroy(pMsvOperation);
       
  1381         CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  1382     } 
  1160     } 
  1383     
  1161     
  1384     CleanupStack::PopAndDestroy(pEntry);
  1162     CleanupStack::PopAndDestroy(pEntry);
  1385 
  1163 
  1386     return true;
  1164     return true;
  1719                         orderMessages(iMessageQueries[index].ids, iMessageQueries[index].sortOrder);
  1497                         orderMessages(iMessageQueries[index].ids, iMessageQueries[index].sortOrder);
  1720                     }
  1498                     }
  1721                     applyOffsetAndLimitToMsgIds(iMessageQueries[index].ids,
  1499                     applyOffsetAndLimitToMsgIds(iMessageQueries[index].ids,
  1722                                                 iMessageQueries[index].offset,
  1500                                                 iMessageQueries[index].offset,
  1723                                                 iMessageQueries[index].limit);
  1501                                                 iMessageQueries[index].limit);
  1724                     emit iMessageQueries[index].privateService->messagesFound(iMessageQueries[index].ids);
  1502                     iMessageQueries[index].privateService->messagesFound(iMessageQueries[index].ids, true, true);
  1725                 } else {
  1503                 } else {
  1726                     emit iMessageQueries[index].privateService->messagesCounted(iMessageQueries[index].count);
  1504                     iMessageQueries[index].privateService->messagesCounted(iMessageQueries[index].count);
  1727                 }
  1505                 }
  1728                 iMessageQueries[index].privateService->_active = false;
       
  1729                 emit iMessageQueries[index].privateService->stateChanged(QMessageService::FinishedState);
       
  1730             }
  1506             }
  1731         } else {
  1507         } else {
  1732             // There was only one single filter to handle
  1508             // There was only one single filter to handle
  1733             if (numberOfHandledFilters == 0) {
  1509             if (numberOfHandledFilters == 0) {
  1734                 // The one and only filter was not handled
  1510                 // The one and only filter was not handled
  1746                 if (!iMessageQueries[index].sortOrder.isEmpty() && !resultSetOrdered) {
  1522                 if (!iMessageQueries[index].sortOrder.isEmpty() && !resultSetOrdered) {
  1747                     orderMessages(ids, iMessageQueries[index].sortOrder);
  1523                     orderMessages(ids, iMessageQueries[index].sortOrder);
  1748                 }
  1524                 }
  1749                 // Handle offest & limit
  1525                 // Handle offest & limit
  1750                 applyOffsetAndLimitToMsgIds(ids, iMessageQueries[index].offset, iMessageQueries[index].limit);
  1526                 applyOffsetAndLimitToMsgIds(ids, iMessageQueries[index].offset, iMessageQueries[index].limit);
  1751                 emit iMessageQueries[index].privateService->messagesFound(ids);
  1527                 iMessageQueries[index].privateService->messagesFound(ids, true, true);
  1752             } else {
  1528             } else {
  1753                 emit iMessageQueries[index].privateService->messagesCounted(ids.count());
  1529                 iMessageQueries[index].privateService->messagesCounted(ids.count());
  1754             }
  1530             }
  1755             iMessageQueries[index].privateService->_active = false;
       
  1756             emit iMessageQueries[index].privateService->stateChanged(QMessageService::FinishedState);
       
  1757         }
  1531         }
  1758     } else {
  1532     } else {
  1759         iMessageQueries[index].privateService->_active = false;
  1533         iMessageQueries[index].privateService->_active = false;
  1760         if (iMessageQueries[index].privateService->_error == QMessageManager::NoError) {
  1534         if (iMessageQueries[index].privateService->_error == QMessageManager::NoError) {
  1761             iMessageQueries[index].privateService->_error = QMessageManager::RequestIncomplete;
  1535             iMessageQueries[index].privateService->_error = QMessageManager::RequestIncomplete;
  1762         }
  1536         }
  1763         emit iMessageQueries[index].privateService->stateChanged(QMessageService::FinishedState);
       
  1764     }
  1537     }
  1765 
  1538 
  1766     delete iMessageQueries[index].findOperation;
  1539     delete iMessageQueries[index].findOperation;
  1767     iMessageQueries.removeAt(index);
  1540     iMessageQueries.removeAt(index);
  1768 }
  1541 }
  2317     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  2090     long int messageId = SymbianHelpers::stripIdPrefix(id.toString()).toLong();
  2318     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  2091     CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  2319     CleanupStack::PushL(pEntry);
  2092     CleanupStack::PushL(pEntry);
  2320     
  2093     
  2321     if (pEntry->Entry().iMtm == KUidMsgTypeSMS) {
  2094     if (pEntry->Entry().iMtm == KUidMsgTypeSMS) {
  2322         if (!ipSmsMtm)
  2095         if (ipSmsMtm)
  2323             return message;
  2096             message = smsMessageL(*pEntry, messageId);
  2324         message = smsMessageL(*pEntry, messageId);
       
  2325     } else if (pEntry->Entry().iMtm == KUidMsgTypeMultimedia) {
  2097     } else if (pEntry->Entry().iMtm == KUidMsgTypeMultimedia) {
  2326         if (!ipMmsMtm)
  2098         if (ipMmsMtm)
  2327             return message;
  2099             message = mmsMessageL(*pEntry, messageId);
  2328         message = mmsMessageL(*pEntry, messageId);
       
  2329     }  else if (pEntry->Entry().iMtm == KUidMsgTypeSMTP) {
  2100     }  else if (pEntry->Entry().iMtm == KUidMsgTypeSMTP) {
  2330         if (!ipSmtpMtm)
  2101         if (ipSmtpMtm)
  2331             return message;
  2102             message = emailMessageL(*pEntry, messageId);
  2332         message = emailMessageL(*pEntry, messageId);
       
  2333     } else if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4) {
  2103     } else if (pEntry->Entry().iMtm == KUidMsgTypeIMAP4) {
  2334         if (!ipImap4Mtm)
  2104         if (ipImap4Mtm)
  2335             return message;
  2105             message = emailMessageL(*pEntry, messageId);
  2336         message = emailMessageL(*pEntry, messageId);
       
  2337     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3) {
  2106     } else if (pEntry->Entry().iMtm == KUidMsgTypePOP3) {
  2338         if (!ipPop3Mtm)
  2107         if (ipPop3Mtm)
  2339             return message;
  2108             message = emailMessageL(*pEntry, messageId);
  2340         message = emailMessageL(*pEntry, messageId);
       
  2341     }
  2109     }
  2342 
  2110 
  2343     CleanupStack::PopAndDestroy(pEntry);
  2111     CleanupStack::PopAndDestroy(pEntry);
  2344 
  2112 
  2345     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  2113     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  2601     // <=> Date field is used to control message send time
  2369     // <=> Date field is used to control message send time
  2602     entry.iDate.UniversalTime();
  2370     entry.iDate.UniversalTime();
  2603 
  2371 
  2604     // Update message sending state
  2372     // Update message sending state
  2605     entry.SetSendingState(KMsvSendStateWaiting);
  2373     entry.SetSendingState(KMsvSendStateWaiting);
  2606     
  2374 
  2607     // Set SMS Service & delivery settings to the SMS header
  2375     // Set SMS Service & delivery settings to the SMS header
  2608     CSmsHeader& smsHeader = ipSmsMtm->SmsHeader();
  2376     CSmsHeader& smsHeader = ipSmsMtm->SmsHeader();
  2609     CSmsSettings* pSmsSettings = CSmsSettings::NewL();
  2377     CSmsSettings* pSmsSettings = CSmsSettings::NewL();
  2610     CleanupStack::PushL(pSmsSettings);
  2378     CleanupStack::PushL(pSmsSettings);
  2611  
  2379  
  2615     smsHeader.SetSmsSettingsL(*pSmsSettings);
  2383     smsHeader.SetSmsSettingsL(*pSmsSettings);
  2616  
  2384  
  2617     if (smsHeader.Message().ServiceCenterAddress().Length() == 0) {
  2385     if (smsHeader.Message().ServiceCenterAddress().Length() == 0) {
  2618         CSmsSettings* pSmsServiceSettings = &(ipSmsMtm->ServiceSettings());
  2386         CSmsSettings* pSmsServiceSettings = &(ipSmsMtm->ServiceSettings());
  2619         if (!pSmsServiceSettings->ServiceCenterCount()) {
  2387         if (!pSmsServiceSettings->ServiceCenterCount()) {
       
  2388 #ifndef Q_CC_NOKIAX86
  2620             User::Leave(KErrNotReady);
  2389             User::Leave(KErrNotReady);
       
  2390 #endif
  2621         } else {
  2391         } else {
  2622             CSmsNumber* pSmsCenterNumber = CSmsNumber::NewL();
  2392             CSmsNumber* pSmsCenterNumber = CSmsNumber::NewL();
  2623             CleanupStack::PushL(pSmsCenterNumber);
  2393             CleanupStack::PushL(pSmsCenterNumber);
  2624             pSmsCenterNumber->SetAddressL((pSmsServiceSettings->GetServiceCenter(pSmsServiceSettings->DefaultServiceCenter())).Address());
  2394             pSmsCenterNumber->SetAddressL((pSmsServiceSettings->GetServiceCenter(pSmsServiceSettings->DefaultServiceCenter())).Address());
  2625             smsHeader.Message().SetServiceCenterAddressL(pSmsCenterNumber->Address());
  2395             smsHeader.Message().SetServiceCenterAddressL(pSmsCenterNumber->Address());
  2638     
  2408     
  2639     if (validateSMS()) {
  2409     if (validateSMS()) {
  2640         // Switch current SMS MTM context to SMS message parent folder entry
  2410         // Switch current SMS MTM context to SMS message parent folder entry
  2641         ipSmsMtm->SwitchCurrentEntryL(ipSmsMtm->Entry().Entry().Parent());
  2411         ipSmsMtm->SwitchCurrentEntryL(ipSmsMtm->Entry().Entry().Parent());
  2642         
  2412         
  2643         CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  2413         QMTMWait mtmWait;
  2644     
  2414     
  2645         // Move SMS Message to Outbox
  2415         // Move SMS Message to Outbox
  2646         CMsvOperation* pMsvOperation = ipSmsMtm->Entry().MoveL(messageId,
  2416         CMsvOperation* pMsvOperation = ipSmsMtm->Entry().MoveL(messageId,
  2647                                                                KMsvGlobalOutBoxIndexEntryId,
  2417                                                                KMsvGlobalOutBoxIndexEntryId,
  2648                                                                pMsvOperationWait->iStatus);
  2418                                                                mtmWait.iStatus);
  2649         pMsvOperationWait->Start();
  2419         mtmWait.start();
  2650         CActiveScheduler::Start();
       
  2651         delete pMsvOperation;
  2420         delete pMsvOperation;
  2652         
  2421         
  2653         // Send SMS Message
  2422         // Send SMS Message
  2654         CMsvEntrySelection* pMsvEntrySelection = new(ELeave) CMsvEntrySelection;
  2423         CMsvEntrySelection* pMsvEntrySelection = new(ELeave) CMsvEntrySelection;
  2655         CleanupStack::PushL(pMsvEntrySelection);
  2424         CleanupStack::PushL(pMsvEntrySelection);
  2660         // Add selection (containing SMS Message Id) to task scheduler
  2429         // Add selection (containing SMS Message Id) to task scheduler
  2661         TBuf8<1> dummyParams;
  2430         TBuf8<1> dummyParams;
  2662         pMsvOperation = ipSmsMtm->InvokeAsyncFunctionL(ESmsMtmCommandScheduleCopy,
  2431         pMsvOperation = ipSmsMtm->InvokeAsyncFunctionL(ESmsMtmCommandScheduleCopy,
  2663                                                        *pMsvEntrySelection,
  2432                                                        *pMsvEntrySelection,
  2664                                                        dummyParams,
  2433                                                        dummyParams,
  2665                                                        pMsvOperationWait->iStatus);
  2434                                                        mtmWait.iStatus);
  2666         pMsvOperationWait->Start();
  2435         mtmWait.start();
  2667         CActiveScheduler::Start();
       
  2668         delete pMsvOperation;    
  2436         delete pMsvOperation;    
  2669      
  2437      
  2670         CleanupStack::PopAndDestroy(pMsvEntrySelection);    
  2438         CleanupStack::PopAndDestroy(pMsvEntrySelection);    
  2671     
       
  2672         CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  2673     } else {
  2439     } else {
  2674         User::Leave(KErrCorrupt);
  2440         User::Leave(KErrCorrupt);
  2675     }
  2441     }
  2676 }
  2442 }
  2677 
  2443 
  2687     } else {
  2453     } else {
  2688         // parentFolderId was not defined for new Message
  2454         // parentFolderId was not defined for new Message
  2689         // => Message will be created into defined standard Folder (Default value is Drafts Folder)
  2455         // => Message will be created into defined standard Folder (Default value is Drafts Folder)
  2690         destinationFolderId =  standardFolderId(message.standardFolder());
  2456         destinationFolderId =  standardFolderId(message.standardFolder());
  2691     }
  2457     }
  2692         
  2458     
  2693     CMsvOperationWait* wait = CMsvOperationWait::NewLC();
  2459     // Switch current MMS MTM context to folder entry    
  2694     wait->Start();
  2460     ipMmsMtm->SwitchCurrentEntryL(destinationFolderId);
  2695     CMsvOperation* operation = ipMmsMtm->CreateNewEntryL(destinationFolderId, wait->iStatus);
  2461 
  2696     CleanupStack::PushL(operation);
  2462     // Create a new MMS message entry as a child of the current context
  2697     CActiveScheduler::Start();
  2463     // Note: CreateMessageL switches current MMS MTM context to
  2698     if (wait->iStatus.Int() != KErrNone){ 
  2464     //       a new MMS message context
  2699     //TODO: handle error
  2465     ipMmsMtm->CreateMessageL(ipMmsMtm->DefaultServiceL());    
  2700     }
  2466 
  2701     TPckgBuf<TMsvId> pkg;
  2467     // Get the current context (new message context)
  2702     pkg.Copy( operation->ProgressL());
  2468     CMsvEntry& newMessageContext = ipMmsMtm->Entry();
  2703     TMsvId indexEntry= pkg();
  2469 
  2704     CleanupStack::PopAndDestroy(2); // operation and wait
  2470     // Copy entry values from the new message context index entry
  2705     ipMmsMtm->SwitchCurrentEntryL(indexEntry);
  2471     TMsvEntry entry = newMessageContext.Entry();
  2706     ipMmsMtm->LoadMessageL();
  2472 
  2707             
       
  2708     // Add receivers
  2473     // Add receivers
  2709     QList<QMessageAddress> list(message.to());
  2474     QList<QMessageAddress> list(message.to());
  2710     TPtrC16 receiver(KNullDesC);
  2475     TPtrC16 receiver(KNullDesC);
  2711     QString qreceiver;
  2476     QString qreceiver;
  2712     for (int i = 0; i < list.size(); ++i) {
  2477     for (int i = 0; i < list.size(); ++i) {
  2718     // Set Subject
  2483     // Set Subject
  2719     QString subject = message.subject();
  2484     QString subject = message.subject();
  2720     TPtrC16 sbj(reinterpret_cast<const TUint16*>(subject.utf16()));
  2485     TPtrC16 sbj(reinterpret_cast<const TUint16*>(subject.utf16()));
  2721     ipMmsMtm->SetSubjectL(sbj); 
  2486     ipMmsMtm->SetSubjectL(sbj); 
  2722     
  2487     
  2723     TMsvEntry ent = ipMmsMtm->Entry().Entry();       
  2488     entry.SetInPreparation(EFalse); 
  2724     ent.SetInPreparation(EFalse); 
  2489     entry.SetVisible(ETrue);
  2725     ent.SetVisible(ETrue);
       
  2726     
  2490     
  2727     switch (message.priority()) {
  2491     switch (message.priority()) {
  2728     case QMessage::HighPriority:
  2492     case QMessage::HighPriority:
  2729         ent.SetPriority(EMsvHighPriority);
  2493         entry.SetPriority(EMsvHighPriority);
  2730         break;
  2494         break;
  2731     case QMessage::NormalPriority:
  2495     case QMessage::NormalPriority:
  2732         ent.SetPriority(EMsvMediumPriority);
  2496         entry.SetPriority(EMsvMediumPriority);
  2733         break;
  2497         break;
  2734     case QMessage::LowPriority:
  2498     case QMessage::LowPriority:
  2735         ent.SetPriority(EMsvLowPriority);
  2499         entry.SetPriority(EMsvLowPriority);
  2736         break;
  2500         break;
  2737     }
  2501     }
  2738     if (message.status() & QMessage::Read) { 
  2502     if (message.status() & QMessage::Read) { 
  2739         ent.SetUnread(false);
  2503         entry.SetUnread(false);
  2740         ent.SetNew(false);
  2504         entry.SetNew(false);
  2741     } else {
  2505     } else {
  2742         ent.SetUnread(true);
  2506         entry.SetUnread(true);
  2743         ent.SetNew(true);
  2507         entry.SetNew(true);
  2744     }
  2508     }
  2745     ipMmsMtm->Entry().ChangeL(ent); 
  2509     
  2746     // Save the changes    
  2510     // Set new message's context's index entry to the specified values.
       
  2511     // <=> Changes are set into cache only
       
  2512     newMessageContext.ChangeL(entry);
       
  2513 
       
  2514     // Commit cached changes to the storage
  2747     ipMmsMtm->SaveMessageL();
  2515     ipMmsMtm->SaveMessageL();
  2748     
  2516     
  2749     CMsvStore* store = ipMmsMtm->Entry().EditStoreL();    
  2517     CMsvStore* store = ipMmsMtm->Entry().EditStoreL();    
  2750     CleanupStack::PushL(store); 
  2518     CleanupStack::PushL(store); 
  2751     
  2519     
  2863     }
  2631     }
  2864          
  2632          
  2865     CleanupStack::PopAndDestroy(); // store    
  2633     CleanupStack::PopAndDestroy(); // store    
  2866     
  2634     
  2867     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  2635     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  2868     privateMessage->_id = QMessageId(SymbianHelpers::addIdPrefix(QString::number(indexEntry,SymbianHelpers::EngineTypeMTM)));
  2636     privateMessage->_id = QMessageId(SymbianHelpers::addIdPrefix(QString::number(entry.Id()),SymbianHelpers::EngineTypeMTM));
  2869     // Save the changes
  2637     // Save the changes
  2870     ipMmsMtm->SaveMessageL();
  2638     ipMmsMtm->SaveMessageL();
  2871     
  2639     
  2872     if (!message.receivedDate().isNull() || !message.date().isNull()) {
  2640     if (!message.receivedDate().isNull() || !message.date().isNull()) {
  2873         // Change the date to given date
  2641         // Change the date to given date
  2874         CMsvEntry* pEntry = ipMsvSession->GetEntryL(indexEntry);
  2642         CMsvEntry* pEntry = ipMsvSession->GetEntryL(entry.Id());
  2875         CleanupStack::PushL(pEntry);
  2643         CleanupStack::PushL(pEntry);
  2876         TMsvEntry changedEntry = pEntry->Entry();
  2644         TMsvEntry changedEntry = pEntry->Entry();
  2877         if (!message.date().isNull()) {
  2645         if (!message.date().isNull()) {
  2878             changedEntry.iDate = qDateTimeToSymbianTTime(message.date());
  2646             changedEntry.iDate = qDateTimeToSymbianTTime(message.date());
  2879         } else {
  2647         } else {
  3216     msvEntry.iDetails.Set(TPtrC(reinterpret_cast<const TUint16*>(message.from().addressee().utf16())));
  2984     msvEntry.iDetails.Set(TPtrC(reinterpret_cast<const TUint16*>(message.from().addressee().utf16())));
  3217     
  2985     
  3218     if (mtmUid == KUidMsgTypeSMTP) {
  2986     if (mtmUid == KUidMsgTypeSMTP) {
  3219         ipSmtpMtm->Entry().ChangeL(msvEntry);
  2987         ipSmtpMtm->Entry().ChangeL(msvEntry);
  3220     } else {
  2988     } else {
  3221         CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  2989         QMTMWait mtmWait;
  3222         CMsvOperation* pMsvOperation = NULL;
  2990         CMsvOperation* pMsvOperation = NULL;
  3223         if (mtmUid == KUidMsgTypePOP3) {
  2991         if (mtmUid == KUidMsgTypePOP3) {
  3224             pMsvOperation = ipPop3Mtm->Entry().ChangeL(msvEntry, pMsvOperationWait->iStatus);
  2992             pMsvOperation = ipPop3Mtm->Entry().ChangeL(msvEntry, mtmWait.iStatus);
  3225         } else {
  2993         } else {
  3226             pMsvOperation = ipImap4Mtm->Entry().ChangeL(msvEntry, pMsvOperationWait->iStatus);
  2994             pMsvOperation = ipImap4Mtm->Entry().ChangeL(msvEntry, mtmWait.iStatus);
  3227         }
  2995         }
  3228         pMsvOperationWait->Start();
  2996         mtmWait.start();
  3229         CActiveScheduler::Start();
       
  3230         delete pMsvOperation;
  2997         delete pMsvOperation;
  3231         CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  3232     }
  2998     }
  3233     
  2999     
  3234     // Save the changes    
  3000     // Save the changes    
  3235     ipSmtpMtm->SaveMessageL();
  3001     ipSmtpMtm->SaveMessageL();
  3236     
  3002     
  3237     CMsvEntry* entry = ipMsvSession->GetEntryL(messageId);
  3003     CMsvEntry* entry = ipMsvSession->GetEntryL(messageId);
  3238     CleanupStack::PushL(entry);
  3004     CleanupStack::PushL(entry);
  3239     
  3005     
  3240     CImEmailMessage* mailMsg = CImEmailMessage::NewLC(*entry); 
  3006     CImEmailMessage* mailMsg = CImEmailMessage::NewLC(*entry); 
  3241     
       
  3242     CMsvOperationWait* waiter = CMsvOperationWait::NewLC();
       
  3243     
  3007     
  3244     CImMimeHeader* mime = CImMimeHeader::NewLC(); 
  3008     CImMimeHeader* mime = CImMimeHeader::NewLC(); 
  3245     
  3009     
  3246     QByteArray filePath;
  3010     QByteArray filePath;
  3247     QMessageContentContainerIdList contentIds = message.contentIds();
  3011     QMessageContentContainerIdList contentIds = message.contentIds();
  3267             int count = filePath.count();
  3031             int count = filePath.count();
  3268             QByteArray name = filePath.right(count-last-1);
  3032             QByteArray name = filePath.right(count-last-1);
  3269             QString fileName = QString(name);
  3033             QString fileName = QString(name);
  3270             CleanupStack::PushL(attachmentInfo);  
  3034             CleanupStack::PushL(attachmentInfo);  
  3271             attachmentInfo->SetAttachmentNameL(TPtrC(reinterpret_cast<const TUint16*>(fileName.utf16())));
  3035             attachmentInfo->SetAttachmentNameL(TPtrC(reinterpret_cast<const TUint16*>(fileName.utf16())));
  3272             mailMsg->AttachmentManager().AddAttachmentL(attachment, attachmentInfo, waiter->iStatus);
  3036             QMTMWait mtmWait;
  3273             waiter->Start();
  3037             mailMsg->AttachmentManager().AddAttachmentL(attachment, attachmentInfo, mtmWait.iStatus);
  3274             CActiveScheduler::Start();
  3038             mtmWait.start();
  3275             CleanupStack::Pop(attachmentInfo);
  3039             CleanupStack::Pop(attachmentInfo);
  3276             CleanupStack::Pop(&attachment); // close file
  3040             CleanupStack::Pop(&attachment); // close file
  3277         } else if (pPrivateContainer->_id == message.bodyId()) { // content is body text
  3041         } else if (pPrivateContainer->_id == message.bodyId()) { // content is body text
  3278             CParaFormatLayer* paragraphFormatLayer = CParaFormatLayer::NewL();
  3042             CParaFormatLayer* paragraphFormatLayer = CParaFormatLayer::NewL();
  3279             CleanupStack::PushL(paragraphFormatLayer);
  3043             CleanupStack::PushL(paragraphFormatLayer);
  3300             if (charset == "UTF-16") {
  3064             if (charset == "UTF-16") {
  3301                 mime->SetMimeCharset(KCharacterSetIdentifierUcs2);
  3065                 mime->SetMimeCharset(KCharacterSetIdentifierUcs2);
  3302             }
  3066             }
  3303             // Insert the contents of a buffer into the document at specified position
  3067             // Insert the contents of a buffer into the document at specified position
  3304             bodyText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(container.textContent().utf16())));
  3068             bodyText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(container.textContent().utf16())));
  3305             mailMsg->StoreBodyTextWithMimeHeaderL(messageId, *bodyText, *mime, waiter->iStatus);
  3069             QMTMWait mtmWait;
  3306             
  3070             mailMsg->StoreBodyTextWithMimeHeaderL(messageId, *bodyText, *mime, mtmWait.iStatus);
  3307             waiter->Start();
  3071             mtmWait.start();
  3308             CActiveScheduler::Start();
       
  3309                     
  3072                     
  3310             CleanupStack::PopAndDestroy(3); // bodyText, characterFormatLayer, paragraphFormatLayer
  3073             CleanupStack::PopAndDestroy(3); // bodyText, characterFormatLayer, paragraphFormatLayer
  3311         }
  3074         }
  3312         
  3075         
  3313     }  
  3076     }  
  3340             mime->SetMimeCharset(KCharacterSetIdentifierUcs2);
  3103             mime->SetMimeCharset(KCharacterSetIdentifierUcs2);
  3341         }
  3104         }
  3342         
  3105         
  3343         // Insert the contents of a buffer into the document at specified position
  3106         // Insert the contents of a buffer into the document at specified position
  3344         bodyText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(message.textContent().utf16())));
  3107         bodyText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(message.textContent().utf16())));
  3345         mailMsg->StoreBodyTextWithMimeHeaderL(messageId, *bodyText, *mime, waiter->iStatus);
  3108         QMTMWait mtmWait;
  3346         waiter->Start();
  3109         mailMsg->StoreBodyTextWithMimeHeaderL(messageId, *bodyText, *mime, mtmWait.iStatus);
  3347         CActiveScheduler::Start();
  3110         mtmWait.start();
  3348         CleanupStack::PopAndDestroy(3);
  3111         CleanupStack::PopAndDestroy(3);
  3349         //bodyText, characterFormatLayer, paragraphFormatLayer,
  3112         //bodyText, characterFormatLayer, paragraphFormatLayer,
  3350     }
  3113     }
  3351 
  3114 
  3352     CMsvStore* store = entry->EditStoreL();
  3115     CMsvStore* store = entry->EditStoreL();
  3391      
  3154      
  3392     emailEntry->StoreL(*store);
  3155     emailEntry->StoreL(*store);
  3393     // Store the changes permanently
  3156     // Store the changes permanently
  3394     store->CommitL();
  3157     store->CommitL();
  3395     
  3158     
  3396     CleanupStack::PopAndDestroy(6, entry); 
  3159     CleanupStack::PopAndDestroy(5, entry); 
  3397     // mailMsg, emailEntry, store, waiter, entry, mime   
  3160     // mailMsg, emailEntry, store, waiter, entry, mime   
  3398 
  3161 
  3399     if (!message.receivedDate().isNull() || !message.date().isNull()) {
  3162     if (!message.receivedDate().isNull() || !message.date().isNull()) {
  3400         // Change the date to given date
  3163         // Change the date to given date
  3401         CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  3164         CMsvEntry* pEntry = ipMsvSession->GetEntryL(messageId);
  3407             changedEntry.iDate = qDateTimeToSymbianTTime(message.receivedDate());
  3170             changedEntry.iDate = qDateTimeToSymbianTTime(message.receivedDate());
  3408         }
  3171         }
  3409         if (mtmUid == KUidMsgTypeSMTP) {
  3172         if (mtmUid == KUidMsgTypeSMTP) {
  3410             ipSmtpMtm->Entry().ChangeL(changedEntry);
  3173             ipSmtpMtm->Entry().ChangeL(changedEntry);
  3411         } else {
  3174         } else {
  3412             CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  3175             QMTMWait mtmWait;
  3413             CMsvOperation* pMsvOperation = NULL;
  3176             CMsvOperation* pMsvOperation = NULL;
  3414             if (mtmUid == KUidMsgTypePOP3) {
  3177             if (mtmUid == KUidMsgTypePOP3) {
  3415                 pMsvOperation = ipPop3Mtm->Entry().ChangeL(changedEntry, pMsvOperationWait->iStatus);
  3178                 pMsvOperation = ipPop3Mtm->Entry().ChangeL(changedEntry, mtmWait.iStatus);
  3416             } else {
  3179             } else {
  3417                 pMsvOperation = ipImap4Mtm->Entry().ChangeL(changedEntry, pMsvOperationWait->iStatus);
  3180                 pMsvOperation = ipImap4Mtm->Entry().ChangeL(changedEntry, mtmWait.iStatus);
  3418             }
  3181             }
  3419             pMsvOperationWait->Start();
  3182             mtmWait.start();
  3420             CActiveScheduler::Start();
       
  3421             delete pMsvOperation;
  3183             delete pMsvOperation;
  3422             CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  3423         }
  3184         }
  3424         CleanupStack::PopAndDestroy(pEntry);
  3185         CleanupStack::PopAndDestroy(pEntry);
  3425     }
  3186     }
  3426 }
  3187 }
  3427 
  3188 
  3442     if (messageId == 0) {
  3203     if (messageId == 0) {
  3443         User::Leave(KErrNotReady);
  3204         User::Leave(KErrNotReady);
  3444     }
  3205     }
  3445 
  3206 
  3446     CMsvEntry* pMsvEntry = retrieveCMsvEntryAndPushToCleanupStack(messageId);
  3207     CMsvEntry* pMsvEntry = retrieveCMsvEntryAndPushToCleanupStack(messageId);
  3447     CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  3208 
       
  3209     QMTMWait mtmWait;
  3448     
  3210     
  3449     CMsvOperation* pMsvOperation = NULL;
  3211     CMsvOperation* pMsvOperation = NULL;
  3450     if (!messageCreated) {
  3212     if (!messageCreated) {
  3451         ipMmsMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3213         ipMmsMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3452         pMsvOperation = ipMmsMtm->Entry().CopyL(messageId, KMsvGlobalOutBoxIndexEntryId, pMsvOperationWait->iStatus);
  3214         pMsvOperation = ipMmsMtm->Entry().CopyL(messageId, KMsvGlobalOutBoxIndexEntryId, mtmWait.iStatus);
  3453         pMsvOperationWait->Start();
  3215         mtmWait.start();
  3454         CActiveScheduler::Start();
       
  3455         delete pMsvOperation;
  3216         delete pMsvOperation;
  3456         pMsvOperation = NULL;
  3217         pMsvOperation = NULL;
  3457     }
  3218     }
  3458     
  3219     
  3459     // Start sending the message via the Server MTM to the MMS server    
  3220     // Start sending the message via the Server MTM to the MMS server    
  3460     CMsvEntrySelection* pMsvEntrySelection = new(ELeave) CMsvEntrySelection;
  3221     CMsvEntrySelection* pMsvEntrySelection = new(ELeave) CMsvEntrySelection;
  3461     pMsvEntrySelection->AppendL(messageId);
  3222     pMsvEntrySelection->AppendL(messageId);
  3462     ipMmsMtm->SwitchCurrentEntryL(KMsvGlobalOutBoxIndexEntryId);
  3223     ipMmsMtm->SwitchCurrentEntryL(KMsvGlobalOutBoxIndexEntryId);
  3463     pMsvOperation = ipMmsMtm->SendL(*pMsvEntrySelection, pMsvOperationWait->iStatus);
  3224     pMsvOperation = ipMmsMtm->SendL(*pMsvEntrySelection, mtmWait.iStatus);
  3464     pMsvOperationWait->Start();
  3225     mtmWait.start();
  3465     CActiveScheduler::Start();
  3226     
  3466     delete pMsvOperation;
  3227     delete pMsvOperation;
  3467    
       
  3468     while (pMsvOperationWait->iStatus == KRequestPending) {
       
  3469          CActiveScheduler::Start();
       
  3470     }
       
  3471     
       
  3472     CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  3473     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);    
  3228     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);    
  3474 }
  3229 }
  3475 
  3230 
  3476 QString CMTMEngine::privateFolderPath()
  3231 QString CMTMEngine::privateFolderPath()
  3477 {
  3232 {
  3519         // => Message will be created into defined standard Folder (Default value is Drafts Folder)
  3274         // => Message will be created into defined standard Folder (Default value is Drafts Folder)
  3520         destinationFolderId =  standardFolderId(message.standardFolder());
  3275         destinationFolderId =  standardFolderId(message.standardFolder());
  3521     }
  3276     }
  3522     
  3277     
  3523     QMessageAccount messageAccount = this->account(message.parentAccountId());
  3278     QMessageAccount messageAccount = this->account(message.parentAccountId());
  3524     
  3279 
  3525     CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  3280     QMTMWait mtmWait;
       
  3281     
  3526     TMsvEmailTypeList msvEmailTypeList = 0;
  3282     TMsvEmailTypeList msvEmailTypeList = 0;
  3527     TMsvPartList msvPartList = 0;
  3283     TMsvPartList msvPartList = 0;
  3528     if (message.status() & QMessage::HasAttachments == QMessage::HasAttachments) {
  3284     if (message.status() & QMessage::HasAttachments == QMessage::HasAttachments) {
  3529         msvPartList = (KMsvMessagePartBody | KMsvMessagePartAttachments);
  3285         msvPartList = (KMsvMessagePartBody | KMsvMessagePartAttachments);
  3530     } else {
  3286     } else {
  3531         msvPartList = KMsvMessagePartBody;
  3287         msvPartList = KMsvMessagePartBody;
  3532     }
  3288     }
  3533     CImEmailOperation* pImEmailOperation = CImEmailOperation::CreateNewL(pMsvOperationWait->iStatus, *ipMsvSession, destinationFolderId,
  3289     CImEmailOperation* pImEmailOperation = CImEmailOperation::CreateNewL(mtmWait.iStatus, *ipMsvSession, destinationFolderId,
  3534                                                                          messageAccount.d_ptr->_service2EntryId, msvPartList,
  3290                                                                          messageAccount.d_ptr->_service2EntryId, msvPartList,
  3535                                                                          msvEmailTypeList, KUidMsgTypeSMTP);
  3291                                                                          msvEmailTypeList, KUidMsgTypeSMTP);
  3536     CleanupStack::PushL(pImEmailOperation);
  3292     CleanupStack::PushL(pImEmailOperation);
  3537     pMsvOperationWait->Start();
  3293     mtmWait.start();
  3538     CActiveScheduler::Start();
       
  3539     
  3294     
  3540     TMsvId newMessageId;
  3295     TMsvId newMessageId;
  3541     TPckgC<TMsvId> paramPack(newMessageId);
  3296     TPckgC<TMsvId> paramPack(newMessageId);
  3542     paramPack.Set(pImEmailOperation->ProgressL());
  3297     paramPack.Set(pImEmailOperation->ProgressL());
  3543     newMessageId = paramPack();
  3298     newMessageId = paramPack();
  3604     
  3359     
  3605             // Insert the contents of a buffer into the document at specified position
  3360             // Insert the contents of a buffer into the document at specified position
  3606             pBodyRichText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(message.textContent().utf16())));
  3361             pBodyRichText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(message.textContent().utf16())));
  3607             // Note: Email message MIME header is same as Body MIME header
  3362             // Note: Email message MIME header is same as Body MIME header
  3608             pImEmailMessage->StoreBodyTextWithMimeHeaderL(newMessageId, *pBodyRichText, *pImMimeHeader,
  3363             pImEmailMessage->StoreBodyTextWithMimeHeaderL(newMessageId, *pBodyRichText, *pImMimeHeader,
  3609                                                           pMsvOperationWait->iStatus);
  3364                                                           mtmWait.iStatus);
  3610             pMsvOperationWait->Start();
  3365             mtmWait.start();
  3611             CActiveScheduler::Start();
       
  3612             
  3366             
  3613             CleanupStack::PopAndDestroy(pBodyRichText);
  3367             CleanupStack::PopAndDestroy(pBodyRichText);
  3614             CleanupStack::PopAndDestroy(pCharFormatLayer);
  3368             CleanupStack::PopAndDestroy(pCharFormatLayer);
  3615             CleanupStack::PopAndDestroy(pParaFormatLayer);
  3369             CleanupStack::PopAndDestroy(pParaFormatLayer);
  3616         }
  3370         }
  3649                     CleanupStack::PushL(pBodyRichText);
  3403                     CleanupStack::PushL(pBodyRichText);
  3650                     // Insert the contents of a buffer into the document at specified position
  3404                     // Insert the contents of a buffer into the document at specified position
  3651                     pBodyRichText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(container.textContent().utf16())));
  3405                     pBodyRichText->InsertL(0, TPtrC(reinterpret_cast<const TUint16*>(container.textContent().utf16())));
  3652             
  3406             
  3653                     // Store MIME Header and Body text to message
  3407                     // Store MIME Header and Body text to message
  3654                     pImEmailMessage->StoreBodyTextWithMimeHeaderL(newMessageId, *pBodyRichText, *pBodyImMimeHeader, pMsvOperationWait->iStatus);
  3408                     pImEmailMessage->StoreBodyTextWithMimeHeaderL(newMessageId, *pBodyRichText,
  3655                     pMsvOperationWait->Start();
  3409                                                                   *pBodyImMimeHeader,
  3656                     CActiveScheduler::Start();
  3410                                                                   mtmWait.iStatus);
       
  3411                     mtmWait.start();
  3657                     
  3412                     
  3658                     CleanupStack::PopAndDestroy(pBodyRichText);
  3413                     CleanupStack::PopAndDestroy(pBodyRichText);
  3659                     CleanupStack::PopAndDestroy(pCharFormatLayer);
  3414                     CleanupStack::PopAndDestroy(pCharFormatLayer);
  3660                     CleanupStack::PopAndDestroy(pParaFormatLayer);
  3415                     CleanupStack::PopAndDestroy(pParaFormatLayer);
  3661                     CleanupStack::PopAndDestroy(pBodyImMimeHeader);
  3416                     CleanupStack::PopAndDestroy(pBodyImMimeHeader);
  3688                 QByteArray name = filePath.right(count-last-1);
  3443                 QByteArray name = filePath.right(count-last-1);
  3689                 QString fileName = QString(name);
  3444                 QString fileName = QString(name);
  3690                 CleanupStack::PushL(pMsvAttachment);  
  3445                 CleanupStack::PushL(pMsvAttachment);  
  3691                 pMsvAttachment->SetAttachmentNameL(TPtrC(reinterpret_cast<const TUint16*>(fileName.utf16())));
  3446                 pMsvAttachment->SetAttachmentNameL(TPtrC(reinterpret_cast<const TUint16*>(fileName.utf16())));
  3692                 // Note: Following call transfers ownership of attachmentFile and pMsvAttachment to AttachmentManager
  3447                 // Note: Following call transfers ownership of attachmentFile and pMsvAttachment to AttachmentManager
  3693                 pImEmailMessage->AttachmentManager().AddAttachmentL(attachmentFile, pMsvAttachment, pMsvOperationWait->iStatus);
  3448                 pImEmailMessage->AttachmentManager().AddAttachmentL(attachmentFile, pMsvAttachment,
  3694                 pMsvOperationWait->Start();
  3449                                                                     mtmWait.iStatus);
  3695                 CActiveScheduler::Start();
  3450                 mtmWait.start();
       
  3451 
  3696                 CleanupStack::Pop(pMsvAttachment); // Pop attachment from CleanupStack
  3452                 CleanupStack::Pop(pMsvAttachment); // Pop attachment from CleanupStack
  3697                 CleanupStack::Pop(&attachmentFile); // Pop file from CleanupStack
  3453                 CleanupStack::Pop(&attachmentFile); // Pop file from CleanupStack
  3698             }        
  3454             }        
  3699         }
  3455         }
  3700     }
  3456     }
  3768     
  3524     
  3769     if (imapDestinationFolderId != 0) {
  3525     if (imapDestinationFolderId != 0) {
  3770         TMsvId parent = pMsvEntry->Entry().Parent();
  3526         TMsvId parent = pMsvEntry->Entry().Parent();
  3771         ipImap4Mtm->SwitchCurrentEntryL(parent);        
  3527         ipImap4Mtm->SwitchCurrentEntryL(parent);        
  3772         CMsvOperation* pMsvOperation = ipImap4Mtm->Entry().MoveL(newMessageId, imapDestinationFolderId,
  3528         CMsvOperation* pMsvOperation = ipImap4Mtm->Entry().MoveL(newMessageId, imapDestinationFolderId,
  3773                                                                  pMsvOperationWait->iStatus);
  3529                                                                  mtmWait.iStatus);
  3774         CleanupStack::PushL(pMsvOperation);
  3530         mtmWait.start();
  3775         pMsvOperationWait->Start();
  3531         delete pMsvOperation;
  3776         CActiveScheduler::Start();
       
  3777         CleanupStack::PopAndDestroy(pMsvOperation);
       
  3778     }
  3532     }
  3779     
  3533     
  3780     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);
  3534     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);
  3781 
  3535 
  3782     CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  3783     
       
  3784     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  3536     QMessagePrivate* privateMessage = QMessagePrivate::implementation(message);
  3785     privateMessage->_id = QMessageId(SymbianHelpers::addIdPrefix(QString::number(newMessageId),SymbianHelpers::EngineTypeMTM));
  3537     privateMessage->_id = QMessageId(SymbianHelpers::addIdPrefix(QString::number(newMessageId),SymbianHelpers::EngineTypeMTM));
  3786 }
  3538 }
  3787 
  3539 
  3788 void CMTMEngine::sendEmailL(QMessage &message)
  3540 void CMTMEngine::sendEmailL(QMessage &message)
  3815     if (messageId == 0) {
  3567     if (messageId == 0) {
  3816         User::Leave(KErrNotReady);
  3568         User::Leave(KErrNotReady);
  3817     }    
  3569     }    
  3818 
  3570 
  3819     CMsvEntry* pMsvEntry = retrieveCMsvEntryAndPushToCleanupStack(messageId);
  3571     CMsvEntry* pMsvEntry = retrieveCMsvEntryAndPushToCleanupStack(messageId);
  3820     CMsvOperationWait* pMsvOperationWait = CMsvOperationWait::NewLC();
  3572     
       
  3573     QMTMWait mtmWait;
  3821     
  3574     
  3822     CMsvOperation* pMsvOperation = NULL;
  3575     CMsvOperation* pMsvOperation = NULL;
  3823     if (!messageCreated) {
  3576     if (!messageCreated) {
  3824         // Sending (old) message that's in message store
  3577         // Sending (old) message that's in message store
  3825         // => Copy message from its original location to Outbox folder
  3578         // => Copy message from its original location to Outbox folder
  3826         ipSmtpMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3579         ipSmtpMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3827         pMsvOperation = ipSmtpMtm->Entry().CopyL(messageId, KMsvGlobalOutBoxIndexEntryId, pMsvOperationWait->iStatus);
  3580         pMsvOperation = ipSmtpMtm->Entry().CopyL(messageId,
  3828         pMsvOperationWait->Start();
  3581                                                  KMsvGlobalOutBoxIndexEntryId,
  3829         CActiveScheduler::Start();
  3582                                                  mtmWait.iStatus);
       
  3583         mtmWait.start();
  3830         delete pMsvOperation;
  3584         delete pMsvOperation;
  3831         pMsvOperation = NULL;
  3585         pMsvOperation = NULL;
  3832     }    
  3586     }    
  3833     
  3587     
  3834     ipSmtpMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3588     ipSmtpMtm->SwitchCurrentEntryL(pMsvEntry->Entry().Parent());
  3835     // Following sends Email and _moves_ Email from Outbox Folder to Sent Folder
  3589     // Following sends Email and _moves_ Email from Outbox Folder to Sent Folder
  3836     pMsvOperation = ipSmtpMtm->Entry().CopyL(messageId, messageAccount.d_ptr->_service2EntryId, pMsvOperationWait->iStatus);
  3590     pMsvOperation = ipSmtpMtm->Entry().CopyL(messageId, messageAccount.d_ptr->_service2EntryId, mtmWait.iStatus);
  3837     pMsvOperationWait->Start();
  3591     mtmWait.start();
  3838     CActiveScheduler::Start();
       
  3839     delete pMsvOperation;    
  3592     delete pMsvOperation;    
  3840     
  3593     
  3841     CleanupStack::PopAndDestroy(pMsvOperationWait);
       
  3842     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);   
  3594     releaseCMsvEntryAndPopFromCleanupStack(pMsvEntry);   
  3843 }
  3595 }
  3844 
  3596 
  3845 QDateTime CMTMEngine::symbianTTimetoQDateTime(const TTime& time) const
  3597 QDateTime CMTMEngine::symbianTTimetoQDateTime(const TTime& time) const
  3846 {
  3598 {
  4552         if (filter.isEmpty()) {
  4304         if (filter.isEmpty()) {
  4553             // Empty filter matches to all messages
  4305             // Empty filter matches to all messages
  4554             matchingFilters.insert(it.key());
  4306             matchingFilters.insert(it.key());
  4555         } else {
  4307         } else {
  4556             QMessageFilterPrivate* privateMessageFilter = QMessageFilterPrivate::implementation(filter);
  4308             QMessageFilterPrivate* privateMessageFilter = QMessageFilterPrivate::implementation(filter);
  4557             if (privateMessageFilter->_field == QMessageFilterPrivate::Type) {
  4309             if (privateMessageFilter->_field == QMessageFilterPrivate::Type && aEvent != EMsvEntriesCreated) {
  4558                 if (aMsgType == KUidMsgTypeSMS) {
  4310                 if (aMsgType == KUidMsgTypeSMS) {
  4559                     message.setType(QMessage::Sms);
  4311                     message.setType(QMessage::Sms);
  4560                 } else if (aMsgType == KUidMsgTypeMultimedia) {
  4312                 } else if (aMsgType == KUidMsgTypeMultimedia) {
  4561                     message.setType(QMessage::Mms);
  4313                     message.setType(QMessage::Mms);
  4562                 } else if (aMsgType == KUidMsgTypeSMTP ||
  4314                 } else if (aMsgType == KUidMsgTypeSMTP ||
  4564                            aMsgType == KUidMsgTypeIMAP4) {
  4316                            aMsgType == KUidMsgTypeIMAP4) {
  4565                     message.setType(QMessage::Email);
  4317                     message.setType(QMessage::Email);
  4566                 } else {
  4318                 } else {
  4567                     message.setType(QMessage::NoType);
  4319                     message.setType(QMessage::NoType);
  4568                 }
  4320                 }
  4569             } else if ((privateMessageFilter->_field == QMessageFilterPrivate::StandardFolder) &&
  4321             } else if ((privateMessageFilter->_field == QMessageFilterPrivate::StandardFolder && aEvent != EMsvEntriesCreated) &&
  4570                        (aMsgType == KUidMsgTypeSMS || aMsgType == KUidMsgTypeMultimedia)) {
  4322                        (aMsgType == KUidMsgTypeSMS || aMsgType == KUidMsgTypeMultimedia)) {
  4571                 if (aFolderId == KMsvGlobalInBoxIndexEntryId) {
  4323                 if (aFolderId == KMsvGlobalInBoxIndexEntryId) {
  4572                     QMessagePrivate::setStandardFolder(message,QMessage::InboxFolder);
  4324                     QMessagePrivate::setStandardFolder(message,QMessage::InboxFolder);
  4573                 } else if (aFolderId == KMsvDraftEntryId) {
  4325                 } else if (aFolderId == KMsvDraftEntryId) {
  4574                     QMessagePrivate::setStandardFolder(message,QMessage::DraftsFolder);
  4326                     QMessagePrivate::setStandardFolder(message,QMessage::DraftsFolder);
  4767         break;
  4519         break;
  4768     
  4520     
  4769     default:
  4521     default:
  4770         break;
  4522         break;
  4771     }
  4523     }
       
  4524 }
       
  4525 
       
  4526 CAsynchronousMTMOperation* CMTMEngine::createAsynchronousMTMOperation(QMessageServicePrivate& privateService,
       
  4527                                                                       CBaseMtm* mtm,
       
  4528                                                                       TMsvId serviceId)
       
  4529 {
       
  4530     TInt operationId = ++iOperationIds;
       
  4531     CAsynchronousMTMOperation* op = new CAsynchronousMTMOperation((CMTMEngine&)*this,
       
  4532                                                                    privateService,
       
  4533                                                                    mtm,
       
  4534                                                                    serviceId,
       
  4535                                                                    operationId);
       
  4536     return op;
       
  4537 }
       
  4538 
       
  4539 void CMTMEngine::deleteAsynchronousMTMOperation(CAsynchronousMTMOperation *apOperation)
       
  4540 {
       
  4541     delete apOperation;
  4772 }
  4542 }
  4773 
  4543 
  4774 CMessagesFindOperation::CMessagesFindOperation(CMTMEngine& aOwner, CMsvSession* apMsvSession, int aOperationId)
  4544 CMessagesFindOperation::CMessagesFindOperation(CMTMEngine& aOwner, CMsvSession* apMsvSession, int aOperationId)
  4775     : CActive(CActive::EPriorityStandard), iOwner(aOwner), ipMsvSession(apMsvSession), iOperationId(aOperationId),
  4545     : CActive(CActive::EPriorityStandard), iOwner(aOwner), ipMsvSession(apMsvSession), iOperationId(aOperationId),
  4776         iResultCorrectlyOrdered(false)
  4546         iResultCorrectlyOrdered(false)
  5655         }
  5425         }
  5656         iOwner.releaseCMsvEntryAndPopFromCleanupStack(pEntry);
  5426         iOwner.releaseCMsvEntryAndPopFromCleanupStack(pEntry);
  5657     }
  5427     }
  5658 }
  5428 }
  5659 
  5429 
       
  5430 QMTMWait::QMTMWait(TInt priority)
       
  5431     : CActive(priority)
       
  5432 {
       
  5433     CActiveScheduler::Add(this);
       
  5434 }
       
  5435 
       
  5436 QMTMWait::~QMTMWait()
       
  5437 {
       
  5438     Cancel();
       
  5439 }
       
  5440 
       
  5441 void QMTMWait::start()
       
  5442 {
       
  5443     SetActive();
       
  5444     m_eventLoop.exec();
       
  5445 }
       
  5446 
       
  5447 void QMTMWait::RunL()
       
  5448 {
       
  5449     m_eventLoop.quit();
       
  5450 }
       
  5451 
       
  5452 void QMTMWait::DoCancel()
       
  5453 {
       
  5454     Cancel();
       
  5455 }
       
  5456 
       
  5457 CAsynchronousMTMOperation::CAsynchronousMTMOperation(CMTMEngine& aParent,
       
  5458                                                      QMessageServicePrivate& aPrivateService,
       
  5459                                                      CBaseMtm* apMTM,
       
  5460                                                      TMsvId aServiceId,
       
  5461                                                      TInt aOperationId)
       
  5462     : CActive(EPriorityStandard),
       
  5463       ipParent(&aParent),
       
  5464       iOperationId(aOperationId),
       
  5465       iServiceId(aServiceId),
       
  5466       ipPrivateService(&aPrivateService),
       
  5467       ipMTM(apMTM)
       
  5468 {
       
  5469     CActiveScheduler::Add(this);
       
  5470 }
       
  5471 
       
  5472 CAsynchronousMTMOperation::~CAsynchronousMTMOperation()
       
  5473 {
       
  5474     ipParent = NULL;
       
  5475     Cancel();
       
  5476 }
       
  5477 
       
  5478 bool CAsynchronousMTMOperation::retrieveMessageHeader(TMsvId aMessageId)
       
  5479 {
       
  5480     if (!isActive) {
       
  5481         isActive = true;
       
  5482         iOperation = MTMOperationRetrieveMessageHeader;
       
  5483         iOperationStep = MTMOperationStepConnect;
       
  5484         
       
  5485         iMessageId = aMessageId;
       
  5486         
       
  5487         TRAPD(err, RunL());
       
  5488         if (err == KErrNone) {
       
  5489             return true;
       
  5490         }
       
  5491     }
       
  5492     return false;
       
  5493 }
       
  5494 
       
  5495 bool CAsynchronousMTMOperation::retrieveMessageBody(TMsvId aMessageId)
       
  5496 {
       
  5497     if (!isActive) {
       
  5498         isActive = true;
       
  5499         iOperation = MTMOperationRetrieveMessageBody;
       
  5500         iOperationStep = MTMOperationStepConnect;
       
  5501         
       
  5502         iMessageId = aMessageId;
       
  5503         
       
  5504         TRAPD(err, RunL());
       
  5505         if (err == KErrNone) {
       
  5506             return true;
       
  5507         }
       
  5508     }
       
  5509     return false;
       
  5510 }
       
  5511 
       
  5512 bool CAsynchronousMTMOperation::retrieveMessageAttachments(TMsvId aMessageId)
       
  5513 {
       
  5514     if (!isActive) {
       
  5515         isActive = true;
       
  5516         iOperation = MTMOperationRetrieveMessageAttachments;
       
  5517         iOperationStep = MTMOperationStepConnect;
       
  5518         
       
  5519         iMessageId = aMessageId;
       
  5520         
       
  5521         TRAPD(err, RunL());
       
  5522         if (err == KErrNone) {
       
  5523             return true;
       
  5524         }
       
  5525     }
       
  5526     return false;
       
  5527 }
       
  5528 
       
  5529 bool CAsynchronousMTMOperation::doFullSync()
       
  5530 {
       
  5531     if (!isActive) {
       
  5532         isActive = true;
       
  5533         iOperation = MTMOperationFullSync;
       
  5534         iOperationStep = MTMOperationStepConnect;
       
  5535         TRAPD(err, RunL());
       
  5536         if (err == KErrNone) {
       
  5537             return true;
       
  5538         }
       
  5539     }
       
  5540     return false;
       
  5541 }
       
  5542 
       
  5543 void CAsynchronousMTMOperation::RunL()
       
  5544 {
       
  5545     if (iStatus.Int() != KErrNone && iOperationStep != MTMOperationStepDisconnect) {
       
  5546         delete ipMsvEntrySelection;
       
  5547         ipMsvEntrySelection = NULL;
       
  5548         delete ipMsvOperation;
       
  5549         ipMsvOperation = NULL;
       
  5550     
       
  5551         isActive = false;
       
  5552         ipPrivateService->setFinished(false);
       
  5553 
       
  5554         if (ipParent) {
       
  5555             ipParent->deleteAsynchronousMTMOperation(this);
       
  5556         }
       
  5557         return;
       
  5558     }
       
  5559 
       
  5560     ipMTM->SwitchCurrentEntryL(iServiceId);
       
  5561     
       
  5562     switch(iOperationStep) {
       
  5563     case CAsynchronousMTMOperation::MTMOperationStepConnect:
       
  5564         {
       
  5565         TPckgBuf<TInt> parameter;
       
  5566         ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5567         ipMsvEntrySelection->AppendL(iServiceId);
       
  5568         
       
  5569         if (ipMTM->Type() == KUidMsgTypeIMAP4) {
       
  5570             ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMConnect,
       
  5571                                                          *ipMsvEntrySelection,
       
  5572                                                          parameter,
       
  5573                                                          iStatus);
       
  5574         } else if (ipMTM->Type() == KUidMsgTypePOP3) {
       
  5575             ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KPOP3MTMConnect,
       
  5576                                                          *ipMsvEntrySelection,
       
  5577                                                          parameter,
       
  5578                                                          iStatus);
       
  5579         }
       
  5580         iOperationStep = MTMOperationStepDoOperation;
       
  5581         SetActive();
       
  5582         }
       
  5583         break;
       
  5584     case CAsynchronousMTMOperation::MTMOperationStepDoOperation:
       
  5585         {
       
  5586         delete ipMsvEntrySelection;
       
  5587         ipMsvEntrySelection = NULL;
       
  5588         delete ipMsvOperation;
       
  5589         ipMsvOperation = NULL;
       
  5590         
       
  5591         if (iOperation == MTMOperationRetrieveMessageHeader) {
       
  5592             // Retrieve header
       
  5593             ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5594             
       
  5595             if (ipMTM->Type() == KUidMsgTypeIMAP4) {
       
  5596                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5597                 TImImap4GetMailInfo info;
       
  5598                 info.iMaxEmailSize = KMaxTInt;
       
  5599                 info.iGetMailBodyParts = EGetImap4EmailHeaders;
       
  5600                 TPckg<TImImap4GetMailInfo> bodyInfo(info);
       
  5601                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
       
  5602                                                              *ipMsvEntrySelection,
       
  5603                                                              bodyInfo,
       
  5604                                                              iStatus);
       
  5605             } else if (ipMTM->Type() == KUidMsgTypePOP3) {
       
  5606                 ipMsvEntrySelection->AppendL(iServiceId);
       
  5607                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5608                 TPckgBuf<TInt> parameter;
       
  5609                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  5610                                                              *ipMsvEntrySelection,
       
  5611                                                              parameter,
       
  5612                                                              iStatus);
       
  5613             }
       
  5614         } else if (iOperation == MTMOperationRetrieveMessageBody) {
       
  5615             // Retrieve message body
       
  5616             ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5617             
       
  5618             if (ipMTM->Type() == KUidMsgTypeIMAP4) {
       
  5619                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5620                 TImImap4GetPartialMailInfo info;
       
  5621                 info.iMaxEmailSize = KMaxTInt;
       
  5622                 info.iTotalSizeLimit = KMaxTInt;
       
  5623                 info.iBodyTextSizeLimit = KMaxTInt;
       
  5624                 info.iPartialMailOptions = EBodyTextOnly;
       
  5625                 TPckg<TImImap4GetPartialMailInfo> bodyInfo(info);
       
  5626                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
       
  5627                                                              *ipMsvEntrySelection,
       
  5628                                                              bodyInfo,
       
  5629                                                              iStatus);
       
  5630             } else if (ipMTM->Type() == KUidMsgTypePOP3) {
       
  5631                 ipMsvEntrySelection->AppendL(iServiceId);
       
  5632                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5633                 TPckgBuf<TInt> parameter;
       
  5634                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  5635                                                              *ipMsvEntrySelection,
       
  5636                                                              parameter,
       
  5637                                                              iStatus);
       
  5638             }
       
  5639         }  else if (iOperation == MTMOperationRetrieveMessageAttachments) {
       
  5640             // Retrieve message attachments
       
  5641             ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5642             
       
  5643             if (ipMTM->Type() == KUidMsgTypeIMAP4) {
       
  5644                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5645                 TImImap4GetPartialMailInfo info;
       
  5646                 info.iMaxEmailSize = KMaxTInt;
       
  5647                 info.iTotalSizeLimit = KMaxTInt;
       
  5648                 info.iAttachmentSizeLimit = KMaxTInt;
       
  5649                 info.iPartialMailOptions = EAttachmentsOnly;
       
  5650                 TPckg<TImImap4GetPartialMailInfo> bodyInfo(info);
       
  5651                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMPopulate,
       
  5652                                                              *ipMsvEntrySelection,
       
  5653                                                              bodyInfo,
       
  5654                                                              iStatus);
       
  5655             } else if (ipMTM->Type() == KUidMsgTypePOP3) {
       
  5656                 ipMsvEntrySelection->AppendL(iServiceId);
       
  5657                 ipMsvEntrySelection->AppendL(iMessageId);
       
  5658                 TPckgBuf<TInt> parameter;
       
  5659                 ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KPOP3MTMPopulate,
       
  5660                                                              *ipMsvEntrySelection,
       
  5661                                                              parameter,
       
  5662                                                              iStatus);
       
  5663             }
       
  5664         } else if (iOperation == MTMOperationFullSync) {
       
  5665             // Do full sync for IMAP Account
       
  5666             // <=> in addition to syncing messages from server to client
       
  5667             //     syncs also changes (like read statuses) from client to server
       
  5668             ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5669             ipMsvEntrySelection->AppendL(iServiceId);
       
  5670             
       
  5671             TImImap4GetMailInfo imap4GetMailInfo;
       
  5672             imap4GetMailInfo.iMaxEmailSize = KMaxTInt;
       
  5673             imap4GetMailInfo.iDestinationFolder = iServiceId+1; // remote inbox
       
  5674             imap4GetMailInfo.iGetMailBodyParts = EGetImap4EmailHeaders;
       
  5675             TPckgBuf<TImImap4GetMailInfo> package(imap4GetMailInfo);
       
  5676             
       
  5677             ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMFullSync,
       
  5678                                                          *ipMsvEntrySelection,
       
  5679                                                          package,
       
  5680                                                          iStatus);
       
  5681             
       
  5682         
       
  5683         }
       
  5684         iOperationStep = MTMOperationStepDisconnect;
       
  5685         SetActive();
       
  5686         }
       
  5687         break;
       
  5688     case CAsynchronousMTMOperation::MTMOperationStepDisconnect:
       
  5689         {
       
  5690         delete ipMsvEntrySelection;
       
  5691         ipMsvEntrySelection = NULL;
       
  5692         delete ipMsvOperation;
       
  5693         ipMsvOperation = NULL;
       
  5694         
       
  5695         TPckgBuf<TInt> parameter;
       
  5696         ipMsvEntrySelection = new(ELeave) CMsvEntrySelection;
       
  5697         ipMsvEntrySelection->AppendL(iServiceId);
       
  5698         if (ipMTM->Type() == KUidMsgTypeIMAP4) {
       
  5699             ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,
       
  5700                                                          *ipMsvEntrySelection,
       
  5701                                                          parameter,
       
  5702                                                          iStatus);
       
  5703         } else if (ipMTM->Type() == KUidMsgTypePOP3) {
       
  5704             ipMsvOperation = ipMTM->InvokeAsyncFunctionL(KPOP3MTMDisconnect,
       
  5705                                                          *ipMsvEntrySelection,
       
  5706                                                          parameter,
       
  5707                                                          iStatus);
       
  5708         }
       
  5709         iOperationStep = MTMOperationStepFinished;
       
  5710         SetActive();
       
  5711         }
       
  5712         break;
       
  5713     case CAsynchronousMTMOperation::MTMOperationStepFinished:
       
  5714         delete ipMsvEntrySelection;
       
  5715         ipMsvEntrySelection = NULL;
       
  5716         delete ipMsvOperation;
       
  5717         ipMsvOperation = NULL;
       
  5718 
       
  5719         isActive = false;
       
  5720         ipPrivateService->setFinished(true);
       
  5721         ipParent->deleteAsynchronousMTMOperation(this);
       
  5722         break;
       
  5723     }
       
  5724 }
       
  5725 
       
  5726 void CAsynchronousMTMOperation::DoCancel()
       
  5727 {
       
  5728     ipMsvOperation->Cancel();
       
  5729 }
  5660 
  5730 
  5661 QTM_END_NAMESPACE
  5731 QTM_END_NAMESPACE