Resolve the "Klocwork" detected defect and add debug feature.
[platform/framework/native/appfw.git] / src / app / FApp_AppSettingImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 /**
18  * @file        FApp_AppResourceImpl.cpp
19  * @brief       This is the implementation file of the _AppResourceImpl class.
20  */
21
22 #include <memory>
23 #include <unistd.h>
24 #include <libxml/tree.h>
25 #include <unique_ptr.h>
26 #include <FAppApp.h>
27 #include <FBaseString.h>
28 #include <FBaseBoolean.h>
29 #include <FBaseInteger.h>
30 #include <FBaseUtilStringUtil.h>
31 #include <FBaseSysLog.h>
32 #include <FIoFile.h>
33 #include <FIoDirectory.h>
34 #include <FAppPkgPackageInfo.h>
35 #include <FAppPkgPackageManager.h>
36 #include <FAppIAppSettingEventListener.h>
37 #include <FSec_AccessController.h>
38 #include "FApp_AppSettingImpl.h"
39 #include "FApp_AppInfo.h"
40 #include "FAppPkg_PackageManagerImpl.h"
41 #include "FAppPkg_PackageInfoImpl.h"
42
43
44 using namespace Tizen::App::Package;
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Io;
48 using namespace Tizen::Security;
49 using namespace Tizen::App;
50
51
52 namespace Tizen { namespace App
53 {
54
55 const int APP_ID_LENTH = 10;
56 const int MAX_LOCAL_BUFSIZE = 128;
57 const char* DBUS_PATH = "/setting/dbus_handler";
58 const char* DBUS_SIGNAL_INTERFACE = "org.tizen.setting.signal";
59 static const wchar_t DBUS_SIGNAL_PREFIX[] = L"Update_";
60
61 static const wchar_t RESOUCE_FILE_PATH[] = L"setting/";
62 static const wchar_t RESOUCE_FILE_NAME[] = L"setting";
63 static const wchar_t RESOUCE_FILE_EXT[] = L".xml";
64
65 static _AppSettingImpl* pAppSettingImplInstance = null;
66
67 _AppSettingImpl::_MutiInstanceManager _AppSettingImpl::__appIdMultiInstanceMgr; // static member
68 DBusConnection* pDBusConnection = null;
69
70 class _CleanUpDBus
71 {
72 public:
73         ~_CleanUpDBus()
74         {
75                 if (pDBusConnection)
76                 {
77                         dbus_connection_close(pDBusConnection);
78                         pDBusConnection = null;
79                 }
80         }
81 };
82 static _CleanUpDBus cleanUpDBus;
83
84 _AppSettingImpl::_MutiInstanceManager::_MutiInstanceManager(void)
85 {
86         __stringToInstanceMap.Construct();
87 }
88
89 _AppSettingImpl::_MutiInstanceManager::~_MutiInstanceManager(void)
90 {
91         __stringToInstanceMap.RemoveAll(true);          // Remove instance on exit.
92 }
93
94 AppSetting*
95 _AppSettingImpl::_MutiInstanceManager::GetInstance(const String& version)
96 {
97         result r = E_SUCCESS;
98         AppSetting* pAppSettingInstance = null;
99         String* pKeyStr = null;
100
101         AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(version));
102         if (pAppSetting)
103         {
104                 return pAppSetting;
105         }
106
107         // Common creation part
108         std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
109         SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
110                                  GetErrorMessage(E_OUT_OF_MEMORY));
111
112         // Get current application context AppId
113         App* pApp = App::GetInstance();
114         String appId;
115         SysTryCatch(NID_APP, pApp != null, , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
116                                 GetErrorMessage(E_SYSTEM));
117         appId = pApp->GetAppId();
118
119         r = pAppSettingImpl->Construct(appId, version);
120         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
121
122         pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
123         SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
124                                 GetErrorMessage(E_OUT_OF_MEMORY));
125         _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
126
127         pKeyStr = new (std::nothrow) String(version);
128         SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
129
130         r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
131         SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
132         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
133
134         return pAppSettingInstance;
135
136 CATCH:
137         delete pAppSettingInstance;
138         return null;
139 }
140
141 AppSetting*
142 _AppSettingImpl::_MutiInstanceManager::GetInstanceByAppId(const AppId& appId)
143 {
144         result r = E_SUCCESS;
145         AppSetting* pAppSettingInstance = null;
146         String* pKeyStr = null;
147
148         r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
149         SysTryReturn(NID_APP, !IsFailed(r), null, E_PRIVILEGE_DENIED,
150                                 "[%s] The application does not have the privilege to call this method.", GetErrorMessage(E_PRIVILEGE_DENIED));
151
152         // Convert AppId to package type id.
153         String packageId;
154         appId.SubString(0, APP_ID_LENTH, packageId);
155
156         AppSetting* pAppSetting = static_cast<AppSetting*>( __stringToInstanceMap.GetValue(packageId));
157         if (pAppSetting)
158         {
159                 return pAppSetting;
160         }
161
162         _PackageManagerImpl* pPkgMgrImpl = _PackageManagerImpl::GetInstance();
163         SysTryReturn(NID_APP, pPkgMgrImpl != null, null, E_SYSTEM,
164                                  "[%s] A system error has been occurred. failed to get _PackageMaangerImpl.", GetErrorMessage(E_SYSTEM));
165
166         std::unique_ptr<PackageInfo> pPackageInfo(pPkgMgrImpl->GetPackageInfoN(packageId));     // !E_SUCCESS for DB fail and query fail
167         SysTryReturn(NID_APP, pPackageInfo != null, null, E_APP_NOT_INSTALLED, "[%s] Propagating.", GetErrorMessage(E_APP_NOT_INSTALLED));
168
169         const _PackageInfoImpl* pPkgInfoImpl = _PackageInfoImpl::GetInstance(pPackageInfo.get());
170         SysTryReturn(NID_APP, pPkgInfoImpl != null, null, E_SYSTEM,
171                                  "[%s] A system error has been occurred. failed to get _PackageInfoImpl.", GetErrorMessage(E_SYSTEM));
172
173         String rootPath = pPkgInfoImpl->GetAppRootPath();
174         rootPath += L"/";
175
176         // Common creation part
177         std::unique_ptr<_AppSettingImpl> pAppSettingImpl(new (std::nothrow) _AppSettingImpl());
178         SysTryReturn(NID_APP, pAppSettingImpl != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
179                                  GetErrorMessage(E_OUT_OF_MEMORY));
180
181         r = pAppSettingImpl->ConstructByAppPath(packageId, rootPath);
182         SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
183
184         pAppSettingInstance = _AppSettingImpl::CreateAppSetting();
185         SysTryCatch(NID_APP, pAppSettingInstance != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
186                                 GetErrorMessage(E_OUT_OF_MEMORY));
187         _AppSettingImpl::SetImplInstance(*pAppSettingInstance, *pAppSettingImpl.release());
188
189         pKeyStr = new (std::nothrow) String(packageId);
190         SysTryCatch(NID_APP, pKeyStr != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
191
192         r = __stringToInstanceMap.Add(*pKeyStr, *pAppSettingInstance);
193         SysTryCatch(NID_APP, r != E_OUT_OF_MEMORY, , r, "[%s] Memory allocation failed.", GetErrorMessage(r));
194         SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
195
196         return pAppSettingInstance;
197
198 CATCH:
199         delete pAppSettingInstance;
200         return null;
201 }
202
203 result
204 _AppSettingImpl::_MutiInstanceManager::ReleaseInstanceByAppId(const AppId& appId)
205 {
206         result r = _AccessController::CheckUserPrivilege(_PRV_APPSETTING);
207         SysTryReturnResult(NID_APP, !IsFailed(r), E_PRIVILEGE_DENIED,
208                                            "The application does not have the privilege to call this method.");
209
210         // Convert AppId to package type id.
211         String packageId;
212         appId.SubString(0, APP_ID_LENTH, packageId);
213
214         AppSetting* pAppSetting = static_cast<AppSetting*>(__stringToInstanceMap.GetValue(packageId));
215         if (pAppSetting)
216         {
217                 if (pDBusConnection)
218                 {
219                         _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
220                         dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
221                 }
222                 __stringToInstanceMap.Remove(packageId, true);  // Delete item instance and remove item from __stringToInstanceMap
223                 return E_SUCCESS;
224         }
225         else
226         {
227                 return E_OBJ_NOT_FOUND;
228         }
229 }
230
231 result
232 _AppSettingImpl::_MutiInstanceManager::ReleaseOtherAppInstances(void)
233 {
234         if (pDBusConnection)
235         {       // Iterate all element and remove correspond dbus filter
236                 std::unique_ptr<IMapEnumerator> pEnum(__stringToInstanceMap.GetMapEnumeratorN());
237                 if (pEnum.get())
238                 {
239                         while (pEnum->MoveNext() == E_SUCCESS)
240                         {
241                                 AppSetting* pAppSetting = null;
242                                 pAppSetting = static_cast<AppSetting*>(pEnum->GetValue());
243                                 _AppSettingImpl* pThisAppSettingImpl = _AppSettingImpl::GetInstance(*pAppSetting);
244                                 dbus_connection_remove_filter(pDBusConnection, HandleDBusMessage, pThisAppSettingImpl);
245                         }
246                 }
247         }
248         __stringToInstanceMap.RemoveAll(true);
249         return E_SUCCESS;
250 }
251
252 class _SettingItem
253         : public Object
254 {
255 public:
256         _SettingItem(Object* pItemValue, int min, int max, xmlNodePtr pItemXmlNode)
257                 : __pValue(pItemValue)
258                 , __min(min)
259                 , __max(max)
260                 , __pXmlNode(pItemXmlNode)
261         {}
262         ~_SettingItem(void)
263         {
264                 delete __pValue;
265         }
266         Object* GetValue(void)
267         {
268                 return __pValue;
269         }
270         const Object* GetValue(void) const
271         {
272                 return __pValue;
273         }
274         int GetMin(void)
275         {
276                 return __min;
277         }
278         int GetMax(void)
279         {
280                 return __max;
281         }
282         xmlNodePtr GetXmlNode(void)
283         {
284                 return __pXmlNode;
285         }
286 private:
287         _SettingItem(void);
288         _SettingItem(const _SettingItem& rhs);
289         _SettingItem& operator =(const _SettingItem& rhs);
290 private:
291         Object* __pValue;
292         int __min;
293         int __max;
294         xmlNodePtr __pXmlNode;
295 };
296
297 class _ReverseStringComparer
298         : public Tizen::Base::Collection::IComparer
299 {
300 public:
301         _ReverseStringComparer(void) {};
302         virtual ~_ReverseStringComparer(void) {};
303         virtual result Compare(const Tizen::Base::Object& obj1, const Tizen::Base::Object& obj2, int& cmp) const
304         {
305                 const String& objString1 = static_cast<const String&>(obj1);
306                 const String& objString2 = static_cast<const String&>(obj2);
307                 cmp = objString2.CompareTo(objString1); // reverse
308                 return E_SUCCESS;
309         }
310 };
311
312 _AppSettingImpl::_AppSettingImpl(void)
313         : __oldVersionInstance(false)
314         , __pEventListener(null)
315         , __pDoc(null)
316 {
317
318 }
319
320 _AppSettingImpl::~_AppSettingImpl(void)
321 {
322         if (__pDoc)
323         {
324                 xmlFreeDoc(__pDoc);
325                 __pDoc = null;
326         }
327         __settingContainer.RemoveAll(true);
328 }
329
330 _AppSettingImpl*
331 _AppSettingImpl::GetInstance(void)
332 {
333         ClearLastResult();
334         result r = E_SUCCESS;
335
336         if (pAppSettingImplInstance == null)
337         {
338                 pAppSettingImplInstance = new (std::nothrow) _AppSettingImpl();
339                 SysTryReturn(NID_APP, pAppSettingImplInstance != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
340                                          GetErrorMessage(E_OUT_OF_MEMORY));
341
342                 // Get current application context AppId
343                 App* pApp = App::GetInstance();
344                 SysTryCatch(NID_APP, !IsFailed(r), , E_SYSTEM, "[%s] A system error has been occurred. App::GetInstance() failed",
345                                         GetErrorMessage(E_SYSTEM));
346                 String appId;
347                 appId = pApp->GetAppId();
348                 r = pAppSettingImplInstance->Construct(appId);
349                 SysTryCatch(NID_APP, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
350         }
351         return pAppSettingImplInstance;
352
353 CATCH:
354         delete pAppSettingImplInstance;
355         pAppSettingImplInstance = null;
356         return null;
357 }
358
359 AppSetting*
360 _AppSettingImpl::GetInstance(const Tizen::Base::String& version)
361 {
362         ClearLastResult();
363         //result r = E_SUCCESS;
364
365         static _MutiInstanceManager multiInstanceManager;
366         AppSetting* pVersionAppSetting = multiInstanceManager.GetInstance(version);
367         SysTryReturn(NID_APP, pVersionAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
368                                  GetErrorMessage(E_OUT_OF_MEMORY));
369
370         return pVersionAppSetting;
371 }
372
373 AppSetting*
374 _AppSettingImpl::GetInstanceByAppId(const AppId& appId)
375 {
376         ClearLastResult();
377
378         AppSetting* pAppSettingByAppId = __appIdMultiInstanceMgr.GetInstanceByAppId(appId);
379         return pAppSettingByAppId;
380 }
381
382 result
383 _AppSettingImpl::ReleaseInstanceByAppId(const AppId& appId)
384 {
385         return __appIdMultiInstanceMgr.ReleaseInstanceByAppId(appId);
386 }
387
388 result
389 _AppSettingImpl::ReleaseOtherAppInstances(void)
390 {
391         return __appIdMultiInstanceMgr.ReleaseOtherAppInstances();
392 }
393
394 Tizen::Base::Collection::IList*
395 _AppSettingImpl::GetAppSettingVersionListN(void)
396 {
397         result r = E_SUCCESS;
398         ArrayList* pVersionList = new (std::nothrow) ArrayList;
399         SysTryReturn(NID_APP, pVersionList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
400                                  GetErrorMessage(E_OUT_OF_MEMORY));
401         r = pVersionList->Construct();
402         SysTryReturn(NID_APP, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
403                                  GetErrorMessage(E_OUT_OF_MEMORY));
404
405         // Iterate file and add to list
406         String dirPath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
407         Directory dir;
408         r = dir.Construct(dirPath);
409         SysTryReturn(NID_APP, !IsFailed(r), null, E_SYSTEM, "[%s] A system error has been occurred. Directory construction failed.",
410                                  GetErrorMessage(E_SYSTEM));
411
412         std::unique_ptr<DirEnumerator> pDirEnum(dir.ReadN());
413         SysTryReturn(NID_APP, pDirEnum != null, null, E_SYSTEM,
414                                  "[%s] A system error has been occurred. Directory enumerator getting failed.", GetErrorMessage(E_SYSTEM));
415
416         String settingName(RESOUCE_FILE_NAME);
417         String settingExt(RESOUCE_FILE_EXT);
418         while (pDirEnum->MoveNext() == E_SUCCESS)
419         {
420                 DirEntry dirEntry = pDirEnum->GetCurrentDirEntry();
421                 if (!dirEntry.IsDirectory())
422                 {
423                         String fullName = dirEntry.GetName();
424                         // Check name and extension to getting valid one
425                         String name;
426                         String ext;
427                         const int extLength = settingExt.GetLength();
428                         const int posStart = settingName.GetLength();
429                         const int posEnd = fullName.GetLength() - extLength;
430                         if (posStart >= posEnd)
431                         {
432                                 continue;
433                         }
434                         fullName.SubString(0, posStart, name);
435                         fullName.SubString(posEnd, ext);
436                         if ((name == settingName) && (ext == settingExt))
437                         {
438                                 String version;
439                                 if (E_SUCCESS == fullName.SubString(posStart, posEnd-posStart, version))
440                                 {
441                                         String* pVersionStr = new (std::nothrow) String(version);
442                                         SysTryReturn(NID_APP, pVersionStr != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
443                                                                  GetErrorMessage(E_OUT_OF_MEMORY));
444                                         pVersionList->Add(*pVersionStr);
445                                 }
446                         }
447                 }
448         }
449
450         if (pVersionList->GetCount())
451         {
452                 static _ReverseStringComparer strComparator;
453                 pVersionList->Sort(strComparator);
454                 pVersionList->RemoveAt(0);      // remove latest version from list.
455         }
456
457         return pVersionList;
458 }
459
460 result
461 _AppSettingImpl::GetValue(const Tizen::Base::String& id, bool& value) const
462 {
463         const Object* pObject = __settingContainer.GetValue(id);
464         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
465         const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
466         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
467         const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
468         SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
469         value = pBoolean->ToBool();
470
471         return E_SUCCESS;
472 }
473
474 result
475 _AppSettingImpl::GetValue(const Tizen::Base::String& id, int& value) const
476 {
477         const Object* pObject = __settingContainer.GetValue(id);
478         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
479         const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
480         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
481         const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
482         SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
483         value = pInteger->ToInt();
484
485         return E_SUCCESS;
486 }
487
488 result
489 _AppSettingImpl::GetValue(const Tizen::Base::String& id, Tizen::Base::String& value) const
490 {
491         const Object* pObject = __settingContainer.GetValue(id);
492         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
493         const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
494         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
495         const String* pString = dynamic_cast<const String*>(pItem->GetValue());
496         SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
497         value = *pString;
498
499         return E_SUCCESS;
500 }
501
502 result
503 _AppSettingImpl::SetValue(const Tizen::Base::String& id, bool value, bool save)
504 {
505         Object* pObject = __settingContainer.GetValue(id);
506         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
507         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
508         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
509         Boolean* pBoolean = dynamic_cast<Boolean*>(pItem->GetValue());
510         SysTryReturnResult(NID_APP, pBoolean != null, E_TYPE_MISMATCH, "Type mismatch between requested type bool and object type.");
511         *pBoolean = value;
512
513         static const String strTrue(L"true");
514         static const String strFalse(L"false");
515         const String& strValue = value ? strTrue : strFalse;
516         UpdateProperty(pItem->GetXmlNode(), strValue);
517         if (save)
518         {
519                 Save();
520                 static const String strIntTrue(L"1");
521                 static const String strIntFalse(L"0");
522                 SendMessage(id, value ? strIntTrue : strIntFalse, true);
523         }
524         if (__pEventListener)
525         {
526                 __pEventListener->OnAppSettingChanged(id);
527         }
528
529         return E_SUCCESS;
530 }
531
532 result
533 _AppSettingImpl::SetValue(const Tizen::Base::String& id, int value, bool save)
534 {
535         Object* pObject = __settingContainer.GetValue(id);
536         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
537         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
538         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
539         Integer* pInteger = dynamic_cast<Integer*>(pItem->GetValue());
540         SysTryReturnResult(NID_APP, pInteger != null, E_TYPE_MISMATCH, "Type mismatch between requested type int and object type.");
541
542         SysTryReturnResult(NID_APP, (pItem->GetMin() <= value), E_OUT_OF_RANGE, "value(%d) is less than minimum range(%d).",
543                                            value, pItem->GetMin());
544         SysTryReturnResult(NID_APP, (pItem->GetMax() >= value), E_OUT_OF_RANGE, "value(%d) is greater than maximum range(%d).",
545                                            value, pItem->GetMax());
546         *pInteger = value;
547
548         String strValue = pInteger->ToString();
549         UpdateProperty(pItem->GetXmlNode(), strValue);
550         if (save)
551         {
552                 Save();
553                 SendMessage(id, strValue, true);
554         }
555         if (__pEventListener)
556         {
557                 __pEventListener->OnAppSettingChanged(id);
558         }
559
560         return E_SUCCESS;
561 }
562
563 result
564 _AppSettingImpl::SetValue(const Tizen::Base::String& id, const Tizen::Base::String& value, bool save)
565 {
566         Object* pObject = __settingContainer.GetValue(id);
567         SysTryReturnResult(NID_APP, pObject != null, E_OBJ_NOT_FOUND, "Specified id is not found in the application setting");
568         _SettingItem* pItem = dynamic_cast<_SettingItem*>(pObject);
569         SysTryReturnResult(NID_APP, pItem != null, E_TYPE_MISMATCH, "Type mismatch for instance of id.");
570         String* pString = dynamic_cast<String*>(pItem->GetValue());
571         SysTryReturnResult(NID_APP, pString != null, E_TYPE_MISMATCH, "Type mismatch between requested type String and object type.");
572
573         SysTryReturnResult(NID_APP, pItem->GetMin() <= value.GetLength(), E_OUT_OF_RANGE,
574                                            "value length(%d) less than minimum length(%d).", value.GetLength(), pItem->GetMin());
575         SysTryReturnResult(NID_APP, pItem->GetMax() >= value.GetLength(), E_OUT_OF_RANGE,
576                                            "value length(%d) greater than maximum range(%d).", value.GetLength(), pItem->GetMax());
577         *pString = value;
578
579         UpdateProperty(pItem->GetXmlNode(), *pString);
580         if (save)
581         {
582                 Save();
583                 SendMessage(id, *pString, false);
584         }
585         if (__pEventListener)
586         {
587                 __pEventListener->OnAppSettingChanged(id);
588         }
589
590         return E_SUCCESS;
591 }
592
593 result
594 _AppSettingImpl::SetAppSettingEventListener(IAppSettingEventListener* pListener)
595 {
596         __pEventListener = pListener;
597         return E_SUCCESS;
598 }
599
600 _AppSettingImpl*
601 _AppSettingImpl::GetInstance(AppSetting& appSetting)
602 {
603         return appSetting.__pAppSettingImpl;
604 }
605
606 const _AppSettingImpl*
607 _AppSettingImpl::GetInstance(const AppSetting& appSetting)
608 {
609         return appSetting.__pAppSettingImpl;
610 }
611
612 result
613 _AppSettingImpl::Construct(const AppId& correspondAppId, const String& version)
614 {
615         result r = E_SUCCESS;
616         if (version.GetLength() > 0)
617         {
618                 __oldVersionInstance = true;
619         }
620         correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId);  // Use 10 char id value.
621
622         __filePath = _AppInfo::GetAppRootPath() + RESOUCE_FILE_PATH;
623         __filePath += RESOUCE_FILE_NAME;
624         if (version.GetLength())
625         {
626                 __filePath += version;
627         }
628         __filePath += RESOUCE_FILE_EXT;
629
630         r = __settingContainer.Construct();
631         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
632         if (!__oldVersionInstance)
633         {
634                 SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
635         }
636         r = Load();
637         SysTryReturnResult(NID_APP, !IsFailed(r), E_SYSTEM, "A system error has been occurred. Loading procedure failed.");
638         return r;
639 }
640
641 result
642 _AppSettingImpl::ConstructByAppPath(const AppId& correspondAppId, const Tizen::Base::String& appRootPath)
643 {
644         result r = E_SUCCESS;
645         SysTryReturn(NID_APP, !appRootPath.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG,
646                                  "[%s] Invalid argument is used. appRootPath length is 0.", GetErrorMessage(E_INVALID_ARG));
647
648         correspondAppId.SubString(0, APP_ID_LENTH, __correspondAppId);  // Use 10 char id value.
649         __filePath = appRootPath + RESOUCE_FILE_PATH;
650         __filePath += RESOUCE_FILE_NAME;
651         __filePath += RESOUCE_FILE_EXT;
652
653         r = __settingContainer.Construct();
654         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
655         SysTryReturnResult(NID_APP, InitializeDBus(), E_SYSTEM, "A system error has been occurred. DBus initialization failed.");
656         r = Load();
657         SysTryReturn(NID_APP, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
658
659         return r;
660 }
661
662 result
663 _AppSettingImpl::Load(void)
664 {
665         SysTryReturnResult(NID_APP, File::IsFileExist(__filePath), E_OBJ_NOT_FOUND,
666                                            "The instance of specified AppId does not have setting information. %ls", __filePath.GetPointer());
667
668         result r = E_SUCCESS;
669         std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
670         SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
671
672         __pDoc = xmlParseFile(reinterpret_cast<const char*>(pfilePath->GetPointer()));
673         SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
674                                            "A system error has been occurred. Can not parse xml file: %ls", __filePath.GetPointer());
675
676         xmlNodePtr rootNodePtr = xmlDocGetRootElement(__pDoc);
677         SysTryReturnResult(NID_APP, rootNodePtr != null, E_SYSTEM, "A system error has been occurred. Can not find root node");
678
679         VisitNode(rootNodePtr);
680
681         return r;
682 }
683
684 result
685 _AppSettingImpl::Save(void)
686 {
687         static const int XML_SAVE_FAILED = -1;
688         static const int XML_SAVE_FORMAT = 1;
689
690         SysTryReturnResult(NID_APP, __pDoc != null, E_SYSTEM,
691                                            "A system error has been occurred. XML Document not valid so can not save it.");
692         std::unique_ptr<ByteBuffer> pfilePath(Tizen::Base::Utility::StringUtil::StringToUtf8N(__filePath));
693         SysTryReturnResult(NID_APP, pfilePath != null, E_OUT_OF_MEMORY, "Memory allocation failed. File path conversion failed.");
694
695         int result = xmlSaveFormatFile(reinterpret_cast<const char*>(pfilePath->GetPointer()), __pDoc, XML_SAVE_FORMAT);
696         SysTryReturnResult(NID_APP, result != XML_SAVE_FAILED, E_SYSTEM, "A system error has been occurred. Document saving failed.");
697
698         return E_SUCCESS;
699 }
700
701 void
702 _AppSettingImpl::VisitNode(xmlNodePtr pCurrentNode)
703 {
704         for (xmlNodePtr nodePtr = pCurrentNode; nodePtr != null; nodePtr = nodePtr->next)       // Visit sibling node
705         {
706                 // Check node name and add to collection.
707                 if (nodePtr->type == XML_ELEMENT_NODE)
708                 {
709                         CheckNodeNameAndAddToCollection(nodePtr);
710                 }
711                 // Visit child node
712                 VisitNode(nodePtr->children);
713         }
714 }
715
716 void
717 _AppSettingImpl::CheckNodeNameAndAddToCollection(xmlNodePtr pNode)
718 {
719         static const char* pPropId = "id";
720         static const char* pPropValue = "value";
721
722         // 1. Get type (bool, integer, string, expandlist)
723         _ElementType detectedType = GetElementType(pNode);
724
725         // Make item and add to collection.
726         if (detectedType != ELEMENT_INVALID)
727         {
728                 // 2. Get "id"
729                 xmlChar* pId = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropId));
730                 if (pId)
731                 {
732                         int min = 0;
733                         int max = 255;
734
735                         std::unique_ptr<String> pStrId(new (std::nothrow) String(reinterpret_cast<char*>(pId)));
736                         SysTryReturnVoidResult(NID_APP, pStrId, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
737                                                                    GetErrorMessage(E_OUT_OF_MEMORY));
738
739                         xmlFree(pId);
740
741                         if (pStrId)
742                         {       // 3. Get min(length), max(length) value
743                                 if ((detectedType == ELEMENT_INTEGER) || (detectedType == ELEMENT_STRING))
744                                 {
745                                         GetMinMaxValue(pNode, detectedType, min, max);
746                                 }
747                                 // 4. Get "value"
748                                 String strValue;
749                                 bool validValue = false;
750                                 xmlChar* pValue = xmlGetProp(pNode, reinterpret_cast<const xmlChar*>(pPropValue));
751                                 if (pValue)
752                                 {
753                                         strValue = String(reinterpret_cast<char*>(pValue));
754                                         xmlFree(pValue);
755                                         pValue = null;
756                                         validValue = true;
757                                 }
758                                 // 5. Create value object from value
759                                 Object* pValueObject = GetValueObjectN(detectedType, strValue, validValue);
760                                 if (pValueObject)
761                                 {
762                                         _SettingItem* pSettingItem = new (std::nothrow) _SettingItem(pValueObject, min, max, pNode);
763                                         SysTryReturnVoidResult(NID_APP, pSettingItem, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
764                                                                                    GetErrorMessage(E_OUT_OF_MEMORY));
765                                         if (pSettingItem)
766                                         {       // 6. Add item to container
767                                                 SysLog(NID_APP, "AppSetting item adding: ID=%ls min=%d max=%d Value=%ls",
768                                                                         pStrId->GetPointer(), min, max, strValue.GetPointer());
769                                                 __settingContainer.Add(*pStrId.release(), *pSettingItem);
770                                         }
771                                         else
772                                         {
773                                                 SysLog(NID_APP, "Failed create new _SettingItem");
774                                         }
775                                 }
776                         } // pStrId
777                 } // pId
778         } // ELEMET_INVALID
779 }
780
781 _ElementType
782 _AppSettingImpl::GetElementType(xmlNodePtr pNode)
783 {
784         const xmlChar* pElementBool = reinterpret_cast<const xmlChar*>("bool");
785         const xmlChar* pElementInteger = reinterpret_cast<const xmlChar*>("integer");
786         const xmlChar* pElementString = reinterpret_cast<const xmlChar*>("string");
787         const xmlChar* pElementExpandlist = reinterpret_cast<const xmlChar*>("expandlist");
788
789         _ElementType type = ELEMENT_INVALID;
790         if (xmlStrcmp(pNode->name, pElementBool) == 0)
791         {
792                 type = ELEMENT_BOOL;
793         }
794         else
795         if (xmlStrcmp(pNode->name, pElementInteger) == 0)
796         {
797                 type = ELEMENT_INTEGER;
798         }
799         else
800         if (xmlStrcmp(pNode->name, pElementString) == 0)
801         {
802                 type = ELEMENT_STRING;
803         }
804         else
805         if (xmlStrcmp(pNode->name, pElementExpandlist) == 0)
806         {
807                 type = ELEMENT_EXPANDLIST;
808         }
809
810         return type;
811 }
812
813 void
814 _AppSettingImpl::GetMinMaxValue(xmlNodePtr pNode, _ElementType type, int& min, int& max)
815 {
816         const xmlChar* pPropMin = reinterpret_cast<const xmlChar*>("min");
817         const xmlChar* pPropMax = reinterpret_cast<const xmlChar*>("max");
818         const xmlChar* pPropMinLength = reinterpret_cast<const xmlChar*>("minlength");
819         const xmlChar* pPropMaxLength = reinterpret_cast<const xmlChar*>("maxlength");
820
821         if (type == ELEMENT_INTEGER)
822         {
823                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMin);
824                 if (pStrMin)
825                 {
826                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
827                         xmlFree(pStrMin);
828                 }
829                 else
830                 {
831                         SysLog(NID_APP, "Failed to get 'min' value.");
832                 }
833
834                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMax);
835                 if (pStrMax)
836                 {
837                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
838                         xmlFree(pStrMax);
839                 }
840                 else
841                 {
842                         SysLog(NID_APP, "Failed to get 'max' value.");
843                 }
844         }
845         else
846         if (type == ELEMENT_STRING)
847         {
848                 xmlChar* pStrMin = xmlGetProp(pNode, pPropMinLength);
849                 if (pStrMin)
850                 {
851                         Integer::Parse(reinterpret_cast<char*>(pStrMin), min);
852                         xmlFree(pStrMin);
853                 }
854                 else
855                 {
856                         SysLog(NID_APP, "Failed to get 'minlength' value.");
857                 }
858
859                 xmlChar* pStrMax = xmlGetProp(pNode, pPropMaxLength);
860                 if (pStrMax)
861                 {
862                         Integer::Parse(reinterpret_cast<char*>(pStrMax), max);
863                         xmlFree(pStrMax);
864                 }
865                 else
866                 {
867                         SysLog(NID_APP, "Failed to get 'maxlength' value.");
868                 }
869         }
870         else
871         {
872                 SysLog(NID_APP, "Invalid element type.");
873         }
874 }
875
876 Object*
877 _AppSettingImpl::GetValueObjectN(_ElementType type, String& strValue, bool validValue)
878 {
879         const String strTrue(L"true");
880         Object* pValueObject = null;
881
882         switch (type)
883         {
884         case ELEMENT_BOOL:
885                 {
886                         bool valueBool = false;
887                         if (validValue)
888                         {
889                                 if (strValue == strTrue)
890                                 {
891                                         valueBool = true;
892                                 }
893                         }
894                         pValueObject = new (std::nothrow) Boolean(valueBool);
895                         SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
896                                                  GetErrorMessage(E_OUT_OF_MEMORY));
897                 }
898                 break;
899
900         case ELEMENT_INTEGER:
901                 {
902                         int valueInteger = 0;
903                         if (validValue)
904                         {
905                                 Integer::Parse(strValue, valueInteger);
906                         }
907                         pValueObject = new (std::nothrow) Integer(valueInteger);
908                         SysTryReturn(NID_APP, pValueObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
909                                                  GetErrorMessage(E_OUT_OF_MEMORY));
910                 }
911                 break;
912
913         case ELEMENT_STRING:
914         case ELEMENT_EXPANDLIST:
915                 {
916                         String* pStringObject = new (std::nothrow) String;
917                         SysTryReturn(NID_APP, pStringObject != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
918                                                  GetErrorMessage(E_OUT_OF_MEMORY));
919                         if (pStringObject && validValue)
920                         {
921                                 *pStringObject = strValue;
922                         }
923                         pValueObject = pStringObject;
924                 }
925                 break;
926
927         case ELEMENT_INVALID:
928                 // It already validate by caller;
929                 SysLog(NID_APP, "Invalid element type.");
930                 break;
931         }
932
933         return pValueObject;
934 }
935
936 bool
937 _AppSettingImpl::UpdateProperty(xmlNodePtr pNode, const String& value)
938 {
939         const xmlChar* pPropValue = reinterpret_cast<const xmlChar*>("value");
940
941         if (value.IsEmpty())
942         {
943                 const xmlChar* pZeroLength = reinterpret_cast<const xmlChar*>("");
944                 xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, pZeroLength);
945                 if (attrPtr)
946                 {
947                         return true;
948                 }
949         }
950         else
951         {
952                 std::unique_ptr<ByteBuffer> pBuf(Tizen::Base::Utility::StringUtil::StringToUtf8N(value));
953                 if (pBuf)
954                 {
955                         xmlAttrPtr attrPtr = xmlSetProp(pNode, pPropValue, reinterpret_cast<const xmlChar*>(pBuf->GetPointer()));
956                         if (attrPtr)
957                         {
958                                 return true;
959                         }
960                 }
961                 else
962                 {
963                         SysLog(NID_APP, "StringToUtf8N is failed.");
964                 }
965         }
966         return false;
967 }
968
969 bool
970 _AppSettingImpl::InitializeDBus(void)
971 {
972         DBusError error;
973         dbus_error_init(&error);
974
975         if (pDBusConnection == null)
976         {
977                 char rule[MAX_LOCAL_BUFSIZE];
978                 pDBusConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
979                 if (pDBusConnection == null)
980                 {
981                         SysLog(NID_APP, "Fail to connect to the D-BUS daemon: %s", error.message);
982                         dbus_error_free(&error);
983                         return false;
984                 }
985                 dbus_connection_setup_with_g_main(pDBusConnection, null);
986
987                 snprintf(rule, MAX_LOCAL_BUFSIZE, "path='%s',type='signal',interface='%s'", DBUS_PATH, DBUS_SIGNAL_INTERFACE);
988                 dbus_bus_add_match(pDBusConnection, rule, &error);
989                 if (dbus_error_is_set(&error))
990                 {
991                         SysLog(NID_APP, "Fail to add match : %s", error.message);
992                         dbus_error_free(&error);
993                         return false;
994                 }
995         }
996
997         // this value is vary by AppSetting instance for proper handling.
998         // Should be explicit remove filter on ReleaseInstanceByAppId.
999         if (dbus_connection_add_filter(pDBusConnection, HandleDBusMessage, this, NULL) == FALSE)
1000         {
1001                 SysLog(NID_APP, "Fail to add filter : %s", error.message);
1002                 dbus_error_free(&error);
1003                 return false;
1004         }
1005         return true;
1006 }
1007
1008 DBusHandlerResult
1009 _AppSettingImpl::HandleDBusMessage(DBusConnection* connection, DBusMessage* message, void* user_data)
1010 {
1011         int my_pid = getpid();
1012         int sender_pid = 0;
1013         char* pId = NULL;
1014         char* pValue = NULL;
1015
1016         DBusError error;
1017         dbus_error_init(&error);
1018
1019         _AppSettingImpl* pAppSettingImpl = static_cast<_AppSettingImpl*>(user_data);
1020         SysTryLogReturn(NID_APP, pAppSettingImpl != null, DBUS_HANDLER_RESULT_HANDLED,
1021                                         "Not valid pAppSettingImpl from user_data");
1022         String signalString(DBUS_SIGNAL_PREFIX);
1023         signalString += pAppSettingImpl->__correspondAppId;
1024         std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1025         SysTryLogReturn(NID_APP, pBufferSignal != null, DBUS_HANDLER_RESULT_HANDLED, "pBufferSignal is not valid");
1026         const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1027
1028         if (dbus_message_is_signal(message, DBUS_SIGNAL_INTERFACE, pCharBufferSignal))
1029         {
1030                 if (dbus_message_get_args(message, &error,
1031                                                                   DBUS_TYPE_UINT32, &sender_pid,
1032                                                                   DBUS_TYPE_STRING, &pId,
1033                                                                   DBUS_TYPE_STRING, &pValue,
1034                                                                   DBUS_TYPE_INVALID) == FALSE)
1035                 {
1036                         SysLog(NID_APP, "Fail to get data : %s", error.message);
1037                         dbus_error_free(&error);
1038                         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1039                 }
1040
1041                 if (sender_pid != 0 && my_pid != sender_pid)
1042                 {
1043                         pAppSettingImpl->SetValueFromDBusData(pId, pValue);
1044                 }
1045                 return DBUS_HANDLER_RESULT_HANDLED;
1046         }
1047         else
1048         {
1049                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1050         }
1051 }
1052
1053 void
1054 _AppSettingImpl::SetValueFromDBusData(const char* pId, const char* pValue)
1055 {
1056         const String delimiter(L"|");
1057         String strId;
1058         String strValue;
1059         int indexOfDelimiter = 0;
1060
1061         // Native Setting module send the key as "id|title" so need to get id part before the '|' character.
1062         // Value is "INT|value" for check box and slider Or "STRING|value" so need to get right side value string.
1063         String strOrg(pId);
1064         strOrg.Trim();
1065         result r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1066         SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1067         strOrg.SubString(0, indexOfDelimiter, strId);   // Get left string of '|'
1068
1069         strOrg = pValue;
1070         strOrg.Trim();
1071         r = strOrg.IndexOf(delimiter, 0, indexOfDelimiter);
1072         SysTryReturnVoidResult(NID_APP, !IsFailed(r), r, "[%s] Propagating. Bus data parsing failed", GetErrorMessage(r));
1073         strOrg.SubString(indexOfDelimiter+1, strValue); // Get right string of '|'
1074
1075         const Object* pObject = __settingContainer.GetValue(strId);
1076         if (pObject)
1077         {
1078                 const _SettingItem* pItem = dynamic_cast<const _SettingItem*>(pObject);
1079                 SysTryReturnVoidResult(NID_APP, pItem != null, E_SYSTEM, "[%s] A system error has been occurred. Casting failed to item.",
1080                                                            GetErrorMessage(E_SYSTEM));
1081                 const Boolean* pBoolean = dynamic_cast<const Boolean*>(pItem->GetValue());
1082                 if (pBoolean)
1083                 {
1084                         static const String strFalse(L"0");
1085                         bool valueBool = true;
1086
1087                         if (strValue == strFalse)
1088                         {
1089                                 valueBool = false;
1090                         }
1091                         SetValue(strId, valueBool, false);
1092                         return;
1093                 }
1094                 const Integer* pInteger = dynamic_cast<const Integer*>(pItem->GetValue());
1095                 if (pInteger)
1096                 {
1097                         int valueInteger = 0;
1098                         Integer::Parse(strValue, valueInteger);
1099                         SetValue(strId, valueInteger, false);
1100                         return;
1101                 }
1102                 const String* pString = dynamic_cast<const String*>(pItem->GetValue());
1103                 if (pString)
1104                 {
1105                         SetValue(strId, strValue, false);
1106                         return;
1107                 }
1108                 SysSecureLog(NID_APP, "Failed determine value type for id= %s", pId);
1109         }
1110         else
1111         {
1112                 SysSecureLog(NID_APP, "Failed find value for id= %s", pId);
1113         }
1114 }
1115
1116 bool
1117 _AppSettingImpl::SendMessage(const String& id, const String& value, bool intType)
1118 {
1119         if (__oldVersionInstance)
1120         {
1121                 return true;    // NOP for old version instance.
1122         }
1123
1124         if (!pDBusConnection)
1125         {
1126                 SysLog(NID_APP, "DBus connection invalid");
1127                 return false;
1128         }
1129
1130         bool result = true;
1131         const String strRightOfId(L"|N/A");
1132         const String strLeftOfValueIntType(L"INT|");
1133         const String strLeftOfValueStrType(L"STRING|");
1134
1135         String strId = id + strRightOfId;
1136         String strValue;
1137         if (intType)
1138         {
1139                 strValue = strLeftOfValueIntType;
1140         }
1141         else
1142         {
1143                 strValue = strLeftOfValueStrType;
1144         }
1145         strValue += value;
1146         String signalString(DBUS_SIGNAL_PREFIX);
1147         signalString += __correspondAppId;
1148
1149         std::unique_ptr<ByteBuffer> pBufferId(Tizen::Base::Utility::StringUtil::StringToUtf8N(strId));
1150         std::unique_ptr<ByteBuffer> pBufferValue(Tizen::Base::Utility::StringUtil::StringToUtf8N(strValue));
1151         std::unique_ptr<ByteBuffer> pBufferSignal(Tizen::Base::Utility::StringUtil::StringToUtf8N(signalString));
1152         if (pBufferId && pBufferValue && pBufferSignal)
1153         {
1154                 int sender_pid = getpid();
1155                 const char* pCharBufferId = reinterpret_cast<const char*>(pBufferId->GetPointer());
1156                 const char* pCharBufferValue = reinterpret_cast<const char*>(pBufferValue->GetPointer());
1157                 const char* pCharBufferSignal = reinterpret_cast<const char*>(pBufferSignal->GetPointer());
1158
1159                 DBusMessage* pMessageWihtAppId = dbus_message_new_signal(DBUS_PATH, DBUS_SIGNAL_INTERFACE, pCharBufferSignal);
1160                 if (dbus_message_append_args(pMessageWihtAppId,
1161                                                                          DBUS_TYPE_UINT32, &sender_pid,
1162                                                                          DBUS_TYPE_STRING, &pCharBufferId,
1163                                                                          DBUS_TYPE_STRING, &pCharBufferValue,
1164                                                                          DBUS_TYPE_INVALID) == FALSE)
1165                 {
1166                         SysLog(NID_APP, "DBus connection invalid");
1167                         result = false;
1168                 }
1169
1170                 if (dbus_connection_send(pDBusConnection, pMessageWihtAppId, NULL) == FALSE)
1171                 {
1172                         SysLog(NID_APP, "Fail to send message");
1173                         result = false;
1174                 }
1175
1176                 dbus_connection_flush(pDBusConnection);
1177                 dbus_message_unref(pMessageWihtAppId);
1178         }
1179         else
1180         {
1181                 SysLog(NID_APP, "System error: memory full ?");
1182         }
1183
1184         return result;
1185 }
1186
1187 AppSetting*
1188 _AppSettingImpl::CreateAppSetting(void)
1189 {
1190         AppSetting* pAppSetting = new (std::nothrow) AppSetting;
1191         SysTryReturn(NID_APP, pAppSetting != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
1192                                  GetErrorMessage(E_OUT_OF_MEMORY));
1193
1194         return pAppSetting;
1195 }
1196
1197 void
1198 _AppSettingImpl::SetImplInstance(AppSetting& appSetting, _AppSettingImpl& impl)
1199 {
1200         appSetting.__pAppSettingImpl = &impl;
1201 }
1202
1203 } } // Tizen::App