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+");
68 SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
70 FileLock* pReglock = reg.LockN(FILE_LOCK_EXCLUSIVE);
71 SysTryLog(NID_APP, pReglock != null, "[%s] Locking the app registry file has failed.", GetErrorMessage(GetLastResult()));
73 r = reg.AddSection(__sectionName);
74 if (r == E_SECTION_ALREADY_EXIST)
86 _AppRegistryImpl::Add(const String& key, const String& value)
88 MutexGuard lock(__mutex);
90 Registry* pReg = LoadN(ReadWrite);
93 result r = GetLastResult();
94 SysPropagate(NID_APP, r);
98 result r = pReg->AddValue(__sectionName, key, value);
99 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
102 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
110 _AppRegistryImpl::Add(const String& key, int value)
112 MutexGuard lock(__mutex);
114 Registry* pReg = LoadN(ReadWrite);
117 result r = GetLastResult();
118 SysPropagate(NID_APP, r);
122 result r = pReg->AddValue(__sectionName, key, value);
123 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
126 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
134 _AppRegistryImpl::Add(const String& key, double value)
136 MutexGuard lock(__mutex);
138 Registry* pReg = LoadN(ReadWrite);
141 result r = GetLastResult();
142 SysPropagate(NID_APP, r);
146 result r = pReg->AddValue(__sectionName, key, value);
147 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
150 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
158 _AppRegistryImpl::Set(const String& key, const String& value)
160 MutexGuard lock(__mutex);
162 Registry* pReg = LoadN(ReadWrite);
165 result r = GetLastResult();
166 SysPropagate(NID_APP, r);
170 result r = pReg->SetValue(__sectionName, key, value);
171 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
174 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
182 _AppRegistryImpl::Set(const String& key, int value)
184 MutexGuard lock(__mutex);
186 Registry* pReg = LoadN(ReadWrite);
189 result r = GetLastResult();
190 SysPropagate(NID_APP, r);
194 result r = pReg->SetValue(__sectionName, key, value);
195 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
198 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
206 _AppRegistryImpl::Set(const String& key, double value)
208 MutexGuard lock(__mutex);
210 Registry* pReg = LoadN(ReadWrite);
213 result r = GetLastResult();
214 SysPropagate(NID_APP, r);
218 result r = pReg->SetValue(__sectionName, key, value);
219 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
222 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
230 _AppRegistryImpl::Save(void)
236 _AppRegistryImpl::Remove(const String& key)
238 MutexGuard lock(__mutex);
240 Registry* pReg = LoadN(ReadWrite);
243 result r = GetLastResult();
244 SysPropagate(NID_APP, r);
248 result r = pReg->RemoveValue(__sectionName, key);
249 SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
257 _AppRegistryImpl::Get(const String& key, String& value) const
259 MutexGuard lock(__mutex);
261 Registry* pReg = LoadN(ReadOnly);
264 result r = GetLastResult();
265 SysPropagate(NID_APP, r);
269 result r = pReg->GetValue(__sectionName, key, value);
270 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
278 _AppRegistryImpl::Get(const String& key, int& value) const
280 MutexGuard lock(__mutex);
282 Registry* pReg = LoadN(ReadOnly);
285 result r = GetLastResult();
286 SysPropagate(NID_APP, r);
290 result r = pReg->GetValue(__sectionName, key, value);
291 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
299 _AppRegistryImpl::Get(const String& key, double& value) const
301 MutexGuard lock(__mutex);
303 Registry* pReg = LoadN(ReadOnly);
306 result r = GetLastResult();
307 SysPropagate(NID_APP, r);
311 result r = pReg->GetValue(__sectionName, key, value);
312 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
320 _AppRegistryImpl::LoadN(ReadOnlyTag) const
322 Registry* pReg = new (std::nothrow) Registry();
323 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
325 result r = pReg->Construct(__regPath, "r");
326 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
327 __regPath.GetPointer(), GetErrorMessage(r));
334 _AppRegistryImpl::LoadN(ReadWriteTag) const
336 Registry* pReg = new (std::nothrow) Registry();
337 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
339 result r = pReg->Construct(__regPath, "a+");
340 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
341 __regPath.GetPointer(), GetErrorMessage(r));