2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FIoRegistry.cpp
19 * @brief This is the implementation file for %Registry class.
24 #include <unique_ptr.h>
26 #include <FBaseInteger.h>
27 #include <FBaseDouble.h>
28 #include <FBaseFloat.h>
29 #include <FBaseUuId.h>
30 #include <FBaseString.h>
31 #include <FBaseByteBuffer.h>
32 #include <FBaseUtilStringTokenizer.h>
33 #include <FBaseResult.h>
34 #include <FBaseSysLog.h>
35 #include <FIoRegistry.h>
37 #include <FBase_StringConverter.h>
38 #include <FApp_AppInfo.h>
39 #include "FIo_RegistryImpl.h"
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Utility;
44 using namespace Tizen::Base::Collection;
46 namespace Tizen { namespace Io
49 Registry::Registry(void)
50 : __pRegistryImpl(null)
54 Registry::~Registry(void)
56 delete __pRegistryImpl;
57 __pRegistryImpl = null;
60 // This method will be removed in a future release.
62 Registry::Construct(const String& regPath, bool createIfNotExist)
64 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
66 long openMode = REG_OPEN_READ_WRITE;
69 openMode |= REG_OPEN_CREATE;
72 return Construct(regPath, openMode, null);
75 // This method will be removed in a future release.
77 Registry::Construct(const String& regPath, long openMode, long option)
79 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
80 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
81 "The length of the specified regPath is zero or exceeds system limitations.");
83 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
84 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
86 result r = pRegistryImpl->Construct(regPath, openMode, null);
89 if (r == E_SYSTEM && Tizen::App::_AppInfo::IsOspCompat() == true)
93 SysPropagate(NID_IO, r);
96 __pRegistryImpl = pRegistryImpl.release();
102 Registry::Construct(const String& regPath, const char* pOpenMode)
104 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
105 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
106 "The length of the specified regPath is zero or exceeds system limitations.");
108 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
109 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
111 result r = pRegistryImpl->Construct(regPath, pOpenMode, null);
112 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
114 __pRegistryImpl = pRegistryImpl.release();
120 Registry::Construct(const String& regPath, const char* pOpenMode, const ByteBuffer& key)
122 SysAssertf(__pRegistryImpl == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
123 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
124 "The length of the specified regPath is zero or exceeds system limitations.");
126 unique_ptr<_RegistryImpl> pRegistryImpl(new (std::nothrow) _RegistryImpl());
127 SysTryReturnResult(NID_IO, pRegistryImpl != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
129 result r = pRegistryImpl->Construct(regPath, pOpenMode, &key);
130 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
132 __pRegistryImpl = pRegistryImpl.release();
138 Registry::Flush(void)
140 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
141 result r = E_SUCCESS;
143 r = __pRegistryImpl->Flush();
144 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Flush failed.", GetErrorMessage(r));
150 Registry::AddSection(const String& sectionName)
152 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
153 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
155 return __pRegistryImpl->AddSection(sectionName);
159 Registry::RemoveSection(const String& sectionName)
161 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
162 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
164 return __pRegistryImpl->RemoveSection(sectionName);
168 Registry::GetAllSectionNamesN(void) const
170 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
171 result r = E_SUCCESS;
174 r = __pRegistryImpl->GetSectionListN(&pList);
175 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagating to caller...", GetErrorMessage(r));
177 SetLastResult(E_SUCCESS);
182 Registry::GetAllEntryNamesN(const String& sectionName) const
184 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
185 return __pRegistryImpl->GetAllEntryNamesN(sectionName);
189 Registry::AddValue(const String& sectionName, const String& entryName, int entryValue)
191 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
192 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
193 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
195 return __pRegistryImpl->AddValue(sectionName, entryName, Integer::ToString(entryValue));
199 Registry::AddValue(const String& sectionName, const String& entryName, double entryValue)
201 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
202 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
203 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
205 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
209 Registry::AddValue(const String& sectionName, const String& entryName, float entryValue)
211 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
212 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
213 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
215 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
219 Registry::AddValue(const String& sectionName, const String& entryName, const String& entryValue)
221 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
222 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
223 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
225 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue);
229 Registry::AddValue(const String& sectionName, const String& entryName, const UuId& entryValue)
231 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
232 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
233 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
235 return __pRegistryImpl->AddValue(sectionName, entryName, entryValue.ToString());
239 Registry::AddValue(const String& sectionName, const String& entryName, const ByteBuffer& entryValue)
241 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
242 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
243 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
245 int size = entryValue.GetLimit();
246 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "invalid buffer length");
248 String strValEncoded;
250 result r = E_SUCCESS;
251 byte* pValue = const_cast< byte* >(entryValue.GetPointer());
254 for (int i = 0; i < size; i++)
256 r = strValEncoded.Format(3, L"%02x", (static_cast< unsigned char* >(pValue)[i]));
259 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
262 r = strVal.Append(strValEncoded);
265 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
270 return __pRegistryImpl->AddValue(sectionName, entryName, strVal);
274 Registry::GetValue(const String& sectionName, const String& entryName, int& retVal) const
276 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
277 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
278 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
280 result r = E_SUCCESS;
283 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
286 r = Integer::Parse(valStr, retVal);
289 r = E_PARSING_FAILED;
297 Registry::GetValue(const String& sectionName, const String& entryName, double& retVal) const
299 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
300 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
301 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
303 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
307 Registry::GetValue(const String& sectionName, const String& entryName, float& retVal) const
309 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
310 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
311 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
313 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
317 Registry::GetValue(const String& sectionName, const String& entryName, String& retVal) const
319 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
320 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
321 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
323 return __pRegistryImpl->GetValue(sectionName, entryName, retVal);
327 Registry::GetValue(const String& sectionName, const String& entryName, UuId& retVal) const
329 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
330 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
331 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
333 result r = E_SUCCESS;
336 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
339 r = UuId::Parse(valStr, retVal);
342 r = E_PARSING_FAILED;
350 Registry::GetValue(const String& sectionName, const String& entryName, ByteBuffer& retVal) const
352 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
353 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
354 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
356 int size = retVal.GetLimit();
357 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "Invalid buffer length");
359 result r = E_SUCCESS;
362 r = __pRegistryImpl->GetValue(sectionName, entryName, valStr);
365 int length = valStr.GetLength();
366 int length2 = (size * 2);
367 int parseSize = (length2 <= length) ? length2 : length;
368 byte* pRetVal = const_cast< byte* >(retVal.GetPointer());
370 for (int i = 0, j = 0; i < parseSize; i++)
372 unsigned long ul = 0;
373 wchar_t ch = valStr[i];
376 if ((ch >= L'0' && ch <= L'9') || (ch >= L'a' && ch <= L'f') || (ch >= L'A' && ch <= L'F'))
382 ul = wcstoul(tmp, null, 16);
383 (static_cast< unsigned char* >(pRetVal))[j++] = static_cast< unsigned char >(ul);
388 return E_PARSING_FAILED;
393 parseSize = parseSize/2;
394 SysAssertf(parseSize <= size, "The specified ByteBuffer is not enough to get the entry value from registry file.");
395 if (parseSize < size)
397 // resize buffer limit
398 r = retVal.SetLimit(parseSize);
399 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
406 Registry::SetValue(const String& sectionName, const String& entryName, int newValue)
408 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
409 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
410 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
412 return __pRegistryImpl->SetValue(sectionName, entryName, Integer::ToString(newValue));
416 Registry::SetValue(const String& sectionName, const String& entryName, double newValue)
418 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
419 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
420 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
422 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
426 Registry::SetValue(const String& sectionName, const String& entryName, float newValue)
428 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
429 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
430 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
432 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
436 Registry::SetValue(const String& sectionName, const String& entryName, const String& newValue)
438 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
439 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName is empty");
440 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName is empty");
441 //SysTryReturnResult(NID_IO, newValue.GetLength() > 0, E_INVALID_ARG, "newValue is empty");
443 return __pRegistryImpl->SetValue(sectionName, entryName, newValue);
447 Registry::SetValue(const String& sectionName, const String& entryName, const UuId& newValue)
449 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
450 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
451 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
453 return __pRegistryImpl->SetValue(sectionName, entryName, newValue.ToString());
457 Registry::SetValue(const String& sectionName, const String& entryName, const ByteBuffer& newValue)
459 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
460 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
461 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
463 int size = newValue.GetLimit();
464 SysTryReturnResult(NID_IO, size > 0, E_INVALID_ARG, "invalid buffer length");
466 String strValEncoded;
468 result r = E_SUCCESS;
469 byte* pNewValue = const_cast< byte* >(newValue.GetPointer());
470 SysAssert(pNewValue);
472 for (int i = 0; i < size; i++)
474 r = strValEncoded.Format(3, L"%02x", (static_cast< unsigned char* >(pNewValue)[i]));
477 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
480 r = strVal.Append(strValEncoded);
483 SysLog(NID_IO, "[%s] Propagated.", GetErrorMessage(r));
488 return __pRegistryImpl->SetValue(sectionName, entryName, strVal);
492 Registry::RemoveValue(const String& sectionName, const String& entryName)
494 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
495 SysTryReturnResult(NID_IO, sectionName.GetLength() > 0, E_INVALID_ARG, "sectionName.GetLength() <= 0");
496 SysTryReturnResult(NID_IO, entryName.GetLength() > 0, E_INVALID_ARG, "entryName.GetLength() <= 0");
498 return __pRegistryImpl->RemoveValue(sectionName, entryName);
502 Registry::Remove(const String& regPath)
504 SysTryReturnResult(NID_IO, regPath.GetLength() > 0 && regPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
505 "regPath.GetLength <= 0 or long name was given.");
507 return _RegistryImpl::Remove(regPath);
511 Registry::ConvertToSecureRegistry(const String& plainRegPath, const String& secureRegPath, const ByteBuffer& key)
513 SysTryReturnResult(NID_IO, plainRegPath.GetLength() > 0 && plainRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
514 "plainRegPath.GetLength <= 0 or long name was given.");
515 SysTryReturnResult(NID_IO, secureRegPath.GetLength() > 0 && secureRegPath.GetLength() <= PATH_MAX, E_INVALID_ARG,
516 "secureRegPath.GetLength <= 0 or long name was given.");
517 SysTryReturnResult(NID_IO, key.GetLimit() > 0, E_INVALID_ARG, "key.GetLimit <= 0.");
519 return _RegistryImpl::ConvertToSecureRegistry(plainRegPath, secureRegPath, &key);
523 Registry::LockN(FileLockType lockType)
525 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
526 return __pRegistryImpl->LockN(lockType);
530 Registry::TryToLockN(FileLockType lockType)
532 SysAssertf(__pRegistryImpl != null, "Not yet constructed. Construct() should be called before use.\n");
533 return __pRegistryImpl->TryToLockN(lockType);