|
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 // imapclient.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 #include "decodeimapclient.h" |
|
19 |
|
20 _LIT(KLoopbackAddress,"127.0.0.1"); |
|
21 |
|
22 void TDummySessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/) |
|
23 { |
|
24 } |
|
25 |
|
26 TImapAccount CActiveImapClient::GetAccount() |
|
27 { |
|
28 return iImapAccount; |
|
29 } |
|
30 |
|
31 CActiveImapClient::CActiveImapClient() : CActive(EPriorityStandard) |
|
32 { |
|
33 } |
|
34 |
|
35 EXPORT_C CActiveImapClient* CActiveImapClient::NewL(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage) |
|
36 { |
|
37 CActiveImapClient* self = NewLC(aOwner,aFetchWholeMessage); |
|
38 CleanupStack::Pop(self); |
|
39 return self; |
|
40 } |
|
41 |
|
42 EXPORT_C CActiveImapClient* CActiveImapClient::NewLC(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage) |
|
43 { |
|
44 CActiveImapClient* self = new(ELeave) CActiveImapClient; |
|
45 CleanupStack::PushL(self); |
|
46 self->ConstructL(aOwner,aFetchWholeMessage); |
|
47 return self; |
|
48 } |
|
49 |
|
50 CActiveImapClient::~CActiveImapClient() |
|
51 { |
|
52 Cancel(); |
|
53 delete iMsvOperation; |
|
54 delete iSelection; |
|
55 delete iClientMtm; |
|
56 delete iClientRegistry; |
|
57 delete iSession; |
|
58 delete iSessionObserver; |
|
59 delete iAccounts; |
|
60 } |
|
61 |
|
62 |
|
63 void CActiveImapClient::ConstructL(MImapTestEventHandler* aOwner,TBool aFetchWholeMessage) |
|
64 { |
|
65 CActiveScheduler::Add(this); |
|
66 iFetchWholeMessage=aFetchWholeMessage; |
|
67 iOwner=aOwner; |
|
68 //create an account |
|
69 iAccounts = CEmailAccounts::NewL(); |
|
70 CreateImapAccountL(); |
|
71 // Session observer. Needed to create a session |
|
72 iSessionObserver = new (ELeave) TDummySessionObserver; |
|
73 // Session. Needed to create a client registry. |
|
74 iSession=CMsvSession::OpenSyncL(*iSessionObserver); |
|
75 // Client registry. Needed to get the MTM component |
|
76 iClientRegistry=CClientMtmRegistry::NewL(*iSession,KMsvDefaultTimeoutMicroSeconds32); |
|
77 //get the client mtm |
|
78 iClientMtm=iClientRegistry->NewMtmL(KUidMsgTypeIMAP4); |
|
79 |
|
80 // Load the serial comms device drivers. If this is not done, |
|
81 // connecting via NT-RAS returns KErrNotFound (-1). |
|
82 TInt driverErr; |
|
83 driverErr=User::LoadPhysicalDevice(PDD_NAME); |
|
84 if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists) |
|
85 User::Leave(driverErr); |
|
86 driverErr=User::LoadLogicalDevice(LDD_NAME); |
|
87 if (driverErr!=KErrNone && driverErr!=KErrAlreadyExists) |
|
88 User::Leave(driverErr); |
|
89 } |
|
90 |
|
91 void CActiveImapClient::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 //create the account |
|
112 iImapAccount = iAccounts->CreateImapAccountL(_L("TestAccount"), *imap4Settings, *imapIap, EFalse); |
|
113 TSmtpAccount smtpAccount; |
|
114 smtpAccount = iAccounts->CreateSmtpAccountL(iImapAccount, *smtpSettings, *smtpIap, EFalse); |
|
115 |
|
116 //clean up |
|
117 CleanupStack::PopAndDestroy(smtpIap); |
|
118 CleanupStack::PopAndDestroy(imapIap); |
|
119 CleanupStack::PopAndDestroy(smtpSettings); |
|
120 CleanupStack::PopAndDestroy(imap4Settings); |
|
121 } |
|
122 |
|
123 void CActiveImapClient::StartL() |
|
124 { |
|
125 |
|
126 //if we are to collect the whole email then the next operation will be to fetch it |
|
127 iNextStep=(!iFetchWholeMessage? EDisconnect:EFetch); |
|
128 iSelection = new (ELeave) CMsvEntrySelection; |
|
129 TPckg<MMsvImapConnectionObserver*> param(this); |
|
130 //select the imap service entry |
|
131 iSelection->AppendL(iImapAccount.iImapService); |
|
132 //make the service entry the current context |
|
133 iClientMtm->SwitchCurrentEntryL(iImapAccount.iImapService); |
|
134 //sync the account |
|
135 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMConnectAndSyncCompleteAfterFullSync,*iSelection,param,iStatus); |
|
136 SetActive(); |
|
137 } |
|
138 |
|
139 void CActiveImapClient::DoCancel() |
|
140 { |
|
141 __ASSERT_DEBUG(iMsvOperation!=NULL, User::Panic(_L("CActiveImapClient"), -3)); |
|
142 iMsvOperation->Cancel(); |
|
143 } |
|
144 |
|
145 void CActiveImapClient::RunL() |
|
146 { |
|
147 |
|
148 TPckg<MMsvImapConnectionObserver*> param(this); |
|
149 TImImap4GetMailInfo imap4GetMailInfo; |
|
150 imap4GetMailInfo.iMaxEmailSize = KMaxTInt; |
|
151 imap4GetMailInfo.iDestinationFolder = KMsvGlobalInBoxIndexEntryIdValue; |
|
152 imap4GetMailInfo.iGetMailBodyParts = EGetImap4EmailBodyTextAndAttachments; |
|
153 |
|
154 TPckgBuf<TImImap4GetMailInfo> package(imap4GetMailInfo); |
|
155 |
|
156 CMsvEntry* inboxEntry=NULL; |
|
157 CMsvEntry* imapService=NULL; |
|
158 TMsvSelectionOrdering ordering; |
|
159 |
|
160 switch(iNextStep) |
|
161 { |
|
162 //connect and sync |
|
163 case EDisconnect: |
|
164 iNextStep=EComplete; |
|
165 delete iMsvOperation; |
|
166 iMsvOperation=NULL; |
|
167 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMDisconnect,*iSelection,param,iStatus); |
|
168 SetActive(); |
|
169 break; |
|
170 |
|
171 case EFetch: |
|
172 iNextStep=EDisconnect; |
|
173 // message in remote inbox |
|
174 //create a CMsvEntry for the service entry |
|
175 imapService = CMsvEntry::NewL(*iSession, iImapAccount.iImapService,ordering); |
|
176 CleanupStack::PushL(imapService); |
|
177 //retrieve the inbox from the imap service entry |
|
178 CMsvEntrySelection* msvEntrySelection; |
|
179 msvEntrySelection=imapService->ChildrenL(); |
|
180 CleanupStack::PushL(msvEntrySelection); |
|
181 //create a CMsvEntry for the inbox entry |
|
182 inboxEntry = CMsvEntry::NewL(*iSession, (*msvEntrySelection)[0],ordering); |
|
183 CleanupStack::PopAndDestroy(msvEntrySelection); |
|
184 CleanupStack::PushL(inboxEntry); |
|
185 //retrieve the message from the inboxEntry service entry |
|
186 msvEntrySelection=inboxEntry->ChildrenL(); |
|
187 CleanupStack::PushL(msvEntrySelection); |
|
188 //append the message entry to our selection |
|
189 iSelection->AppendL((*msvEntrySelection)[0]); |
|
190 //cleanup |
|
191 CleanupStack::PopAndDestroy(msvEntrySelection); |
|
192 CleanupStack::PopAndDestroy(inboxEntry); |
|
193 CleanupStack::PopAndDestroy(imapService); |
|
194 |
|
195 package = imap4GetMailInfo; |
|
196 delete iMsvOperation; |
|
197 iMsvOperation=NULL; |
|
198 iMsvOperation = iClientMtm->InvokeAsyncFunctionL(KIMAP4MTMCopyMailSelectionWhenAlreadyConnected, *iSelection, package, iStatus); |
|
199 SetActive(); |
|
200 break; |
|
201 |
|
202 case EComplete: |
|
203 //inform the owning object that the process is complete |
|
204 iOwner->TestComplete(KErrNone); |
|
205 break; |
|
206 |
|
207 default: |
|
208 __ASSERT_DEBUG(0, User::Panic(_L("CActiveImapClient unknown state"), KErrUnknown)); |
|
209 break; |
|
210 } |
|
211 } |
|
212 |
|
213 TInt CActiveImapClient::RunError(TInt aError) |
|
214 { |
|
215 iOwner->TestComplete(aError); |
|
216 return KErrNone; |
|
217 } |
|
218 |
|
219 void CActiveImapClient::HandleImapConnectionEvent(TImapConnectionEvent /*aConnectionState*/) |
|
220 { |
|
221 //this method does nothing |
|
222 } |
|
223 |
|
224 |
|
225 |