merge wrt-plugins-tizen_0.2.0-4
[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                          "folderURI"
43 #define TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE          "storageType"
44 #define TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE          "modifiedDate"
45
46
47 JSClassDefinition JSFolder::m_classInfo =
48 {
49     0,
50     kJSClassAttributeNone,
51     TIZEN_MEDIACONTENT_FOLDER_ATTRIBUTENAME,
52     0,
53     m_property,
54     m_function, //    m_function,
55     initialize,
56     finalize,
57     NULL, //hasProperty,
58     NULL, //getProperty,
59     NULL, //setProperty,
60     NULL, //DeleteProperty,
61     NULL, //GetPropertyNames,
62     NULL, //CallAsFunction,
63     NULL, //CallAsConstructor,
64     NULL, //HasInstance,
65     NULL  //ConvertToType
66 };
67
68 JSStaticValue JSFolder::m_property[] =
69 {
70     //FolderProperties
71     { TIZEN_MEDIACONTENT_FOLDER_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
72     { TIZEN_MEDIACONTENT_FOLDER_NAME, getPropertyName, NULL, kJSPropertyAttributeReadOnly},
73     { TIZEN_MEDIACONTENT_FOLDER_PATH, getPropertyPath, NULL, kJSPropertyAttributeReadOnly},
74     { TIZEN_MEDIACONTENT_FOLDER_STORAGE_TYPE, getPropertyStorageType, NULL, kJSPropertyAttributeReadOnly},
75     { TIZEN_MEDIACONTENT_FOLDER_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly}, 
76     { 0, 0, 0, 0 }
77 };
78
79 JSStaticFunction JSFolder::m_function[] =
80 {
81     { 0, 0, 0 }
82 };
83
84
85 JSClassRef JSFolder::m_jsClassRef = JSClassCreate(JSFolder::getClassInfo());
86
87 void JSFolder::initialize(JSContextRef context, JSObjectRef object)
88 {
89     LogDebug( "entered" );
90     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) );
91     if (!priv) 
92         {
93         MediacontentFolderPtr privateData(new MediacontentFolder());
94         priv = new FolderPrivObject(context, privateData);
95         JSObjectSetPrivate(object, static_cast<void*>(priv));
96         LogDebug("new event is created" );
97     }
98     else 
99         {
100         LogDebug("private object already exists");
101     }
102 }
103
104
105 void JSFolder::finalize(JSObjectRef object)
106 {
107     LogDebug( "entered" );
108     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) ) ;
109     delete priv;
110 }
111
112
113
114 const JSClassRef JSFolder::getClassRef()
115 {
116     if (!m_jsClassRef) 
117         {
118             m_jsClassRef = JSClassCreate(&m_classInfo);
119     }
120     return m_jsClassRef;
121 }
122
123 const JSClassDefinition* JSFolder::getClassInfo()
124 {
125     return &m_classInfo;
126 }
127
128
129 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
130 {
131     LogDebug("entered");
132     FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
133     if(!priv) {
134         ThrowMsg(NullPointerException, "Private object is null");
135     }
136     MediacontentFolderPtr result = priv->getObject();
137     if (!result) {
138         ThrowMsg(NullPointerException, "Private object is null");
139     }
140     return result;
141 }
142
143
144
145 JSValueRef JSFolder::getPropertyId(
146                 JSContextRef context, 
147                 JSObjectRef object,
148                 JSStringRef propertyName,
149                 JSValueRef* exception)
150 {
151     LogDebug("entered");
152     Try
153     {
154         Converter converter(context);
155         MediacontentFolderPtr folder = getFolderObject(object);
156
157         return converter.toJSValueRef(folder->getFolderUUID());
158     }
159     Catch(Exception)
160     {
161         LogWarning("trying to get incorrect value");
162     }
163     return JSValueMakeUndefined(context);
164
165 }
166                                         
167 JSValueRef JSFolder::getPropertyName(
168                 JSContextRef context,
169                 JSObjectRef object,
170                 JSStringRef propertyName,
171                 JSValueRef* exception)
172 {
173     LogDebug("entered");
174     Try
175     {
176         Converter converter(context);
177         MediacontentFolderPtr folder = getFolderObject(object);
178         return converter.toJSValueRef(folder->getFolderName());
179     }
180     Catch(Exception)
181     {
182         LogWarning("trying to get incorrect value");
183     }
184     return JSValueMakeUndefined(context);
185 }
186
187
188 JSValueRef JSFolder::getPropertyPath(
189                 JSContextRef context,
190             JSObjectRef object, 
191             JSStringRef propertyName, 
192             JSValueRef* exception)
193 {
194     LogDebug("getPropertyPath::entered");
195     Try
196     {
197         Converter converter(context);
198         MediacontentFolderPtr folder = getFolderObject(object);
199         return converter.toJSValueRef(folder->getFolderPath());
200     }
201     Catch(Exception)
202     {
203         LogWarning("trying to get incorrect value");
204     }
205     return JSValueMakeUndefined(context);
206 }
207
208
209 JSValueRef JSFolder::getPropertyStorageType(
210                 JSContextRef context,
211             JSObjectRef object,
212             JSStringRef propertyName,
213             JSValueRef* exception)
214 {
215     LogDebug("entered");
216     Try
217     {
218         Converter converter(context);
219         MediacontentFolderPtr folder = getFolderObject(object);
220         return converter.toJSValueRef(folder->getFolderStorageType());
221     }
222     Catch(Exception)
223     {
224         LogWarning("trying to get incorrect value");
225     }
226     return JSValueMakeUndefined(context);
227 }
228
229
230 JSValueRef JSFolder::getPropertyModifiedDate(
231                 JSContextRef context,
232                 JSObjectRef object,
233                 JSStringRef propertyName,
234                 JSValueRef* exception)
235 {
236     LogDebug("entered");
237     Try
238     {
239         Converter converter(context);
240         MediacontentFolderPtr folder = getFolderObject(object);
241         return converter.toJSValueRef(folder->getFolderModifiedDate());
242     }
243     Catch(Exception)
244     {
245         LogWarning("trying to get incorrect value");
246     }
247     return JSValueMakeUndefined(context);
248 }
249
250
251 JSValueRef      JSFolder::getPropertyMediaId(
252                 JSContextRef context,
253                 JSObjectRef object,
254                 JSStringRef propertyName,
255                 JSValueRef* exception)
256 {
257     LogDebug("entered");
258     Try
259     {
260         Converter converter(context);
261         MediacontentFolderPtr folder = getFolderObject(object);
262         //folder->getMediaIdList()
263         //MediaIdListPtr        getMediaIdList() const; 
264                 //typedef vector<std::string> MediaIdList;
265                 //typedef DPL::SharedPtr<MediaIdList> MediaIdListPtr;
266                 MediaIdListPtr mediaIdLstPtr = folder->getMediaIdList();
267                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
268
269                 if(mediaIdLstPtr)
270                 {
271                         if (NULL == jsResult) 
272                         {
273                 ThrowMsg(NullPointerException, "Could not create js array object");
274             }
275             for(unsigned int i=0; i<mediaIdLstPtr->size(); i++) 
276             {
277                 JSValueRef val = converter.toJSValueRef(mediaIdLstPtr->at(i));
278                 if(!JSSetArrayElement(context, jsResult, i, val)) 
279                 {
280                    ThrowMsg(UnknownException, "Could not insert value into js array");
281                 }
282             }
283                 }
284         return jsResult;
285     }
286     Catch(Exception)
287     {
288         LogWarning("trying to get incorrect value");
289     }
290     return JSValueMakeUndefined(context);
291
292 }
293
294
295
296 }
297 }
298 }