email/imum/Mtms/Src/ImapPopulateOperation.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *       CImapPopulateOp implementation file
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "ImapPopulateOperation.h"
       
    21 
       
    22 //  General includes
       
    23 #include <eikenv.h>
       
    24 #include <eikrutil.h>
       
    25 #include <StringLoader.h>
       
    26 
       
    27 //  Messaging includes
       
    28 #include <miut_err.h>
       
    29 #include <miutset.h>
       
    30 #include <msvuids.h>
       
    31 #include <mtmuibas.h>
       
    32 #include <imapset.h>
       
    33 #include <Msvprgreporter.H>
       
    34 
       
    35 #include <imapcmds.h>
       
    36 #include <imum.rsg>
       
    37 #include "ImapConnectionOp.h"
       
    38 #include "MsvConnectionValidation.h"
       
    39 #include "MsvEmailConnectionProgressProvider.h"
       
    40 #include "progtype.h"
       
    41 #include "ImumPanic.h"
       
    42 #include "ImumMtmLogging.h"
       
    43 #include "ImapFolderSyncOperation.h"
       
    44 
       
    45 #include "emailutils.h"
       
    46 
       
    47 const TInt KImumMaxLoginRetries = 3;
       
    48 const TInt KImumOperationsArrayGranularity = 2;
       
    49 
       
    50 
       
    51 // ----------------------------------------------------
       
    52 // CImapPopulateOp::NewL
       
    53 // ----------------------------------------------------
       
    54 CImapPopulateOp* CImapPopulateOp::NewL(
       
    55     CMsvSession& aSession,
       
    56     TInt aPriority,
       
    57     TRequestStatus& aStatus,
       
    58     TMsvId aService,
       
    59     MMsvProgressReporter& aProgressReporter)
       
    60     {
       
    61     CImapPopulateOp* me=new (ELeave) CImapPopulateOp(
       
    62         aSession, aPriority, aStatus,
       
    63         aService, aProgressReporter );
       
    64     CleanupStack::PushL(me);
       
    65     me->ConstructL();
       
    66     CleanupStack::Pop();    //  me
       
    67     return me;
       
    68     }
       
    69 
       
    70 // ----------------------------------------------------
       
    71 // CImapPopulateOp::CImapPopulateOp
       
    72 // ----------------------------------------------------
       
    73 CImapPopulateOp::CImapPopulateOp(
       
    74     CMsvSession& aSession,
       
    75     TInt aPriority,
       
    76     TRequestStatus& aStatus,
       
    77     TMsvId aService,
       
    78     MMsvProgressReporter& aProgressReporter)
       
    79     :
       
    80     CMsvOperation(aSession, aPriority, aStatus),
       
    81     iState(EPopulateConstructing),
       
    82     iSyncProgress(),
       
    83     iProgressReporter(aProgressReporter)
       
    84     {
       
    85     iService=aService;
       
    86     iMtm=KUidMsgTypeIMAP4;
       
    87     iSyncProgress().iType=EImap4SyncProgressType;
       
    88     iCoeEnv = CCoeEnv::Static();
       
    89     }
       
    90 
       
    91     // ----------------------------------------------------
       
    92 // CImapPopulateOp::~CImapPopulateOp
       
    93 // ----------------------------------------------------
       
    94 CImapPopulateOp::~CImapPopulateOp()
       
    95     {
       
    96     Cancel();
       
    97     iProgressReporter.UnsetProgressDecoder();
       
    98 
       
    99     //  Release the shared MtmUi
       
   100     if(iMtmUi)
       
   101         {
       
   102         iProgressReporter.MtmStore().ReleaseMtmUi(iMtm);
       
   103         }
       
   104 
       
   105     delete iConnectionProgressProvider;
       
   106     delete iSubOperation;
       
   107     delete iTitleText;
       
   108     // don't own iCoeEnv so don't delete it
       
   109     }
       
   110 
       
   111 // ----------------------------------------------------
       
   112 // CImapPopulateOp::ConstructL
       
   113 // ----------------------------------------------------
       
   114 void CImapPopulateOp::ConstructL()
       
   115     {
       
   116     CMsvEntry* serviceEntry=NULL;
       
   117 
       
   118     serviceEntry=iMsvSession.GetEntryL(iService);
       
   119     CleanupStack::PushL(serviceEntry);
       
   120 
       
   121     iSyncProgress().iState=TImap4SyncProgress::EConnecting;
       
   122     iSyncProgress().iErrorCode=KErrNone;
       
   123 
       
   124     //  Store the related service Id
       
   125     iRelatedServiceId=serviceEntry->Entry().iRelatedId;
       
   126 
       
   127     iTitleText = StringLoader::LoadL(
       
   128         R_EMAIL_CONNECTING_SERVER,
       
   129         serviceEntry->Entry().iDetails,
       
   130         iCoeEnv );
       
   131 
       
   132     CleanupStack::PopAndDestroy( serviceEntry );
       
   133 
       
   134     CActiveScheduler::Add(this);
       
   135 
       
   136     iObserverRequestStatus=KRequestPending;
       
   137 
       
   138     TRequestStatus* myStatus=&iStatus;
       
   139     User::RequestComplete(myStatus,KErrNone);
       
   140 
       
   141     SetActive();
       
   142     //  We complete ourselves so that the dialog gets a chance to redraw
       
   143     //  before entering the lengthy process of opening system agent and
       
   144     //  connection progress provider
       
   145     }
       
   146 
       
   147 
       
   148 // ----------------------------------------------------
       
   149 // CImapPopulateOp::RunL
       
   150 // ----------------------------------------------------
       
   151 void CImapPopulateOp::RunL()
       
   152     {
       
   153 	DoRunL();
       
   154     }
       
   155 
       
   156 // ----------------------------------------------------
       
   157 // CImapPopulateOp::DoRunL
       
   158 // ----------------------------------------------------
       
   159 void CImapPopulateOp::DoRunL()
       
   160     {
       
   161     if(iState==EPopulateConstructing)
       
   162         {
       
   163         FinishConstructionL();
       
   164         }
       
   165     else
       
   166         {
       
   167         ConnectionCompletionL();
       
   168         }
       
   169     }
       
   170 
       
   171 // ----------------------------------------------------
       
   172 // CImapPopulateOp::RunError
       
   173 // ----------------------------------------------------
       
   174 TInt CImapPopulateOp::RunError(TInt aError)
       
   175 	{
       
   176     iSyncProgress().iErrorCode = aError;
       
   177     Complete();		
       
   178 	}
       
   179     
       
   180 // ----------------------------------------------------
       
   181 // CImapPopulateOp::FinishConstructionL
       
   182 // ----------------------------------------------------
       
   183 void CImapPopulateOp::FinishConstructionL()
       
   184     {
       
   185 
       
   186     iMtmUi=&(iProgressReporter.MtmStore().ClaimMtmUiL(iMtm));
       
   187     iMtmUi->BaseMtm().SwitchCurrentEntryL(iService);
       
   188 
       
   189 
       
   190     iConnectionProgressProvider=CMsvEmailConnectionProgressProvider::NewL(
       
   191         iMsvSession, iService);
       
   192 
       
   193     CImImap4Settings* settings = new(ELeave) CImImap4Settings;
       
   194     CleanupStack::PushL( settings );
       
   195     CMsvEntry* cEntry = iMsvSession.GetEntryL(iService);
       
   196     CleanupStack::PushL( cEntry );
       
   197     MsvEmailMtmUiUtils::RestoreSettingsL( *cEntry, *settings );
       
   198 
       
   199     TInt bodySizeLimit = settings->BodyTextSizeLimit();
       
   200 
       
   201     // -1 for full mail body
       
   202     // > 0 for some KB
       
   203     // KMaxTInt32 is default value, so no limits set
       
   204     if( bodySizeLimit == -1 || bodySizeLimit > 0 || bodySizeLimit < KMaxTInt32 )
       
   205         {
       
   206         iState = EPopulating;
       
   207         iSubOperation = DoPopulateL( *settings );
       
   208         CleanupStack::PopAndDestroy(2); // CSI: 47 # settings, cEntry
       
   209         }
       
   210     else
       
   211         {
       
   212         CleanupStack::PopAndDestroy(2); // CSI: 47 # settings, cEntry
       
   213         }
       
   214 
       
   215 
       
   216     SetActive();
       
   217     //
       
   218     //  We're going to try and connect; tell our MMsvProgressReporter
       
   219     //  to come to us for progress.
       
   220     //
       
   221     iProgressReporter.SetProgressDecoder(*this);
       
   222 
       
   223     iObserverRequestStatus=KRequestPending;
       
   224     }
       
   225 
       
   226 // ----------------------------------------------------
       
   227 // CImapPopulateOp::ConnectionCompletionL
       
   228 // ----------------------------------------------------
       
   229 void CImapPopulateOp::ConnectionCompletionL()
       
   230     {
       
   231     //  Called by RunL when past EPopulateConstructing state
       
   232     TInt err=KErrNone;
       
   233     if(iSubOperation)
       
   234         {
       
   235         //  The sub connection operation has completed.
       
   236 
       
   237         //  We need to detect a failure to connect due to invalid login details,
       
   238         //  and prompt the user accordingly.
       
   239         //
       
   240         TBuf<CBaseMtmUi::EProgressStringMaxLen> dummyString;
       
   241         TInt dummyInt[4]; // CSI: 47 # For dummyInt
       
   242 
       
   243         err=DecodeProgress(iSubOperation->ProgressL(),dummyString,dummyInt[0],dummyInt[1],dummyInt[2],dummyInt[3],ETrue); // CSI: 47 # For dummyInts and dummyStrings.
       
   244         }
       
   245 
       
   246 
       
   247     User::LeaveIfError(err);
       
   248 
       
   249     Complete();
       
   250     }
       
   251 
       
   252 // ----------------------------------------------------
       
   253 // CImapPopulateOp::DoCancel
       
   254 // ----------------------------------------------------
       
   255 void CImapPopulateOp::DoCancel()
       
   256     {
       
   257     if(iSubOperation)
       
   258         iSubOperation->Cancel();
       
   259 
       
   260     // Unset password.
       
   261     if(iResetPassword)
       
   262         {
       
   263         TRAPD(ignore, MsvConnectionValidation::UnsetPasswordL(iMsvSession,iService));
       
   264         }
       
   265 
       
   266     iSyncProgress().iErrorCode=KErrCancel;
       
   267     Complete();
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------
       
   271 // CImapPopulateOp::ProgressL
       
   272 // ----------------------------------------------------
       
   273 const TDesC8& CImapPopulateOp::ProgressL()
       
   274     {
       
   275     if(iSubOperation)
       
   276         {
       
   277         return iSubOperation->ProgressL();
       
   278         }
       
   279     else
       
   280         {
       
   281         return iSyncProgress;
       
   282         }
       
   283     }
       
   284 
       
   285 // ----------------------------------------------------
       
   286 // CImapPopulateOp::DecodeProgress
       
   287 // ----------------------------------------------------
       
   288 TInt CImapPopulateOp::DecodeProgress(const TDesC8& aProgress,
       
   289         TBuf<CBaseMtmUi::EProgressStringMaxLen>& aReturnString,
       
   290         TInt& aTotalEntryCount, TInt& aEntriesDone,
       
   291         TInt& aCurrentEntrySize, TInt& aCurrentBytesTrans, TBool aInternal)
       
   292     {
       
   293     if(aProgress.Length()==0)
       
   294         return KErrNone;
       
   295 
       
   296     TPckgC<TInt> type(0);
       
   297     type.Set(aProgress.Left(sizeof(TInt)));
       
   298 
       
   299     if(!aInternal || type()!=EUiProgTypeConnecting)
       
   300         return iMtmUi->GetProgress(aProgress, aReturnString, aTotalEntryCount,
       
   301             aEntriesDone, aCurrentEntrySize, aCurrentBytesTrans);
       
   302     return KErrNone;
       
   303     }
       
   304 
       
   305 
       
   306 // ----------------------------------------------------
       
   307 // CImapPopulateOp::Complete
       
   308 // ----------------------------------------------------
       
   309 void CImapPopulateOp::Complete()
       
   310     {
       
   311     TRequestStatus* observer=&iObserverRequestStatus;
       
   312     User::RequestComplete(observer, KErrNone);
       
   313     }
       
   314 
       
   315 
       
   316 CMsvOperation* CImapPopulateOp::DoPopulateL( CImImap4Settings& aSettings )
       
   317     {
       
   318     iPopulated = ETrue;
       
   319     //iStatus = KRequestPending;
       
   320 
       
   321     TImImap4GetPartialMailInfo info;
       
   322 
       
   323     CMsvEntry* cEntry = iMsvSession.GetEntryL(iService);
       
   324     CleanupStack::PushL( cEntry );
       
   325 
       
   326 
       
   327     TInt bodySizeLimit = aSettings.BodyTextSizeLimit();
       
   328     TInt attaSizeLimit = aSettings.AttachmentSizeLimit();
       
   329 
       
   330     info.iBodyTextSizeLimit = bodySizeLimit;
       
   331     info.iAttachmentSizeLimit = attaSizeLimit;
       
   332     info.iTotalSizeLimit = ( bodySizeLimit + attaSizeLimit );//??
       
   333     info.iPartialMailOptions = aSettings.PartialMailOptions();
       
   334 
       
   335     TPckg<TImImap4GetPartialMailInfo> param(info);
       
   336 
       
   337     CMsvEntrySelection* selection = new(ELeave) CMsvEntrySelection;
       
   338     CleanupStack::PushL( selection );
       
   339     selection->AppendL( iService );
       
   340 
       
   341     TMsvId inbox = FindInboxL( *cEntry );
       
   342     selection->AppendL( inbox );
       
   343 
       
   344     CMsvOperation* op = iMtmUi->InvokeAsyncFunctionL(
       
   345         KIMAP4MTMPopulateNewMailWhenAlreadyConnected,
       
   346         //KIMAP4MTMPopulateAllMailWhenAlreadyConnected,
       
   347         *selection,
       
   348         iStatus,
       
   349         param);
       
   350 
       
   351 
       
   352     CleanupStack::PopAndDestroy(2); // CSI: 47 # selection, centry
       
   353 
       
   354     return op;
       
   355     }
       
   356 
       
   357 TMsvId CImapPopulateOp::FindInboxL( CMsvEntry& aEntry )
       
   358     {
       
   359     TMsvId inboxId = KErrNotFound;
       
   360     TMsvEntry child;
       
   361     const TInt count = aEntry.Count();
       
   362     _LIT( KTMceUiInboxName, "INBOX");
       
   363     for (TInt loop = 0; loop < count && inboxId == KErrNotFound; loop++)
       
   364         {
       
   365         child = (aEntry)[loop];
       
   366         if ( child.iType == KUidMsvFolderEntry  &&
       
   367              child.iDetails.CompareF( KTMceUiInboxName ) == 0 )
       
   368             {
       
   369             inboxId = child.Id();
       
   370             }
       
   371         }
       
   372 
       
   373     return inboxId;
       
   374     }
       
   375 
       
   376 // End of File