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