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.
18 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
23 #include <Commons/Exception.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/Common/JSTizenException.h>
26 #include <Tizen/Common/SecurityExceptions.h>
28 #include "AlarmAbsolute.h"
29 #include "AlarmConverter.h"
33 #include "plugin_config.h"
34 #include "JSAlarmAbsolute.h"
35 #include "JSAlarmManager.h"
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace TizenApis::Commons;
44 using namespace TizenApis::Api::Alarm;
46 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
48 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
50 kJSClassAttributeNone,
51 TIZEN_ALARM_ABSOLUTE_INTERFACE,
60 NULL, //deleteProperty,Geolocation
61 NULL, //getPropertyNames,
63 constructor, // constructor
68 JSStaticFunction JSAlarmAbsolute::m_function[] = {
69 { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
73 JSStaticValue JSAlarmAbsolute::m_property[] = {
74 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
75 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DATE, getDate, NULL, kJSPropertyAttributeReadOnly },
76 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_PERIOD, getInterval, NULL, kJSPropertyAttributeReadOnly },
77 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DAYSOFTHEWEEK, getDaysOfTheWeek, NULL, kJSPropertyAttributeReadOnly },
81 const JSClassRef JSAlarmAbsolute::getClassRef()
84 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
89 const JSClassDefinition* JSAlarmAbsolute::getClassInfo()
91 return &m_jsClassInfo;
94 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object)
97 void JSAlarmAbsolute::finalize(JSObjectRef object)
99 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
101 LogError("Private object is null");
107 bool JSAlarmAbsolute::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
109 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
112 JSObjectRef JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
115 AlarmConverter converter(ctx);
116 Validator validator(ctx);
119 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
121 if(argumentCount < 1)
123 ThrowMsg(ConversionException, "Wrong parameter type.");
126 if(!validator.isDate(arguments[0]))
128 ThrowMsg(ConversionException, "Wrong first parameter type.");
131 date = converter.toDateTm(arguments[0]);
132 privateData->setDate(date);
134 if(argumentCount >= 2)
136 if(JSIsArrayValue(ctx, arguments[1])) {
137 std::vector<std::string> daysOfTheWeek = converter.toVectorOfStrings(arguments[1]);
138 if(daysOfTheWeek.size() >0) {
139 privateData->setByDayRecurrence(daysOfTheWeek);
142 int interval = converter.toInt(arguments[1]);
144 ThrowMsg(InvalidArgumentException, "Invalid period value.");
146 privateData->setInterval(interval);
150 return JSValueToObject(ctx, createJSObject(ctx, privateData), exception);
151 } Catch(ConversionException) {
152 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
153 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
154 } Catch(UnsupportedException) {
155 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
156 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
157 } Catch(InvalidArgumentException) {
158 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
159 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
161 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
162 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
168 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
170 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
172 ThrowMsg(ConversionException, "Object is null.");
174 AlarmAbsolutePtr result = priv->getObject();
176 ThrowMsg(ConversionException, "Private object is null.");
181 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, const int id)
183 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
184 privateData->setId(id);
186 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
188 ThrowMsg(ConversionException, "Object is null.");
190 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
193 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
195 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
197 ThrowMsg(ConversionException, "Object is null.");
199 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
202 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date, int interval)
204 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
205 privateData->setDate(date);
207 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
209 ThrowMsg(ConversionException, "Object is null.");
211 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
214 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date)
216 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
217 privateData->setDate(date);
219 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
221 ThrowMsg(ConversionException, "Object is null.");
223 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
226 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
228 LogError("GContext = " << TizenApis::Tizen1_0::Alarm::JSAlarmManager::gContext);
229 AceSecurityStatus status = ALARM_CHECK_ACCESS(
230 ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE);
231 TIZEN_SYNC_ACCESS_HANDLER(status, ctx, exception);
234 Converter converter(ctx);
238 AlarmAbsolutePtr privateData = getPrivData(thisObject);
239 id = privateData->getId();
240 int err = alarm_get_scheduled_date(id, &date);
241 JSValueRef result = converter.toJSValueRef(date);
243 if(err != ALARM_ERROR_NONE)
245 return JSValueMakeNull(ctx);
248 } Catch(ConversionException) {
249 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
250 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
251 } Catch(UnsupportedException) {
252 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
253 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
254 } Catch(InvalidArgumentException) {
255 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
256 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
258 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
259 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
263 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
265 JSStringRef propertyName,
266 JSValueRef* exception)
268 Converter converter(ctx);
272 AlarmAbsolutePtr privateData = getPrivData(object);
273 date = privateData->getDate();
274 LogInfo("JSAlarmAbsolute Date = " << " Sec : " << date.tm_sec << " Min : "<< date.tm_min
275 << " Hour" << date.tm_hour << "Day : " << date.tm_mday << " MON : " << date.tm_mon
276 << " Year : " << date.tm_year);
279 args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
280 args[1] = JSValueMakeNumber(ctx, date.tm_mon);
281 args[2] = JSValueMakeNumber(ctx, date.tm_mday);
282 args[3] = JSValueMakeNumber(ctx, date.tm_hour);
283 args[4] = JSValueMakeNumber(ctx, date.tm_min);
284 args[5] = JSValueMakeNumber(ctx, date.tm_sec);
286 JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
291 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
293 return JSValueMakeUndefined(ctx);
296 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
298 JSStringRef propertyName,
299 JSValueRef* exception)
301 Converter converter(ctx);
305 AlarmAbsolutePtr privateData = getPrivData(object);
306 Converter converter(ctx);
307 id = privateData->getId();
309 std::string strId = converter.toString(id);
310 return converter.toJSValueRef(strId);
312 return JSValueMakeNull(ctx);
317 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
319 return JSValueMakeUndefined(ctx);
322 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
324 JSStringRef propertyName,
325 JSValueRef* exception)
327 Converter converter(ctx);
331 AlarmAbsolutePtr privateData = getPrivData(object);
332 AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
334 if(alarmType == AbsoluteRecurrence::Interval) {
335 interval = privateData->getInterval();
336 return converter.toJSValueRef(interval);
338 return JSValueMakeNull(ctx);
343 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
345 return JSValueMakeUndefined(ctx);
348 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
350 JSStringRef propertyName,
351 JSValueRef* exception)
353 Converter converter(ctx);
356 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
357 AlarmAbsolutePtr privateData = getPrivData(object);
358 std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
360 if(daysOfTheWeek.size() >0)
362 if (jsResult == NULL)
364 ThrowMsg(NullPointerException, "Could not create js array object");
367 for(unsigned int i=0; i<daysOfTheWeek.size(); i++)
369 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
370 if(!JSSetArrayElement(ctx, jsResult, i, val))
372 ThrowMsg(UnknownException, "Could not insert value into js array");
381 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
383 return JSValueMakeUndefined(ctx);