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.
18 #include <dpl/log/log.h>
19 #include <CommonsJavaScript/PrivateObject.h>
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
26 #include "JSMediacontent.h"
31 namespace Mediacontent {
33 #define TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME "MediaItem"
34 #define TIZEN_MEDIACONTENT_MEDIA_UID "id"
35 #define TIZEN_MEDIACONTENT_MEDIA_TYPE "type"
36 #define TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE "mimeType"
37 #define TIZEN_MEDIACONTENT_MEDIA_TITLE "title"
38 #define TIZEN_MEDIACONTENT_MEDIA_FILE_URL "fileURL"
39 #define TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH "thumbnailPath"
40 #define TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE "createdDate"
41 #define TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE "releasedDate"
42 #define TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE "modifiedDate"
43 #define TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION "description"
44 #define TIZEN_MEDIACONTENT_MEDIA_RATING "rating"
45 #define TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR "editableAttibutes"
49 JSClassDefinition JSMedia::m_classInfo =
52 kJSClassAttributeNone,
53 TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME,
62 NULL, //DeleteProperty,
63 NULL, //GetPropertyNames,
64 NULL, //CallAsFunction,
65 NULL, //CallAsConstructor,
71 JSStaticValue JSMedia::m_property[] =
74 { TIZEN_MEDIACONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_MEDIACONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
77 { TIZEN_MEDIACONTENT_MEDIA_TITLE, getPropertyDisplayName, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_MEDIACONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
79 { TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
80 { TIZEN_MEDIACONTENT_MEDIA_CREATEDDATE, getPropertyCreatedDate, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
82 { TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
83 { TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION, getPropertyDescription, NULL, kJSPropertyAttributeReadOnly},
84 { TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},
85 { TIZEN_MEDIACONTENT_MEDIA_RATING, getPropertyFavorite, setPropertyFavorite, kJSPropertyAttributeNone},
90 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
92 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
94 LogDebug( "entered" );
95 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
98 MediacontentMediaPtr privateData(new MediacontentMedia());
99 priv = new MediaPrivObject(context, privateData);
100 JSObjectSetPrivate(object, static_cast<void*>(priv));
101 LogDebug("new event is created" );
105 LogDebug("private object already exists");
109 void JSMedia::finalize(JSObjectRef object)
111 LogDebug( "entered" );
112 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
116 const JSClassRef JSMedia::getClassRef()
118 LogDebug("JSMedia::getClassRef()");
121 m_jsClassRef = JSClassCreate(&m_classInfo);
126 const JSClassDefinition* JSMedia::getClassInfo()
131 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
134 MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
137 ThrowMsg(NullPointerException, "Private object is null");
139 MediacontentMediaPtr result = priv->getObject();
142 ThrowMsg(NullPointerException, "Private object is null");
147 JSValueRef JSMedia::getPropertyId(
148 JSContextRef context,
150 JSStringRef propertyName,
151 JSValueRef* exception)
156 Converter converter(context);
157 MediacontentMediaPtr event = getMediaObject(object);
159 return converter.toJSValueRef(event->getMediaUUID());
163 LogWarning("trying to get incorrect value");
165 return JSValueMakeUndefined(context);
169 JSValueRef JSMedia::getPropertyMimeType(
170 JSContextRef context,
172 JSStringRef propertyName,
173 JSValueRef* exception)
178 Converter converter(context);
179 MediacontentMediaPtr event = getMediaObject(object);
180 return converter.toJSValueRef(event->getMimeType());
184 LogWarning("trying to get incorrect value");
186 return JSValueMakeUndefined(context);
191 JSValueRef JSMedia::getPropertyDisplayName(
192 JSContextRef context,
194 JSStringRef propertyName,
195 JSValueRef* exception)
197 LogDebug("getPropertyDisplayName::entered");
200 Converter converter(context);
201 MediacontentMediaPtr event = getMediaObject(object);
202 LogDebug("getDisplayName:"<<event->getDisplayName());
203 return converter.toJSValueRef(event->getDisplayName());
207 LogWarning("trying to get incorrect value");
209 return JSValueMakeUndefined(context);
213 JSValueRef JSMedia::getPropertyFilePath(
214 JSContextRef context,
216 JSStringRef propertyName,
217 JSValueRef* exception)
222 Converter converter(context);
223 MediacontentMediaPtr event = getMediaObject(object);
224 return converter.toJSValueRef(event->getFilePath());
228 LogWarning("trying to get incorrect value");
230 return JSValueMakeUndefined(context);
234 JSValueRef JSMedia::getPropertyThumbnailPath(
235 JSContextRef context,
237 JSStringRef propertyName,
238 JSValueRef* exception)
243 Converter converter(context);
244 MediacontentMediaPtr event = getMediaObject(object);
245 return converter.toJSValueRef(event->getThumbnailPath());
249 LogWarning("trying to get incorrect value");
251 return JSValueMakeUndefined(context);
254 JSValueRef JSMedia::getPropertyDescription(
255 JSContextRef context,
257 JSStringRef propertyName,
258 JSValueRef* exception)
263 Converter converter(context);
264 MediacontentMediaPtr event = getMediaObject(object);
265 return converter.toJSValueRef(event->getDescription());
269 LogWarning("trying to get incorrect value");
271 return JSValueMakeUndefined(context);
276 JSValueRef JSMedia::getPropertyCreatedDate(
277 JSContextRef context,
279 JSStringRef propertyName,
280 JSValueRef* exception)
285 Converter converter(context);
286 MediacontentMediaPtr event = getMediaObject(object);
287 return converter.toJSValueRef(event->getCreatedDate());
291 LogWarning("trying to get incorrect value");
293 return JSValueMakeUndefined(context);
297 JSValueRef JSMedia::getPropertyModifiedDate(
298 JSContextRef context,
300 JSStringRef propertyName,
301 JSValueRef* exception)
306 Converter converter(context);
307 MediacontentMediaPtr event = getMediaObject(object);
308 return converter.toJSValueRef(event->getModifiedDate());
312 LogWarning("trying to get incorrect value");
314 return JSValueMakeUndefined(context);
317 JSValueRef JSMedia::getPropertyReleasedDate(
318 JSContextRef context,
320 JSStringRef propertyName,
321 JSValueRef* exception)
326 Converter converter(context);
327 MediacontentMediaPtr event = getMediaObject(object);
328 return converter.toJSValueRef(event->getReleasedDate());
332 LogWarning("trying to get incorrect value");
334 return JSValueMakeUndefined(context);
339 JSValueRef JSMedia::getPropertyFavorite(
340 JSContextRef context,
342 JSStringRef propertyName,
343 JSValueRef* exception)
348 Converter converter(context);
349 MediacontentMediaPtr event = getMediaObject(object);
350 return converter.toJSValueRef(event->getFavorite());
354 LogWarning("trying to get incorrect value");
356 return JSValueMakeUndefined(context);
359 JSValueRef JSMedia::getPropertyType(
360 JSContextRef context,
362 JSStringRef propertyName,
363 JSValueRef* exception)
365 LogDebug("getPropertyType::entered");
368 Converter converter(context);
369 MediacontentMediaPtr event = getMediaObject(object);
370 return converter.toJSValueRef(event->getMediaType());
374 LogWarning("trying to get incorrect value");
376 return JSValueMakeUndefined(context);
379 JSValueRef JSMedia::getPropertyEditableAttr(
380 JSContextRef context,
382 JSStringRef propertyName,
383 JSValueRef* exception)
387 Converter converter(context);
388 MediacontentMediaPtr event = getMediaObject(object);
389 EditableAttributeListPtr editableAttrPtr = event->getEditableAttr();
390 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
394 if (NULL == jsResult)
396 ThrowMsg(NullPointerException, "Could not create js array object");
398 for(unsigned int i=0; i<editableAttrPtr->size(); i++)
400 JSValueRef val = converter.toJSValueRef(editableAttrPtr->at(i));
401 if(!JSSetArrayElement(context, jsResult, i, val))
403 ThrowMsg(UnknownException, "Could not insert value into js array");
411 LogWarning("trying to get incorrect value");
413 return JSValueMakeUndefined(context);
418 bool JSMedia::setPropertyFavorite(
419 JSContextRef context,
421 JSStringRef propertyName,
423 JSValueRef* exception)
428 Converter converter(context);
429 MediacontentMediaPtr event = getMediaObject(object);
430 int rating = converter.toInt(value);
432 LogDebug("value : " << rating);
433 event->setFavorite(rating);
438 LogWarning("trying to set incorrect value");
439 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);