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 std::unique_ptr<PackageInfo> pPackageInfo(pPkgMgrImpl->GetPackageInfoN(packageId)); // !E_SUCCESS for DB fail and query fail
169 SysTryReturn(NID_APP, pPackageInfo != null, null, E_APP_NOT_INSTALLED, "[%s] Propagating.", GetErrorMessage(E_APP_NOT_INSTALLED));
171 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
172 SysTryReturn(NID_APP, pPkgInfoImpl != null, null, E_SYSTEM,
173 "[%s] A system error has been occurred. failed to get _PackageInfoImpl.", GetErrorMessage(E_SYSTEM));
175 String rootPath = pPkgInfoImpl->GetAppRootPath();
178 // Common creation part
179 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
180 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
181 GetErrorMessage(E_OUT_OF_MEMORY));
183 r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
184 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
186 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
187 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
188 GetErrorMessage(E_OUT_OF_MEMORY));
189 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
191 pKeyStr = new (std::nothrow) String(packageId);
192 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
194 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
195 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
196 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
198 return pAppSettingInstance;
201 delete pAppSettingInstance;
206 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
208 result r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
209 SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED,
210 "The application does not have the privilege to call this method.");
212 // Convert AppId to package type id.
214 appId.SubString(0, APP_ID_LENTH, packageId);
216 AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
221 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
222 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
224 __stringToInstanceMap.Remove(packageId, true); // Delete item instance and remove item from __stringToInstanceMap
229 return E_OBJ_NOT_FOUND;
234 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
237 { // Iterate all element and remove correspond dbus filter
238 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
241 while (pEnum->MoveNext() == E_SUCCESS)
243 AppSetting* pAppSetting = null;
244 pAppSetting = static_cast<AppSetting*>(pEnum->GetValue());
245 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
246 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
250 __stringToInstanceMap.RemoveAll(true);
258 _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
259 : __pValue(pItemValue)
262 , __pXmlNode(pItemXmlNode)
268 Object* GetValue(void)
272 const Object* GetValue(void) const
284 xmlNodePtr GetXmlNode(void)
290 _SettingItem(const _SettingItem& rhs);
291 _SettingItem& operator =(const _SettingItem& rhs);
296 xmlNodePtr __pXmlNode;
299 class _ReverseStringComparer
300 : public Tizen::Base::Collection::IComparer
303 _ReverseStringComparer(void) {};
304 virtual ~_ReverseStringComparer(void) {};
305 virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
307 const String& objString1 = static_cast<const String&>(obj1);
308 const String& objString2 = static_cast<const String&>(obj2);
309 cmp = objString2.CompareTo(objString1); // reverse
314 _AppSettingImpl::_AppSettingImpl(void)
315 : __oldVersionInstance(false)
316 , __pEventListener(null)
322 _AppSettingImpl::~_AppSettingImpl(void)
329 __settingContainer.RemoveAll(true);
333 _AppSettingImpl::GetInstance(void)
336 result r = E_SUCCESS;
338 if (pAppSettingImplInstance == null)
340 pAppSettingImplInstance = new (std::nothrow) _AppSettingImpl();
341 SysTryReturn(NID_APP, pAppSettingImplInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
342 GetErrorMessage(E_OUT_OF_MEMORY));
344 // Get current application context AppId
345 App* pApp = App::GetInstance();
346 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
347 GetErrorMessage(E_SYSTEM));
349 appId = pApp->GetAppId();
350 r = pAppSettingImplInstance->Construct(appId);
351 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
353 return pAppSettingImplInstance;
356 delete pAppSettingImplInstance;
357 pAppSettingImplInstance = null;
362 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
365 //result r = E_SUCCESS;
367 static _MutiInstanceManager multiInstanceManager;
368 AppSetting* pVersionAppSetting = multiInstanceManager.GetInstance(version);
369 SysTryReturn(NID_APP, pVersionAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
370 GetErrorMessage(E_OUT_OF_MEMORY));
372 return pVersionAppSetting;
376 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
380 AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
381 return pAppSettingByAppId;
385 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
387 return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
391 _AppSettingImpl::ReleaseOtherAppInstances(void)
393 return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
396 Tizen::Base::Collection::IList*
397 _AppSettingImpl::GetAppSettingVersionListN(void)
399 result r = E_SUCCESS;
400 ArrayList* pVersionList = new (std::nothrow) ArrayList;
401 SysTryReturn(NID_APP, pVersionList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
402 GetErrorMessage(E_OUT_OF_MEMORY));
403 r = pVersionList->Construct();
404 SysTryReturn(NID_APP, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
405 GetErrorMessage(E_OUT_OF_MEMORY));
407 // Iterate file and add to list
408 String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
410 r = dir.Construct(dirPath);
411 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[%s] A system error has been occurred. Directory construction failed.",
412 GetErrorMessage(E_SYSTEM));
414 std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
415 SysTryReturn(NID_APP, pDirEnum != null, null, E_SYSTEM,
416 "[%s] A system error has been occurred. Directory enumerator getting failed.", GetErrorMessage(E_SYSTEM));
418 String settingName(RESOUCE_FILE_NAME);
419 String settingExt(RESOUCE_FILE_EXT);
420 while (pDirEnum->MoveNext() == E_SUCCESS)
422 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
423 if (!dirEntry.IsDirectory())
425 String fullName = dirEntry.GetName();
426 // Check name and extension to getting valid one
429 const int extLength = settingExt.GetLength();
430 const int posStart = settingName.GetLength();
431 const int posEnd = fullName.GetLength() - extLength;
432 if (posStart >= posEnd)
436 fullName.SubString(0, posStart, name);
437 fullName.SubString(posEnd, ext);
438 if ((name == settingName) && (ext == settingExt))
441 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
443 String* pVersionStr = new (std::nothrow) String(version);
444 SysTryReturn(NID_APP, pVersionStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
445 GetErrorMessage(E_OUT_OF_MEMORY));
446 pVersionList->Add(*pVersionStr);
452 if (pVersionList->GetCount())
454 static _ReverseStringComparer strComparator;
455 pVersionList->Sort(strComparator);
456 pVersionList->RemoveAt(0); // remove latest version from list.
463 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
465 const Object* pObject = __settingContainer.GetValue(id);
466 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
467 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
468 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
469 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
470 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
471 value = pBoolean->ToBool();
477 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
479 const Object* pObject = __settingContainer.GetValue(id);
480 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
481 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
482 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
483 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
484 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
485 value = pInteger->ToInt();
491 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
493 const Object* pObject = __settingContainer.GetValue(id);
494 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
495 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
496 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
497 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
498 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
505 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
507 Object* pObject = __settingContainer.GetValue(id);
508 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
509 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
510 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
511 Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
512 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
515 static const String strTrue(L"true");
516 static const String strFalse(L"false");
517 const String& strValue = value ? strTrue : strFalse;
518 UpdateProperty(pItem->GetXmlNode(), strValue);
522 static const String strIntTrue(L"1");
523 static const String strIntFalse(L"0");
524 SendMessage(id, value ? strIntTrue : strIntFalse, true);
526 if (__pEventListener)
528 __pEventListener->OnAppSettingChanged(id);
535 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
537 Object* pObject = __settingContainer.GetValue(id);
538 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
539 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
540 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
541 Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
542 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
544 SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
545 value, pItem->GetMin());
546 SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
547 value, pItem->GetMax());
550 String strValue = pInteger->ToString();
551 UpdateProperty(pItem->GetXmlNode(), strValue);
555 SendMessage(id, strValue, true);
557 if (__pEventListener)
559 __pEventListener->OnAppSettingChanged(id);
566 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
568 Object* pObject = __settingContainer.GetValue(id);
569 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
570 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
571 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
572 String* pString = dynamic_cast<String*>(pItem->GetValue());
573 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
575 SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
576 "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
577 SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
578 "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
581 UpdateProperty(pItem->GetXmlNode(), *pString);
585 SendMessage(id, *pString, false);
587 if (__pEventListener)
589 __pEventListener->OnAppSettingChanged(id);
596 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
598 __pEventListener = pListener;
603 _AppSettingImpl::GetInstance(AppSetting& appSetting)
605 return appSetting.__pAppSettingImpl;
608 const _AppSettingImpl*
609 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
611 return appSetting.__pAppSettingImpl;
615 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
617 result r = E_SUCCESS;
618 if (version.GetLength() > 0)
620 __oldVersionInstance = true;
622 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
624 __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
625 __filePath += RESOUCE_FILE_NAME;
626 if (version.GetLength())
628 __filePath += version;
630 __filePath += RESOUCE_FILE_EXT;
632 r = __settingContainer.Construct();
633 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
634 if (!__oldVersionInstance)
636 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
639 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
644 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
646 result r = E_SUCCESS;
647 SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
648 "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
650 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
651 __filePath = appRootPath + RESOUCE_FILE_PATH;
652 __filePath += RESOUCE_FILE_NAME;
653 __filePath += RESOUCE_FILE_EXT;
655 r = __settingContainer.Construct();
656 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
657 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
659 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
665 _AppSettingImpl::Load(void)
667 SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
668 "The instance of specified AppId does not have setting information.");
670 result r = E_SUCCESS;
671 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
672 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
674 __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
675 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
676 "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
678 xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
679 SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
681 VisitNode(rootNodePtr);
687 _AppSettingImpl::Save(void)
689 static const int XML_SAVE_FAILED = -1;
690 static const int XML_SAVE_FORMAT = 1;
692 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
693 "A system error has been occurred. XML Document not valid so can not save it.");
694 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
695 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
697 int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
698 SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
704 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
706 for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next) // Visit sibling node
708 // Check node name and add to collection.
709 if (nodePtr->type == XML_ELEMENT_NODE)
711 CheckNodeNameAndAddToCollection(nodePtr);
714 VisitNode(nodePtr->children);
719 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
721 static const char* pPropId = "id";
722 static const char* pPropValue = "value";
724 // 1. Get type (bool, integer, string, expandlist)
725 _ElementType detectedType = GetElementType(pNode);
727 // Make item and add to collection.
728 if (detectedType != ELEMENT_INVALID)
731 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
737 std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
738 SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
739 GetErrorMessage(E_OUT_OF_MEMORY));
744 { // 3. Get min(length), max(length) value
745 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
747 GetMinMaxValue(pNode, detectedType, min, max);
751 bool validValue = false;
752 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
755 strValue = String(reinterpret_cast<char*>(pValue));
760 // 5. Create value object from value
761 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
764 _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
765 SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
766 GetErrorMessage(E_OUT_OF_MEMORY));
768 { // 6. Add item to container
769 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
770 pStrId->GetPointer(), min, max, strValue.GetPointer());
771 __settingContainer.Add(*pStrId.release(), *pSettingItem);
775 SysLog(NID_APP, "Failed create new _SettingItem");
784 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
786 const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
787 const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
788 const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
789 const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
791 _ElementType type = ELEMENT_INVALID;
792 if (xmlStrcmp(pNode->name, pElementBool) == 0)
797 if (xmlStrcmp(pNode->name, pElementInteger) == 0)
799 type = ELEMENT_INTEGER;
802 if (xmlStrcmp(pNode->name, pElementString) == 0)
804 type = ELEMENT_STRING;
807 if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
809 type = ELEMENT_EXPANDLIST;
816 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
818 const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
819 const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
820 const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
821 const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
823 if (type == ELEMENT_INTEGER)
825 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
828 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
833 SysLog(NID_APP, "Failed to get 'min' value.");
836 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
839 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
844 SysLog(NID_APP, "Failed to get 'max' value.");
848 if (type == ELEMENT_STRING)
850 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
853 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
858 SysLog(NID_APP, "Failed to get 'minlength' value.");
861 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
864 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
869 SysLog(NID_APP, "Failed to get 'maxlength' value.");
874 SysLog(NID_APP, "Invalid element type.");
879 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
881 const String strTrue(L"true");
882 Object* pValueObject = null;
888 bool valueBool = false;
891 if (strValue == strTrue)
896 pValueObject = new (std::nothrow) Boolean(valueBool);
897 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
898 GetErrorMessage(E_OUT_OF_MEMORY));
902 case ELEMENT_INTEGER:
904 int valueInteger = 0;
907 Integer::Parse(strValue, valueInteger);
909 pValueObject = new (std::nothrow) Integer(valueInteger);
910 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
911 GetErrorMessage(E_OUT_OF_MEMORY));
916 case ELEMENT_EXPANDLIST:
918 String* pStringObject = new (std::nothrow) String;
919 SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
920 GetErrorMessage(E_OUT_OF_MEMORY));
921 if (pStringObject && validValue)
923 *pStringObject = strValue;
925 pValueObject = pStringObject;
929 case ELEMENT_INVALID:
930 // It already validate by caller;
931 SysLog(NID_APP, "Invalid element type.");
939 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
941 const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
945 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
946 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
954 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
957 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
965 SysLog(NID_APP, "StringToUtf8N is failed.");
972 _AppSettingImpl::InitizlizeDBus(void)
975 dbus_error_init(&error);
977 if (pDBusConnection == null)
979 char rule[MAX_LOCAL_BUFSIZE];
980 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
981 if (pDBusConnection == null)
983 SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
984 dbus_error_free(&error);
987 dbus_connection_setup_with_g_main(pDBusConnection, null);
989 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
990 dbus_bus_add_match(pDBusConnection, rule, &error);
991 if (dbus_error_is_set(&error))
993 SysLog(NID_APP, "Fail to add match : %s", error.message);
994 dbus_error_free(&error);
999 // this value is vary by AppSetting instance for proper handling.
1000 // Should be explicit remove filter on ReleaseInstanceByAppId.
1001 if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
1003 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1004 dbus_error_free(&error);
1011 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1013 int my_pid = getpid();
1016 char* pValue = NULL;
1019 dbus_error_init(&error);
1021 _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1022 SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1023 "Not valid pAppSettingImpl from user_data");
1024 String signalString(DBUS_SIGNAL_PREFIX);
1025 signalString += pAppSettingImpl->__correspondAppId;
1026 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1027 SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1028 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1030 if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1032 if (dbus_message_get_args(message, &error,
1033 DBUS_TYPE_UINT32, &sender_pid,
1034 DBUS_TYPE_STRING, &pId,
1035 DBUS_TYPE_STRING, &pValue,
1036 DBUS_TYPE_INVALID) == FALSE)
1038 SysLog(NID_APP, "Fail to get data : %s", error.message);
1039 dbus_error_free(&error);
1040 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1043 if (sender_pid != 0 && my_pid != sender_pid)
1045 pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1047 return DBUS_HANDLER_RESULT_HANDLED;
1051 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1056 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1058 const String delimiter(L"|");
1061 int indexOfDelimiter = 0;
1063 // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1064 // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1067 result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1068 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1069 strOrg.SubString(0, indexOfDelimiter, strId); // Get left string of '|'
1073 r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1074 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1075 strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1077 const Object* pObject = __settingContainer.GetValue(strId);
1080 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1081 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1082 GetErrorMessage(E_SYSTEM));
1083 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1086 static const String strFalse(L"0");
1087 bool valueBool = true;
1089 if (strValue == strFalse)
1093 SetValue(strId, valueBool, false);
1096 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1099 int valueInteger = 0;
1100 Integer::Parse(strValue, valueInteger);
1101 SetValue(strId, valueInteger, false);
1104 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1107 SetValue(strId, strValue, false);
1110 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1114 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1119 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1121 if (__oldVersionInstance)
1123 return true; // NOP for old version instance.
1126 if (!pDBusConnection)
1128 SysLog(NID_APP, "DBus connection invalid");
1133 const String strRightOfId(L"|N/A");
1134 const String strLeftOfValueIntType(L"INT|");
1135 const String strLeftOfValueStrType(L"STRING|");
1137 String strId = id + strRightOfId;
1141 strValue = strLeftOfValueIntType;
1145 strValue = strLeftOfValueStrType;
1148 String signalString(DBUS_SIGNAL_PREFIX);
1149 signalString += __correspondAppId;
1151 std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1152 std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1153 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1154 if (pBufferId && pBufferValue && pBufferSignal)
1156 int sender_pid = getpid();
1157 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1158 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1159 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1161 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1162 if (dbus_message_append_args(pMessageWihtAppId,
1163 DBUS_TYPE_UINT32, &sender_pid,
1164 DBUS_TYPE_STRING, &pCharBufferId,
1165 DBUS_TYPE_STRING, &pCharBufferValue,
1166 DBUS_TYPE_INVALID) == FALSE)
1168 SysLog(NID_APP, "DBus connection invalid");
1172 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1174 SysLog(NID_APP, "Fail to send message");
1178 dbus_connection_flush(pDBusConnection);
1179 dbus_message_unref(pMessageWihtAppId);
1183 SysLog(NID_APP, "System error: memory full ?");
1190 _AppSettingImpl::CreateAppSetting(void)
1192 AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1193 SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1194 GetErrorMessage(E_OUT_OF_MEMORY));
1200 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1202 appSetting.__pAppSettingImpl = &impl;