Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Mediacontent / JSFolder.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24
25 #include <API/Mediacontent/IMediacontent.h>
26 #include <API/Mediacontent/MediacontentFactory.h>
27 //#include <API/Mediacontent/IEventFolderFindMedia.h>
28
29
30 #include "MediacontentController.h"
31 #include "JSMediacontent.h"
32 #include "JSFolder.h"
33
34
35 namespace TizenApis {
36 namespace Tizen1_0 {
37 namespace Mediacontent {
38
39 #define TIZEN_MEDIACONTENT_FOLDER_ATTRIBUTENAME         "Folder"
40 #define TIZEN_MEDIACONTENT_FOLDER_UID                           "id"
41 #define TIZEN_MEDIACONTENT_FOLDER_NAME                          "title"
42 #define TIZEN_MEDIACONTENT_FOLDER_PATH                          "folderUrl"
43 #define TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE          "storageType"
44 #define TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE          "modifiedDate"
45 #define TIZEN_MEDIACONTENT_FOLDER_MEDIAID                       "mediaItems"
46
47
48 JSClassDefinition JSFolder::m_classInfo =
49 {
50     0,
51     kJSClassAttributeNone,
52     TIZEN_MEDIACONTENT_FOLDER_ATTRIBUTENAME,
53     0,
54     m_property,
55     m_function, //    m_function,
56     initialize,
57     finalize,
58     NULL, //hasProperty,
59     NULL, //getProperty,
60     NULL, //setProperty,
61     NULL, //DeleteProperty,
62     NULL, //GetPropertyNames,
63     NULL, //CallAsFunction,
64     NULL, //CallAsConstructor,
65     NULL, //HasInstance,
66     NULL  //ConvertToType
67 };
68
69 JSStaticValue JSFolder::m_property[] =
70 {
71     //FolderProperties
72     { TIZEN_MEDIACONTENT_FOLDER_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
73     { TIZEN_MEDIACONTENT_FOLDER_NAME, getPropertyName, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_MEDIACONTENT_FOLDER_PATH, getPropertyPath, NULL, kJSPropertyAttributeReadOnly},
75     { TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE, getPropertyStorageType, NULL, kJSPropertyAttributeReadOnly},
76     { TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly}, 
77     { TIZEN_MEDIACONTENT_FOLDER_MEDIAID, getPropertyMediaId, NULL, kJSPropertyAttributeReadOnly}, 
78      { 0, 0, 0, 0 }
79 };
80
81 JSStaticFunction JSFolder::m_function[] =
82 {
83     { 0, 0, 0 }
84 };
85
86
87 JSClassRef JSFolder::m_jsClassRef = JSClassCreate(JSFolder::getClassInfo());
88
89 void JSFolder::initialize(JSContextRef context, JSObjectRef object)
90 {
91     LogDebug( "entered" );
92     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) );
93     if (!priv) 
94         {
95         MediacontentFolderPtr privateData(new MediacontentFolder());
96         priv = new FolderPrivObject(context, privateData);
97         JSObjectSetPrivate(object, static_cast<void*>(priv));
98         LogDebug("new event is created" );
99     }
100     else 
101         {
102         LogDebug("private object already exists");
103     }
104 }
105
106
107 void JSFolder::finalize(JSObjectRef object)
108 {
109     LogDebug( "entered" );
110     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) ) ;
111     delete priv;
112 }
113
114
115
116 const JSClassRef JSFolder::getClassRef()
117 {
118     if (!m_jsClassRef) 
119         {
120             m_jsClassRef = JSClassCreate(&m_classInfo);
121     }
122     return m_jsClassRef;
123 }
124
125 const JSClassDefinition* JSFolder::getClassInfo()
126 {
127     return &m_classInfo;
128 }
129
130
131 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
132 {
133     LogDebug("entered");
134     FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
135     if(!priv) {
136         ThrowMsg(NullPointerException, "Private object is null");
137     }
138     MediacontentFolderPtr result = priv->getObject();
139     if (!result) {
140         ThrowMsg(NullPointerException, "Private object is null");
141     }
142     return result;
143 }
144
145
146
147 JSValueRef JSFolder::getPropertyId(
148                 JSContextRef context, 
149                 JSObjectRef object,
150                 JSStringRef propertyName,
151                 JSValueRef* exception)
152 {
153     LogDebug("entered");
154     Try
155     {
156         Converter converter(context);
157         MediacontentFolderPtr folder = getFolderObject(object);
158
159         return converter.toJSValueRef(folder->getFolderUUID());
160     }
161     Catch(Exception)
162     {
163         LogWarning("trying to get incorrect value");
164     }
165     return JSValueMakeUndefined(context);
166
167 }
168                                         
169 JSValueRef JSFolder::getPropertyName(
170                 JSContextRef context,
171                 JSObjectRef object,
172                 JSStringRef propertyName,
173                 JSValueRef* exception)
174 {
175     LogDebug("entered");
176     Try
177     {
178         Converter converter(context);
179         MediacontentFolderPtr folder = getFolderObject(object);
180         return converter.toJSValueRef(folder->getFolderName());
181     }
182     Catch(Exception)
183     {
184         LogWarning("trying to get incorrect value");
185     }
186     return JSValueMakeUndefined(context);
187 }
188
189
190 JSValueRef JSFolder::getPropertyPath(
191                 JSContextRef context,
192             JSObjectRef object, 
193             JSStringRef propertyName, 
194             JSValueRef* exception)
195 {
196     LogDebug("entered");
197     Try
198     {
199         Converter converter(context);
200         MediacontentFolderPtr folder = getFolderObject(object);
201         return converter.toJSValueRef(folder->getFolderPath());
202     }
203     Catch(Exception)
204     {
205         LogWarning("trying to get incorrect value");
206     }
207     return JSValueMakeUndefined(context);
208 }
209
210
211 JSValueRef JSFolder::getPropertyStorageType(
212                 JSContextRef context,
213             JSObjectRef object,
214             JSStringRef propertyName,
215             JSValueRef* exception)
216 {
217     LogDebug("entered");
218     Try
219     {
220         Converter converter(context);
221         MediacontentFolderPtr folder = getFolderObject(object);
222         return converter.toJSValueRef(folder->getFolderStorageType());
223     }
224     Catch(Exception)
225     {
226         LogWarning("trying to get incorrect value");
227     }
228     return JSValueMakeUndefined(context);
229 }
230
231
232 JSValueRef JSFolder::getPropertyModifiedDate(
233                 JSContextRef context,
234                 JSObjectRef object,
235                 JSStringRef propertyName,
236                 JSValueRef* exception)
237 {
238     LogDebug("entered");
239     Try
240     {
241         Converter converter(context);
242         MediacontentFolderPtr folder = getFolderObject(object);
243         return converter.toJSValueRef(folder->getFolderModifiedDate());
244     }
245     Catch(Exception)
246     {
247         LogWarning("trying to get incorrect value");
248     }
249     return JSValueMakeUndefined(context);
250 }
251
252
253 JSValueRef      JSFolder::getPropertyMediaId(
254                 JSContextRef context,
255                 JSObjectRef object,
256                 JSStringRef propertyName,
257                 JSValueRef* exception)
258 {
259     LogDebug("entered");
260     Try
261     {
262         Converter converter(context);
263         MediacontentFolderPtr folder = getFolderObject(object);
264         //folder->getMediaIdList()
265         //MediaIdListPtr        getMediaIdList() const; 
266                 //typedef vector<std::string> MediaIdList;
267                 //typedef DPL::SharedPtr<MediaIdList> MediaIdListPtr;
268                 MediaIdListPtr mediaIdLstPtr = folder->getMediaIdList();
269                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
270
271                 if(mediaIdLstPtr)
272                 {
273                         if (NULL == jsResult) 
274                         {
275                 ThrowMsg(NullPointerException, "Could not create js array object");
276             }
277             for(unsigned int i=0; i<mediaIdLstPtr->size(); i++) 
278             {
279                 JSValueRef val = converter.toJSValueRef(mediaIdLstPtr->at(i));
280                 if(!JSSetArrayElement(context, jsResult, i, val)) 
281                 {
282                    ThrowMsg(UnknownException, "Could not insert value into js array");
283                 }
284             }
285                 }
286         return jsResult;
287     }
288     Catch(Exception)
289     {
290         LogWarning("trying to get incorrect value");
291     }
292     return JSValueMakeUndefined(context);
293
294 }
295
296
297 /*
298 JSValueRef      JSFolder::findMedia(
299                 JSContextRef context, 
300                 JSObjectRef object,
301                 JSObjectRef thisObject,
302                 size_t argumentCount,
303                 const JSValueRef arguments[],
304                 JSValueRef* exception )
305 {
306         FolderPrivObject *privFolder = static_cast<FolderPrivObject*>(JSObjectGetPrivate(thisObject));
307
308     LogDebug("JSFolder::FolderfindMedia entered" << getClassInfo());
309
310     assert(privFolder);
311
312     JSCallbackManagerPtr cbm(NULL);
313     Try
314     {
315                 IMediacontentPtr mediacontent = Api::MediacontentFactory::getInstance().createMediacontentObject();
316
317         if (argumentCount < 1) 
318                 {
319             LogError("No callback parameters");
320             return JSExceptionFactory::TypeMismatchException.make(context, exception);
321         }
322         JSValueRef onError = (argumentCount > 1 ? getFunctionOrNull(context, arguments[1]) : NULL);
323         JSContextRef globalContext = privFolder->getContext();
324         JSCallbackManagerPtr cbm = JSCallbackManager::createObject(globalContext, NULL, onError);
325         Commons::Validator validator(context);
326         if (validator.isCallback(arguments[0])) 
327                 {
328             cbm->setOnSuccess(arguments[0]);
329         }
330         else if (JSValueIsNull(context, arguments[0]) || JSValueIsUndefined(context, arguments[0]))
331                 {
332             cbm->callOnError(JSExceptionFactory::InvalidValuesException.make(context));
333             return JSValueMakeUndefined(context);
334         }
335         else 
336                 {
337             return JSExceptionFactory::TypeMismatchException.make(context, exception);
338         }
339         IEventFolderFindMediaPtr dplEvent(new IEventFolderFindMedia());
340                 MediacontentFolderPtr folder = getFolderObject(thisObject);
341                 dplEvent->setFolder(folder);
342         dplEvent->setPrivateData( DPL::StaticPointerCast<Platform::IEventPrivateData> (cbm));
343         dplEvent->setForAsynchronousCall(&MediacontentManagerController::getInstance());
344         mediacontent->folderFindMedia(dplEvent);
345
346         return Utils::makePendingOperation(cbm->getContext(), dplEvent);
347     }
348     Catch(Commons::InvalidArgumentException)
349     {
350         LogError("Invalid argument");
351         return JSExceptionFactory::TypeMismatchException.make(context, exception);
352     }
353     Catch(Commons::Exception)
354     {
355         LogError("unknow error occured");
356     }
357     if (NULL != cbm) {
358         cbm->callOnError(JSExceptionFactory::UnknownException.make(privFolder->getContext()));
359     }
360     return JSValueMakeUndefined(context);
361
362 }
363 */
364
365 }
366 }
367 }