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