00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "SMSEngine.h"
00031 #include <msvids.h>
00032 #include <txtrich.h>
00033 #include <smut.h>
00034 #include <eikdef.h>
00035 #include <eikenv.h>
00036 #include <rsendas.h>
00037 #include <rsendasmessage.h>
00038
00039 #include "SmsEnginePanics.pan"
00040
00041
00042 #ifdef __WINS__
00043 const TMsvId KObservedFolderId = KMsvDraftEntryId;
00044 #else
00045 const TMsvId KObservedFolderId = KMsvGlobalInBoxIndexEntryId;
00046 #endif
00047
00048 const TMsvId KInbox = KMsvGlobalInBoxIndexEntryId;
00049 const TMsvId KOutbox = KMsvGlobalOutBoxIndexEntryId;
00050 const TMsvId KDrafts = KMsvDraftEntryId;
00051
00052 const TInt KDelayTime = 1000*3000;
00053 const TInt KErrMsgLength = 20;
00054
00055 _LIT(KEmptyMsg,"");
00056
00057
00058 EXPORT_C CSmsEngine* CSmsEngine::NewL(MSmsEngineObserver& aObserver)
00059 {
00060 CSmsEngine* self = CSmsEngine::NewLC(aObserver);
00061 CleanupStack::Pop( self );
00062 return self;
00063 }
00064
00065 EXPORT_C CSmsEngine* CSmsEngine::NewLC(MSmsEngineObserver& aObserver)
00066 {
00067 CSmsEngine* self = new( ELeave ) CSmsEngine(aObserver);
00068 CleanupStack::PushL( self );
00069 self->ConstructL();
00070 return self;
00071 }
00072
00073 EXPORT_C CSmsEngine::~CSmsEngine()
00074 {
00075 delete iMsvEntry;
00076 iMsvEntry = NULL;
00077
00078 delete iMsvSession;
00079 iMsvSession = NULL;
00080
00081 delete iEngine;
00082 iEngine = NULL;
00083 }
00084
00085 CSmsEngine::CSmsEngine(MSmsEngineObserver& aObserver) : iObserver(aObserver)
00086 {
00087 }
00088
00089 void CSmsEngine::ConstructL()
00090 {
00091 iAutomaticDelete = EFalse;
00092
00093 iListeningForIncoming = ETrue;
00094
00095 iEngine = CSMSExampleMtmsEngine::NewL(*this);
00096
00097
00098 iMsvSession = CMsvSession::OpenAsyncL(*this);
00099 }
00100
00101
00102 void CSmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1,
00103 TAny* aArg2, TAny* )
00104 {
00105 switch (aEvent)
00106 {
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 case EMsvServerReady:
00137
00138 if (!iMsvEntry)
00139 {
00140 iMsvEntry = CMsvEntry::NewL(*iMsvSession, KInbox,
00141 TMsvSelectionOrdering());
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 }
00192 break;
00193
00194 case EMsvEntriesCreated:
00195
00196 if (aArg2 && *(static_cast<TMsvId*>(aArg2)) == KObservedFolderId)
00197 {
00198 CMsvEntrySelection* entries =
00199 static_cast<CMsvEntrySelection*>(aArg1);
00200 if( entries->Count() >= 1 )
00201 {
00202 iNewMessageId = entries->At(0);
00203 }
00204 else
00205 {
00206 Panic(ESmsEngineInternal);
00207 }
00208 }
00209 break;
00210
00211 case EMsvEntriesChanged:
00212
00213
00214
00215 if (aArg2 && *(static_cast<TMsvId*>(aArg2)) == KObservedFolderId
00216 && iListeningForIncoming )
00217 {
00218 CMsvEntrySelection* entries =
00219 static_cast<CMsvEntrySelection*>(aArg1);
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 if( entries->Count() < 1 )
00237 {
00238 Panic(ESmsEngineInternal);
00239 }
00240 else if (iNewMessageId == entries->At(0))
00241 {
00242
00243 if( !iMsvEntry )
00244 {
00245 Panic(ESmsEngineNotInitialized);
00246 return;
00247 }
00248
00249
00250 iMsvEntry->SetEntryL(iNewMessageId);
00251
00252
00253
00254
00255 if ( iMsvEntry->Entry().iMtm != KUidMsgTypeSMS ||
00256 !iMsvEntry->Entry().Complete() )
00257 {
00258 return;
00259 }
00260
00261
00262 CMsvStore* store = iMsvEntry->ReadStoreL();
00263 CleanupStack::PushL(store);
00264
00265
00266 TBuf<KSmsMessageLength> iAddress(
00267 iMsvEntry->Entry().iDetails
00268 );
00269
00270 if (store->HasBodyTextL())
00271 {
00272 CRichText* richText = CRichText::NewL(
00273 CEikonEnv::Static()->SystemParaFormatLayerL(),
00274 CEikonEnv::Static()->SystemCharFormatLayerL());
00275 CleanupStack::PushL(richText);
00276 store->RestoreBodyTextL(*richText);
00277 const TInt length = richText->DocumentLength();
00278
00279 TBuf<KSmsMessageLength> number;
00280 TPtrC ptr = richText->Read(0, length);
00281
00282
00283 iMessage.Copy(ptr);
00284
00285 CleanupStack::PopAndDestroy(richText);
00286
00287
00288 iObserver.MessageReceived(iAddress, iMessage);
00289
00290 }
00291 else
00292 {
00293 iObserver.MessageReceived(iAddress, KEmptyMsg);
00294 }
00295
00296 CleanupStack::PopAndDestroy(store);
00297
00298
00299 if ( iAutomaticDelete )
00300 {
00301
00302
00303
00304 User::After(KDelayTime);
00305
00306 iMsvSession->RemoveEntry(iNewMessageId);
00307
00308 iObserver.MessageDeleted();
00309 }
00310 }
00311 }
00312 break;
00313
00314 default:
00315 break;
00316 }
00317 }
00318
00319
00320 void CSmsEngine::HandleMessageSentL(TInt aError)
00321 {
00322 if (aError == KErrNone)
00323 {
00324 iObserver.MessageSent();
00325 }
00326 else
00327 {
00328 iObserver.SmsEngineError(aError);
00329 }
00330 }
00331
00332
00333 EXPORT_C void CSmsEngine::SendSmsL(const TDesC& aAddr, const TDesC& aMsg)
00334 {
00335 TInt err (KErrNone);
00336
00337 TRAP(err, SendSmsInThirdEditionL(aAddr, aMsg));
00338
00339 if( err )
00340 {
00341 iObserver.SmsEngineError(err);
00342 }
00343 else
00344 {
00345 iObserver.SendingMessage();
00346 }
00347 }
00348
00349
00350 void CSmsEngine::SendSmsInThirdEditionL(const TDesC& aAddr, const TDesC& aMsg)
00351 {
00352 RSendAs sendAs;
00353 User::LeaveIfError(sendAs.Connect());
00354 CleanupClosePushL(sendAs);
00355
00356 RSendAsMessage sendAsMessage;
00357 sendAsMessage.CreateL(sendAs, KUidMsgTypeSMS);
00358 CleanupClosePushL(sendAsMessage);
00359
00360
00361 sendAsMessage.AddRecipientL(aAddr, RSendAsMessage::ESendAsRecipientTo);
00362 sendAsMessage.SetBodyTextL(aMsg);
00363
00364
00365 sendAsMessage.SendMessageAndCloseL();
00366
00367
00368 CleanupStack::Pop();
00369
00370
00371 CleanupStack::PopAndDestroy();
00372 }
00373
00374
00375 EXPORT_C void CSmsEngine::ListenforIncomingSms(TBool aListening)
00376 {
00377
00378
00379 iListeningForIncoming = aListening;
00380 }
00381
00382
00383 EXPORT_C void CSmsEngine::SetAutomaticDeletetion(TBool aDeletion)
00384 {
00385
00386
00387 iAutomaticDelete = aDeletion;
00388 }
00389
00390
00391 EXPORT_C void CSmsEngine::GetFolderSMSMessageInformationL(TMsvId aFolderID,
00392 CDesCArrayFlat*& aAddresses,
00393 CDesCArrayFlat*& aMessages)
00394 {
00395 iEngine->GetFolderSMSMessageInformationL(aFolderID, aAddresses, aMessages);
00396 }
00397
00398
00399
00400 EXPORT_C RArray<TMsvId>* CSmsEngine::GetMessageIds()
00401 {
00402 return iEngine->GetMessageIds();
00403 }
00404
00405
00406 EXPORT_C void CSmsEngine::CopyMessageL( TMsvId aMessageId, TMsvId aFolder )
00407 {
00408 return iEngine->CopyMessageL(aMessageId, aFolder);
00409 }
00410
00411
00412 EXPORT_C void CSmsEngine::MoveToFolderL( TMsvId aMessageId, TMsvId aFolder )
00413 {
00414 iEngine->MoveToFolderL(aMessageId, aFolder);
00415 }
00416
00417
00418 EXPORT_C void CSmsEngine::DeleteMessageL( TMsvId aMessageId )
00419 {
00420 iEngine->DeleteMessageL(aMessageId);
00421 }
00422
00423