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.
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 <JSTizenExceptionFactory.h>
26 #include <JSTizenException.h>
27 #include <SecurityExceptions.h>
29 #include "AlarmAbsolute.h"
30 #include "AlarmConverter.h"
34 #include "plugin_config.h"
35 #include "JSAlarmAbsolute.h"
36 #include "JSAlarmManager.h"
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace DeviceAPI::Common;
45 JSClassRef JSAlarmAbsolute::m_jsClassRef = NULL;
47 JSClassDefinition JSAlarmAbsolute::m_jsClassInfo = {
49 kJSClassAttributeNone,
50 TIZEN_ALARM_ABSOLUTE_INTERFACE,
59 NULL, //deleteProperty,Geolocation
60 NULL, //getPropertyNames,
62 constructor, // constructor
67 JSStaticFunction JSAlarmAbsolute::m_function[] = {
68 { ALARM_FUNCTION_API_GET_NEXT_SCHEDULED_DATE, JSAlarmAbsolute::getNextScheduledDate, kJSPropertyAttributeNone },
72 JSStaticValue JSAlarmAbsolute::m_property[] = {
73 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_ID, getId, NULL, kJSPropertyAttributeReadOnly },
74 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DATE, getDate, NULL, kJSPropertyAttributeReadOnly },
75 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_PERIOD, getInterval, NULL, kJSPropertyAttributeReadOnly },
76 { TIZEN_ALARM_ABSOLUTE_ATTRIBUTE_DAYSOFTHEWEEK, getDaysOfTheWeek, NULL, kJSPropertyAttributeReadOnly },
80 const JSClassRef JSAlarmAbsolute::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
88 const JSClassDefinition* JSAlarmAbsolute::getClassInfo()
90 return &m_jsClassInfo;
93 void JSAlarmAbsolute::initialize(JSContextRef context, JSObjectRef object)
96 void JSAlarmAbsolute::finalize(JSObjectRef object)
98 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
100 LogError("Private object is null");
106 bool JSAlarmAbsolute::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
108 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
111 JSObjectRef JSAlarmAbsolute::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
114 AlarmConverter converter(ctx);
115 Validator validator(ctx);
118 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
120 if(argumentCount < 1)
122 ThrowMsg(ConversionException, "Wrong parameter type.");
125 if(!validator.isDate(arguments[0]))
127 ThrowMsg(ConversionException, "Wrong first parameter type.");
130 date = converter.toDateTm(arguments[0]);
131 privateData->setDate(date);
133 if(argumentCount >= 2)
135 if(JSIsArrayValue(ctx, arguments[1])) {
136 std::vector<std::string> daysOfTheWeek = converter.toVectorOfStrings(arguments[1]);
138 for( int i = 0; i< daysOfTheWeek.size(); i++ )
140 if( (daysOfTheWeek[i]!="SU") && (daysOfTheWeek[i]!="MO") &&
141 (daysOfTheWeek[i]!="TU") && (daysOfTheWeek[i]!="WE") &&
142 (daysOfTheWeek[i]!="TH") && (daysOfTheWeek[i]!="FR") &&
143 (daysOfTheWeek[i]!="SA") ) {
144 ThrowMsg(ConversionException, "Invalid days of the week value.");
148 if(daysOfTheWeek.size() > 0) {
149 privateData->setByDayRecurrence(daysOfTheWeek);
152 int interval = converter.toInt(arguments[1]);
154 ThrowMsg(InvalidArgumentException, "Invalid period value.");
156 privateData->setInterval(interval);
160 return JSValueToObject(ctx, createJSObject(ctx, privateData), exception);
161 } Catch(ConversionException) {
162 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
163 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
164 } Catch(UnsupportedException) {
165 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
166 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
167 } Catch(InvalidArgumentException) {
168 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
169 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
171 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
172 *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
178 AlarmAbsolutePtr JSAlarmAbsolute::getPrivData(JSObjectRef object)
180 JSAlarmAbsolutePriv *priv = static_cast<JSAlarmAbsolutePriv*>(JSObjectGetPrivate(object));
182 ThrowMsg(ConversionException, "Object is null.");
184 AlarmAbsolutePtr result = priv->getObject();
186 ThrowMsg(ConversionException, "Private object is null.");
191 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, const int id)
193 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
194 privateData->setId(id);
196 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
198 ThrowMsg(ConversionException, "Object is null.");
200 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
203 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, AlarmAbsolutePtr privateData)
205 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
207 ThrowMsg(ConversionException, "Object is null.");
209 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
212 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date, int interval)
214 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
215 privateData->setDate(date);
217 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
219 ThrowMsg(ConversionException, "Object is null.");
221 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
224 JSValueRef JSAlarmAbsolute::createJSObject(JSContextRef context, struct tm date)
226 AlarmAbsolutePtr privateData = AlarmAbsolutePtr(new AlarmAbsolute());
227 privateData->setDate(date);
229 JSAlarmAbsolutePriv *priv = new JSAlarmAbsolutePriv(context, privateData);
231 ThrowMsg(ConversionException, "Object is null.");
233 return JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
236 JSValueRef JSAlarmAbsolute::getNextScheduledDate( JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception)
238 LogError("GContext = " << DeviceAPI::Alarm::JSAlarmManager::gContext);
241 Converter converter(ctx);
245 AlarmAbsolutePtr privateData = getPrivData(thisObject);
246 id = privateData->getId();
247 int err = alarm_get_scheduled_date(id, &date);
248 JSValueRef result = converter.toJSValueRef(date);
250 if(err != ALARM_ERROR_NONE)
252 return JSValueMakeNull(ctx);
255 } Catch(ConversionException) {
256 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
257 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
258 } Catch(UnsupportedException) {
259 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
260 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
261 } Catch(InvalidArgumentException) {
262 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
263 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
265 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
266 return JSTizenExceptionFactory::postException(ctx, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
270 JSValueRef JSAlarmAbsolute::getDate(JSContextRef ctx,
272 JSStringRef propertyName,
273 JSValueRef* exception)
275 Converter converter(ctx);
279 AlarmAbsolutePtr privateData = getPrivData(object);
280 date = privateData->getDate();
281 LogInfo("JSAlarmAbsolute Date = " << " Sec : " << date.tm_sec << " Min : "<< date.tm_min
282 << " Hour" << date.tm_hour << "Day : " << date.tm_mday << " MON : " << date.tm_mon
283 << " Year : " << date.tm_year);
286 args[0] = JSValueMakeNumber(ctx, date.tm_year + 1900);
287 args[1] = JSValueMakeNumber(ctx, date.tm_mon);
288 args[2] = JSValueMakeNumber(ctx, date.tm_mday);
289 args[3] = JSValueMakeNumber(ctx, date.tm_hour);
290 args[4] = JSValueMakeNumber(ctx, date.tm_min);
291 args[5] = JSValueMakeNumber(ctx, date.tm_sec);
293 JSObjectRef result = JSObjectMakeDate(ctx, 6, args, exception);
298 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
300 return JSValueMakeUndefined(ctx);
303 JSValueRef JSAlarmAbsolute::getId(JSContextRef ctx,
305 JSStringRef propertyName,
306 JSValueRef* exception)
308 Converter converter(ctx);
312 AlarmAbsolutePtr privateData = getPrivData(object);
313 Converter converter(ctx);
314 id = privateData->getId();
316 std::string strId = converter.toString(id);
317 return converter.toJSValueRef(strId);
319 return JSValueMakeNull(ctx);
324 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
326 return JSValueMakeUndefined(ctx);
329 JSValueRef JSAlarmAbsolute::getInterval(JSContextRef ctx,
331 JSStringRef propertyName,
332 JSValueRef* exception)
334 Converter converter(ctx);
338 AlarmAbsolutePtr privateData = getPrivData(object);
339 AbsoluteRecurrence::Type alarmType = privateData->getRecurrenceType();
341 if(alarmType == AbsoluteRecurrence::Interval) {
342 interval = privateData->getInterval();
343 return converter.toJSValueRef(interval);
345 return JSValueMakeNull(ctx);
350 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
352 return JSValueMakeUndefined(ctx);
355 JSValueRef JSAlarmAbsolute::getDaysOfTheWeek(JSContextRef ctx,
357 JSStringRef propertyName,
358 JSValueRef* exception)
360 Converter converter(ctx);
363 JSObjectRef jsResult = JSCreateArrayObject(ctx, 0, NULL);
364 AlarmAbsolutePtr privateData = getPrivData(object);
365 std::vector<std::string> daysOfTheWeek = privateData->getByDayRecurrence();
367 if(daysOfTheWeek.size() >0)
369 if (jsResult == NULL)
371 ThrowMsg(NullPointerException, "Could not create js array object");
374 for(unsigned int i=0; i<daysOfTheWeek.size(); i++)
376 JSValueRef val = converter.toJSValueRef(daysOfTheWeek.at(i));
377 if(!JSSetArrayElement(ctx, jsResult, i, val))
379 ThrowMsg(UnknownException, "Could not insert value into js array");
388 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
390 return JSValueMakeUndefined(ctx);