upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarEvent.cpp
index e436c6f..eac5832 100755 (executable)
@@ -20,6 +20,7 @@
 #include <CommonsJavaScript/PrivateObject.h>
 #include <CommonsJavaScript/Converter.h>
 #include <CommonsJavaScript/JSUtils.h>
+#include <CommonsJavaScript/Utils.h>
 #include <CommonsJavaScript/Validator.h>
 #include <CommonsJavaScript/JSCallbackManager.h>
 #include <API/Calendar/EventId.h>
@@ -40,22 +41,6 @@ using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
 using namespace TizenApis::Commons;
 
-namespace {
-/**
- * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
- */
-JSValueRef getFunctionOrNull(JSContextRef ctx,
-        JSValueRef arg)
-{
-    if (Validator(ctx).isCallback(arg)) {
-        return arg;
-    } else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg)) {
-        ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
-    }
-    return NULL;
-}
-}
-
 namespace TizenApis {
 namespace Tizen1_0 {
 namespace Calendar {
@@ -104,29 +89,31 @@ ICalendarPtr JSCalendarEvent::m_calendar = CalendarFactory::getInstance().create
 void JSCalendarEvent::initialize(JSContextRef context,
         JSObjectRef object)
 {
-    LogDebug("entered");
-    CalendarEventPrivObject *priv =
-        static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
-    if (!priv) {
-        CalendarEventPtr privateData(new CalendarEvent());
-        priv = new CalendarEventPrivObject(context, privateData);
-        JSObjectSetPrivate(object, static_cast<void*>(priv));
-        LogDebug("New event is created.");
+    if (!JSObjectGetPrivate(object)) {
+        LogDebug("Create calendar event private object.");
+        CalendarEventPtr event( new CalendarEvent() );
+        CalendarEventPrivObject *priv = new CalendarEventPrivObject(context, event);
+        if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
+            delete priv;
+        }
     } else {
-        LogDebug("Private object already exists.");
+        LogDebug("Private object alrerady set.");
     }
 
-    if (!m_calendar) {
-        LogDebug("Calendar object does not exist!");
-    } else {
+    if (m_calendar) {
         m_calendar->setType(CalendarEvent::EVENT_TYPE);
-        LogDebug("Calendar object type is set.");
+        LogDebug("Calendar object type is set to event.");
     }
 }
 
 void JSCalendarEvent::finalize(JSObjectRef object)
 {
-    LogDebug("entered");
+    LogDebug("enter");
+    CalendarEventPrivObject* priv = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
+    if (priv) {
+        delete priv;
+        JSObjectSetPrivate(object, NULL);
+    }
 }
 
 JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
@@ -140,7 +127,7 @@ JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
     Try
     {
                CalendarEventPrivObject* privateObject = static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(constructor));
-               JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
+               JSContextRef globalContext = privateObject->getContext();
 
         CalendarConverter converter(globalContext);
         CalendarEventPtr event;
@@ -148,18 +135,23 @@ JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
         if (argumentCount==0) {
             CalendarEventPtr result(new CalendarEvent());
             event = result;
-        } else if (argumentCount==1) { // eventInitDict case
-            event = converter.toEvent(arguments[0]);
-            if (!event) {
+        } else if (argumentCount==1) {
+            LogInfo("eventInitDict case");
+            if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0])) {
+                CalendarEventPtr result(new CalendarEvent());
+                event = result;
+            } else if (JSValueIsObject(context, arguments[0])) {
+                event = converter.toEvent(arguments[0]);
+                if (!event) {
+                    ThrowMsg(ConversionException, "Parameter conversion failed.");
+                }
+            } else {
                 ThrowMsg(ConversionException, "Parameter conversion failed.");
             }
-        } else if (argumentCount==2) { // stringRepresentation case
-            if (!JSValueIsString(context, arguments[0]) || !JSValueIsString(context, arguments[1])) {
-                ThrowMsg(ConversionException, "Wrong parameter type.");
-            }
-
+        } else if (argumentCount>=2) {
+            LogInfo("event stringRepresentation case");
             std::string eventStr;
-            CalendarEvent::VObjectFormat format = CalendarEvent::ICALENDAR_20;
+            CalendarEvent::VObjectFormat format = CalendarEvent::UNDEFINED_FORMAT;
             eventStr = converter.toString(arguments[0]);
             format = converter.toVObjectFormat(converter.toString(arguments[1]));
 
@@ -171,13 +163,15 @@ JSObjectRef JSCalendarEvent::constructor(JSContextRef context,
 
             // Process the returned object.
             if (dplEvent->getResult()) {
-                LogInfo("Successfully created an item.");
+                LogInfo("Successfully created an event.");
                 event = dplEvent->getEvent();
             } else {
-                ThrowMsg(UnknownException, "Converting from string failed by unkown reason.");
+                if (dplEvent->getExceptionCode()==ExceptionCodes::InvalidArgumentException) {
+                    ThrowMsg(InvalidArgumentException, "Wrong string to convert.");
+                } else {
+                    ThrowMsg(UnknownException, "Converting string failed.");
+                }
             }
-        } else {
-            ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
         }
 
         return createJSCalendarEvent(globalContext, event);
@@ -269,20 +263,18 @@ JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
         JSValueRef* exception)
 {
     LogDebug("entered");
+
     CalendarEventPrivObject *privateObject =
         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(thisObject));
-    assert(privateObject);
 
-    AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_EXPAND_RECURRENCE);
+    AceSecurityStatus status = CALENDAR_CHECK_ACCESS(CALENDAR_FUNCTION_API_EXPAND_RECURRENCE);
 
     TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
-    JSCallbackManagerPtr cbm(NULL);
-
     Try
     {
-        if (argumentCount<3 || argumentCount>4) {
-            ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
+        if (!privateObject) {
+            ThrowMsg(ConversionException, "Object is null.");
         }
 
         CalendarEventPtr event = privateObject->getObject();
@@ -290,34 +282,45 @@ JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
             ThrowMsg(ConversionException, "Parameter conversion failed.");
         }
 
-        JSValueRef onError =(argumentCount > 3 ? getFunctionOrNull(context, arguments[3]) : NULL);
         JSContextRef globalContext = privateObject->getContext();
-        cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
-
-        Validator validator(context);
-        if (validator.isCallback(arguments[2])) {
-            cbm->setOnSuccess(arguments[2]);
-        } else if (JSValueIsNull(context, arguments[2]) || JSValueIsUndefined(context, arguments[2])) {
-            ThrowMsg(ConversionException, "Wrong parameter type.");
-        } else {
-            ThrowMsg(ConversionException, "Wrong parameter type.");
-        }
+        CalendarConverter converter(context);
 
         std::time_t startDate = 0;
         std::time_t endDate = INT_MAX; // about 60 years in 4 bytes system.
-        Converter converter(context);
         TimeUtilConverter timeConverter(context);
-        if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
-            startDate = timeConverter.toTZDateTimeT(arguments[0]);
-        } else {
-            ThrowMsg(ConversionException, "Wrong first parameter type.");
+        if (argumentCount>=1) {
+            if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
+                startDate = timeConverter.toTZDateTimeT(arguments[0]);
+                LogInfo("startDate: "<<startDate);
+            } else {
+                ThrowMsg(ConversionException, "Wrong first parameter type.");
+            }
+        }
+        if (argumentCount>=2) {
+            if (JSValueIsObjectOfClass(context, arguments[1], JSTZDate::getClassRef())) {
+                endDate = timeConverter.toTZDateTimeT(arguments[1]);
+                LogInfo("endDate: "<<endDate);
+            } else {
+                ThrowMsg(ConversionException, "Wrong second parameter type.");
+            }
         }
-        if (JSValueIsObjectOfClass(context, arguments[1], JSTZDate::getClassRef())) {
-            endDate = timeConverter.toDateTimeT(arguments[1]);
+
+        JSValueRef onError = argumentCount > 1 ? converter.toFunctionOrNull(arguments[3]) : NULL;
+
+        JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
+
+        Validator validator(context);
+        if (validator.isCallback(arguments[2])) {
+            cbm->setOnSuccess(arguments[2]);
         } else {
-            ThrowMsg(ConversionException, "Wrong second parameter type.");
+            ThrowMsg(ConversionException, "Wrong third parameter type.");
         }
 
+               // Protect the super object until the callback operation is finished.
+               JSValueProtect(globalContext, thisObject);
+
+        LogDebug("Proceed the expand event to the platform.");
+
         IEventExpandEventRecurrencePtr dplEvent(new IEventExpandEventRecurrence());
         dplEvent->setPrivateData(DPL::StaticPointerCast<IEventPrivateData>(cbm));
         dplEvent->setForAsynchronousCall(&CalendarResponseDispatcher::getInstance());
@@ -326,7 +329,7 @@ JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
         dplEvent->setEndDate(endDate);
         m_calendar->expandEventRecurrence(dplEvent);
 
-        return JSValueMakeNull(context);
+        return JSValueMakeUndefined(context);
     }
     Catch(UnsupportedException)
     {
@@ -354,7 +357,7 @@ JSValueRef JSCalendarEvent::expandRecurrence(JSContextRef context,
         return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
     }
 
-    return JSValueMakeNull(context);
+    return JSValueMakeUndefined(context);
 }
 
 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
@@ -370,10 +373,18 @@ JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
         CalendarEventPtr event = privateObject->getObject();
 
         EventIdPtr eventId( new EventId() );
-        eventId->setUId(event->getUId());
-        eventId->setRecurrenceId(event->getRecurrenceId());
-        eventId->setTimeZone(event->getTimeZone());
-        return JSCalendarEventId::createJSCalendarEventId(privateObject->getContext(), eventId);
+               if (0<event->getParentId()) {
+                       std::stringstream ss;
+                       ss<<event->getParentId();
+                       eventId->setUId(ss.str());
+               } else {
+               eventId->setUId(event->getUId());
+               }
+        std::stringstream ss;
+        std::time_t rid = event->getRecurrenceId();
+        ss<<rid;
+        eventId->setRecurrenceId(ss.str());
+        return JSCalendarEventId::createJSCalendarEventId(context, eventId);
     }
     Catch(Exception)
     {
@@ -393,15 +404,14 @@ JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context
         CalendarEventPrivObject *privateObject =
             static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
         CalendarEventPtr event = privateObject->getObject();
-
         if (!event) {
             Throw(NullPointerException);
         }
-        if (event->getLastModifiedDate() != 0) {
-            // Use the global context saved in the event struct.
-            return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
+
+        if (UNDEFINED_TIME==event->getLastModifiedDate()) {
+            return JSValueMakeNull(context);
         } else {
-            return JSValueMakeUndefined(context);
+            return JSTZDate::createJSObject(context, event->getLastModifiedDate(), event->getTimeZone());
         }
     }
     Catch(Exception)