Fix build breaks in contact
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSMediacontent.cpp
old mode 100644 (file)
new mode 100755 (executable)
index 5ebcb25..3141372
@@ -41,6 +41,7 @@
 #include "JSAudio.h"
 #include "MediaConverter.h"
 #include "plugin_config.h"
+#include "MediaContentAsyncCallbackManager.h"
 
 using namespace TizenApis::Commons;
 using namespace TizenApis::Api::Tizen;
@@ -168,7 +169,6 @@ JSValueRef JSMediacontent::getFolders(
                                                        const JSValueRef arguments[], 
                                                        JSValueRef* exception )
 {
-
        LogDebug("JSMediacontent::findFolders entered");
 
        MediacontentPrivObject *privateObject;
@@ -191,9 +191,7 @@ JSValueRef JSMediacontent::getFolders(
        MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
 
        cbm = JSCallbackManager::createObject(globalContext);
-
-       // Protect the super object until the callback operation is finished.
-       JSValueProtect(globalContext, thisObject);
+       cbm->setObject(thisObject);
 
     Try
     {
@@ -241,6 +239,8 @@ JSValueRef JSMediacontent::getFolders(
         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
 
         mediacontent->findFolder(dplEvent);
+
+       MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
     }
     Catch(UnsupportedException)
     {
@@ -292,7 +292,6 @@ JSValueRef JSMediacontent::findItems(
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
        }
 
-
        AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
             MEDIACONTENT_FUNCTION_API_FIND_ITEMS);
 
@@ -307,15 +306,14 @@ JSValueRef JSMediacontent::findItems(
        MediaConverter mediaConverter(globalContext);
 
        cbm = JSCallbackManager::createObject(globalContext);
+       cbm->setObject(thisObject);
 
-       // Protect the super object until the callback operation is finished.
-       JSValueProtect(globalContext, thisObject);
 
     Try
     {
                IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
                IEventBrowseFolderPtr dplEvent(new IEventBrowseFolder());
-       JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
+               JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
 
                if(argumentCount >= 1)  //MediaItemArraySuccessCB successCallback
                {
@@ -358,6 +356,17 @@ JSValueRef JSMediacontent::findItems(
                        if(!(validator.isNullOrUndefined(arguments[2])) && (folderId.length() > 0)  )
                        {
                                LogDebug("folderId:"+ folderId);
+                               
+                               int nFolderId = 0;
+                               istringstream(folderId) >> nFolderId; //string -> int
+
+                               if (nFolderId<0)
+                               {
+                                       cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context,              
+                                               JSTizenException::INVALID_VALUES_ERROR, "Argument is invalid"));
+                                       return JSValueMakeUndefined(context);
+                               }
+                               
                                dplEvent->setFolderID(folderId);
                        }
                }               
@@ -376,13 +385,14 @@ JSValueRef JSMediacontent::findItems(
                }
                if(argumentCount >= 5)  //optional SortMode? sortMode
                {
-                       if(JSValueIsObject(context, arguments[4]))
+
+                       if ( JSValueIsObject(context, arguments[4]) && 
+                               !JSValueIsNull(context, arguments[4]) && 
+                               !JSValueIsUndefined(context, arguments[4]) &&
+                               !JSIsArrayValue(context, arguments[4]))
                        {
                                LogDebug("sortmode is object");
-                               // Though the sortMode is a single type, we save it in an array internally.
-                               TizenApis::Api::Tizen::SortModeArrayPtr sortModes(new TizenApis::Api::Tizen::SortModeArray());
-                               sortModes->push_back(filterConverter->toSortMode(arguments[4]));
-                               dplEvent->setSortModes(sortModes);
+                               dplEvent->setSortMode(filterConverter->toSortMode(arguments[4]));
                        }
                        else if(!validator.isNullOrUndefined(arguments[4]))             //nullable
                        {
@@ -393,31 +403,40 @@ JSValueRef JSMediacontent::findItems(
                if(argumentCount >= 6)  //optional unsigned long? count
                {
                        long count = filterConverter->toLong(arguments[5]);
-                       if(count != 0.0)
+
+                       if(count > 0)
                        {
                                dplEvent->setLimit(count);
                        }
+                       else if ( count == 0)
+                       {
+                               dplEvent->setLimit(-1); // -1 is all row.
+                       }
                        else
                        {
-                               dplEvent->setLimit(-1);
+                               cbm->callOnError(JSTizenExceptionFactory::makeErrorObject(context,              
+                                       JSTizenException::INVALID_VALUES_ERROR, "Argument is invalid"));
+                               return JSValueMakeUndefined(context);
                        }
 
                }               
                if(argumentCount >= 7)  //optional unsigned long? offset
                {
-                       long limit = filterConverter->toLong(arguments[6]);
-                       if(limit != 0.0)
+                       long offset = filterConverter->toLong(arguments[6]);
+                       if(offset != 0)
                        {
-                               dplEvent->setLimit(limit);
+                               dplEvent->setOffset(offset);
                        }
                        else
                        {
-                               dplEvent->setLimit(-1);
+                               dplEvent->setOffset(0); 
                        }
                }               
         dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
         mediacontent->browseFolder(dplEvent);
+
+       MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
     }
     Catch(UnsupportedException)
     {
@@ -478,9 +497,7 @@ JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
        MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
 
        cbm = JSCallbackManager::createObject(globalContext);
-
-       // Protect the super object until the callback operation is finished.
-       JSValueProtect(globalContext, thisObject);
+       cbm->setObject(thisObject);
 
        Try
     {
@@ -541,6 +558,7 @@ JSValueRef JSMediacontent::updateItemsBatch(JSContextRef context,
         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance()); 
         mediacontent->updateMediaItems(dplEvent);
 
+       MediaContentAsyncCallbackManagerSingleton::Instance().registerCallbackManager(cbm, globalContext);
     }
     Catch(UnsupportedException)
     {
@@ -592,8 +610,7 @@ LogDebug("updateItem::entered");
                return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong object");
        }
        
-       AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(
-            MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
+       AceSecurityStatus status = MEDIACONTENT_CHECK_ACCESS(MEDIACONTENT_FUNCTION_API_UPDATE_ITEM);
        
        TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);