Add event for update application
[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                 r = reg.AddSection(__sectionName);
71                 if (r == E_SECTION_ALREADY_EXIST)
72                 {
73                         r = E_SUCCESS;
74                 }
75         }
76         return r;
77 }
78
79 result
80 _AppRegistryImpl::Add(const String& key, const String& value)
81 {
82         MutexGuard lock(__mutex);
83
84         Registry* pReg = LoadN(ReadWrite);
85         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
86
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));
89
90         r = pReg->Flush();
91         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
92
93 CATCH:
94         delete pReg;
95         return r;
96 }
97
98 result
99 _AppRegistryImpl::Add(const String& key, int value)
100 {
101         MutexGuard lock(__mutex);
102
103         Registry* pReg = LoadN(ReadWrite);
104         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
105
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));
108
109         r = pReg->Flush();
110         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
111
112 CATCH:
113         delete pReg;
114         return r;
115 }
116
117 result
118 _AppRegistryImpl::Add(const String& key, double value)
119 {
120         MutexGuard lock(__mutex);
121
122         Registry* pReg = LoadN(ReadWrite);
123         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
124
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));
127
128         r = pReg->Flush();
129         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
130
131 CATCH:
132         delete pReg;
133         return r;
134 }
135
136 result
137 _AppRegistryImpl::Set(const String& key, const String& value)
138 {
139         MutexGuard lock(__mutex);
140
141         Registry* pReg = LoadN(ReadWrite);
142         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
143
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));
146
147         r = pReg->Flush();
148         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
149
150 CATCH:
151         delete pReg;
152         return r;
153 }
154
155 result
156 _AppRegistryImpl::Set(const String& key, int 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         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
165
166         r = pReg->Flush();
167         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
168
169 CATCH:
170         delete pReg;
171         return r;
172 }
173
174 result
175 _AppRegistryImpl::Set(const String& key, double value)
176 {
177         MutexGuard lock(__mutex);
178
179         Registry* pReg = LoadN(ReadWrite);
180         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
181
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));
184
185         r = pReg->Flush();
186         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
187
188 CATCH:
189         delete pReg;
190         return r;
191 }
192
193 result
194 _AppRegistryImpl::Save(void)
195 {
196     return E_SUCCESS;
197 }
198
199 result
200 _AppRegistryImpl::Remove(const String& key)
201 {
202         MutexGuard lock(__mutex);
203
204         Registry* pReg = LoadN(ReadWrite);
205         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
206
207         result r = pReg->RemoveValue(__sectionName, key);
208         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
209
210         delete pReg;
211
212         return r;
213 }
214
215 result
216 _AppRegistryImpl::Get(const String& key, String& value) const
217 {
218         MutexGuard lock(__mutex);
219
220         Registry* pReg = LoadN(ReadOnly);
221         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
222
223         result r = pReg->GetValue(__sectionName, key, value);
224         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
225
226         delete pReg;
227
228         return r;
229 }
230
231 result
232 _AppRegistryImpl::Get(const String& key, int& value) const
233 {
234         MutexGuard lock(__mutex);
235
236         Registry* pReg = LoadN(ReadOnly);
237         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
238
239         result r = pReg->GetValue(__sectionName, key, value);
240         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
241
242         delete pReg;
243
244         return r;
245 }
246
247 result
248 _AppRegistryImpl::Get(const String& key, double& value) const
249 {
250         MutexGuard lock(__mutex);
251
252         Registry* pReg = LoadN(ReadOnly);
253         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
254
255         result r = pReg->GetValue(__sectionName, key, value);
256         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
257
258         delete pReg;
259
260         return r;
261 }
262
263 Registry*
264 _AppRegistryImpl::LoadN(ReadOnlyTag) const
265 {
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.");
268
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));
272
273         SetLastResult(r);
274         return pReg;
275 }
276
277 Registry*
278 _AppRegistryImpl::LoadN(ReadWriteTag) const
279 {
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.");
282
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));
286
287         SetLastResult(r);
288         return pReg;
289 }
290
291 }} // Tizen::App
292