00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <msvstd.h>
00017 #include <msvstore.h>
00018 #include <msvuids.h>
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;
00027 const TUint32 EAccountMask = 0x800FFFFF;
00028
00029
00030 enum
00031 {
00032 EServiceId = 0,
00033 EFolderSettingId = 1
00034 };
00035
00036
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
00047 CMTMTxtSettings::~CMTMTxtSettings()
00048 {
00049 delete iRepository;
00050 }
00051
00052
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
00087 TRAP(error, accountId = FindAccountL(aServiceId));
00088 if (error != KErrUnknown) User::LeaveIfError(error);
00089
00090 if (error == KErrUnknown) accountId = GetNextAccountSlotL();
00091
00092 TRAP( error,
00093
00094 CreateOrSetL(accountId, static_cast<TInt>(aServiceId));
00095 CreateOrSetL(accountId + EFolderSettingId, aSettings.RootFolder());
00096 );
00097 if (error != KErrNone)
00098 {
00099
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
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
00139 void CMTMTxtSettings::ConstructL()
00140 {
00141 iRepository = CRepository::NewL(KUidMsgTypeText);
00142 }
00143
00144
00145 void CMTMTxtSettings::CreateOrSetL(TUint aKey, const TDesC& aValue)
00146 {
00147 TInt error = iRepository->Set(aKey, aValue);
00148 if (error == KErrNotFound)
00149 {
00150
00151 User::LeaveIfError(iRepository->Create(aKey, aValue));
00152 }
00153 else
00154 {
00155 User::LeaveIfError(error);
00156 }
00157 }
00158
00159
00160 void CMTMTxtSettings::CreateOrSetL(TUint aKey, TInt aValue)
00161 {
00162 TInt error = iRepository->Set(aKey, aValue);
00163 if (error == KErrNotFound)
00164 {
00165
00166 User::LeaveIfError(iRepository->Create(aKey, aValue));
00167 }
00168 else
00169 {
00170 User::LeaveIfError(error);
00171 }
00172 }
00173
00174
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
00183 User::Leave(KErrUnknown);
00184 }
00185 else
00186 {
00187 User::LeaveIfError(error);
00188 }
00189
00190 if (accounts.Count()>1)
00191 {
00192
00193 User::Leave(KErrOverflow);
00194 }
00195
00196 TUint32 account = accounts[0];
00197 CleanupStack::PopAndDestroy(&accounts);
00198 return account;
00199 }
00200
00201
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
00227 User::Leave(KErrNotFound);
00228 }
00229
00230 return accountId;
00231 }
00232
00233
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
00245
00246
00247 EXPORT_C void TxtUtils::GetEntryFileNameL(TFileName& aFileName, TMsvEntry& aEntry)
00248
00249 {
00250 CMTMTxtSettings* settings = CMTMTxtSettings::NewL();
00251 CleanupStack::PushL(settings);
00252 TMTMTxtSettings root;
00253 settings->LoadSettingsL(aEntry.iServiceId, root);
00254 CleanupStack::PopAndDestroy();
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