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 "itemURI"
39 #define TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH "thumbnailURIs"
40 #define TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE "releaseDate"
41 #define TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE "modifiedDate"
42 #define TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION "description"
43 #define TIZEN_MEDIACONTENT_MEDIA_RATING "rating"
44 #define TIZEN_MEDIACONTENT_MEDIA_SIZE "size"
45 #define TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR "editableAttributes"
48 JSClassDefinition JSMedia::m_classInfo =
51 kJSClassAttributeNone,
52 TIZEN_MEDIACONTENT_MEDIA_ATTRIBUTENAME,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
70 JSStaticValue JSMedia::m_property[] =
73 { TIZEN_MEDIACONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
74 { TIZEN_MEDIACONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_MEDIACONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_MEDIACONTENT_MEDIA_TITLE, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
77 { TIZEN_MEDIACONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_MEDIACONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
79 { TIZEN_MEDIACONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
80 { TIZEN_MEDIACONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_MEDIACONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},
82 { TIZEN_MEDIACONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},
83 { TIZEN_MEDIACONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},
84 { TIZEN_MEDIACONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, kJSPropertyAttributeNone},
89 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
91 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
93 LogDebug( "entered" );
94 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
97 MediacontentMediaPtr privateData(new MediacontentMedia());
98 priv = new MediaPrivObject(context, privateData);
99 JSObjectSetPrivate(object, static_cast<void*>(priv));
100 LogDebug("new event is created" );
104 LogDebug("private object already exists");
108 void JSMedia::finalize(JSObjectRef object)
110 LogDebug( "entered" );
111 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
116 JSObjectSetPrivate(object, NULL);
121 const JSClassRef JSMedia::getClassRef()
123 LogDebug("JSMedia::getClassRef()");
126 m_jsClassRef = JSClassCreate(&m_classInfo);
131 const JSClassDefinition* JSMedia::getClassInfo()
136 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
139 MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
142 ThrowMsg(NullPointerException, "Private object is null");
144 MediacontentMediaPtr result = priv->getObject();
147 ThrowMsg(NullPointerException, "Private object is null");
152 JSValueRef JSMedia::getPropertyId(
153 JSContextRef context,
155 JSStringRef propertyName,
156 JSValueRef* exception)
161 Converter converter(context);
162 MediacontentMediaPtr media = getMediaObject(object);
164 return converter.toJSValueRef(media->getMediaUUID());
168 LogWarning("trying to get incorrect value");
170 return JSValueMakeUndefined(context);
174 JSValueRef JSMedia::getPropertyMimeType(
175 JSContextRef context,
177 JSStringRef propertyName,
178 JSValueRef* exception)
183 Converter converter(context);
184 MediacontentMediaPtr media = getMediaObject(object);
185 return converter.toJSValueRef(media->getMimeType());
189 LogWarning("trying to get incorrect value");
191 return JSValueMakeUndefined(context);
196 JSValueRef JSMedia::getPropertyDisplayName(
197 JSContextRef context,
199 JSStringRef propertyName,
200 JSValueRef* exception)
202 LogDebug("getPropertyDisplayName::entered");
205 Converter converter(context);
206 MediacontentMediaPtr media = getMediaObject(object);
207 LogDebug("getDisplayName:"<<media->getDisplayName());
208 return converter.toJSValueRef(media->getDisplayName());
212 LogWarning("trying to get incorrect value");
214 return JSValueMakeUndefined(context);
218 JSValueRef JSMedia::getPropertyFilePath(
219 JSContextRef context,
221 JSStringRef propertyName,
222 JSValueRef* exception)
227 Converter converter(context);
228 MediacontentMediaPtr media = getMediaObject(object);
229 return converter.toJSValueRef(media->getFilePath());
233 LogWarning("trying to get incorrect value");
235 return JSValueMakeUndefined(context);
239 JSValueRef JSMedia::getPropertyThumbnailPath(
240 JSContextRef context,
242 JSStringRef propertyName,
243 JSValueRef* exception)
248 Converter converter(context);
249 MediacontentMediaPtr media = getMediaObject(object);
250 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
251 if (NULL == jsResult)
253 ThrowMsg(NullPointerException, "Could not create js array object");
255 // for(unsigned int i=0; i<event->getThumbnailPath()->size(); i++)
257 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
258 if(!JSSetArrayElement(context, jsResult, 0, val))
260 ThrowMsg(UnknownException, "Could not insert value into js array");
269 LogWarning("trying to get incorrect value");
271 return JSValueMakeUndefined(context);
274 JSValueRef JSMedia::getPropertyDescription(
275 JSContextRef context,
277 JSStringRef propertyName,
278 JSValueRef* exception)
280 LogDebug("getPropertyDescription::entered");
283 Converter converter(context);
284 MediacontentMediaPtr media = getMediaObject(object);
285 return converter.toJSValueRef(media->getDescription());
289 LogWarning("trying to get incorrect value");
291 return JSValueMakeUndefined(context);
295 JSValueRef JSMedia::getPropertyModifiedDate(
296 JSContextRef context,
298 JSStringRef propertyName,
299 JSValueRef* exception)
304 Converter converter(context);
305 MediacontentMediaPtr media = getMediaObject(object);
306 return converter.toJSValueRef(media->getModifiedDate());
310 LogWarning("trying to get incorrect value");
312 return JSValueMakeUndefined(context);
315 JSValueRef JSMedia::getPropertyReleasedDate(
316 JSContextRef context,
318 JSStringRef propertyName,
319 JSValueRef* exception)
324 Converter converter(context);
325 MediacontentMediaPtr media = getMediaObject(object);
326 return converter.toJSValueRef(media->getReleasedDate());
330 LogWarning("trying to get incorrect value");
332 return JSValueMakeUndefined(context);
337 JSValueRef JSMedia::getPropertyRating(
338 JSContextRef context,
340 JSStringRef propertyName,
341 JSValueRef* exception)
346 Converter converter(context);
347 MediacontentMediaPtr objMedia = getMediaObject(object);
348 return converter.toJSValueRef(objMedia->getRating());
352 LogWarning("trying to get incorrect value");
354 return JSValueMakeUndefined(context);
357 JSValueRef JSMedia::getPropertyType(
358 JSContextRef context,
360 JSStringRef propertyName,
361 JSValueRef* exception)
363 LogDebug("getPropertyType::entered");
366 Converter converter(context);
367 MediacontentMediaPtr media = getMediaObject(object);
368 return converter.toJSValueRef(media->getMediaType());
372 LogWarning("trying to get incorrect value");
374 return JSValueMakeUndefined(context);
378 JSValueRef JSMedia::getPropertySize(
379 JSContextRef context,
381 JSStringRef propertyName,
382 JSValueRef* exception)
384 LogDebug("getPropertyType::entered");
387 Converter converter(context);
388 MediacontentMediaPtr media = getMediaObject(object);
389 LogDebug("size:" << media->getSize());
390 double var = (double)media->getSize(); //casting.
391 return converter.toJSValueRef(var);
395 LogWarning("trying to get incorrect value");
398 return JSValueMakeUndefined(context);
402 JSValueRef JSMedia::getPropertyEditableAttr(
403 JSContextRef context,
405 JSStringRef propertyName,
406 JSValueRef* exception)
410 Converter converter(context);
411 MediacontentMediaPtr media = getMediaObject(object);
412 vector<std::string> editableAttrList = media->getEditableAttr();
413 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
415 if (NULL == jsResult)
417 ThrowMsg(NullPointerException, "Could not create js array object");
419 for(unsigned int i=0; i<editableAttrList.size(); i++)
421 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
422 if(!JSSetArrayElement(context, jsResult, i, val))
424 ThrowMsg(UnknownException, "Could not insert value into js array");
431 LogWarning("trying to get incorrect value");
433 return JSValueMakeUndefined(context);
438 bool JSMedia::setPropertyRating(
439 JSContextRef context,
441 JSStringRef propertyName,
443 JSValueRef* exception)
448 Converter converter(context);
449 MediacontentMediaPtr objMedia = getMediaObject(object);
450 int rating = converter.toInt(value);
452 LogDebug("Inserted value : " << rating);
457 LogDebug("Rating value set 0 as inserted value is too small");
460 if ( objMedia->getRating() != rating)
462 objMedia->setRating(rating, true);
469 LogWarning("trying to set incorrect value");
470 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
477 bool JSMedia::setPropertyDisplayName(
478 JSContextRef context,
480 JSStringRef propertyName,
482 JSValueRef* exception)
487 Converter converter(context);
488 MediacontentMediaPtr objMedia = getMediaObject(object);
489 string displayName = converter.toString(value);
491 LogDebug("Inserted value : " << displayName);
493 if ((objMedia->getDescription()).compare(displayName) != 0)
495 objMedia->setDisplayName(displayName, true);
502 LogWarning("trying to set incorrect value");
503 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
509 bool JSMedia::setPropertyDescription(
510 JSContextRef context,
512 JSStringRef propertyName,
514 JSValueRef* exception)
519 Converter converter(context);
520 MediacontentMediaPtr objMedia = getMediaObject(object);
521 string description = converter.toString(value);
523 LogDebug("Inserted value : " << description);
525 if ( (objMedia->getDescription()).compare(description) != 0 )
527 objMedia->setDescription(description, true);
534 LogWarning("trying to set incorrect value");
535 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);