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/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
25 #include <API/Mediacontent/IMediacontent.h>
26 #include <API/Mediacontent/MediacontentFactory.h>
27 //#include <API/Mediacontent/IEventFolderFindMedia.h>
30 #include "MediacontentController.h"
31 #include "JSMediacontent.h"
37 namespace Mediacontent {
39 #define TIZEN_MEDIACONTENT_FOLDER_ATTRIBUTENAME "Folder"
40 #define TIZEN_MEDIACONTENT_FOLDER_UID "id"
41 #define TIZEN_MEDIACONTENT_FOLDER_NAME "title"
42 #define TIZEN_MEDIACONTENT_FOLDER_PATH "folderUrl"
43 #define TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE "storageType"
44 #define TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE "modifiedDate"
45 #define TIZEN_MEDIACONTENT_FOLDER_MEDIAID "mediaItems"
48 JSClassDefinition JSFolder::m_classInfo =
51 kJSClassAttributeNone,
52 TIZEN_MEDIACONTENT_FOLDER_ATTRIBUTENAME,
55 m_function, // m_function,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
69 JSStaticValue JSFolder::m_property[] =
72 { TIZEN_MEDIACONTENT_FOLDER_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
73 { TIZEN_MEDIACONTENT_FOLDER_NAME, getPropertyName, NULL, kJSPropertyAttributeReadOnly},
74 { TIZEN_MEDIACONTENT_FOLDER_PATH, getPropertyPath, NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE, getPropertyStorageType, NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
77 { TIZEN_MEDIACONTENT_FOLDER_MEDIAID, getPropertyMediaId, NULL, kJSPropertyAttributeReadOnly},
81 JSStaticFunction JSFolder::m_function[] =
87 JSClassRef JSFolder::m_jsClassRef = JSClassCreate(JSFolder::getClassInfo());
89 void JSFolder::initialize(JSContextRef context, JSObjectRef object)
91 LogDebug( "entered" );
92 FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) );
95 MediacontentFolderPtr privateData(new MediacontentFolder());
96 priv = new FolderPrivObject(context, privateData);
97 JSObjectSetPrivate(object, static_cast<void*>(priv));
98 LogDebug("new event is created" );
102 LogDebug("private object already exists");
107 void JSFolder::finalize(JSObjectRef object)
109 LogDebug( "entered" );
110 FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) ) ;
116 const JSClassRef JSFolder::getClassRef()
120 m_jsClassRef = JSClassCreate(&m_classInfo);
125 const JSClassDefinition* JSFolder::getClassInfo()
131 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
134 FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
136 ThrowMsg(NullPointerException, "Private object is null");
138 MediacontentFolderPtr result = priv->getObject();
140 ThrowMsg(NullPointerException, "Private object is null");
147 JSValueRef JSFolder::getPropertyId(
148 JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
156 Converter converter(context);
157 MediacontentFolderPtr folder = getFolderObject(object);
159 return converter.toJSValueRef(folder->getFolderUUID());
163 LogWarning("trying to get incorrect value");
165 return JSValueMakeUndefined(context);
169 JSValueRef JSFolder::getPropertyName(
170 JSContextRef context,
172 JSStringRef propertyName,
173 JSValueRef* exception)
178 Converter converter(context);
179 MediacontentFolderPtr folder = getFolderObject(object);
180 return converter.toJSValueRef(folder->getFolderName());
184 LogWarning("trying to get incorrect value");
186 return JSValueMakeUndefined(context);
190 JSValueRef JSFolder::getPropertyPath(
191 JSContextRef context,
193 JSStringRef propertyName,
194 JSValueRef* exception)
199 Converter converter(context);
200 MediacontentFolderPtr folder = getFolderObject(object);
201 return converter.toJSValueRef(folder->getFolderPath());
205 LogWarning("trying to get incorrect value");
207 return JSValueMakeUndefined(context);
211 JSValueRef JSFolder::getPropertyStorageType(
212 JSContextRef context,
214 JSStringRef propertyName,
215 JSValueRef* exception)
220 Converter converter(context);
221 MediacontentFolderPtr folder = getFolderObject(object);
222 return converter.toJSValueRef(folder->getFolderStorageType());
226 LogWarning("trying to get incorrect value");
228 return JSValueMakeUndefined(context);
232 JSValueRef JSFolder::getPropertyModifiedDate(
233 JSContextRef context,
235 JSStringRef propertyName,
236 JSValueRef* exception)
241 Converter converter(context);
242 MediacontentFolderPtr folder = getFolderObject(object);
243 return converter.toJSValueRef(folder->getFolderModifiedDate());
247 LogWarning("trying to get incorrect value");
249 return JSValueMakeUndefined(context);
253 JSValueRef JSFolder::getPropertyMediaId(
254 JSContextRef context,
256 JSStringRef propertyName,
257 JSValueRef* exception)
262 Converter converter(context);
263 MediacontentFolderPtr folder = getFolderObject(object);
264 //folder->getMediaIdList()
265 //MediaIdListPtr getMediaIdList() const;
266 //typedef vector<std::string> MediaIdList;
267 //typedef DPL::SharedPtr<MediaIdList> MediaIdListPtr;
268 MediaIdListPtr mediaIdLstPtr = folder->getMediaIdList();
269 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
273 if (NULL == jsResult)
275 ThrowMsg(NullPointerException, "Could not create js array object");
277 for(unsigned int i=0; i<mediaIdLstPtr->size(); i++)
279 JSValueRef val = converter.toJSValueRef(mediaIdLstPtr->at(i));
280 if(!JSSetArrayElement(context, jsResult, i, val))
282 ThrowMsg(UnknownException, "Could not insert value into js array");
290 LogWarning("trying to get incorrect value");
292 return JSValueMakeUndefined(context);
298 JSValueRef JSFolder::findMedia(
299 JSContextRef context,
301 JSObjectRef thisObject,
302 size_t argumentCount,
303 const JSValueRef arguments[],
304 JSValueRef* exception )
306 FolderPrivObject *privFolder = static_cast<FolderPrivObject*>(JSObjectGetPrivate(thisObject));
308 LogDebug("JSFolder::FolderfindMedia entered" << getClassInfo());
312 JSCallbackManagerPtr cbm(NULL);
315 IMediacontentPtr mediacontent = Api::MediacontentFactory::getInstance().createMediacontentObject();
317 if (argumentCount < 1)
319 LogError("No callback parameters");
320 return JSExceptionFactory::TypeMismatchException.make(context, exception);
322 JSValueRef onError = (argumentCount > 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
323 JSContextRef globalContext = privFolder->getContext();
324 JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
325 Commons::Validator validator(context);
326 if (validator.isCallback(arguments[0]))
328 cbm->setOnSuccess(arguments[0]);
330 else if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]))
332 cbm->callOnError(JSExceptionFactory::InvalidValuesException.make(context));
333 return JSValueMakeUndefined(context);
337 return JSExceptionFactory::TypeMismatchException.make(context, exception);
339 IEventFolderFindMediaPtr dplEvent(new IEventFolderFindMedia());
340 MediacontentFolderPtr folder = getFolderObject(thisObject);
341 dplEvent->setFolder(folder);
342 dplEvent->setPrivateData( DPL::StaticPointerCast<Platform::IEventPrivateData> (cbm));
343 dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
344 mediacontent->folderFindMedia(dplEvent);
346 return Utils::makePendingOperation(cbm->getContext(), dplEvent);
348 Catch(Commons::InvalidArgumentException)
350 LogError("Invalid argument");
351 return JSExceptionFactory::TypeMismatchException.make(context, exception);
353 Catch(Commons::Exception)
355 LogError("unknow error occured");
358 cbm->callOnError(JSExceptionFactory::UnknownException.make(privFolder->getContext()));
360 return JSValueMakeUndefined(context);