Applied sizeof operator on fixed sized array to calculate size.
[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                 SysAssertf(!IsFailed(r), "[%s] Constructing the registry file (%ls) has failed.", GetErrorMessage(r), __regPath.GetPointer());
69
70                 FileLock* pReglock = reg.LockN(FILE_LOCK_EXCLUSIVE);
71                 SysTryLog(NID_APP, pReglock != null, "[%s] Locking the app registry file has failed.", GetErrorMessage(GetLastResult()));
72
73                 r = reg.AddSection(__sectionName);
74                 if (r == E_SECTION_ALREADY_EXIST)
75                 {
76                         r = E_SUCCESS;
77                 }
78
79                 delete pReglock;
80         }
81         return r;
82 }
83
84 result
85 _AppRegistryImpl::Add(const String& key, const String& value)
86 {
87         MutexGuard lock(__mutex);
88
89         Registry* pReg = LoadN(ReadWrite);
90         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
91
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));
94
95         r = pReg->Flush();
96         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
97
98 CATCH:
99         delete pReg;
100         return r;
101 }
102
103 result
104 _AppRegistryImpl::Add(const String& key, int value)
105 {
106         MutexGuard lock(__mutex);
107
108         Registry* pReg = LoadN(ReadWrite);
109         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
110
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));
113
114         r = pReg->Flush();
115         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
116
117 CATCH:
118         delete pReg;
119         return r;
120 }
121
122 result
123 _AppRegistryImpl::Add(const String& key, double value)
124 {
125         MutexGuard lock(__mutex);
126
127         Registry* pReg = LoadN(ReadWrite);
128         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
129
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));
132
133         r = pReg->Flush();
134         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
135
136 CATCH:
137         delete pReg;
138         return r;
139 }
140
141 result
142 _AppRegistryImpl::Set(const String& key, const String& value)
143 {
144         MutexGuard lock(__mutex);
145
146         Registry* pReg = LoadN(ReadWrite);
147         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
148
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));
151
152         r = pReg->Flush();
153         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
154
155 CATCH:
156         delete pReg;
157         return r;
158 }
159
160 result
161 _AppRegistryImpl::Set(const String& key, int value)
162 {
163         MutexGuard lock(__mutex);
164
165         Registry* pReg = LoadN(ReadWrite);
166         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
167
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));
170
171         r = pReg->Flush();
172         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
173
174 CATCH:
175         delete pReg;
176         return r;
177 }
178
179 result
180 _AppRegistryImpl::Set(const String& key, double value)
181 {
182         MutexGuard lock(__mutex);
183
184         Registry* pReg = LoadN(ReadWrite);
185         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
186
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));
189
190         r = pReg->Flush();
191         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
192
193 CATCH:
194         delete pReg;
195         return r;
196 }
197
198 result
199 _AppRegistryImpl::Save(void)
200 {
201     return E_SUCCESS;
202 }
203
204 result
205 _AppRegistryImpl::Remove(const String& key)
206 {
207         MutexGuard lock(__mutex);
208
209         Registry* pReg = LoadN(ReadWrite);
210         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
211
212         result r = pReg->RemoveValue(__sectionName, key);
213         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
214
215         delete pReg;
216
217         return r;
218 }
219
220 result
221 _AppRegistryImpl::Get(const String& key, String& value) const
222 {
223         MutexGuard lock(__mutex);
224
225         Registry* pReg = LoadN(ReadOnly);
226         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
227
228         result r = pReg->GetValue(__sectionName, key, value);
229         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
230
231         delete pReg;
232
233         return r;
234 }
235
236 result
237 _AppRegistryImpl::Get(const String& key, int& value) const
238 {
239         MutexGuard lock(__mutex);
240
241         Registry* pReg = LoadN(ReadOnly);
242         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
243
244         result r = pReg->GetValue(__sectionName, key, value);
245         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
246
247         delete pReg;
248
249         return r;
250 }
251
252 result
253 _AppRegistryImpl::Get(const String& key, double& value) const
254 {
255         MutexGuard lock(__mutex);
256
257         Registry* pReg = LoadN(ReadOnly);
258         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
259
260         result r = pReg->GetValue(__sectionName, key, value);
261         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
262
263         delete pReg;
264
265         return r;
266 }
267
268 Registry*
269 _AppRegistryImpl::LoadN(ReadOnlyTag) const
270 {
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.");
273
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));
277
278         SetLastResult(r);
279         return pReg;
280 }
281
282 Registry*
283 _AppRegistryImpl::LoadN(ReadWriteTag) const
284 {
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.");
287
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));
291
292         SetLastResult(r);
293         return pReg;
294 }
295
296 }} // Tizen::App
297