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 <Commons/Exception.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include "AlarmRecurrence.h"
27 #include "JSAlarmRecurrence.h"
28 #include "AlarmConverter.h"
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace TizenApis::Commons;
41 JSClassRef JSAlarmRecurrence::m_jsClassRef = NULL;
43 JSClassDefinition JSAlarmRecurrence::m_jsClassInfo = {
45 kJSClassAttributeNone,
55 NULL, //deleteProperty
56 NULL, //getPropertyNames,
58 constructor, // constructor
63 JSStaticValue JSAlarmRecurrence::m_property[] = {
64 { "frequency", getFrequency, setFrequency, kJSPropertyAttributeNone },
65 { "interval", getInterval, setInterval, kJSPropertyAttributeNone },
66 { "daysOfTheWeek", getDaysOfTheWeek, setDaysOfTheWeek, kJSPropertyAttributeNone },
70 const JSClassRef JSAlarmRecurrence::getClassRef()
73 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
78 const JSClassDefinition* JSAlarmRecurrence::getClassInfo()
80 return &m_jsClassInfo;
83 void JSAlarmRecurrence::initialize(JSContextRef context, JSObjectRef object)
88 void JSAlarmRecurrence::finalize(JSObjectRef object)
93 JSObjectRef JSAlarmRecurrence::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
95 // Parameter can be 1 or 2.
97 // 1. Parameter : AlarmFrequency
98 // 2. Parameter : interval
99 // 3. Parameter : ByDayValue[]
100 AlarmConverter converter(ctx);
102 // return : Should return AlarmRecurrence object
103 if(argumentCount < 1)
105 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
108 if(argumentCount == 1)
111 // convert javascript array to string array
112 // How can i check javascript
113 if(!JSIsArrayValue(ctx, arguments[0]))
116 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
118 std::vector<std::string> temp = converter.toVectorOfStrings(arguments[0]);
119 JSObjectRef obj = createJSObject(ctx, temp);
124 std::string frequencyStr;
126 AlarmRecurrence::alarm_frequency_e frequency;
127 // Should frequency & interval
128 if(!JSValueIsString(ctx, arguments[0]))
130 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
132 frequencyStr = converter.toString(arguments[0]);
134 frequency = converter.toRecurrenceFrequency(frequencyStr);
136 if(frequency == AlarmRecurrence::NO_RECURRENCE)
138 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
141 if(JSValueIsNumber(ctx, arguments[1]))
143 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
146 interval = converter.toInt(arguments[1]);
150 return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
153 JSObjectRef obj = createJSObject(ctx, frequency, interval);
160 bool JSAlarmRecurrence::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
162 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
165 AlarmRecurrencePtr JSAlarmRecurrence::getPrivData(JSObjectRef object)
167 JSAlarmRecurrencePriv*priv = static_cast<JSAlarmRecurrencePriv*>(JSObjectGetPrivate(object));
169 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
172 AlarmRecurrencePtr result = priv->getObject();
174 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
179 JSObjectRef JSAlarmRecurrence::createJSObject(JSContextRef context, std::vector<std::string> daysOfTheWeek)
181 AlarmRecurrencePtr privateData = AlarmRecurrencePtr(new AlarmRecurrence());
182 privateData->setDaysOfTheWeek(daysOfTheWeek);
183 JSAlarmRecurrencePriv *priv = new JSAlarmRecurrencePriv(context, privateData);
184 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
185 if (NULL == jsObjectRef) {
187 LogError("JSAlarmRecurrence object creation error");
193 JSObjectRef JSAlarmRecurrence::createJSObject(JSContextRef context, AlarmRecurrence::alarm_frequency_e frequency, int interval)
195 AlarmRecurrencePtr privateData = AlarmRecurrencePtr(new AlarmRecurrence());
196 privateData->setInterval(interval);
197 privateData->setFrequency(frequency);
198 JSAlarmRecurrencePriv *priv = new JSAlarmRecurrencePriv(context, privateData);
199 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
200 if (NULL == jsObjectRef) {
202 LogError("JSAlarmRecurrence object creation error");
209 JSValueRef JSAlarmRecurrence::getFrequency(JSContextRef ctx,
211 JSStringRef propertyName,
212 JSValueRef* exception)
214 AlarmConverter converter(ctx);
215 AlarmRecurrence::alarm_frequency_e frequency;
218 AlarmRecurrencePtr privateData = getPrivData(object);
219 frequency = privateData->getFrequency();
221 Catch(WrtDeviceApis::Commons::Exception)
223 LogWarning("trying to get incorrect value");
226 return converter.toJSValueRef(converter.toTizenValue(frequency));
229 bool JSAlarmRecurrence::setFrequency(JSContextRef ctx,
231 JSStringRef propertyName,
233 JSValueRef* exception)
235 AlarmConverter converter(ctx);
236 std::string frequencyStr;
238 if(!JSValueIsString(ctx, value))
245 AlarmRecurrencePtr privateData = getPrivData(object);
246 frequencyStr = converter.toString(value);
247 privateData->setFrequency(converter.toRecurrenceFrequency(frequencyStr));
249 Catch(WrtDeviceApis::Commons::Exception)
251 LogWarning("trying to get incorrect value");
257 JSValueRef JSAlarmRecurrence::getInterval(JSContextRef ctx,
259 JSStringRef propertyName,
260 JSValueRef* exception)
263 Converter converter(ctx);
266 AlarmRecurrencePtr privateData = getPrivData(object);
267 interval = privateData->getInterval();
269 Catch(WrtDeviceApis::Commons::Exception)
271 LogWarning("trying to get incorrect value");
274 return converter.toJSValueRef(interval);
278 bool JSAlarmRecurrence::setInterval(JSContextRef ctx,
280 JSStringRef propertyName,
282 JSValueRef* exception)
284 Converter converter(ctx);
286 if(!JSValueIsNumber(ctx, value))
293 AlarmRecurrencePtr privateData = getPrivData(object);
294 privateData->setInterval(converter.toInt(value));
296 Catch(WrtDeviceApis::Commons::Exception)
298 LogWarning("trying to get incorrect value");
303 JSValueRef JSAlarmRecurrence::getDaysOfTheWeek(JSContextRef ctx,
305 JSStringRef propertyName,
306 JSValueRef* exception)
308 // todo change to AlarmConverter
309 std::vector<std::string> daysOfTheWeek;
310 Converter converter(ctx);
313 AlarmRecurrencePtr privateData = getPrivData(object);
314 daysOfTheWeek = privateData->getDaysOfTheWeek();
316 Catch(WrtDeviceApis::Commons::Exception)
318 LogWarning("trying to get incorrect value");
321 return converter.toJSValueRef(daysOfTheWeek);
325 bool JSAlarmRecurrence::setDaysOfTheWeek(JSContextRef ctx,
327 JSStringRef propertyName,
329 JSValueRef* exception)
332 AlarmConverter converter(ctx);
333 std::vector<std::string> daysOfTheWeek;
337 AlarmRecurrencePtr privateData = getPrivData(object);
338 daysOfTheWeek = converter.toVectorOfStrings(value);
339 privateData->setDaysOfTheWeek(daysOfTheWeek);
341 Catch(WrtDeviceApis::Commons::Exception)
343 LogWarning("trying to get incorrect value");
346 return converter.toJSValueRef(daysOfTheWeek);
352 void convertDaysOfTheWeekToFlag(std::vector<string> daysOfTheWeek, char weekFlag[])
354 for( unsigned int i=0; i<daysOfTheWeek.size(); i++ )
356 if( daysOfTheWeek[i]=="SU" )
358 else if( daysOfTheWeek[i]=="MO" )
360 else if( daysOfTheWeek[i]=="TU" )
362 else if( daysOfTheWeek[i]=="WE" )
364 else if( daysOfTheWeek[i]=="TH" )
366 else if( daysOfTheWeek[i]=="FR" )
368 else if( daysOfTheWeek[i]=="SA" )
375 JSValueRef JSAlarmRecurrence::getId(JSContextRef ctx,
377 JSStringRef propertyName,
378 JSValueRef* exception)
380 Converter converter(ctx);
384 AlarmPtr privateData = getPrivData(object);
385 id = privateData->getId();
387 Catch(WrtDeviceApis::Commons::Exception)
389 LogWarning("trying to get incorrect value");
392 return converter.toJSValueRef(id);
397 JSValueRef JSAlarmRecurrence::getInterval(JSContextRef ctx,
399 JSStringRef propertyName,
400 JSValueRef* exception)
402 Converter converter(ctx);
406 AlarmPtr privateData = getPrivData(object);
407 interval = privateData->getInterval();
409 Catch(WrtDeviceApis::Commons::Exception)
411 LogWarning("trying to get incorrect value");
414 return converter.toJSValueRef(interval);