2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FApp_AppRegistryImpl.cpp
19 * @brief This is the implementation for the _AppRegistryImpl class.
22 #include <FBaseSysLog.h>
23 #include <FBaseRtMutexGuard.h>
25 #include <FIoRegistry.h>
26 #include <FAppAppRegistry.h>
28 #include "FApp_AppRegistryImpl.h"
29 #include "FApp_AppInfo.h"
31 using namespace Tizen::Io;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Runtime;
35 namespace Tizen { namespace App
38 static const ReadOnlyTag ReadOnly = {};
39 static const ReadWriteTag ReadWrite = {};
41 _AppRegistryImpl::_AppRegistryImpl(void)
42 : __sectionName(L"__ApplicationStates")
46 _AppRegistryImpl::~_AppRegistryImpl(void)
51 _AppRegistryImpl::Construct(void)
53 const String& packageId = _AppInfo::GetPackageId();
54 SysAssertf(!packageId.IsEmpty(), "Empty package.");
56 __regPath = _AppInfo::GetAppRootPath() + L"data/";
57 result r = __regPath.Append(packageId);
58 SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
61 SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex intialization failed.");
64 MutexGuard lock(__mutex);
67 r = reg.Construct(__regPath, "a+");
70 r = File::Remove(__regPath);
71 SysAssertf(!IsFailed(r), "[%s] Removing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
73 r = reg.Construct(__regPath, "a+");
74 SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
77 FileLock* pReglock = reg.LockN(FILE_LOCK_EXCLUSIVE);
78 SysTryLog(NID_APP, pReglock != null, "[%s] Locking the app registry file has failed.", GetErrorMessage(GetLastResult()));
80 r = reg.AddSection(__sectionName);
81 if (r == E_SECTION_ALREADY_EXIST)
93 _AppRegistryImpl::Add(const String& key, const String& value)
95 MutexGuard lock(__mutex);
97 Registry* pReg = LoadN(ReadWrite);
100 result r = GetLastResult();
101 SysPropagate(NID_APP, r);
105 result r = pReg->AddValue(__sectionName, key, value);
106 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
109 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
117 _AppRegistryImpl::Add(const String& key, int value)
119 MutexGuard lock(__mutex);
121 Registry* pReg = LoadN(ReadWrite);
124 result r = GetLastResult();
125 SysPropagate(NID_APP, r);
129 result r = pReg->AddValue(__sectionName, key, value);
130 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
133 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
141 _AppRegistryImpl::Add(const String& key, double value)
143 MutexGuard lock(__mutex);
145 Registry* pReg = LoadN(ReadWrite);
148 result r = GetLastResult();
149 SysPropagate(NID_APP, r);
153 result r = pReg->AddValue(__sectionName, key, value);
154 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
157 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
165 _AppRegistryImpl::Set(const String& key, const String& value)
167 MutexGuard lock(__mutex);
169 Registry* pReg = LoadN(ReadWrite);
172 result r = GetLastResult();
173 SysPropagate(NID_APP, r);
177 result r = pReg->SetValue(__sectionName, key, value);
178 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
181 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
189 _AppRegistryImpl::Set(const String& key, int value)
191 MutexGuard lock(__mutex);
193 Registry* pReg = LoadN(ReadWrite);
196 result r = GetLastResult();
197 SysPropagate(NID_APP, r);
201 result r = pReg->SetValue(__sectionName, key, value);
202 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
205 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
213 _AppRegistryImpl::Set(const String& key, double value)
215 MutexGuard lock(__mutex);
217 Registry* pReg = LoadN(ReadWrite);
220 result r = GetLastResult();
221 SysPropagate(NID_APP, r);
225 result r = pReg->SetValue(__sectionName, key, value);
226 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
229 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
237 _AppRegistryImpl::Save(void)
243 _AppRegistryImpl::Remove(const String& key)
245 MutexGuard lock(__mutex);
247 Registry* pReg = LoadN(ReadWrite);
250 result r = GetLastResult();
251 SysPropagate(NID_APP, r);
255 result r = pReg->RemoveValue(__sectionName, key);
256 SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
264 _AppRegistryImpl::Get(const String& key, String& value) const
266 MutexGuard lock(__mutex);
268 Registry* pReg = LoadN(ReadOnly);
271 result r = GetLastResult();
272 SysPropagate(NID_APP, r);
276 result r = pReg->GetValue(__sectionName, key, value);
277 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
285 _AppRegistryImpl::Get(const String& key, int& value) const
287 MutexGuard lock(__mutex);
289 Registry* pReg = LoadN(ReadOnly);
292 result r = GetLastResult();
293 SysPropagate(NID_APP, r);
297 result r = pReg->GetValue(__sectionName, key, value);
298 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
306 _AppRegistryImpl::Get(const String& key, double& value) const
308 MutexGuard lock(__mutex);
310 Registry* pReg = LoadN(ReadOnly);
313 result r = GetLastResult();
314 SysPropagate(NID_APP, r);
318 result r = pReg->GetValue(__sectionName, key, value);
319 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
327 _AppRegistryImpl::LoadN(ReadOnlyTag) const
329 Registry* pReg = new (std::nothrow) Registry();
330 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
332 result r = pReg->Construct(__regPath, "r");
333 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
334 __regPath.GetPointer(), GetErrorMessage(r));
341 _AppRegistryImpl::LoadN(ReadWriteTag) const
343 Registry* pReg = new (std::nothrow) Registry();
344 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
346 result r = pReg->Construct(__regPath, "a+");
347 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
348 __regPath.GetPointer(), GetErrorMessage(r));