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 "plugin_config.h"
37 #include "JSAlarmAbsolute.h"
38 #include "JSAlarmManager.h"
45 using namespace WrtDeviceApis::Commons;
46 using namespace WrtDeviceApis::CommonsJavaScript;
47 using namespace DeviceAPI::Common;
49 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
51 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
53 kJSClassAttributeNone,
54 TIZEN_ALARM_ABSOLUTE_INTERFACE,
63 NULL, //deleteProperty,Geolocation
64 NULL, //getPropertyNames,
71 JSStaticFunction JSAlarmAbsolute::m_function[] = {
72 { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
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 },
84 const JSClassRef DLL_EXPORT JSAlarmAbsolute::getClassRef()
87 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
92 const JSClassDefinition* JSAlarmAbsolute::getClassInfo()
94 return &m_jsClassInfo;
97 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object)
100 void JSAlarmAbsolute::finalize(JSObjectRef object)
102 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
104 LoggerE("Private object is null");
111 JSObjectRef DLL_EXPORT JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
113 ArgumentValidator validator(ctx, argumentCount, arguments);
115 AlarmAbsolutePtr priv = AlarmAbsolutePtr(new AlarmAbsolute());
118 time_t date = validator.toTimeT(0);
119 struct tm *startDate = localtime(&date);
122 priv->setDate(*startDate);
124 if (argumentCount >= 2) {
125 if (JSIsArrayValue(ctx, arguments[1])) {
126 std::vector<std::string> daysOfTheWeek = validator.toStringVector(1);
128 for (int i = 0; i < daysOfTheWeek.size(); i++ ) {
129 if ( (daysOfTheWeek[i]!="SU") && (daysOfTheWeek[i]!="MO") &&
130 (daysOfTheWeek[i]!="TU") && (daysOfTheWeek[i]!="WE") &&
131 (daysOfTheWeek[i]!="TH") && (daysOfTheWeek[i]!="FR") &&
132 (daysOfTheWeek[i]!="SA") ) {
133 throw TypeMismatchException("Invalid days of the week value.");
137 if(daysOfTheWeek.size() > 0) {
138 priv->setByDayRecurrence(daysOfTheWeek);
141 long interval = validator.toLong(1);
143 throw InvalidValuesException("period can not be negative value");
146 priv->setInterval(interval);
149 } catch (const BasePlatformException& err) {
150 LoggerE("Exception occured while creating constructor : " << err.getMessage());
153 JSAlarmAbsolutePriv *jspriv = new JSAlarmAbsolutePriv(ctx, priv);
154 JSObjectRef obj = JSObjectMake(ctx, getClassRef(), jspriv);
156 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
157 JSObjectSetProperty(ctx, obj, ctorName, constructor,
158 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
159 JSStringRelease(ctorName);
164 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
166 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
168 throw TypeMismatchException("Private object is null");
170 AlarmAbsolutePtr result = priv->getObject();
172 throw TypeMismatchException("Private object is null");
178 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
180 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
182 throw TypeMismatchException("Private object is null");
184 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
187 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
189 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
192 Converter converter(ctx);
194 AlarmAbsolutePtr privateData = getPrivData(thisObject);
196 throw TypeMismatchException("Private object is null");
199 if(!privateData->is_registered) {
200 return JSValueMakeNull(ctx);
203 int id = privateData->getId();
204 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_scheduled_date", 0);
205 int err = alarm_get_scheduled_date(id, &date);
206 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_scheduled_date", 0);
207 if(err != ALARM_ERROR_NONE) {
208 return JSValueMakeNull(ctx);
211 // check wheter the alarm is expired or not
213 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_current_time", 0);
214 err = alarm_get_current_time(&curr_date);
215 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_current_time", 0);
216 if(err != ALARM_ERROR_NONE) {
217 return JSValueMakeNull(ctx);
220 if (mktime(&date) < mktime(&curr_date)) {
221 return JSValueMakeNull(ctx);
223 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
225 return converter.toJSValueRef(date);
227 } catch (const BasePlatformException &err) {
228 return JSWebAPIErrorFactory::postException(ctx, exception, err);
230 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
231 return JSWebAPIErrorFactory::postException(ctx, exception, err);
235 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
237 JSStringRef propertyName,
238 JSValueRef* exception)
240 Converter converter(ctx);
244 AlarmAbsolutePtr privateData = getPrivData(object);
246 throw TypeMismatchException("Private object is null");
249 date = privateData->getDate();
250 LoggerI("JSAlarmAbsolute Date = " << " Sec : " << date.tm_sec << " Min : "<< date.tm_min
251 << " Hour" << date.tm_hour << "Day : " << date.tm_mday << " MON : " << date.tm_mon
252 << " Year : " << date.tm_year);
255 args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
256 args[1] = JSValueMakeNumber(ctx, date.tm_mon);
257 args[2] = JSValueMakeNumber(ctx, date.tm_mday);
258 args[3] = JSValueMakeNumber(ctx, date.tm_hour);
259 args[4] = JSValueMakeNumber(ctx, date.tm_min);
260 args[5] = JSValueMakeNumber(ctx, date.tm_sec);
262 JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
265 LoggerE("Exception: occured");
268 return JSValueMakeUndefined(ctx);
271 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
273 JSStringRef propertyName,
274 JSValueRef* exception)
277 AlarmAbsolutePtr privateData = getPrivData(object);
279 throw TypeMismatchException("Private object is null");
282 Converter converter(ctx);
283 if (privateData->is_registered) {
284 std::string strId = converter.toString(privateData->getId());
285 return converter.toJSValueRef(strId);
287 return JSValueMakeNull(ctx);
290 LoggerE("Exception: occured");
293 return JSValueMakeUndefined(ctx);
296 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
298 JSStringRef propertyName,
299 JSValueRef* exception)
302 AlarmAbsolutePtr privateData = getPrivData(object);
303 AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
305 if(alarmType == AbsoluteRecurrence::Interval) {
306 long interval = privateData->getInterval();
307 if (interval == -1 ) {
308 return JSValueMakeNull(ctx);
310 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, interval);
313 return JSValueMakeNull(ctx);
316 LoggerI("Exception: occured");
319 return JSValueMakeUndefined(ctx);
322 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
324 JSStringRef propertyName,
325 JSValueRef* exception)
327 Converter converter(ctx);
330 AlarmAbsolutePtr privateData = getPrivData(object);
332 throw TypeMismatchException("Private object is null");
335 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
336 if (jsResult == NULL) {
337 throw UnknownException("Could not create js array object");
340 std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
342 if(daysOfTheWeek.size() > 0) {
343 for(size_t i = 0; i<daysOfTheWeek.size(); i++) {
344 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
345 if(!JSSetArrayElement(ctx, jsResult, i, val)) {
346 throw UnknownException("Could not insert value into js array");
353 LoggerI("Exception: occured");
356 return JSValueMakeUndefined(ctx);