Merge "Fix prevent issues" into tizen_2.1
[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,
473                                            "Type mismatch between requested type bool and object type of %ls.", id.GetPointer());
474         value = pBoolean->ToBool();
475
476         return E_SUCCESS;
477 }
478
479 result
480 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
481 {
482         const Object* pObject = __settingContainer.GetValue(id);
483         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
484         const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
485         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
486         const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
487         SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH,
488                                            "Type mismatch between requested type int and object type of %ls.", id.GetPointer());
489         value = pInteger->ToInt();
490
491         return E_SUCCESS;
492 }
493
494 result
495 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
496 {
497         const Object* pObject = __settingContainer.GetValue(id);
498         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
499         const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
500         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
501         const String* pString = dynamic_cast<const String*>(pItem->GetValue());
502         SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH,
503                                            "Type mismatch between requested type String and object type of %ls.", id.GetPointer());
504         value = *pString;
505
506         return E_SUCCESS;
507 }
508
509 result
510 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
511 {
512         Object* pObject = __settingContainer.GetValue(id);
513         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
514         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
515         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
516         Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
517         SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH,
518                                            "Type mismatch between requested type bool and object type of %ls.", id.GetPointer());
519         *pBoolean = value;
520
521         static const String strTrue(L"true");
522         static const String strFalse(L"false");
523         const String& strValue = value ? strTrue : strFalse;
524         UpdateProperty(pItem->GetXmlNode(), strValue);
525         if (save)
526         {
527                 Save();
528                 static const String strIntTrue(L"1");
529                 static const String strIntFalse(L"0");
530                 SendMessage(id, value ? strIntTrue : strIntFalse, true);
531         }
532         if (__pEventListener)
533         {
534                 __pEventListener->OnAppSettingChanged(id);
535         }
536
537         return E_SUCCESS;
538 }
539
540 result
541 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
542 {
543         Object* pObject = __settingContainer.GetValue(id);
544         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
545         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
546         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
547         Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
548         SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH,
549                                            "Type mismatch between requested type int and object type of %ls.", id.GetPointer());
550
551         SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
552                                            value, pItem->GetMin());
553         SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
554                                            value, pItem->GetMax());
555         *pInteger = value;
556
557         String strValue = pInteger->ToString();
558         UpdateProperty(pItem->GetXmlNode(), strValue);
559         if (save)
560         {
561                 Save();
562                 SendMessage(id, strValue, true);
563         }
564         if (__pEventListener)
565         {
566                 __pEventListener->OnAppSettingChanged(id);
567         }
568
569         return E_SUCCESS;
570 }
571
572 result
573 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
574 {
575         Object* pObject = __settingContainer.GetValue(id);
576         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
577         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
578         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
579         String* pString = dynamic_cast<String*>(pItem->GetValue());
580         SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH,
581                                            "Type mismatch between requested type String and object type of %ls.", id.GetPointer());
582
583         SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
584                                            "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
585         SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
586                                            "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
587         *pString = value;
588
589         UpdateProperty(pItem->GetXmlNode(), *pString);
590         if (save)
591         {
592                 Save();
593                 SendMessage(id, *pString, false);
594         }
595         if (__pEventListener)
596         {
597                 __pEventListener->OnAppSettingChanged(id);
598         }
599
600         return E_SUCCESS;
601 }
602
603 result
604 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
605 {
606         __pEventListener = pListener;
607         return E_SUCCESS;
608 }
609
610 _AppSettingImpl*
611 _AppSettingImpl::GetInstance(AppSetting& appSetting)
612 {
613         return appSetting.__pAppSettingImpl;
614 }
615
616 const _AppSettingImpl*
617 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
618 {
619         return appSetting.__pAppSettingImpl;
620 }
621
622 result
623 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
624 {
625         result r = E_SUCCESS;
626         if (version.GetLength() > 0)
627         {
628                 __oldVersionInstance = true;
629         }
630         correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId);  // Use 10 char id value.
631
632         __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
633         __filePath += RESOUCE_FILE_NAME;
634         if (version.GetLength())
635         {
636                 __filePath += version;
637         }
638         __filePath += RESOUCE_FILE_EXT;
639
640         r = __settingContainer.Construct();
641         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
642         if (!__oldVersionInstance)
643         {
644                 SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
645         }
646         r = Load();
647         SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
648         return r;
649 }
650
651 result
652 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
653 {
654         result r = E_SUCCESS;
655         SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
656                                  "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
657
658         correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId);  // Use 10 char id value.
659         __filePath = appRootPath + RESOUCE_FILE_PATH;
660         __filePath += RESOUCE_FILE_NAME;
661         __filePath += RESOUCE_FILE_EXT;
662
663         r = __settingContainer.Construct();
664         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
665         SysTryReturnResult(NID_APP, InitizlizeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
666         r = Load();
667         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
668
669         return r;
670 }
671
672 result
673 _AppSettingImpl::Load(void)
674 {
675         SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
676                                            "The instance of specified AppId does not have setting information.");
677
678         result r = E_SUCCESS;
679         std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
680         SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
681
682         __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
683         SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
684                                            "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
685
686         xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
687         SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
688
689         VisitNode(rootNodePtr);
690
691         return r;
692 }
693
694 result
695 _AppSettingImpl::Save(void)
696 {
697         static const int XML_SAVE_FAILED = -1;
698         static const int XML_SAVE_FORMAT = 1;
699
700         SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
701                                            "A system error has been occurred. XML Document not valid so can not save it.");
702         std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
703         SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
704
705         int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
706         SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
707
708         return E_SUCCESS;
709 }
710
711 void
712 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
713 {
714         for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next)       // Visit sibling node
715         {
716                 // Check node name and add to collection.
717                 if (nodePtr->type == XML_ELEMENT_NODE)
718                 {
719                         CheckNodeNameAndAddToCollection(nodePtr);
720                 }
721                 // Visit child node
722                 VisitNode(nodePtr->children);
723         }
724 }
725
726 void
727 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
728 {
729         static const char* pPropId = "id";
730         static const char* pPropValue = "value";
731
732         // 1. Get type (bool, integer, string, expandlist)
733         _ElementType detectedType = GetElementType(pNode);
734
735         // Make item and add to collection.
736         if (detectedType != ELEMENT_INVALID)
737         {
738                 // 2. Get "id"
739                 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
740                 if (pId)
741                 {
742                         int min = 0;
743                         int max = 255;
744
745                         std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
746                         SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
747                                                                    GetErrorMessage(E_OUT_OF_MEMORY));
748
749                         xmlFree(pId);
750
751                         if (pStrId)
752                         {       // 3. Get min(length), max(length) value
753                                 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
754                                 {
755                                         GetMinMaxValue(pNode, detectedType, min, max);
756                                 }
757                                 // 4. Get "value"
758                                 String strValue;
759                                 bool validValue = false;
760                                 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
761                                 if (pValue)
762                                 {
763                                         strValue = String(reinterpret_cast<char*>(pValue));
764                                         xmlFree(pValue);
765                                         pValue = null;
766                                         validValue = true;
767                                 }
768                                 // 5. Create value object from value
769                                 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
770                                 if (pValueObject)
771                                 {
772                                         _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
773                                         SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
774                                                                                    GetErrorMessage(E_OUT_OF_MEMORY));
775                                         if (pSettingItem)
776                                         {       // 6. Add item to container
777                                                 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
778                                                                         pStrId->GetPointer(), min, max, strValue.GetPointer());
779                                                 __settingContainer.Add(*pStrId.release(), *pSettingItem);
780                                         }
781                                         else
782                                         {
783                                                 SysLog(NID_APP, "Failed create new _SettingItem");
784                                         }
785                                 }
786                         } // pStrId
787                 } // pId
788         } // ELEMET_INVALID
789 }
790
791 _ElementType
792 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
793 {
794         const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
795         const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
796         const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
797         const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
798
799         _ElementType type = ELEMENT_INVALID;
800         if (xmlStrcmp(pNode->name, pElementBool) == 0)
801         {
802                 type = ELEMENT_BOOL;
803         }
804         else
805         if (xmlStrcmp(pNode->name, pElementInteger) == 0)
806         {
807                 type = ELEMENT_INTEGER;
808         }
809         else
810         if (xmlStrcmp(pNode->name, pElementString) == 0)
811         {
812                 type = ELEMENT_STRING;
813         }
814         else
815         if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
816         {
817                 type = ELEMENT_EXPANDLIST;
818         }
819
820         return type;
821 }
822
823 void
824 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
825 {
826         const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
827         const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
828         const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
829         const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
830
831         if (type == ELEMENT_INTEGER)
832         {
833                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
834                 if (pStrMin)
835                 {
836                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
837                         xmlFree(pStrMin);
838                 }
839                 else
840                 {
841                         SysLog(NID_APP, "Failed to get 'min' value.");
842                 }
843
844                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
845                 if (pStrMax)
846                 {
847                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
848                         xmlFree(pStrMax);
849                 }
850                 else
851                 {
852                         SysLog(NID_APP, "Failed to get 'max' value.");
853                 }
854         }
855         else
856         if (type == ELEMENT_STRING)
857         {
858                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
859                 if (pStrMin)
860                 {
861                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
862                         xmlFree(pStrMin);
863                 }
864                 else
865                 {
866                         SysLog(NID_APP, "Failed to get 'minlength' value.");
867                 }
868
869                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
870                 if (pStrMax)
871                 {
872                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
873                         xmlFree(pStrMax);
874                 }
875                 else
876                 {
877                         SysLog(NID_APP, "Failed to get 'maxlength' value.");
878                 }
879         }
880         else
881         {
882                 SysLog(NID_APP, "Invalid element type.");
883         }
884 }
885
886 Object*
887 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
888 {
889         const String strTrue(L"true");
890         Object* pValueObject = null;
891
892         switch (type)
893         {
894         case ELEMENT_BOOL:
895                 {
896                         bool valueBool = false;
897                         if (validValue)
898                         {
899                                 if (strValue == strTrue)
900                                 {
901                                         valueBool = true;
902                                 }
903                         }
904                         pValueObject = new (std::nothrow) Boolean(valueBool);
905                         SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
906                                                  GetErrorMessage(E_OUT_OF_MEMORY));
907                 }
908                 break;
909
910         case ELEMENT_INTEGER:
911                 {
912                         int valueInteger = 0;
913                         if (validValue)
914                         {
915                                 Integer::Parse(strValue, valueInteger);
916                         }
917                         pValueObject = new (std::nothrow) Integer(valueInteger);
918                         SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
919                                                  GetErrorMessage(E_OUT_OF_MEMORY));
920                 }
921                 break;
922
923         case ELEMENT_STRING:
924         case ELEMENT_EXPANDLIST:
925                 {
926                         String* pStringObject = new (std::nothrow) String;
927                         SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
928                                                  GetErrorMessage(E_OUT_OF_MEMORY));
929                         if (pStringObject && validValue)
930                         {
931                                 *pStringObject = strValue;
932                         }
933                         pValueObject = pStringObject;
934                 }
935                 break;
936
937         case ELEMENT_INVALID:
938                 // It already validate by caller;
939                 SysLog(NID_APP, "Invalid element type.");
940                 break;
941         }
942
943         return pValueObject;
944 }
945
946 bool
947 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
948 {
949         const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
950
951         if (value.IsEmpty())
952         {
953                 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
954                 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
955                 if (attrPtr)
956                 {
957                         return true;
958                 }
959         }
960         else
961         {
962                 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
963                 if (pBuf)
964                 {
965                         xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
966                         if (attrPtr)
967                         {
968                                 return true;
969                         }
970                 }
971                 else
972                 {
973                         SysLog(NID_APP, "StringToUtf8N is failed.");
974                 }
975         }
976         return false;
977 }
978
979 bool
980 _AppSettingImpl::InitizlizeDBus(void)
981 {
982         DBusError error;
983         dbus_error_init(&error);
984
985         if (pDBusConnection == null)
986         {
987                 char rule[MAX_LOCAL_BUFSIZE];
988                 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
989                 if (pDBusConnection == null)
990                 {
991                         SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
992                         dbus_error_free(&error);
993                         return false;
994                 }
995                 dbus_connection_setup_with_g_main(pDBusConnection, null);
996
997                 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
998                 dbus_bus_add_match(pDBusConnection, rule, &error);
999                 if (dbus_error_is_set(&error))
1000                 {
1001                         SysLog(NID_APP, "Fail to add match : %s", error.message);
1002                         dbus_error_free(&error);
1003                         return false;
1004                 }
1005         }
1006
1007         // this value is vary by AppSetting instance for proper handling.
1008         // Should be explicit remove filter on ReleaseInstanceByAppId.
1009         if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
1010         {
1011                 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1012                 dbus_error_free(&error);
1013                 return false;
1014         }
1015         return true;
1016 }
1017
1018 DBusHandlerResult
1019 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1020 {
1021         int my_pid = getpid();
1022         int sender_pid = 0;
1023         char* pId = NULL;
1024         char* pValue = NULL;
1025
1026         DBusError error;
1027         dbus_error_init(&error);
1028
1029         _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1030         SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1031                                         "Not valid pAppSettingImpl from user_data");
1032         String signalString(DBUS_SIGNAL_PREFIX);
1033         signalString += pAppSettingImpl->__correspondAppId;
1034         std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1035         SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1036         const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1037
1038         if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1039         {
1040                 if (dbus_message_get_args(message, &error,
1041                                                                   DBUS_TYPE_UINT32, &sender_pid,
1042                                                                   DBUS_TYPE_STRING, &pId,
1043                                                                   DBUS_TYPE_STRING, &pValue,
1044                                                                   DBUS_TYPE_INVALID) == FALSE)
1045                 {
1046                         SysLog(NID_APP, "Fail to get data : %s", error.message);
1047                         dbus_error_free(&error);
1048                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1049                 }
1050
1051                 if (sender_pid != 0 && my_pid != sender_pid)
1052                 {
1053                         pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1054                 }
1055                 return DBUS_HANDLER_RESULT_HANDLED;
1056         }
1057         else
1058         {
1059                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1060         }
1061 }
1062
1063 void
1064 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1065 {
1066         const String delimiter(L"|");
1067         String strId;
1068         String strValue;
1069         int indexOfDelimiter = 0;
1070
1071         // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1072         // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1073         String strOrg(pId);
1074         strOrg.Trim();
1075         result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1076         SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1077         strOrg.SubString(0, indexOfDelimiter, strId);   // Get left string of '|'
1078
1079         strOrg = pValue;
1080         strOrg.Trim();
1081         r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1082         SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1083         strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1084
1085         const Object* pObject = __settingContainer.GetValue(strId);
1086         if (pObject)
1087         {
1088                 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1089                 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1090                                                            GetErrorMessage(E_SYSTEM));
1091                 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1092                 if (pBoolean)
1093                 {
1094                         static const String strFalse(L"0");
1095                         bool valueBool = true;
1096
1097                         if (strValue == strFalse)
1098                         {
1099                                 valueBool = false;
1100                         }
1101                         SetValue(strId, valueBool, false);
1102                         return;
1103                 }
1104                 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1105                 if (pInteger)
1106                 {
1107                         int valueInteger = 0;
1108                         Integer::Parse(strValue, valueInteger);
1109                         SetValue(strId, valueInteger, false);
1110                         return;
1111                 }
1112                 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1113                 if (pString)
1114                 {
1115                         SetValue(strId, strValue, false);
1116                         return;
1117                 }
1118                 SysLog(NID_APP, "Failed determin value type for id=%s", pId);
1119         }
1120         else
1121         {
1122                 SysLog(NID_APP, "Failed find value for id=%s", pId);
1123         }
1124 }
1125
1126 bool
1127 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1128 {
1129         if (__oldVersionInstance)
1130         {
1131                 return true;    // NOP for old version instance.
1132         }
1133
1134         if (!pDBusConnection)
1135         {
1136                 SysLog(NID_APP, "DBus connection invalid");
1137                 return false;
1138         }
1139
1140         bool result = true;
1141         const String strRightOfId(L"|N/A");
1142         const String strLeftOfValueIntType(L"INT|");
1143         const String strLeftOfValueStrType(L"STRING|");
1144
1145         String strId = id + strRightOfId;
1146         String strValue;
1147         if (intType)
1148         {
1149                 strValue = strLeftOfValueIntType;
1150         }
1151         else
1152         {
1153                 strValue = strLeftOfValueStrType;
1154         }
1155         strValue += value;
1156         String signalString(DBUS_SIGNAL_PREFIX);
1157         signalString += __correspondAppId;
1158
1159         std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1160         std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1161         std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1162         if (pBufferId && pBufferValue && pBufferSignal)
1163         {
1164                 int sender_pid = getpid();
1165                 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1166                 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1167                 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1168
1169                 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1170                 if (dbus_message_append_args(pMessageWihtAppId,
1171                                                                          DBUS_TYPE_UINT32, &sender_pid,
1172                                                                          DBUS_TYPE_STRING, &pCharBufferId,
1173                                                                          DBUS_TYPE_STRING, &pCharBufferValue,
1174                                                                          DBUS_TYPE_INVALID) == FALSE)
1175                 {
1176                         SysLog(NID_APP, "DBus connection invalid");
1177                         result = false;
1178                 }
1179
1180                 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1181                 {
1182                         SysLog(NID_APP, "Fail to send message");
1183                         result = false;
1184                 }
1185
1186                 dbus_connection_flush(pDBusConnection);
1187                 dbus_message_unref(pMessageWihtAppId);
1188         }
1189         else
1190         {
1191                 SysLog(NID_APP, "System error: memory full ?");
1192         }
1193
1194         return result;
1195 }
1196
1197 AppSetting*
1198 _AppSettingImpl::CreateAppSetting(void)
1199 {
1200         AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1201         SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1202                                  GetErrorMessage(E_OUT_OF_MEMORY));
1203
1204         return pAppSetting;
1205 }
1206
1207 void
1208 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1209 {
1210         appSetting.__pAppSettingImpl = &impl;
1211 }
1212
1213 } } // Tizen::App