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