wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / SystemSetting / JSSystemSetting.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 <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/ScopedJSStringRef.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "SystemSettingFactory.h"
25 #include "ISystemSetting.h"
26 #include "EventSetWallpaper.h"
27 #include "EventSetRingtone.h"
28 #include "EventSetSetting.h"
29 #include "EventGetSetting.h"
30 #include "SystemSettingFactory.h"
31
32 #include <JSTizenExceptionFactory.h>
33 #include <JSTizenException.h>
34 #include <SecurityExceptions.h>
35 #include <TimeTracer.h>
36 #include "JSSystemSetting.h"
37 #include "SystemSettingResponseDispatcher.h"
38
39 #include "ConverterSystemSetting.h"
40 #include "SystemSettingAsyncCallbackManager.h"
41
42 //#include <JSFile.h>
43 //#include <EventGetNodeData.h>
44 #include <FilesystemUtils.h>
45 #include "plugin_config.h"
46 #include <Logger.h>
47
48 using namespace std;
49 using namespace DPL;
50 using namespace WrtDeviceApis::CommonsJavaScript;
51 using namespace WrtDeviceApis::Commons;
52 using namespace DeviceAPI::Common;
53
54 namespace DeviceAPI {
55 namespace SystemSetting {
56
57 JSClassDefinition JSSystemSetting::m_classInfo = {
58     0,
59     kJSClassAttributeNone,
60     "systemsetting",
61     NULL,
62     NULL,
63     m_function,
64     initialize,
65     finalize,
66     NULL,
67     NULL,
68     NULL,
69     NULL,
70     NULL,
71     NULL,
72     NULL,
73     NULL,
74     NULL
75 };
76
77 JSStaticFunction JSSystemSetting::m_function[] = {
78 //    { "setWallpaper", JSSystemSetting::setWallpaper, kJSPropertyAttributeNone },
79 //    { "setRingtone", JSSystemSetting::setRingtone, kJSPropertyAttributeNone },
80     { "setProperty", JSSystemSetting::setProperty, kJSPropertyAttributeNone },
81     { "getProperty", JSSystemSetting::getProperty, kJSPropertyAttributeNone },
82     { 0, 0, 0 }
83 };
84
85 const JSClassRef JSSystemSetting::getClassRef()
86 {
87     if (!m_jsClassRef) {
88         m_jsClassRef = JSClassCreate(&m_classInfo);
89     }
90     return m_jsClassRef;
91 }
92
93 const JSClassDefinition* JSSystemSetting::getClassInfo()
94 {
95     return &m_classInfo;
96 }
97
98 JSClassRef JSSystemSetting::m_jsClassRef = JSClassCreate(JSSystemSetting::getClassInfo());
99
100 void JSSystemSetting::initialize(JSContextRef context, JSObjectRef object)
101 {
102     JSSystemSettingPriv* priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(object));
103     ISystemSettingPtr SystemSettings(SystemSettingFactory::getInstance().getSystemSetting());
104
105         if (priv != NULL)
106         {
107             LoggerD("JSSystemSetting::initialize already");
108                 return;
109         }
110
111         Try {
112                 priv = new JSSystemSettingPriv(context, SystemSettings);
113                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
114                         LoggerE("Object can't store private data.");
115                         delete priv;
116                 }
117         } Catch (UnsupportedException) {
118                 LoggerE("UnsupportedException: " << _rethrown_exception.GetMessage());
119         } Catch (UnknownException) {
120                 LoggerE("UnknownExceptionException: " << _rethrown_exception.GetMessage());
121         } Catch (PlatformException) {
122                 LoggerE("PlatformExceptionException: " << _rethrown_exception.GetMessage());
123         } Catch (WrtDeviceApis::Commons::Exception) {
124                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
125         }
126
127     LoggerD("JSSystemSetting::initialize ");
128         
129 }
130
131 void JSSystemSetting::finalize(JSObjectRef object)
132 {
133     JSSystemSettingPriv* priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(object));
134     
135     JSObjectSetPrivate(object, NULL);
136     LoggerD("Deleting gallery");
137     delete priv;
138 }
139
140 JSValueRef JSSystemSetting::setProperty(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
141         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
142 {
143         LoggerD("enter");
144
145         SettingType settingType;
146         std::string settingValue;
147         
148         JSSystemSettingPriv *priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(thisObject));
149         ConverterSystemSettingFactory::ConverterType converter = ConverterSystemSettingFactory::getConverter(context);
150         Validator check(context, exception);
151         
152         if (!priv) {
153                 LoggerE("private object is null");
154                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
155         }
156         if (argumentCount < 3) {
157                 LoggerD("Argument is too few");
158                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
159         }
160         if (!check.isCallback(arguments[2])) {
161                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
162         }
163
164         AceSecurityStatus status = SYSTEMSETTING_CHECK_ACCESS(SYSTEMSETTING_FUNCTION_API_SET_PROPERTY);
165         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
166
167         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
168         onSuccessForCbm = arguments[2];
169         if (argumentCount > 3) {
170                 if (check.isCallback(arguments[3])) {
171                         onErrorForCbm = arguments[3];
172                 } else if (!JSValueIsNull(context, arguments[3])) {
173                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
174                 }
175         }
176
177         JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
178         callbackManager->setObject(thisObject);
179
180         std::string strSettingType;
181         Try {
182                 strSettingType = converter->toString(arguments[0]);
183                 TIME_TRACER_ITEM_BEGIN(strSettingType.c_str(), 0);
184
185                 settingType = converter->toSettingType(arguments[0]);
186                 settingValue = converter->toString(arguments[1]);
187                 Try{
188                         DeviceAPI::Filesystem::IPathPtr src = DeviceAPI::Filesystem::Utils::fromVirtualPath(priv->getContext(), settingValue);
189                         if (src)
190                                 settingValue = src->getFullPath();
191                         else
192                                 settingValue = "";      //set empty string.
193
194                         LoggerD("real path = " << settingValue);
195                 }
196                 Catch(WrtDeviceApis::Commons::Exception) {
197                         LoggerD("settingValue is not Virtual Path");
198                 }
199                 
200                 LoggerD("settingValue = " << settingValue);
201                 
202                 ISystemSettingPtr SystemSettings(priv->getObject());
203                 EventSetSettingPtr dplEvent(new EventSetSetting());
204                 dplEvent->setSettingType(settingType);
205                 dplEvent->setSettingValue(settingValue);
206                 dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
207                 dplEvent->setForAsynchronousCall(&SystemSettingResponseDispatcher::getInstance());
208            
209                 SystemSettings->setProperty(dplEvent);
210                 SystemSettingAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, priv->getContext());
211         }
212
213         Catch(WrtDeviceApis::Commons::ConversionException) {
214                 LoggerD("Exception");
215                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
216         }
217
218         Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
219                 LoggerD("Exception");
220                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
221         }
222
223         Catch(WrtDeviceApis::Commons::PlatformException) {
224                 LoggerD("Exception");
225                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "platform error");
226         }
227
228         Catch(WrtDeviceApis::Commons::Exception) {
229                 LoggerD("Exception");
230                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
231         }
232
233         LoggerD("end");
234         TIME_TRACER_ITEM_END(strSettingType.c_str(), 0);
235         return JSValueMakeUndefined(context);
236 }
237
238 JSValueRef JSSystemSetting::getProperty(JSContextRef context, JSObjectRef object, JSObjectRef thisObject,
239         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
240 {
241         LoggerD("enter");
242         JSSystemSettingPriv *priv = static_cast<JSSystemSettingPriv*>(JSObjectGetPrivate(thisObject));
243
244         ConverterSystemSettingFactory::ConverterType converter = ConverterSystemSettingFactory::getConverter(context);
245
246         Validator check(context, exception);
247         
248         if (!priv) {
249                 LoggerE("private object is null");
250                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Wrong Object");
251         }
252
253         if (argumentCount < 2) {
254                 LoggerD("Argument is too few");
255                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
256         }
257         if (!check.isCallback(arguments[1])) {
258                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
259         }
260
261 /*
262         AceSecurityStatus status = SYSTEMSETTING_CHECK_ACCESS(SYSTEMSETTING_FUNCTION_API_GET_PROPERTY);
263         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
264 */
265
266         JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
267         onSuccessForCbm = arguments[1];
268         if (argumentCount > 2) {
269                 if (check.isCallback(arguments[2])) {
270                         onErrorForCbm = arguments[2];
271                 } else if (!JSValueIsNull(context, arguments[2])) {
272                         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
273                 }
274         }
275
276         JSCallbackManagerPtr callbackManager(JSCallbackManager::createObject(priv->getContext(), onSuccessForCbm, onErrorForCbm, true, true));
277         callbackManager->setObject(thisObject);
278
279         std::string strGettingType;
280         Try {
281                 strGettingType = converter->toString(arguments[0]);
282                 TIME_TRACER_ITEM_BEGIN(strGettingType.c_str(), 0);
283                 SettingType settingType = converter->toSettingType(arguments[0]);
284
285                 ISystemSettingPtr SystemSettings(priv->getObject());
286                 EventGetSettingPtr dplEvent(new EventGetSetting());
287                 dplEvent->setSettingType(settingType);
288                 dplEvent->setPrivateData(StaticPointerCast<IEventPrivateData>(callbackManager));
289                 dplEvent->setForAsynchronousCall(&SystemSettingResponseDispatcher::getInstance());
290            
291                 SystemSettings->getProperty(dplEvent);
292                 SystemSettingAsyncCallbackManagerSingleton::Instance().registerCallbackManager(callbackManager, priv->getContext());
293
294         }
295
296         Catch(WrtDeviceApis::Commons::ConversionException) {
297                 LoggerD("Exception");
298                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error");
299         }
300
301         Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
302                 LoggerD("Exception");
303                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value error");
304         }
305
306         Catch(WrtDeviceApis::Commons::PlatformException) {
307                 LoggerD("Exception");
308                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "platform error");
309         }
310
311         Catch(WrtDeviceApis::Commons::Exception) {
312                 LoggerD("Exception");
313                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown error");
314         }
315
316         LoggerD("end");
317         TIME_TRACER_ITEM_BEGIN(strGettingType.c_str(), 0);
318         return JSValueMakeUndefined(context);
319 }
320
321
322 }
323 }