Merge "Update deprecated libprivilege-control API functions." into tizen
[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 static const ReadOnlyTag ReadOnly = {};
39 static const ReadWriteTag ReadWrite = {};
40
41 _AppRegistryImpl::_AppRegistryImpl(void)
42         : __sectionName(L"__ApplicationStates")
43 {
44 }
45
46 _AppRegistryImpl::~_AppRegistryImpl(void)
47 {
48 }
49
50 result
51 _AppRegistryImpl::Construct(void)
52 {
53         const String& packageId = _AppInfo::GetPackageId();
54         SysAssertf(!packageId.IsEmpty(), "Empty package.");
55
56         __regPath = _AppInfo::GetAppRootPath() + L"data/";
57         result r = __regPath.Append(packageId);
58         SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
59
60         r = __mutex.Create();
61         SysTryReturnResult(NID_APP, !IsFailed(r), r, "Mutex intialization failed.");
62
63         {
64                 MutexGuard lock(__mutex);
65
66                 Registry reg;
67                 r = reg.Construct(__regPath, "a+");
68                 if (r == E_IO)
69                 {
70                         r = File::Remove(__regPath);
71                         SysAssertf(!IsFailed(r), "[%s] Removing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
72
73                         r = reg.Construct(__regPath, "a+");
74                         SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
75                 }
76
77                 FileLock* pReglock = reg.LockN(FILE_LOCK_EXCLUSIVE);
78                 SysTryLog(NID_APP, pReglock != null, "[%s] Locking the app registry file has failed.", GetErrorMessage(GetLastResult()));
79
80                 r = reg.AddSection(__sectionName);
81                 if (r == E_SECTION_ALREADY_EXIST)
82                 {
83                         r = E_SUCCESS;
84                 }
85
86                 reg.Flush();
87                 delete pReglock;
88         }
89         return r;
90 }
91
92 result
93 _AppRegistryImpl::Add(const String& key, const String& value)
94 {
95         MutexGuard lock(__mutex);
96
97         Registry* pReg = LoadN(ReadWrite);
98         if (pReg == null)
99         {
100                 result r = GetLastResult();
101                 SysPropagate(NID_APP, r);
102                 return r;
103         }
104
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));
107
108         r = pReg->Flush();
109         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
110
111 CATCH:
112         delete pReg;
113         return r;
114 }
115
116 result
117 _AppRegistryImpl::Add(const String& key, int value)
118 {
119         MutexGuard lock(__mutex);
120
121         Registry* pReg = LoadN(ReadWrite);
122         if (pReg == null)
123         {
124                 result r = GetLastResult();
125                 SysPropagate(NID_APP, r);
126                 return r;
127         }
128
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));
131
132         r = pReg->Flush();
133         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
134
135 CATCH:
136         delete pReg;
137         return r;
138 }
139
140 result
141 _AppRegistryImpl::Add(const String& key, double value)
142 {
143         MutexGuard lock(__mutex);
144
145         Registry* pReg = LoadN(ReadWrite);
146         if (pReg == null)
147         {
148                 result r = GetLastResult();
149                 SysPropagate(NID_APP, r);
150                 return r;
151         }
152
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));
155
156         r = pReg->Flush();
157         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
158
159 CATCH:
160         delete pReg;
161         return r;
162 }
163
164 result
165 _AppRegistryImpl::Set(const String& key, const String& value)
166 {
167         MutexGuard lock(__mutex);
168
169         Registry* pReg = LoadN(ReadWrite);
170         if (pReg == null)
171         {
172                 result r = GetLastResult();
173                 SysPropagate(NID_APP, r);
174                 return r;
175         }
176
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));
179
180         r = pReg->Flush();
181         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
182
183 CATCH:
184         delete pReg;
185         return r;
186 }
187
188 result
189 _AppRegistryImpl::Set(const String& key, int value)
190 {
191         MutexGuard lock(__mutex);
192
193         Registry* pReg = LoadN(ReadWrite);
194         if (pReg == null)
195         {
196                 result r = GetLastResult();
197                 SysPropagate(NID_APP, r);
198                 return r;
199         }
200
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));
203
204         r = pReg->Flush();
205         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
206
207 CATCH:
208         delete pReg;
209         return r;
210 }
211
212 result
213 _AppRegistryImpl::Set(const String& key, double value)
214 {
215         MutexGuard lock(__mutex);
216
217         Registry* pReg = LoadN(ReadWrite);
218         if (pReg == null)
219         {
220                 result r = GetLastResult();
221                 SysPropagate(NID_APP, r);
222                 return r;
223         }
224
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));
227
228         r = pReg->Flush();
229         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
230
231 CATCH:
232         delete pReg;
233         return r;
234 }
235
236 result
237 _AppRegistryImpl::Save(void)
238 {
239     return E_SUCCESS;
240 }
241
242 result
243 _AppRegistryImpl::Remove(const String& key)
244 {
245         MutexGuard lock(__mutex);
246
247         Registry* pReg = LoadN(ReadWrite);
248         if (pReg == null)
249         {
250                 result r = GetLastResult();
251                 SysPropagate(NID_APP, r);
252                 return r;
253         }
254
255         result r = pReg->RemoveValue(__sectionName, key);
256         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
257
258         delete pReg;
259
260         return r;
261 }
262
263 result
264 _AppRegistryImpl::Get(const String& key, String& value) const
265 {
266         MutexGuard lock(__mutex);
267
268         Registry* pReg = LoadN(ReadOnly);
269         if (pReg == null)
270         {
271                 result r = GetLastResult();
272                 SysPropagate(NID_APP, r);
273                 return r;
274         }
275
276         result r = pReg->GetValue(__sectionName, key, value);
277         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
278
279         delete pReg;
280
281         return r;
282 }
283
284 result
285 _AppRegistryImpl::Get(const String& key, int& value) const
286 {
287         MutexGuard lock(__mutex);
288
289         Registry* pReg = LoadN(ReadOnly);
290         if (pReg == null)
291         {
292                 result r = GetLastResult();
293                 SysPropagate(NID_APP, r);
294                 return r;
295         }
296
297         result r = pReg->GetValue(__sectionName, key, value);
298         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
299
300         delete pReg;
301
302         return r;
303 }
304
305 result
306 _AppRegistryImpl::Get(const String& key, double& value) const
307 {
308         MutexGuard lock(__mutex);
309
310         Registry* pReg = LoadN(ReadOnly);
311         if (pReg == null)
312         {
313                 result r = GetLastResult();
314                 SysPropagate(NID_APP, r);
315                 return r;
316         }
317
318         result r = pReg->GetValue(__sectionName, key, value);
319         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
320
321         delete pReg;
322
323         return r;
324 }
325
326 Registry*
327 _AppRegistryImpl::LoadN(ReadOnlyTag) const
328 {
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.");
331
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));
335
336         SetLastResult(r);
337         return pReg;
338 }
339
340 Registry*
341 _AppRegistryImpl::LoadN(ReadWriteTag) const
342 {
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.");
345
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));
349
350         SetLastResult(r);
351         return pReg;
352 }
353
354 }} // Tizen::App
355