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