4649cc27ba597998fddf2a3695ca934bb61ce3ce
[platform/framework/native/appfw.git] / src / app / FApp_AppSettingImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FApp_AppResourceImpl.cpp
20  * @brief       This is the implementation file of the _AppResourceImpl class.
21  */
22
23 #include <memory>
24 #include <unistd.h>
25 #include <libxml/tree.h>
26 #include <unique_ptr.h>
27 #include <FAppApp.h>
28 #include <FBaseString.h>
29 #include <FBaseBoolean.h>
30 #include <FBaseInteger.h>
31 #include <FBaseUtilStringUtil.h>
32 #include <FBaseSysLog.h>
33 #include <FIoFile.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"
43
44
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;
51
52
53 namespace Tizen { namespace App
54 {
55
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_";
62
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";
66
67 static _AppSettingImpl* pAppSettingImplInstance = null;
68
69 _AppSettingImpl::_MutiInstanceManager _AppSettingImpl::__appIdMultiInstanceMgr; // static member
70 DBusConnection* pDBusConnection = null;
71
72 class _CleanUpDBus
73 {
74 public:
75         ~_CleanUpDBus()
76         {
77                 if (pDBusConnection)
78                 {
79                         dbus_connection_close(pDBusConnection);
80                         pDBusConnection = null;
81                 }
82         }
83 };
84 static _CleanUpDBus cleanUpDBus;
85
86 _AppSettingImpl::_MutiInstanceManager::_MutiInstanceManager(void)
87 {
88         __stringToInstanceMap.Construct();
89 }
90
91 _AppSettingImpl::_MutiInstanceManager::~_MutiInstanceManager(void)
92 {
93         __stringToInstanceMap.RemoveAll(true);          // Remove instance on exit.
94 }
95
96 AppSetting*
97 _AppSettingImpl::_MutiInstanceManager::GetInstance(const String& version)
98 {
99         result r = E_SUCCESS;
100         AppSetting* pAppSettingInstance = null;
101         String* pKeyStr = null;
102
103         AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(version));
104         if (pAppSetting)
105         {
106                 return pAppSetting;
107         }
108
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));
113
114         // Get current application context AppId
115         App* pApp = App::GetInstance();
116         String appId;
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();
120
121         r = pAppSettingImpl->Construct(appId, version);
122         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
123
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());
128
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));
131
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));
135
136         return pAppSettingInstance;
137
138 CATCH:
139         delete pAppSettingInstance;
140         return null;
141 }
142
143 AppSetting*
144 _AppSettingImpl::_MutiInstanceManager::GetInstanceByAppId(const AppId& appId)
145 {
146         result r = E_SUCCESS;
147         AppSetting* pAppSettingInstance = null;
148         String* pKeyStr = null;
149
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));
153
154         // Convert AppId to package type id.
155         String packageId;
156         appId.SubString(0, APP_ID_LENTH, packageId);
157
158         AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(packageId));
159         if (pAppSetting)
160         {
161                 return pAppSetting;
162         }
163
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));
167
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));
171
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));
175
176         String rootPath = pPkgInfoImpl->GetAppRootPath();
177         rootPath += L"/";
178
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));
183
184         r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
185         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
186
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());
191
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));
194
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));
198
199         return pAppSettingInstance;
200
201 CATCH:
202         delete pAppSettingInstance;
203         delete pPackageInfo;
204         return null;
205 }
206
207 result
208 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
209 {
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.");
213
214         // Convert AppId to package type id.
215         String packageId;
216         appId.SubString(0, APP_ID_LENTH, packageId);
217
218         AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
219         if (pAppSetting)
220         {
221                 if (pDBusConnection)
222                 {
223                         _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
224                         dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
225                 }
226                 __stringToInstanceMap.Remove(packageId, true);  // Delete item instance and remove item from __stringToInstanceMap
227                 return E_SUCCESS;
228         }
229         else
230         {
231                 return E_OBJ_NOT_FOUND;
232         }
233 }
234
235 result
236 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
237 {
238         if (pDBusConnection)
239         {       // Iterate all element and remove correspond dbus filter
240                 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
241                 if (pEnum.get())
242                 {
243                         while (pEnum->MoveNext() == E_SUCCESS)
244                         {
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);
249                         }
250                 }
251         }
252         __stringToInstanceMap.RemoveAll(true);
253         return E_SUCCESS;
254 }
255
256 class _SettingItem
257         : public Object
258 {
259 public:
260         _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
261                 : __pValue(pItemValue)
262                 , __min(min)
263                 , __max(max)
264                 , __pXmlNode(pItemXmlNode)
265         {}
266         ~_SettingItem(void)
267         {
268                 delete __pValue;
269         }
270         Object* GetValue(void)
271         {
272                 return __pValue;
273         }
274         const Object* GetValue(void) const
275         {
276                 return __pValue;
277         }
278         int GetMin(void)
279         {
280                 return __min;
281         }
282         int GetMax(void)
283         {
284                 return __max;
285         }
286         xmlNodePtr GetXmlNode(void)
287         {
288                 return __pXmlNode;
289         }
290 private:
291         _SettingItem(void);
292         _SettingItem(const _SettingItem& rhs);
293         _SettingItem& operator =(const _SettingItem& rhs);
294 private:
295         Object* __pValue;
296         int __min;
297         int __max;
298         xmlNodePtr __pXmlNode;
299 };
300
301 class _ReverseStringComparer
302         : public Tizen::Base::Collection::IComparer
303 {
304 public:
305         _ReverseStringComparer(void) {};
306         virtual ~_ReverseStringComparer(void) {};
307         virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
308         {
309                 const String& objString1 = static_cast<const String&>(obj1);
310                 const String& objString2 = static_cast<const String&>(obj2);
311                 cmp = objString2.CompareTo(objString1); // reverse
312                 return E_SUCCESS;
313         }
314 };
315
316 _AppSettingImpl::_AppSettingImpl(void)
317         : __oldVersionInstance(false)
318         , __pEventListener(null)
319         , __pDoc(null)
320 {
321
322 }
323
324 _AppSettingImpl::~_AppSettingImpl(void)
325 {
326         if (__pDoc)
327         {
328                 xmlFreeDoc(__pDoc);
329                 __pDoc = null;
330         }
331         __settingContainer.RemoveAll(true);
332 }
333
334 _AppSettingImpl*
335 _AppSettingImpl::GetInstance(void)
336 {
337         ClearLastResult();
338         result r = E_SUCCESS;
339
340         if (pAppSettingImplInstance == null)
341         {
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));
345
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));
350                 String appId;
351                 appId = pApp->GetAppId();
352                 r = pAppSettingImplInstance->Construct(appId);
353                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
354         }
355         return pAppSettingImplInstance;
356
357 CATCH:
358         delete pAppSettingImplInstance;
359         pAppSettingImplInstance = null;
360         return null;
361 }
362
363 AppSetting*
364 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
365 {
366         ClearLastResult();
367         //result r = E_SUCCESS;
368
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));
373
374         return pVersionAppSetting;
375 }
376
377 AppSetting*
378 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
379 {
380         ClearLastResult();
381
382         AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
383         return pAppSettingByAppId;
384 }
385
386 result
387 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
388 {
389         return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
390 }
391
392 result
393 _AppSettingImpl::ReleaseOtherAppInstances(void)
394 {
395         return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
396 }
397
398 Tizen::Base::Collection::IList*
399 _AppSettingImpl::GetAppSettingVersionListN(void)
400 {
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));
408
409         // Iterate file and add to list
410         String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
411         Directory dir;
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));
415
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));
419
420         String settingName(RESOUCE_FILE_NAME);
421         String settingExt(RESOUCE_FILE_EXT);
422         while (pDirEnum->MoveNext() == E_SUCCESS)
423         {
424                 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
425                 if (!dirEntry.IsDirectory())
426                 {
427                         String fullName = dirEntry.GetName();
428                         // Check name and extension to getting valid one
429                         String name;
430                         String ext;
431                         const int extLength = settingExt.GetLength();
432                         const int posStart = settingName.GetLength();
433                         const int posEnd = fullName.GetLength() - extLength;
434                         if (posStart >= posEnd)
435                         {
436                                 continue;
437                         }
438                         fullName.SubString(0, posStart, name);
439                         fullName.SubString(posEnd, ext);
440                         if ((name == settingName) && (ext == settingExt))
441                         {
442                                 String version;
443                                 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
444                                 {
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);
449                                 }
450                         }
451                 }
452         }
453
454         if (pVersionList->GetCount())
455         {
456                 static _ReverseStringComparer strComparator;
457                 pVersionList->Sort(strComparator);
458                 pVersionList->RemoveAt(0);      // remove latest version from list.
459         }
460
461         return pVersionList;
462 }
463
464 result
465 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
466 {
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();
474
475         return E_SUCCESS;
476 }
477
478 result
479 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
480 {
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();
488
489         return E_SUCCESS;
490 }
491
492 result
493 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
494 {
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.");
501         value = *pString;
502
503         return E_SUCCESS;
504 }
505
506 result
507 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
508 {
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.");
515         *pBoolean = value;
516
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);
521         if (save)
522         {
523                 Save();
524                 static const String strIntTrue(L"1");
525                 static const String strIntFalse(L"0");
526                 SendMessage(id, value ? strIntTrue : strIntFalse, true);
527         }
528         if (__pEventListener)
529         {
530                 __pEventListener->OnAppSettingChanged(id);
531         }
532
533         return E_SUCCESS;
534 }
535
536 result
537 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
538 {
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.");
545
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());
550         *pInteger = value;
551
552         String strValue = pInteger->ToString();
553         UpdateProperty(pItem->GetXmlNode(), strValue);
554         if (save)
555         {
556                 Save();
557                 SendMessage(id, strValue, true);
558         }
559         if (__pEventListener)
560         {
561                 __pEventListener->OnAppSettingChanged(id);
562         }
563
564         return E_SUCCESS;
565 }
566
567 result
568 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
569 {
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.");
576
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());
581         *pString = value;
582
583         UpdateProperty(pItem->GetXmlNode(), *pString);
584         if (save)
585         {
586                 Save();
587                 SendMessage(id, *pString, false);
588         }
589         if (__pEventListener)
590         {
591                 __pEventListener->OnAppSettingChanged(id);
592         }
593
594         return E_SUCCESS;
595 }
596
597 result
598 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
599 {
600         __pEventListener = pListener;
601         return E_SUCCESS;
602 }
603
604 _AppSettingImpl*
605 _AppSettingImpl::GetInstance(AppSetting& appSetting)
606 {
607         return appSetting.__pAppSettingImpl;
608 }
609
610 const _AppSettingImpl*
611 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
612 {
613         return appSetting.__pAppSettingImpl;
614 }
615
616 result
617 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
618 {
619         result r = E_SUCCESS;
620         if (version.GetLength() > 0)
621         {
622                 __oldVersionInstance = true;
623         }
624         correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId);  // Use 10 char id value.
625
626         __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
627         __filePath += RESOUCE_FILE_NAME;
628         if (version.GetLength())
629         {
630                 __filePath += version;
631         }
632         __filePath += RESOUCE_FILE_EXT;
633
634         r = __settingContainer.Construct();
635         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
636         if (!__oldVersionInstance)
637         {
638                 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
639         }
640         r = Load();
641         SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
642         return r;
643 }
644
645 result
646 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
647 {
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));
651
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;
656
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.");
660         r = Load();
661         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
662
663         return r;
664 }
665
666 result
667 _AppSettingImpl::Load(void)
668 {
669         SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
670                                            "The instance of specified AppId does not have setting information.");
671
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.");
675
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());
679
680         xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
681         SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
682
683         VisitNode(rootNodePtr);
684
685         return r;
686 }
687
688 result
689 _AppSettingImpl::Save(void)
690 {
691         static const int XML_SAVE_FAILED = -1;
692         static const int XML_SAVE_FORMAT = 1;
693
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.");
698
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.");
701
702         return E_SUCCESS;
703 }
704
705 void
706 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
707 {
708         for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next)       // Visit sibling node
709         {
710                 // Check node name and add to collection.
711                 if (nodePtr->type == XML_ELEMENT_NODE)
712                 {
713                         CheckNodeNameAndAddToCollection(nodePtr);
714                 }
715                 // Visit child node
716                 VisitNode(nodePtr->children);
717         }
718 }
719
720 void
721 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
722 {
723         static const char* pPropId = "id";
724         static const char* pPropValue = "value";
725
726         // 1. Get type (bool, integer, string, expandlist)
727         _ElementType detectedType = GetElementType(pNode);
728
729         // Make item and add to collection.
730         if (detectedType != ELEMENT_INVALID)
731         {
732                 // 2. Get "id"
733                 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
734                 if (pId)
735                 {
736                         int min = 0;
737                         int max = 255;
738
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));
742
743                         xmlFree(pId);
744
745                         if (pStrId)
746                         {       // 3. Get min(length), max(length) value
747                                 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
748                                 {
749                                         GetMinMaxValue(pNode, detectedType, min, max);
750                                 }
751                                 // 4. Get "value"
752                                 String strValue;
753                                 bool validValue = false;
754                                 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
755                                 if (pValue)
756                                 {
757                                         strValue = String(reinterpret_cast<char*>(pValue));
758                                         xmlFree(pValue);
759                                         pValue = null;
760                                         validValue = true;
761                                 }
762                                 // 5. Create value object from value
763                                 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
764                                 if (pValueObject)
765                                 {
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));
769                                         if (pSettingItem)
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);
774                                         }
775                                         else
776                                         {
777                                                 SysLog(NID_APP, "Failed create new _SettingItem");
778                                         }
779                                 }
780                         } // pStrId
781                 } // pId
782         } // ELEMET_INVALID
783 }
784
785 _ElementType
786 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
787 {
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");
792
793         _ElementType type = ELEMENT_INVALID;
794         if (xmlStrcmp(pNode->name, pElementBool) == 0)
795         {
796                 type = ELEMENT_BOOL;
797         }
798         else
799         if (xmlStrcmp(pNode->name, pElementInteger) == 0)
800         {
801                 type = ELEMENT_INTEGER;
802         }
803         else
804         if (xmlStrcmp(pNode->name, pElementString) == 0)
805         {
806                 type = ELEMENT_STRING;
807         }
808         else
809         if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
810         {
811                 type = ELEMENT_EXPANDLIST;
812         }
813
814         return type;
815 }
816
817 void
818 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
819 {
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");
824
825         if (type == ELEMENT_INTEGER)
826         {
827                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
828                 if (pStrMin)
829                 {
830                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
831                         xmlFree(pStrMin);
832                 }
833                 else
834                 {
835                         SysLog(NID_APP, "Failed to get 'min' value.");
836                 }
837
838                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
839                 if (pStrMax)
840                 {
841                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
842                         xmlFree(pStrMax);
843                 }
844                 else
845                 {
846                         SysLog(NID_APP, "Failed to get 'max' value.");
847                 }
848         }
849         else
850         if (type == ELEMENT_STRING)
851         {
852                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
853                 if (pStrMin)
854                 {
855                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
856                         xmlFree(pStrMin);
857                 }
858                 else
859                 {
860                         SysLog(NID_APP, "Failed to get 'minlength' value.");
861                 }
862
863                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
864                 if (pStrMax)
865                 {
866                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
867                         xmlFree(pStrMax);
868                 }
869                 else
870                 {
871                         SysLog(NID_APP, "Failed to get 'maxlength' value.");
872                 }
873         }
874         else
875         {
876                 SysLog(NID_APP, "Invalid element type.");
877         }
878 }
879
880 Object*
881 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
882 {
883         const String strTrue(L"true");
884         Object* pValueObject = null;
885
886         switch (type)
887         {
888         case ELEMENT_BOOL:
889                 {
890                         bool valueBool = false;
891                         if (validValue)
892                         {
893                                 if (strValue == strTrue)
894                                 {
895                                         valueBool = true;
896                                 }
897                         }
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));
901                 }
902                 break;
903
904         case ELEMENT_INTEGER:
905                 {
906                         int valueInteger = 0;
907                         if (validValue)
908                         {
909                                 Integer::Parse(strValue, valueInteger);
910                         }
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));
914                 }
915                 break;
916
917         case ELEMENT_STRING:
918         case ELEMENT_EXPANDLIST:
919                 {
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)
924                         {
925                                 *pStringObject = strValue;
926                         }
927                         pValueObject = pStringObject;
928                 }
929                 break;
930
931         case ELEMENT_INVALID:
932                 // It already validate by caller;
933                 SysLog(NID_APP, "Invalid element type.");
934                 break;
935         }
936
937         return pValueObject;
938 }
939
940 bool
941 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
942 {
943         const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
944
945         if (value.IsEmpty())
946         {
947                 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
948                 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
949                 if (attrPtr)
950                 {
951                         return true;
952                 }
953         }
954         else
955         {
956                 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
957                 if (pBuf)
958                 {
959                         xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
960                         if (attrPtr)
961                         {
962                                 return true;
963                         }
964                 }
965                 else
966                 {
967                         SysLog(NID_APP, "StringToUtf8N is failed.");
968                 }
969         }
970         return false;
971 }
972
973 bool
974 _AppSettingImpl::InitizlizeDBus(void)
975 {
976         DBusError error;
977         dbus_error_init(&error);
978
979         if (pDBusConnection == null)
980         {
981                 char rule[MAX_LOCAL_BUFSIZE];
982                 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
983                 if (pDBusConnection == null)
984                 {
985                         SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
986                         dbus_error_free(&error);
987                         return false;
988                 }
989                 dbus_connection_setup_with_g_main(pDBusConnection, null);
990
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))
994                 {
995                         SysLog(NID_APP, "Fail to add match : %s", error.message);
996                         dbus_error_free(&error);
997                         return false;
998                 }
999         }
1000
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)
1004         {
1005                 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1006                 dbus_error_free(&error);
1007                 return false;
1008         }
1009         return true;
1010 }
1011
1012 DBusHandlerResult
1013 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1014 {
1015         int my_pid = getpid();
1016         int sender_pid = 0;
1017         char* pId = NULL;
1018         char* pValue = NULL;
1019
1020         DBusError error;
1021         dbus_error_init(&error);
1022
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());
1031
1032         if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1033         {
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)
1039                 {
1040                         SysLog(NID_APP, "Fail to get data : %s", error.message);
1041                         dbus_error_free(&error);
1042                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1043                 }
1044
1045                 if (sender_pid != 0 && my_pid != sender_pid)
1046                 {
1047                         pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1048                 }
1049                 return DBUS_HANDLER_RESULT_HANDLED;
1050         }
1051         else
1052         {
1053                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1054         }
1055 }
1056
1057 void
1058 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1059 {
1060         const String delimiter(L"|");
1061         String strId;
1062         String strValue;
1063         int indexOfDelimiter = 0;
1064
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.
1067         String strOrg(pId);
1068         strOrg.Trim();
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 '|'
1072
1073         strOrg = pValue;
1074         strOrg.Trim();
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 '|'
1078
1079         const Object* pObject = __settingContainer.GetValue(strId);
1080         if (pObject)
1081         {
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());
1086                 if (pBoolean)
1087                 {
1088                         static const String strFalse(L"0");
1089                         bool valueBool = true;
1090
1091                         if (strValue == strFalse)
1092                         {
1093                                 valueBool = false;
1094                         }
1095                         SetValue(strId, valueBool, false);
1096                         return;
1097                 }
1098                 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1099                 if (pInteger)
1100                 {
1101                         int valueInteger = 0;
1102                         Integer::Parse(strValue, valueInteger);
1103                         SetValue(strId, valueInteger, false);
1104                         return;
1105                 }
1106                 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1107                 if (pString)
1108                 {
1109                         SetValue(strId, strValue, false);
1110                         return;
1111                 }
1112                 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1113         }
1114         else
1115         {
1116                 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1117         }
1118 }
1119
1120 bool
1121 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1122 {
1123         if (__oldVersionInstance)
1124         {
1125                 return true;    // NOP for old version instance.
1126         }
1127
1128         if (!pDBusConnection)
1129         {
1130                 SysLog(NID_APP, "DBus connection invalid");
1131                 return false;
1132         }
1133
1134         bool result = true;
1135         const String strRightOfId(L"|N/A");
1136         const String strLeftOfValueIntType(L"INT|");
1137         const String strLeftOfValueStrType(L"STRING|");
1138
1139         String strId = id + strRightOfId;
1140         String strValue;
1141         if (intType)
1142         {
1143                 strValue = strLeftOfValueIntType;
1144         }
1145         else
1146         {
1147                 strValue = strLeftOfValueStrType;
1148         }
1149         strValue += value;
1150         String signalString(DBUS_SIGNAL_PREFIX);
1151         signalString += __correspondAppId;
1152
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)
1157         {
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());
1162
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)
1169                 {
1170                         SysLog(NID_APP, "DBus connection invalid");
1171                         result = false;
1172                 }
1173
1174                 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1175                 {
1176                         SysLog(NID_APP, "Fail to send message");
1177                         result = false;
1178                 }
1179
1180                 dbus_connection_flush(pDBusConnection);
1181                 dbus_message_unref(pMessageWihtAppId);
1182         }
1183         else
1184         {
1185                 SysLog(NID_APP, "System error: memory full ?");
1186         }
1187
1188         return result;
1189 }
1190
1191 AppSetting*
1192 _AppSettingImpl::CreateAppSetting(void)
1193 {
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));
1197
1198         return pAppSetting;
1199 }
1200
1201 void
1202 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1203 {
1204         appSetting.__pAppSettingImpl = &impl;
1205 }
1206
1207 } } // Tizen::App