e5618bd95708db0e0a0403ce4ed24b2aed9f06fd
[platform/framework/web/wrt-plugins-tizen.git] / src / Alarm / JSAlarmAbsolute.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 "AlarmAbsolute.h"
32 #include "AlarmConverter.h"
33 #include <app.h>
34 #include <time.h>
35 #include <JSUtil.h>
36
37 #include "plugin_config.h"
38 #include "JSAlarmAbsolute.h"
39 #include "JSAlarmManager.h"
40 #include <Logger.h>
41
42 namespace DeviceAPI {
43 namespace Alarm {
44
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47 using namespace DeviceAPI::Common;
48
49 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
50
51 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
52                 0,
53                 kJSClassAttributeNone,
54                 TIZEN_ALARM_ABSOLUTE_INTERFACE,
55                 NULL,
56                 m_property,
57                 m_function,
58                 initialize,
59                 finalize,
60                 NULL, //hasProperty,
61                 NULL, //getProperty,
62                 NULL, //setProperty,
63                 NULL, //deleteProperty,Geolocation
64                 NULL, //getPropertyNames,
65                 NULL,
66                 NULL, // constructor
67                 NULL,
68                 NULL
69 };
70
71 JSStaticFunction JSAlarmAbsolute::m_function[] = { 
72         { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
73         { 0, 0, 0 }
74 };
75
76 JSStaticValue JSAlarmAbsolute::m_property[] = {
77         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
78         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DATE, getDate, NULL, kJSPropertyAttributeReadOnly },
79         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_PERIOD, getInterval, NULL, kJSPropertyAttributeReadOnly },
80         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DAYSOFTHEWEEK, getDaysOfTheWeek, NULL, kJSPropertyAttributeReadOnly },
81         { 0, 0, 0, 0 }                                                                                            
82 };
83
84 const JSClassRef JSAlarmAbsolute::getClassRef() 
85 {
86         if (!m_jsClassRef) {
87                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
88         }
89         return m_jsClassRef;
90 }
91
92 const JSClassDefinition* JSAlarmAbsolute::getClassInfo() 
93 {
94         return &m_jsClassInfo;
95 }
96
97 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object) 
98 {
99 }
100 void JSAlarmAbsolute::finalize(JSObjectRef object) 
101 {
102     JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
103     if (!priv) {
104         LoggerE("Private object is null");
105     }
106     delete priv;
107
108 }
109
110 bool JSAlarmAbsolute::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
111 {
112     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
113 }
114
115 JSObjectRef JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
116 {
117     try {
118         AlarmAbsolutePtr priv = AlarmAbsolutePtr(new AlarmAbsolute());
119                 if (!priv) {
120                         throw TypeMismatchException("Private object is null");
121                 }
122
123                 ArgumentValidator validator(ctx, argumentCount, arguments);     
124                 time_t date = validator.toTimeT(0);
125                 struct tm *startDate = localtime(&date);
126                 mktime(startDate);
127                 
128                 priv->setDate(*startDate);
129
130                 if (argumentCount >= 2) {
131                         if (JSIsArrayValue(ctx, arguments[1])) {
132                                 std::vector<std::string> daysOfTheWeek =  validator.toStringVector(1);
133
134                 for (int i = 0; i < daysOfTheWeek.size(); i++ ) {
135                     if ( (daysOfTheWeek[i]!="SU") && (daysOfTheWeek[i]!="MO") &&
136                          (daysOfTheWeek[i]!="TU") && (daysOfTheWeek[i]!="WE") &&
137                          (daysOfTheWeek[i]!="TH") && (daysOfTheWeek[i]!="FR") &&
138                          (daysOfTheWeek[i]!="SA") ) {
139                         throw TypeMismatchException("Invalid days of the week value.");
140                     }
141                 }
142
143                 if(daysOfTheWeek.size() > 0) {
144                     priv->setByDayRecurrence(daysOfTheWeek);    
145                 }
146                         } else {
147                                 long interval = validator.toLong(1);
148                                 if (interval < 0) {
149                                         throw InvalidValuesException("period can not be negative value");
150                                 }
151
152                                 if (!JSValueIsNull(ctx, arguments[1])) {
153                                         priv->setInterval(interval);
154                                 } else {
155                                         priv->setInterval(-1);
156                                 }
157                         }
158                 }
159
160                 return JSValueToObject(ctx, createJSObject(ctx, priv), exception);
161
162     } catch (const BasePlatformException& err) {
163                 JSObjectRef exceptionObj = JSWebAPIException::makeJSWebAPIException(ctx, err);
164                 *exception = exceptionObj;
165                 return exceptionObj;
166     }
167 }
168
169 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
170 {
171     JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
172     if (!priv) {
173                 throw TypeMismatchException("Private object is null");
174     }
175     AlarmAbsolutePtr result = priv->getObject();
176     if (!result) {
177                 throw TypeMismatchException("Private object is null");
178     }
179     return result;
180 }
181
182 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, const int id)
183 {
184     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());   
185     privateData->setId(id);
186     
187     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
188     if (!priv) {
189                 throw TypeMismatchException("Private object is null");
190     }
191     return  JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
192 }
193
194 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
195 {
196     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
197     if (!priv) {
198                 throw TypeMismatchException("Private object is null");
199     }
200     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
201 }
202
203 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date, int interval)
204 {
205     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
206     privateData->setDate(date);
207
208     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
209     if (!priv) {
210                 throw TypeMismatchException("Private object is null");
211     }
212     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
213 }
214
215 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date)
216 {
217     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
218     privateData->setDate(date);
219
220     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
221     if (!priv) {
222                 throw TypeMismatchException("Private object is null");
223     }
224     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
225 }
226
227 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
228 {   
229         try {
230             struct tm date;
231             Converter converter(ctx);
232                 
233         AlarmAbsolutePtr privateData = getPrivData(thisObject);
234                 if (!privateData) {
235                         throw TypeMismatchException("Private object is null");
236             }
237                 
238         int id = privateData->getId(); 
239         int err = alarm_get_scheduled_date(id, &date);
240         JSValueRef result = converter.toJSValueRef(date);
241
242         if(err != ALARM_ERROR_NONE) {
243             return JSValueMakeNull(ctx);
244         }
245         return result;
246                 
247         } catch (const BasePlatformException &err) {
248         return JSWebAPIException::throwException(ctx, exception, err);
249     } catch (...) {
250         DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
251         return JSWebAPIException::throwException(ctx, exception, err);
252     }
253 }
254
255 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
256                 JSObjectRef object,
257                 JSStringRef propertyName,
258                 JSValueRef* exception)
259 {
260     Converter converter(ctx);
261     struct tm date;
262
263         try {
264         AlarmAbsolutePtr privateData = getPrivData(object);
265                 if (!privateData) {
266                         throw TypeMismatchException("Private object is null");
267             }
268                 
269         date = privateData->getDate();
270         LoggerI("JSAlarmAbsolute Date  = " << "  Sec : " << date.tm_sec << "  Min : "<< date.tm_min
271         << "  Hour" << date.tm_hour << "Day : " << date.tm_mday << "  MON : " << date.tm_mon 
272         << "  Year : " <<  date.tm_year);
273
274         JSValueRef args[6];
275         args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
276         args[1] = JSValueMakeNumber(ctx, date.tm_mon);
277         args[2] = JSValueMakeNumber(ctx, date.tm_mday);
278         args[3] = JSValueMakeNumber(ctx, date.tm_hour);
279         args[4] = JSValueMakeNumber(ctx, date.tm_min);
280         args[5] = JSValueMakeNumber(ctx, date.tm_sec);
281
282         JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
283         return result;
284     } catch (...) {
285         LoggerE("Exception: occured");
286     }
287
288         return JSValueMakeUndefined(ctx);
289 }
290
291 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
292                 JSObjectRef object,
293                 JSStringRef propertyName,
294                 JSValueRef* exception)
295 {
296         try {
297         AlarmAbsolutePtr privateData = getPrivData(object);
298                 if (!privateData) {
299                         throw TypeMismatchException("Private object is null");
300             }
301                 
302         Converter converter(ctx);
303         int id = privateData->getId();
304         if(id >= 0) {
305             std::string strId = converter.toString(id);
306             return converter.toJSValueRef(strId);
307         } else {
308             return JSValueMakeNull(ctx);
309         }
310         } catch (...) {
311         LoggerE("Exception: occured");
312     }
313
314         return JSValueMakeUndefined(ctx);
315 }
316
317 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
318                 JSObjectRef object,
319                 JSStringRef propertyName,
320                 JSValueRef* exception)
321 {
322         try {
323         AlarmAbsolutePtr privateData = getPrivData(object);
324         AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
325        
326         if(alarmType == AbsoluteRecurrence::Interval) {
327             long interval = privateData->getInterval();
328                         if (interval == -1 ) {
329                                 return JSValueMakeNull(ctx);
330                         } else {
331                     return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, interval);
332                         }
333         } else {
334             return JSValueMakeNull(ctx);    
335         }
336     } catch (...) {
337         LoggerI("Exception: occured");
338     }
339
340         return JSValueMakeUndefined(ctx);
341 }
342
343 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
344                 JSObjectRef object,
345                 JSStringRef propertyName,
346                 JSValueRef* exception)
347 {
348     Converter converter(ctx);
349     
350     try {
351         AlarmAbsolutePtr privateData = getPrivData(object);
352                 if (!privateData) {
353                         throw TypeMismatchException("Private object is null");
354             }
355
356         JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
357                 if (jsResult == NULL) {
358                         throw UnknownException("Could not create js array object");
359         }
360                 
361         std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
362
363         if(daysOfTheWeek.size() > 0) {
364             for(size_t i = 0; i<daysOfTheWeek.size(); i++) {
365                 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
366                 if(!JSSetArrayElement(ctx, jsResult, i, val)) {
367                                         throw UnknownException("Could not insert value into js array");
368                 }
369             }
370         }
371
372         return jsResult;
373         } catch (...) {
374         LoggerI("Exception: occured");
375     }
376
377         return JSValueMakeUndefined(ctx);
378 }
379
380 } // Alarm
381 } // TizenApis
382
383