Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Alarm / JSAlarmRelative.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cassert>
18 #include <memory>
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"
30 #include <app.h>
31 #include <time.h>
32 #include "JSAlarmRecurrence.h"
33 #include "JSAbstractAlarm.h"
34 #include "JSAlarmRelative.h"
35
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Alarm {
40
41 using namespace std;
42 using namespace DPL;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45 using namespace TizenApis::Api::Alarm;
46
47 JSClassRef JSAlarmRelative::m_jsClassRef = NULL;
48
49 JSClassDefinition JSAlarmRelative::m_jsClassInfo = {
50                 0,
51                 kJSClassAttributeNone,
52                 "AlarmRelative",
53                 JSAbstractAlarm::getClassRef(),
54                 m_property,
55                 m_function,
56                 initialize,
57                 finalize,
58                 NULL, //hasProperty,
59                 NULL, //getProperty,
60                 NULL, //setProperty,
61                 NULL, //deleteProperty,Geolocation
62                 NULL, //getPropertyNames,
63                 NULL,
64                 constructor, // constructor
65                 hasInstance,
66                 NULL
67 };
68
69 JSStaticFunction JSAlarmRelative::m_function[] = { 
70         { "getNextScheduledDate",JSAlarmRelative::getNextScheduledDate,kJSPropertyAttributeNone },
71         { 0, 0, 0 }
72 };
73
74 JSStaticValue JSAlarmRelative::m_property[] = {
75         { "id", getId, NULL, kJSPropertyAttributeReadOnly },
76         { "recurrenceRule", getRecurrenceRule, NULL, kJSPropertyAttributeReadOnly },
77         { 0, 0, 0, 0 }                                                                                            
78 };
79
80 const JSClassRef JSAlarmRelative::getClassRef() 
81 {
82         if (!m_jsClassRef) {
83                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
84         }
85         return m_jsClassRef;
86 }
87
88 const JSClassDefinition* JSAlarmRelative::getClassInfo() 
89 {
90         return &m_jsClassInfo;
91 }
92
93 void JSAlarmRelative::initialize(JSContextRef context, JSObjectRef object) 
94 {
95
96 }
97 void JSAlarmRelative::finalize(JSObjectRef object) 
98 {
99
100 }
101
102 bool JSAlarmRelative::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
103 {
104     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
105 }
106
107
108 JSObjectRef JSAlarmRelative::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
109 {
110     #if 0
111     LogError("[ILOVEFILM] Enter constructor");
112     // 2 Param
113     // 1. Date, Recurrence
114     // 2. Delay, Recurrence
115     struct tm date;
116     AlarmConverter converter(ctx);
117     Validator validator(ctx);
118     AlarmRelative::alarm_type_e alarm_type;
119
120      bool isRecurrence = false;
121        
122     
123     if(argumentCount < 1)
124     {
125         // excpetion
126         LogWarning("Argument is not corrent");
127     }
128
129     int delay;
130     if(JSValueIsNumber(ctx, arguments[0]));
131     {
132         // delay
133         delay = converter.toInt(arguments[0]);
134         alarm_type = Alarm::ALARM_TYPE_DELAY;
135     }  
136     
137     if( validator.isDate(arguments[0]) == true)
138     {   
139         // date
140         date = converter.toDateTm(arguments[0]);
141         alarm_type = Alarm::ALARM_TYPE_DATE;
142     }
143
144     AlarmRecurrencePtr result;
145     
146     if(argumentCount == 2)
147     {
148         if(JSValueIsObjectOfClass(ctx, arguments[1], JSAlarmRecurrence::getClassRef()))
149         {
150              JSObjectRef recurreceObj = JSValueToObject(ctx, arguments[1], exception);
151             
152             JSAlarmRecurrencePriv*priv = static_cast<JSAlarmRecurrencePriv*>(JSObjectGetPrivate(recurreceObj));
153             if (!priv) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155             }
156
157             result = priv->getObject();
158             if (!result) {
159                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160             }
161         }
162         isRecurrence = true;
163     }
164
165     if(alarm_type == Alarm::ALARM_TYPE_DELAY)
166     {
167         if(isRecurrence == true) {
168             return JSValueToObject(ctx, createJSObject(ctx, delay, result), exception);
169         }
170         else
171         {
172             return JSValueToObject(ctx, createJSObject(ctx, delay,0), exception);
173         }    
174     }
175     else
176     {
177         if(isRecurrence == true) {
178             return JSValueToObject(ctx, createJSObject(ctx, date, result), exception);
179         }
180         else
181         {
182             return JSValueToObject(ctx, createJSObject(ctx, date), exception);
183         }
184     }
185 #endif
186
187 }
188 AlarmRelativePtr JSAlarmRelative::getPrivData(JSObjectRef object)
189 {
190         JSAlarmRelativePriv *priv = static_cast<JSAlarmRelativePriv*>(JSObjectGetPrivate(object));
191         if (!priv) {
192                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
193         }
194         AlarmRelativePtr result = priv->getObject();
195         if (!result) {
196                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
197         }
198         return result;
199 }
200
201
202 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, const int id)
203 {
204     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
205     
206     privateData->setId(id);
207
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);
213     }
214     return jsValueRef;
215 }
216
217 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, struct tm date, AlarmRecurrencePtr ptr)
218 {
219     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
220
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);
226     }
227     return jsValueRef;
228
229 }
230 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, AlarmRecurrencePtr ptr)
231 {
232     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
233
234     privateData->setDelay(delay);
235     privateData->setRecurrence(ptr);
236     
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);
242     }
243     return jsValueRef;
244 }
245
246
247 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, struct tm date)
248 {
249     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
250    
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);
256     }
257     return jsValueRef;
258
259 }
260 JSValueRef JSAlarmRelative::createJSObject(JSContextRef context, int delay, int dummy)
261 {
262     AlarmRelativePtr privateData = AlarmRelativePtr(new AlarmRelative());
263     privateData->setDelay(delay);
264    
265     JSAlarmRelativePriv *priv = new JSAlarmRelativePriv(context, privateData);
266
267     JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
268     if (NULL == jsValueRef) {
269         LogError("object creation error");
270         return JSValueMakeUndefined(context);
271     }
272     return jsValueRef;
273 }
274
275 JSValueRef JSAlarmRelative::getNextScheduledDate(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
276
277     struct tm date;
278     Converter converter(ctx);
279     int id;
280     Try
281     {
282         AlarmRelativePtr privateData = getPrivData(object);
283         id = privateData->getId();
284     }
285     Catch(WrtDeviceApis::Commons::Exception)
286     {
287         LogWarning("trying to get incorrect value");
288     }
289     
290     int err = alarm_get_scheduled_date(id, &date);
291
292     JSValueRef result = converter.toJSValueRef(date);
293
294     if(err != ALARM_ERROR_NONE)
295     {
296         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
297     }
298     return result;      
299 }
300
301 JSValueRef JSAlarmRelative::getId(JSContextRef ctx,
302                 JSObjectRef object,
303                 JSStringRef propertyName,
304                 JSValueRef* exception)
305 {
306     Converter converter(ctx);
307     int id;
308     Try
309     {
310         AlarmRelativePtr privateData = getPrivData(object);
311         id = privateData->getId();
312     }
313     Catch(WrtDeviceApis::Commons::Exception)
314     {
315         LogWarning("trying to get incorrect value");
316     }
317
318     return converter.toJSValueRef(id);
319 }
320
321
322 JSValueRef JSAlarmRelative::getRecurrenceRule(JSContextRef ctx,
323                 JSObjectRef object,
324                 JSStringRef propertyName,
325                 JSValueRef* exception)
326 {
327     #if 0
328     Converter converter(ctx);
329     int interval;
330     Try
331     {
332         AlarmPtr privateData = getPrivData(object);
333         interval = privateData->getRecurrence();
334     }
335     Catch(WrtDeviceApis::Commons::Exception)
336     {
337         LogWarning("trying to get incorrect value");
338     }
339
340     return converter.toJSValueRef(interval);
341     #endif
342
343 }
344
345 } // Alarm
346 } // Tizen1_0 
347 } // TizenApis
348
349