2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/JSUtils.h>
24 #include <CommonsJavaScript/JSPendingOperation.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <Tizen/Common/SecurityExceptions.h>
29 #include <Tizen/Tizen/FilterConverter.h>
33 #include <API/Mediacontent/MediacontentFactory.h>
34 #include "MediacontentController.h"
35 #include "JSMediacontent.h"
36 #include "JSMediacontentManager.h"
38 #include "MediaConverter.h"
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Tizen;
42 using namespace TizenApis::Tizen1_0::Tizen;
45 #define TIZEN_MEDIACONTENT_ATTRIBUTENAME "MediaSource"
49 * @throw InvalidArgumentException If not a callback nor JS null nor JS undefined.
51 JSValueRef getFunctionOrNull(JSContextRef ctx, JSValueRef arg)
53 if (Validator(ctx).isCallback(arg))
57 else if (!JSValueIsNull(ctx, arg) && !JSValueIsUndefined(ctx, arg))
59 ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
67 namespace Mediacontent {
70 JSClassDefinition JSMediacontent::m_classInfo =
73 kJSClassAttributeNone,
74 TIZEN_MEDIACONTENT_ATTRIBUTENAME,
83 NULL,//DeleteProperty,
84 NULL,//GetPropertyNames,
85 NULL,//CallAsFunction,
86 NULL,//CallAsConstructor,
91 JSStaticValue JSMediacontent::m_property[] =
96 JSStaticFunction JSMediacontent::m_function[] =
98 { "findItems", findMedia, kJSPropertyAttributeNone },
99 { "findFolders", findFolders, kJSPropertyAttributeNone },
100 { "updateItem", updateItem, kJSPropertyAttributeNone },
104 JSClassRef JSMediacontent::m_jsClassRef = JSClassCreate(JSMediacontent::getClassInfo());
106 void JSMediacontent::initialize(JSContextRef context, JSObjectRef object)
108 LogDebug("JSMediacontent::initialize entered");
109 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
112 //create default instance
113 LogWarning("create default instance");
114 IMediacontentPtr mediacontent = MediacontentFactory::getInstance().createMediacontentObject();
115 priv = new MediacontentPrivObject(context, mediacontent);
116 if (!JSObjectSetPrivate(object, static_cast<void*>(priv)))
123 LogDebug("private object alrerady exists");
128 void JSMediacontent::finalize(JSObjectRef object)
131 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( object ) ) ;
135 const JSClassRef JSMediacontent::getClassRef()
139 m_jsClassRef = JSClassCreate(&m_classInfo);
144 const JSClassDefinition* JSMediacontent::getClassInfo()
150 JSValueRef JSMediacontent::findMedia(
151 JSContextRef context,
153 JSObjectRef thisObject,
154 size_t argumentCount,
155 const JSValueRef arguments[],
156 JSValueRef* exception )
158 LogDebug("JSMediacontent::findMedia entered");
160 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
161 assert(privateObject);
163 JSContextRef globalContext = privateObject->getContext();
165 Validator validator(context);
166 JSCallbackManagerPtr cbm(NULL);
167 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
168 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
169 cbm = JSCallbackManager::createObject(globalContext);
171 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
175 if (argumentCount == 0 ||
176 (!validator.isCallback(arguments[0])) ||
177 (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) //||
178 // (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) /*&& !validator.isNullOrUndefined(arguments[2])*/))
179 // (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) /*&& !validator.isNullOrUndefined(arguments[3])*/)) ||
180 // (argumentCount >= 5 && (!JSValueIsObject(context, arguments[4]) /*&& !validator.isNullOrUndefined(arguments[4]))*/)) ||
181 /*(argumentCount >= 6 && (!JSValueIsObject(context, arguments[5]) && !validator.isNullOrUndefined(arguments[4]))))*/)
183 LogDebug("Argument type mismatch");
184 Throw(InvalidArgumentException);
188 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
189 onSuccessForCbm = arguments[0];
190 if (argumentCount >= 2)
192 onErrorForCbm = arguments[1];
194 cbm->setOnSuccess(onSuccessForCbm);
195 cbm->setOnError(onErrorForCbm);
199 IEventFindMediaPtr dplEvent(new IEventFindMedia());
200 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
201 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
203 if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2]))
205 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
207 if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3]))
209 SortModeArrayPtr sortModes(new SortModeArray());
210 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
211 dplEvent->setSortModes(sortModes);
213 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4]))
215 long limit = filterConverter->toLong(arguments[4]);
216 dplEvent->setLimit(limit);
218 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5]))
220 long offset = filterConverter->toLong(arguments[5]);
221 dplEvent->setOffset(offset);
224 mediacontent->findMedia(dplEvent);
227 Catch(InvalidArgumentException)
229 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
231 Catch(ConversionException)
233 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
237 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
240 return JSValueMakeNull(context);
245 JSValueRef JSMediacontent::findFolders(
246 JSContextRef context,
248 JSObjectRef thisObject,
249 size_t argumentCount,
250 const JSValueRef arguments[],
251 JSValueRef* exception )
254 LogDebug("JSMediacontent::findFolders entered");
256 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
257 assert(privateObject);
261 //AceSecurityStatus status = CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_FIND);
262 //TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
264 JSContextRef globalContext = privateObject->getContext();
265 Validator validator(context);
266 JSCallbackManagerPtr cbm(NULL);
267 FilterConverterFactory::ConverterType filterConverter = FilterConverterFactory::getConverter(context);
268 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
270 cbm = JSCallbackManager::createObject(globalContext);
272 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
277 if (argumentCount == 0 ||
278 (!validator.isCallback(arguments[0])) ||
279 (argumentCount >= 2 && (!validator.isCallback(arguments[1]))) ||
280 (argumentCount >= 3 && (!JSValueIsObject(context, arguments[2]) /*&& !validator.isNullOrUndefined(arguments[2])*/))
281 // (argumentCount >= 4 && (!JSValueIsObject(context, arguments[3]) /*&& !validator.isNullOrUndefined(arguments[3])*/)) ||
282 // (argumentCount >= 5 && (!JSValueIsObject(context, arguments[4]) /*&& !validator.isNullOrUndefined(arguments[4]))*/)) ||
283 /*(argumentCount >= 6 && (!JSValueIsObject(context, arguments[5]) && !validator.isNullOrUndefined(arguments[4]))))*/)
285 LogDebug("Argument type mismatch");
286 Throw(InvalidArgumentException);
291 JSValueRef onSuccessForCbm = NULL, onErrorForCbm = NULL;
292 onSuccessForCbm = arguments[0];
293 if (argumentCount >= 2)
295 onErrorForCbm = arguments[1];
297 cbm->setOnSuccess(onSuccessForCbm);
298 cbm->setOnError(onErrorForCbm);
301 IEventFindFolderPtr dplEvent(new IEventFindFolder());
302 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
303 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
305 if (argumentCount >= 3 && !validator.isNullOrUndefined(arguments[2]))
307 dplEvent->setFilter(filterConverter->toFilter(arguments[2]));
309 if (argumentCount >= 4 && !validator.isNullOrUndefined(arguments[3]))
311 SortModeArrayPtr sortModes(new SortModeArray());
312 sortModes->push_back(filterConverter->toSortMode(arguments[3]));
313 dplEvent->setSortModes(sortModes);
315 if (argumentCount >= 5 && !validator.isNullOrUndefined(arguments[4]))
317 long limit = filterConverter->toLong(arguments[4]);
318 dplEvent->setLimit(limit);
320 if (argumentCount >= 6 && !validator.isNullOrUndefined(arguments[5]))
322 long offset = filterConverter->toLong(arguments[5]);
323 dplEvent->setOffset(offset);
327 mediacontent->findFolder(dplEvent);
329 Catch(InvalidArgumentException)
331 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
333 Catch(ConversionException)
335 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
339 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
342 return JSValueMakeNull(context);
347 JSValueRef JSMediacontent::updateItem(
348 JSContextRef context,
350 JSObjectRef thisObject,
351 size_t argumentCount,
352 const JSValueRef arguments[],
353 JSValueRef* exception)
355 LogDebug("updateItem::entered");
358 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
359 assert(privateObject);
362 //TIZEN_SYNC_ACCESS_HANDLER
363 Validator validator(context);
364 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
368 if (argumentCount == 0 ||
369 (!JSValueIsObject(context, arguments[0])) ||
370 (argumentCount >= 2 && (!validator.isNullOrUndefined(arguments[1]) && (!validator.isCallback(arguments[1])))) ||
371 (argumentCount >= 3 && (!validator.isNullOrUndefined(arguments[2]) && (!validator.isCallback(arguments[2])))) )
373 LogDebug("Argument type mismatch");
374 Throw(InvalidArgumentException);
378 JSObjectRef arg = JSValueToObject(context, arguments[0], exception);
379 MediacontentMediaPtr event = JSMedia::getMediaObject(arg);
381 IEventUpdateMediaPtr dplEvent(new IEventUpdateMedia());
382 dplEvent->setMediaItem(event);
383 dplEvent->setForSynchronousCall();
385 mediacontent->updateMedia(dplEvent);
386 if (dplEvent->getResult())
388 return JSValueMakeNull(context);
392 ThrowMsg(UnknownException, "Updating failed by unkown reason.");
395 Catch(InvalidArgumentException)
397 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
399 Catch(ConversionException)
401 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
405 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
408 return JSValueMakeNull(context);
414 JSValueRef JSMediacontent::findPlaylist(
415 JSContextRef context,
417 JSObjectRef thisObject,
418 size_t argumentCount,
419 const JSValueRef arguments[],
420 JSValueRef* exception )
422 LogDebug("JSMediacontent::findMedia entered");
424 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
425 assert(privateObject);
429 JSCallbackManagerPtr cbm(NULL);
432 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
433 if (argumentCount < 1)
435 LogError("No callback parameters");
436 Throw(InvalidArgumentException);
438 JSValueRef onError = (argumentCount > 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
439 JSContextRef globalContext = privateObject->getContext();
440 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
443 Validator validator(context);
444 if (validator.isCallback(arguments[0]))
446 cbm->setOnSuccess(arguments[0]);
448 else if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]))
450 Throw(InvalidArgumentException);
456 IEventFindPlaylistPtr dplEvent(new IEventFindPlaylist());
457 dplEvent->setPrivateData( DPL::StaticPointerCast<IEventPrivateData> (cbm));
458 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
459 mediacontent->findPlaylist(dplEvent);
462 Catch(InvalidArgumentException)
464 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
466 Catch(ConversionException)
468 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
472 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
475 return JSValueMakeNull(context);
481 JSValueRef JSMediacontent::deletePlaylist(
482 JSContextRef context,
484 JSObjectRef thisObject,
485 size_t argumentCount,
486 const JSValueRef arguments[],
487 JSValueRef* exception)
489 LogDebug("deletePlaylist::entered");
491 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
492 assert(privateObject);
494 //CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_EVENT);
498 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
499 if (argumentCount != 1)
501 LogError("Wrong number of parameters.");
502 Throw(InvalidArgumentException);
505 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]))
507 Throw(InvalidArgumentException);
508 //return JSValueMakeNull(context);
511 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
512 string playlistID = converter->toString(arguments[0]);
514 LogDebug("deletePlaylist::entered" << playlistID);
515 IEventDeletePlaylistPtr dplEvent(new IEventDeletePlaylist());
516 LogDebug("deletePlaylist::entered11");
517 dplEvent->setPlaylistId(playlistID);
518 LogDebug("deletePlaylist::entered22");
519 dplEvent->setForSynchronousCall();
520 LogDebug("deletePlaylist::entered33");
521 mediacontent->deletePlaylist(dplEvent);
522 LogDebug("deletePlaylist::entered44");
524 if (dplEvent->getResult())
526 // return converter->toJSValueRef(dplEvent->getPlaylist()->getPlaylistId());
529 Throw(InvalidArgumentException);
532 Catch(InvalidArgumentException)
534 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
536 Catch(ConversionException)
538 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
542 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
545 return JSValueMakeNull(context);
550 JSValueRef JSMediacontent::addAudioToPlaylist(
551 JSContextRef context,
553 JSObjectRef thisObject,
554 size_t argumentCount,
555 const JSValueRef arguments[],
556 JSValueRef* exception)
558 LogDebug("addAudioToPlaylist::entered");
560 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
561 assert(privateObject);
563 //CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_EVENT);
567 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
568 if (argumentCount != 2)
570 LogError("Wrong number of parameters.");
571 Throw(InvalidArgumentException);
574 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]))
576 Throw(InvalidArgumentException);
577 //return JSValueMakeNull(context);
580 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
581 string playlistID = converter->toString(arguments[0]);
582 string mediaUID = converter->toString(arguments[1]);
584 IEventAddAudioToPlaylistPtr dplEvent(new IEventAddAudioToPlaylist());
585 dplEvent->setPlaylistId(playlistID);
586 dplEvent->setMediaUID(mediaUID);
587 dplEvent->setForSynchronousCall();
588 mediacontent->addAudioToPlaylist(dplEvent);
590 if (dplEvent->getResult())
593 // return converter->toJSValueRef(dplEvent->getPlaylist()->getPlaylistId());
596 Throw(InvalidArgumentException);
599 Catch(InvalidArgumentException)
601 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
603 Catch(ConversionException)
605 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
607 Catch (NotFoundException)
609 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::NOT_FOUND_ERROR);
613 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
616 return JSValueMakeNull(context);
620 JSValueRef JSMediacontent::deleteAudioToPlaylist(
621 JSContextRef context,
623 JSObjectRef thisObject,
624 size_t argumentCount,
625 const JSValueRef arguments[],
626 JSValueRef* exception)
628 LogDebug("deleteAudioToPlaylist::entered");
630 MediacontentPrivObject *privateObject = static_cast<MediacontentPrivObject*>( JSObjectGetPrivate( thisObject ) ) ;
631 assert(privateObject);
633 //CALENDAR_CHECK_ACCESS(privateObject->getContext(), CALENDAR_FUNCTION_API_ADD_EVENT);
637 IMediacontentPtr mediacontent = getMediacontentObject(context, thisObject, exception);
638 if (argumentCount != 2)
640 LogError("Wrong number of parameters.");
641 Throw(InvalidArgumentException);
644 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]))
646 Throw(InvalidArgumentException);
647 //return JSValueMakeNull(context);
650 MediaConverterFactory::ConverterType converter = MediaConverterFactory::getConverter(context);
651 string playlistID = converter->toString(arguments[0]);
652 string mediaUID = converter->toString(arguments[1]);
654 IEventDeleteAudioToPlaylistPtr dplEvent(new IEventDeleteAudioToPlaylist());
655 dplEvent->setPlaylistId(playlistID);
656 dplEvent->setMediaUID(mediaUID);
657 dplEvent->setForSynchronousCall();
658 mediacontent->deleteAudioToPlaylist(dplEvent);
660 if (dplEvent->getResult())
662 // return converter->toJSValueRef(dplEvent->getPlaylist()->getPlaylistId());
665 Throw(NotFoundException);
668 Catch(InvalidArgumentException)
670 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
672 Catch(ConversionException)
674 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::INVALID_VALUES_ERROR);
678 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::UNKNOWN_ERROR);
681 return JSValueMakeNull(context);
690 IMediacontentPtr JSMediacontent::getMediacontentObject(
692 const JSObjectRef object,
693 JSValueRef* exception)
695 MediacontentPrivObject *priv = static_cast<MediacontentPrivObject*>(JSObjectGetPrivate(object));
698 return priv->getObject();
700 ThrowMsg(NullPointerException, "Private object is NULL.");