sync with tizen_2.0
[platform/framework/native/appfw.git] / src / io / FIo_RegistryImpl.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        FIo_RegistryImpl.cpp
20  * @brief       This is the implementation file for _RegistryImpl class.
21  */
22
23 #include <unistd.h>
24 #include <new>
25 #include <unique_ptr.h>
26
27 #include <FBaseInteger.h>
28 #include <FBaseDouble.h>
29 #include <FBaseFloat.h>
30 #include <FBaseUuId.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseColHashMap.h>
33 #include <FBaseColIEnumerator.h>
34 #include <FBaseUtilStringTokenizer.h>
35 #include <FBaseResult.h>
36 #include <FBaseSysLog.h>
37 #include <FIoFile.h>
38 #include <FIoRegistry.h>
39
40 #include <FBase_StringConverter.h>
41 #include <FIo_FileImpl.h>
42 #include <FIo_RegistryImpl.h>
43 #include <FIo_NormalRegistry.h>
44 #include <FIo_SecureRegistry.h>
45 #include <FIo_SecureIoUtil.h>
46
47 using namespace std;
48 using namespace Tizen::Base;
49 using namespace Tizen::Base::Utility;
50 using namespace Tizen::Base::Collection;
51
52 namespace Tizen { namespace Io
53 {
54
55 static const int _MAX_REG_OPENMODE_LENGTH = 2;
56
57 _RegistryImpl::_RegistryImpl(void)
58         : __pRegistryCore(null)
59 {
60 }
61
62 _RegistryImpl::~_RegistryImpl(void)
63 {
64         delete __pRegistryCore;
65 }
66
67 bool
68 _RegistryImpl::ConvertRegistryOpenMode(long legacyMode, char* pOpenMode)
69 {
70         switch (legacyMode)
71         {
72         case REG_OPEN_READ_ONLY:
73                 strcpy(pOpenMode, "r\0");
74                 break;
75         case REG_OPEN_READ_WRITE:
76                 strcpy(pOpenMode, "r+\0");
77                 break; 
78         case (REG_OPEN_READ_WRITE | REG_OPEN_CREATE):
79                 strcpy(pOpenMode, "a+\0");
80                 break;
81         default:
82                 SysLog(NID_IO, "[E_INVALID_ARG] The specified openMode is invalid.");
83                 return false;
84         }
85         return true;
86 }
87
88 // This method will be removed in a future release.
89 result
90 _RegistryImpl::Construct(const String& regPath, long legacyMode, const ByteBuffer* pSecretKey)
91 {
92         SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
93
94         unique_ptr<char[]> pOpenMode(new char[_MAX_REG_OPENMODE_LENGTH + 1]);
95         SysTryReturnResult(NID_IO, pOpenMode != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
96
97         bool isValid = _RegistryImpl::ConvertRegistryOpenMode(legacyMode, pOpenMode.get());
98         SysTryReturnResult(NID_IO, isValid == true, E_INVALID_ARG, "The specified openMode is invalid.");
99
100         return Construct(regPath, pOpenMode.get(), pSecretKey);
101 }
102
103 result
104 _RegistryImpl::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer* pSecretKey)
105 {
106         SysAssertf(__pRegistryCore == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class\n");
107
108         result r = E_SUCCESS;
109         if (!pSecretKey)
110         {
111                 unique_ptr<_NormalRegistry> pNormalRegistry(new (std::nothrow) _NormalRegistry());
112                 SysTryReturnResult(NID_IO, pNormalRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
113
114                 r = pNormalRegistry->Construct(regPath, pOpenMode);
115                 if (IsFailed(r))
116                 {
117                         SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
118                         return r;
119                 }
120                 __pRegistryCore = pNormalRegistry.release();
121         }
122         else
123         {
124                 unique_ptr<_SecureRegistry> pSecureRegistry(new (std::nothrow) _SecureRegistry());
125                 SysTryReturnResult(NID_IO, pSecureRegistry != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
126
127                 r = pSecureRegistry->Construct(regPath, pOpenMode, pSecretKey);
128                 if (IsFailed(r))
129                 {
130                         SysLogException(NID_IO, r, "[%s] Propagated.", GetErrorMessage(r));
131                         return r;
132                 }
133                 __pRegistryCore = pSecureRegistry.release();
134         }
135
136         return r;
137 }
138
139 result
140 _RegistryImpl::Remove(const String& regPath)
141 {
142         return _FileImpl::Remove(regPath);
143 }
144
145 result
146 _RegistryImpl::AddSection(const String& sectionName)
147 {
148         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
149         return __pRegistryCore->AddSection(sectionName);
150 }
151
152 result
153 _RegistryImpl::RemoveSection(const String& sectionName)
154 {
155         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
156         return __pRegistryCore->RemoveSection(sectionName);
157 }
158
159 result
160 _RegistryImpl::GetSectionListN(IList** pRetList)
161 {
162         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
163         return __pRegistryCore->GetSectionListN(pRetList);
164 }
165
166
167 result
168 _RegistryImpl::GetEntryList(const String& sectionName, Collection::HashMap& retMap)
169 {
170         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
171         return __pRegistryCore->GetEntryList(sectionName, retMap);
172 }
173
174
175 result
176 _RegistryImpl::GetEntryListN(const String& sectionName, HashMap** pRetList)
177 {
178         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
179         return __pRegistryCore->GetEntryListN(sectionName, pRetList);
180 }
181
182 IMap*
183 _RegistryImpl::GetAllEntriesN(const String& sectionName) const
184 {
185         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
186         return __pRegistryCore->GetAllEntriesN(sectionName);
187 }
188
189 IList*
190 _RegistryImpl::GetAllEntryNamesN(const String& sectionName) const
191 {
192         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
193         return __pRegistryCore->GetAllEntryNamesN(sectionName);
194 }
195
196 result
197 _RegistryImpl::GetValue(const String& sectionName, const String& entryName, String& valStr)
198 {
199         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
200         return __pRegistryCore->GetValue(sectionName, entryName, valStr);
201 }
202
203 result
204 _RegistryImpl::AddValue(const String& sectionName, const String& entryName, const String& valStr)
205 {
206         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
207         return __pRegistryCore->AddValue(sectionName, entryName, valStr);
208 }
209
210 result
211 _RegistryImpl::SetValue(const String& sectionName, const String& entryName, const String& valStr)
212 {
213         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
214         return __pRegistryCore->SetValue(sectionName, entryName, valStr);
215 }
216
217 result
218 _RegistryImpl::RemoveValue(const String& sectionName, const String& entryName)
219 {
220         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
221         return __pRegistryCore->RemoveValue(sectionName, entryName);
222 }
223
224 result
225 _RegistryImpl::Flush(void)
226 {
227         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
228         return __pRegistryCore->Flush();
229 }
230
231 result
232 _RegistryImpl::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer* pKey)
233 {
234         return _SecureRegistry::ConvertToSecureRegistry(plainRegPath, secureRegPath, pKey);
235 }
236
237 int
238 _RegistryImpl::GetSectionIndex(const String& sectionName)
239 {
240         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
241         return __pRegistryCore->GetSectionIndex(sectionName);
242 }
243
244 void
245 _RegistryImpl::Removekey(const String& sectionName, const String& keyName)
246 {
247         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
248         __pRegistryCore->Removekey(sectionName, keyName);
249 }
250
251 int
252 _RegistryImpl::GetAllSectionCount(void)
253 {
254         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
255         return __pRegistryCore->GetAllSectionCount();
256 }
257
258
259 int
260 _RegistryImpl::GetAllEntryCount(int sectionIndex)
261 {
262         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
263         return __pRegistryCore->GetAllEntryCount(sectionIndex);
264 }
265
266 void
267 _RegistryImpl::RemoveEntry(int sectionIndex, int entryIndex)
268 {
269         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
270         __pRegistryCore->RemoveEntry(sectionIndex, entryIndex);
271 }
272
273 void
274 _RegistryImpl::ModifyEntryValue(int sectionIndex, int entryIndex, _RegValueType type, const void* pValue, int size)
275 {
276         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
277         __pRegistryCore->ModifyEntryValue(sectionIndex, entryIndex, type, pValue, size);
278 }
279
280 int
281 _RegistryImpl::GetEntryIndex(int sectionIndex, const String& entryName)
282 {
283         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
284         return __pRegistryCore->GetEntryIndex(sectionIndex, entryName);
285 }
286
287 String
288 _RegistryImpl::GetEntryName(int sectionIndex, int entryIndex)
289 {
290         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
291         return __pRegistryCore->GetEntryName(sectionIndex, entryIndex);
292 }
293
294 void
295 _RegistryImpl::GetEntryValue(int sectionIndex, int entryIndex, _RegValueType type, void* pValue, int* pSize)
296 {
297         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
298         __pRegistryCore->GetEntryValue(sectionIndex, entryIndex, type, pValue, pSize);
299 }
300
301 String
302 _RegistryImpl::GetSectionName(int sectionIndex)
303 {
304         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
305         return __pRegistryCore->GetSectionName(sectionIndex);
306 }
307
308 void*
309 _RegistryImpl::GetSectionByIndex(int sectionIndex)
310 {
311         SysAssertf(__pRegistryCore != null, "Not yet constructed. Construct() should be called before use.\n");
312         return __pRegistryCore->GetSectionByIndex(sectionIndex);
313 }
314
315 _RegistryImpl*
316 _RegistryImpl::GetInstance(Registry& registry)
317 {
318         return registry.__pRegistryImpl;
319 }
320
321 const _RegistryImpl*
322 _RegistryImpl::GetInstance(const Registry& registry)
323 {
324         return registry.__pRegistryImpl;
325 }
326
327 }} // Tizen::Io
328