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