667a0162cab2d1e2ca05dcec9411939e0c58e4f0
[platform/framework/native/appfw.git] / src / app / FApp_AppRegistryImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FApp_AppRegistryImpl.cpp
19  * @brief       This is the implementation for the _AppRegistryImpl class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FBaseRtMutexGuard.h>
24 #include <FIoFile.h>
25 #include <FIoRegistry.h>
26 #include <FAppAppRegistry.h>
27
28 #include "FApp_AppRegistryImpl.h"
29 #include "FApp_AppInfo.h"
30
31 using namespace Tizen::Io;
32 using namespace Tizen::Base;
33 using namespace Tizen::Base::Runtime;
34
35 namespace Tizen { namespace App
36 {
37
38 _AppRegistryImpl::_AppRegistryImpl(void)
39         : __sectionName(L"__ApplicationStates")
40 {
41 }
42
43 _AppRegistryImpl::~_AppRegistryImpl(void)
44 {
45 }
46
47 result
48 _AppRegistryImpl::Construct(void)
49 {
50         const String& packageId = _AppInfo::GetPackageId();
51         SysAssertf(!packageId.IsEmpty(), "Empty package.");
52
53         __regPath = _AppInfo::GetAppRootPath() + L"data/";
54         result r = __regPath.Append(packageId);
55         SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
56
57         __mutex.Create();
58
59         {
60                 MutexGuard lock(__mutex);
61
62                 Registry reg;
63                 r = reg.Construct(__regPath, "a+");
64                 SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
65
66                 r = reg.AddSection(__sectionName);
67                 if (r == E_SECTION_ALREADY_EXIST)
68                 {
69                         r = E_SUCCESS;
70                 }
71         }
72         return r;
73 }
74
75 result
76 _AppRegistryImpl::Add(const String& key, const String& value)
77 {
78         MutexGuard lock(__mutex);
79
80         Registry* pReg = LoadN(ReadWrite);
81         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
82
83         result r = pReg->AddValue(__sectionName, key, value);
84         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
85
86         delete pReg;
87
88         return r;
89 }
90
91 result
92 _AppRegistryImpl::Add(const String& key, int value)
93 {
94         MutexGuard lock(__mutex);
95
96         Registry* pReg = LoadN(ReadWrite);
97         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
98
99         result r = pReg->AddValue(__sectionName, key, value);
100         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
101
102         delete pReg;
103
104         return r;
105 }
106
107 result
108 _AppRegistryImpl::Add(const String& key, double value)
109 {
110         MutexGuard lock(__mutex);
111
112         Registry* pReg = LoadN(ReadWrite);
113         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
114
115         result r = pReg->AddValue(__sectionName, key, value);
116         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
117
118         delete pReg;
119
120         return r;
121 }
122
123 result
124 _AppRegistryImpl::Set(const String& key, const String& value)
125 {
126         MutexGuard lock(__mutex);
127
128         Registry* pReg = LoadN(ReadWrite);
129         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
130
131         result r = pReg->SetValue(__sectionName, key, value);
132         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
133
134         delete pReg;
135
136         return r;
137 }
138
139 result
140 _AppRegistryImpl::Set(const String& key, int value)
141 {
142         MutexGuard lock(__mutex);
143
144         Registry* pReg = LoadN(ReadWrite);
145         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
146
147         result r = pReg->SetValue(__sectionName, key, value);
148         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
149
150         delete pReg;
151
152         return r;
153 }
154
155 result
156 _AppRegistryImpl::Set(const String& key, double value)
157 {
158         MutexGuard lock(__mutex);
159
160         Registry* pReg = LoadN(ReadWrite);
161         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
162
163         result r = pReg->SetValue(__sectionName, key, value);
164         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
165
166         delete pReg;
167
168         return r;
169 }
170
171 result
172 _AppRegistryImpl::Save(void)
173 {
174     return E_SUCCESS;
175 }
176
177 result
178 _AppRegistryImpl::Remove(const String& key)
179 {
180         MutexGuard lock(__mutex);
181
182         Registry* pReg = LoadN(ReadWrite);
183         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
184
185         result r = pReg->RemoveValue(__sectionName, key);
186         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
187
188         delete pReg;
189
190         return r;
191 }
192
193 result
194 _AppRegistryImpl::Get(const String& key, String& value) const
195 {
196         MutexGuard lock(__mutex);
197
198         Registry* pReg = LoadN(ReadOnly);
199         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
200
201         result r = pReg->GetValue(__sectionName, key, value);
202         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
203
204         delete pReg;
205
206         return r;
207 }
208
209 result
210 _AppRegistryImpl::Get(const String& key, int& value) const
211 {
212         MutexGuard lock(__mutex);
213
214         Registry* pReg = LoadN(ReadOnly);
215         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
216
217         result r = pReg->GetValue(__sectionName, key, value);
218         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
219
220         delete pReg;
221
222         return r;
223 }
224
225 result
226 _AppRegistryImpl::Get(const String& key, double& value) const
227 {
228         MutexGuard lock(__mutex);
229
230         Registry* pReg = LoadN(ReadOnly);
231         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
232
233         result r = pReg->GetValue(__sectionName, key, value);
234         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
235
236         delete pReg;
237
238         return r;
239 }
240
241 Registry*
242 _AppRegistryImpl::LoadN(ReadOnlyTag) const
243 {
244         Registry* pReg = new (std::nothrow) Registry();
245         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
246
247         result r = pReg->Construct(__regPath, "r");
248         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
249                         __regPath.GetPointer(), GetErrorMessage(r));
250
251         SetLastResult(r);
252         return pReg;
253 }
254
255 Registry*
256 _AppRegistryImpl::LoadN(ReadWriteTag) const
257 {
258         Registry* pReg = new (std::nothrow) Registry();
259         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
260
261         result r = pReg->Construct(__regPath, "a+");
262         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
263                         __regPath.GetPointer(), GetErrorMessage(r));
264
265         SetLastResult(r);
266         return pReg;
267 }
268
269 }} // Tizen::App
270