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 "FApp_AppSettingImpl.h"
38 #include "FApp_AppInfo.h"
39 #include "FAppPkg_PackageManagerImpl.h"
40 #include "FAppPkg_PackageInfoImpl.h"
43 using namespace Tizen::App::Package;
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Io;
47 using namespace Tizen::App;
50 namespace Tizen { namespace App
53 const int APP_ID_LENTH = 10;
54 const int MAX_LOCAL_BUFSIZE = 128;
55 const char* DBUS_PATH = "/setting/dbus_handler";
56 const char* DBUS_SIGNAL_INTERFACE = "org.tizen.setting.signal";
57 static const wchar_t DBUS_SIGNAL_PREFIX[] = L"Update_";
59 static const wchar_t RESOUCE_FILE_PATH[] = L"setting/";
60 static const wchar_t RESOUCE_FILE_NAME[] = L"setting";
61 static const wchar_t RESOUCE_FILE_EXT[] = L".xml";
63 static _AppSettingImpl* pAppSettingImplInstance = null;
65 _AppSettingImpl::_MutiInstanceManager _AppSettingImpl::__appIdMultiInstanceMgr; // static member
66 DBusConnection* pDBusConnection = null;
75 dbus_connection_close(pDBusConnection);
76 pDBusConnection = null;
80 static _CleanUpDBus cleanUpDBus;
82 _AppSettingImpl::_MutiInstanceManager::_MutiInstanceManager(void)
84 __stringToInstanceMap.Construct();
87 _AppSettingImpl::_MutiInstanceManager::~_MutiInstanceManager(void)
89 __stringToInstanceMap.RemoveAll(true); // Remove instance on exit.
93 _AppSettingImpl::_MutiInstanceManager::GetInstance(const String& version)
96 AppSetting* pAppSettingInstance = null;
97 String* pKeyStr = null;
99 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(version));
105 // Common creation part
106 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
107 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
108 GetErrorMessage(E_OUT_OF_MEMORY));
110 // Get current application context AppId
111 App* pApp = App::GetInstance();
113 SysTryCatch(NID_APP, pApp != null, , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
114 GetErrorMessage(E_SYSTEM));
115 appId = pApp->GetAppId();
117 r = pAppSettingImpl->Construct(appId, version);
118 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
120 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
121 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
122 GetErrorMessage(E_OUT_OF_MEMORY));
123 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
125 pKeyStr = new (std::nothrow) String(version);
126 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
128 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
129 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
130 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
132 return pAppSettingInstance;
135 delete pAppSettingInstance;
140 _AppSettingImpl::_MutiInstanceManager::GetInstanceByAppId(const AppId& appId)
142 result r = E_SUCCESS;
143 AppSetting* pAppSettingInstance = null;
144 String* pKeyStr = null;
146 // Convert AppId to package type id.
148 appId.SubString(0, APP_ID_LENTH, packageId);
150 AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(packageId));
156 _PackageManagerImpl* pPkgMgrImpl = _PackageManagerImpl::GetInstance();
157 SysTryReturn(NID_APP, pPkgMgrImpl != null, null, E_SYSTEM,
158 "[%s] A system error has been occurred. failed to get _PackageMaangerImpl.", GetErrorMessage(E_SYSTEM));
160 std::unique_ptr<PackageInfo> pPackageInfo(pPkgMgrImpl->GetPackageInfoN(packageId)); // !E_SUCCESS for DB fail and query fail
161 SysTryReturn(NID_APP, pPackageInfo != null, null, E_APP_NOT_INSTALLED, "[%s] Propagating.", GetErrorMessage(E_APP_NOT_INSTALLED));
163 const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
164 SysTryReturn(NID_APP, pPkgInfoImpl != null, null, E_SYSTEM,
165 "[%s] A system error has been occurred. failed to get _PackageInfoImpl.", GetErrorMessage(E_SYSTEM));
167 String rootPath = pPkgInfoImpl->GetAppRootPath();
170 // Common creation part
171 std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
172 SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
173 GetErrorMessage(E_OUT_OF_MEMORY));
175 r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
176 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
178 pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
179 SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
180 GetErrorMessage(E_OUT_OF_MEMORY));
181 _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
183 pKeyStr = new (std::nothrow) String(packageId);
184 SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
186 r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
187 SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
188 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
190 return pAppSettingInstance;
193 delete pAppSettingInstance;
198 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
200 // Convert AppId to package type id.
202 appId.SubString(0, APP_ID_LENTH, packageId);
204 AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
209 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
210 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
212 __stringToInstanceMap.Remove(packageId, true); // Delete item instance and remove item from __stringToInstanceMap
217 return E_OBJ_NOT_FOUND;
222 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
225 { // Iterate all element and remove correspond dbus filter
226 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
229 while (pEnum->MoveNext() == E_SUCCESS)
231 AppSetting* pAppSetting = null;
232 pAppSetting = static_cast<AppSetting*>(pEnum->GetValue());
233 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
234 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
238 __stringToInstanceMap.RemoveAll(true);
246 _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
247 : __pValue(pItemValue)
250 , __pXmlNode(pItemXmlNode)
256 Object* GetValue(void)
260 const Object* GetValue(void) const
272 xmlNodePtr GetXmlNode(void)
278 _SettingItem(const _SettingItem& rhs);
279 _SettingItem& operator =(const _SettingItem& rhs);
284 xmlNodePtr __pXmlNode;
287 class _ReverseStringComparer
288 : public Tizen::Base::Collection::IComparer
291 _ReverseStringComparer(void) {};
292 virtual ~_ReverseStringComparer(void) {};
293 virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
295 const String& objString1 = static_cast<const String&>(obj1);
296 const String& objString2 = static_cast<const String&>(obj2);
297 cmp = objString2.CompareTo(objString1); // reverse
302 _AppSettingImpl::_AppSettingImpl(void)
303 : __oldVersionInstance(false)
304 , __pEventListener(null)
310 _AppSettingImpl::~_AppSettingImpl(void)
317 __settingContainer.RemoveAll(true);
321 _AppSettingImpl::GetInstance(void)
324 result r = E_SUCCESS;
326 if (pAppSettingImplInstance == null)
328 pAppSettingImplInstance = new (std::nothrow) _AppSettingImpl();
329 SysTryReturn(NID_APP, pAppSettingImplInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
330 GetErrorMessage(E_OUT_OF_MEMORY));
332 // Get current application context AppId
333 App* pApp = App::GetInstance();
334 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
335 GetErrorMessage(E_SYSTEM));
337 appId = pApp->GetAppId();
338 r = pAppSettingImplInstance->Construct(appId);
339 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
341 return pAppSettingImplInstance;
344 delete pAppSettingImplInstance;
345 pAppSettingImplInstance = null;
350 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
353 //result r = E_SUCCESS;
355 static _MutiInstanceManager multiInstanceManager;
356 AppSetting* pVersionAppSetting = multiInstanceManager.GetInstance(version);
357 SysTryReturn(NID_APP, pVersionAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
358 GetErrorMessage(E_OUT_OF_MEMORY));
360 return pVersionAppSetting;
364 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
368 AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
369 return pAppSettingByAppId;
373 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
375 return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
379 _AppSettingImpl::ReleaseOtherAppInstances(void)
381 return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
384 Tizen::Base::Collection::IList*
385 _AppSettingImpl::GetAppSettingVersionListN(void)
387 result r = E_SUCCESS;
388 ArrayList* pVersionList = new (std::nothrow) ArrayList;
389 SysTryReturn(NID_APP, pVersionList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
390 GetErrorMessage(E_OUT_OF_MEMORY));
391 r = pVersionList->Construct();
392 SysTryReturn(NID_APP, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
393 GetErrorMessage(E_OUT_OF_MEMORY));
395 // Iterate file and add to list
396 String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
398 r = dir.Construct(dirPath);
399 SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[%s] A system error has been occurred. Directory construction failed.",
400 GetErrorMessage(E_SYSTEM));
402 std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
403 SysTryReturn(NID_APP, pDirEnum != null, null, E_SYSTEM,
404 "[%s] A system error has been occurred. Directory enumerator getting failed.", GetErrorMessage(E_SYSTEM));
406 String settingName(RESOUCE_FILE_NAME);
407 String settingExt(RESOUCE_FILE_EXT);
408 while (pDirEnum->MoveNext() == E_SUCCESS)
410 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
411 if (!dirEntry.IsDirectory())
413 String fullName = dirEntry.GetName();
414 // Check name and extension to getting valid one
417 const int extLength = settingExt.GetLength();
418 const int posStart = settingName.GetLength();
419 const int posEnd = fullName.GetLength() - extLength;
420 if (posStart >= posEnd)
424 fullName.SubString(0, posStart, name);
425 fullName.SubString(posEnd, ext);
426 if ((name == settingName) && (ext == settingExt))
429 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
431 String* pVersionStr = new (std::nothrow) String(version);
432 SysTryReturn(NID_APP, pVersionStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
433 GetErrorMessage(E_OUT_OF_MEMORY));
434 pVersionList->Add(*pVersionStr);
440 if (pVersionList->GetCount())
442 static _ReverseStringComparer strComparator;
443 pVersionList->Sort(strComparator);
444 pVersionList->RemoveAt(0); // remove latest version from list.
451 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
453 const Object* pObject = __settingContainer.GetValue(id);
454 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
455 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
456 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
457 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
458 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
459 value = pBoolean->ToBool();
465 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& 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 Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
472 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
473 value = pInteger->ToInt();
479 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& 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 String* pString = dynamic_cast<const String*>(pItem->GetValue());
486 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
493 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
495 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 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
498 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
499 Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
500 SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
503 static const String strTrue(L"true");
504 static const String strFalse(L"false");
505 const String& strValue = value ? strTrue : strFalse;
506 UpdateProperty(pItem->GetXmlNode(), strValue);
510 static const String strIntTrue(L"1");
511 static const String strIntFalse(L"0");
512 SendMessage(id, value ? strIntTrue : strIntFalse, true);
514 if (__pEventListener)
516 __pEventListener->OnAppSettingChanged(id);
523 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
525 Object* pObject = __settingContainer.GetValue(id);
526 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
527 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
528 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
529 Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
530 SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
532 SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
533 value, pItem->GetMin());
534 SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
535 value, pItem->GetMax());
538 String strValue = pInteger->ToString();
539 UpdateProperty(pItem->GetXmlNode(), strValue);
543 SendMessage(id, strValue, true);
545 if (__pEventListener)
547 __pEventListener->OnAppSettingChanged(id);
554 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
556 Object* pObject = __settingContainer.GetValue(id);
557 SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
558 _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
559 SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
560 String* pString = dynamic_cast<String*>(pItem->GetValue());
561 SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
563 SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
564 "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
565 SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
566 "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
569 UpdateProperty(pItem->GetXmlNode(), *pString);
573 SendMessage(id, *pString, false);
575 if (__pEventListener)
577 __pEventListener->OnAppSettingChanged(id);
584 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
586 __pEventListener = pListener;
591 _AppSettingImpl::GetInstance(AppSetting& appSetting)
593 return appSetting.__pAppSettingImpl;
596 const _AppSettingImpl*
597 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
599 return appSetting.__pAppSettingImpl;
603 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
605 result r = E_SUCCESS;
606 if (version.GetLength() > 0)
608 __oldVersionInstance = true;
610 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
612 __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
613 __filePath += RESOUCE_FILE_NAME;
614 if (version.GetLength())
616 __filePath += version;
618 __filePath += RESOUCE_FILE_EXT;
620 r = __settingContainer.Construct();
621 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
622 if (!__oldVersionInstance)
624 SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
627 SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
632 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
634 result r = E_SUCCESS;
635 SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
636 "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
638 correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId); // Use 10 char id value.
639 __filePath = appRootPath + RESOUCE_FILE_PATH;
640 __filePath += RESOUCE_FILE_NAME;
641 __filePath += RESOUCE_FILE_EXT;
643 r = __settingContainer.Construct();
644 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
645 SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
647 SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
653 _AppSettingImpl::Load(void)
655 SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
656 "The instance of specified AppId does not have setting information. %ls", __filePath.GetPointer());
658 result r = E_SUCCESS;
659 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
660 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
662 __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
663 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
664 "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
666 xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
667 SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
669 VisitNode(rootNodePtr);
675 _AppSettingImpl::Save(void)
677 static const int XML_SAVE_FAILED = -1;
678 static const int XML_SAVE_FORMAT = 1;
680 SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
681 "A system error has been occurred. XML Document not valid so can not save it.");
682 std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
683 SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
685 int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
686 SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
692 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
694 for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next) // Visit sibling node
696 // Check node name and add to collection.
697 if (nodePtr->type == XML_ELEMENT_NODE)
699 CheckNodeNameAndAddToCollection(nodePtr);
702 VisitNode(nodePtr->children);
707 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
709 static const char* pPropId = "id";
710 static const char* pPropValue = "value";
712 // 1. Get type (bool, integer, string, expandlist)
713 _ElementType detectedType = GetElementType(pNode);
715 // Make item and add to collection.
716 if (detectedType != ELEMENT_INVALID)
719 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
725 std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
726 SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
727 GetErrorMessage(E_OUT_OF_MEMORY));
732 { // 3. Get min(length), max(length) value
733 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
735 GetMinMaxValue(pNode, detectedType, min, max);
739 bool validValue = false;
740 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
743 strValue = String(reinterpret_cast<char*>(pValue));
748 // 5. Create value object from value
749 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
752 _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
753 SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
754 GetErrorMessage(E_OUT_OF_MEMORY));
756 { // 6. Add item to container
757 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
758 pStrId->GetPointer(), min, max, strValue.GetPointer());
759 __settingContainer.Add(*pStrId.release(), *pSettingItem);
763 SysLog(NID_APP, "Failed create new _SettingItem");
772 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
774 const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
775 const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
776 const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
777 const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
779 _ElementType type = ELEMENT_INVALID;
780 if (xmlStrcmp(pNode->name, pElementBool) == 0)
785 if (xmlStrcmp(pNode->name, pElementInteger) == 0)
787 type = ELEMENT_INTEGER;
790 if (xmlStrcmp(pNode->name, pElementString) == 0)
792 type = ELEMENT_STRING;
795 if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
797 type = ELEMENT_EXPANDLIST;
804 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
806 const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
807 const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
808 const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
809 const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
811 if (type == ELEMENT_INTEGER)
813 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
816 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
821 SysLog(NID_APP, "Failed to get 'min' value.");
824 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
827 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
832 SysLog(NID_APP, "Failed to get 'max' value.");
836 if (type == ELEMENT_STRING)
838 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
841 Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
846 SysLog(NID_APP, "Failed to get 'minlength' value.");
849 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
852 Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
857 SysLog(NID_APP, "Failed to get 'maxlength' value.");
862 SysLog(NID_APP, "Invalid element type.");
867 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
869 const String strTrue(L"true");
870 Object* pValueObject = null;
876 bool valueBool = false;
879 if (strValue == strTrue)
884 pValueObject = new (std::nothrow) Boolean(valueBool);
885 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
886 GetErrorMessage(E_OUT_OF_MEMORY));
890 case ELEMENT_INTEGER:
892 int valueInteger = 0;
895 Integer::Parse(strValue, valueInteger);
897 pValueObject = new (std::nothrow) Integer(valueInteger);
898 SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
899 GetErrorMessage(E_OUT_OF_MEMORY));
904 case ELEMENT_EXPANDLIST:
906 String* pStringObject = new (std::nothrow) String;
907 SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
908 GetErrorMessage(E_OUT_OF_MEMORY));
909 if (pStringObject && validValue)
911 *pStringObject = strValue;
913 pValueObject = pStringObject;
917 case ELEMENT_INVALID:
918 // It already validate by caller;
919 SysLog(NID_APP, "Invalid element type.");
927 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
929 const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
933 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
934 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
942 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
945 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
953 SysLog(NID_APP, "StringToUtf8N is failed.");
960 _AppSettingImpl::InitializeDBus(void)
963 dbus_error_init(&error);
965 if (pDBusConnection == null)
967 char rule[MAX_LOCAL_BUFSIZE];
968 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
969 if (pDBusConnection == null)
971 SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
972 dbus_error_free(&error);
975 dbus_connection_setup_with_g_main(pDBusConnection, null);
977 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
978 dbus_bus_add_match(pDBusConnection, rule, &error);
979 if (dbus_error_is_set(&error))
981 SysLog(NID_APP, "Fail to add match : %s", error.message);
982 dbus_error_free(&error);
987 // this value is vary by AppSetting instance for proper handling.
988 // Should be explicit remove filter on ReleaseInstanceByAppId.
989 if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
991 SysLog(NID_APP, "Fail to add filter : %s", error.message);
992 dbus_error_free(&error);
999 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1001 int my_pid = getpid();
1004 char* pValue = NULL;
1007 dbus_error_init(&error);
1009 _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1010 SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1011 "Not valid pAppSettingImpl from user_data");
1012 String signalString(DBUS_SIGNAL_PREFIX);
1013 signalString += pAppSettingImpl->__correspondAppId;
1014 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1015 SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1016 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1018 if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1020 if (dbus_message_get_args(message, &error,
1021 DBUS_TYPE_UINT32, &sender_pid,
1022 DBUS_TYPE_STRING, &pId,
1023 DBUS_TYPE_STRING, &pValue,
1024 DBUS_TYPE_INVALID) == FALSE)
1026 SysLog(NID_APP, "Fail to get data : %s", error.message);
1027 dbus_error_free(&error);
1028 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1031 if (sender_pid != 0 && my_pid != sender_pid)
1033 pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1035 return DBUS_HANDLER_RESULT_HANDLED;
1039 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1044 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1046 const String delimiter(L"|");
1049 int indexOfDelimiter = 0;
1051 // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1052 // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1055 result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1056 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1057 strOrg.SubString(0, indexOfDelimiter, strId); // Get left string of '|'
1061 r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1062 SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1063 strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1065 const Object* pObject = __settingContainer.GetValue(strId);
1068 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1069 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1070 GetErrorMessage(E_SYSTEM));
1071 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1074 static const String strFalse(L"0");
1075 bool valueBool = true;
1077 if (strValue == strFalse)
1081 SetValue(strId, valueBool, false);
1084 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1087 int valueInteger = 0;
1088 Integer::Parse(strValue, valueInteger);
1089 SetValue(strId, valueInteger, false);
1092 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1095 SetValue(strId, strValue, false);
1098 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1102 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1107 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1109 if (__oldVersionInstance)
1111 return true; // NOP for old version instance.
1114 if (!pDBusConnection)
1116 SysLog(NID_APP, "DBus connection invalid");
1121 const String strRightOfId(L"|N/A");
1122 const String strLeftOfValueIntType(L"INT|");
1123 const String strLeftOfValueStrType(L"STRING|");
1125 String strId = id + strRightOfId;
1129 strValue = strLeftOfValueIntType;
1133 strValue = strLeftOfValueStrType;
1136 String signalString(DBUS_SIGNAL_PREFIX);
1137 signalString += __correspondAppId;
1139 std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1140 std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1141 std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1142 if (pBufferId && pBufferValue && pBufferSignal)
1144 int sender_pid = getpid();
1145 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1146 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1147 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1149 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1150 if (dbus_message_append_args(pMessageWihtAppId,
1151 DBUS_TYPE_UINT32, &sender_pid,
1152 DBUS_TYPE_STRING, &pCharBufferId,
1153 DBUS_TYPE_STRING, &pCharBufferValue,
1154 DBUS_TYPE_INVALID) == FALSE)
1156 SysLog(NID_APP, "DBus connection invalid");
1160 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1162 SysLog(NID_APP, "Fail to send message");
1166 dbus_connection_flush(pDBusConnection);
1167 dbus_message_unref(pMessageWihtAppId);
1171 SysLog(NID_APP, "System error: memory full ?");
1178 _AppSettingImpl::CreateAppSetting(void)
1180 AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1181 SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1182 GetErrorMessage(E_OUT_OF_MEMORY));
1188 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1190 appSetting.__pAppSettingImpl = &impl;