tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / SystemSetting / SystemSetting.cpp
1 //
2 // Tizen Web Device API
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 #include <Commons/Exception.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include "SystemSetting.h"
22 #include <netdb.h>
23 #include <arpa/inet.h>
24 #include <string.h>
25 #include <system_settings.h>
26
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace WrtDeviceApis::Commons;
29
30 namespace DeviceAPI {
31 namespace SystemSetting {
32
33 namespace {
34 static const std::string ERR_SYSTEM_SETTINGS_INVALID_PARAMETER = "Invalid parameter";
35 static const std::string ERR_SYSTEM_SETTINGS_OUT_OF_MEMORY = "Out of memory";
36 static const std::string ERR_SYSTEM_SETTINGS_IO_ERROR = "Internal I/O error";
37 static const std::string ERR_SYSTEM_SETTINGS_PERMISSION_DENIED = "Permission denied";
38 static const std::string ERR_SYSTEM_SETTINGS_UNKNOWN = "Unknown error";
39 }
40
41 const std::string& getSystemSettingsErrorMessage(int errorCode)
42 {
43     switch (errorCode) {
44         case SYSTEM_SETTINGS_ERROR_INVALID_PARAMETER:
45             return ERR_SYSTEM_SETTINGS_INVALID_PARAMETER;
46         case SYSTEM_SETTINGS_ERROR_OUT_OF_MEMORY:
47             return ERR_SYSTEM_SETTINGS_OUT_OF_MEMORY;
48         case SYSTEM_SETTINGS_ERROR_IO_ERROR:
49             return ERR_SYSTEM_SETTINGS_IO_ERROR;
50         case SYSTEM_SETTINGS_ERROR_PERMISSION_DENIED:
51             return ERR_SYSTEM_SETTINGS_PERMISSION_DENIED;
52         default:
53             return ERR_SYSTEM_SETTINGS_UNKNOWN;
54     }
55 }
56
57 std::string SystemSetting::getSystemSettingsLogMessage(const int errorCode, const std::string &hint)
58 {
59     std::stringstream ss;
60     ss << "Failed " << hint << " : " << getSystemSettingsErrorMessage(errorCode) << ", " << errorCode;
61     return ss.str();
62 }
63
64 SystemSetting::SystemSetting()
65 {
66 }
67
68 SystemSetting::~SystemSetting()
69 {
70 }
71
72 void SystemSetting::setWallpaper(const EventSetWallpaperPtr &event)
73 {
74     LOGD("enter");
75     EventRequestReceiver<EventSetWallpaper>::PostRequest(event);
76 }
77
78 void SystemSetting::setRingtone(const EventSetRingtonePtr &event)
79 {
80     LOGD("enter");
81     EventRequestReceiver<EventSetRingtone>::PostRequest(event);
82 }
83
84 void SystemSetting::setProperty(const EventSetSettingPtr &event)
85 {
86     LOGD("enter");
87     EventRequestReceiver<EventSetSetting>::PostRequest(event);
88 }
89
90 void SystemSetting::getProperty(const EventGetSettingPtr &event)
91 {
92     LOGD("enter");
93     EventRequestReceiver<EventGetSetting>::PostRequest(event);
94 }
95
96 std::string SystemSetting::getProperty(const SettingType settingType)
97 {
98     LOGD("enter");
99     int ret = SYSTEM_SETTINGS_ERROR_NONE;
100
101     char *value = NULL;
102     std::string gettingValue;
103
104     if (settingType == HOME_SCREEN)
105     {
106         LOGD("home screen");
107         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &value);
108         LOGD("result : %d value: %s", ret, value);
109     }
110     else if (settingType == LOCK_SCREEN)
111     {
112         LOGD("lock screen");
113         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, &value);
114         LOGD("result : %d value: %s", ret, value);
115     }else if (settingType == INCOMMING_CALL)
116     {
117         LOGD("call");
118         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &value);
119         LOGD("result : %d value: %s", ret, value);
120     }
121     else if (settingType == NOTIFICATION_EMAIL)
122     {
123         LOGD("email");
124         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, &value);
125         LOGD("result : %d value: %s", ret, value);
126     }
127
128     if (ret == SYSTEM_SETTINGS_ERROR_NONE)
129     {
130         LOGD("success");
131         gettingValue = value;
132         return value;
133     }
134     else
135     {
136         LOGE("err : %d", ret);
137         throwSystemSettingsException(ret, "system_settings_get_value_string()");
138     }
139 }
140
141 void SystemSetting::OnRequestReceived(const EventSetWallpaperPtr &event)
142 {
143     LOGD("enter");
144     int ret = SYSTEM_SETTINGS_ERROR_NONE;
145
146     std::string wallpaperUri = event->getWallpaperURI();
147     std::string wallpaperType = event->getWallpaperType();
148
149     if (strcmp(wallpaperType.c_str(), "HOME_SCREEN") == 0)
150     {
151         LOGD("home screen : %s", wallpaperUri.c_str());
152         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, wallpaperUri.c_str());
153     }
154     else if (strcmp(wallpaperType.c_str(), "LOCK_SCREEN") == 0)
155     {
156         LOGD("lock screen : %s", wallpaperUri.c_str());
157         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, wallpaperUri.c_str());
158     }
159
160     if (ret == SYSTEM_SETTINGS_ERROR_NONE)
161     {
162         LOGD("success");
163         event->setExceptionCode(ExceptionCodes::None);
164     }
165     else
166     {
167         LOGW("%s", getSystemSettingsLogMessage(ret, "system_settings_set_value_string()").c_str());
168         event->setExceptionCode(ExceptionCodes::PlatformException);
169     }
170     LOGD("end");
171 }
172
173 void SystemSetting::OnRequestReceived(const EventSetRingtonePtr &event)
174 {
175     LOGD("enter");
176     int ret = SYSTEM_SETTINGS_ERROR_NONE;
177
178     std::string ringtoneUri = event->getRingtoneURI();
179     std::string ringtoneType = event->getRingtoneType();
180
181     if (strcmp(ringtoneType.c_str(), "INCOMING_CALL") == 0)
182     {
183         LOGD("call  : %s", ringtoneUri.c_str());
184         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, ringtoneUri.c_str());
185     }
186     else if (strcmp(ringtoneType.c_str(), "NOTIFICATION_EMAIL") == 0)
187     {
188         // TODO need manage-api
189         LOGD("email  : %s", ringtoneUri.c_str());
190         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, ringtoneUri.c_str());
191     }
192
193     if (ret == SYSTEM_SETTINGS_ERROR_NONE)
194     {
195         LOGD("success");
196         event->setExceptionCode(ExceptionCodes::None);
197     }
198     else
199     {
200         LOGW("%s", getSystemSettingsLogMessage(ret, "system_settings_set_value_string()").c_str());
201         event->setExceptionCode(ExceptionCodes::PlatformException);
202     }
203     LOGD("end");
204 }
205
206 void SystemSetting::OnRequestReceived(const EventSetSettingPtr &event)
207 {
208     LOGD("enter");
209     int ret = SYSTEM_SETTINGS_ERROR_NONE;
210
211     std::string settingValue = event->getSettingValue();
212     SettingType settingType = event->getSettingType();
213
214     if (settingType == HOME_SCREEN)
215     {
216         LOGD("home screen : %s", settingValue.c_str());
217         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, settingValue.c_str());
218         LOGD("result : %d", ret);
219     }
220     else if (settingType == LOCK_SCREEN)
221     {
222         LOGD("lock screen : %s", settingValue.c_str());
223         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, settingValue.c_str());
224         LOGD("result : %d", ret);
225     }else if (settingType == INCOMMING_CALL)
226     {
227         LOGD("call  : %s", settingValue.c_str());
228         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, settingValue.c_str());
229         LOGD("result : %d", ret);
230     }
231     else if (settingType == NOTIFICATION_EMAIL)
232     {
233         LOGD("email  : %s", settingValue.c_str());
234         ret = system_settings_set_value_string(SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, settingValue.c_str());
235         LOGD("result : %d", ret);
236     }
237
238     if (ret == SYSTEM_SETTINGS_ERROR_NONE)
239     {
240         LOGD("success");
241         event->setExceptionCode(ExceptionCodes::None);
242     }
243     else
244     {
245         LOGW("%s", getSystemSettingsLogMessage(ret, "system_settings_set_value_string()").c_str());
246         event->setExceptionCode(ExceptionCodes::PlatformException);
247     }
248     LOGD("end");
249 }
250
251 void SystemSetting::OnRequestReceived(const EventGetSettingPtr &event)
252 {
253     LOGD("enter");
254     int ret = SYSTEM_SETTINGS_ERROR_NONE;
255
256     std::string settingValue;
257     SettingType settingType = event->getSettingType();
258     char *value = NULL;
259
260     if (settingType == HOME_SCREEN)
261     {
262         LOGD("home screen");
263         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_HOME_SCREEN, &value);
264         LOGD("result : %d", ret);
265     }
266     else if (settingType == LOCK_SCREEN)
267     {
268         LOGD("lock screen");
269         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_WALLPAPER_LOCK_SCREEN, &value);
270         LOGD("result : %d", ret);
271     }else if (settingType == INCOMMING_CALL)
272     {
273         LOGD("call");
274         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_INCOMING_CALL_RINGTONE, &value);
275         LOGD("result : %d", ret);
276     }
277     else if (settingType == NOTIFICATION_EMAIL)
278     {
279         LOGD("email");
280         ret = system_settings_get_value_string(SYSTEM_SETTINGS_KEY_EMAIL_ALERT_RINGTONE, &value);
281         LOGD("result : %d", ret);
282     }
283
284     if (ret == SYSTEM_SETTINGS_ERROR_NONE)
285     {
286         LOGD("success");
287         event->setExceptionCode(ExceptionCodes::None);
288         settingValue = value;
289         event->setSettingValue(settingValue);
290     }
291     else
292     {
293         LOGW("%s", getSystemSettingsLogMessage(ret, "system_settings_get_value_string()").c_str());
294         event->setExceptionCode(ExceptionCodes::PlatformException);
295     }
296
297     if (value) {
298         free(value);
299     }
300     LOGD("end");
301 }
302
303 }
304 }