00001
00002
00003
00004
00005
00006 #include "SMSExampleMtmsEngine.h"
00007 #include "SmsEnginePanics.pan"
00008
00009
00010 #include <f32file.h>
00011 #include <mmsclient.h>
00012 #include <mtclreg.h>
00013 #include <mtmdef.h>
00014 #include <smsclnt.h>
00015 #include <smscmds.h>
00016 #include <smutset.h>
00017 #include <smuthdr.h>
00018 #include <txtrich.h>
00019 #include <eikenv.h>
00020
00021 const TInt KMessageAddressLength = 100;
00022 const TInt KMessageBodySize = 512;
00023 const TInt KArrayGranularity=10;
00024
00025 _LIT(KSenderJohnDoe,"*Unknown*");
00026
00027 CSMSExampleMtmsEngine* CSMSExampleMtmsEngine::NewL(
00028 MSMSExampleMtmsEngineObserver& aObserver)
00029 {
00030 CSMSExampleMtmsEngine* self =
00031 new (ELeave) CSMSExampleMtmsEngine(aObserver);
00032 CleanupStack::PushL(self);
00033 self->ConstructL();
00034 CleanupStack::Pop(self);
00035 return self;
00036 }
00037
00038 CSMSExampleMtmsEngine::CSMSExampleMtmsEngine(
00039 MSMSExampleMtmsEngineObserver& aObserver)
00040 : CActive(0),
00041 iObserver(aObserver),
00042 iSmsId(KMsvNullIndexEntryId)
00043 {
00044
00045 }
00046
00047 void CSMSExampleMtmsEngine::ConstructL()
00048 {
00049 CActiveScheduler::Add(this);
00050
00051
00052 iEntrySelection = new (ELeave) CMsvEntrySelection;
00053
00054
00055
00056
00057
00058
00059 iSession = CMsvSession::OpenAsyncL(*this);
00060 }
00061
00062
00063 void CSMSExampleMtmsEngine::CreateMtmClientL()
00064 {
00065
00066 iClientMtmReg = CClientMtmRegistry::NewL(*iSession);
00067
00068
00069
00070 iSmsMtm = static_cast<CSmsClientMtm*>(iClientMtmReg->NewMtmL(KUidMsgTypeSMS));
00071 }
00072
00073 CSMSExampleMtmsEngine::~CSMSExampleMtmsEngine()
00074 {
00075 Cancel();
00076 delete iMsvOper;
00077 delete iEntrySelection;
00078 delete iSmsMtm;
00079 delete iClientMtmReg;
00080 delete iSession;
00081 }
00082
00083 void CSMSExampleMtmsEngine::DoCancel()
00084 {
00085 if (iMsvOper)
00086 {
00087 iMsvOper->Cancel();
00088 delete iMsvOper;
00089 iMsvOper = NULL;
00090 }
00091 }
00092
00093 void CSMSExampleMtmsEngine::RunL()
00094 {
00095 iObserver.HandleMessageSentL(iStatus.Int());
00096 }
00097
00098
00099
00100
00101 void CSMSExampleMtmsEngine::MoveToFolderL( TMsvId aMessageId, TMsvId aFolder )
00102 {
00103 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00104 TMsvSelectionOrdering selection;
00105 selection.SetShowInvisibleEntries(ETrue);
00106 CMsvEntry* parentEntry = CMsvEntry::NewL( iSmsMtm->Session(),
00107 iSmsMtm->Entry().Entry().Parent(), selection );
00108 CleanupStack::PushL(parentEntry);
00109
00110 parentEntry->MoveL( aMessageId, aFolder );
00111 CleanupStack::PopAndDestroy(parentEntry);
00112 }
00113
00114
00115
00116 void CSMSExampleMtmsEngine::DeleteMessageL( TMsvId aMessageId )
00117 {
00118 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00119 TMsvId parent = iSmsMtm->Entry().Entry().Parent();
00120
00121 iSmsMtm->SwitchCurrentEntryL( parent );
00122 iSmsMtm->Entry().DeleteL( aMessageId );
00123 }
00124
00125
00126 void CSMSExampleMtmsEngine::GetMessageAddressL( TMsvId aMessageId,
00127 TDes& aAddress )
00128 {
00129 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00130
00131
00132 iSmsMtm->LoadMessageL();
00133
00134 CSmsHeader& header = iSmsMtm->SmsHeader();
00135
00136 aAddress.Append( header.FromAddress() );
00137
00138
00139 }
00140
00141
00142
00143 TBool CSMSExampleMtmsEngine::GetMessageL( TMsvId aMessageId, TDes& aMessage)
00144 {
00145 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00146
00147 if ( iSmsMtm->Entry().HasStoreL() )
00148 {
00149
00150 CMsvStore* store = iSmsMtm->Entry().ReadStoreL();
00151 CleanupStack::PushL(store);
00152
00153 if (store->HasBodyTextL())
00154 {
00155 CRichText* richText = CRichText::NewL(
00156 CEikonEnv::Static()->SystemParaFormatLayerL(),
00157 CEikonEnv::Static()->SystemCharFormatLayerL());
00158 richText->Reset();
00159 CleanupStack::PushL(richText);
00160
00161
00162 store->RestoreBodyTextL(*richText);
00163 const TInt length = richText->DocumentLength();
00164 TBuf<KMessageBodySize> message;
00165
00166
00167 if ( length >= KMessageBodySize )
00168 {
00169 message.Append( richText->Read(0, KMessageBodySize -1) );
00170 }
00171 else
00172 {
00173 message.Append( richText->Read(0, length) );
00174 }
00175
00176 aMessage.Append( message );
00177
00178 CleanupStack::PopAndDestroy(richText);
00179 }
00180 CleanupStack::PopAndDestroy(store);
00181 }
00182 else
00183 {
00184 return EFalse;
00185 }
00186
00187 return ETrue;
00188 }
00189
00190
00191
00192 TBool CSMSExampleMtmsEngine::GetMessageIndexBodyTextL( TMsvId aMessageId,
00193 TDes& aMessage)
00194 {
00195 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00196
00197 aMessage.Append(iSmsMtm->Entry().Entry().iDescription );
00198 return ETrue;
00199 }
00200
00201
00202
00203 void CSMSExampleMtmsEngine::CopyMessageL( TMsvId aMessageId, TMsvId aFolder )
00204 {
00205 iSmsMtm->SwitchCurrentEntryL( aMessageId );
00206 TMsvSelectionOrdering selection;
00207 selection.SetShowInvisibleEntries(ETrue);
00208 CMsvEntry* parentEntry = CMsvEntry::NewL( iSmsMtm->Session(),
00209 iSmsMtm->Entry().Entry().Parent(), selection );
00210 CleanupStack::PushL(parentEntry);
00211
00212 parentEntry->CopyL( aMessageId, aFolder );
00213 CleanupStack::PopAndDestroy();
00214 }
00215
00216
00217 RArray<TMsvId>* CSMSExampleMtmsEngine::GetMessageIds()
00218 {
00219 return iIdArray;
00220 }
00221
00222
00223
00224
00225
00226 void CSMSExampleMtmsEngine::GetFolderSMSMessageInformationL(
00227 TMsvId aFolderID,
00228 CDesCArrayFlat*& aAddresses,
00229 CDesCArrayFlat*& aMessages )
00230 {
00231
00232 iSmsMtm->SwitchCurrentEntryL( aFolderID );
00233 CMsvEntry& entry = iSmsMtm->Entry();
00234
00235
00236
00237 CMsvEntrySelection* entries = entry.ChildrenWithMtmL(KUidMsgTypeSMS);
00238
00239 CDesCArrayFlat* arrayAddr =
00240 new (ELeave) CDesCArrayFlat( KArrayGranularity );
00241 CDesCArrayFlat* arrayMsgBody =
00242 new (ELeave) CDesCArrayFlat( KArrayGranularity );
00243
00244 iIdArray = new (ELeave) RArray<TMsvId>;
00245
00246 for (TInt i = 0; i < entries->Count(); i++ )
00247 {
00248 TBuf<KMessageBodySize> body;
00249 TBuf<KMessageAddressLength> address;
00250
00251
00252
00253
00254 if ( GetMessageIndexBodyTextL( (*entries)[i], body ) )
00255 {
00256 iIdArray->Append( (*entries)[i] );
00257 arrayMsgBody->AppendL ( body );
00258
00259
00260
00261 TRAPD(err, GetMessageAddressL( (*entries)[i], address ) );
00262 if( err == KErrNone)
00263 arrayAddr->AppendL ( address );
00264 else
00265 arrayAddr->AppendL ( KSenderJohnDoe );
00266 }
00267 }
00268
00269
00270 entries->Reset();
00271 delete entries;
00272 entries = 0;
00273
00274 aAddresses = arrayAddr;
00275 aMessages = arrayMsgBody;
00276 }
00277
00278
00279 void CSMSExampleMtmsEngine::HandleSessionEventL(TMsvSessionEvent aEvent,
00280 TAny* ,
00281 TAny* ,
00282 TAny* )
00283 {
00284 switch (aEvent)
00285 {
00286
00287 case EMsvServerReady:
00288 CreateMtmClientL();
00289 break;
00290
00291 default:
00292
00293 break;
00294 }
00295 }