Update change log and spec for wrt-plugins-tizen_0.4.13
[framework/web/wrt-plugins-tizen.git] / src / Content / JSFolder.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
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 #include "ContentFactory.h"
25 #include "ContentController.h"
26 #include "JSFolder.h"
27
28
29 namespace DeviceAPI {
30 namespace Content {
31
32 #define TIZEN_CONTENT_FOLDER_ATTRIBUTENAME              "ContentDirectory"
33 #define TIZEN_CONTENT_FOLDER_UID                        "id"
34 #define TIZEN_CONTENT_FOLDER_NAME                       "title"
35 #define TIZEN_CONTENT_FOLDER_PATH                       "directoryURI"
36 #define TIZEN_CONTENT_FOLDER_STORAGE_TYPE               "storageType"
37 #define TIZEN_CONTENT_FOLDER_MODIFIEDDATE               "modifiedDate"
38
39
40 JSClassDefinition JSFolder::m_classInfo =
41 {
42     0,
43     kJSClassAttributeNone,
44     TIZEN_CONTENT_FOLDER_ATTRIBUTENAME,
45     0,
46     m_property,
47     m_function, //    m_function,
48     initialize,
49     finalize,
50     NULL, //hasProperty,
51     NULL, //getProperty,
52     NULL, //setProperty,
53     NULL, //DeleteProperty,
54     NULL, //GetPropertyNames,
55     NULL, //CallAsFunction,
56     NULL, //CallAsConstructor,
57     NULL, //HasInstance,
58     NULL  //ConvertToType
59 };
60
61 JSStaticValue JSFolder::m_property[] =
62 {
63     //FolderProperties
64     { TIZEN_CONTENT_FOLDER_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
65     { TIZEN_CONTENT_FOLDER_NAME, getPropertyName, NULL, kJSPropertyAttributeReadOnly},
66     { TIZEN_CONTENT_FOLDER_PATH, getPropertyPath, NULL, kJSPropertyAttributeReadOnly},
67     { TIZEN_CONTENT_FOLDER_STORAGE_TYPE, getPropertyStorageType, NULL, kJSPropertyAttributeReadOnly},
68     { TIZEN_CONTENT_FOLDER_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly}, 
69     { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSFolder::m_function[] =
73 {
74     { 0, 0, 0 }
75 };
76
77
78 JSClassRef JSFolder::m_jsClassRef = JSClassCreate(JSFolder::getClassInfo());
79
80 void JSFolder::initialize(JSContextRef context, JSObjectRef object)
81 {
82     LogDebug( "entered" );
83     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) );
84     if (!priv) 
85         {
86         MediacontentFolderPtr privateData(new MediacontentFolder());
87         priv = new FolderPrivObject(context, privateData);
88         JSObjectSetPrivate(object, static_cast<void*>(priv));
89         LogDebug("new event is created" );
90     }
91     else 
92         {
93         LogDebug("private object already exists");
94     }
95 }
96
97
98 void JSFolder::finalize(JSObjectRef object)
99 {
100     LogDebug( "entered" );
101     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) ) ;
102         if (priv != NULL)
103         {
104                 delete (priv);
105                 priv = NULL;
106                 JSObjectSetPrivate(object, NULL);               
107         }
108
109 }
110
111
112
113 const JSClassRef JSFolder::getClassRef()
114 {
115     if (!m_jsClassRef) 
116         {
117             m_jsClassRef = JSClassCreate(&m_classInfo);
118     }
119     return m_jsClassRef;
120 }
121
122 const JSClassDefinition* JSFolder::getClassInfo()
123 {
124     return &m_classInfo;
125 }
126
127
128 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
129 {
130     LogDebug("entered");
131     FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
132     if(!priv) {
133         ThrowMsg(NullPointerException, "Private object is null");
134     }
135     MediacontentFolderPtr result = priv->getObject();
136     if (!result) {
137         ThrowMsg(NullPointerException, "Private object is null");
138     }
139     return result;
140 }
141
142
143
144 JSValueRef JSFolder::getPropertyId(
145                 JSContextRef context, 
146                 JSObjectRef object,
147                 JSStringRef propertyName,
148                 JSValueRef* exception)
149 {
150     LogDebug("entered");
151     Try
152     {
153         Converter converter(context);
154         MediacontentFolderPtr folder = getFolderObject(object);
155
156         return converter.toJSValueRef(folder->getFolderUUID());
157     }
158     Catch(Exception)
159     {
160         LogWarning("trying to get incorrect value");
161     }
162     return JSValueMakeUndefined(context);
163
164 }
165                                         
166 JSValueRef JSFolder::getPropertyName(
167                 JSContextRef context,
168                 JSObjectRef object,
169                 JSStringRef propertyName,
170                 JSValueRef* exception)
171 {
172     LogDebug("entered");
173     Try
174     {
175         Converter converter(context);
176         MediacontentFolderPtr folder = getFolderObject(object);
177         return converter.toJSValueRef(folder->getFolderName());
178     }
179     Catch(Exception)
180     {
181         LogWarning("trying to get incorrect value");
182     }
183     return JSValueMakeUndefined(context);
184 }
185
186
187 JSValueRef JSFolder::getPropertyPath(
188                 JSContextRef context,
189             JSObjectRef object, 
190             JSStringRef propertyName, 
191             JSValueRef* exception)
192 {
193     LogDebug("getPropertyPath::entered");
194     Try
195     {
196         Converter converter(context);
197         MediacontentFolderPtr folder = getFolderObject(object);
198         return converter.toJSValueRef(folder->getFolderPath());
199     }
200     Catch(Exception)
201     {
202         LogWarning("trying to get incorrect value");
203     }
204     return JSValueMakeUndefined(context);
205 }
206
207
208 JSValueRef JSFolder::getPropertyStorageType(
209                 JSContextRef context,
210             JSObjectRef object,
211             JSStringRef propertyName,
212             JSValueRef* exception)
213 {
214     LogDebug("entered");
215     Try
216     {
217         Converter converter(context);
218         MediacontentFolderPtr folder = getFolderObject(object);
219         return converter.toJSValueRef(folder->getFolderStorageType());
220     }
221     Catch(Exception)
222     {
223         LogWarning("trying to get incorrect value");
224     }
225     return JSValueMakeUndefined(context);
226 }
227
228
229 JSValueRef JSFolder::getPropertyModifiedDate(
230                 JSContextRef context,
231                 JSObjectRef object,
232                 JSStringRef propertyName,
233                 JSValueRef* exception)
234 {
235     LogDebug("entered");
236     Try
237     {
238         Converter converter(context);
239         MediacontentFolderPtr folder = getFolderObject(object);
240         if(folder->getFolderModifiedDate() != NULL)
241         {
242             return converter.toJSValueRef(*(folder->getFolderModifiedDate()));
243         }
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 }
299 }
300