nativeinfologctrl - native platform info log on/off script
[platform/framework/native/appfw.git] / src / system-server / setting / providers / FSys_SettingApplicationProvider.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        FSys_SettingApplicationProvider.cpp
19  * @brief       This is the implementation for the _SettingApplicationProvider class.
20  */
21 #include <unique_ptr.h>
22
23 #include <FApp.h>
24 #include <FApp_Aul.h>
25 #include <FBase_StringConverter.h>
26
27 #include <FBaseString.h>
28
29 #include "FSys_SettingInfo.h"
30 #include "FSys_SettingApplicationProvider.h"
31
32 using namespace std;
33
34 using namespace Tizen::App;
35 using namespace Tizen::Base;
36
37 namespace Tizen { namespace System
38 {
39
40 static const wchar_t* _APPLICATION_HOME = L"http://tizen.org/setting/application.home";
41 static const wchar_t* _APPLICATION_LOCK = L"http://tizen.org/setting/application.lock";
42
43 struct charDeleter
44 {
45         void operator()(char* pValue)
46         {
47                 if(pValue != null)
48                 {
49                         free(pValue);
50                         pValue = null;
51                 }
52         }
53 };
54
55 _SettingApplicationProvider::_SettingApplicationProvider()
56 {
57         int errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, SettingEventVConf, null);
58         if(errorCode != 0)
59         {
60                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register event listen for home application change.");
61         }
62         errorCode = vconf_notify_key_changed(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, SettingEventVConf, null);
63         if(errorCode != 0)
64         {
65                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to register event listen for lock application change.");
66         }
67 }
68
69 _SettingApplicationProvider::~_SettingApplicationProvider()
70 {
71         int errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, SettingEventVConf);
72         if(errorCode != 0)
73         {
74                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister event listen for home application change.");
75         }
76
77         errorCode = vconf_ignore_key_changed(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, SettingEventVConf);
78         if(errorCode != 0)
79         {
80                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to unregister event listen for lock application change.");
81         }
82 }
83
84 bool
85 _SettingApplicationProvider::HasKey(const String& key)
86 {
87         if(key == _APPLICATION_HOME)
88         {
89                 unique_ptr<char, charDeleter> pPackageName(vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME));
90
91                 if(pPackageName.get() != null)
92                 {
93                         return true;
94                 }
95         }
96         else if(key == _APPLICATION_LOCK)
97         {
98                 unique_ptr<char, charDeleter> pPackageName(vconf_get_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR));
99
100                 if(pPackageName.get() != null)
101                 {
102                         return true;
103                 }
104         }
105         return false;
106 }
107
108 result
109 _SettingApplicationProvider::GetValue(const String& key, String& value)
110 {
111         result r = E_OBJ_NOT_FOUND;
112
113         SysLog(NID_SYS, "Key[%ls] is required.", key.GetPointer());
114         if(key == _APPLICATION_HOME)
115         {
116                 unique_ptr<char, charDeleter> pPackageName(vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME));
117                 SysLog(NID_SYS, "Value for Key[%ls] is %s.", key.GetPointer(), pPackageName.get());
118                 SysTryReturnResult(NID_SYS, pPackageName != null, E_UNSUPPORTED_OPERATION, "It is failed to get package name of home application.");
119                 value.Clear();
120                 value.Append(pPackageName.get());
121                 r = E_SUCCESS;
122         }
123         else if(key == _APPLICATION_LOCK)
124         {
125                 unique_ptr<char, charDeleter> pPackageName(vconf_get_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR));
126                 SysLog(NID_SYS, "Value for Key[%ls] is %s.", key.GetPointer(), pPackageName.get());
127                 SysTryReturnResult(NID_SYS, pPackageName != null, E_UNSUPPORTED_OPERATION, "It is failed to get package name of lock application.");
128                 value.Clear();
129                 value.Append(pPackageName.get());
130                 r = E_SUCCESS;
131         }
132
133         return r;
134 }
135
136 result
137 _SettingApplicationProvider::SetValueForPrivilegedKey(const String& key, String value)
138 {
139         result r = E_OBJ_NOT_FOUND;
140
141         int errorCode = 0;
142
143         if(key == _APPLICATION_HOME)
144         {
145                 bool installed = _Aul::IsInstalled(value);
146                 SysTryReturnResult(NID_SYS, installed == true, E_INVALID_ARG, "Specified application[%ls] is not installed.", value.GetPointer());
147                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support home application.");
148                 r = E_SUCCESS;
149
150                 unique_ptr<char []> pPackageName(_StringConverter::CopyToCharArrayN(value));
151                 SysTryReturnResult(NID_SYS, pPackageName.get() != null, E_SYSTEM, "It is failed to get string of specified value[%ls].", value.GetPointer());
152                 SysLog(NID_SYS, "Value for Key[%ls] is %s.", key.GetPointer(), pPackageName.get());
153                 errorCode = vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, pPackageName.get());
154                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set home application.");
155         }
156         else if(key == _APPLICATION_LOCK)
157         {
158                 bool installed = _Aul::IsInstalled(value);
159                 SysTryReturnResult(NID_SYS, installed == true, E_INVALID_ARG, "Specified application[%ls] is not installed.", value.GetPointer());
160                 SysTryReturnResult(NID_SYS, HasKey(key) == true, E_UNSUPPORTED_OPERATION, "Current device does not support lock application.");
161                 r = E_SUCCESS;
162
163                 unique_ptr<char []> pPackageName(_StringConverter::CopyToCharArrayN(value));
164                 SysTryReturnResult(NID_SYS, pPackageName.get() != null, E_SYSTEM, "It is failed to get string of specified value[%ls].", value.GetPointer());
165                 SysLog(NID_SYS, "Value for Key[%ls] is %s.", key.GetPointer(), pPackageName.get());
166                 errorCode = vconf_set_str(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, pPackageName.get());
167                 SysTryReturnResult(NID_SYS, errorCode == 0, E_SYSTEM, "It is failed to set lock application.");
168         }
169         return r;
170 }
171
172 void
173 _SettingApplicationProvider::SettingEventVConf(keynode_t* node, void* userData)
174 {
175         String settingKey;
176
177         _SettingInfo* pSettingInfo = _SettingInfo::GetInstance();
178         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_SYSTEM, "_SettingInfo is not ready.");
179
180         if(strcmp(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, vconf_keynode_get_name(node)) == 0)
181         {
182                 settingKey.Append(_APPLICATION_HOME);
183         }
184         else if(strcmp(VCONFKEY_SETAPPL_3RD_LOCK_PKG_NAME_STR, vconf_keynode_get_name(node)) == 0)
185         {
186                 settingKey.Append(_APPLICATION_LOCK);
187         }
188         else
189         {
190                 SysLogException(NID_SYS, E_SYSTEM, "There is no reserved key [%s].", vconf_keynode_get_name(node));
191                 return;
192         }
193
194         result r = pSettingInfo->AnnounceSettingEvent(settingKey);
195         SysTryReturnVoidResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to send the event[%ls].", settingKey.GetPointer());
196 }
197
198 }}