examples/ForumNokia/S60_3rd_Edition_TextMTM_Example/modules/Util/src/txut.cpp

00001 // TXUT.CPP
00002 //
00003 // � 2004 Nokia Corporation.  All rights reserved.
00004 //
00005 
00006 #include <msvstd.h>             // TMsvEntry
00007 #include <msvstore.h>   // CMsvStore
00008 #include <MSVUIDS.H>    //KUidMsvFolderEntry
00009 #include <centralrepository.h>
00010 #include "txut.h"
00011 
00012 // system includes
00013 #include <msvuids.h>    //KUidMsvFolderEntry
00014 const TUint32 KNullId = 0x00000000;
00015 const TUint32 KIncrementAccount = 0x00100000;
00016 const TInt KMaxAccount = 2048;
00017 const TUint32 KDefaultServiceId = 0x80000000; // set top bit
00018 const TUint32 EAccountMask = 0x800FFFFF;
00019 
00020 // Key IDs for particular text mtm settings in repository
00021 enum
00022         {
00023         EServiceId = 0,
00024         EFolderSettingId = 1
00025         };
00026 // user includes
00027 // standard NewL
00028 EXPORT_C CMTMTxtSettings* CMTMTxtSettings::NewL()
00029         {
00030         CMTMTxtSettings* self = new (ELeave) CMTMTxtSettings();
00031         CleanupStack::PushL(self);
00032         self->ConstructL();
00033         CleanupStack::Pop();
00034         return self;
00035         }
00036 
00037 // destructor
00038 CMTMTxtSettings::~CMTMTxtSettings()
00039         {
00040         delete iRepository;
00041         }
00042 
00043 // delete all settings for specified service
00044 EXPORT_C void CMTMTxtSettings::DeleteSettingsL(TMsvId aServiceId)
00045         {
00046         TUint32 serviceKey = FindAccountL(aServiceId);
00047         DeleteSettingL(serviceKey);
00048         DeleteSettingL(serviceKey + EFolderSettingId);
00049         }
00050 
00058 EXPORT_C void CMTMTxtSettings::LoadSettingsL(TMsvId aServiceId, TMTMTxtSettings& aSettings) const
00059         {
00060         TUint32 serviceKey = FindAccountL(aServiceId);
00061         TFileName rootFolder;
00062         User::LeaveIfError(iRepository->Get(serviceKey + EFolderSettingId, rootFolder));
00063         aSettings.SetRootFolder(rootFolder);
00064         }
00065 
00073 EXPORT_C void CMTMTxtSettings::SaveSettingsL(TMsvId aServiceId, const TMTMTxtSettings& aSettings)
00074         {
00075         TUint32 accountId = 0;
00076         TInt error = 0;
00077         // see if account already exists
00078         TRAP(error, accountId = FindAccountL(aServiceId));
00079         if (error != KErrUnknown) User::LeaveIfError(error);
00080         // doesn't already exist, so get id of new account
00081         if (error == KErrUnknown) accountId = GetNextAccountSlotL();
00082 
00083         TRAP( error,
00084                 // Save settings to CenRep
00085                 CreateOrSetL(accountId, static_cast<TInt>(aServiceId));
00086                 CreateOrSetL(accountId + EFolderSettingId, aSettings.RootFolder());
00087                 );
00088         if (error != KErrNone)
00089                 {
00090                 // saving settings to CenRep failed, so cleanup account and leave
00091                 DeleteSettingsL(aServiceId);
00092                 User::Leave(error);
00093                 }
00094         }
00095 
00101 EXPORT_C void CMTMTxtSettings::SetDefaultServiceL(TMsvId aService)
00102         {
00103         CreateOrSetL(KDefaultServiceId, static_cast<TInt>(aService));
00104         }
00105 
00113 EXPORT_C TMsvId CMTMTxtSettings::DefaultServiceL() const
00114         {
00115         // Get the service Id from CenRep
00116         TInt temp = 0;
00117         User::LeaveIfError(iRepository->Get(KDefaultServiceId, temp));
00118         return static_cast<TMsvId>(temp);
00119         }
00120 
00124 EXPORT_C void CMTMTxtSettings::DeleteDefaultServiceSettingL()
00125         {
00126         DeleteSettingL(KDefaultServiceId);
00127         }
00128 
00129 // create cenrep repository in which to store textmtm settings
00130 void CMTMTxtSettings::ConstructL()
00131         {
00132 
00133 //#ifdef __WINS__
00134 //      const TUid KUidMtm = { KUidMsgTypeText };
00135 
00136 //#else
00137         const TInt KUidDefaultMtmRepositoryFileValue = 0x10274556;
00138         const TUid KUidMtm = { KUidDefaultMtmRepositoryFileValue };
00139 //#endif
00140 
00141         iRepository = CRepository::NewL(KUidMtm);
00142 
00143         }
00144 
00145 // sets (or creates if it does not already exist) a string key
00146 void CMTMTxtSettings::CreateOrSetL(TUint aKey, const TDesC& aValue)
00147         {
00148         TInt error = iRepository->Set(aKey, aValue);
00149         if (error == KErrNotFound)
00150                 {
00151                 // setting does not exist, so create it
00152                 User::LeaveIfError(iRepository->Create(aKey, aValue));
00153                 }
00154         else
00155                 {
00156                 User::LeaveIfError(error);
00157                 }
00158         }
00159 
00160 // sets (or creates if it does not already exist) an integer key
00161 void CMTMTxtSettings::CreateOrSetL(TUint aKey, TInt aValue)
00162         {
00163         TInt error = iRepository->Set(aKey, aValue);
00164         if (error == KErrNotFound)
00165                 {
00166                 // setting does not exist, so create it
00167                 User::LeaveIfError(iRepository->Create(aKey, aValue));
00168                 }
00169         else
00170                 {
00171                 User::LeaveIfError(error);
00172                 }
00173         }
00174 
00175 // Leaves with KErrUnknown if account does not exist
00176 TUint32 CMTMTxtSettings::FindAccountL(TMsvId aService) const
00177         {
00178         RArray<TUint32> accounts;
00179         CleanupClosePushL(accounts);
00180         TInt error = iRepository->FindEqL(KNullId, static_cast<TUint32>(EAccountMask), static_cast<TInt>(aService), accounts);
00181         if (error == KErrNotFound)
00182                 {
00183                 // account does not exist
00184                 User::Leave(KErrUnknown);
00185                 }
00186         else
00187                 {
00188                 User::LeaveIfError(error);
00189                 }
00190 
00191         if (accounts.Count()>1)
00192                 {
00193                 // There should be only one account for the service
00194                 User::Leave(KErrOverflow);
00195                 }
00196 
00197         TUint32 account = accounts[0];
00198         CleanupStack::PopAndDestroy(&accounts);
00199         return account;
00200         }
00201 //      TMTMTxtSettings: service settings storage
00202 //
00203 
00204 EXPORT_C void TMTMTxtSettings::ExternalizeL( RMsvWriteStream& aWriteStream ) const
00205         {
00206         // store iRootFolder
00207         aWriteStream << iRootFolder;
00208         return;
00209         }
00210 
00211 EXPORT_C void TMTMTxtSettings::InternalizeL( RMsvReadStream& aReadStream )
00212         {
00213         aReadStream >> iRootFolder;
00214         return;
00215         }
00216 
00217 EXPORT_C void TMTMTxtSettings::StoreL(CMsvStore& aMsvStore) const
00218         {
00219         RMsvWriteStream out;
00220         out.AssignLC( aMsvStore, KUidTxtMTMSettings ); // pushes 'out' to the stack
00221         ExternalizeL(out);
00222         out.CommitL();
00223         CleanupStack::PopAndDestroy();
00224         }
00225 
00226 // get a base (account) id to identify all keys for a particular service
00227 TUint CMTMTxtSettings::GetNextAccountSlotL()
00228         {
00229         TUint32 accountId = KNullId;
00230         TInt serviceId = 0;
00231         TInt error = 0;
00232         TBool found = EFalse;
00233 
00234         for (TInt count = 0; count < KMaxAccount; ++count)
00235                 {
00236                 accountId = accountId + KIncrementAccount;
00237                 error = iRepository->Get(accountId, serviceId);
00238                 if (error == KErrNotFound)
00239                         {
00240                         found = ETrue;
00241                         break;
00242                         }
00243                 else
00244                         {
00245                         User::LeaveIfError(error);
00246                         }
00247                 }
00248 
00249         if (found == EFalse)
00250                 {
00251                 // No empty slot available
00252                 User::Leave(KErrNotFound);
00253                 }
00254 
00255         return accountId;
00256         }
00257 
00258 // delete a setting, and don't give an error if it doesn't exist
00259 void CMTMTxtSettings::DeleteSettingL(TUint32 settingId)
00260         {
00261         TInt error = iRepository->Delete(settingId);
00262         if (error != KErrNotFound)
00263                 {
00264                 User::LeaveIfError(error);
00265                 }
00266         }
00267 EXPORT_C void TMTMTxtSettings::RestoreL(const CMsvStore& aMessageStore )
00268 
00269         {
00270         if (aMessageStore.IsPresentL(KUidTxtMTMSettings))
00271                 {
00272                 RMsvReadStream in;
00273                 in.OpenLC( aMessageStore, KUidTxtMTMSettings ); // pushes 'in' to the stack
00274                 InternalizeL(in);
00275                 CleanupStack::PopAndDestroy();
00276                 }
00277         }
00278 
00279 //
00280 // TxtUtils: Generic static utility functions
00281 //
00282 
00283 EXPORT_C void TxtUtils::GetEntryFileNameL(TFileName& aFileName, TMsvEntry& aEntry)
00284 // Create absolute file name: default path + aEntry.iDetails + aEntry.iDescription
00285         {
00286         CMTMTxtSettings* settings = CMTMTxtSettings::NewL();
00287         CleanupStack::PushL(settings);
00288         TMTMTxtSettings root;
00289         settings->LoadSettingsL(aEntry.iServiceId, root);
00290         CleanupStack::PopAndDestroy(); //settings
00291         aFileName = root.RootFolder();
00292         aFileName.Append(aEntry.iDetails);
00293         aFileName.Append(aEntry.iDescription);
00294         if (aEntry.iType == KUidMsvFolderEntry)
00295                 aFileName.Append(KPathDelimiter);
00296         }
00297 
00298 EXPORT_C void TxtUtils::FindFileL(const TDesC& aFileName, const TDesC& aLocation, TFileName& aRetVal)
00299     {
00300     RFs fs;
00301     User::LeaveIfError(fs.Connect());
00302     CleanupClosePushL(fs);
00303     TFindFile finder(fs);
00304     User::LeaveIfError(finder.FindByDir(aFileName, aLocation));
00305     aRetVal = finder.File();
00306     CleanupStack::PopAndDestroy();
00307     }
00308 
00309 //
00310 // CMsvOpWait
00311 //      Allows a synchronous wait on a operation
00312 //
00313 EXPORT_C CMsvOpWait* CMsvOpWait::NewLC(TInt aPriority)
00314         {
00315         CMsvOpWait* self = new (ELeave) CMsvOpWait(aPriority);
00316         CleanupStack::PushL(self);
00317         return self;
00318         }
00319 
00320 CMsvOpWait::CMsvOpWait(TInt aPriority)
00321 : CActive(aPriority)
00322         {
00323         CActiveScheduler::Add(this);
00324         }
00325 
00326 EXPORT_C CMsvOpWait::~CMsvOpWait()
00327         {
00328         Cancel();
00329         }
00330 
00331 EXPORT_C void CMsvOpWait::Start()
00332         {
00333         SetActive();
00334         }
00335 
00336 void CMsvOpWait::RunL()
00337         {
00338         CActiveScheduler::Stop();
00339         }
00340 
00341 void CMsvOpWait::DoCancel()
00342         {
00343         TRequestStatus* s=&iStatus;
00344         User::RequestComplete(s, KErrCancel);
00345         }
00346 //
00347 // CMsvCompOperation
00348 //      An operation which is already completed on construction
00349 //
00350 EXPORT_C CMsvCompOperation* CMsvCompOperation::NewL(CMsvSession& aSession, TUid aMtm,
00351         const TDesC8& aProgress, TMsvId aService, TRequestStatus& aObserverRequestStatus, TInt aError)
00352 // aMtm and aService set CMsvOperation protected data members
00353 // aProgress is progress information
00354 // aObserverRequestStatus is the active object to signal on completion
00355 // aError is the error code from the relevant operation
00356         {
00357         CMsvCompOperation* self = new (ELeave) CMsvCompOperation(aSession, aObserverRequestStatus);
00358         CleanupStack::PushL(self);
00359         self->ConstructL(aMtm, aError, aProgress, aService);
00360         CleanupStack::Pop(self);
00361         return self;
00362         }
00363 
00364 CMsvCompOperation::CMsvCompOperation(CMsvSession& aSession, TRequestStatus& aObserverRequestStatus)
00365 : CMsvOperation(aSession, EPriorityStandard, aObserverRequestStatus)
00366         {
00367         }
00368 
00369 EXPORT_C CMsvCompOperation::~CMsvCompOperation()
00370         {
00371         Cancel();
00372         delete iProgress;
00373         }
00374 
00375 void CMsvCompOperation::ConstructL(TUid aMtm, TInt aError, const TDesC8& aProgress, TMsvId aService)
00376         {
00377         iProgress = HBufC8::NewL(aProgress.Length());
00378         *iProgress = aProgress;
00379         iMtm=aMtm;
00380         iService=aService;
00381         //
00382         CActiveScheduler::Add(this);
00383         iStatus = KRequestPending;
00384         SetActive();
00385         //
00386         TRequestStatus* pstat=&iStatus;
00387         User::RequestComplete(pstat, aError);
00388         iObserverRequestStatus = KRequestPending;
00389         }
00390 
00391 const TDesC8& CMsvCompOperation::ProgressL()
00392         {
00393         return *iProgress;
00394         }
00395 
00396 void CMsvCompOperation::DoCancel()
00397         {
00398         // does nothing as iStatus has already been completed in in ConstructL()
00399         }
00400 
00401 void CMsvCompOperation::RunL()
00402         {
00403         TRequestStatus* pstat=&iObserverRequestStatus;
00404         User::RequestComplete(pstat, iStatus.Int());
00405         }

Generated by  doxygen 1.6.2