|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "t_partialdownloadimapclient.h" |
|
17 |
|
18 _LIT(KLoopbackAddress,"127.0.0.1"); |
|
19 |
|
20 void TDummySessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/) |
|
21 { |
|
22 } |
|
23 |
|
24 CPartialDownLoadImapClient::CPartialDownLoadImapClient(MImapTestEventHandler& aObserver,TBool aFetchWholeMessage) |
|
25 : CActive(EPriorityStandard), iObserver(aObserver), iFetchWholeMessage(aFetchWholeMessage) |
|
26 { |
|
27 CActiveScheduler::Add(this); |
|
28 } |
|
29 |
|
30 CPartialDownLoadImapClient* CPartialDownLoadImapClient::NewL(MImapTestEventHandler& aObserver,TBool aFetchWholeMessage) |
|
31 { |
|
32 CPartialDownLoadImapClient* self = NewLC(aObserver,aFetchWholeMessage); |
|
33 CleanupStack::Pop(self); |
|
34 return self; |
|
35 } |
|
36 |
|
37 CPartialDownLoadImapClient* CPartialDownLoadImapClient::NewLC(MImapTestEventHandler& aObserver,TBool aFetchWholeMessage) |
|
38 { |
|
39 CPartialDownLoadImapClient* self = new(ELeave) CPartialDownLoadImapClient(aObserver,aFetchWholeMessage); |
|
40 CleanupStack::PushL(self); |
|
41 self->ConstructL(); |
|
42 return self; |
|
43 } |
|
44 |
|
45 CPartialDownLoadImapClient::~CPartialDownLoadImapClient() |
|
46 { |
|
47 Cancel(); |
|
48 delete iMsvOperation; |
|
49 delete iSelection; |
|
50 delete iClientMtm; |
|
51 delete iClientRegistry; |
|
52 delete iSession; |
|
53 delete iSessionObserver; |
|
54 delete iAccounts; |
|
55 } |
|
56 |
|
57 TImapAccount CPartialDownLoadImapClient::GetImapAccount() |
|
58 { |
|
59 return iImapAccount; |
|
60 } |
|
61 |
|
62 void CPartialDownLoadImapClient::ConstructL() |
|
63 { |
|
64 //create an account |
|
65 iAccounts = CEmailAccounts::NewL(); |
|
66 CreateImapAccountL(); |
|
67 // Session observer. Needed to create a session |
|
68 iSessionObserver = new (ELeave) TDummySessionObserver; |
|
69 // Session. Needed to create a client registry. |
|
70 iSession=CMsvSession::OpenSyncL(*iSessionObserver); |
|
71 // Client registry. Needed to get the MTM component |
|
72 iClientRegistry=CClientMtmRegistry::NewL(*iSession,KMsvDefaultTimeoutMicroSeconds32); |
|
73 //get the client mtm |
|
74 iClientMtm=iClientRegistry->NewMtmL(KUidMsgTypeIMAP4); |
|
75 |
|
76 // Load the serial comms device drivers. If this is not done, |
|
77 // connecting via NT-RAS returns KErrNotFound (-1). |
|
78 TInt driverErr; |
|
79 driverErr=User::LoadPhysicalDevice(PDD_NAME); |
|
80 if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists) |
|
81 { |
|
82 User::Leave(driverErr); |
|
83 } |
|
84 driverErr=User::LoadLogicalDevice(LDD_NAME); |
|
85 if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists) |
|
86 { |
|
87 User::Leave(driverErr); |
|
88 } |
|
89 } |
|
90 |
|
91 void CPartialDownLoadImapClient::CreateImapAccountL() |
|
92 { |
|
93 //create objects and initialise with the defaults |
|
94 CImImap4Settings* imap4Settings=new(ELeave)CImImap4Settings; |
|
95 CleanupStack::PushL(imap4Settings); |
|
96 CImSmtpSettings* smtpSettings=new(ELeave)CImSmtpSettings; |
|
97 CleanupStack::PushL(smtpSettings); |
|
98 CImIAPPreferences* imapIap = CImIAPPreferences::NewLC(); |
|
99 CImIAPPreferences* smtpIap = CImIAPPreferences::NewLC(); |
|
100 |
|
101 iAccounts->PopulateDefaultImapSettingsL(*imap4Settings, *imapIap); |
|
102 iAccounts->PopulateDefaultSmtpSettingsL(*smtpSettings, *smtpIap); |
|
103 |
|
104 //override some of the defaults |
|
105 imap4Settings->SetPasswordL(_L8("davids")); |
|
106 imap4Settings->SetLoginNameL(_L8("davids")); |
|
107 imap4Settings->SetServerAddressL(KLoopbackAddress); |
|
108 imap4Settings->SetFolderPathL(_L8("")); |
|
109 imap4Settings->SetSynchronise(EUseLocal); |
|
110 |
|
111 imap4Settings->SetPartialMailOptionsL(ECumulative); |
|
112 imap4Settings->SetAttachmentSizeLimitL(2); |
|
113 |
|
114 //create the account |
|
115 iImapAccount = iAccounts->CreateImapAccountL(_L("TestAccount"), *imap4Settings, *imapIap, EFalse); |
|
116 TSmtpAccount smtpAccount; |
|
117 smtpAccount = iAccounts->CreateSmtpAccountL(iImapAccount, *smtpSettings, *smtpIap, EFalse); |
|
118 |
|
119 //clean up |
|
120 CleanupStack::PopAndDestroy(4,imap4Settings); |
|
121 } |
|
122 |
|
123 void CPartialDownLoadImapClient::StartL() |
|
124 { |
|
125 //if we are to collect the whole email then the next operation will be to fetch it |
|
126 iNextStep=(!iFetchWholeMessage? EDisconnect:EFetch); |
|
127 iSelection = new (ELeave) CMsvEntrySelection; |
|
128 TPckg<MMsvImapConnectionObserver*> param(this); |
|
129 //select the imap service entry |
|
130 iSelection->AppendL(iImapAccount.iImapService); |
|
131 //make the service entry the current context |
|
132 iClientMtm->SwitchCurrentEntryL(iImapAccount.iImapService); |
|
133 //sync the account |
|
134 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMConnectAndSyncCompleteAfterFullSync,*iSelection,param,iStatus); |
|
135 SetActive(); |
|
136 } |
|
137 |
|
138 void CPartialDownLoadImapClient::DoCancel() |
|
139 { |
|
140 __ASSERT_DEBUG(iMsvOperation!=NULL, User::Invariant()); |
|
141 iMsvOperation->Cancel(); |
|
142 } |
|
143 |
|
144 void CPartialDownLoadImapClient::RunL() |
|
145 { |
|
146 User::LeaveIfError(iStatus.Int()); |
|
147 |
|
148 TPckg<MMsvImapConnectionObserver*> param(this); |
|
149 TImImap4GetPartialMailInfo imapGetPartialMailInfo; |
|
150 imapGetPartialMailInfo.iPartialMailOptions=ECumulative; |
|
151 imapGetPartialMailInfo.iBodyTextSizeLimit = 2; |
|
152 imapGetPartialMailInfo.iAttachmentSizeLimit = 2; |
|
153 TPckgBuf<TImImap4GetPartialMailInfo> package(imapGetPartialMailInfo); |
|
154 |
|
155 CMsvEntry* inboxEntry=NULL; |
|
156 CMsvEntry* imapService=NULL; |
|
157 TMsvSelectionOrdering ordering; |
|
158 |
|
159 switch(iNextStep) |
|
160 { |
|
161 //connect and sync |
|
162 case EDisconnect: |
|
163 delete iMsvOperation; |
|
164 iMsvOperation=NULL; |
|
165 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,*iSelection,param,iStatus); |
|
166 SetActive(); |
|
167 iNextStep=EComplete; |
|
168 break; |
|
169 |
|
170 case EFetch: |
|
171 // message in remote inbox |
|
172 //create a CMsvEntry for the service entry |
|
173 imapService = CMsvEntry::NewL(*iSession, iImapAccount.iImapService,ordering); |
|
174 CleanupStack::PushL(imapService); |
|
175 //retrieve the inbox from the imap service entry |
|
176 CMsvEntrySelection* msvEntrySelection; |
|
177 msvEntrySelection=imapService->ChildrenL(); |
|
178 CleanupStack::PushL(msvEntrySelection); |
|
179 //create a CMsvEntry for the inbox entry |
|
180 inboxEntry = CMsvEntry::NewL(*iSession, (*msvEntrySelection)[0],ordering); |
|
181 CleanupStack::PopAndDestroy(msvEntrySelection); |
|
182 CleanupStack::PushL(inboxEntry); |
|
183 //retrieve the message from the inboxEntry service entry |
|
184 iSelection=inboxEntry->ChildrenL(); |
|
185 |
|
186 CleanupStack::PopAndDestroy(inboxEntry); |
|
187 CleanupStack::PopAndDestroy(imapService); |
|
188 delete iMsvOperation; |
|
189 iMsvOperation=NULL; |
|
190 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMPopulate, *iSelection, package, iStatus); |
|
191 SetActive(); |
|
192 iNextStep=EDisconnect; |
|
193 break; |
|
194 |
|
195 case EComplete: |
|
196 //inform the owning object that the process is complete |
|
197 iObserver.TestComplete(KErrNone); |
|
198 break; |
|
199 |
|
200 default: |
|
201 __ASSERT_DEBUG(0, User::Panic(_L("CPartialDownLoadImapClient unknown state"), KErrUnknown)); |
|
202 break; |
|
203 } |
|
204 } |
|
205 |
|
206 TInt CPartialDownLoadImapClient::RunError(TInt aError) |
|
207 { |
|
208 iObserver.TestComplete(aError); |
|
209 return KErrNone; |
|
210 } |
|
211 |
|
212 void CPartialDownLoadImapClient::HandleImapConnectionEvent(TImapConnectionEvent /*aConnectionState*/) |
|
213 { |
|
214 //this method does nothing |
|
215 } |
|
216 |
|
217 |
|
218 |