examples/Messaging/TextMTM/txut/TXUT.CPP

00001 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #include <msvstd.h>             // TMsvEntry
00017 #include <msvstore.h>   // CMsvStore
00018 #include <msvuids.h>    //KUidMsvFolderEntry
00019 #include <centralrepository.h>
00020 
00021 #include "TXUT.H"
00022 
00023 const TUint32 KNullId = 0x00000000;
00024 const TUint32 KIncrementAccount = 0x00100000;
00025 const TInt KMaxAccount = 2048;
00026 const TUint32 KDefaultServiceId = 0x80000000; // set top bit
00027 const TUint32 EAccountMask = 0x800FFFFF;
00028 
00029 // Key IDs for particular text mtm settings in repository
00030 enum 
00031         {
00032         EServiceId = 0,
00033         EFolderSettingId = 1
00034         };
00035 
00036 // standard NewL
00037 EXPORT_C CMTMTxtSettings* CMTMTxtSettings::NewL()
00038         {
00039         CMTMTxtSettings* self = new (ELeave) CMTMTxtSettings();
00040         CleanupStack::PushL(self);
00041         self->ConstructL();
00042         CleanupStack::Pop();
00043         return self;    
00044         }
00045 
00046 // destructor   
00047 CMTMTxtSettings::~CMTMTxtSettings()
00048         {
00049         delete iRepository;
00050         }
00051 
00052 // delete all settings for specified service
00053 EXPORT_C void CMTMTxtSettings::DeleteSettingsL(TMsvId aServiceId)
00054         {
00055         TUint32 serviceKey = FindAccountL(aServiceId);
00056         DeleteSettingL(serviceKey);
00057         DeleteSettingL(serviceKey + EFolderSettingId);
00058         }       
00059 
00067 EXPORT_C void CMTMTxtSettings::LoadSettingsL(TMsvId aServiceId, TMTMTxtSettings& aSettings) const
00068         {
00069         TUint32 serviceKey = FindAccountL(aServiceId);
00070         TFileName rootFolder;
00071         User::LeaveIfError(iRepository->Get(serviceKey + EFolderSettingId, rootFolder));
00072         aSettings.SetRootFolder(rootFolder);
00073         }
00074 
00082 EXPORT_C void CMTMTxtSettings::SaveSettingsL(TMsvId aServiceId, const TMTMTxtSettings& aSettings)
00083         {
00084         TUint32 accountId = 0;
00085         TInt error = 0;
00086         // see if account already exists
00087         TRAP(error, accountId = FindAccountL(aServiceId));
00088         if (error != KErrUnknown) User::LeaveIfError(error);
00089         // doesn't already exist, so get id of new account
00090         if (error == KErrUnknown) accountId = GetNextAccountSlotL();
00091                 
00092         TRAP( error,
00093                 // Save settings to CenRep              
00094                 CreateOrSetL(accountId, static_cast<TInt>(aServiceId));
00095                 CreateOrSetL(accountId + EFolderSettingId, aSettings.RootFolder());             
00096                 );
00097         if (error != KErrNone)
00098                 {
00099                 // saving settings to CenRep failed, so cleanup account and leave
00100                 DeleteSettingsL(aServiceId);
00101                 User::Leave(error);
00102                 }       
00103         }
00104 
00110 EXPORT_C void CMTMTxtSettings::SetDefaultServiceL(TMsvId aService)
00111         {
00112         CreateOrSetL(KDefaultServiceId, static_cast<TInt>(aService));
00113         }
00114 
00122 EXPORT_C TMsvId CMTMTxtSettings::DefaultServiceL() const
00123         {
00124         // Get the service Id from CenRep
00125         TInt temp = 0;
00126         User::LeaveIfError(iRepository->Get(KDefaultServiceId, temp));                          
00127         return static_cast<TMsvId>(temp);
00128         }
00129 
00133 EXPORT_C void CMTMTxtSettings::DeleteDefaultServiceSettingL()
00134         {
00135         DeleteSettingL(KDefaultServiceId);                              
00136         }
00137 
00138 // create cenrep repository in which to store textmtm settings 
00139 void CMTMTxtSettings::ConstructL()
00140         {
00141         iRepository = CRepository::NewL(KUidMsgTypeText);
00142         }
00143 
00144 // sets (or creates if it does not already exist) a string key
00145 void CMTMTxtSettings::CreateOrSetL(TUint aKey, const TDesC& aValue)
00146         {
00147         TInt error = iRepository->Set(aKey, aValue);
00148         if (error == KErrNotFound)
00149                 {
00150                 // setting does not exist, so create it
00151                 User::LeaveIfError(iRepository->Create(aKey, aValue));
00152                 }
00153         else
00154                 {
00155                 User::LeaveIfError(error);
00156                 }               
00157         }
00158 
00159 // sets (or creates if it does not already exist) an integer key
00160 void CMTMTxtSettings::CreateOrSetL(TUint aKey, TInt aValue)
00161         {
00162         TInt error = iRepository->Set(aKey, aValue);
00163         if (error == KErrNotFound)
00164                 {
00165                 // setting does not exist, so create it
00166                 User::LeaveIfError(iRepository->Create(aKey, aValue));
00167                 }
00168         else
00169                 {
00170                 User::LeaveIfError(error);
00171                 }               
00172         }
00173 
00174 // Leaves with KErrUnknown if account does not exist
00175 TUint32 CMTMTxtSettings::FindAccountL(TMsvId aService) const
00176         {
00177         RArray<TUint32> accounts;
00178         CleanupClosePushL(accounts);    
00179         TInt error = iRepository->FindEqL(KNullId, static_cast<TUint32>(EAccountMask), static_cast<TInt>(aService), accounts);
00180         if (error == KErrNotFound)
00181                 {
00182                 // account does not exist
00183                 User::Leave(KErrUnknown);               
00184                 }
00185         else
00186                 {
00187                 User::LeaveIfError(error);      
00188                 }
00189         
00190         if (accounts.Count()>1)
00191                 {
00192                 // There should be only one account for the service
00193                 User::Leave(KErrOverflow);
00194                 }
00195         
00196         TUint32 account = accounts[0];
00197         CleanupStack::PopAndDestroy(&accounts);
00198         return account;
00199         }
00200 
00201 // get a base (account) id to identify all keys for a particular service
00202 TUint CMTMTxtSettings::GetNextAccountSlotL()
00203         {
00204         TUint32 accountId = KNullId;
00205         TInt serviceId = 0;
00206         TInt error = 0;
00207         TBool found = EFalse;
00208         
00209         for (TInt count = 0; count < KMaxAccount; ++count)
00210                 {               
00211                 accountId = accountId + KIncrementAccount;
00212                 error = iRepository->Get(accountId, serviceId);         
00213                 if (error == KErrNotFound)
00214                         {
00215                         found = ETrue;                          
00216                         break;
00217                         }
00218                 else
00219                         {
00220                         User::LeaveIfError(error);
00221                         }
00222                 }
00223 
00224         if (found == EFalse)
00225                 {
00226                 // No empty slot available              
00227                 User::Leave(KErrNotFound);
00228                 }
00229         
00230         return accountId;
00231         }
00232 
00233 // delete a setting, and don't give an error if it doesn't exist
00234 void CMTMTxtSettings::DeleteSettingL(TUint32 settingId)
00235         {
00236         TInt error = iRepository->Delete(settingId);
00237         if (error != KErrNotFound)
00238                 {
00239                 User::LeaveIfError(error);
00240                 }
00241         }
00242         
00243 //
00244 // TxtUtils: Generic static utility functions
00245 // 
00246 
00247 EXPORT_C void TxtUtils::GetEntryFileNameL(TFileName& aFileName, TMsvEntry& aEntry) 
00248 // Create absolute file name: default path + aEntry.iDetails + aEntry.iDescription
00249         {
00250         CMTMTxtSettings* settings = CMTMTxtSettings::NewL();
00251         CleanupStack::PushL(settings);
00252         TMTMTxtSettings root;
00253         settings->LoadSettingsL(aEntry.iServiceId, root);
00254         CleanupStack::PopAndDestroy(); //settings
00255         aFileName = root.RootFolder();          
00256         aFileName.Append(aEntry.iDetails);
00257         aFileName.Append(aEntry.iDescription);
00258         if (aEntry.iType == KUidMsvFolderEntry)
00259                 aFileName.Append(KPathDelimiter);
00260         }
00261 

Generated on Thu Jan 21 10:32:59 2010 for TB10.1 Example Applications by  doxygen 1.5.3