Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Alarm / JSAlarmManager.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 <dpl/log/log.h>
18 #include <vector>
19 #include <app.h>
20 #include <time.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/Validator.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
25 #include <Tizen/Common/SecurityExceptions.h>
26 #include <Commons/Exception.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29
30 #include "AlarmAbsolute.h"
31 #include "AlarmConverter.h"
32 #include "JSAlarmManager.h"
33 #include "JSAlarmAbsolute.h"
34
35 using namespace std;
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Alarm {
40
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43 using namespace TizenApis::Tizen1_0::Alarm;
44 using namespace TizenApis::Api::Alarm;
45
46 #if 0
47 using TizenApis::Api::Alarm::Alarm;
48 using TizenApis::Tizen1_0::Alarm::JSAlarm;
49 class JSAlarm;
50 #endif
51
52 #if 0
53 using namespace std;
54 using namespace DPL;
55 using namespace TizenApis::Api::Alarm;
56 using namespace WrtDeviceApis;
57 using namespace WrtDeviceApis::CommonsJavaScript;
58 #endif
59
60
61 static bool alarm_iterate_callback(int alarm_id, void *user_data)
62 {
63     vector<int> *alarmIds = reinterpret_cast<vector<int>*>(user_data);
64    
65     alarmIds->push_back(alarm_id);
66     return true;
67 }
68
69
70 JSClassRef JSAlarmManager::m_jsClassRef = NULL;
71
72 JSClassDefinition JSAlarmManager::m_jsClassInfo = {
73         0,
74         kJSClassAttributeNone,
75         "AlarmManager",
76         NULL,
77         NULL,
78         m_function,
79         initialize,
80         finalize,
81         NULL, //hasProperty,
82         NULL, //getProperty,
83         NULL, //setProperty,
84         NULL, //deleteProperty,Geolocation
85         NULL, //getPropertyNames,
86         NULL,
87         NULL, // constructor
88         hasInstance,
89         NULL
90 };
91
92 JSStaticFunction JSAlarmManager::m_function[] = {    
93         { "add",JSAlarmManager::add,kJSPropertyAttributeNone },
94         { "remove",JSAlarmManager::remove,kJSPropertyAttributeNone },
95         { "getAlarms",JSAlarmManager::getAlarms,kJSPropertyAttributeNone },
96         { 0, 0, 0 }
97 };
98
99 const JSClassRef JSAlarmManager::getClassRef() 
100 {
101     if (!m_jsClassRef) {
102         m_jsClassRef = JSClassCreate(&m_jsClassInfo);
103     }
104     return m_jsClassRef;
105 }
106
107 const JSClassDefinition* JSAlarmManager::getClassInfo() 
108 {
109     return &m_jsClassInfo;
110 }
111
112 void JSAlarmManager::initialize(JSContextRef ctx, JSObjectRef object) 
113 {
114     
115 }
116 void JSAlarmManager::finalize(JSObjectRef object) 
117 {
118         
119 }
120
121 bool JSAlarmManager::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
122     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
123 }
124
125 JSValueRef JSAlarmManager::add(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
126 {
127
128     #if 0
129     service_h service;
130     char *package;
131     Alarm::alarm_type_e type = Alarm::ALARM_TYPE_INVALID;
132     struct tm startDate;
133     int delay;
134     AlarmRecurrencePtr recurrencePtr;
135     int interval = 0;
136     int id;
137     
138     LogError("[ILOVEFILM] Enter add");
139     
140     Validator check(ctx, exception);
141     AlarmConverter converter(ctx);
142
143     LogError("[ILOVEFILM] Enter add2");
144     if(argumentCount < 2) {
145         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146     } 
147     LogError("[ILOVEFILM] Enter add3");
148     if(!JSValueIsObjectOfClass(ctx, arguments[0], JSAlarmAbsolute::getClassRef())) {
149        ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
150     }
151     
152     // need to know
153     // 1. type(delay, date)
154     // 2. weekly or time
155     // 3. convert freqeuncy to second    
156
157    // 1. GetAlarmPrivate Data
158     LogError("[ILOVEFILM] Enter add4");
159     JSObjectRef alarmObj = JSValueToObject(ctx, arguments[0], exception);
160     
161     LogError("[ILOVEFILM] Enter add5");
162     JSAlarmPriv *priv = static_cast<JSAlarmPriv*>(JSObjectGetPrivate(alarmObj));
163     if (!priv) {
164         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165     }
166     LogError("[ILOVEFILM] Enter add6");
167     AlarmAbsolutePtr alarmPtr = priv->getObject();
168     if (!alarmPtr) {
169         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
170     } 
171
172     LogError("[ILOVEFILM] Enter add7");
173
174     type = alarmPtr->getAlarmType();
175     if(type == Alarm::ALARM_TYPE_DELAY)
176     {
177         LogError("[ILOVEFILM] Enter add8 get Date");
178         delay = alarmPtr->getDelay();
179     }
180     else if(type == Alarm::ALARM_TYPE_DATE)
181     {
182          LogError("[ILOVEFILM] Enter add8 get Delay");
183         startDate = alarmPtr->getDate();
184
185     }
186
187     if(type == Alarm::ALARM_TYPE_DELAY)
188     {
189         service = converter.toService(converter.toString(arguments[1]));
190         if(alarmPtr->isRecurrence())
191         {
192             recurrencePtr = alarmPtr->getRecurrence();
193         }
194         else
195         {
196             int err = alarm_schedule_after_delay(service, delay, 0, &id);
197             alarmPtr->setId(id);
198         }      
199         
200     } else if(type == Alarm::ALARM_TYPE_DATE)
201     {
202         LogError("[ILOVEFILM] Enter add9");
203         service = converter.toService(converter.toString(arguments[1]));
204         if(alarmPtr->isRecurrence())
205         {
206             LogError("[ILOVEFILM] Enter add10");
207             recurrencePtr = alarmPtr->getRecurrence();
208             int err = alarm_schedule_at_date(service, &startDate, 0, &id);
209         }
210         else
211         {
212             char *package;
213             service_get_package(service, &package);
214             struct tm current;
215             alarm_get_current_time(&current);
216             LogError("[ILOVEFILM] Current Date  = " << "Sec : " << current.tm_sec << "  Min : "<< current.tm_min
217                 << "  Hour : " << current.tm_hour << "  Day : " << current.tm_mday << "  MON : " << current.tm_mon 
218                 << "  Year : " <<  current.tm_year);
219
220             LogError("[ILOVEFILM] Enter add11 service package = " << package);
221             LogError("[ILOVEFILM] Date  = " << "Sec : " << startDate.tm_sec << "  Min : "<< startDate.tm_min
222                 << "  Hour : " << startDate.tm_hour << "  Day : " << startDate.tm_mday << "  MON : " << startDate.tm_mon 
223                 << "  Year : " <<  startDate.tm_year);
224             int err = alarm_schedule_at_date(service, &startDate, 0, &id);
225             LogError("[ILOVEFILM] Enter add12 error code " << err );
226
227            
228             LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_INVALID_PARAMETER = " << ALARM_ERROR_INVALID_PARAMETER );
229             LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_INVALID_DATE = " << ALARM_ERROR_INVALID_DATE );
230             LogError("[ILOVEFILM] Enter add12 error code ALARM_ERROR_CONNECTION_FAIL = " << ALARM_ERROR_CONNECTION_FAIL );
231             
232         }
233         
234         alarmPtr->setId(id);
235     }
236     #endif
237  
238 }
239
240
241 JSValueRef JSAlarmManager::remove(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
242 {
243     int err;
244
245     if(argumentCount < 1 || !JSValueIsString(ctx, arguments[0])) 
246         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
247
248     Converter converter(ctx);
249     // todo : check exception or error
250     string stringID = converter.toString(arguments[0]);
251     
252     int id = atoi(stringID.c_str());
253
254     err = alarm_cancel(id);
255
256     if(err != ALARM_ERROR_NONE)
257     {
258         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
259     }
260
261     return JSValueMakeUndefined(ctx);
262
263 }
264
265
266 JSValueRef JSAlarmManager::getAlarms(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
267 {
268     int error;
269     vector<int> alarmIds;
270     error = alarm_foreach_registered_alarm(alarm_iterate_callback, &alarmIds);
271
272     if(error != ALARM_ERROR_NONE)
273     {
274         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
275     }
276
277 #if 0
278     AlarmArrayPtr alarmObjectArrary;
279     for(int i = 0; i < alarmIds.size(); i++)
280     {
281         TizenApis::Tizen1_0::Alarm::JSAlarmAbsolute::createJSObject(ctx, alarmIds[i]);
282     } 
283 #endif
284     
285 }
286
287 #if 0
288 JSValueRef JSAlarmManager::createAlarm(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
289 {
290     struct tm date;
291     int id;
292     service_h service;
293     char *package;
294
295     Validator check(ctx,exception);
296
297     if(argumentCount < 3) {
298         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
299     }
300
301     if(JSValueIsNumber(ctx, arguments[1])) {
302         return createAfterDelay(ctx, object, thisObject, argumentCount, arguments, exception);
303     }
304
305     if(check.isCallback(arguments[0]) | check.isNullOrUndefined(arguments[0])) {
306         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
307     }
308
309     if(!check.isDate(arguments[1])) {
310         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
311     }
312
313     if(!JSValueIsNumber(ctx, arguments[2])) {
314         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
315     }
316     
317     AlarmConverter alarmConverter(ctx);
318     date = alarmConverter.toDateTm(arguments[1]);
319     int period = (int)JSValueToNumber(ctx, arguments[2], exception);
320     service = alarmConverter.toService(arguments[0]);
321     int err = alarm_schedule_at_date(service, &date, period, &id);
322     
323     service_destroy(service);
324     
325     if(err != ALARM_ERROR_NONE) {
326         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
327     }
328     
329     return alarmConverter.toJSValueRef(id);
330 }
331
332
333
334 JSValueRef JSAlarmManager::createAfterDelay(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
335 {
336     struct tm date;
337     int id;
338     service_h service;
339     char *package;
340
341     Validator check(ctx,exception);
342
343     if(argumentCount < 3) {
344         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
345     }
346
347     if(check.isCallback(arguments[0]) | check.isNullOrUndefined(arguments[0])) {
348         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
349     }
350
351     if( !JSValueIsNumber(ctx, arguments[1])) {
352         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
353     }
354
355     if(!JSValueIsNumber(ctx, arguments[2])) {
356         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
357     }
358
359     AlarmConverter alarmConverter(ctx);
360     int delay = (int)JSValueToNumber(ctx, arguments[1], exception);
361     int period = (int)JSValueToNumber(ctx, arguments[2], exception);
362     service = alarmConverter.toService(arguments[0]);
363
364     int err = alarm_schedule_after_delay(service, delay, period, &id);
365
366     service_destroy(service);
367
368     if(err != ALARM_ERROR_NONE) {
369         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
370     }
371     
372     return alarmConverter.toJSValueRef(id);
373 }
374
375 JSValueRef JSAlarmManager::cancelAlarm(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
376 {
377     int err;
378
379     if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0])) 
380         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
381
382     int id = (int)JSValueToNumber(ctx, arguments[0], exception);
383
384     err = alarm_cancel(id);
385
386     if(err != ALARM_ERROR_NONE)
387     {
388         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
389     }
390
391     return JSValueMakeUndefined(ctx);
392 }
393
394
395 JSValueRef JSAlarmManager::getAlarmIDs(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
396 {
397     vector<int> alarmIds;
398     alarm_foreach_registered_alarm(alarm_iterate_callback, &alarmIds);
399     
400     Converter converter(ctx);
401     return converter.toJSValueRef(alarmIds);    
402 }
403
404
405 JSValueRef JSAlarmManager::getNextScheduledDate(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
406 {
407     if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0])) 
408         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
409     
410     struct tm date;
411     Converter converter(ctx);
412     int id = (int)JSValueToNumber(ctx, arguments[0], exception);
413     int err = alarm_get_scheduled_date(id, &date);
414
415     JSValueRef result = converter.toJSValueRef(date);
416     
417     if(err != ALARM_ERROR_NONE)
418     {
419         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
420     }
421
422     return result;
423 }
424
425
426 JSValueRef JSAlarmManager::getReccurentSecond(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
427 {
428     if(argumentCount < 1 || !JSValueIsNumber(ctx, arguments[0])) 
429         return JSDOMExceptionFactory::TypeMismatchException.make(ctx, exception);
430
431     int period;
432     Converter converter(ctx);
433     int id = (int)JSValueToNumber(ctx, arguments[0], exception);
434     int err = alarm_get_scheduled_period(id, &period);
435     if(err != ALARM_ERROR_NONE)
436     {
437         return JSDOMExceptionFactory::UnknownException.make(ctx, exception);
438     }
439     
440     return converter.toJSValueRef(period);
441 }
442 #endif
443
444 } // Alarm
445 } // Tizen1_0 
446 } // TizenApis
447