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.
18 #include <CommonsJavaScript/Converter.h>
19 #include <CommonsJavaScript/Validator.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/Utils.h>
23 #include <SecurityExceptions.h>
24 #include <JSTizenExceptionFactory.h>
25 #include <JSTizenException.h>
26 #include "NotificationFactory.h"
27 #include "JSStatusNotification.h"
28 #include "NotificationConverter.h"
30 //#include "plugin_config.h"
32 using namespace DeviceAPI::Common;
35 namespace Notification {
38 JSClassDefinition JSStatusNotification::m_classInfo =
41 kJSClassAttributeNone,
59 JSStaticValue JSStatusNotification::m_properties[] =
61 {NOTIFICATION_ID, JSStatusNotification::getProperty, NULL, kJSPropertyAttributeReadOnly },
62 {NOTIFICATION_TYPE, JSStatusNotification::getProperty, NULL, kJSPropertyAttributeReadOnly },
63 {NOTIFICATION_POSTED_TIME, JSStatusNotification::getProperty, NULL, kJSPropertyAttributeReadOnly },
64 {NOTIFICATION_TITLE, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
65 {NOTIFICATION_CONTENT, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
67 {NOTIFICATION_STATUS_TYPE, JSStatusNotification::getProperty, NULL, kJSPropertyAttributeReadOnly },
68 {NOTIFICATION_ICON_PATH, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
69 {NOTIFICATION_SOUND_PATH, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
70 {NOTIFICATION_VIBRATION, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
71 #ifdef APPLICATION_API_BACKWARD_COMPATIBILITY
72 {NOTIFICATION_SEVICE, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
74 {NOTIFICATION_APP_CONTROL, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
75 {NOTIFICATION_APP_ID, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
76 {NOTIFICATION_PROGRESS_VALUE, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
77 {NOTIFICATION_LINES, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
78 {NOTIFICATION_BACKGROUND_IMAGE_PATH, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
79 {NOTIFICATION_NUMBER, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
80 {NOTIFICATION_THUMBNAILS, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
81 {NOTIFICATION_SUB_ICON_PATH, JSStatusNotification::getProperty, JSStatusNotification::setProperty, kJSPropertyAttributeNone },
85 const JSClassRef JSStatusNotification::getClassRef()
88 m_jsClassRef = JSClassCreate(&m_classInfo);
93 const JSClassDefinition* JSStatusNotification::getClassInfo()
98 JSClassRef JSStatusNotification::m_jsClassRef = JSClassCreate(JSStatusNotification::getClassInfo());
100 void JSStatusNotification::initialize(JSContextRef context, JSObjectRef object)
102 LogDebug("JSStatusNotification::initialize, nothing ");
105 void JSStatusNotification::finalize(JSObjectRef object)
107 JSStatusNotificationPriv* priv = static_cast<JSStatusNotificationPriv*>(JSObjectGetPrivate(object));
109 LogDebug("JSStatusNotification::Finalize");
113 JSObjectSetPrivate(object, NULL);
118 bool JSStatusNotification::hasInstance(JSContextRef context, JSObjectRef constructor,
119 JSValueRef possibleInstance, JSValueRef* exception)
121 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
124 bool JSStatusNotification::setProperty(JSContextRef context,
126 JSStringRef propertyName,
128 JSValueRef* exception)
132 JSStatusNotificationPriv* priv = static_cast<JSStatusNotificationPriv*>(JSObjectGetPrivate(object));
133 //NotificationConverter converter(context);
134 NotificationConverterFactory::ConverterType converter =
135 NotificationConverterFactory::getConverter(context);
136 std::string property = converter->toString(propertyName);
137 LogDebug("Property = " << property);
142 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"Private Object is null");
146 INotificationPtr notification(priv->getObject());
148 if (notification == NULL)
150 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"Private Object is null");
153 if (property == NOTIFICATION_TITLE)
155 std::string title = converter->toString(value);
156 notification->setTitle(title);
159 else if (property == NOTIFICATION_CONTENT)
161 std::string content = converter->toString(value);
162 notification->setContent(content);
166 else if (property == NOTIFICATION_ICON_PATH)
168 if (JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
173 std::string iconPath = converter->toString(value);
174 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(context, iconPath);
175 iconPath = path->getFullPath();
177 notification->setIconPath(iconPath);
181 else if (property == NOTIFICATION_SOUND_PATH)
183 if (JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
188 std::string soundPath = converter->toString(value);
189 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(context, soundPath);
190 soundPath = path->getFullPath();
192 notification->setSoundPath(soundPath);
195 else if (property == NOTIFICATION_VIBRATION)
197 bool virbration = converter->toBool(value);
198 notification->setDefaultVibration(virbration);
201 else if (property == NOTIFICATION_APP_CONTROL
202 #ifdef APPLICATION_API_BACKWARD_COMPATIBILITY
203 || property == NOTIFICATION_SEVICE
207 DeviceAPI::Application::ApplicationControlPtr appControl = converter->toIApplicationPtr(value);
208 notification->setApplicationControl(appControl);
211 else if (property == NOTIFICATION_APP_ID)
213 std::string appId = converter->toString(value);
214 notification->setApplicationId(appId);
216 else if (property == NOTIFICATION_PROGRESS_VALUE)
218 unsigned short progressValue = (unsigned short)converter->toULong(value);
219 NotificationProgressType progressType = notification->getProgressType();
221 LogDebug(" progress type = " << progressType << "progressValue = " << progressValue);
223 if ( NOTI_PROGRESS_TYPE_SIZE == progressType)
225 notification->setProgressValue(progressValue);
227 else if ( NOTI_PROGRESS_TYPE_PERCENTAGE == progressType)
230 if ( progressValue > 100 )
232 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "progress value error");
235 double percentage = (double)progressValue/(double)100;
236 LogDebug(" Percentage Value = " << percentage);
237 notification->setProgressValue(percentage);
243 else if (property == NOTIFICATION_LINES)
245 NotificationLineArrayPtr lines = converter->toNotificationLineArray(value);
246 notification->setLines(lines);
248 else if (property == NOTIFICATION_BACKGROUND_IMAGE_PATH )
250 if (JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
255 std::string backgroundImagePath = converter->toString(value);
256 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(context, backgroundImagePath);
257 backgroundImagePath = path->getFullPath();
258 notification->setBackground(backgroundImagePath);
261 else if (property == NOTIFICATION_NUMBER)
263 unsigned int number = converter->toULong(value);
264 LogDebug("Number is = " << number);
265 notification->setNumber(number);
267 else if (property == NOTIFICATION_THUMBNAILS)
270 StringArrayPtr thumbnailsPtr = converter->toStringArray(value);
271 notification->setThumbnails(thumbnailsPtr);
273 std::vector<std::string>::iterator it;
276 for (it = thumbnailsPtr->begin(); it < thumbnailsPtr->end(); ++it)
278 std::string str = *it;
279 LogDebug(" thumbs : " << str);
280 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(context, str);
281 str = path->getFullPath();
283 LogDebug(" thumbs : " << str);
284 notification->setThumbnail(str, idx);
289 else if (property == NOTIFICATION_SUB_ICON_PATH )
291 if (JSValueIsUndefined(context, value) || JSValueIsNull(context, value))
296 std::string subIconPath = converter->toString(value);
297 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(context, subIconPath);
298 subIconPath = path->getFullPath();
299 notification->setSubIconPath(subIconPath);
304 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"No attribute");
308 Catch (WrtDeviceApis::Commons::ConversionException)
310 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
311 return JSTizenExceptionFactory::postException(context, exception,
312 JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
315 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
317 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
318 return JSTizenExceptionFactory::postException(context, exception,
319 JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
321 Catch(WrtDeviceApis::Commons::UnsupportedException)
323 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
324 return JSTizenExceptionFactory::postException(context, exception,
325 JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
327 Catch (WrtDeviceApis::Commons::Exception)
329 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
330 return JSTizenExceptionFactory::postException(context, exception,
331 JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
337 JSValueRef JSStatusNotification::getProperty(JSContextRef context,
339 JSStringRef propertyName,
340 JSValueRef* exception)
344 JSStatusNotificationPriv* priv = static_cast<JSStatusNotificationPriv*>(JSObjectGetPrivate(object));
345 //NotificationConverter converter(context);
346 NotificationConverterFactory::ConverterType converter =
347 NotificationConverterFactory::getConverter(context);
348 std::string property = converter->toString(propertyName);
350 LogDebug("property = " << property);
356 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"Private Object is null");
359 INotificationPtr notification(priv->getObject());
361 if (notification == NULL)
363 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"Private Object is null");
366 if (property == NOTIFICATION_ID)
368 LogDebug(" ID : " << notification->getID());
369 int id = notification->getID();
372 std::stringstream stream;
375 return converter->toJSValueRef(stream.str());
379 return JSValueMakeNull(context);
382 return converter->toJSValueRef(notification->getID());
384 else if (property == NOTIFICATION_TYPE)
386 LogDebug(" TYPE : " << notification->getNotiType());
387 return converter->toJSValueRef(notification->getNotiType());
389 else if (property == NOTIFICATION_POSTED_TIME)
391 LogDebug("notification =" << notification) ;
392 time_t postedTime = notification->getPostedTime();
394 LogDebug("postedtime = " << postedTime);
397 return converter->toJSValueRef(postedTime);
399 return JSValueMakeNull(context);
401 else if (property == NOTIFICATION_TITLE)
403 std::string title = notification->getTitle();
404 LogDebug(" Title : " << title);
405 return converter->toJSValueRef(title);
407 else if (property == NOTIFICATION_CONTENT)
409 std::string content = notification->getContent();
411 if (content.size() == 0)
412 return JSValueMakeNull(context);
414 LogDebug(" Content : " << content);
416 return converter->toJSValueRef(content);
418 else if (property == NOTIFICATION_STATUS_TYPE)
420 //std::string statusType = notification->getStatusType();
421 LogDebug(" NOTIFICATION_STATUS_TYPE : " << notification->getNotiType());
422 return converter->toJSValueRef(notification->getNotiType());
424 else if (property == NOTIFICATION_ICON_PATH)
426 std::string iconPath = notification->getIconPath();
427 LogDebug(" IconPath : " << iconPath);
428 if (iconPath.empty())
429 return JSValueMakeNull(context);
431 iconPath = DeviceAPI::Filesystem::Utils::toVirtualPath(context, iconPath);
433 return converter->toJSValueRef(iconPath);
435 else if (property == NOTIFICATION_SOUND_PATH)
437 std::string soundPath = notification->getSoundPath();
438 LogDebug(" NOTIFICATION_SOUND_PATH : " << soundPath);
439 if (soundPath.empty())
440 return JSValueMakeNull(context);
442 soundPath = DeviceAPI::Filesystem::Utils::toVirtualPath(context, soundPath);
444 return converter->toJSValueRef(soundPath);
446 else if (property == NOTIFICATION_VIBRATION)
448 bool virbration = notification->getDefaultVibration();
449 LogDebug(" NOTIFICATION_VIBRATION : " << virbration);
450 return converter->toJSValueRef(virbration);
452 else if (property == NOTIFICATION_APP_ID)
454 std::string appid = notification->getApplicationId();
455 LogDebug(" NOTIFICATION_APP_ID : " << appid);
456 return converter->toJSValueRef(appid);
458 else if (property == NOTIFICATION_APP_CONTROL
459 #ifdef APPLICATION_API_BACKWARD_COMPATIBILITY
460 || property == NOTIFICATION_SEVICE
464 DeviceAPI::Application::ApplicationControlPtr appControl = notification->getApplicationControl();
465 return converter->toJSValueRef(appControl);
467 else if (property == NOTIFICATION_PROGRESS_VALUE)
469 double progress = notification->getProgressValue();
470 NotificationProgressType progressType = notification->getProgressType();
471 LogDebug(" progress type = " << progressType << " progress = " << progress);
473 int progressValue = 0;
475 if ( NOTI_PROGRESS_TYPE_SIZE == progressType)
477 progressValue = int(progress);
479 else if ( NOTI_PROGRESS_TYPE_PERCENTAGE == progressType)
481 progressValue = int(progress*100 + 0.01);
483 LogDebug("ProgressValue = " << progressValue);
485 //int progressValue = int (progress);
486 //LogDebug("progress = " << progress << " value = " << progressValue);
488 return converter->toJSValueRef(progressValue);
490 else if (property == NOTIFICATION_LINES)
492 if (notification->getLines())
493 return converter->toJSValueRef(notification->getLines());
495 return JSValueMakeNull(context);
497 else if (property == NOTIFICATION_BACKGROUND_IMAGE_PATH )
499 std::string strBackground = notification->getBackground();
500 LogDebug(" NOTIFICATION_BACKGROUND_PATH : " << strBackground);
501 if (strBackground.empty())
502 return JSValueMakeNull(context);
504 strBackground = DeviceAPI::Filesystem::Utils::toVirtualPath(context, strBackground);
505 return converter->toJSValueRef(strBackground);
507 else if (property == NOTIFICATION_NUMBER)
509 unsigned int number = notification->getNumber();
510 LogDebug(" NUMBER : " << notification->getNumber());
511 return converter->toJSValueRef(number);
513 else if (property == NOTIFICATION_THUMBNAILS)
515 return converter->toJSValueRef(notification->getThumbnails());
517 else if (property == NOTIFICATION_SUB_ICON_PATH )
519 std::string strSubIcon = notification->getSubIconPath();
520 LogDebug(" NOTIFICATION_SUB_ICON_PATH : " << strSubIcon);
521 if (strSubIcon.empty())
522 return JSValueMakeNull(context);
524 strSubIcon = DeviceAPI::Filesystem::Utils::toVirtualPath(context, strSubIcon);
525 return converter->toJSValueRef(strSubIcon);
529 ThrowMsg(WrtDeviceApis::Commons::ConversionException,"No attribute");
533 Catch (WrtDeviceApis::Commons::NullPointerException)
535 if (property == NOTIFICATION_POSTED_TIME || property == NOTIFICATION_APP_CONTROL
536 #ifdef APPLICATION_API_BACKWARD_COMPATIBILITY
537 || property == NOTIFICATION_SEVICE
541 return JSValueMakeNull(context);
545 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
546 return JSTizenExceptionFactory::postException(context, exception,
547 JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
550 Catch (WrtDeviceApis::Commons::ConversionException)
552 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
553 return JSTizenExceptionFactory::postException(context, exception,
554 JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
557 Catch (WrtDeviceApis::Commons::InvalidArgumentException)
559 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
560 return JSTizenExceptionFactory::postException(context, exception,
561 JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
563 Catch(WrtDeviceApis::Commons::UnsupportedException)
565 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
566 return JSTizenExceptionFactory::postException(context, exception,
567 JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
569 Catch (WrtDeviceApis::Commons::Exception)
571 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
572 return JSTizenExceptionFactory::postException(context, exception,
573 JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
579 JSObjectRef JSStatusNotification::createJSObject(JSContextRef context, INotificationPtr notification)
581 JSStatusNotificationPriv* priv = new JSStatusNotificationPriv( context, notification);
582 return JSObjectMake(context, getClassRef(), priv);
586 JSObjectRef JSStatusNotification::constructor(JSContextRef context,
587 JSObjectRef constructor,
588 size_t argumentCount,
589 const JSValueRef arguments[],
590 JSValueRef* exception)
593 //NotificationConverter converter(context);
594 NotificationConverterFactory::ConverterType converter =
595 NotificationConverterFactory::getConverter(context);
596 JSValueRef reserveArguments[3];
598 JSStatusNotificationPriv* priv = NULL;
602 for (index = 0; index < 3; index++)
604 if (index < argumentCount)
605 reserveArguments[index] = arguments[index];
607 reserveArguments[index] = JSValueMakeUndefined(context);
610 std::string statusType = converter->toString(reserveArguments[0]);
612 //create notification private object.
613 INotificationPtr notification = NotificationFactory::getInstance().getNotification(converter->toNotificationType(reserveArguments[0]));
614 priv = new JSStatusNotificationPriv(context, notification);
616 LogDebug("notification=" << notification);
619 if (statusType == "ONGOING" || statusType == "PROGRESS")
621 notification.Reset();
622 INotificationPtr newNotification(
623 NotificationFactory::getInstance().getNotification(TRUE));
624 notification = newNotification;
629 priv = new JSStatusNotificationPriv(context, notification);
633 //notification->setStatusType(converter.toString(reserveArguments[0]));
634 //notification->setNotiType(converter.toNotificationType(reserveArguments[0]));
636 notification->setTitle(converter->toString(reserveArguments[1]));
638 if (!JSValueIsUndefined(context, reserveArguments[2]))
640 converter->toNotificationFromDict(notification, reserveArguments[2]);
643 return JSObjectMake(context, getClassRef(), priv);
645 Catch (WrtDeviceApis::Commons::Exception)
651 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
652 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
658 INotificationPtr JSStatusNotification::getNotification(JSContextRef context, JSValueRef value)
660 JSObjectRef object = JSValueToObject(context, value, NULL);
663 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
665 JSStatusNotificationPriv *priv = static_cast<JSStatusNotificationPriv*>(JSObjectGetPrivate(object));
669 Throw(WrtDeviceApis::Commons::NullPointerException);
671 return priv->getObject();