wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Alarm / JSAlarmRelative.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/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22
23 #include <JSWebAPIException.h>
24 #include <ArgumentValidator.h>
25
26 #include <Commons/Exception.h>
27 #include <JSTizenExceptionFactory.h>
28 #include <JSTizenException.h>
29 #include <SecurityExceptions.h>
30
31 #include "plugin_config.h"
32
33 #include "AlarmRelative.h"
34 #include "AlarmConverter.h"
35 #include "JSAlarmRelative.h"
36 #include "JSAlarmManager.h"
37
38 #include <JSUtil.h>
39
40 #include <app.h>
41 #include <time.h>
42 #include <Logger.h>
43
44 namespace DeviceAPI {
45 namespace Alarm {
46
47 using namespace WrtDeviceApis::Commons;
48 using namespace WrtDeviceApis::CommonsJavaScript;
49 using namespace DeviceAPI::Common;
50
51 JSClassRef JSAlarmRelative::m_jsClassRef = NULL;
52
53 JSClassDefinition JSAlarmRelative::m_jsClassInfo = {
54         0,
55         kJSClassAttributeNone,
56         TIZEN_ALARM_RELATIVE_INTERFACE,
57         NULL,
58         m_property,
59         m_function,
60         initialize,
61         finalize,
62         NULL, //hasProperty,
63         NULL, //getProperty,
64         NULL, //setProperty,
65         NULL, //deleteProperty,Geolocation
66         NULL, //getPropertyNames,
67         NULL,
68         NULL, // constructor
69         NULL,
70         NULL
71 };
72
73 JSStaticFunction JSAlarmRelative::m_function[] = { 
74         { ALARM_FUNCTION_API_GET_REMAINING_SECONDS, JSAlarmRelative::getRemainingSeconds, kJSPropertyAttributeNone },
75         { 0, 0, 0 }
76 };
77
78 JSStaticValue JSAlarmRelative::m_property[] = {
79         { TIZEN_ALARM_RELATIVE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
80         { TIZEN_ALARM_RELATIVE_ATTRIBUTE_DELAY, getDelay, NULL, kJSPropertyAttributeReadOnly },
81         { TIZEN_ALARM_RELATIVE_ATTRIBUTE_PERIOD, getPeriod, NULL, kJSPropertyAttributeReadOnly },
82         { 0, 0, 0, 0 }                                                                                            
83 };
84
85 const JSClassRef JSAlarmRelative::getClassRef() 
86 {
87         if (!m_jsClassRef) {
88                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
89         }
90         return m_jsClassRef;
91 }
92
93 const JSClassDefinition* JSAlarmRelative::getClassInfo() 
94 {
95         return &m_jsClassInfo;
96 }
97
98 void JSAlarmRelative::initialize(JSContextRef context, JSObjectRef object) 
99 {
100 }
101 void JSAlarmRelative::finalize(JSObjectRef object) 
102 {
103     JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
104     if (!priv) {
105         LoggerE("Private object is null");
106     }
107     delete priv;
108 }
109
110 bool JSAlarmRelative::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
111 {
112     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
113 }
114
115 JSObjectRef JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
116 {
117         try {
118             ArgumentValidator validator(ctx, argumentCount, arguments);
119
120                 long delay = validator.toLong(0);
121                 if (delay < 0) {
122                         throw InvalidValuesException("delay cannot be negative value");
123                 }
124                 long period = validator.toLong(1, true, 0);
125                 if (period < 0) {
126                         throw InvalidValuesException("period cannot be negative value");
127                 }
128
129                 if ((argumentCount > 1) && !JSValueIsNull(ctx, arguments[1])) {
130                         return JSValueToObject(ctx, createJSObject(ctx, delay, period), exception);
131                 } else {
132                         return JSValueToObject(ctx, createJSObject(ctx, delay, -1), exception);
133                 }
134         } catch (const BasePlatformException& err) {
135                 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(ctx, err);
136                 *exception = exceptionObj;
137                 return exceptionObj;
138     }
139
140 }
141
142 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
143 {
144     JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
145     if (!priv) {
146                 throw TypeMismatchException("Private object is null");
147     }
148     AlarmRelativePtr result = priv->getObject();
149     if (!result) {
150                 throw TypeMismatchException("Private object is null");
151     }
152     return result;
153 }
154
155 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, AlarmRelativePtr privateData)
156 {
157     JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
158     if (!priv) {
159                 throw TypeMismatchException("Private object is null");
160     }
161     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
162 }
163
164
165 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int interval)
166 {
167     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
168     privateData->setDelay(delay);
169     privateData->setPeriod(interval);
170     
171     JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
172     if (!priv) {
173                 throw TypeMismatchException("Private object is null");
174     }
175     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
176 }
177
178 JSValueRef JSAlarmRelative::getRemainingSeconds(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
179 {
180     struct tm date;
181     struct tm current;
182     time_t currentTime;
183     time_t nextTime;
184     int id;
185
186     try {
187         AlarmRelativePtr privateData = getPrivData(thisObject);
188         id = privateData->getId();
189
190         int err = alarm_get_scheduled_date(id, &date);
191         if(err != ALARM_ERROR_NONE)
192         {
193             if(err == ALARM_ERROR_INVALID_PARAMETER) {
194                 return JSValueMakeNull(ctx);
195             } else  {
196                 throw UnknownException("Unknown exception occurred.");
197             }
198         }
199
200         alarm_get_current_time(&current);
201
202         nextTime = mktime(&date);
203         currentTime = mktime(&current);
204
205         long result = nextTime - currentTime;
206
207         LoggerD("nextTime: "<<nextTime<<", currentTime: "<<currentTime<<", result: "<<result);
208
209         if(result < 0) {
210             // It is impossible
211             throw UnknownException("Unknown exception occurred.");
212         }
213
214                 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, result);
215         } catch (const BasePlatformException &err) {
216         return JSWebAPIException::throwException(ctx, exception, err);
217     } catch (...) {
218         DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
219         return JSWebAPIException::throwException(ctx, exception, err);
220     }
221 }
222
223 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
224                 JSObjectRef object,
225                 JSStringRef propertyName,
226                 JSValueRef* exception)
227 {
228     Converter converter(ctx);
229     int id;
230     try {
231         AlarmRelativePtr privateData = getPrivData(object);
232                 if (!privateData) {
233                         throw TypeMismatchException("Private object is null");
234             }
235                 
236         id = privateData->getId();
237         if(id >= 0) {
238             std::string strId = converter.toString(id);
239             return converter.toJSValueRef(strId);
240         } else {
241             return JSValueMakeNull(ctx);            
242         }
243     } catch (const BasePlatformException &err) {
244         return JSWebAPIException::throwException(ctx, exception, err);
245     } catch (...) {
246         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
247         return JSWebAPIException::throwException(ctx, exception, err);
248     }
249 }
250
251 JSValueRef JSAlarmRelative::getDelay(JSContextRef ctx,
252                 JSObjectRef object,
253                 JSStringRef propertyName,
254                 JSValueRef* exception)
255 {
256     long delay;
257
258         try {
259         AlarmRelativePtr privateData = getPrivData(object);
260                 if (!privateData) {
261                         throw TypeMismatchException("Private object is null");
262             }
263                 
264         delay = privateData->getDelay();
265         LoggerI("JSAlarmRelative delay = " << delay);
266         return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, delay);
267     } catch (const BasePlatformException &err) {
268         return JSWebAPIException::throwException(ctx, exception, err);
269     } catch (...) {
270         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
271         return JSWebAPIException::throwException(ctx, exception, err);
272     }
273 }
274
275 JSValueRef JSAlarmRelative::getPeriod(JSContextRef ctx,
276                 JSObjectRef object,
277                 JSStringRef propertyName,
278                 JSValueRef* exception)
279 {
280     long period =0;
281
282         try {
283         AlarmRelativePtr privateData = getPrivData(object);
284                 if (!privateData) {
285                         throw TypeMismatchException("Private object is null");
286             }
287                 
288         period = privateData->getPeriod();
289         LoggerI("JSAlarmRelative interval = " << period);
290          if(period == -1) {
291             return JSValueMakeNull(ctx);
292         } else {
293                 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, period);
294         }
295         } catch (const BasePlatformException &err) {
296                 return JSWebAPIException::throwException(ctx, exception, err);
297         } catch (...) {
298                 DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
299                 return JSWebAPIException::throwException(ctx, exception, err);
300         }
301 }
302
303 } // Alarm
304 } // TizenApis
305
306