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 FApp_AppResourceImpl.cpp
20 * @brief This is the implementation file of the _AppResourceImpl class.
25 #include <libxml/tree.h>
26 #include <unique_ptr.h>
28 #include <FBaseString.h>
29 #include <FBaseBoolean.h>
30 #include <FBaseInteger.h>
31 #include <FBaseUtilStringUtil.h>
32 #include <FBaseSysLog.h>
34 #include <FIoDirectory.h>
35 #include <FAppPkgPackageInfo.h>
36 #include <FAppPkgPackageManager.h>
37 #include <FAppIAppSettingEventListener.h>
38 #include <FSec_AccessController.h>
39 #include "FApp_AppSettingImpl.h"
40 #include "FApp_AppInfo.h"
41 #include "FAppPkg_PackageManagerImpl.h"
42 #include "FAppPkg_PackageInfoImpl.h"
45 using namespace Tizen::App::Package;
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Collection;
48 using namespace Tizen::Io;
49 using namespace Tizen::Security;
50 using namespace Tizen::App;
53 namespace Tizen { namespace App
56 const int APP_ID_LENTH = 10;
57 const int MAX_CONTENT_LEN = 512;
58 const int MAX_LOCAL_BUFSIZE = 128;
59 const char* DBUS_PATH = "/setting/dbus_handler";
60 const char* DBUS_SIGNAL_INTERFACE = "org.tizen.setting.signal";
61 static const wchar_t DBUS_SIGNAL_PREFIX[] = L"Update_";
63 static const wchar_t RESOUCE_FILE_PATH[] = L"setting/";
64 static const wchar_t RESOUCE_FILE_NAME[] = L"setting";
65 static const wchar_t RESOUCE_FILE_EXT[] = L".xml";
67 static _AppSettingImpl* pAppSettingImplInstance = null;
69 _AppSettingImpl::_MutiInstanceManager _AppSettingImpl::__appIdMultiInstanceMgr; // static member
70 DBusConnection* pDBusConnection = null;
79 dbus_connection_close(pDBusConnection);
80 pDBusConnection = null;
84 static _CleanUpDBus cleanUpDBus;
86 _AppSettingImpl::_MutiInstanceManager::_MutiInstanceManager(void)
88 __stringToInstanceMap.Construct();
91 _AppSettingImpl::_MutiInstanceManager::~_MutiInstanceManager(void)
93 __stringToInstanceMap.RemoveAll(true); // Remove instance on exit.
97 _AppSettingImpl::_MutiInstanceManager::GetInstance(const String& version)
100 AppSetting* pAppSettingInstance = null;
101 String* pKeyStr = null;
103 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(version));
109 // Common creation part
110 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
111 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
112 GetErrorMessage(E_OUT_OF_MEMORY));
114 // Get current application context AppId
115 App* pApp = App::GetInstance();
117 SysTryCatch(NID_APP, pApp != null, , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
118 GetErrorMessage(E_SYSTEM));
119 appId = pApp->GetAppId();
121 r = pAppSettingImpl->Construct(appId, version);
122 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
124 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
125 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
126 GetErrorMessage(E_OUT_OF_MEMORY));
127 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
129 pKeyStr = new (std::nothrow) String(version);
130 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
132 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
133 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
134 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
136 return pAppSettingInstance;
139 delete pAppSettingInstance;
144 _AppSettingImpl::_MutiInstanceManager::GetInstanceByAppId(const AppId& appId)
146 result r = E_SUCCESS;
147 AppSetting* pAppSettingInstance = null;
148 String* pKeyStr = null;
150 r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
151 SysTryReturn(NID_APP, !IsFailed(r), null, E_PRIVILEGE_DENIED,
152 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
154 // Convert AppId to package type id.
156 appId.SubString(0, APP_ID_LENTH, packageId);
158 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(packageId));
164 _PackageManagerImpl* pPkgMgrImpl = _PackageManagerImpl::GetInstance();
165 SysTryReturn(NID_APP, pPkgMgrImpl != null, null, E_SYSTEM,
166 "[%s] A system error has been occurred. failed to get _PackageMaangerImpl.", GetErrorMessage(E_SYSTEM));
168 PackageInfo* pPackageInfo = null;
169 pPackageInfo = pPkgMgrImpl->GetPackageInfoN(packageId); // !E_SUCCESS for DB fail and query fail
170 SysTryReturn(NID_APP, pPackageInfo != null, null, E_APP_NOT_INSTALLED, "[%s] Propagating.", GetErrorMessage(E_APP_NOT_INSTALLED));
172 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo);
173 SysTryReturn(NID_APP, pPkgInfoImpl != null, null, E_SYSTEM,
174 "[%s] A system error has been occurred. failed to get _PackageInfoImpl.", GetErrorMessage(E_SYSTEM));
176 String rootPath = pPkgInfoImpl->GetAppRootPath();
179 // Common creation part
180 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
181 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
182 GetErrorMessage(E_OUT_OF_MEMORY));
184 r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
185 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
187 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
188 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
189 GetErrorMessage(E_OUT_OF_MEMORY));
190 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
192 pKeyStr = new (std::nothrow) String(packageId);
193 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
195 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
196 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
197 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
199 return pAppSettingInstance;
202 delete pAppSettingInstance;
208 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
210 result r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
211 SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED,
212 "The application does not have the privilege to call this method.");
214 // Convert AppId to package type id.
216 appId.SubString(0, APP_ID_LENTH, packageId);
218 AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
223 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
224 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
226 __stringToInstanceMap.Remove(packageId, true); // Delete item instance and remove item from __stringToInstanceMap
231 return E_OBJ_NOT_FOUND;
236 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
239 { // Iterate all element and remove correspond dbus filter
240 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
243 while (pEnum->MoveNext() == E_SUCCESS)
245 AppSetting* pAppSetting = null;
246 pAppSetting = static_cast<AppSetting*>(pEnum->GetValue());
247 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
248 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
252 __stringToInstanceMap.RemoveAll(true);
260 _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
261 : __pValue(pItemValue)
264 , __pXmlNode(pItemXmlNode)
270 Object* GetValue(void)
274 const Object* GetValue(void) const
286 xmlNodePtr GetXmlNode(void)
292 _SettingItem(const _SettingItem& rhs);
293 _SettingItem& operator =(const _SettingItem& rhs);
298 xmlNodePtr __pXmlNode;
301 class _ReverseStringComparer
302 : public Tizen::Base::Collection::IComparer
305 _ReverseStringComparer(void) {};
306 virtual ~_ReverseStringComparer(void) {};
307 virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
309 const String& objString1 = static_cast<const String&>(obj1);
310 const String& objString2 = static_cast<const String&>(obj2);
311 cmp = objString2.CompareTo(objString1); // reverse
316 _AppSettingImpl::_AppSettingImpl(void)
317 : __oldVersionInstance(false)
318 , __pEventListener(null)
324 _AppSettingImpl::~_AppSettingImpl(void)
331 __settingContainer.RemoveAll(true);
335 _AppSettingImpl::GetInstance(void)
338 result r = E_SUCCESS;
340 if (pAppSettingImplInstance == null)
342 pAppSettingImplInstance = new (std::nothrow) _AppSettingImpl();
343 SysTryReturn(NID_APP, pAppSettingImplInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
344 GetErrorMessage(E_OUT_OF_MEMORY));
346 // Get current application context AppId
347 App* pApp = App::GetInstance();
348 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
349 GetErrorMessage(E_SYSTEM));
351 appId = pApp->GetAppId();
352 r = pAppSettingImplInstance->Construct(appId);
353 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
355 return pAppSettingImplInstance;
358 delete pAppSettingImplInstance;
359 pAppSettingImplInstance = null;
364 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
367 //result r = E_SUCCESS;
369 static _MutiInstanceManager multiInstanceManager;
370 AppSetting* pVersionAppSetting = multiInstanceManager.GetInstance(version);
371 SysTryReturn(NID_APP, pVersionAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
372 GetErrorMessage(E_OUT_OF_MEMORY));
374 return pVersionAppSetting;
378 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
382 AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
383 return pAppSettingByAppId;
387 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
389 return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
393 _AppSettingImpl::ReleaseOtherAppInstances(void)
395 return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
398 Tizen::Base::Collection::IList*
399 _AppSettingImpl::GetAppSettingVersionListN(void)
401 result r = E_SUCCESS;
402 ArrayList* pVersionList = new (std::nothrow) ArrayList;
403 SysTryReturn(NID_APP, pVersionList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
404 GetErrorMessage(E_OUT_OF_MEMORY));
405 r = pVersionList->Construct();
406 SysTryReturn(NID_APP, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
407 GetErrorMessage(E_OUT_OF_MEMORY));
409 // Iterate file and add to list
410 String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
412 r = dir.Construct(dirPath);
413 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[%s] A system error has been occurred. Directory construction failed.",
414 GetErrorMessage(E_SYSTEM));
416 std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
417 SysTryReturn(NID_APP, pDirEnum != null, null, E_SYSTEM,
418 "[%s] A system error has been occurred. Directory enumerator getting failed.", GetErrorMessage(E_SYSTEM));
420 String settingName(RESOUCE_FILE_NAME);
421 String settingExt(RESOUCE_FILE_EXT);
422 while (pDirEnum->MoveNext() == E_SUCCESS)
424 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
425 if (!dirEntry.IsDirectory())
427 String fullName = dirEntry.GetName();
428 // Check name and extension to getting valid one
431 const int extLength = settingExt.GetLength();
432 const int posStart = settingName.GetLength();
433 const int posEnd = fullName.GetLength() - extLength;
434 if (posStart >= posEnd)
438 fullName.SubString(0, posStart, name);
439 fullName.SubString(posEnd, ext);
440 if ((name == settingName) && (ext == settingExt))
443 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
445 String* pVersionStr = new (std::nothrow) String(version);
446 SysTryReturn(NID_APP, pVersionStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
447 GetErrorMessage(E_OUT_OF_MEMORY));
448 pVersionList->Add(*pVersionStr);
454 if (pVersionList->GetCount())
456 static _ReverseStringComparer strComparator;
457 pVersionList->Sort(strComparator);
458 pVersionList->RemoveAt(0); // remove latest version from list.
465 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
467 const Object* pObject = __settingContainer.GetValue(id);
468 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
469 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
470 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
471 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
472 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
473 value = pBoolean->ToBool();
479 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
481 const Object* pObject = __settingContainer.GetValue(id);
482 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
483 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
484 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
485 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
486 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
487 value = pInteger->ToInt();
493 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
495 const Object* pObject = __settingContainer.GetValue(id);
496 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
497 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
498 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
499 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
500 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
507 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
509 Object* pObject = __settingContainer.GetValue(id);
510 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
511 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
512 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
513 Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
514 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
517 static const String strTrue(L"true");
518 static const String strFalse(L"false");
519 const String& strValue = value ? strTrue : strFalse;
520 UpdateProperty(pItem->GetXmlNode(), strValue);
524 static const String strIntTrue(L"1");
525 static const String strIntFalse(L"0");
526 SendMessage(id, value ? strIntTrue : strIntFalse, true);
528 if (__pEventListener)
530 __pEventListener->OnAppSettingChanged(id);
537 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
539 Object* pObject = __settingContainer.GetValue(id);
540 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
541 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
542 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
543 Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
544 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
546 SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
547 value, pItem->GetMin());
548 SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
549 value, pItem->GetMax());
552 String strValue = pInteger->ToString();
553 UpdateProperty(pItem->GetXmlNode(), strValue);
557 SendMessage(id, strValue, true);
559 if (__pEventListener)
561 __pEventListener->OnAppSettingChanged(id);
568 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
570 Object* pObject = __settingContainer.GetValue(id);
571 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
572 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
573 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
574 String* pString = dynamic_cast<String*>(pItem->GetValue());
575 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
577 SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
578 "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
579 SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
580 "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
583 UpdateProperty(pItem->GetXmlNode(), *pString);
587 SendMessage(id, *pString, false);
589 if (__pEventListener)
591 __pEventListener->OnAppSettingChanged(id);
598 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
600 __pEventListener = pListener;
605 _AppSettingImpl::GetInstance(AppSetting& appSetting)
607 return appSetting.__pAppSettingImpl;
610 const _AppSettingImpl*
611 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
613 return appSetting.__pAppSettingImpl;
617 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
619 result r = E_SUCCESS;
620 if (version.GetLength() > 0)
622 __oldVersionInstance = true;
624 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
626 __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
627 __filePath += RESOUCE_FILE_NAME;
628 if (version.GetLength())
630 __filePath += version;
632 __filePath += RESOUCE_FILE_EXT;
634 r = __settingContainer.Construct();
635 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
636 if (!__oldVersionInstance)
638 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
641 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
646 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
648 result r = E_SUCCESS;
649 SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
650 "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
652 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
653 __filePath = appRootPath + RESOUCE_FILE_PATH;
654 __filePath += RESOUCE_FILE_NAME;
655 __filePath += RESOUCE_FILE_EXT;
657 r = __settingContainer.Construct();
658 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
659 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
661 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
667 _AppSettingImpl::Load(void)
669 SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
670 "The instance of specified AppId does not have setting information.");
672 result r = E_SUCCESS;
673 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
674 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
676 __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
677 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
678 "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
680 xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
681 SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
683 VisitNode(rootNodePtr);
689 _AppSettingImpl::Save(void)
691 static const int XML_SAVE_FAILED = -1;
692 static const int XML_SAVE_FORMAT = 1;
694 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
695 "A system error has been occurred. XML Document not valid so can not save it.");
696 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
697 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
699 int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
700 SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
706 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
708 for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next) // Visit sibling node
710 // Check node name and add to collection.
711 if (nodePtr->type == XML_ELEMENT_NODE)
713 CheckNodeNameAndAddToCollection(nodePtr);
716 VisitNode(nodePtr->children);
721 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
723 static const char* pPropId = "id";
724 static const char* pPropValue = "value";
726 // 1. Get type (bool, integer, string, expandlist)
727 _ElementType detectedType = GetElementType(pNode);
729 // Make item and add to collection.
730 if (detectedType != ELEMENT_INVALID)
733 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
739 std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
740 SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
741 GetErrorMessage(E_OUT_OF_MEMORY));
746 { // 3. Get min(length), max(length) value
747 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
749 GetMinMaxValue(pNode, detectedType, min, max);
753 bool validValue = false;
754 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
757 strValue = String(reinterpret_cast<char*>(pValue));
762 // 5. Create value object from value
763 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
766 _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
767 SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
768 GetErrorMessage(E_OUT_OF_MEMORY));
770 { // 6. Add item to container
771 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
772 pStrId->GetPointer(), min, max, strValue.GetPointer());
773 __settingContainer.Add(*pStrId.release(), *pSettingItem);
777 SysLog(NID_APP, "Failed create new _SettingItem");
786 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
788 const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
789 const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
790 const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
791 const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
793 _ElementType type = ELEMENT_INVALID;
794 if (xmlStrcmp(pNode->name, pElementBool) == 0)
799 if (xmlStrcmp(pNode->name, pElementInteger) == 0)
801 type = ELEMENT_INTEGER;
804 if (xmlStrcmp(pNode->name, pElementString) == 0)
806 type = ELEMENT_STRING;
809 if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
811 type = ELEMENT_EXPANDLIST;
818 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
820 const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
821 const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
822 const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
823 const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
825 if (type == ELEMENT_INTEGER)
827 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
830 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
835 SysLog(NID_APP, "Failed to get 'min' value.");
838 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
841 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
846 SysLog(NID_APP, "Failed to get 'max' value.");
850 if (type == ELEMENT_STRING)
852 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
855 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
860 SysLog(NID_APP, "Failed to get 'minlength' value.");
863 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
866 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
871 SysLog(NID_APP, "Failed to get 'maxlength' value.");
876 SysLog(NID_APP, "Invalid element type.");
881 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
883 const String strTrue(L"true");
884 Object* pValueObject = null;
890 bool valueBool = false;
893 if (strValue == strTrue)
898 pValueObject = new (std::nothrow) Boolean(valueBool);
899 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
900 GetErrorMessage(E_OUT_OF_MEMORY));
904 case ELEMENT_INTEGER:
906 int valueInteger = 0;
909 Integer::Parse(strValue, valueInteger);
911 pValueObject = new (std::nothrow) Integer(valueInteger);
912 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
913 GetErrorMessage(E_OUT_OF_MEMORY));
918 case ELEMENT_EXPANDLIST:
920 String* pStringObject = new (std::nothrow) String;
921 SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
922 GetErrorMessage(E_OUT_OF_MEMORY));
923 if (pStringObject && validValue)
925 *pStringObject = strValue;
927 pValueObject = pStringObject;
931 case ELEMENT_INVALID:
932 // It already validate by caller;
933 SysLog(NID_APP, "Invalid element type.");
941 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
943 const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
947 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
948 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
956 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
959 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
967 SysLog(NID_APP, "StringToUtf8N is failed.");
974 _AppSettingImpl::InitizlizeDBus(void)
977 dbus_error_init(&error);
979 if (pDBusConnection == null)
981 char rule[MAX_LOCAL_BUFSIZE];
982 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
983 if (pDBusConnection == null)
985 SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
986 dbus_error_free(&error);
989 dbus_connection_setup_with_g_main(pDBusConnection, null);
991 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
992 dbus_bus_add_match(pDBusConnection, rule, &error);
993 if (dbus_error_is_set(&error))
995 SysLog(NID_APP, "Fail to add match : %s", error.message);
996 dbus_error_free(&error);
1001 // this value is vary by AppSetting instance for proper handling.
1002 // Should be explicit remove filter on ReleaseInstanceByAppId.
1003 if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
1005 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1006 dbus_error_free(&error);
1013 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1015 int my_pid = getpid();
1018 char* pValue = NULL;
1021 dbus_error_init(&error);
1023 _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1024 SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1025 "Not valid pAppSettingImpl from user_data");
1026 String signalString(DBUS_SIGNAL_PREFIX);
1027 signalString += pAppSettingImpl->__correspondAppId;
1028 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1029 SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1030 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1032 if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1034 if (dbus_message_get_args(message, &error,
1035 DBUS_TYPE_UINT32, &sender_pid,
1036 DBUS_TYPE_STRING, &pId,
1037 DBUS_TYPE_STRING, &pValue,
1038 DBUS_TYPE_INVALID) == FALSE)
1040 SysLog(NID_APP, "Fail to get data : %s", error.message);
1041 dbus_error_free(&error);
1042 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1045 if (sender_pid != 0 && my_pid != sender_pid)
1047 pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1049 return DBUS_HANDLER_RESULT_HANDLED;
1053 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1058 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1060 const String delimiter(L"|");
1063 int indexOfDelimiter = 0;
1065 // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1066 // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1069 result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1070 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1071 strOrg.SubString(0, indexOfDelimiter, strId); // Get left string of '|'
1075 r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1076 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1077 strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1079 const Object* pObject = __settingContainer.GetValue(strId);
1082 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1083 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1084 GetErrorMessage(E_SYSTEM));
1085 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1088 static const String strFalse(L"0");
1089 bool valueBool = true;
1091 if (strValue == strFalse)
1095 SetValue(strId, valueBool, false);
1098 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1101 int valueInteger = 0;
1102 Integer::Parse(strValue, valueInteger);
1103 SetValue(strId, valueInteger, false);
1106 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1109 SetValue(strId, strValue, false);
1112 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1116 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1121 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1123 if (__oldVersionInstance)
1125 return true; // NOP for old version instance.
1128 if (!pDBusConnection)
1130 SysLog(NID_APP, "DBus connection invalid");
1135 const String strRightOfId(L"|N/A");
1136 const String strLeftOfValueIntType(L"INT|");
1137 const String strLeftOfValueStrType(L"STRING|");
1139 String strId = id + strRightOfId;
1143 strValue = strLeftOfValueIntType;
1147 strValue = strLeftOfValueStrType;
1150 String signalString(DBUS_SIGNAL_PREFIX);
1151 signalString += __correspondAppId;
1153 std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1154 std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1155 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1156 if (pBufferId && pBufferValue && pBufferSignal)
1158 int sender_pid = getpid();
1159 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1160 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1161 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1163 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1164 if (dbus_message_append_args(pMessageWihtAppId,
1165 DBUS_TYPE_UINT32, &sender_pid,
1166 DBUS_TYPE_STRING, &pCharBufferId,
1167 DBUS_TYPE_STRING, &pCharBufferValue,
1168 DBUS_TYPE_INVALID) == FALSE)
1170 SysLog(NID_APP, "DBus connection invalid");
1174 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1176 SysLog(NID_APP, "Fail to send message");
1180 dbus_connection_flush(pDBusConnection);
1181 dbus_message_unref(pMessageWihtAppId);
1185 SysLog(NID_APP, "System error: memory full ?");
1192 _AppSettingImpl::CreateAppSetting(void)
1194 AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1195 SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1196 GetErrorMessage(E_OUT_OF_MEMORY));
1202 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1204 appSetting.__pAppSettingImpl = &impl;