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)
85 _AppRegistryImpl::Add(const String& key, const String& value)
87 MutexGuard lock(__mutex);
89 Registry* pReg = LoadN(ReadWrite);
90 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
92 result r = pReg->AddValue(__sectionName, key, value);
93 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
96 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
104 _AppRegistryImpl::Add(const String& key, int value)
106 MutexGuard lock(__mutex);
108 Registry* pReg = LoadN(ReadWrite);
109 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
111 result r = pReg->AddValue(__sectionName, key, value);
112 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
115 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
123 _AppRegistryImpl::Add(const String& key, double value)
125 MutexGuard lock(__mutex);
127 Registry* pReg = LoadN(ReadWrite);
128 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
130 result r = pReg->AddValue(__sectionName, key, value);
131 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
134 SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
142 _AppRegistryImpl::Set(const String& key, const String& value)
144 MutexGuard lock(__mutex);
146 Registry* pReg = LoadN(ReadWrite);
147 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
149 result r = pReg->SetValue(__sectionName, key, value);
150 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
153 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
161 _AppRegistryImpl::Set(const String& key, int value)
163 MutexGuard lock(__mutex);
165 Registry* pReg = LoadN(ReadWrite);
166 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
168 result r = pReg->SetValue(__sectionName, key, value);
169 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
172 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
180 _AppRegistryImpl::Set(const String& key, double value)
182 MutexGuard lock(__mutex);
184 Registry* pReg = LoadN(ReadWrite);
185 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
187 result r = pReg->SetValue(__sectionName, key, value);
188 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
191 SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
199 _AppRegistryImpl::Save(void)
205 _AppRegistryImpl::Remove(const String& key)
207 MutexGuard lock(__mutex);
209 Registry* pReg = LoadN(ReadWrite);
210 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
212 result r = pReg->RemoveValue(__sectionName, key);
213 SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
221 _AppRegistryImpl::Get(const String& key, String& value) const
223 MutexGuard lock(__mutex);
225 Registry* pReg = LoadN(ReadOnly);
226 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
228 result r = pReg->GetValue(__sectionName, key, value);
229 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
237 _AppRegistryImpl::Get(const String& key, int& value) const
239 MutexGuard lock(__mutex);
241 Registry* pReg = LoadN(ReadOnly);
242 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
244 result r = pReg->GetValue(__sectionName, key, value);
245 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
253 _AppRegistryImpl::Get(const String& key, double& value) const
255 MutexGuard lock(__mutex);
257 Registry* pReg = LoadN(ReadOnly);
258 SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
260 result r = pReg->GetValue(__sectionName, key, value);
261 SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
269 _AppRegistryImpl::LoadN(ReadOnlyTag) const
271 Registry* pReg = new (std::nothrow) Registry();
272 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
274 result r = pReg->Construct(__regPath, "r");
275 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
276 __regPath.GetPointer(), GetErrorMessage(r));
283 _AppRegistryImpl::LoadN(ReadWriteTag) const
285 Registry* pReg = new (std::nothrow) Registry();
286 SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
288 result r = pReg->Construct(__regPath, "a+");
289 SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
290 __regPath.GetPointer(), GetErrorMessage(r));