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.
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 <Tizen/Common/JSTizenExceptionFactory.h>
26 #include <Tizen/Common/JSTizenException.h>
27 #include "AlarmRelative.h"
28 #include "AlarmRecurrence.h"
29 #include "AlarmConverter.h"
32 #include "JSAlarmRecurrence.h"
33 #include "JSAbstractAlarm.h"
34 #include "JSAlarmRelative.h"
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45 using namespace TizenApis::Api::Alarm;
47 JSClassRef JSAlarmRelative::m_jsClassRef = NULL;
49 JSClassDefinition JSAlarmRelative::m_jsClassInfo = {
51 kJSClassAttributeNone,
53 JSAbstractAlarm::getClassRef(),
61 NULL, //deleteProperty,Geolocation
62 NULL, //getPropertyNames,
64 constructor, // constructor
69 JSStaticFunction JSAlarmRelative::m_function[] = {
70 { "getNextScheduledDate",JSAlarmRelative::getNextScheduledDate,kJSPropertyAttributeNone },
74 JSStaticValue JSAlarmRelative::m_property[] = {
75 { "id", getId, NULL, kJSPropertyAttributeReadOnly },
76 { "recurrenceRule", getRecurrenceRule, NULL, kJSPropertyAttributeReadOnly },
80 const JSClassRef JSAlarmRelative::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
88 const JSClassDefinition* JSAlarmRelative::getClassInfo()
90 return &m_jsClassInfo;
93 void JSAlarmRelative::initialize(JSContextRef context, JSObjectRef object)
97 void JSAlarmRelative::finalize(JSObjectRef object)
102 bool JSAlarmRelative::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
104 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
108 JSObjectRef JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
111 LogError("[ILOVEFILM] Enter constructor");
113 // 1. Date, Recurrence
114 // 2. Delay, Recurrence
116 AlarmConverter converter(ctx);
117 Validator validator(ctx);
118 AlarmRelative::alarm_type_e alarm_type;
120 bool isRecurrence = false;
123 if(argumentCount < 1)
126 LogWarning("Argument is not corrent");
130 if(JSValueIsNumber(ctx, arguments[0]));
133 delay = converter.toInt(arguments[0]);
134 alarm_type = Alarm::ALARM_TYPE_DELAY;
137 if( validator.isDate(arguments[0]) == true)
140 date = converter.toDateTm(arguments[0]);
141 alarm_type = Alarm::ALARM_TYPE_DATE;
144 AlarmRecurrencePtr result;
146 if(argumentCount == 2)
148 if(JSValueIsObjectOfClass(ctx, arguments[1], JSAlarmRecurrence::getClassRef()))
150 JSObjectRef recurreceObj = JSValueToObject(ctx, arguments[1], exception);
152 JSAlarmRecurrencePriv*priv = static_cast<JSAlarmRecurrencePriv*>(JSObjectGetPrivate(recurreceObj));
154 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157 result = priv->getObject();
159 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165 if(alarm_type == Alarm::ALARM_TYPE_DELAY)
167 if(isRecurrence == true) {
168 return JSValueToObject(ctx, createJSObject(ctx, delay, result), exception);
172 return JSValueToObject(ctx, createJSObject(ctx, delay,0), exception);
177 if(isRecurrence == true) {
178 return JSValueToObject(ctx, createJSObject(ctx, date, result), exception);
182 return JSValueToObject(ctx, createJSObject(ctx, date), exception);
188 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
190 JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
192 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
194 AlarmRelativePtr result = priv->getObject();
196 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
202 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, const int id)
204 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
206 privateData->setId(id);
208 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
209 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
210 if (NULL == jsValueRef) {
211 LogError("object creation error");
212 return JSValueMakeUndefined(context);
217 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, struct tm date, AlarmRecurrencePtr ptr)
219 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
221 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
222 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
223 if (NULL == jsValueRef) {
224 LogError("object creation error");
225 return JSValueMakeUndefined(context);
230 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, AlarmRecurrencePtr ptr)
232 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
234 privateData->setDelay(delay);
235 privateData->setRecurrence(ptr);
237 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
238 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
239 if (NULL == jsValueRef) {
240 LogError("object creation error");
241 return JSValueMakeUndefined(context);
247 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, struct tm date)
249 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
251 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
252 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
253 if (NULL == jsValueRef) {
254 LogError("object creation error");
255 return JSValueMakeUndefined(context);
260 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int dummy)
262 AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
263 privateData->setDelay(delay);
265 JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
267 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
268 if (NULL == jsValueRef) {
269 LogError("object creation error");
270 return JSValueMakeUndefined(context);
275 JSValueRef JSAlarmRelative::getNextScheduledDate(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
278 Converter converter(ctx);
282 AlarmRelativePtr privateData = getPrivData(object);
283 id = privateData->getId();
285 Catch(WrtDeviceApis::Commons::Exception)
287 LogWarning("trying to get incorrect value");
290 int err = alarm_get_scheduled_date(id, &date);
292 JSValueRef result = converter.toJSValueRef(date);
294 if(err != ALARM_ERROR_NONE)
296 return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
301 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
303 JSStringRef propertyName,
304 JSValueRef* exception)
306 Converter converter(ctx);
310 AlarmRelativePtr privateData = getPrivData(object);
311 id = privateData->getId();
313 Catch(WrtDeviceApis::Commons::Exception)
315 LogWarning("trying to get incorrect value");
318 return converter.toJSValueRef(id);
322 JSValueRef JSAlarmRelative::getRecurrenceRule(JSContextRef ctx,
324 JSStringRef propertyName,
325 JSValueRef* exception)
328 Converter converter(ctx);
332 AlarmPtr privateData = getPrivData(object);
333 interval = privateData->getRecurrence();
335 Catch(WrtDeviceApis::Commons::Exception)
337 LogWarning("trying to get incorrect value");
340 return converter.toJSValueRef(interval);