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 if(media->getModifiedDate() != NULL)
328 return converter.toJSValueRef(*(media->getModifiedDate()));
333 LogWarning("trying to get incorrect value");
335 return JSValueMakeUndefined(context);
338 JSValueRef JSMedia::getPropertyReleasedDate(
339 JSContextRef context,
341 JSStringRef propertyName,
342 JSValueRef* exception)
347 Converter converter(context);
348 MediacontentMediaPtr media = getMediaObject(object);
349 if(media->getReleasedDate() != NULL)
351 return converter.toJSValueRef(*(media->getReleasedDate()));
356 LogWarning("trying to get incorrect value");
358 return JSValueMakeUndefined(context);
363 JSValueRef JSMedia::getPropertyRating(
364 JSContextRef context,
366 JSStringRef propertyName,
367 JSValueRef* exception)
372 Converter converter(context);
373 MediacontentMediaPtr objMedia = getMediaObject(object);
374 return converter.toJSValueRef(objMedia->getRating());
378 LogWarning("trying to get incorrect value");
380 return JSValueMakeUndefined(context);
383 JSValueRef JSMedia::getPropertyType(
384 JSContextRef context,
386 JSStringRef propertyName,
387 JSValueRef* exception)
389 LogDebug("getPropertyType::entered");
392 Converter converter(context);
393 MediacontentMediaPtr media = getMediaObject(object);
394 return converter.toJSValueRef(media->getMediaType());
398 LogWarning("trying to get incorrect value");
400 return JSValueMakeUndefined(context);
404 JSValueRef JSMedia::getPropertySize(
405 JSContextRef context,
407 JSStringRef propertyName,
408 JSValueRef* exception)
410 LogDebug("getPropertyType::entered");
413 Converter converter(context);
414 MediacontentMediaPtr media = getMediaObject(object);
415 LogDebug("size:" << media->getSize());
416 double var = (double)media->getSize(); //casting.
417 return converter.toJSValueRef(var);
421 LogWarning("trying to get incorrect value");
424 return JSValueMakeUndefined(context);
428 JSValueRef JSMedia::getPropertyEditableAttr(
429 JSContextRef context,
431 JSStringRef propertyName,
432 JSValueRef* exception)
436 Converter converter(context);
437 MediacontentMediaPtr media = getMediaObject(object);
438 vector<std::string> editableAttrList = media->getEditableAttr();
439 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
441 if (NULL == jsResult)
443 ThrowMsg(NullPointerException, "Could not create js array object");
445 for(unsigned int i=0; i<editableAttrList.size(); i++)
447 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
448 if(!JSSetArrayElement(context, jsResult, i, val))
450 ThrowMsg(UnknownException, "Could not insert value into js array");
457 LogWarning("trying to get incorrect value");
459 return JSValueMakeUndefined(context);
464 bool JSMedia::setPropertyRating(
465 JSContextRef context,
467 JSStringRef propertyName,
469 JSValueRef* exception)
474 Converter converter(context);
475 MediacontentMediaPtr objMedia = getMediaObject(object);
476 int rating = converter.toInt(value);
478 LogDebug("Inserted value : " << rating);
483 LogDebug("Rating value set 0 as inserted value is too small");
486 if ( objMedia->getRating() != rating)
488 objMedia->setRating(rating, true);
495 LogWarning("trying to set incorrect value");
496 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
503 bool JSMedia::setPropertyDisplayName(
504 JSContextRef context,
506 JSStringRef propertyName,
508 JSValueRef* exception)
513 Converter converter(context);
514 MediacontentMediaPtr objMedia = getMediaObject(object);
515 string displayName = converter.toString(value);
517 LogDebug("Inserted value : " << displayName);
519 if ((objMedia->getDescription()).compare(displayName) != 0)
521 objMedia->setDisplayName(displayName, true);
528 LogWarning("trying to set incorrect value");
529 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
535 bool JSMedia::setPropertyDescription(
536 JSContextRef context,
538 JSStringRef propertyName,
540 JSValueRef* exception)
545 Converter converter(context);
546 MediacontentMediaPtr objMedia = getMediaObject(object);
547 string description = converter.toString(value);
549 LogDebug("Inserted value : " << description);
551 if ( (objMedia->getDescription()).compare(description) != 0 )
553 objMedia->setDescription(description, true);
560 LogWarning("trying to set incorrect value");
561 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);