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)
275 ThrowMsg(NullPointerException, "Could not create js array object");
278 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
279 if(!JSSetArrayElement(context, jsResult, 0, val))
281 ThrowMsg(UnknownException, "Could not insert value into js array");
289 LogWarning("trying to get incorrect value");
291 return JSValueMakeUndefined(context);
294 JSValueRef JSMedia::getPropertyDescription(
295 JSContextRef context,
297 JSStringRef propertyName,
298 JSValueRef* exception)
300 LogDebug("getPropertyDescription::entered");
303 Converter converter(context);
304 MediacontentMediaPtr media = getMediaObject(object);
305 return converter.toJSValueRef(media->getDescription());
309 LogWarning("trying to get incorrect value");
311 return JSValueMakeUndefined(context);
315 JSValueRef JSMedia::getPropertyModifiedDate(
316 JSContextRef context,
318 JSStringRef propertyName,
319 JSValueRef* exception)
324 Converter converter(context);
325 MediacontentMediaPtr media = getMediaObject(object);
326 return converter.toJSValueRef(media->getModifiedDate());
330 LogWarning("trying to get incorrect value");
332 return JSValueMakeUndefined(context);
335 JSValueRef JSMedia::getPropertyReleasedDate(
336 JSContextRef context,
338 JSStringRef propertyName,
339 JSValueRef* exception)
344 Converter converter(context);
345 MediacontentMediaPtr media = getMediaObject(object);
346 return converter.toJSValueRef(media->getReleasedDate());
350 LogWarning("trying to get incorrect value");
352 return JSValueMakeUndefined(context);
357 JSValueRef JSMedia::getPropertyRating(
358 JSContextRef context,
360 JSStringRef propertyName,
361 JSValueRef* exception)
366 Converter converter(context);
367 MediacontentMediaPtr objMedia = getMediaObject(object);
368 return converter.toJSValueRef(objMedia->getRating());
372 LogWarning("trying to get incorrect value");
374 return JSValueMakeUndefined(context);
377 JSValueRef JSMedia::getPropertyType(
378 JSContextRef context,
380 JSStringRef propertyName,
381 JSValueRef* exception)
383 LogDebug("getPropertyType::entered");
386 Converter converter(context);
387 MediacontentMediaPtr media = getMediaObject(object);
388 return converter.toJSValueRef(media->getMediaType());
392 LogWarning("trying to get incorrect value");
394 return JSValueMakeUndefined(context);
398 JSValueRef JSMedia::getPropertySize(
399 JSContextRef context,
401 JSStringRef propertyName,
402 JSValueRef* exception)
404 LogDebug("getPropertyType::entered");
407 Converter converter(context);
408 MediacontentMediaPtr media = getMediaObject(object);
409 LogDebug("size:" << media->getSize());
410 double var = (double)media->getSize(); //casting.
411 return converter.toJSValueRef(var);
415 LogWarning("trying to get incorrect value");
418 return JSValueMakeUndefined(context);
422 JSValueRef JSMedia::getPropertyEditableAttr(
423 JSContextRef context,
425 JSStringRef propertyName,
426 JSValueRef* exception)
430 Converter converter(context);
431 MediacontentMediaPtr media = getMediaObject(object);
432 vector<std::string> editableAttrList = media->getEditableAttr();
433 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
435 if (NULL == jsResult)
437 ThrowMsg(NullPointerException, "Could not create js array object");
439 for(unsigned int i=0; i<editableAttrList.size(); i++)
441 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
442 if(!JSSetArrayElement(context, jsResult, i, val))
444 ThrowMsg(UnknownException, "Could not insert value into js array");
451 LogWarning("trying to get incorrect value");
453 return JSValueMakeUndefined(context);
458 bool JSMedia::setPropertyRating(
459 JSContextRef context,
461 JSStringRef propertyName,
463 JSValueRef* exception)
468 Converter converter(context);
469 MediacontentMediaPtr objMedia = getMediaObject(object);
470 int rating = converter.toInt(value);
472 LogDebug("Inserted value : " << rating);
477 LogDebug("Rating value set 0 as inserted value is too small");
480 if ( objMedia->getRating() != rating)
482 objMedia->setRating(rating, true);
489 LogWarning("trying to set incorrect value");
490 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
497 bool JSMedia::setPropertyDisplayName(
498 JSContextRef context,
500 JSStringRef propertyName,
502 JSValueRef* exception)
507 Converter converter(context);
508 MediacontentMediaPtr objMedia = getMediaObject(object);
509 string displayName = converter.toString(value);
511 LogDebug("Inserted value : " << displayName);
513 if ((objMedia->getDescription()).compare(displayName) != 0)
515 objMedia->setDisplayName(displayName, true);
522 LogWarning("trying to set incorrect value");
523 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
529 bool JSMedia::setPropertyDescription(
530 JSContextRef context,
532 JSStringRef propertyName,
534 JSValueRef* exception)
539 Converter converter(context);
540 MediacontentMediaPtr objMedia = getMediaObject(object);
541 string description = converter.toString(value);
543 LogDebug("Inserted value : " << description);
545 if ( (objMedia->getDescription()).compare(description) != 0 )
547 objMedia->setDescription(description, true);
554 LogWarning("trying to set incorrect value");
555 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);