2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // 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 <JSTizenException.h>
24 #include <JSTizenExceptionFactory.h>
27 #include "JSContent.h"
32 #define TIZEN_CONTENT_MEDIA_ATTRIBUTENAME "Content"
33 #define TIZEN_CONTENT_MEDIA_UID "id"
34 #define TIZEN_CONTENT_MEDIA_TYPE "type"
35 #define TIZEN_CONTENT_MEDIA_MIME_TYPE "mimeType"
36 #define TIZEN_CONTENT_MEDIA_NAME "name"
37 #define TIZEN_CONTENT_MEDIA_TITLE "title"
38 #define TIZEN_CONTENT_MEDIA_FILE_URL "contentURI"
39 #define TIZEN_CONTENT_MEDIA_THUMBNAILPATH "thumbnailURIs"
40 #define TIZEN_CONTENT_MEDIA_RELEASEDDATE "releaseDate"
41 #define TIZEN_CONTENT_MEDIA_MODIFIEDDATE "modifiedDate"
42 #define TIZEN_CONTENT_MEDIA_DESCRIPTION "description"
43 #define TIZEN_CONTENT_MEDIA_RATING "rating"
44 #define TIZEN_CONTENT_MEDIA_SIZE "size"
45 #define TIZEN_CONTENT_MEDIA_EDIABLEATTR "editableAttributes"
48 JSClassDefinition JSMedia::m_classInfo =
51 kJSClassAttributeNone,
52 TIZEN_CONTENT_MEDIA_ATTRIBUTENAME,
61 NULL, //DeleteProperty,
62 NULL, //GetPropertyNames,
63 NULL, //CallAsFunction,
64 NULL, //CallAsConstructor,
70 JSStaticValue JSMedia::m_property[] =
73 { TIZEN_CONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
74 { TIZEN_CONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_CONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_CONTENT_MEDIA_NAME, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
77 { TIZEN_CONTENT_MEDIA_TITLE, getPropertyTitle, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_CONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
79 { TIZEN_CONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
80 { TIZEN_CONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_CONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
82 { TIZEN_CONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},
83 { TIZEN_CONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},
84 { TIZEN_CONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},
85 { TIZEN_CONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, 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 ) ) ;
117 JSObjectSetPrivate(object, NULL);
122 const JSClassRef JSMedia::getClassRef()
124 LogDebug("JSMedia::getClassRef()");
127 m_jsClassRef = JSClassCreate(&m_classInfo);
132 const JSClassDefinition* JSMedia::getClassInfo()
137 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
140 MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
143 ThrowMsg(NullPointerException, "Private object is null");
145 MediacontentMediaPtr result = priv->getObject();
148 ThrowMsg(NullPointerException, "Private object is null");
153 JSValueRef JSMedia::getPropertyId(
154 JSContextRef context,
156 JSStringRef propertyName,
157 JSValueRef* exception)
162 Converter converter(context);
163 MediacontentMediaPtr media = getMediaObject(object);
165 return converter.toJSValueRef(media->getMediaUUID());
169 LogWarning("trying to get incorrect value");
171 return JSValueMakeUndefined(context);
175 JSValueRef JSMedia::getPropertyMimeType(
176 JSContextRef context,
178 JSStringRef propertyName,
179 JSValueRef* exception)
184 Converter converter(context);
185 MediacontentMediaPtr media = getMediaObject(object);
186 return converter.toJSValueRef(media->getMimeType());
190 LogWarning("trying to get incorrect value");
192 return JSValueMakeUndefined(context);
197 JSValueRef JSMedia::getPropertyDisplayName(
198 JSContextRef context,
200 JSStringRef propertyName,
201 JSValueRef* exception)
203 LogDebug("getPropertyDisplayName::entered");
206 Converter converter(context);
207 MediacontentMediaPtr media = getMediaObject(object);
208 LogDebug("getDisplayName:"<<media->getDisplayName());
209 return converter.toJSValueRef(media->getDisplayName());
213 LogWarning("trying to get incorrect value");
215 return JSValueMakeUndefined(context);
218 JSValueRef JSMedia::getPropertyTitle(
219 JSContextRef context,
221 JSStringRef propertyName,
222 JSValueRef* exception)
224 LogDebug("getPropertyDisplayName::entered");
227 Converter converter(context);
228 MediacontentMediaPtr media = getMediaObject(object);
229 LogDebug("getTitle:"<<media->getTitle());
230 return converter.toJSValueRef(media->getTitle());
234 LogWarning("trying to get incorrect value");
236 return JSValueMakeUndefined(context);
240 JSValueRef JSMedia::getPropertyFilePath(
241 JSContextRef context,
243 JSStringRef propertyName,
244 JSValueRef* exception)
249 Converter converter(context);
250 MediacontentMediaPtr media = getMediaObject(object);
251 return converter.toJSValueRef(media->getFilePath());
255 LogWarning("trying to get incorrect value");
257 return JSValueMakeUndefined(context);
261 JSValueRef JSMedia::getPropertyThumbnailPath(
262 JSContextRef context,
264 JSStringRef propertyName,
265 JSValueRef* exception)
270 Converter converter(context);
271 MediacontentMediaPtr media = getMediaObject(object);
272 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
273 if (NULL == jsResult){
274 ThrowMsg(NullPointerException, "Could not create js array object");
277 if(!(media->getThumbnailPath().empty())){
278 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
279 if(!JSSetArrayElement(context, jsResult, 0, val)){
280 ThrowMsg(UnknownException, "Could not insert value into js array");
287 LogWarning("trying to get incorrect value");
289 return JSValueMakeNull(context);
292 JSValueRef JSMedia::getPropertyDescription(
293 JSContextRef context,
295 JSStringRef propertyName,
296 JSValueRef* exception)
298 LogDebug("getPropertyDescription::entered");
301 Converter converter(context);
302 MediacontentMediaPtr media = getMediaObject(object);
303 if(!(media->getDescription().empty()))
305 return converter.toJSValueRef(media->getDescription());
310 LogWarning("trying to get incorrect value");
312 return JSValueMakeNull(context);
316 JSValueRef JSMedia::getPropertyModifiedDate(
317 JSContextRef context,
319 JSStringRef propertyName,
320 JSValueRef* exception)
325 Converter converter(context);
326 MediacontentMediaPtr media = getMediaObject(object);
327 if(media->getModifiedDate() != NULL)
329 return converter.toJSValueRef(*(media->getModifiedDate()));
334 LogWarning("trying to get incorrect value");
336 return JSValueMakeNull(context);
339 JSValueRef JSMedia::getPropertyReleasedDate(
340 JSContextRef context,
342 JSStringRef propertyName,
343 JSValueRef* exception)
348 Converter converter(context);
349 MediacontentMediaPtr media = getMediaObject(object);
350 if(media->getReleasedDate() != NULL)
352 return converter.toJSValueRef(*(media->getReleasedDate()));
357 LogWarning("trying to get incorrect value");
359 return JSValueMakeNull(context);
364 JSValueRef JSMedia::getPropertyRating(
365 JSContextRef context,
367 JSStringRef propertyName,
368 JSValueRef* exception)
373 Converter converter(context);
374 MediacontentMediaPtr objMedia = getMediaObject(object);
375 return converter.toJSValueRef(objMedia->getRating());
379 LogWarning("trying to get incorrect value");
381 return JSValueMakeNull(context);
384 JSValueRef JSMedia::getPropertyType(
385 JSContextRef context,
387 JSStringRef propertyName,
388 JSValueRef* exception)
390 LogDebug("getPropertyType::entered");
393 Converter converter(context);
394 MediacontentMediaPtr media = getMediaObject(object);
395 return converter.toJSValueRef(media->getMediaType());
399 LogWarning("trying to get incorrect value");
401 return JSValueMakeUndefined(context);
405 JSValueRef JSMedia::getPropertySize(
406 JSContextRef context,
408 JSStringRef propertyName,
409 JSValueRef* exception)
411 LogDebug("getPropertyType::entered");
414 Converter converter(context);
415 MediacontentMediaPtr media = getMediaObject(object);
416 LogDebug("size:" << media->getSize());
417 double var = (double)media->getSize(); //casting.
418 return converter.toJSValueRef(var);
422 LogWarning("trying to get incorrect value");
425 return JSValueMakeNull(context);
429 JSValueRef JSMedia::getPropertyEditableAttr(
430 JSContextRef context,
432 JSStringRef propertyName,
433 JSValueRef* exception)
437 Converter converter(context);
438 MediacontentMediaPtr media = getMediaObject(object);
439 vector<std::string> editableAttrList = media->getEditableAttr();
440 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
442 if (NULL == jsResult)
444 ThrowMsg(NullPointerException, "Could not create js array object");
446 for(unsigned int i=0; i<editableAttrList.size(); i++)
448 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
449 if(!JSSetArrayElement(context, jsResult, i, val))
451 ThrowMsg(UnknownException, "Could not insert value into js array");
458 LogWarning("trying to get incorrect value");
460 return JSValueMakeUndefined(context);
465 bool JSMedia::setPropertyRating(
466 JSContextRef context,
468 JSStringRef propertyName,
470 JSValueRef* exception)
475 Converter converter(context);
476 MediacontentMediaPtr objMedia = getMediaObject(object);
477 int rating = converter.toInt(value);
479 LogDebug("Inserted value : " << rating);
484 LogDebug("Rating value set 0 as inserted value is too small");
487 if ( objMedia->getRating() != rating)
489 objMedia->setRating(rating, true);
496 LogWarning("trying to set incorrect value");
497 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
504 bool JSMedia::setPropertyDisplayName(
505 JSContextRef context,
507 JSStringRef propertyName,
509 JSValueRef* exception)
514 Converter converter(context);
515 MediacontentMediaPtr objMedia = getMediaObject(object);
516 string displayName = converter.toString(value);
518 LogDebug("Inserted value : " << displayName);
520 if ((objMedia->getDescription()).compare(displayName) != 0)
522 objMedia->setDisplayName(displayName, true);
529 LogWarning("trying to set incorrect value");
530 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
536 bool JSMedia::setPropertyDescription(
537 JSContextRef context,
539 JSStringRef propertyName,
541 JSValueRef* exception)
546 Converter converter(context);
547 MediacontentMediaPtr objMedia = getMediaObject(object);
548 string description = converter.toString(value);
550 LogDebug("Inserted value : " << description);
552 if ( (objMedia->getDescription()).compare(description) != 0 )
554 objMedia->setDescription(description, true);
561 LogWarning("trying to set incorrect value");
562 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);