|
1 /* |
|
2 * Copyright (c) 2010 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 * |
|
16 */ |
|
17 |
|
18 #include "cmessagesessionobserver.h" |
|
19 #include <mtclreg.h> |
|
20 #include <smsclnt.h> |
|
21 #include <txtrich.h> |
|
22 #include <mmsclient.h> |
|
23 #include <cmsvmimeheaders.h> |
|
24 #include <cmsvattachment.h> |
|
25 #include <smtcmtm.h> |
|
26 |
|
27 |
|
28 CMessageSessionObserver* CMessageSessionObserver::NewL() |
|
29 { |
|
30 CMessageSessionObserver* self = CMessageSessionObserver::NewLC(); |
|
31 CleanupStack::Pop(); |
|
32 return self; |
|
33 } |
|
34 |
|
35 CMessageSessionObserver* CMessageSessionObserver::NewLC() |
|
36 { |
|
37 CMessageSessionObserver* self = new (ELeave) CMessageSessionObserver(); |
|
38 CleanupStack::PushL(self); |
|
39 self->ConstructL(); |
|
40 return self; |
|
41 } |
|
42 |
|
43 void CMessageSessionObserver::ConstructL() |
|
44 { |
|
45 } |
|
46 |
|
47 CMessageSessionObserver::CMessageSessionObserver() |
|
48 { |
|
49 |
|
50 } |
|
51 |
|
52 CMessageSessionObserver::~CMessageSessionObserver() |
|
53 { |
|
54 |
|
55 } |
|
56 |
|
57 void CMessageSessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, |
|
58 TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/) |
|
59 { |
|
60 //Dummy implementation |
|
61 } |
|
62 |
|
63 CMessAsyncWaiter* CMessAsyncWaiter::NewL() |
|
64 { |
|
65 CMessAsyncWaiter* self = new(ELeave) CMessAsyncWaiter(); |
|
66 return self; |
|
67 } |
|
68 |
|
69 //Adds the active object to the scheduler |
|
70 CMessAsyncWaiter::CMessAsyncWaiter() : CActive(EPriorityStandard) |
|
71 { |
|
72 CActiveScheduler::Add(this); |
|
73 } |
|
74 |
|
75 CMessAsyncWaiter::~CMessAsyncWaiter() |
|
76 { |
|
77 Cancel(); |
|
78 } |
|
79 |
|
80 //Indicates that the active object has issued a request and that it is now outstanding |
|
81 void CMessAsyncWaiter::StartAndWait() |
|
82 { |
|
83 SetActive(); |
|
84 CActiveScheduler::Start(); |
|
85 } |
|
86 |
|
87 TInt CMessAsyncWaiter::Result() const |
|
88 { |
|
89 return iError; |
|
90 } |
|
91 |
|
92 /** |
|
93 Handles an active object’s request completion event. |
|
94 It sets to run the desired task. |
|
95 */ |
|
96 void CMessAsyncWaiter::RunL() |
|
97 { |
|
98 iError = iStatus.Int(); |
|
99 CActiveScheduler::Stop(); |
|
100 } |
|
101 |
|
102 //Implements cancellation of an outstanding request,called as part of the active object's Cancel() |
|
103 void CMessAsyncWaiter::DoCancel() |
|
104 { |
|
105 iError = KErrCancel; |
|
106 CActiveScheduler::Stop(); |
|
107 } |
|
108 |
|
109 TInt MessagingUtils::CreateMessageL(CMsvSession* aMsgSession, const TDesC& aMsgFrom, |
|
110 const TDesC& aMsgTo,const TDesC& aMsgBody) |
|
111 { |
|
112 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL( *aMsgSession ); |
|
113 CSmsClientMtm* smsMtm = static_cast<CSmsClientMtm*>( mtmReg->NewMtmL( KUidMsgTypeSMS )); |
|
114 smsMtm->SwitchCurrentEntryL( KMsvGlobalInBoxIndexEntryId ); |
|
115 smsMtm->CreateMessageL( KUidMsgTypeSMS.iUid ); |
|
116 TMsvId newMsgId = smsMtm->Entry().EntryId(); |
|
117 smsMtm->AddAddresseeL( aMsgTo ); //to address if need |
|
118 CMsvStore* messageStore = smsMtm->Entry().EditStoreL(); |
|
119 CleanupStack::PushL( messageStore ); |
|
120 CSmsHeader& header = smsMtm->SmsHeader(); |
|
121 header.SetFromAddressL(aMsgFrom); |
|
122 header.StoreL( *messageStore ); |
|
123 CleanupStack::PopAndDestroy( messageStore ); |
|
124 CRichText& body = smsMtm->Body(); |
|
125 body.Reset(); |
|
126 // Put 16bits stream stored in 8bits buffer back to 16bits buffer. |
|
127 body.InsertL( 0, aMsgBody); |
|
128 TMsvEntry entry = smsMtm->Entry().Entry(); |
|
129 entry.SetInPreparation( EFalse ); |
|
130 entry.SetVisible( ETrue ); |
|
131 entry.iDate.HomeTime(); |
|
132 entry.iDetails.Set(aMsgFrom); |
|
133 entry.SetUnread( EFalse ); |
|
134 smsMtm->Entry().ChangeL( entry ); |
|
135 smsMtm->SaveMessageL(); |
|
136 delete smsMtm; |
|
137 delete mtmReg; |
|
138 return newMsgId; |
|
139 } |
|
140 |
|
141 TInt MessagingUtils::RemoveEntryL(CMsvSession* aMsgSession,TMsvId aMsgId) |
|
142 { |
|
143 aMsgSession->RemoveEntry(aMsgId); |
|
144 return KErrNone; |
|
145 } |
|
146 |
|
147 TInt MessagingUtils::CreateMmsMessageL(CMsvSession* aMsgSession, const TDesC& aMsgFrom, |
|
148 const TDesC& aMsgTo,const TDesC& afilename) |
|
149 { |
|
150 _LIT(KMessageSubject, "MMS Test"); |
|
151 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL( *aMsgSession ); |
|
152 CMmsClientMtm* iMmsClient = static_cast<CMmsClientMtm*>( mtmReg->NewMtmL( KUidMsgTypeMultimedia )); |
|
153 |
|
154 iMmsClient->SwitchCurrentEntryL( KMsvGlobalInBoxIndexEntryId ); |
|
155 iMmsClient->CreateMessageL( KUidMsgTypeMultimedia.iUid ); |
|
156 TMsvId newMsgId = iMmsClient->Entry().EntryId(); |
|
157 iMmsClient->AddAddresseeL( aMsgTo ); //to address if need |
|
158 iMmsClient->SetSubjectL(KMessageSubject); |
|
159 |
|
160 CMsvStore* store = iMmsClient->Entry().EditStoreL(); |
|
161 CleanupStack::PushL( store ); |
|
162 |
|
163 CMsvAttachment* attaInfo = NULL; |
|
164 TMsvAttachmentId attaId = KMsvNullIndexEntryId; |
|
165 |
|
166 RFs iFSsession; |
|
167 iFSsession.Connect(); |
|
168 RFile testfile; |
|
169 |
|
170 CMsvMimeHeaders* mimeHeaders = CMsvMimeHeaders::NewL(); |
|
171 CleanupStack::PushL( mimeHeaders ); |
|
172 |
|
173 mimeHeaders->SetContentTypeL( _L8("text") ); |
|
174 mimeHeaders->SetContentSubTypeL( _L8("plain") ); |
|
175 mimeHeaders->SetMimeCharset( KMmsUtf8 ); |
|
176 mimeHeaders->SetSuggestedFilenameL( _L("body.txt") ); |
|
177 |
|
178 _LIT8(KMimeType, "text/plain"); |
|
179 TBufC8<10> mimeType(KMimeType); |
|
180 // CreateAttachment2L will set the content type to attachment Info |
|
181 TInt error = testfile.Open(iFSsession, afilename ,EFileShareReadersOnly | EFileRead); |
|
182 |
|
183 CleanupClosePushL(testfile); |
|
184 |
|
185 attaInfo = CMsvAttachment::NewL(CMsvAttachment::EMsvFile); |
|
186 // attaInfo ownerhip will be transferred to Attachment Manager |
|
187 // It must not be pushed onto the cleanupStack before calling CreateAttachment2L |
|
188 |
|
189 iMmsClient->CreateAttachment2L( |
|
190 *store, // edit store |
|
191 testfile, // open file handle |
|
192 mimeType, |
|
193 *mimeHeaders, |
|
194 attaInfo, |
|
195 attaId); |
|
196 // Now Attachment Manager owns the attaInfo |
|
197 attaInfo = NULL; |
|
198 |
|
199 CleanupStack::PopAndDestroy(); // testfile.Close() |
|
200 CleanupStack::PopAndDestroy(); // mimeHeaders |
|
201 |
|
202 store->CommitL(); |
|
203 CleanupStack::PopAndDestroy(); // store |
|
204 |
|
205 TMsvEntry entry = iMmsClient->Entry().Entry(); |
|
206 |
|
207 entry.SetInPreparation( EFalse ); |
|
208 entry.SetVisible( ETrue ); |
|
209 entry.iDate.HomeTime(); |
|
210 entry.iDetails.Set(aMsgFrom); |
|
211 entry.SetUnread( EFalse ); |
|
212 iMmsClient->Entry().ChangeL( entry ); |
|
213 iMmsClient->SaveMessageL(); |
|
214 |
|
215 delete iMmsClient; |
|
216 delete mtmReg; |
|
217 return newMsgId; |
|
218 } |
|
219 |
|
220 TInt MessagingUtils::RemoveMmsEntryL(CMsvSession* aMsgSession,TMsvId aMsgId) |
|
221 { |
|
222 CMsvEntry* cEntry = aMsgSession->GetEntryL( aMsgId ); |
|
223 CleanupStack::PushL( cEntry ); |
|
224 cEntry->SetEntryL( cEntry->Entry().Parent() ); |
|
225 cEntry->DeleteL( aMsgId ); |
|
226 CleanupStack::PopAndDestroy(); |
|
227 return KErrNone; |
|
228 } |
|
229 |
|
230 TInt MessagingUtils::CreateEmailEntryL(CMsvSession* aMsgSession) |
|
231 { |
|
232 _LIT8(KFrom, "test@email.in"); |
|
233 _LIT(KSubject, "SampleEmail"); |
|
234 _LIT(KBodyContents, "This is a Email entry"); |
|
235 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL( *aMsgSession ); |
|
236 CSmtpClientMtm* iSmtpClient = static_cast<CSmtpClientMtm*>( mtmReg->NewMtmL( KUidMsgTypeSMTP )); |
|
237 |
|
238 TMsvId outboxId = KMsvGlobalOutBoxIndexEntryId; |
|
239 |
|
240 // Set the context to the folder in which message has to be created |
|
241 CMsvEntry* entry = CMsvEntry::NewL(*aMsgSession,outboxId,TMsvSelectionOrdering()); |
|
242 CleanupStack::PushL(entry); |
|
243 entry->SetEntryL(outboxId); |
|
244 |
|
245 CMessAsyncWaiter* waiter = CMessAsyncWaiter::NewL(); |
|
246 CleanupStack::PushL(waiter); |
|
247 |
|
248 TMsvEmailTypeList msvEmailTypeList = 0; |
|
249 TMsvPartList partList = (KMsvMessagePartBody | KMsvMessagePartAttachments); |
|
250 |
|
251 CImEmailOperation* emailOperation = CImEmailOperation::CreateNewL(waiter->iStatus, *aMsgSession, KMsvGlobalOutBoxIndexEntryId, partList, msvEmailTypeList, KUidMsgTypeSMTP); |
|
252 CleanupStack::PushL(emailOperation); |
|
253 waiter->StartAndWait(); |
|
254 |
|
255 TMsvId temp; |
|
256 TPckgC<TMsvId> paramPack(temp); |
|
257 const TDesC8& progBuf = emailOperation->ProgressL(); |
|
258 paramPack.Set(progBuf); |
|
259 TMsvId newMsgId; |
|
260 newMsgId = paramPack(); |
|
261 entry->SetEntryL(newMsgId); |
|
262 |
|
263 CMsvStore* store = entry->EditStoreL(); |
|
264 CleanupStack::PushL(store); |
|
265 CImHeader* emailEntry = CImHeader::NewLC(); |
|
266 //emailEntry->RestoreL(*store); |
|
267 emailEntry->SetFromL((TDesC8&)KFrom); |
|
268 emailEntry->SetSubjectL((TDesC&)KSubject); |
|
269 |
|
270 // Paragraph format layer for the rich text object |
|
271 CParaFormatLayer* paraFormatLayer = CParaFormatLayer::NewL(); |
|
272 CleanupStack::PushL(paraFormatLayer); |
|
273 // Character format layer for the rich text object |
|
274 CCharFormatLayer* charFormatLayer = CCharFormatLayer::NewL(); |
|
275 CleanupStack::PushL(charFormatLayer); |
|
276 CRichText* bodyText = CRichText::NewL(paraFormatLayer, charFormatLayer, CEditableText::EFlatStorage, 256); |
|
277 CleanupStack::PushL(bodyText); |
|
278 // Inserts the contents of a buffer into the document at specified position |
|
279 bodyText->InsertL(0, KBodyContents); |
|
280 store->StoreBodyTextL(*bodyText); |
|
281 emailEntry->StoreL(*store); |
|
282 // Store the changes permanently |
|
283 store->CommitL(); |
|
284 CleanupStack::PopAndDestroy(8,entry); |
|
285 |
|
286 |
|
287 delete iSmtpClient; |
|
288 delete mtmReg; |
|
289 return newMsgId; |
|
290 } |
|
291 |