Add a forward decl of Io::Registry
[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         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
89
90         delete pReg;
91
92         return r;
93 }
94
95 result
96 _AppRegistryImpl::Add(const String& key, int value)
97 {
98         MutexGuard lock(__mutex);
99
100         Registry* pReg = LoadN(ReadWrite);
101         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
102
103         result r = pReg->AddValue(__sectionName, key, value);
104         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
105
106         delete pReg;
107
108         return r;
109 }
110
111 result
112 _AppRegistryImpl::Add(const String& key, double value)
113 {
114         MutexGuard lock(__mutex);
115
116         Registry* pReg = LoadN(ReadWrite);
117         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
118
119         result r = pReg->AddValue(__sectionName, key, value);
120         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
121
122         delete pReg;
123
124         return r;
125 }
126
127 result
128 _AppRegistryImpl::Set(const String& key, const String& value)
129 {
130         MutexGuard lock(__mutex);
131
132         Registry* pReg = LoadN(ReadWrite);
133         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
134
135         result r = pReg->SetValue(__sectionName, key, value);
136         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
137
138         delete pReg;
139
140         return r;
141 }
142
143 result
144 _AppRegistryImpl::Set(const String& key, int value)
145 {
146         MutexGuard lock(__mutex);
147
148         Registry* pReg = LoadN(ReadWrite);
149         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
150
151         result r = pReg->SetValue(__sectionName, key, value);
152         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
153
154         delete pReg;
155
156         return r;
157 }
158
159 result
160 _AppRegistryImpl::Set(const String& key, double value)
161 {
162         MutexGuard lock(__mutex);
163
164         Registry* pReg = LoadN(ReadWrite);
165         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
166
167         result r = pReg->SetValue(__sectionName, key, value);
168         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
169
170         delete pReg;
171
172         return r;
173 }
174
175 result
176 _AppRegistryImpl::Save(void)
177 {
178     return E_SUCCESS;
179 }
180
181 result
182 _AppRegistryImpl::Remove(const String& key)
183 {
184         MutexGuard lock(__mutex);
185
186         Registry* pReg = LoadN(ReadWrite);
187         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
188
189         result r = pReg->RemoveValue(__sectionName, key);
190         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
191
192         delete pReg;
193
194         return r;
195 }
196
197 result
198 _AppRegistryImpl::Get(const String& key, String& value) const
199 {
200         MutexGuard lock(__mutex);
201
202         Registry* pReg = LoadN(ReadOnly);
203         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
204
205         result r = pReg->GetValue(__sectionName, key, value);
206         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
207
208         delete pReg;
209
210         return r;
211 }
212
213 result
214 _AppRegistryImpl::Get(const String& key, int& value) const
215 {
216         MutexGuard lock(__mutex);
217
218         Registry* pReg = LoadN(ReadOnly);
219         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
220
221         result r = pReg->GetValue(__sectionName, key, value);
222         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
223
224         delete pReg;
225
226         return r;
227 }
228
229 result
230 _AppRegistryImpl::Get(const String& key, double& value) const
231 {
232         MutexGuard lock(__mutex);
233
234         Registry* pReg = LoadN(ReadOnly);
235         SysTryReturnResult(NID_APP, pReg != null, GetLastResult(), "Propagating to caller...");
236
237         result r = pReg->GetValue(__sectionName, key, value);
238         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
239
240         delete pReg;
241
242         return r;
243 }
244
245 Registry*
246 _AppRegistryImpl::LoadN(ReadOnlyTag) const
247 {
248         Registry* pReg = new (std::nothrow) Registry();
249         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
250
251         result r = pReg->Construct(__regPath, "r");
252         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
253                         __regPath.GetPointer(), GetErrorMessage(r));
254
255         SetLastResult(r);
256         return pReg;
257 }
258
259 Registry*
260 _AppRegistryImpl::LoadN(ReadWriteTag) const
261 {
262         Registry* pReg = new (std::nothrow) Registry();
263         SysTryReturn(NID_APP, pReg != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
264
265         result r = pReg->Construct(__regPath, "a+");
266         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed. %s occurred.",
267                         __regPath.GetPointer(), GetErrorMessage(r));
268
269         SetLastResult(r);
270         return pReg;
271 }
272
273 }} // Tizen::App
274