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 value)
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(value));
200 Registry::AddValue(const String& sectionName, const String& entryName, double value)
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, Double::ToString(value));
210 Registry::AddValue(const String& sectionName, const String& entryName, float value)
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, Float::ToString(value));
220 Registry::AddValue(const String& sectionName, const String& entryName, const String& value)
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, value);
230 Registry::AddValue(const String& sectionName, const String& entryName, const UuId& value)
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, value.ToString());
240 Registry::AddValue(const String& sectionName, const String& entryName, const ByteBuffer& value)
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 = value.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* >(value.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 result r = E_SUCCESS;
307 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
310 r = Double::Parse(valStr, retVal);
313 r = E_PARSING_FAILED;
321 Registry::GetValue(const String& sectionName, const String& entryName, float& retVal) const
323 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
324 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
325 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
327 result r = E_SUCCESS;
330 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
333 r = Float::Parse(valStr, retVal);
336 r = E_PARSING_FAILED;
344 Registry::GetValue(const String& sectionName, const String& entryName, String& retVal) const
346 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
347 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
348 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
350 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
354 Registry::GetValue(const String& sectionName, const String& entryName, UuId& retVal) const
356 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
357 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
358 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
360 result r = E_SUCCESS;
363 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
366 r = UuId::Parse(valStr, retVal);
369 r = E_PARSING_FAILED;
377 Registry::GetValue(const String& sectionName, const String& entryName, ByteBuffer& retVal) const
379 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
380 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
381 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
383 int size = retVal.GetLimit();
384 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "Invalid buffer length");
386 result r = E_SUCCESS;
389 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
392 int length = valStr.GetLength();
393 int length2 = (size * 2);
394 int parseSize = (length2 <= length) ? length2 : length;
395 byte* pRetVal = const_cast< byte* >(retVal.GetPointer());
397 for (int i = 0, j = 0; i < parseSize; i++)
399 unsigned long ul = 0;
400 wchar_t ch = valStr[i];
403 if ((ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'f') || (ch >= L'A' && ch <= L'F'))
409 ul = wcstoul(tmp, null, 16);
410 (static_cast< unsigned char* >(pRetVal))[j++] = static_cast< unsigned char >(ul);
415 return E_PARSING_FAILED;
420 parseSize = parseSize/2;
421 SysAssertf(parseSize <= size, "The specified ByteBuffer is not enough to get the entry value from registry file.");
422 if (parseSize < size)
424 // resize buffer limit
425 r = retVal.SetLimit(parseSize);
426 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
433 Registry::SetValue(const String& sectionName, const String& entryName, int newValue)
435 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
436 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
437 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
439 return __pRegistryImpl->SetValue(sectionName, entryName, Integer::ToString(newValue));
443 Registry::SetValue(const String& sectionName, const String& entryName, double newValue)
445 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
446 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
447 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
449 return __pRegistryImpl->SetValue(sectionName, entryName, Double::ToString(newValue));
453 Registry::SetValue(const String& sectionName, const String& entryName, float newValue)
455 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
456 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
457 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
459 return __pRegistryImpl->SetValue(sectionName, entryName, Float::ToString(newValue));
463 Registry::SetValue(const String& sectionName, const String& entryName, const String& newValue)
465 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
466 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName is empty");
467 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName is empty");
468 //SysTryReturnResult(NID_IO, newValue.GetLength() > 0, E_INVALID_ARG, "newValue is empty");
470 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
474 Registry::SetValue(const String& sectionName, const String& entryName, const UuId& newValue)
476 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
477 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
478 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
480 return __pRegistryImpl->SetValue(sectionName, entryName, newValue.ToString());
484 Registry::SetValue(const String& sectionName, const String& entryName, const ByteBuffer& newValue)
486 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
487 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
488 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
490 int size = newValue.GetLimit();
491 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "invalid buffer length");
493 String strValEncoded;
495 result r = E_SUCCESS;
496 byte* pNewValue = const_cast< byte* >(newValue.GetPointer());
497 SysAssert(pNewValue);
499 for (int i = 0; i < size; i++)
501 r = strValEncoded.Format(3, L"%02x", (static_cast< unsigned char* >(pNewValue)[i]));
504 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
507 r = strVal.Append(strValEncoded);
510 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
515 return __pRegistryImpl->SetValue(sectionName, entryName, strVal);
519 Registry::RemoveValue(const String& sectionName, const String& entryName)
521 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
522 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
523 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
525 return __pRegistryImpl->RemoveValue(sectionName, entryName);
529 Registry::Remove(const String& regPath)
531 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
532 "regPath.GetLength <= 0 or long name was given.");
534 return _RegistryImpl::Remove(regPath);
538 Registry::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer& key)
540 SysTryReturnResult(NID_IO, plainRegPath.GetLength() > 0 && plainRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
541 "plainRegPath.GetLength <= 0 or long name was given.");
542 SysTryReturnResult(NID_IO, secureRegPath.GetLength() > 0 && secureRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
543 "secureRegPath.GetLength <= 0 or long name was given.");
544 SysTryReturnResult(NID_IO, key.GetLimit() > 0, E_INVALID_ARG, "key.GetLimit <= 0.");
546 return _RegistryImpl::ConvertToSecureRegistry(plainRegPath, secureRegPath, &key);
550 Registry::LockN(FileLockType lockType)
552 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
553 return __pRegistryImpl->LockN(lockType);
557 Registry::TryToLockN(FileLockType lockType)
559 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
560 return __pRegistryImpl->TryToLockN(lockType);