Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Alarm / JSAlarmRecurrence.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 <Commons/Exception.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include "AlarmRecurrence.h"
26 #include <app.h>
27 #include "JSAlarmRecurrence.h"
28 #include "AlarmConverter.h"
29
30 namespace TizenApis {
31 namespace Tizen1_0 {
32 namespace Alarm {
33
34 using namespace std;
35 using namespace DPL;
36 using namespace WrtDeviceApis::Commons;
37 using namespace WrtDeviceApis::CommonsJavaScript;
38 using namespace TizenApis::Commons;
39
40
41 JSClassRef JSAlarmRecurrence::m_jsClassRef = NULL;
42
43 JSClassDefinition JSAlarmRecurrence::m_jsClassInfo = {
44                 0,
45                 kJSClassAttributeNone,
46                 "JSAlarmRecurrence",
47                 NULL,
48                 m_property,
49                 NULL,
50                 initialize,
51                 finalize,
52                 NULL, //hasProperty,
53                 NULL, //getProperty,
54                 NULL, //setProperty,
55                 NULL, //deleteProperty
56                 NULL, //getPropertyNames,
57                 NULL,
58                 constructor, // constructor
59                 hasInstance,
60                 NULL
61 };
62
63 JSStaticValue JSAlarmRecurrence::m_property[] = {
64         { "frequency", getFrequency, setFrequency, kJSPropertyAttributeNone },
65         { "interval", getInterval, setInterval, kJSPropertyAttributeNone },
66         { "daysOfTheWeek", getDaysOfTheWeek, setDaysOfTheWeek, kJSPropertyAttributeNone },
67         { 0, 0, 0, 0 }                                                                                            
68 };
69
70 const JSClassRef JSAlarmRecurrence::getClassRef() 
71 {
72         if (!m_jsClassRef) {
73                 m_jsClassRef = JSClassCreate(&m_jsClassInfo);
74         }
75         return m_jsClassRef;
76 }
77
78 const JSClassDefinition* JSAlarmRecurrence::getClassInfo() 
79 {
80         return &m_jsClassInfo;
81 }
82
83 void JSAlarmRecurrence::initialize(JSContextRef context, JSObjectRef object) 
84 {
85
86 }
87
88 void JSAlarmRecurrence::finalize(JSObjectRef object) 
89 {
90
91 }
92
93 JSObjectRef JSAlarmRecurrence::constructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
94 {
95     // Parameter can be 1 or 2.
96         
97     // 1. Parameter : AlarmFrequency
98     // 2. Parameter : interval
99     // 3. Parameter : ByDayValue[]
100     AlarmConverter converter(ctx);
101
102     // return : Should return AlarmRecurrence object
103     if(argumentCount < 1)
104     {
105         return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
106     }
107
108     if(argumentCount == 1)
109     {
110         // daysOfTheweek
111         // convert javascript array to string array
112         // How can i check javascript
113         if(!JSIsArrayValue(ctx, arguments[0]))
114         {
115             // exception.
116             return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
117         }
118         std::vector<std::string> temp =  converter.toVectorOfStrings(arguments[0]);
119         JSObjectRef obj = createJSObject(ctx, temp);
120         return obj;
121     }
122     else
123     {
124         std::string frequencyStr;
125         int interval;
126         AlarmRecurrence::alarm_frequency_e  frequency;
127         // Should frequency & interval
128         if(!JSValueIsString(ctx, arguments[0]))
129         {
130             return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
131         }
132         frequencyStr = converter.toString(arguments[0]);
133
134         frequency = converter.toRecurrenceFrequency(frequencyStr);
135
136         if(frequency == AlarmRecurrence::NO_RECURRENCE)
137         {
138             return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
139         }
140
141         if(JSValueIsNumber(ctx, arguments[1]))
142         {
143             return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
144         }
145
146         interval = converter.toInt(arguments[1]);
147
148         if(interval <=0)
149         {
150             return converter.toJSObjectRef(TizenApis::Commons::JSTizenExceptionFactory::postException(ctx, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR));
151         }
152
153         JSObjectRef obj = createJSObject(ctx, frequency, interval);
154         return obj;
155        
156     }
157         
158 }
159
160 bool JSAlarmRecurrence::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception)
161 {
162     return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
163 }
164
165 AlarmRecurrencePtr JSAlarmRecurrence::getPrivData(JSObjectRef object)
166 {
167         JSAlarmRecurrencePriv*priv = static_cast<JSAlarmRecurrencePriv*>(JSObjectGetPrivate(object));
168         if (!priv) {
169                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
170         }
171     
172         AlarmRecurrencePtr result = priv->getObject();
173         if (!result) {
174                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
175         }
176         return result;
177 }
178
179 JSObjectRef JSAlarmRecurrence::createJSObject(JSContextRef context, std::vector<std::string> daysOfTheWeek)
180 {
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) {
186         // Exception
187         LogError("JSAlarmRecurrence object creation error");
188         
189     }
190     return jsObjectRef;    
191 }
192
193 JSObjectRef JSAlarmRecurrence::createJSObject(JSContextRef context, AlarmRecurrence::alarm_frequency_e frequency, int interval)
194 {
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) {
201         // Exception
202         LogError("JSAlarmRecurrence object creation error");
203         
204     }
205     return jsObjectRef;    
206 }
207
208
209 JSValueRef JSAlarmRecurrence::getFrequency(JSContextRef ctx,
210                                         JSObjectRef object,
211                                         JSStringRef propertyName,
212                                         JSValueRef* exception)
213 {
214     AlarmConverter converter(ctx);
215     AlarmRecurrence::alarm_frequency_e frequency;
216     Try
217     {
218         AlarmRecurrencePtr privateData = getPrivData(object);
219         frequency = privateData->getFrequency();
220     }
221     Catch(WrtDeviceApis::Commons::Exception)
222     {
223         LogWarning("trying to get incorrect value");
224     }
225
226     return converter.toJSValueRef(converter.toTizenValue(frequency));   
227 }
228
229 bool JSAlarmRecurrence::setFrequency(JSContextRef ctx,
230                                         JSObjectRef object,
231                                         JSStringRef propertyName,
232                                         JSValueRef value,
233                                         JSValueRef* exception)
234 {
235     AlarmConverter converter(ctx);
236     std::string frequencyStr;
237     
238     if(!JSValueIsString(ctx, value))
239     {
240         return false;    
241     }
242     
243     Try
244     {
245         AlarmRecurrencePtr privateData = getPrivData(object);
246         frequencyStr = converter.toString(value);
247         privateData->setFrequency(converter.toRecurrenceFrequency(frequencyStr));
248     }
249     Catch(WrtDeviceApis::Commons::Exception)
250     {
251         LogWarning("trying to get incorrect value");
252     }
253
254     return true;
255 }
256                 
257 JSValueRef JSAlarmRecurrence::getInterval(JSContextRef ctx,
258                                         JSObjectRef object,
259                                         JSStringRef propertyName,
260                                         JSValueRef* exception)
261 {
262     int interval;
263     Converter converter(ctx);
264     Try
265     {
266         AlarmRecurrencePtr privateData = getPrivData(object);
267         interval = privateData->getInterval();
268     }
269     Catch(WrtDeviceApis::Commons::Exception)
270     {
271         LogWarning("trying to get incorrect value");
272     }
273
274     return converter.toJSValueRef(interval);
275
276 }
277
278 bool JSAlarmRecurrence::setInterval(JSContextRef ctx,
279                                         JSObjectRef object,
280                                         JSStringRef propertyName,
281                                         JSValueRef value,
282                                         JSValueRef* exception)
283 {
284     Converter converter(ctx);
285
286     if(!JSValueIsNumber(ctx, value))
287     {
288         return false;
289     }
290     
291     Try
292     {
293         AlarmRecurrencePtr privateData = getPrivData(object);
294         privateData->setInterval(converter.toInt(value));
295     }
296     Catch(WrtDeviceApis::Commons::Exception)
297     {
298         LogWarning("trying to get incorrect value");
299     }
300     return true;
301     
302 }
303 JSValueRef JSAlarmRecurrence::getDaysOfTheWeek(JSContextRef ctx,
304                                         JSObjectRef object,
305                                         JSStringRef propertyName,
306                                         JSValueRef* exception)
307 {
308     // todo change to AlarmConverter
309     std::vector<std::string> daysOfTheWeek;
310     Converter converter(ctx);
311     Try
312     {
313         AlarmRecurrencePtr privateData = getPrivData(object);
314         daysOfTheWeek = privateData->getDaysOfTheWeek();
315     }
316     Catch(WrtDeviceApis::Commons::Exception)
317     {
318         LogWarning("trying to get incorrect value");
319     }
320
321     return converter.toJSValueRef(daysOfTheWeek);
322
323 }
324
325 bool JSAlarmRecurrence::setDaysOfTheWeek(JSContextRef ctx,
326                                         JSObjectRef object,
327                                         JSStringRef propertyName,
328                                         JSValueRef value,
329                                         JSValueRef* exception)
330 {
331
332     AlarmConverter converter(ctx);
333     std::vector<std::string> daysOfTheWeek;
334
335     Try
336     {
337         AlarmRecurrencePtr privateData = getPrivData(object);
338         daysOfTheWeek = converter.toVectorOfStrings(value);
339         privateData->setDaysOfTheWeek(daysOfTheWeek);
340      }
341     Catch(WrtDeviceApis::Commons::Exception)
342     {
343         LogWarning("trying to get incorrect value");
344     }
345
346     return converter.toJSValueRef(daysOfTheWeek);
347
348 }
349
350
351 #if 0
352 void convertDaysOfTheWeekToFlag(std::vector<string> daysOfTheWeek, char weekFlag[])
353 {
354     for( unsigned int i=0; i<daysOfTheWeek.size(); i++ )
355     {
356         if( daysOfTheWeek[i]=="SU" )
357             weekFlag[0] = '1';
358         else if( daysOfTheWeek[i]=="MO" )
359             weekFlag[1] = '1';
360         else if( daysOfTheWeek[i]=="TU" )
361             weekFlag[2] = '1';
362         else if( daysOfTheWeek[i]=="WE" )
363             weekFlag[3] = '1';
364         else if( daysOfTheWeek[i]=="TH" )
365             weekFlag[4] = '1';
366         else if( daysOfTheWeek[i]=="FR" )
367             weekFlag[5] = '1';
368         else if( daysOfTheWeek[i]=="SA" )
369             weekFlag[6] = '1';
370     }
371 }
372 #endif
373
374 #if 0
375 JSValueRef JSAlarmRecurrence::getId(JSContextRef ctx,
376                 JSObjectRef object,
377                 JSStringRef propertyName,
378                 JSValueRef* exception)
379 {
380     Converter converter(ctx);
381     int id;
382     Try
383     {
384         AlarmPtr privateData = getPrivData(object);
385         id = privateData->getId();
386     }
387     Catch(WrtDeviceApis::Commons::Exception)
388     {
389         LogWarning("trying to get incorrect value");
390     }
391
392     return converter.toJSValueRef(id);
393
394 }
395
396
397 JSValueRef JSAlarmRecurrence::getInterval(JSContextRef ctx,
398                 JSObjectRef object,
399                 JSStringRef propertyName,
400                 JSValueRef* exception)
401 {
402     Converter converter(ctx);
403     int interval;
404     Try
405     {
406         AlarmPtr privateData = getPrivData(object);
407         interval = privateData->getInterval();
408     }
409     Catch(WrtDeviceApis::Commons::Exception)
410     {
411         LogWarning("trying to get incorrect value");
412     }
413
414     return converter.toJSValueRef(interval);
415
416 }
417
418 #endif
419
420 } // Alarm
421 } // Tizen1_0 
422 } // TizenApis
423
424