Merge "Change the way to conver Mbs to Wcs and vice versa" into tizen_2.1
[platform/framework/native/appfw.git] / src / app / FApp_AppRegistryImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FApp_AppRegistryImpl.cpp
20  * @brief       This is the implementation for the _AppRegistryImpl class.
21  */
22
23 #include <FAppAppRegistry.h>
24 #include <FIoRegistry.h>
25
26 #include <FBaseSysLog.h>
27 #include "FApp_AppRegistryImpl.h"
28 #include "FApp_AppInfo.h"
29
30 using namespace Tizen::Io;
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33
34 namespace Tizen { namespace App
35 {
36
37
38 _AppRegistryImpl::_AppRegistryImpl(void)
39         : __pRegistry(null)
40         , __sectionName(L"__ApplicationStates")
41 {
42 }
43
44
45 _AppRegistryImpl::~_AppRegistryImpl(void)
46 {
47         if (__pRegistry != null)
48         {
49                 delete __pRegistry;
50                 __pRegistry = null;
51         }
52 }
53
54
55 result
56 _AppRegistryImpl::Construct(void)
57 {
58         result r = E_SUCCESS;
59
60         SysAssertf(__pRegistry == null,
61                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
62
63         const String& packageId = _AppInfo::GetPackageId();
64         String regName = _AppInfo::GetAppRootPath() + L"data/";
65
66         SysAssertf(!packageId.IsEmpty(), "Empty packageId.");
67         r = regName.Append(static_cast <const String&>(packageId));
68         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] String appending has failed.", GetErrorMessage(r));
69
70         __pRegistry = new (std::nothrow) Registry();
71         SysTryCatch(NID_APP, __pRegistry != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
72                            "[E_OUT_OF_MEMORY] Failed while instantiating registry.");
73
74         r = __pRegistry->Construct(regName, true);
75         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%ls] Constructing the registry %s has failed.", regName.GetPointer(),
76                            GetErrorMessage(r));
77
78         r = __pRegistry->AddSection(__sectionName);
79         if (r == E_SECTION_ALREADY_EXIST)
80         {
81                 // section may exist already
82                 r = E_SUCCESS;
83         }
84
85         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding section to registry has failed.", GetErrorMessage(r));
86
87         return r;
88
89 CATCH:
90         delete __pRegistry;
91         __pRegistry = null;
92
93         SysLog(NID_APP, "Exit.");
94
95         return r;
96 }
97
98
99 result
100 _AppRegistryImpl::Add(const String& key, const String& value)
101 {
102         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
103
104         result r = E_SUCCESS;
105
106         r = __pRegistry->AddValue(__sectionName, key, value);
107
108         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
109
110         return r;
111 }
112
113
114 result
115 _AppRegistryImpl::Add(const String& key, int value)
116 {
117         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
118
119         result r = E_SUCCESS;
120
121         r = __pRegistry->AddValue(__sectionName, key, value);
122
123         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
124
125         return r;
126 }
127
128
129 result
130 _AppRegistryImpl::Add(const String& key, double value)
131 {
132         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
133
134         result r = E_SUCCESS;
135
136         r = __pRegistry->AddValue(__sectionName, key, value);
137
138         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
139
140         return r;
141 }
142
143
144 result
145 _AppRegistryImpl::Set(const String& key, const String& value)
146 {
147         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
148
149         result r = E_SUCCESS;
150
151         r = __pRegistry->SetValue(__sectionName, key, value);
152
153         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
154
155         return r;
156 }
157
158
159 result
160 _AppRegistryImpl::Set(const String& key, int value)
161 {
162         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
163
164         result r = E_SUCCESS;
165
166         r = __pRegistry->SetValue(__sectionName, key, value);
167
168         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
169
170         return r;
171 }
172
173
174 result
175 _AppRegistryImpl::Set(const String& key, double value)
176 {
177         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
178
179         result r = E_SUCCESS;
180
181         r = __pRegistry->SetValue(__sectionName, key, value);
182
183         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
184
185         return r;
186 }
187
188
189 result
190 _AppRegistryImpl::Save(void)
191 {
192         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
193
194         result r = E_SUCCESS;
195
196         r = __pRegistry->Flush();
197
198         SysTryLog(NID_APP, !IsFailed(r), "[%s] Saving value to the registry has failed.", GetErrorMessage(r));
199
200         return r;
201 }
202
203
204 result
205 _AppRegistryImpl::Remove(const String& key)
206 {
207         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
208
209         result r = E_SUCCESS;
210
211         r = __pRegistry->RemoveValue(__sectionName, key);
212
213         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
214
215         return r;
216 }
217
218
219 result
220 _AppRegistryImpl::Get(const String& key, String& value) const
221 {
222         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
223
224         result r = E_SUCCESS;
225
226         r = __pRegistry->GetValue(__sectionName, key, value);
227
228         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
229
230         return r;
231 }
232
233
234 result
235 _AppRegistryImpl::Get(const String& key, int& value) const
236 {
237         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
238
239         result r = E_SUCCESS;
240
241         r = __pRegistry->GetValue(__sectionName, key, value);
242
243         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
244
245         return r;
246 }
247
248
249 result
250 _AppRegistryImpl::Get(const String& key, double& value) const
251 {
252         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
253
254         result r = E_SUCCESS;
255
256         r = __pRegistry->GetValue(__sectionName, key, value);
257
258         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
259
260         return r;
261 }
262
263
264 } } // Tizen::App