2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <dpl/log/log.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/Validator.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26 #include <Commons/Exception.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
30 #include "AlarmAbsolute.h"
31 #include "AlarmConverter.h"
32 #include "JSAlarmManager.h"
33 #include "JSAlarmAbsolute.h"
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace TizenApis::Tizen1_0::Alarm;
44 using namespace TizenApis::Api::Alarm;
47 using TizenApis::Api::Alarm::Alarm;
48 using TizenApis::Tizen1_0::Alarm::JSAlarm;
55 using namespace TizenApis::Api::Alarm;
56 using namespace WrtDeviceApis;
57 using namespace WrtDeviceApis::CommonsJavaScript;
61 static bool alarm_iterate_callback(int alarm_id, void *user_data)
63 vector<int> *alarmIds = reinterpret_cast<vector<int>*>(user_data);
65 alarmIds->push_back(alarm_id);
70 JSClassRef JSAlarmManager::m_jsClassRef = NULL;
72 JSClassDefinition JSAlarmManager::m_jsClassInfo = {
74 kJSClassAttributeNone,
84 NULL, //deleteProperty,Geolocation
85 NULL, //getPropertyNames,
92 JSStaticFunction JSAlarmManager::m_function[] = {
93 { "add",JSAlarmManager::add,kJSPropertyAttributeNone },
94 { "remove",JSAlarmManager::remove,kJSPropertyAttributeNone },
95 { "getAlarms",JSAlarmManager::getAlarms,kJSPropertyAttributeNone },
99 const JSClassRef JSAlarmManager::getClassRef()
102 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
107 const JSClassDefinition* JSAlarmManager::getClassInfo()
109 return &m_jsClassInfo;
112 void JSAlarmManager::initialize(JSContextRef ctx, JSObjectRef object)
116 void JSAlarmManager::finalize(JSObjectRef object)
121 bool JSAlarmManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
122 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
125 JSValueRef JSAlarmManager::add(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
131 Alarm::alarm_type_e type = Alarm::ALARM_TYPE_INVALID;
134 AlarmRecurrencePtr recurrencePtr;
138 LogError("[ILOVEFILM] Enter add");
140 Validator check(ctx, exception);
141 AlarmConverter converter(ctx);
143 LogError("[ILOVEFILM] Enter add2");
144 if(argumentCount < 2) {
145 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147 LogError("[ILOVEFILM] Enter add3");
148 if(!JSValueIsObjectOfClass(ctx, arguments[0], JSAlarmAbsolute::getClassRef())) {
149 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153 // 1. type(delay, date)
155 // 3. convert freqeuncy to second
157 // 1. GetAlarmPrivate Data
158 LogError("[ILOVEFILM] Enter add4");
159 JSObjectRef alarmObj = JSValueToObject(ctx, arguments[0], exception);
161 LogError("[ILOVEFILM] Enter add5");
162 JSAlarmPriv *priv = static_cast<JSAlarmPriv*>(JSObjectGetPrivate(alarmObj));
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
166 LogError("[ILOVEFILM] Enter add6");
167 AlarmAbsolutePtr alarmPtr = priv->getObject();
169 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
172 LogError("[ILOVEFILM] Enter add7");
174 type = alarmPtr->getAlarmType();
175 if(type == Alarm::ALARM_TYPE_DELAY)
177 LogError("[ILOVEFILM] Enter add8 get Date");
178 delay = alarmPtr->getDelay();
180 else if(type == Alarm::ALARM_TYPE_DATE)
182 LogError("[ILOVEFILM] Enter add8 get Delay");
183 startDate = alarmPtr->getDate();
187 if(type == Alarm::ALARM_TYPE_DELAY)
189 service = converter.toService(converter.toString(arguments[1]));
190 if(alarmPtr->isRecurrence())
192 recurrencePtr = alarmPtr->getRecurrence();
196 int err = alarm_schedule_after_delay(service, delay, 0, &id);
200 } else if(type == Alarm::ALARM_TYPE_DATE)
202 LogError("[ILOVEFILM] Enter add9");
203 service = converter.toService(converter.toString(arguments[1]));
204 if(alarmPtr->isRecurrence())
206 LogError("[ILOVEFILM] Enter add10");
207 recurrencePtr = alarmPtr->getRecurrence();
208 int err = alarm_schedule_at_date(service, &startDate, 0, &id);
213 service_get_package(service, &package);
215 alarm_get_current_time(¤t);
216 LogError("[ILOVEFILM] Current Date = " << "Sec : " << current.tm_sec << " Min : "<< current.tm_min
217 << " Hour : " << current.tm_hour << " Day : " << current.tm_mday << " MON : " << current.tm_mon
218 << " Year : " << current.tm_year);
220 LogError("[ILOVEFILM] Enter add11 service package = " << package);
221 LogError("[ILOVEFILM] Date = " << "Sec : " << startDate.tm_sec << " Min : "<< startDate.tm_min
222 << " Hour : " << startDate.tm_hour << " Day : " << startDate.tm_mday << " MON : " << startDate.tm_mon
223 << " Year : " << startDate.tm_year);
224 int err = alarm_schedule_at_date(service, &startDate, 0, &id);
225 LogError("[ILOVEFILM] Enter add12 error code " << err );
228 LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_INVALID_PARAMETER = " << ALARM_ERROR_INVALID_PARAMETER );
229 LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_INVALID_DATE = " << ALARM_ERROR_INVALID_DATE );
230 LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_CONNECTION_FAIL = " << ALARM_ERROR_CONNECTION_FAIL );
241 JSValueRef JSAlarmManager::remove(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
245 if(argumentCount < 1 || !JSValueIsString(ctx, arguments[0]))
246 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
248 Converter converter(ctx);
249 // todo : check exception or error
250 string stringID = converter.toString(arguments[0]);
252 int id = atoi(stringID.c_str());
254 err = alarm_cancel(id);
256 if(err != ALARM_ERROR_NONE)
258 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
261 return JSValueMakeUndefined(ctx);
266 JSValueRef JSAlarmManager::getAlarms(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
269 vector<int> alarmIds;
270 error = alarm_foreach_registered_alarm(alarm_iterate_callback, &alarmIds);
272 if(error != ALARM_ERROR_NONE)
274 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
278 AlarmArrayPtr alarmObjectArrary;
279 for(int i = 0; i < alarmIds.size(); i++)
281 TizenApis::Tizen1_0::Alarm::JSAlarmAbsolute::createJSObject(ctx, alarmIds[i]);
288 JSValueRef JSAlarmManager::createAlarm(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
295 Validator check(ctx,exception);
297 if(argumentCount < 3) {
298 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
301 if(JSValueIsNumber(ctx, arguments[1])) {
302 return createAfterDelay(ctx, object, thisObject, argumentCount, arguments, exception);
305 if(check.isCallback(arguments[0]) | check.isNullOrUndefined(arguments[0])) {
306 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
309 if(!check.isDate(arguments[1])) {
310 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
313 if(!JSValueIsNumber(ctx, arguments[2])) {
314 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
317 AlarmConverter alarmConverter(ctx);
318 date = alarmConverter.toDateTm(arguments[1]);
319 int period = (int)JSValueToNumber(ctx, arguments[2], exception);
320 service = alarmConverter.toService(arguments[0]);
321 int err = alarm_schedule_at_date(service, &date, period, &id);
323 service_destroy(service);
325 if(err != ALARM_ERROR_NONE) {
326 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
329 return alarmConverter.toJSValueRef(id);
334 JSValueRef JSAlarmManager::createAfterDelay(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
341 Validator check(ctx,exception);
343 if(argumentCount < 3) {
344 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
347 if(check.isCallback(arguments[0]) | check.isNullOrUndefined(arguments[0])) {
348 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
351 if( !JSValueIsNumber(ctx, arguments[1])) {
352 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
355 if(!JSValueIsNumber(ctx, arguments[2])) {
356 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
359 AlarmConverter alarmConverter(ctx);
360 int delay = (int)JSValueToNumber(ctx, arguments[1], exception);
361 int period = (int)JSValueToNumber(ctx, arguments[2], exception);
362 service = alarmConverter.toService(arguments[0]);
364 int err = alarm_schedule_after_delay(service, delay, period, &id);
366 service_destroy(service);
368 if(err != ALARM_ERROR_NONE) {
369 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
372 return alarmConverter.toJSValueRef(id);
375 JSValueRef JSAlarmManager::cancelAlarm(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
379 if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0]))
380 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
382 int id = (int)JSValueToNumber(ctx, arguments[0], exception);
384 err = alarm_cancel(id);
386 if(err != ALARM_ERROR_NONE)
388 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
391 return JSValueMakeUndefined(ctx);
395 JSValueRef JSAlarmManager::getAlarmIDs(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
397 vector<int> alarmIds;
398 alarm_foreach_registered_alarm(alarm_iterate_callback, &alarmIds);
400 Converter converter(ctx);
401 return converter.toJSValueRef(alarmIds);
405 JSValueRef JSAlarmManager::getNextScheduledDate(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
407 if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0]))
408 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
411 Converter converter(ctx);
412 int id = (int)JSValueToNumber(ctx, arguments[0], exception);
413 int err = alarm_get_scheduled_date(id, &date);
415 JSValueRef result = converter.toJSValueRef(date);
417 if(err != ALARM_ERROR_NONE)
419 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
426 JSValueRef JSAlarmManager::getReccurentSecond(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
428 if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0]))
429 return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
432 Converter converter(ctx);
433 int id = (int)JSValueToNumber(ctx, arguments[0], exception);
434 int err = alarm_get_scheduled_period(id, &period);
435 if(err != ALARM_ERROR_NONE)
437 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
440 return converter.toJSValueRef(period);