Add DRAFT stubs for Vehicle plugin
[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         if (priv != NULL)
110         {
111                 delete (priv);
112                 priv = NULL;
113                 JSObjectSetPrivate(object, NULL);               
114         }
115
116 }
117
118
119
120 const JSClassRef JSFolder::getClassRef()
121 {
122     if (!m_jsClassRef) 
123         {
124             m_jsClassRef = JSClassCreate(&m_classInfo);
125     }
126     return m_jsClassRef;
127 }
128
129 const JSClassDefinition* JSFolder::getClassInfo()
130 {
131     return &m_classInfo;
132 }
133
134
135 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
136 {
137     LogDebug("entered");
138     FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
139     if(!priv) {
140         ThrowMsg(NullPointerException, "Private object is null");
141     }
142     MediacontentFolderPtr result = priv->getObject();
143     if (!result) {
144         ThrowMsg(NullPointerException, "Private object is null");
145     }
146     return result;
147 }
148
149
150
151 JSValueRef JSFolder::getPropertyId(
152                 JSContextRef context, 
153                 JSObjectRef object,
154                 JSStringRef propertyName,
155                 JSValueRef* exception)
156 {
157     LogDebug("entered");
158     Try
159     {
160         Converter converter(context);
161         MediacontentFolderPtr folder = getFolderObject(object);
162
163         return converter.toJSValueRef(folder->getFolderUUID());
164     }
165     Catch(Exception)
166     {
167         LogWarning("trying to get incorrect value");
168     }
169     return JSValueMakeUndefined(context);
170
171 }
172                                         
173 JSValueRef JSFolder::getPropertyName(
174                 JSContextRef context,
175                 JSObjectRef object,
176                 JSStringRef propertyName,
177                 JSValueRef* exception)
178 {
179     LogDebug("entered");
180     Try
181     {
182         Converter converter(context);
183         MediacontentFolderPtr folder = getFolderObject(object);
184         return converter.toJSValueRef(folder->getFolderName());
185     }
186     Catch(Exception)
187     {
188         LogWarning("trying to get incorrect value");
189     }
190     return JSValueMakeUndefined(context);
191 }
192
193
194 JSValueRef JSFolder::getPropertyPath(
195                 JSContextRef context,
196             JSObjectRef object, 
197             JSStringRef propertyName, 
198             JSValueRef* exception)
199 {
200     LogDebug("getPropertyPath::entered");
201     Try
202     {
203         Converter converter(context);
204         MediacontentFolderPtr folder = getFolderObject(object);
205         return converter.toJSValueRef(folder->getFolderPath());
206     }
207     Catch(Exception)
208     {
209         LogWarning("trying to get incorrect value");
210     }
211     return JSValueMakeUndefined(context);
212 }
213
214
215 JSValueRef JSFolder::getPropertyStorageType(
216                 JSContextRef context,
217             JSObjectRef object,
218             JSStringRef propertyName,
219             JSValueRef* exception)
220 {
221     LogDebug("entered");
222     Try
223     {
224         Converter converter(context);
225         MediacontentFolderPtr folder = getFolderObject(object);
226         return converter.toJSValueRef(folder->getFolderStorageType());
227     }
228     Catch(Exception)
229     {
230         LogWarning("trying to get incorrect value");
231     }
232     return JSValueMakeUndefined(context);
233 }
234
235
236 JSValueRef JSFolder::getPropertyModifiedDate(
237                 JSContextRef context,
238                 JSObjectRef object,
239                 JSStringRef propertyName,
240                 JSValueRef* exception)
241 {
242     LogDebug("entered");
243     Try
244     {
245         Converter converter(context);
246         MediacontentFolderPtr folder = getFolderObject(object);
247         return converter.toJSValueRef(folder->getFolderModifiedDate());
248     }
249     Catch(Exception)
250     {
251         LogWarning("trying to get incorrect value");
252     }
253     return JSValueMakeUndefined(context);
254 }
255
256
257 JSValueRef      JSFolder::getPropertyMediaId(
258                 JSContextRef context,
259                 JSObjectRef object,
260                 JSStringRef propertyName,
261                 JSValueRef* exception)
262 {
263     LogDebug("entered");
264     Try
265     {
266         Converter converter(context);
267         MediacontentFolderPtr folder = getFolderObject(object);
268         //folder->getMediaIdList()
269         //MediaIdListPtr        getMediaIdList() const; 
270                 //typedef vector<std::string> MediaIdList;
271                 //typedef DPL::SharedPtr<MediaIdList> MediaIdListPtr;
272                 MediaIdListPtr mediaIdLstPtr = folder->getMediaIdList();
273                 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
274
275                 if(mediaIdLstPtr)
276                 {
277                         if (NULL == jsResult) 
278                         {
279                 ThrowMsg(NullPointerException, "Could not create js array object");
280             }
281             for(unsigned int i=0; i<mediaIdLstPtr->size(); i++) 
282             {
283                 JSValueRef val = converter.toJSValueRef(mediaIdLstPtr->at(i));
284                 if(!JSSetArrayElement(context, jsResult, i, val)) 
285                 {
286                    ThrowMsg(UnknownException, "Could not insert value into js array");
287                 }
288             }
289                 }
290         return jsResult;
291     }
292     Catch(Exception)
293     {
294         LogWarning("trying to get incorrect value");
295     }
296     return JSValueMakeUndefined(context);
297
298 }
299
300
301
302 }
303 }
304 }