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 // remove unacceptable data from vector
134 daysOfTheWeek.erase(std::remove(daysOfTheWeek.begin(), daysOfTheWeek.end(), daysOfTheWeek[i]), daysOfTheWeek.end());
138 if(daysOfTheWeek.size() > 0) {
139 priv->setByDayRecurrence(daysOfTheWeek);
142 long interval = validator.toLong(1);
144 throw InvalidValuesException("period can not be negative value");
147 priv->setInterval(interval);
150 } catch (const BasePlatformException& err) {
151 LoggerE("Exception occured while creating constructor : " << err.getMessage());
154 JSAlarmAbsolutePriv *jspriv = new JSAlarmAbsolutePriv(ctx, priv);
155 JSObjectRef obj = JSObjectMake(ctx, getClassRef(), jspriv);
157 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
158 JSObjectSetProperty(ctx, obj, ctorName, constructor,
159 kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
160 JSStringRelease(ctorName);
165 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
167 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
169 throw TypeMismatchException("Private object is null");
171 AlarmAbsolutePtr result = priv->getObject();
173 throw TypeMismatchException("Private object is null");
179 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
181 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
183 throw TypeMismatchException("Private object is null");
185 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
188 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
190 TIME_TRACER_ITEM_BEGIN(__FUNCTION__, 0);
193 Converter converter(ctx);
195 AlarmAbsolutePtr privateData = getPrivData(thisObject);
197 throw TypeMismatchException("Private object is null");
200 if(!privateData->is_registered) {
201 return JSValueMakeNull(ctx);
204 int id = privateData->getId();
205 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_scheduled_date", 0);
206 int err = alarm_get_scheduled_date(id, &date);
207 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_scheduled_date", 0);
208 if(err != ALARM_ERROR_NONE) {
209 return JSValueMakeNull(ctx);
212 // check wheter the alarm is expired or not
214 TIME_TRACER_ITEM_BEGIN("(getNextScheduledDate)alarm_get_current_time", 0);
215 err = alarm_get_current_time(&curr_date);
216 TIME_TRACER_ITEM_END("(getNextScheduledDate)alarm_get_current_time", 0);
217 if(err != ALARM_ERROR_NONE) {
218 return JSValueMakeNull(ctx);
221 if (mktime(&date) < mktime(&curr_date)) {
222 return JSValueMakeNull(ctx);
224 TIME_TRACER_ITEM_END(__FUNCTION__, 0);
226 return converter.toJSValueRef(date);
228 } catch (const BasePlatformException &err) {
229 return JSWebAPIErrorFactory::postException(ctx, exception, err);
231 DeviceAPI::Common::UnknownException err("Unknown Error in ApplicationManager.getAppSharedURI().");
232 return JSWebAPIErrorFactory::postException(ctx, exception, err);
236 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
238 JSStringRef propertyName,
239 JSValueRef* exception)
241 Converter converter(ctx);
245 AlarmAbsolutePtr privateData = getPrivData(object);
247 throw TypeMismatchException("Private object is null");
250 date = privateData->getDate();
251 LoggerI("JSAlarmAbsolute Date = " << " Sec : " << date.tm_sec << " Min : "<< date.tm_min
252 << " Hour" << date.tm_hour << "Day : " << date.tm_mday << " MON : " << date.tm_mon
253 << " Year : " << date.tm_year);
256 args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
257 args[1] = JSValueMakeNumber(ctx, date.tm_mon);
258 args[2] = JSValueMakeNumber(ctx, date.tm_mday);
259 args[3] = JSValueMakeNumber(ctx, date.tm_hour);
260 args[4] = JSValueMakeNumber(ctx, date.tm_min);
261 args[5] = JSValueMakeNumber(ctx, date.tm_sec);
263 JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
266 LoggerE("Exception: occured");
269 return JSValueMakeUndefined(ctx);
272 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
274 JSStringRef propertyName,
275 JSValueRef* exception)
278 AlarmAbsolutePtr privateData = getPrivData(object);
280 throw TypeMismatchException("Private object is null");
283 Converter converter(ctx);
284 if (privateData->is_registered) {
285 std::string strId = converter.toString(privateData->getId());
286 return converter.toJSValueRef(strId);
288 return JSValueMakeNull(ctx);
291 LoggerE("Exception: occured");
294 return JSValueMakeUndefined(ctx);
297 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
299 JSStringRef propertyName,
300 JSValueRef* exception)
303 AlarmAbsolutePtr privateData = getPrivData(object);
304 AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
306 if(alarmType == AbsoluteRecurrence::Interval) {
307 long interval = privateData->getInterval();
308 if (interval == -1 ) {
309 return JSValueMakeNull(ctx);
311 return DeviceAPI::Common::JSUtil::toJSValueRef(ctx, interval);
314 return JSValueMakeNull(ctx);
317 LoggerI("Exception: occured");
320 return JSValueMakeUndefined(ctx);
323 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
325 JSStringRef propertyName,
326 JSValueRef* exception)
328 Converter converter(ctx);
331 AlarmAbsolutePtr privateData = getPrivData(object);
333 throw TypeMismatchException("Private object is null");
336 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
337 if (jsResult == NULL) {
338 throw UnknownException("Could not create js array object");
341 std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
343 if(daysOfTheWeek.size() > 0) {
344 for(size_t i = 0; i<daysOfTheWeek.size(); i++) {
345 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
346 if(!JSSetArrayElement(ctx, jsResult, i, val)) {
347 throw UnknownException("Could not insert value into js array");
354 LoggerI("Exception: occured");
357 return JSValueMakeUndefined(ctx);