Update change log and spec for wrt-plugins-tizen_0.2.85
[framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / Alarm / JSAlarmAbsolute.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <Commons/Exception.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/Common/JSTizenException.h>
26 #include <Tizen/Common/SecurityExceptions.h>
27
28 #include "AlarmAbsolute.h"
29 #include "AlarmConverter.h"
30 #include <app.h>
31 #include <time.h>
32
33 #include "plugin_config.h"
34 #include "JSAlarmAbsolute.h"
35 #include "JSAlarmManager.h"
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Alarm {
40
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace TizenApis::Commons;
44 using namespace TizenApis::Api::Alarm;
45
46 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
47
48 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
49                 0,
50                 kJSClassAttributeNone,
51                 TIZEN_ALARM_ABSOLUTE_INTERFACE,
52                 NULL,
53                 m_property,
54                 m_function,
55                 initialize,
56                 finalize,
57                 NULL, //hasProperty,
58                 NULL, //getProperty,
59                 NULL, //setProperty,
60                 NULL, //deleteProperty,Geolocation
61                 NULL, //getPropertyNames,
62                 NULL,
63                 constructor, // constructor
64                 hasInstance,
65                 NULL
66 };
67
68 JSStaticFunction JSAlarmAbsolute::m_function[] = { 
69         { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
70         { 0, 0, 0 }
71 };
72
73 JSStaticValue JSAlarmAbsolute::m_property[] = {
74         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
75         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DATE, getDate, NULL, kJSPropertyAttributeReadOnly },
76         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_PERIOD, getInterval, NULL, kJSPropertyAttributeReadOnly },
77         { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DAYSOFTHEWEEK, getDaysOfTheWeek, NULL, kJSPropertyAttributeReadOnly },
78         { 0, 0, 0, 0 }                                                                                            
79 };
80
81 const JSClassRef JSAlarmAbsolute::getClassRef() 
82 {
83         if (!m_jsClassRef) {
84                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
85         }
86         return m_jsClassRef;
87 }
88
89 const JSClassDefinition* JSAlarmAbsolute::getClassInfo() 
90 {
91         return &m_jsClassInfo;
92 }
93
94 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object) 
95 {
96 }
97 void JSAlarmAbsolute::finalize(JSObjectRef object) 
98 {
99     JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
100     if (!priv) {
101         LogError("Private object is null");
102     }
103     delete priv;
104
105 }
106
107 bool JSAlarmAbsolute::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
108 {
109     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
110 }
111
112 JSObjectRef JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
113 {
114     struct tm date;
115     AlarmConverter converter(ctx);
116     Validator validator(ctx);
117
118     Try {        
119         AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
120
121         if(argumentCount < 1)
122         {
123             ThrowMsg(ConversionException, "Wrong parameter type.");
124         }
125
126         if(!validator.isDate(arguments[0]))
127         {
128             ThrowMsg(ConversionException, "Wrong first parameter type.");
129         }
130
131         date = converter.toDateTm(arguments[0]);
132         privateData->setDate(date);
133
134         if(argumentCount >= 2)
135         {
136             if(JSIsArrayValue(ctx, arguments[1])) {
137                 std::vector<std::string> daysOfTheWeek = converter.toVectorOfStrings(arguments[1]);
138                 if(daysOfTheWeek.size() >0) {
139                     privateData->setByDayRecurrence(daysOfTheWeek);    
140                 }
141             } else {
142                 int interval  = converter.toInt(arguments[1]);
143                 if(interval <0) {
144                                         ThrowMsg(InvalidArgumentException, "Invalid period value.");
145                 }
146                 privateData->setInterval(interval);
147             }
148         }
149
150         return JSValueToObject(ctx, createJSObject(ctx, privateData), exception);
151         } Catch(ConversionException) {
152                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
153         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
154         } Catch(UnsupportedException) {
155                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
156         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
157         } Catch(InvalidArgumentException) {
158                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
159         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
160         } Catch(Exception) {
161                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
162         *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
163         }
164
165     return NULL;
166 }
167
168 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
169 {
170     JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
171     if (!priv) {
172                 ThrowMsg(ConversionException, "Object is null.");
173     }
174     AlarmAbsolutePtr result = priv->getObject();
175     if (!result) {
176                 ThrowMsg(ConversionException, "Private object is null.");
177     }
178     return result;
179 }
180
181 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, const int id)
182 {
183     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());   
184     privateData->setId(id);
185     
186     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
187     if (!priv) {
188                 ThrowMsg(ConversionException, "Object is null.");
189     }
190     return  JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
191 }
192
193 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
194 {
195     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
196     if (!priv) {
197                 ThrowMsg(ConversionException, "Object is null.");
198     }
199     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
200 }
201
202 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date, int interval)
203 {
204     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
205     privateData->setDate(date);
206
207     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
208     if (!priv) {
209                 ThrowMsg(ConversionException, "Object is null.");
210     }
211     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
212 }
213
214 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date)
215 {
216     AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
217     privateData->setDate(date);
218
219     JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
220     if (!priv) {
221                 ThrowMsg(ConversionException, "Object is null.");
222     }
223     return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
224 }
225
226 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
227 {   
228     LogError("GContext = " << TizenApis::Tizen1_0::Alarm::JSAlarmManager::gContext);
229     AceSecurityStatus status = ALARM_CHECK_ACCESS(
230             ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE);
231     TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
232
233     struct tm date;
234     Converter converter(ctx);
235     int id;
236     Try
237     {
238         AlarmAbsolutePtr privateData = getPrivData(thisObject);
239         id = privateData->getId(); 
240         int err = alarm_get_scheduled_date(id, &date);
241         JSValueRef result = converter.toJSValueRef(date);
242
243         if(err != ALARM_ERROR_NONE)
244         {
245             return JSValueMakeNull(ctx);
246         }
247         return result;
248         } Catch(ConversionException) {
249                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
250                 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
251         } Catch(UnsupportedException) {
252                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
253                 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
254         } Catch(InvalidArgumentException) {
255                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
256                 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
257         } Catch(Exception) {
258                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
259                 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
260         }
261 }
262
263 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
264                 JSObjectRef object,
265                 JSStringRef propertyName,
266                 JSValueRef* exception)
267 {
268     Converter converter(ctx);
269     struct tm date;
270     Try
271     {
272         AlarmAbsolutePtr privateData = getPrivData(object);
273         date = privateData->getDate();
274         LogInfo("JSAlarmAbsolute Date  = " << "  Sec : " << date.tm_sec << "  Min : "<< date.tm_min
275         << "  Hour" << date.tm_hour << "Day : " << date.tm_mday << "  MON : " << date.tm_mon 
276         << "  Year : " <<  date.tm_year);
277
278         JSValueRef args[6];
279         args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
280         args[1] = JSValueMakeNumber(ctx, date.tm_mon);
281         args[2] = JSValueMakeNumber(ctx, date.tm_mday);
282         args[3] = JSValueMakeNumber(ctx, date.tm_hour);
283         args[4] = JSValueMakeNumber(ctx, date.tm_min);
284         args[5] = JSValueMakeNumber(ctx, date.tm_sec);
285
286         JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
287         return result;
288     }
289         Catch(Exception)
290         {
291                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
292         }
293         return JSValueMakeUndefined(ctx);
294 }
295
296 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
297                 JSObjectRef object,
298                 JSStringRef propertyName,
299                 JSValueRef* exception)
300 {
301     Converter converter(ctx);
302     int id;
303     Try
304     {
305         AlarmAbsolutePtr privateData = getPrivData(object);
306         Converter converter(ctx);
307         id = privateData->getId();
308         if(id >= 0) {
309             std::string strId = converter.toString(id);
310             return converter.toJSValueRef(strId);
311         } else {
312             return JSValueMakeNull(ctx);
313         }
314         }
315         Catch(Exception)
316         {
317                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
318         }
319         return JSValueMakeUndefined(ctx);
320 }
321
322 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
323                 JSObjectRef object,
324                 JSStringRef propertyName,
325                 JSValueRef* exception)
326 {
327     Converter converter(ctx);
328     int interval;
329      Try
330     {
331         AlarmAbsolutePtr privateData = getPrivData(object);
332         AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
333        
334         if(alarmType == AbsoluteRecurrence::Interval) {
335             interval = privateData->getInterval();
336             return converter.toJSValueRef(interval);
337         } else {
338             return JSValueMakeNull(ctx);    
339         }
340     }
341         Catch(Exception)
342         {
343                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
344         }
345         return JSValueMakeUndefined(ctx);
346 }
347
348 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
349                 JSObjectRef object,
350                 JSStringRef propertyName,
351                 JSValueRef* exception)
352 {
353     Converter converter(ctx);
354     
355     Try {
356         JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
357         AlarmAbsolutePtr privateData = getPrivData(object);
358         std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
359
360         if(daysOfTheWeek.size() >0)
361         {
362             if (jsResult == NULL) 
363             {
364                 ThrowMsg(NullPointerException, "Could not create js array object");
365             }
366
367             for(unsigned int i=0; i<daysOfTheWeek.size(); i++) 
368             {
369                 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
370                 if(!JSSetArrayElement(ctx, jsResult, i, val)) 
371                 {
372                     ThrowMsg(UnknownException, "Could not insert value into js array");
373                 }
374             }
375         }
376
377         return jsResult;
378         }
379         Catch(Exception)
380         {
381                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
382         }
383         return JSValueMakeUndefined(ctx);
384 }
385
386 } // Alarm
387 } // Tizen1_0 
388 } // TizenApis
389
390