2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIoRegistry.cpp
20 * @brief This is the implementation file for %Registry class.
25 #include <unique_ptr.h>
27 #include <FBaseInteger.h>
28 #include <FBaseDouble.h>
29 #include <FBaseFloat.h>
30 #include <FBaseUuId.h>
31 #include <FBaseString.h>
32 #include <FBaseByteBuffer.h>
33 #include <FBaseUtilStringTokenizer.h>
34 #include <FBaseResult.h>
35 #include <FBaseSysLog.h>
36 #include <FIoRegistry.h>
38 #include <FBase_StringConverter.h>
39 #include <FApp_AppInfo.h>
40 #include "FIo_RegistryImpl.h"
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Utility;
45 using namespace Tizen::Base::Collection;
47 namespace Tizen { namespace Io
50 Registry::Registry(void)
51 : __pRegistryImpl(null)
55 Registry::~Registry(void)
57 delete __pRegistryImpl;
58 __pRegistryImpl = null;
61 // This method will be removed in a future release.
63 Registry::Construct(const String& regPath, bool createIfNotExist)
65 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
67 long openMode = REG_OPEN_READ_WRITE;
70 openMode |= REG_OPEN_CREATE;
73 return Construct(regPath, openMode, null);
76 // This method will be removed in a future release.
78 Registry::Construct(const String& regPath, long openMode, long option)
80 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
81 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
82 "The length of the specified regPath is zero or exceeds system limitations.");
84 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
85 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
87 result r = pRegistryImpl->Construct(regPath, openMode, null);
90 if (r == E_SYSTEM && Tizen::App::_AppInfo::IsOspCompat() == true)
94 SysPropagate(NID_IO, r);
97 __pRegistryImpl = pRegistryImpl.release();
103 Registry::Construct(const String& regPath, const char* pOpenMode)
105 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
106 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
107 "The length of the specified regPath is zero or exceeds system limitations.");
109 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
110 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
112 result r = pRegistryImpl->Construct(regPath, pOpenMode, null);
113 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
115 __pRegistryImpl = pRegistryImpl.release();
121 Registry::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer& key)
123 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
124 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
125 "The length of the specified regPath is zero or exceeds system limitations.");
127 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
128 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
130 result r = pRegistryImpl->Construct(regPath, pOpenMode, &key);
131 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
133 __pRegistryImpl = pRegistryImpl.release();
139 Registry::Flush(void)
141 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
142 result r = E_SUCCESS;
144 r = __pRegistryImpl->Flush();
145 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Flush failed.", GetErrorMessage(r));
151 Registry::AddSection(const String& sectionName)
153 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
154 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
156 return __pRegistryImpl->AddSection(sectionName);
160 Registry::RemoveSection(const String& sectionName)
162 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
163 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
165 return __pRegistryImpl->RemoveSection(sectionName);
169 Registry::GetAllSectionNamesN(void) const
171 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
172 result r = E_SUCCESS;
175 r = __pRegistryImpl->GetSectionListN(&pList);
176 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
178 SetLastResult(E_SUCCESS);
183 Registry::GetAllEntryNamesN(const String& sectionName) const
185 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
186 return __pRegistryImpl->GetAllEntryNamesN(sectionName);
190 Registry::AddValue(const String& sectionName, const String& entryName, int entryValue)
192 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
193 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
194 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
196 return __pRegistryImpl->AddValue(sectionName, entryName, Integer::ToString(entryValue));
200 Registry::AddValue(const String& sectionName, const String& entryName, double entryValue)
202 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
203 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
204 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
206 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
210 Registry::AddValue(const String& sectionName, const String& entryName, float entryValue)
212 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
213 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
214 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
216 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
220 Registry::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
222 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
223 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
224 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
226 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
230 Registry::AddValue(const String& sectionName, const String& entryName, const UuId& entryValue)
232 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
233 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
234 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
236 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue.ToString());
240 Registry::AddValue(const String& sectionName, const String& entryName, const ByteBuffer& entryValue)
242 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
243 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
244 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
246 int size = entryValue.GetLimit();
247 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "invalid buffer length");
249 String strValEncoded;
251 result r = E_SUCCESS;
252 byte* pValue = const_cast< byte* >(entryValue.GetPointer());
255 for (int i = 0; i < size; i++)
257 r = strValEncoded.Format(3, L"%02x", (static_cast< unsigned char* >(pValue)[i]));
260 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
263 r = strVal.Append(strValEncoded);
266 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
271 return __pRegistryImpl->AddValue(sectionName, entryName, strVal);
275 Registry::GetValue(const String& sectionName, const String& entryName, int& retVal) const
277 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
278 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
279 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
281 result r = E_SUCCESS;
284 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
287 r = Integer::Parse(valStr, retVal);
290 r = E_PARSING_FAILED;
298 Registry::GetValue(const String& sectionName, const String& entryName, double& retVal) const
300 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
301 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
302 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
304 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
308 Registry::GetValue(const String& sectionName, const String& entryName, float& retVal) const
310 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
311 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
312 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
314 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
318 Registry::GetValue(const String& sectionName, const String& entryName, String& retVal) const
320 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
321 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
322 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
324 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
328 Registry::GetValue(const String& sectionName, const String& entryName, UuId& retVal) const
330 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
331 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
332 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
334 result r = E_SUCCESS;
337 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
340 r = UuId::Parse(valStr, retVal);
343 r = E_PARSING_FAILED;
351 Registry::GetValue(const String& sectionName, const String& entryName, ByteBuffer& retVal) const
353 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
354 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
355 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
357 int size = retVal.GetLimit();
358 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "Invalid buffer length");
360 result r = E_SUCCESS;
363 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
366 int length = valStr.GetLength();
367 int length2 = (size * 2);
368 int parseSize = (length2 <= length) ? length2 : length;
369 byte* pRetVal = const_cast< byte* >(retVal.GetPointer());
371 for (int i = 0, j = 0; i < parseSize; i++)
373 unsigned long ul = 0;
374 wchar_t ch = valStr[i];
377 if ((ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'f') || (ch >= L'A' && ch <= L'F'))
383 ul = wcstoul(tmp, null, 16);
384 (static_cast< unsigned char* >(pRetVal))[j++] = static_cast< unsigned char >(ul);
389 return E_PARSING_FAILED;
394 parseSize = parseSize/2;
395 SysAssertf(parseSize <= size, "The specified ByteBuffer is not enough to get the entry value from registry file.");
396 if (parseSize < size)
398 // resize buffer limit
399 r = retVal.SetLimit(parseSize);
400 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
407 Registry::SetValue(const String& sectionName, const String& entryName, int newValue)
409 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
410 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
411 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
413 return __pRegistryImpl->SetValue(sectionName, entryName, Integer::ToString(newValue));
417 Registry::SetValue(const String& sectionName, const String& entryName, double newValue)
419 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
420 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
421 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
423 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
427 Registry::SetValue(const String& sectionName, const String& entryName, float newValue)
429 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
430 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
431 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
433 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
437 Registry::SetValue(const String& sectionName, const String& entryName, const String& newValue)
439 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
440 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName is empty");
441 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName is empty");
442 //SysTryReturnResult(NID_IO, newValue.GetLength() > 0, E_INVALID_ARG, "newValue is empty");
444 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
448 Registry::SetValue(const String& sectionName, const String& entryName, const UuId& newValue)
450 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
451 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
452 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
454 return __pRegistryImpl->SetValue(sectionName, entryName, newValue.ToString());
458 Registry::SetValue(const String& sectionName, const String& entryName, const ByteBuffer& newValue)
460 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
461 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
462 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
464 int size = newValue.GetLimit();
465 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "invalid buffer length");
467 String strValEncoded;
469 result r = E_SUCCESS;
470 byte* pNewValue = const_cast< byte* >(newValue.GetPointer());
471 SysAssert(pNewValue);
473 for (int i = 0; i < size; i++)
475 r = strValEncoded.Format(3, L"%02x", (static_cast< unsigned char* >(pNewValue)[i]));
478 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
481 r = strVal.Append(strValEncoded);
484 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
489 return __pRegistryImpl->SetValue(sectionName, entryName, strVal);
493 Registry::RemoveValue(const String& sectionName, const String& entryName)
495 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
496 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
497 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
499 return __pRegistryImpl->RemoveValue(sectionName, entryName);
503 Registry::Remove(const String& regPath)
505 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
506 "regPath.GetLength <= 0 or long name was given.");
508 return _RegistryImpl::Remove(regPath);
512 Registry::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer& key)
514 SysTryReturnResult(NID_IO, plainRegPath.GetLength() > 0 && plainRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
515 "plainRegPath.GetLength <= 0 or long name was given.");
516 SysTryReturnResult(NID_IO, secureRegPath.GetLength() > 0 && secureRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
517 "secureRegPath.GetLength <= 0 or long name was given.");
518 SysTryReturnResult(NID_IO, key.GetLimit() > 0, E_INVALID_ARG, "key.GetLimit <= 0.");
520 return _RegistryImpl::ConvertToSecureRegistry(plainRegPath, secureRegPath, &key);
524 Registry::LockN(FileLockType lockType)
526 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
527 return __pRegistryImpl->LockN(lockType);
531 Registry::TryToLockN(FileLockType lockType)
533 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
534 return __pRegistryImpl->TryToLockN(lockType);