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.
18 #include <CommonsJavaScript/PrivateObject.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <JSTizenException.h>
22 #include <JSTizenExceptionFactory.h>
23 #include "JSContent.h"
29 #define TIZEN_CONTENT_MEDIA_ATTRIBUTENAME "Content"
30 #define TIZEN_CONTENT_MEDIA_UID "id"
31 #define TIZEN_CONTENT_MEDIA_TYPE "type"
32 #define TIZEN_CONTENT_MEDIA_MIME_TYPE "mimeType"
33 #define TIZEN_CONTENT_MEDIA_NAME "name"
34 #define TIZEN_CONTENT_MEDIA_TITLE "title"
35 #define TIZEN_CONTENT_MEDIA_FILE_URL "contentURI"
36 #define TIZEN_CONTENT_MEDIA_THUMBNAILPATH "thumbnailURIs"
37 #define TIZEN_CONTENT_MEDIA_RELEASEDDATE "releaseDate"
38 #define TIZEN_CONTENT_MEDIA_MODIFIEDDATE "modifiedDate"
39 #define TIZEN_CONTENT_MEDIA_DESCRIPTION "description"
40 #define TIZEN_CONTENT_MEDIA_RATING "rating"
41 #define TIZEN_CONTENT_MEDIA_SIZE "size"
42 #define TIZEN_CONTENT_MEDIA_EDIABLEATTR "editableAttributes"
45 JSClassDefinition JSMedia::m_classInfo =
48 kJSClassAttributeNone,
49 TIZEN_CONTENT_MEDIA_ATTRIBUTENAME,
58 NULL, //DeleteProperty,
59 NULL, //GetPropertyNames,
60 NULL, //CallAsFunction,
61 NULL, //CallAsConstructor,
67 JSStaticValue JSMedia::m_property[] =
70 { TIZEN_CONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
71 { TIZEN_CONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
72 { TIZEN_CONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
73 { TIZEN_CONTENT_MEDIA_NAME, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
74 { TIZEN_CONTENT_MEDIA_TITLE, getPropertyTitle, NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_CONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_CONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
77 { TIZEN_CONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_CONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
79 { TIZEN_CONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},
80 { TIZEN_CONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_CONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},
82 { TIZEN_CONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, kJSPropertyAttributeNone},
87 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
89 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
92 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
95 MediacontentMediaPtr privateData(new MediacontentMedia());
96 priv = new MediaPrivObject(context, privateData);
97 JSObjectSetPrivate(object, static_cast<void*>(priv));
98 LoggerD("new event is created" );
102 LoggerD("private object already exists");
106 void JSMedia::finalize(JSObjectRef object)
108 LoggerD( "entered" );
109 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
114 JSObjectSetPrivate(object, NULL);
119 const JSClassRef JSMedia::getClassRef()
121 LoggerD("JSMedia::getClassRef()");
124 m_jsClassRef = JSClassCreate(&m_classInfo);
129 const JSClassDefinition* JSMedia::getClassInfo()
134 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
137 MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
140 ThrowMsg(NullPointerException, "Private object is null");
142 MediacontentMediaPtr result = priv->getObject();
145 ThrowMsg(NullPointerException, "Private object is null");
150 JSValueRef JSMedia::getPropertyId(
151 JSContextRef context,
153 JSStringRef propertyName,
154 JSValueRef* exception)
159 Converter converter(context);
160 MediacontentMediaPtr media = getMediaObject(object);
162 return converter.toJSValueRef(media->getMediaUUID());
166 LoggerW("trying to get incorrect value");
168 return JSValueMakeUndefined(context);
172 JSValueRef JSMedia::getPropertyMimeType(
173 JSContextRef context,
175 JSStringRef propertyName,
176 JSValueRef* exception)
181 Converter converter(context);
182 MediacontentMediaPtr media = getMediaObject(object);
183 return converter.toJSValueRef(media->getMimeType());
187 LoggerW("trying to get incorrect value");
189 return JSValueMakeUndefined(context);
194 JSValueRef JSMedia::getPropertyDisplayName(
195 JSContextRef context,
197 JSStringRef propertyName,
198 JSValueRef* exception)
200 LoggerD("getPropertyDisplayName::entered");
203 Converter converter(context);
204 MediacontentMediaPtr media = getMediaObject(object);
205 LoggerD("getDisplayName:"<<media->getDisplayName());
206 return converter.toJSValueRef(media->getDisplayName());
210 LoggerW("trying to get incorrect value");
212 return JSValueMakeUndefined(context);
215 JSValueRef JSMedia::getPropertyTitle(
216 JSContextRef context,
218 JSStringRef propertyName,
219 JSValueRef* exception)
221 LoggerD("getPropertyDisplayName::entered");
224 Converter converter(context);
225 MediacontentMediaPtr media = getMediaObject(object);
226 LoggerD("getTitle:"<<media->getTitle());
227 return converter.toJSValueRef(media->getTitle());
231 LoggerW("trying to get incorrect value");
233 return JSValueMakeUndefined(context);
237 JSValueRef JSMedia::getPropertyFilePath(
238 JSContextRef context,
240 JSStringRef propertyName,
241 JSValueRef* exception)
246 Converter converter(context);
247 MediacontentMediaPtr media = getMediaObject(object);
248 return converter.toJSValueRef(media->getFilePath());
252 LoggerW("trying to get incorrect value");
254 return JSValueMakeUndefined(context);
258 JSValueRef JSMedia::getPropertyThumbnailPath(
259 JSContextRef context,
261 JSStringRef propertyName,
262 JSValueRef* exception)
267 Converter converter(context);
268 MediacontentMediaPtr media = getMediaObject(object);
269 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
270 if (NULL == jsResult){
271 ThrowMsg(NullPointerException, "Could not create js array object");
274 if(!(media->getThumbnailPath().empty())){
275 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
276 if(!JSSetArrayElement(context, jsResult, 0, val)){
277 ThrowMsg(UnknownException, "Could not insert value into js array");
284 LoggerW("trying to get incorrect value");
286 return JSValueMakeNull(context);
289 JSValueRef JSMedia::getPropertyDescription(
290 JSContextRef context,
292 JSStringRef propertyName,
293 JSValueRef* exception)
295 LoggerD("getPropertyDescription::entered");
298 Converter converter(context);
299 MediacontentMediaPtr media = getMediaObject(object);
300 if(!(media->getDescription().empty()))
302 return converter.toJSValueRef(media->getDescription());
307 LoggerW("trying to get incorrect value");
309 return JSValueMakeNull(context);
313 JSValueRef JSMedia::getPropertyModifiedDate(
314 JSContextRef context,
316 JSStringRef propertyName,
317 JSValueRef* exception)
322 Converter converter(context);
323 MediacontentMediaPtr media = getMediaObject(object);
324 if(media->getModifiedDate() != NULL)
326 return converter.toJSValueRef(*(media->getModifiedDate()));
331 LoggerW("trying to get incorrect value");
333 return JSValueMakeNull(context);
336 JSValueRef JSMedia::getPropertyReleasedDate(
337 JSContextRef context,
339 JSStringRef propertyName,
340 JSValueRef* exception)
345 Converter converter(context);
346 MediacontentMediaPtr media = getMediaObject(object);
347 if(media->getReleasedDate() != NULL)
349 return converter.toJSValueRef(*(media->getReleasedDate()));
354 LoggerW("trying to get incorrect value");
356 return JSValueMakeNull(context);
361 JSValueRef JSMedia::getPropertyRating(
362 JSContextRef context,
364 JSStringRef propertyName,
365 JSValueRef* exception)
370 Converter converter(context);
371 MediacontentMediaPtr objMedia = getMediaObject(object);
372 return converter.toJSValueRef(objMedia->getRating());
376 LoggerW("trying to get incorrect value");
378 return JSValueMakeNull(context);
381 JSValueRef JSMedia::getPropertyType(
382 JSContextRef context,
384 JSStringRef propertyName,
385 JSValueRef* exception)
387 LoggerD("getPropertyType::entered");
390 Converter converter(context);
391 MediacontentMediaPtr media = getMediaObject(object);
392 return converter.toJSValueRef(media->getMediaType());
396 LoggerW("trying to get incorrect value");
398 return JSValueMakeUndefined(context);
402 JSValueRef JSMedia::getPropertySize(
403 JSContextRef context,
405 JSStringRef propertyName,
406 JSValueRef* exception)
408 LoggerD("getPropertyType::entered");
411 Converter converter(context);
412 MediacontentMediaPtr media = getMediaObject(object);
413 LoggerD("size:" << media->getSize());
414 double var = (double)media->getSize(); //casting.
415 return converter.toJSValueRef(var);
419 LoggerW("trying to get incorrect value");
422 return JSValueMakeNull(context);
426 JSValueRef JSMedia::getPropertyEditableAttr(
427 JSContextRef context,
429 JSStringRef propertyName,
430 JSValueRef* exception)
434 Converter converter(context);
435 MediacontentMediaPtr media = getMediaObject(object);
436 vector<std::string> editableAttrList = media->getEditableAttr();
437 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
439 if (NULL == jsResult)
441 ThrowMsg(NullPointerException, "Could not create js array object");
443 for(unsigned int i=0; i<editableAttrList.size(); i++)
445 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
446 if(!JSSetArrayElement(context, jsResult, i, val))
448 ThrowMsg(UnknownException, "Could not insert value into js array");
455 LoggerW("trying to get incorrect value");
457 return JSValueMakeUndefined(context);
462 bool JSMedia::setPropertyRating(
463 JSContextRef context,
465 JSStringRef propertyName,
467 JSValueRef* exception)
472 Converter converter(context);
473 MediacontentMediaPtr objMedia = getMediaObject(object);
474 int rating = converter.toInt(value);
476 LoggerD("Inserted value : " << rating);
481 LoggerD("Rating value set 0 as inserted value is too small");
484 if ( objMedia->getRating() != rating)
486 objMedia->setRating(rating, true);
493 LoggerW("trying to set incorrect value");
494 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
501 bool JSMedia::setPropertyDisplayName(
502 JSContextRef context,
504 JSStringRef propertyName,
506 JSValueRef* exception)
511 Converter converter(context);
512 MediacontentMediaPtr objMedia = getMediaObject(object);
513 string displayName = converter.toString(value);
515 LoggerD("Inserted value : " << displayName);
517 if ((objMedia->getDescription()).compare(displayName) != 0)
519 objMedia->setDisplayName(displayName, true);
526 LoggerW("trying to set incorrect value");
527 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);
533 bool JSMedia::setPropertyDescription(
534 JSContextRef context,
536 JSStringRef propertyName,
538 JSValueRef* exception)
543 Converter converter(context);
544 MediacontentMediaPtr objMedia = getMediaObject(object);
545 string description = converter.toString(value);
547 LoggerD("Inserted value : " << description);
549 if ( (objMedia->getDescription()).compare(description) != 0 )
551 objMedia->setDescription(description, true);
558 LoggerW("trying to set incorrect value");
559 DeviceAPI::Common::JSTizenExceptionFactory::postException(context, exception, DeviceAPI::Common::JSTizenException::TYPE_MISMATCH_ERROR);