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 FApp_AppResourceImpl.cpp
19 * @brief This is the implementation file of the _AppResourceImpl class.
24 #include <libxml/tree.h>
25 #include <unique_ptr.h>
27 #include <FBaseString.h>
28 #include <FBaseBoolean.h>
29 #include <FBaseInteger.h>
30 #include <FBaseUtilStringUtil.h>
31 #include <FBaseSysLog.h>
33 #include <FIoDirectory.h>
34 #include <FAppPkgPackageInfo.h>
35 #include <FAppPkgPackageManager.h>
36 #include <FAppIAppSettingEventListener.h>
37 #include <FSec_AccessController.h>
38 #include "FApp_AppSettingImpl.h"
39 #include "FApp_AppInfo.h"
40 #include "FAppPkg_PackageManagerImpl.h"
41 #include "FAppPkg_PackageInfoImpl.h"
44 using namespace Tizen::App::Package;
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Io;
48 using namespace Tizen::Security;
49 using namespace Tizen::App;
52 namespace Tizen { namespace App
55 const int APP_ID_LENTH = 10;
56 const int MAX_LOCAL_BUFSIZE = 128;
57 const char* DBUS_PATH = "/setting/dbus_handler";
58 const char* DBUS_SIGNAL_INTERFACE = "org.tizen.setting.signal";
59 static const wchar_t DBUS_SIGNAL_PREFIX[] = L"Update_";
61 static const wchar_t RESOUCE_FILE_PATH[] = L"setting/";
62 static const wchar_t RESOUCE_FILE_NAME[] = L"setting";
63 static const wchar_t RESOUCE_FILE_EXT[] = L".xml";
65 static _AppSettingImpl* pAppSettingImplInstance = null;
67 _AppSettingImpl::_MutiInstanceManager _AppSettingImpl::__appIdMultiInstanceMgr; // static member
68 DBusConnection* pDBusConnection = null;
77 dbus_connection_close(pDBusConnection);
78 pDBusConnection = null;
82 static _CleanUpDBus cleanUpDBus;
84 _AppSettingImpl::_MutiInstanceManager::_MutiInstanceManager(void)
86 __stringToInstanceMap.Construct();
89 _AppSettingImpl::_MutiInstanceManager::~_MutiInstanceManager(void)
91 __stringToInstanceMap.RemoveAll(true); // Remove instance on exit.
95 _AppSettingImpl::_MutiInstanceManager::GetInstance(const String& version)
98 AppSetting* pAppSettingInstance = null;
99 String* pKeyStr = null;
101 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(version));
107 // Common creation part
108 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
109 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
110 GetErrorMessage(E_OUT_OF_MEMORY));
112 // Get current application context AppId
113 App* pApp = App::GetInstance();
115 SysTryCatch(NID_APP, pApp != null, , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
116 GetErrorMessage(E_SYSTEM));
117 appId = pApp->GetAppId();
119 r = pAppSettingImpl->Construct(appId, version);
120 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
122 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
123 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
124 GetErrorMessage(E_OUT_OF_MEMORY));
125 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
127 pKeyStr = new (std::nothrow) String(version);
128 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
130 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
131 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
132 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
134 return pAppSettingInstance;
137 delete pAppSettingInstance;
142 _AppSettingImpl::_MutiInstanceManager::GetInstanceByAppId(const AppId& appId)
144 result r = E_SUCCESS;
145 AppSetting* pAppSettingInstance = null;
146 String* pKeyStr = null;
148 r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
149 SysTryReturn(NID_APP, !IsFailed(r), null, E_PRIVILEGE_DENIED,
150 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
152 // Convert AppId to package type id.
154 appId.SubString(0, APP_ID_LENTH, packageId);
156 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(packageId));
162 _PackageManagerImpl* pPkgMgrImpl = _PackageManagerImpl::GetInstance();
163 SysTryReturn(NID_APP, pPkgMgrImpl != null, null, E_SYSTEM,
164 "[%s] A system error has been occurred. failed to get _PackageMaangerImpl.", GetErrorMessage(E_SYSTEM));
166 std::unique_ptr<PackageInfo> pPackageInfo(pPkgMgrImpl->GetPackageInfoN(packageId)); // !E_SUCCESS for DB fail and query fail
167 SysTryReturn(NID_APP, pPackageInfo != null, null, E_APP_NOT_INSTALLED, "[%s] Propagating.", GetErrorMessage(E_APP_NOT_INSTALLED));
169 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
170 SysTryReturn(NID_APP, pPkgInfoImpl != null, null, E_SYSTEM,
171 "[%s] A system error has been occurred. failed to get _PackageInfoImpl.", GetErrorMessage(E_SYSTEM));
173 String rootPath = pPkgInfoImpl->GetAppRootPath();
176 // Common creation part
177 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
178 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
179 GetErrorMessage(E_OUT_OF_MEMORY));
181 r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
182 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
184 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
185 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
186 GetErrorMessage(E_OUT_OF_MEMORY));
187 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
189 pKeyStr = new (std::nothrow) String(packageId);
190 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
192 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
193 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
194 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
196 return pAppSettingInstance;
199 delete pAppSettingInstance;
204 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
206 result r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
207 SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED,
208 "The application does not have the privilege to call this method.");
210 // Convert AppId to package type id.
212 appId.SubString(0, APP_ID_LENTH, packageId);
214 AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
219 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
220 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
222 __stringToInstanceMap.Remove(packageId, true); // Delete item instance and remove item from __stringToInstanceMap
227 return E_OBJ_NOT_FOUND;
232 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
235 { // Iterate all element and remove correspond dbus filter
236 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
239 while (pEnum->MoveNext() == E_SUCCESS)
241 AppSetting* pAppSetting = null;
242 pAppSetting = static_cast<AppSetting*>(pEnum->GetValue());
243 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
244 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
248 __stringToInstanceMap.RemoveAll(true);
256 _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
257 : __pValue(pItemValue)
260 , __pXmlNode(pItemXmlNode)
266 Object* GetValue(void)
270 const Object* GetValue(void) const
282 xmlNodePtr GetXmlNode(void)
288 _SettingItem(const _SettingItem& rhs);
289 _SettingItem& operator =(const _SettingItem& rhs);
294 xmlNodePtr __pXmlNode;
297 class _ReverseStringComparer
298 : public Tizen::Base::Collection::IComparer
301 _ReverseStringComparer(void) {};
302 virtual ~_ReverseStringComparer(void) {};
303 virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
305 const String& objString1 = static_cast<const String&>(obj1);
306 const String& objString2 = static_cast<const String&>(obj2);
307 cmp = objString2.CompareTo(objString1); // reverse
312 _AppSettingImpl::_AppSettingImpl(void)
313 : __oldVersionInstance(false)
314 , __pEventListener(null)
320 _AppSettingImpl::~_AppSettingImpl(void)
327 __settingContainer.RemoveAll(true);
331 _AppSettingImpl::GetInstance(void)
334 result r = E_SUCCESS;
336 if (pAppSettingImplInstance == null)
338 pAppSettingImplInstance = new (std::nothrow) _AppSettingImpl();
339 SysTryReturn(NID_APP, pAppSettingImplInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
340 GetErrorMessage(E_OUT_OF_MEMORY));
342 // Get current application context AppId
343 App* pApp = App::GetInstance();
344 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
345 GetErrorMessage(E_SYSTEM));
347 appId = pApp->GetAppId();
348 r = pAppSettingImplInstance->Construct(appId);
349 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
351 return pAppSettingImplInstance;
354 delete pAppSettingImplInstance;
355 pAppSettingImplInstance = null;
360 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
363 //result r = E_SUCCESS;
365 static _MutiInstanceManager multiInstanceManager;
366 AppSetting* pVersionAppSetting = multiInstanceManager.GetInstance(version);
367 SysTryReturn(NID_APP, pVersionAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
368 GetErrorMessage(E_OUT_OF_MEMORY));
370 return pVersionAppSetting;
374 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
378 AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
379 return pAppSettingByAppId;
383 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
385 return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
389 _AppSettingImpl::ReleaseOtherAppInstances(void)
391 return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
394 Tizen::Base::Collection::IList*
395 _AppSettingImpl::GetAppSettingVersionListN(void)
397 result r = E_SUCCESS;
398 ArrayList* pVersionList = new (std::nothrow) ArrayList;
399 SysTryReturn(NID_APP, pVersionList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
400 GetErrorMessage(E_OUT_OF_MEMORY));
401 r = pVersionList->Construct();
402 SysTryReturn(NID_APP, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
403 GetErrorMessage(E_OUT_OF_MEMORY));
405 // Iterate file and add to list
406 String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
408 r = dir.Construct(dirPath);
409 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[%s] A system error has been occurred. Directory construction failed.",
410 GetErrorMessage(E_SYSTEM));
412 std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
413 SysTryReturn(NID_APP, pDirEnum != null, null, E_SYSTEM,
414 "[%s] A system error has been occurred. Directory enumerator getting failed.", GetErrorMessage(E_SYSTEM));
416 String settingName(RESOUCE_FILE_NAME);
417 String settingExt(RESOUCE_FILE_EXT);
418 while (pDirEnum->MoveNext() == E_SUCCESS)
420 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
421 if (!dirEntry.IsDirectory())
423 String fullName = dirEntry.GetName();
424 // Check name and extension to getting valid one
427 const int extLength = settingExt.GetLength();
428 const int posStart = settingName.GetLength();
429 const int posEnd = fullName.GetLength() - extLength;
430 if (posStart >= posEnd)
434 fullName.SubString(0, posStart, name);
435 fullName.SubString(posEnd, ext);
436 if ((name == settingName) && (ext == settingExt))
439 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
441 String* pVersionStr = new (std::nothrow) String(version);
442 SysTryReturn(NID_APP, pVersionStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
443 GetErrorMessage(E_OUT_OF_MEMORY));
444 pVersionList->Add(*pVersionStr);
450 if (pVersionList->GetCount())
452 static _ReverseStringComparer strComparator;
453 pVersionList->Sort(strComparator);
454 pVersionList->RemoveAt(0); // remove latest version from list.
461 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
463 const Object* pObject = __settingContainer.GetValue(id);
464 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
465 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
466 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
467 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
468 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
469 value = pBoolean->ToBool();
475 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
477 const Object* pObject = __settingContainer.GetValue(id);
478 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
479 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
480 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
481 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
482 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
483 value = pInteger->ToInt();
489 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
491 const Object* pObject = __settingContainer.GetValue(id);
492 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
493 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
494 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
495 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
496 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
503 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
505 Object* pObject = __settingContainer.GetValue(id);
506 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
507 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
508 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
509 Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
510 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
513 static const String strTrue(L"true");
514 static const String strFalse(L"false");
515 const String& strValue = value ? strTrue : strFalse;
516 UpdateProperty(pItem->GetXmlNode(), strValue);
520 static const String strIntTrue(L"1");
521 static const String strIntFalse(L"0");
522 SendMessage(id, value ? strIntTrue : strIntFalse, true);
524 if (__pEventListener)
526 __pEventListener->OnAppSettingChanged(id);
533 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
535 Object* pObject = __settingContainer.GetValue(id);
536 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
537 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
538 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
539 Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
540 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
542 SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
543 value, pItem->GetMin());
544 SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
545 value, pItem->GetMax());
548 String strValue = pInteger->ToString();
549 UpdateProperty(pItem->GetXmlNode(), strValue);
553 SendMessage(id, strValue, true);
555 if (__pEventListener)
557 __pEventListener->OnAppSettingChanged(id);
564 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
566 Object* pObject = __settingContainer.GetValue(id);
567 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
568 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
569 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
570 String* pString = dynamic_cast<String*>(pItem->GetValue());
571 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
573 SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
574 "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
575 SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
576 "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
579 UpdateProperty(pItem->GetXmlNode(), *pString);
583 SendMessage(id, *pString, false);
585 if (__pEventListener)
587 __pEventListener->OnAppSettingChanged(id);
594 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
596 __pEventListener = pListener;
601 _AppSettingImpl::GetInstance(AppSetting& appSetting)
603 return appSetting.__pAppSettingImpl;
606 const _AppSettingImpl*
607 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
609 return appSetting.__pAppSettingImpl;
613 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
615 result r = E_SUCCESS;
616 if (version.GetLength() > 0)
618 __oldVersionInstance = true;
620 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
622 __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
623 __filePath += RESOUCE_FILE_NAME;
624 if (version.GetLength())
626 __filePath += version;
628 __filePath += RESOUCE_FILE_EXT;
630 r = __settingContainer.Construct();
631 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
632 if (!__oldVersionInstance)
634 SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
637 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
642 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
644 result r = E_SUCCESS;
645 SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
646 "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
648 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
649 __filePath = appRootPath + RESOUCE_FILE_PATH;
650 __filePath += RESOUCE_FILE_NAME;
651 __filePath += RESOUCE_FILE_EXT;
653 r = __settingContainer.Construct();
654 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
655 SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
657 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
663 _AppSettingImpl::Load(void)
665 SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
666 "The instance of specified AppId does not have setting information. %ls", __filePath.GetPointer());
668 result r = E_SUCCESS;
669 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
670 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
672 __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
673 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
674 "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
676 xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
677 SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
679 VisitNode(rootNodePtr);
685 _AppSettingImpl::Save(void)
687 static const int XML_SAVE_FAILED = -1;
688 static const int XML_SAVE_FORMAT = 1;
690 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
691 "A system error has been occurred. XML Document not valid so can not save it.");
692 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
693 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
695 int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
696 SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
702 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
704 for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next) // Visit sibling node
706 // Check node name and add to collection.
707 if (nodePtr->type == XML_ELEMENT_NODE)
709 CheckNodeNameAndAddToCollection(nodePtr);
712 VisitNode(nodePtr->children);
717 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
719 static const char* pPropId = "id";
720 static const char* pPropValue = "value";
722 // 1. Get type (bool, integer, string, expandlist)
723 _ElementType detectedType = GetElementType(pNode);
725 // Make item and add to collection.
726 if (detectedType != ELEMENT_INVALID)
729 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
735 std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
736 SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
737 GetErrorMessage(E_OUT_OF_MEMORY));
742 { // 3. Get min(length), max(length) value
743 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
745 GetMinMaxValue(pNode, detectedType, min, max);
749 bool validValue = false;
750 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
753 strValue = String(reinterpret_cast<char*>(pValue));
758 // 5. Create value object from value
759 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
762 _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
763 SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
764 GetErrorMessage(E_OUT_OF_MEMORY));
766 { // 6. Add item to container
767 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
768 pStrId->GetPointer(), min, max, strValue.GetPointer());
769 __settingContainer.Add(*pStrId.release(), *pSettingItem);
773 SysLog(NID_APP, "Failed create new _SettingItem");
782 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
784 const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
785 const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
786 const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
787 const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
789 _ElementType type = ELEMENT_INVALID;
790 if (xmlStrcmp(pNode->name, pElementBool) == 0)
795 if (xmlStrcmp(pNode->name, pElementInteger) == 0)
797 type = ELEMENT_INTEGER;
800 if (xmlStrcmp(pNode->name, pElementString) == 0)
802 type = ELEMENT_STRING;
805 if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
807 type = ELEMENT_EXPANDLIST;
814 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
816 const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
817 const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
818 const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
819 const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
821 if (type == ELEMENT_INTEGER)
823 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
826 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
831 SysLog(NID_APP, "Failed to get 'min' value.");
834 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
837 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
842 SysLog(NID_APP, "Failed to get 'max' value.");
846 if (type == ELEMENT_STRING)
848 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
851 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
856 SysLog(NID_APP, "Failed to get 'minlength' value.");
859 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
862 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
867 SysLog(NID_APP, "Failed to get 'maxlength' value.");
872 SysLog(NID_APP, "Invalid element type.");
877 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
879 const String strTrue(L"true");
880 Object* pValueObject = null;
886 bool valueBool = false;
889 if (strValue == strTrue)
894 pValueObject = new (std::nothrow) Boolean(valueBool);
895 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
896 GetErrorMessage(E_OUT_OF_MEMORY));
900 case ELEMENT_INTEGER:
902 int valueInteger = 0;
905 Integer::Parse(strValue, valueInteger);
907 pValueObject = new (std::nothrow) Integer(valueInteger);
908 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
909 GetErrorMessage(E_OUT_OF_MEMORY));
914 case ELEMENT_EXPANDLIST:
916 String* pStringObject = new (std::nothrow) String;
917 SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
918 GetErrorMessage(E_OUT_OF_MEMORY));
919 if (pStringObject && validValue)
921 *pStringObject = strValue;
923 pValueObject = pStringObject;
927 case ELEMENT_INVALID:
928 // It already validate by caller;
929 SysLog(NID_APP, "Invalid element type.");
937 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
939 const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
943 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
944 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
952 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
955 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
963 SysLog(NID_APP, "StringToUtf8N is failed.");
970 _AppSettingImpl::InitializeDBus(void)
973 dbus_error_init(&error);
975 if (pDBusConnection == null)
977 char rule[MAX_LOCAL_BUFSIZE];
978 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
979 if (pDBusConnection == null)
981 SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
982 dbus_error_free(&error);
985 dbus_connection_setup_with_g_main(pDBusConnection, null);
987 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
988 dbus_bus_add_match(pDBusConnection, rule, &error);
989 if (dbus_error_is_set(&error))
991 SysLog(NID_APP, "Fail to add match : %s", error.message);
992 dbus_error_free(&error);
997 // this value is vary by AppSetting instance for proper handling.
998 // Should be explicit remove filter on ReleaseInstanceByAppId.
999 if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
1001 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1002 dbus_error_free(&error);
1009 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1011 int my_pid = getpid();
1014 char* pValue = NULL;
1017 dbus_error_init(&error);
1019 _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1020 SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1021 "Not valid pAppSettingImpl from user_data");
1022 String signalString(DBUS_SIGNAL_PREFIX);
1023 signalString += pAppSettingImpl->__correspondAppId;
1024 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1025 SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1026 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1028 if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1030 if (dbus_message_get_args(message, &error,
1031 DBUS_TYPE_UINT32, &sender_pid,
1032 DBUS_TYPE_STRING, &pId,
1033 DBUS_TYPE_STRING, &pValue,
1034 DBUS_TYPE_INVALID) == FALSE)
1036 SysLog(NID_APP, "Fail to get data : %s", error.message);
1037 dbus_error_free(&error);
1038 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1041 if (sender_pid != 0 && my_pid != sender_pid)
1043 pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1045 return DBUS_HANDLER_RESULT_HANDLED;
1049 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1054 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1056 const String delimiter(L"|");
1059 int indexOfDelimiter = 0;
1061 // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1062 // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1065 result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1066 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1067 strOrg.SubString(0, indexOfDelimiter, strId); // Get left string of '|'
1071 r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1072 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1073 strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1075 const Object* pObject = __settingContainer.GetValue(strId);
1078 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1079 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1080 GetErrorMessage(E_SYSTEM));
1081 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1084 static const String strFalse(L"0");
1085 bool valueBool = true;
1087 if (strValue == strFalse)
1091 SetValue(strId, valueBool, false);
1094 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1097 int valueInteger = 0;
1098 Integer::Parse(strValue, valueInteger);
1099 SetValue(strId, valueInteger, false);
1102 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1105 SetValue(strId, strValue, false);
1108 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1112 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1117 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1119 if (__oldVersionInstance)
1121 return true; // NOP for old version instance.
1124 if (!pDBusConnection)
1126 SysLog(NID_APP, "DBus connection invalid");
1131 const String strRightOfId(L"|N/A");
1132 const String strLeftOfValueIntType(L"INT|");
1133 const String strLeftOfValueStrType(L"STRING|");
1135 String strId = id + strRightOfId;
1139 strValue = strLeftOfValueIntType;
1143 strValue = strLeftOfValueStrType;
1146 String signalString(DBUS_SIGNAL_PREFIX);
1147 signalString += __correspondAppId;
1149 std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1150 std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1151 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1152 if (pBufferId && pBufferValue && pBufferSignal)
1154 int sender_pid = getpid();
1155 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1156 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1157 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1159 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1160 if (dbus_message_append_args(pMessageWihtAppId,
1161 DBUS_TYPE_UINT32, &sender_pid,
1162 DBUS_TYPE_STRING, &pCharBufferId,
1163 DBUS_TYPE_STRING, &pCharBufferValue,
1164 DBUS_TYPE_INVALID) == FALSE)
1166 SysLog(NID_APP, "DBus connection invalid");
1170 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1172 SysLog(NID_APP, "Fail to send message");
1176 dbus_connection_flush(pDBusConnection);
1177 dbus_message_unref(pMessageWihtAppId);
1181 SysLog(NID_APP, "System error: memory full ?");
1188 _AppSettingImpl::CreateAppSetting(void)
1190 AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1191 SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1192 GetErrorMessage(E_OUT_OF_MEMORY));
1198 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1200 appSetting.__pAppSettingImpl = &impl;