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 r = reg.AddSection(__sectionName);
71 if (r == E_SECTION_ALREADY_EXIST)
80 _AppRegistryImpl::Add(const String& key, const String& value)
82 MutexGuard lock(__mutex);
84 Registry* pReg = LoadN(ReadWrite);
85 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
87 result r = pReg->AddValue(__sectionName, key, value);
88 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
91 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
99 _AppRegistryImpl::Add(const String& key, int value)
101 MutexGuard lock(__mutex);
103 Registry* pReg = LoadN(ReadWrite);
104 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
106 result r = pReg->AddValue(__sectionName, key, value);
107 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
110 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
118 _AppRegistryImpl::Add(const String& key, double value)
120 MutexGuard lock(__mutex);
122 Registry* pReg = LoadN(ReadWrite);
123 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
125 result r = pReg->AddValue(__sectionName, key, value);
126 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
129 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
137 _AppRegistryImpl::Set(const String& key, const String& value)
139 MutexGuard lock(__mutex);
141 Registry* pReg = LoadN(ReadWrite);
142 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
144 result r = pReg->SetValue(__sectionName, key, value);
145 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
148 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
156 _AppRegistryImpl::Set(const String& key, int value)
158 MutexGuard lock(__mutex);
160 Registry* pReg = LoadN(ReadWrite);
161 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
163 result r = pReg->SetValue(__sectionName, key, value);
164 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
167 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
175 _AppRegistryImpl::Set(const String& key, double value)
177 MutexGuard lock(__mutex);
179 Registry* pReg = LoadN(ReadWrite);
180 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
182 result r = pReg->SetValue(__sectionName, key, value);
183 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
186 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
194 _AppRegistryImpl::Save(void)
200 _AppRegistryImpl::Remove(const String& key)
202 MutexGuard lock(__mutex);
204 Registry* pReg = LoadN(ReadWrite);
205 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
207 result r = pReg->RemoveValue(__sectionName, key);
208 SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
216 _AppRegistryImpl::Get(const String& key, String& value) const
218 MutexGuard lock(__mutex);
220 Registry* pReg = LoadN(ReadOnly);
221 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
223 result r = pReg->GetValue(__sectionName, key, value);
224 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
232 _AppRegistryImpl::Get(const String& key, int& value) const
234 MutexGuard lock(__mutex);
236 Registry* pReg = LoadN(ReadOnly);
237 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
239 result r = pReg->GetValue(__sectionName, key, value);
240 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
248 _AppRegistryImpl::Get(const String& key, double& value) const
250 MutexGuard lock(__mutex);
252 Registry* pReg = LoadN(ReadOnly);
253 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
255 result r = pReg->GetValue(__sectionName, key, value);
256 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
264 _AppRegistryImpl::LoadN(ReadOnlyTag) const
266 Registry* pReg = new (std::nothrow) Registry();
267 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
269 result r = pReg->Construct(__regPath, "r");
270 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
271 __regPath.GetPointer(), GetErrorMessage(r));
278 _AppRegistryImpl::LoadN(ReadWriteTag) const
280 Registry* pReg = new (std::nothrow) Registry();
281 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
283 result r = pReg->Construct(__regPath, "a+");
284 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
285 __regPath.GetPointer(), GetErrorMessage(r));