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 <JSWebAPIErrorFactory.h>
22 #include "JSContent.h"
28 #define TIZEN_CONTENT_MEDIA_ATTRIBUTENAME "Content"
29 #define TIZEN_CONTENT_MEDIA_UID "id"
30 #define TIZEN_CONTENT_MEDIA_TYPE "type"
31 #define TIZEN_CONTENT_MEDIA_MIME_TYPE "mimeType"
32 #define TIZEN_CONTENT_MEDIA_NAME "name"
33 #define TIZEN_CONTENT_MEDIA_TITLE "title"
34 #define TIZEN_CONTENT_MEDIA_FILE_URL "contentURI"
35 #define TIZEN_CONTENT_MEDIA_THUMBNAILPATH "thumbnailURIs"
36 #define TIZEN_CONTENT_MEDIA_RELEASEDDATE "releaseDate"
37 #define TIZEN_CONTENT_MEDIA_MODIFIEDDATE "modifiedDate"
38 #define TIZEN_CONTENT_MEDIA_DESCRIPTION "description"
39 #define TIZEN_CONTENT_MEDIA_RATING "rating"
40 #define TIZEN_CONTENT_MEDIA_SIZE "size"
41 #define TIZEN_CONTENT_MEDIA_EDIABLEATTR "editableAttributes"
44 JSClassDefinition JSMedia::m_classInfo =
47 kJSClassAttributeNone,
48 TIZEN_CONTENT_MEDIA_ATTRIBUTENAME,
57 NULL, //DeleteProperty,
58 NULL, //GetPropertyNames,
59 NULL, //CallAsFunction,
60 NULL, //CallAsConstructor,
66 JSStaticValue JSMedia::m_property[] =
69 { TIZEN_CONTENT_MEDIA_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
70 { TIZEN_CONTENT_MEDIA_TYPE, getPropertyType , NULL, kJSPropertyAttributeReadOnly},
71 { TIZEN_CONTENT_MEDIA_MIME_TYPE, getPropertyMimeType , NULL, kJSPropertyAttributeReadOnly},
72 { TIZEN_CONTENT_MEDIA_NAME, getPropertyDisplayName, setPropertyDisplayName, kJSPropertyAttributeNone},
73 { TIZEN_CONTENT_MEDIA_TITLE, getPropertyTitle, NULL, kJSPropertyAttributeReadOnly},
74 { TIZEN_CONTENT_MEDIA_FILE_URL, getPropertyFilePath, NULL, kJSPropertyAttributeReadOnly},
75 { TIZEN_CONTENT_MEDIA_THUMBNAILPATH, getPropertyThumbnailPath, NULL, kJSPropertyAttributeReadOnly},
76 { TIZEN_CONTENT_MEDIA_RELEASEDDATE, getPropertyReleasedDate, NULL, kJSPropertyAttributeReadOnly},
77 { TIZEN_CONTENT_MEDIA_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly},
78 { TIZEN_CONTENT_MEDIA_DESCRIPTION, getPropertyDescription, setPropertyDescription, kJSPropertyAttributeNone},
79 { TIZEN_CONTENT_MEDIA_SIZE, getPropertySize, NULL, kJSPropertyAttributeReadOnly},
80 { TIZEN_CONTENT_MEDIA_EDIABLEATTR, getPropertyEditableAttr, NULL, kJSPropertyAttributeReadOnly},
81 { TIZEN_CONTENT_MEDIA_RATING, getPropertyRating, setPropertyRating, kJSPropertyAttributeNone},
86 JSClassRef JSMedia::m_jsClassRef = JSClassCreate(JSMedia::getClassInfo());
88 void JSMedia::initialize(JSContextRef context, JSObjectRef object)
90 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) );
93 MediacontentMediaPtr privateData(new MediacontentMedia());
94 priv = new MediaPrivObject(context, privateData);
95 JSObjectSetPrivate(object, static_cast<void*>(priv));
99 LoggerD("private object already exists");
103 void JSMedia::finalize(JSObjectRef object)
105 MediaPrivObject *priv = static_cast<MediaPrivObject*>( JSObjectGetPrivate( object ) ) ;
110 JSObjectSetPrivate(object, NULL);
114 const JSClassRef JSMedia::getClassRef()
118 m_jsClassRef = JSClassCreate(&m_classInfo);
123 const JSClassDefinition* JSMedia::getClassInfo()
128 MediacontentMediaPtr JSMedia::getMediaObject(JSObjectRef object)
130 MediaPrivObject *priv = static_cast<MediaPrivObject*>(JSObjectGetPrivate(object));
133 ThrowMsg(NullPointerException, "Private object is null");
135 MediacontentMediaPtr result = priv->getObject();
138 ThrowMsg(NullPointerException, "Private object is null");
143 JSValueRef JSMedia::getPropertyId(
144 JSContextRef context,
146 JSStringRef propertyName,
147 JSValueRef* exception)
151 Converter converter(context);
152 MediacontentMediaPtr media = getMediaObject(object);
153 return converter.toJSValueRef(media->getMediaUUID());
157 LoggerW("trying to get incorrect value");
159 return JSValueMakeUndefined(context);
163 JSValueRef JSMedia::getPropertyMimeType(
164 JSContextRef context,
166 JSStringRef propertyName,
167 JSValueRef* exception)
171 Converter converter(context);
172 MediacontentMediaPtr media = getMediaObject(object);
173 return converter.toJSValueRef(media->getMimeType());
177 LoggerW("trying to get incorrect value");
179 return JSValueMakeUndefined(context);
184 JSValueRef JSMedia::getPropertyDisplayName(
185 JSContextRef context,
187 JSStringRef propertyName,
188 JSValueRef* exception)
192 Converter converter(context);
193 MediacontentMediaPtr media = getMediaObject(object);
194 return converter.toJSValueRef(media->getDisplayName());
198 LoggerW("trying to get incorrect value");
200 return JSValueMakeUndefined(context);
203 JSValueRef JSMedia::getPropertyTitle(
204 JSContextRef context,
206 JSStringRef propertyName,
207 JSValueRef* exception)
211 Converter converter(context);
212 MediacontentMediaPtr media = getMediaObject(object);
213 return converter.toJSValueRef(media->getTitle());
217 LoggerW("trying to get incorrect value");
219 return JSValueMakeUndefined(context);
223 JSValueRef JSMedia::getPropertyFilePath(
224 JSContextRef context,
226 JSStringRef propertyName,
227 JSValueRef* exception)
231 Converter converter(context);
232 MediacontentMediaPtr media = getMediaObject(object);
233 return converter.toJSValueRef(media->getFilePath());
237 LoggerW("trying to get incorrect value");
239 return JSValueMakeUndefined(context);
243 JSValueRef JSMedia::getPropertyThumbnailPath(
244 JSContextRef context,
246 JSStringRef propertyName,
247 JSValueRef* exception)
251 Converter converter(context);
252 MediacontentMediaPtr media = getMediaObject(object);
253 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
254 if (NULL == jsResult){
255 ThrowMsg(NullPointerException, "Could not create js array object");
258 if(!(media->getThumbnailPath().empty())){
259 JSValueRef val = converter.toJSValueRef(media->getThumbnailPath());
260 if(!JSSetArrayElement(context, jsResult, 0, val)){
261 ThrowMsg(UnknownException, "Could not insert value into js array");
268 LoggerW("trying to get incorrect value");
270 return JSValueMakeNull(context);
273 JSValueRef JSMedia::getPropertyDescription(
274 JSContextRef context,
276 JSStringRef propertyName,
277 JSValueRef* exception)
281 Converter converter(context);
282 MediacontentMediaPtr media = getMediaObject(object);
283 if(!(media->getDescription().empty()))
285 return converter.toJSValueRef(media->getDescription());
290 LoggerW("trying to get incorrect value");
292 return JSValueMakeNull(context);
296 JSValueRef JSMedia::getPropertyModifiedDate(
297 JSContextRef context,
299 JSStringRef propertyName,
300 JSValueRef* exception)
304 Converter converter(context);
305 MediacontentMediaPtr media = getMediaObject(object);
306 if(media->getModifiedDate() != NULL)
308 return converter.toJSValueRef(*(media->getModifiedDate()));
313 LoggerW("trying to get incorrect value");
315 return JSValueMakeNull(context);
318 JSValueRef JSMedia::getPropertyReleasedDate(
319 JSContextRef context,
321 JSStringRef propertyName,
322 JSValueRef* exception)
326 Converter converter(context);
327 MediacontentMediaPtr media = getMediaObject(object);
328 if(media->getReleasedDate() != NULL)
330 return converter.toJSValueRef(*(media->getReleasedDate()));
335 LoggerW("trying to get incorrect value");
337 return JSValueMakeNull(context);
342 JSValueRef JSMedia::getPropertyRating(
343 JSContextRef context,
345 JSStringRef propertyName,
346 JSValueRef* exception)
350 Converter converter(context);
351 MediacontentMediaPtr objMedia = getMediaObject(object);
352 return converter.toJSValueRef(objMedia->getRating());
356 LoggerW("trying to get incorrect value");
358 return JSValueMakeNull(context);
361 JSValueRef JSMedia::getPropertyType(
362 JSContextRef context,
364 JSStringRef propertyName,
365 JSValueRef* exception)
369 Converter converter(context);
370 MediacontentMediaPtr media = getMediaObject(object);
371 return converter.toJSValueRef(media->getMediaType());
375 LoggerW("trying to get incorrect value");
377 return JSValueMakeUndefined(context);
381 JSValueRef JSMedia::getPropertySize(
382 JSContextRef context,
384 JSStringRef propertyName,
385 JSValueRef* exception)
389 Converter converter(context);
390 MediacontentMediaPtr media = getMediaObject(object);
391 double var = (double)media->getSize();
392 return converter.toJSValueRef(var);
396 LoggerW("trying to get incorrect value");
399 return JSValueMakeNull(context);
403 JSValueRef JSMedia::getPropertyEditableAttr(
404 JSContextRef context,
406 JSStringRef propertyName,
407 JSValueRef* exception)
411 Converter converter(context);
412 MediacontentMediaPtr media = getMediaObject(object);
413 vector<std::string> editableAttrList = media->getEditableAttr();
414 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
416 if (NULL == jsResult)
418 ThrowMsg(NullPointerException, "Could not create js array object");
420 for(unsigned int i=0; i<editableAttrList.size(); i++)
422 JSValueRef val = converter.toJSValueRef(editableAttrList.at(i));
423 if(!JSSetArrayElement(context, jsResult, i, val))
425 ThrowMsg(UnknownException, "Could not insert value into js array");
432 LoggerW("trying to get incorrect value");
434 return JSValueMakeUndefined(context);
439 bool JSMedia::setPropertyRating(
440 JSContextRef context,
442 JSStringRef propertyName,
444 JSValueRef* exception)
448 Converter converter(context);
449 MediacontentMediaPtr objMedia = getMediaObject(object);
450 int rating = converter.toInt(value);
456 if ( objMedia->getRating() != rating)
458 objMedia->setRating(rating, true);
465 LoggerW("trying to set incorrect value");
466 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
473 bool JSMedia::setPropertyDisplayName(
474 JSContextRef context,
476 JSStringRef propertyName,
478 JSValueRef* exception)
482 Converter converter(context);
483 MediacontentMediaPtr objMedia = getMediaObject(object);
484 string displayName = converter.toString(value);
486 if ((objMedia->getDescription()).compare(displayName) != 0)
488 objMedia->setDisplayName(displayName, true);
495 LoggerW("trying to set incorrect value");
496 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);
502 bool JSMedia::setPropertyDescription(
503 JSContextRef context,
505 JSStringRef propertyName,
507 JSValueRef* exception)
511 Converter converter(context);
512 MediacontentMediaPtr objMedia = getMediaObject(object);
513 string description = converter.toString(value);
515 if ( (objMedia->getDescription()).compare(description) != 0 )
517 objMedia->setDescription(description, true);
524 LoggerW("trying to set incorrect value");
525 DeviceAPI::Common::JSWebAPIErrorFactory::postException(context, exception, DeviceAPI::Common::JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR);