2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <CommonsJavaScript/JSUtils.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <ArgumentValidator.h>
25 #include <Commons/Exception.h>
26 #include <JSWebAPIErrorFactory.h>
27 #include <SecurityExceptions.h>
29 #include "AlarmAbsolute.h"
30 #include "AlarmConverter.h"
35 #include <TimeTracer.h>
36 #include "JSAlarmAbsolute.h"
37 #include "JSAlarmManager.h"
41 #define ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE "getNextScheduledDate"
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48 using namespace DeviceAPI::Common;
50 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
52 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
54 kJSClassAttributeNone,
55 TIZEN_ALARM_ABSOLUTE_INTERFACE,
64 NULL, //deleteProperty,Geolocation
65 NULL, //getPropertyNames,
72 JSStaticFunction JSAlarmAbsolute::m_function[] = {
73 { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
77 JSStaticValue JSAlarmAbsolute::m_property[] = {
78 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
79 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DATE, getDate, NULL, kJSPropertyAttributeReadOnly },
80 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_PERIOD, getInterval, NULL, kJSPropertyAttributeReadOnly },
81 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DAYSOFTHEWEEK, getDaysOfTheWeek, NULL, kJSPropertyAttributeReadOnly },
85 const JSClassRef DLL_EXPORT JSAlarmAbsolute::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
93 const JSClassDefinition* JSAlarmAbsolute::getClassInfo()
95 return &m_jsClassInfo;
98 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object)
101 void JSAlarmAbsolute::finalize(JSObjectRef object)
103 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
105 LoggerE("Private object is null");
112 JSObjectRef DLL_EXPORT JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
114 ArgumentValidator validator(ctx, argumentCount, arguments);
116 AlarmAbsolutePtr priv = AlarmAbsolutePtr(new AlarmAbsolute());
119 time_t date = validator.toTimeT(0);
120 struct tm *startDate = localtime(&date);
123 priv->setDate(*startDate);
125 if (argumentCount >= 2) {
126 if (JSIsArrayValue(ctx, arguments[1])) {
127 std::vector<std::string> daysOfTheWeek = validator.toStringVector(1);
129 for (size_t i = 0; i < daysOfTheWeek.size(); i++ ) {
130 if ( (daysOfTheWeek[i]!="SU") && (daysOfTheWeek[i]!="MO") &&
131 (daysOfTheWeek[i]!="TU") && (daysOfTheWeek[i]!="WE") &&
132 (daysOfTheWeek[i]!="TH") && (daysOfTheWeek[i]!="FR") &&
133 (daysOfTheWeek[i]!="SA") ) {
134 // remove unacceptable data from vector
135 daysOfTheWeek.erase(std::remove(daysOfTheWeek.begin(), daysOfTheWeek.end(), daysOfTheWeek[i]), daysOfTheWeek.end());
139 if(daysOfTheWeek.size() > 0) {
140 priv->setByDayRecurrence(daysOfTheWeek);
143 long interval = validator.toLong(1);
145 throw InvalidValuesException("period can not be negative value");
148 priv->setInterval(interval);
151 } catch (const BasePlatformException& err) {
152 LoggerE("Exception occured while creating constructor : " << err.getMessage());
155 JSAlarmAbsolutePriv *jspriv = new JSAlarmAbsolutePriv(ctx, priv);
156 JSObjectRef obj = JSObjectMake(ctx, getClassRef(), jspriv);
158 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
159 JSObjectSetProperty(ctx, obj, ctorName, constructor,
160 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
161 JSStringRelease(ctorName);
166 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
168 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
170 throw TypeMismatchException("Private object is null");
172 AlarmAbsolutePtr result = priv->getObject();
174 throw TypeMismatchException("Private object is null");
180 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
182 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
184 throw TypeMismatchException("Private object is null");
186 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
189 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
191 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
194 Converter converter(ctx);
196 AlarmAbsolutePtr privateData = getPrivData(thisObject);
198 throw TypeMismatchException("Private object is null");
201 if(!privateData->is_registered) {
202 return JSValueMakeNull(ctx);
205 int id = privateData->getId();
206 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_scheduled_date", 0);
207 int err = alarm_get_scheduled_date(id, &date);
208 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_scheduled_date", 0);
209 if(err != ALARM_ERROR_NONE) {
210 return JSValueMakeNull(ctx);
213 // check wheter the alarm is expired or not
215 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_current_time", 0);
216 err = alarm_get_current_time(&curr_date);
217 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_current_time", 0);
218 if(err != ALARM_ERROR_NONE) {
219 return JSValueMakeNull(ctx);
222 if (mktime(&date) < mktime(&curr_date)) {
223 return JSValueMakeNull(ctx);
225 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
227 return converter.toJSValueRef(date);
229 } catch (const BasePlatformException &err) {
230 return JSWebAPIErrorFactory::postException(ctx, exception, err);
232 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
233 return JSWebAPIErrorFactory::postException(ctx, exception, err);
237 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
239 JSStringRef propertyName,
240 JSValueRef* exception)
242 Converter converter(ctx);
246 AlarmAbsolutePtr privateData = getPrivData(object);
248 throw TypeMismatchException("Private object is null");
251 date = privateData->getDate();
252 LoggerI("JSAlarmAbsolute Date = " << " Sec : " << date.tm_sec << " Min : "<< date.tm_min
253 << " Hour" << date.tm_hour << "Day : " << date.tm_mday << " MON : " << date.tm_mon
254 << " Year : " << date.tm_year);
257 args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
258 args[1] = JSValueMakeNumber(ctx, date.tm_mon);
259 args[2] = JSValueMakeNumber(ctx, date.tm_mday);
260 args[3] = JSValueMakeNumber(ctx, date.tm_hour);
261 args[4] = JSValueMakeNumber(ctx, date.tm_min);
262 args[5] = JSValueMakeNumber(ctx, date.tm_sec);
264 JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
267 LoggerE("Exception: occured");
270 return JSValueMakeUndefined(ctx);
273 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
275 JSStringRef propertyName,
276 JSValueRef* exception)
279 AlarmAbsolutePtr privateData = getPrivData(object);
281 throw TypeMismatchException("Private object is null");
284 Converter converter(ctx);
285 if (privateData->is_registered) {
286 std::string strId = converter.toString(privateData->getId());
287 return converter.toJSValueRef(strId);
289 return JSValueMakeNull(ctx);
292 LoggerE("Exception: occured");
295 return JSValueMakeUndefined(ctx);
298 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
300 JSStringRef propertyName,
301 JSValueRef* exception)
304 AlarmAbsolutePtr privateData = getPrivData(object);
305 AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
307 if(alarmType == AbsoluteRecurrence::Interval) {
308 long interval = privateData->getInterval();
309 if (interval == -1 ) {
310 return JSValueMakeNull(ctx);
312 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, interval);
315 return JSValueMakeNull(ctx);
318 LoggerI("Exception: occured");
321 return JSValueMakeUndefined(ctx);
324 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
326 JSStringRef propertyName,
327 JSValueRef* exception)
329 Converter converter(ctx);
332 AlarmAbsolutePtr privateData = getPrivData(object);
334 throw TypeMismatchException("Private object is null");
337 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
338 if (jsResult == NULL) {
339 throw UnknownException("Could not create js array object");
342 std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
344 if(daysOfTheWeek.size() > 0) {
345 for(size_t i = 0; i<daysOfTheWeek.size(); i++) {
346 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
347 if(!JSSetArrayElement(ctx, jsResult, i, val)) {
348 throw UnknownException("Could not insert value into js array");
355 LoggerI("Exception: occured");
358 return JSValueMakeUndefined(ctx);