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