Merge "Fix default memory leak for AppControl request" into tizen_2.2
[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 <FIoFile.h>
24 #include <FIoFileAttributes.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
34 namespace Tizen { namespace App
35 {
36
37 _AppRegistryImpl::_AppRegistryImpl(void)
38         : __pRegistry(null)
39         , __sectionName(L"__ApplicationStates")
40 {
41 }
42
43 _AppRegistryImpl::~_AppRegistryImpl(void)
44 {
45 }
46
47 result
48 _AppRegistryImpl::Construct(void)
49 {
50         const String& packageId = _AppInfo::GetPackageId();
51         SysAssertf(!packageId.IsEmpty(), "Empty package.");
52
53         __regPath = _AppInfo::GetAppRootPath() + L"data/";
54         result r = __regPath.Append(packageId);
55         SysTryReturnResult(NID_APP, !IsFailed(r), r, "String appending has failed.");
56
57         SysLog(NID_APP, "Exit.");
58         return r;
59 }
60
61 result
62 _AppRegistryImpl::Add(const String& key, const String& value)
63 {
64         result r = Load();
65         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
66
67         r = __pRegistry->AddValue(__sectionName, key, value);
68         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
69
70         Unload();
71         return r;
72 }
73
74 result
75 _AppRegistryImpl::Add(const String& key, int value)
76 {
77         result r = Load();
78         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
79
80         r = __pRegistry->AddValue(__sectionName, key, value);
81         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
82
83         Unload();
84         return r;
85 }
86
87 result
88 _AppRegistryImpl::Add(const String& key, double value)
89 {
90         result r = Load();
91         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
92
93         r = __pRegistry->AddValue(__sectionName, key, value);
94         SysTryLog(NID_APP, !IsFailed(r), "[%s] Adding value to the registry has failed.", GetErrorMessage(r));
95
96         Unload();
97         return r;
98 }
99
100 result
101 _AppRegistryImpl::Set(const String& key, const String& value)
102 {
103         result r = Load();
104         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
105
106         r = __pRegistry->SetValue(__sectionName, key, value);
107         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
108
109         Unload();
110         return r;
111 }
112
113 result
114 _AppRegistryImpl::Set(const String& key, int value)
115 {
116         result r = Load();
117         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
118
119         r = __pRegistry->SetValue(__sectionName, key, value);
120         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
121
122         Unload();
123         return r;
124 }
125
126 result
127 _AppRegistryImpl::Set(const String& key, double value)
128 {
129         result r = Load();
130         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
131
132         r = __pRegistry->SetValue(__sectionName, key, value);
133         SysTryLog(NID_APP, !IsFailed(r), "[%s] Setting value to the registry has failed.", GetErrorMessage(r));
134
135         Unload();
136         return r;
137 }
138
139 result
140 _AppRegistryImpl::Save(void)
141 {
142     return E_SUCCESS;
143 }
144
145 result
146 _AppRegistryImpl::Remove(const String& key)
147 {
148         result r = Load();
149         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
150
151         r = __pRegistry->RemoveValue(__sectionName, key);
152         SysTryLog(NID_APP, !IsFailed(r), "[%s] Removing value to the registry has failed.", GetErrorMessage(r));
153
154         Unload();
155         return r;
156 }
157
158 result
159 _AppRegistryImpl::Get(const String& key, String& value) const
160 {
161         result r = Load();
162         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
163
164         r = __pRegistry->GetValue(__sectionName, key, value);
165         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
166
167         Unload();
168         return r;
169 }
170
171 result
172 _AppRegistryImpl::Get(const String& key, int& value) const
173 {
174         result r = Load();
175         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
176
177         r = __pRegistry->GetValue(__sectionName, key, value);
178         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
179
180         Unload();
181         return r;
182 }
183
184 result
185 _AppRegistryImpl::Get(const String& key, double& value) const
186 {
187         result r = Load();
188         SysTryReturnResult(NID_APP, !IsFailed(r), r, "[%s] Propagating to caller...", GetErrorMessage(r));
189
190         r = __pRegistry->GetValue(__sectionName, key, value);
191         SysTryLog(NID_APP, !IsFailed(r), "[%s] Getting value to the registry has failed.", GetErrorMessage(r));
192
193         Unload();
194         return r;
195 }
196
197 result
198 _AppRegistryImpl::Load(void) const
199 {
200         __pRegistry = new (std::nothrow) Registry();
201         SysTryReturnResult(NID_APP, __pRegistry != null, E_OUT_OF_MEMORY, "Instantiating registry has failed.");
202
203         result r = __pRegistry->Construct(__regPath, "a+");
204         SysAssertf(!IsFailed(r), "Constructing the registry file (%ls) has failed.", __regPath.GetPointer());
205
206         r = __pRegistry->AddSection(__sectionName);
207         if (r == E_SECTION_ALREADY_EXIST)
208         {
209                 r = E_SUCCESS;
210         }
211         SysAssertf(!IsFailed(r), "Adding section to registry has failed.");
212
213         return E_SUCCESS;
214 }
215
216 void
217 _AppRegistryImpl::Unload(void) const
218 {
219         delete __pRegistry;
220 }
221
222 }} // Tizen::App
223