Update change log and spec for wrt-plugins-tizen_0.4.70
[platform/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 <ArgumentValidator.h>
24
25 #include <Commons/Exception.h>
26 #include <JSWebAPIErrorFactory.h>
27 #include <SecurityExceptions.h>
28
29 #include "AlarmRelative.h"
30 #include "AlarmConverter.h"
31 #include "JSAlarmRelative.h"
32 #include "JSAlarmManager.h"
33
34 #include <JSUtil.h>
35
36 #include <TimeTracer.h>
37 #include <app.h>
38 #include <time.h>
39 #include <Export.h>
40 #include <Logger.h>
41
42 #define ALARM_FUNCTION_API_GET_REMAINING_SECONDS "getRemainingSeconds"
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 DLL_EXPORT 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  
111 JSObjectRef DLL_EXPORT JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
112 {
113     ArgumentValidator validator(ctx, argumentCount, arguments);
114
115         AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
116
117     try {
118         long delay = validator.toLong(0);
119         privateData->setDelay(delay);
120
121         long period = validator.toLong(1, true, -1);
122         privateData->setPeriod(period);
123
124         } catch (const BasePlatformException& err) {
125                 LoggerE("Exception occured while creating constructor : " << err.getMessage());
126         }
127
128         JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(ctx, privateData);
129         JSObjectRef obj = JSObjectMake(ctx, getClassRef(), priv);
130
131     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
132     JSObjectSetProperty(ctx, obj, ctorName, constructor,
133         kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
134     JSStringRelease(ctorName);
135
136         return obj;
137 }
138
139 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
140 {
141     JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
142     if (!priv) {
143         throw TypeMismatchException("Private object is null");
144     }
145     AlarmRelativePtr result = priv->getObject();
146     if (!result) {
147         throw TypeMismatchException("Private object is null");
148     }
149     return result;
150 }
151
152 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, AlarmRelativePtr privateData)
153 {
154     JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
155     if (!priv) {
156         throw TypeMismatchException("Private object is null");
157     }
158     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
159 }
160
161
162 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int interval)
163 {
164     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
165     privateData->setDelay(delay);
166     privateData->setPeriod(interval);
167     
168     JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
169     if (!priv) {
170         throw TypeMismatchException("Private object is null");
171     }
172     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
173 }
174
175 JSValueRef JSAlarmRelative::getRemainingSeconds(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
176 {
177     struct tm date;
178     struct tm current;
179     time_t currentTime;
180     time_t nextTime;
181     int id;
182
183     TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
184
185     try {
186         AlarmRelativePtr privateData = getPrivData(thisObject);
187
188         if(!privateData->is_registered) {
189             return JSValueMakeNull(ctx);
190         }
191
192         id = privateData->getId();
193
194         TIME_TRACER_ITEM_BEGIN("(getRemainingSeconds)alarm_get_scheduled_date", 0);
195         int err = alarm_get_scheduled_date(id, &date);
196         TIME_TRACER_ITEM_END("(getRemainingSeconds)alarm_get_scheduled_date", 0);
197         if(err != ALARM_ERROR_NONE)
198         {
199             if(err == ALARM_ERROR_INVALID_PARAMETER) {
200                 return JSValueMakeNull(ctx);
201             } else  {
202                 throw UnknownException("Unknown exception occurred. fail to get scheduled date");
203             }
204         }
205
206         TIME_TRACER_ITEM_BEGIN("(getRemainingSeconds)alarm_get_current_time", 0);
207         alarm_get_current_time(&current);
208
209         nextTime = mktime(&date);
210         currentTime = mktime(&current);
211         TIME_TRACER_ITEM_END("(getRemainingSeconds)alarm_get_current_time", 0);
212
213         long result = nextTime - currentTime;
214
215         LoggerI("nextTime: "<<nextTime<<", currentTime: "<<currentTime<<", result: "<<result);
216
217         if(result < 0) {
218             return JSValueMakeNull(ctx);
219         }
220         TIME_TRACER_ITEM_END(__FUNCTION__, 0);
221
222         return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, result);
223     } catch (const BasePlatformException &err) {
224         return JSWebAPIErrorFactory::postException(ctx, exception, err);
225     } catch (...) {
226         DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
227         return JSWebAPIErrorFactory::postException(ctx, exception, err);
228     }
229 }
230
231 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
232         JSObjectRef object,
233         JSStringRef propertyName,
234         JSValueRef* exception)
235 {
236     Converter converter(ctx);
237
238     try {
239         AlarmRelativePtr privateData = getPrivData(object);
240         if (!privateData) {
241             throw TypeMismatchException("Private object is null");
242         }
243         
244         if(privateData->is_registered) {
245             std::string strId = converter.toString(privateData->getId());
246             return converter.toJSValueRef(strId);
247         } else {
248             return JSValueMakeNull(ctx);            
249         }
250     } catch (const BasePlatformException &err) {
251         return JSWebAPIErrorFactory::postException(ctx, exception, err);
252     } catch (...) {
253         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
254         return JSWebAPIErrorFactory::postException(ctx, exception, err);
255     }
256 }
257
258 JSValueRef JSAlarmRelative::getDelay(JSContextRef ctx,
259         JSObjectRef object,
260         JSStringRef propertyName,
261         JSValueRef* exception)
262 {
263     long delay;
264
265     try {
266         AlarmRelativePtr privateData = getPrivData(object);
267         if (!privateData) {
268             throw TypeMismatchException("Private object is null");
269         }
270         
271         delay = privateData->getDelay();
272         LoggerI("JSAlarmRelative delay = " << delay);
273         return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, delay);
274     } catch (const BasePlatformException &err) {
275         return JSWebAPIErrorFactory::postException(ctx, exception, err);
276     } catch (...) {
277         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
278         return JSWebAPIErrorFactory::postException(ctx, exception, err);
279     }
280 }
281
282 JSValueRef JSAlarmRelative::getPeriod(JSContextRef ctx,
283         JSObjectRef object,
284         JSStringRef propertyName,
285         JSValueRef* exception)
286 {
287     long period =0;
288
289     try {
290         AlarmRelativePtr privateData = getPrivData(object);
291         if (!privateData) {
292             throw TypeMismatchException("Private object is null");
293         }
294         
295         period = privateData->getPeriod();
296         LoggerI("JSAlarmRelative interval = " << period);
297          if(period == -1) {
298             return JSValueMakeNull(ctx);
299         } else {
300             return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, period);
301         }
302     } catch (const BasePlatformException &err) {
303         return JSWebAPIErrorFactory::postException(ctx, exception, err);
304     } catch (...) {
305         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
306         return JSWebAPIErrorFactory::postException(ctx, exception, err);
307     }
308 }
309
310 } // Alarm
311 } // TizenApis
312
313