Fix system-server for settings
[platform/framework/native/appfw.git] / src / system-server / setting / FSys_SettingInfo.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_SettingInfo.cpp
19  * @brief               This is the implementation file for _SettingInfo class.
20  */
21
22 #include <new>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <unique_ptr.h>
26 #include <sysman.h>
27
28 #include <FBaseColIEnumeratorT.h>
29 #include <FIoFile.h>
30
31 #include "providers/FSys_ISettingProvider.h"
32 #include "FSys_SettingInfo.h"
33
34 //providers
35 #include "providers/FSys_SettingApplicationProvider.h"
36 #include "providers/FSys_SettingFontProvider.h"
37 #include "providers/FSys_SettingGeneralProvider.h"
38 #include "providers/FSys_SettingLocaleProvider.h"
39 #include "providers/FSys_SettingLocationProvider.h"
40 #include "providers/FSys_SettingNetworkProvider.h"
41 #include "providers/FSys_SettingScreenProvider.h"
42 #include "providers/FSys_SettingSoundProvider.h"
43 #include "providers/FSys_SettingSpeechProvider.h"
44 #include "providers/FSys_SettingStorageProvider.h"
45 #include "providers/FSys_SettingVibrationProvider.h"
46
47 using namespace std;
48
49 using namespace Tizen::Base;
50 using namespace Tizen::Base::Collection;
51 using namespace Tizen::Io;
52
53 namespace Tizen { namespace System
54 {
55
56 static const String _SETTING_APPLICATION_PROVIDER(L"application");
57 static const String _SETTING_FONT_PROVIDER(L"font");
58 static const String _SETTING_DEVICE_NAME_PROVIDER(L"device_name");
59 static const String _SETTING_MOTION_PROVIDER(L"motion");
60 static const String _SETTING_CONTACTS_PROVIDER(L"contacts");
61 static const String _SETTING_DEVELOPER_PROVIDER(L"developer");
62 static const String _SETTING_BATTERY_PROVIDER(L"battery");
63 static const String _SETTING_GRAPHICS_PROVIDER(L"graphics");
64 static const String _SETTING_LOCALE_PROVIDER(L"locale");
65 static const String _SETTING_LOCATION_PROVIDER(L"location");
66 static const String _SETTING_NETWORK_PROVIDER(L"network");
67 static const String _SETTING_SCREEN_PROVIDER(L"screen");
68 static const String _SETTING_SOUND_PROVIDER(L"sound");
69 static const String _SETTING_SPEECH_PROVIDER(L"speech");
70 static const String _SETTING_STORAGE_PROVIDER(L"storage");
71 static const String _SETTING_USB_PROVIDER(L"usb");
72 static const String _SETTING_VIBRATION_PROVIDER(L"vibrator");
73
74 static const wchar_t* _SETTING_PREFIX = L"http://tizen.org/setting/";
75
76 _SettingInfo* _SettingInfo::__pSettingInfo = null;
77 static const char* _BIN_FACTORY_RESET = "/usr/bin/factory-reset";
78
79 _SettingInfo::_SettingInfo()
80 {
81         static _StringHashProvider hashProvider;
82         static _StringComparer stringComparer;
83         __settingProviderList.Construct(0, 0, hashProvider, stringComparer);
84
85         _SettingApplicationProvider* applicationProvider = new (std::nothrow) _SettingApplicationProvider();
86         RegisterProvider(_SETTING_APPLICATION_PROVIDER, (_ISettingProvider*)applicationProvider);
87
88         _SettingFontProvider* fontProvider = new (std::nothrow) _SettingFontProvider();
89         RegisterProvider(_SETTING_FONT_PROVIDER, (_ISettingProvider*)fontProvider);
90
91         _SettingGeneralProvider* generalProvider = new (std::nothrow) _SettingGeneralProvider();
92         RegisterProvider(_SETTING_DEVICE_NAME_PROVIDER, (_ISettingProvider*)generalProvider);
93         RegisterProvider(_SETTING_MOTION_PROVIDER, (_ISettingProvider*)generalProvider);
94         RegisterProvider(_SETTING_CONTACTS_PROVIDER, (_ISettingProvider*)generalProvider);
95         RegisterProvider(_SETTING_DEVELOPER_PROVIDER, (_ISettingProvider*)generalProvider);
96         RegisterProvider(_SETTING_BATTERY_PROVIDER, (_ISettingProvider*)generalProvider);
97         RegisterProvider(_SETTING_GRAPHICS_PROVIDER, (_ISettingProvider*)generalProvider);
98
99         _SettingLocaleProvider* localeProvider = new (std::nothrow) _SettingLocaleProvider();
100         RegisterProvider(_SETTING_LOCALE_PROVIDER, (_ISettingProvider*)localeProvider);
101         
102         _SettingLocationProvider* locationProvider = new (std::nothrow) _SettingLocationProvider();
103         RegisterProvider(_SETTING_LOCATION_PROVIDER, (_ISettingProvider*)locationProvider);
104
105         _SettingNetworkProvider* networkProvider = new (std::nothrow) _SettingNetworkProvider();
106         RegisterProvider(_SETTING_NETWORK_PROVIDER, (_ISettingProvider*)networkProvider);
107         RegisterProvider(_SETTING_USB_PROVIDER, (_ISettingProvider*)networkProvider);
108
109         _SettingScreenProvider* screenProvider = new (std::nothrow) _SettingScreenProvider();
110         RegisterProvider(_SETTING_SCREEN_PROVIDER, (_ISettingProvider*)screenProvider);
111
112         _SettingSoundProvider* soundProvider = new (std::nothrow) _SettingSoundProvider();
113         RegisterProvider(_SETTING_SOUND_PROVIDER, (_ISettingProvider*)soundProvider);
114
115         _SettingSpeechProvider* speechProvider = new (std::nothrow) _SettingSpeechProvider();
116         RegisterProvider(_SETTING_SPEECH_PROVIDER, (_ISettingProvider*)speechProvider);
117         
118         _SettingStorageProvider* storageProvider = new (std::nothrow) _SettingStorageProvider();
119         RegisterProvider(_SETTING_STORAGE_PROVIDER, (_ISettingProvider*)storageProvider);
120
121         _SettingVibrationProvider* vibrationProvider = new (std::nothrow) _SettingVibrationProvider();
122         RegisterProvider(_SETTING_VIBRATION_PROVIDER, (_ISettingProvider*)vibrationProvider);
123
124         __settingEventList.Construct();
125 }
126
127 _SettingInfo::~_SettingInfo()
128 {
129         RemoveProvider(_SETTING_APPLICATION_PROVIDER);
130         RemoveProvider(_SETTING_FONT_PROVIDER);
131         UnregisterProvider(_SETTING_MOTION_PROVIDER);
132         UnregisterProvider(_SETTING_CONTACTS_PROVIDER);
133         UnregisterProvider(_SETTING_DEVELOPER_PROVIDER);
134         UnregisterProvider(_SETTING_BATTERY_PROVIDER);
135         UnregisterProvider(_SETTING_GRAPHICS_PROVIDER);
136         RemoveProvider(_SETTING_LOCALE_PROVIDER);
137         RemoveProvider(_SETTING_LOCATION_PROVIDER);
138         RemoveProvider(_SETTING_NETWORK_PROVIDER);
139         RemoveProvider(_SETTING_SCREEN_PROVIDER);
140         RemoveProvider(_SETTING_SOUND_PROVIDER);
141         RemoveProvider(_SETTING_SPEECH_PROVIDER);
142         RemoveProvider(_SETTING_STORAGE_PROVIDER);
143         RemoveProvider(_SETTING_VIBRATION_PROVIDER);
144         __settingProviderList.RemoveAll();
145 }
146
147 result
148 _SettingInfo::RemoveProvider(const String key)
149 {
150         _ISettingProvider* pProvider = null;
151
152         pProvider = GetProvider(key);
153         if(pProvider != null)
154         {
155                 UnregisterProvider(key);
156                 delete pProvider;
157                 return E_SUCCESS;
158         }
159         return E_SYSTEM;
160 }
161
162 _SettingInfo*
163 _SettingInfo::GetInstance(void)
164 {
165         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
166         if(__pSettingInfo == null)
167         {
168                 pthread_once(&onceBlock, InitSingleton);
169         }
170         return __pSettingInfo;
171 }
172
173 void
174 _SettingInfo::InitSingleton(void)
175 {
176         _SettingInfo* pSettingInfo = new (std::nothrow) _SettingInfo();
177         SysTryReturnVoidResult(NID_SYS, pSettingInfo != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.",
178                                                    GetErrorMessage(E_OUT_OF_MEMORY));
179
180         __pSettingInfo = pSettingInfo;
181         atexit(DestroySingleton);
182 }
183
184 void
185 _SettingInfo::DestroySingleton(void)
186 {
187         delete __pSettingInfo;
188 }
189
190 _ISettingProvider*
191 _SettingInfo::GetProvider(const String& key)
192 {
193         int index = 0;
194         String requiredKey;
195         String prefix(_SETTING_PREFIX);
196         _ISettingProvider* provider = null;
197
198         key.SubString(prefix.GetLength(), requiredKey);
199         requiredKey.IndexOf(L".", 0, index);
200         requiredKey.SubString(0, index, requiredKey);
201
202         result r = __settingProviderList.GetValue(requiredKey, provider);
203
204         if(r != E_SUCCESS || provider == null)
205         {
206                 SysLogException(NID_SYS, E_SYSTEM, "It is failed to find proper provider for required key(%ls).", key.GetPointer());
207                 return null;
208         }
209
210         return provider;        
211 }
212
213 result
214 _SettingInfo::GetValue(const String& key, String& value)
215 {
216         _ISettingProvider* provider = GetProvider(key);
217         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
218         return provider->GetValue(key, value);
219 }
220
221 result
222 _SettingInfo::GetValue(const String& key, int& value)
223 {
224         _ISettingProvider* provider = GetProvider(key);
225         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
226         return provider->GetValue(key, value);
227 }
228
229 result
230 _SettingInfo::GetValue(const String& key, bool& value)
231 {
232         _ISettingProvider* provider = GetProvider(key);
233         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
234         return provider->GetValue(key, value);
235 }
236
237 bool
238 _SettingInfo::HasKey(const String& key)
239 {
240         return false;
241 }
242
243 result
244 _SettingInfo::SetValue(const String& key, bool value)
245 {
246         _ISettingProvider* provider = GetProvider(key);
247         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
248         return provider->SetValue(key, value);
249 }
250
251 result
252 _SettingInfo::SetValue(const String& key, int value)
253 {
254         _ISettingProvider* provider = GetProvider(key);
255         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
256         return provider->SetValue(key, value);
257 }
258
259 result
260 _SettingInfo::SetValue(const String& key, String value)
261 {
262         _ISettingProvider* provider = GetProvider(key);
263         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
264         return provider->SetValue(key, value);
265 }
266
267 result
268 _SettingInfo::AddSettingEventListener(ISettingEventListener& listener)
269 {
270         return __settingEventList.Add(&listener);
271 }
272
273 result
274 _SettingInfo::RemoveSettingEventListener(ISettingEventListener& listener)
275 {
276         return __settingEventList.Remove(&listener);
277 }
278
279 result
280 _SettingInfo::AnnounceSettingEvent(const String& key)
281 {       
282         IEnumeratorT< ISettingEventListener*>* pEnum = __settingEventList.GetEnumeratorN();
283         SysTryReturnResult(NID_SYS, pEnum != null, E_SYSTEM, "It is failed to get event listener list.");
284
285         SysLog(NID_SYS, "Setting event[%ls] is occured.", key.GetPointer());
286         while(pEnum->MoveNext() == E_SUCCESS)
287         {
288                 ISettingEventListener* pEventListener = null;
289                 pEnum->GetCurrent(pEventListener);
290
291                 if(pEventListener != null)
292                 {
293                         String eventKey = key;
294                         pEventListener->OnSettingChanged(eventKey);
295
296                         SysLog(NID_SYS, "Event is delivered to %x.", pEventListener);
297                         
298                 }
299         }
300
301         delete pEnum;
302         return E_SUCCESS;
303 }
304
305 result
306 _SettingInfo::GetValueForPrivilegedKey(const String& key, bool& value)
307 {
308         _ISettingProvider* provider = GetProvider(key);
309         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
310         return provider->GetValueForPrivilegedKey(key, value);
311 }
312
313 result
314 _SettingInfo::SetValueForPrivilegedKey(const String& key, bool value)
315 {
316         _ISettingProvider* provider = GetProvider(key);
317         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
318         return provider->SetValueForPrivilegedKey(key, value);
319 }
320
321 result
322 _SettingInfo::SetValueAsyncForPrivilegedKey(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
323 {
324         _ISettingProvider* provider = GetProvider(key);
325         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
326         return provider->SetValueAsyncForPrivilegedKey(key, value, listener);
327 }
328
329 result
330 _SettingInfo::SetValueAsync(const String& key, bool value, ISettingInfoSetValueAsyncResultListener* listener)
331 {
332         _ISettingProvider* provider = GetProvider(key);
333         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
334         return provider->SetValueAsync(key, value, listener);
335 }
336
337 result
338 _SettingInfo::SetValueForPrivilegedKey(const String& key, String value)
339 {
340         _ISettingProvider* provider = GetProvider(key);
341         SysTryReturnResult(NID_SYS, provider != null, E_SYSTEM, "It is failed to find provider.");
342         return provider->SetValueForPrivilegedKey(key, value);
343 }
344
345 result
346 _SettingInfo::ResetToFactoryDefault(void)
347 {
348         SysTryReturnResult(NID_SYS, File::IsFileExist(_BIN_FACTORY_RESET) == true, E_UNSUPPORTED_OPERATION, "Current device does not support factory reset.");
349         SysTryReturnResult(NID_SYS, sysman_call_predef_action("launchfr", 0, NULL) == 0, E_SYSTEM, "It is failed to execute factory reset.");
350         return E_SUCCESS;
351 }
352
353 result
354 _SettingInfo::RegisterProvider(const String& key, _ISettingProvider* provider)
355 {
356         bool exist = false;
357         result r = __settingProviderList.ContainsKey(key, exist);
358         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check required key(%ls).", key.GetPointer());
359         SysTryReturnResult(NID_SYS, exist == false, E_OBJ_ALREADY_EXIST, "It is already registered key(%ls).", key.GetPointer());
360
361         r = __settingProviderList.Add(key, provider);
362         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to add required key(%ls).", key.GetPointer());
363         return E_SUCCESS;
364 }
365
366 result
367 _SettingInfo::UnregisterProvider(const String& key)
368 {
369         bool exist = false;
370         result r = __settingProviderList.ContainsKey(key, exist);
371         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to check required key(%ls).", key.GetPointer());
372         SysTryReturnResult(NID_SYS, exist == true, E_OBJ_NOT_FOUND, "It is already registered key(%ls).", key.GetPointer());
373
374         r = __settingProviderList.Remove(key);
375         SysTryReturnResult(NID_SYS, r == E_SUCCESS, E_SYSTEM, "It is failed to remove required key(%ls).", key.GetPointer());
376         return E_SUCCESS;
377 }
378
379 } }