Merge "Change the way to Convert UTF8 to wchar_t, and vice versa" into tizen_2.1
[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 <FAppAppRegistry.h>
23 #include <FIoRegistry.h>
24
25 #include <FBaseSysLog.h>
26 #include "FApp_AppRegistryImpl.h"
27 #include "FApp_AppInfo.h"
28
29 using namespace Tizen::Io;
30 using namespace Tizen::Base;
31
32 namespace Tizen { namespace App
33 {
34
35
36 _AppRegistryImpl::_AppRegistryImpl(void)
37         : __pRegistry(null)
38         , __sectionName(L"__ApplicationStates")
39 {
40 }
41
42
43 _AppRegistryImpl::~_AppRegistryImpl(void)
44 {
45         delete __pRegistry;
46 }
47
48
49 result
50 _AppRegistryImpl::Construct(void)
51 {
52         result r = E_SUCCESS;
53
54         SysAssertf(__pRegistry == null,
55                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
56
57         const String& packageId = _AppInfo::GetPackageId();
58         String regName = _AppInfo::GetAppRootPath() + L"data/";
59
60         SysAssertf(!packageId.IsEmpty(), "Empty package.");
61         r = regName.Append(packageId);
62         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] String appending has failed.", GetErrorMessage(r));
63
64         __pRegistry = new (std::nothrow) Registry();
65         SysTryCatch(NID_APP, __pRegistry != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
66                            "[E_OUT_OF_MEMORY] Failed while instantiating registry.");
67
68         r = __pRegistry->Construct(regName, true);
69         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%ls] Constructing the registry %s has failed.", regName.GetPointer(),
70                            GetErrorMessage(r));
71
72         r = __pRegistry->AddSection(__sectionName);
73         if (r == E_SECTION_ALREADY_EXIST)
74         {
75                 // section may exist already
76                 r = E_SUCCESS;
77         }
78
79         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Adding section to registry has failed.", GetErrorMessage(r));
80
81         return r;
82
83 CATCH:
84         delete __pRegistry;
85         __pRegistry = null;
86
87         SysLog(NID_APP, "Exit.");
88
89         return r;
90 }
91
92
93 result
94 _AppRegistryImpl::Add(const String& key, const String& value)
95 {
96         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
97
98         result r = E_SUCCESS;
99
100         r = __pRegistry->AddValue(__sectionName, key, value);
101
102         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
103
104         return r;
105 }
106
107
108 result
109 _AppRegistryImpl::Add(const String& key, int value)
110 {
111         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
112
113         result r = E_SUCCESS;
114
115         r = __pRegistry->AddValue(__sectionName, key, value);
116
117         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
118
119         return r;
120 }
121
122
123 result
124 _AppRegistryImpl::Add(const String& key, double value)
125 {
126         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
127
128         result r = E_SUCCESS;
129
130         r = __pRegistry->AddValue(__sectionName, key, value);
131
132         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
133
134         return r;
135 }
136
137
138 result
139 _AppRegistryImpl::Set(const String& key, const String& value)
140 {
141         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
142
143         result r = E_SUCCESS;
144
145         r = __pRegistry->SetValue(__sectionName, key, value);
146
147         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
148
149         return r;
150 }
151
152
153 result
154 _AppRegistryImpl::Set(const String& key, int value)
155 {
156         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
157
158         result r = E_SUCCESS;
159
160         r = __pRegistry->SetValue(__sectionName, key, value);
161
162         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
163
164         return r;
165 }
166
167
168 result
169 _AppRegistryImpl::Set(const String& key, double value)
170 {
171         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
172
173         result r = E_SUCCESS;
174
175         r = __pRegistry->SetValue(__sectionName, key, value);
176
177         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
178
179         return r;
180 }
181
182
183 result
184 _AppRegistryImpl::Save(void)
185 {
186         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
187
188         result r = E_SUCCESS;
189
190         r = __pRegistry->Flush();
191
192         SysTryLog(NID_APP, !IsFailed(r), "[%s] Saving value to the registry has failed.", GetErrorMessage(r));
193
194         return r;
195 }
196
197
198 result
199 _AppRegistryImpl::Remove(const String& key)
200 {
201         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
202
203         result r = E_SUCCESS;
204
205         r = __pRegistry->RemoveValue(__sectionName, key);
206
207         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
208
209         return r;
210 }
211
212
213 result
214 _AppRegistryImpl::Get(const String& key, String& value) const
215 {
216         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
217
218         result r = E_SUCCESS;
219
220         r = __pRegistry->GetValue(__sectionName, key, value);
221
222         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
223
224         return r;
225 }
226
227
228 result
229 _AppRegistryImpl::Get(const String& key, int& value) const
230 {
231         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
232
233         result r = E_SUCCESS;
234
235         r = __pRegistry->GetValue(__sectionName, key, value);
236
237         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
238
239         return r;
240 }
241
242
243 result
244 _AppRegistryImpl::Get(const String& key, double& value) const
245 {
246         SysAssertf(__pRegistry != null, "Not yet constructed. Construct() should be called before use.");
247
248         result r = E_SUCCESS;
249
250         r = __pRegistry->GetValue(__sectionName, key, value);
251
252         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
253
254         return r;
255 }
256
257
258 } } // Tizen::App