Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/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 #include <CommonsJavaScript/PrivateObject.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
22 #include "ContentFactory.h"
23 #include "ContentController.h"
24 #include "JSFolder.h"
25 #include <Logger.h>
26
27 namespace DeviceAPI {
28 namespace Content {
29
30 #define TIZEN_CONTENT_FOLDER_ATTRIBUTENAME              "ContentDirectory"
31 #define TIZEN_CONTENT_FOLDER_UID                        "id"
32 #define TIZEN_CONTENT_FOLDER_NAME                       "title"
33 #define TIZEN_CONTENT_FOLDER_PATH                       "directoryURI"
34 #define TIZEN_CONTENT_FOLDER_STORAGE_TYPE               "storageType"
35 #define TIZEN_CONTENT_FOLDER_MODIFIEDDATE               "modifiedDate"
36
37
38 JSClassDefinition JSFolder::m_classInfo =
39 {
40     0,
41     kJSClassAttributeNone,
42     TIZEN_CONTENT_FOLDER_ATTRIBUTENAME,
43     0,
44     m_property,
45     m_function, //    m_function,
46     initialize,
47     finalize,
48     NULL, //hasProperty,
49     NULL, //getProperty,
50     NULL, //setProperty,
51     NULL, //DeleteProperty,
52     NULL, //GetPropertyNames,
53     NULL, //CallAsFunction,
54     NULL, //CallAsConstructor,
55     NULL, //HasInstance,
56     NULL  //ConvertToType
57 };
58
59 JSStaticValue JSFolder::m_property[] =
60 {
61     //FolderProperties
62     { TIZEN_CONTENT_FOLDER_UID, getPropertyId, NULL, kJSPropertyAttributeReadOnly},
63     { TIZEN_CONTENT_FOLDER_NAME, getPropertyName, NULL, kJSPropertyAttributeReadOnly},
64     { TIZEN_CONTENT_FOLDER_PATH, getPropertyPath, NULL, kJSPropertyAttributeReadOnly},
65     { TIZEN_CONTENT_FOLDER_STORAGE_TYPE, getPropertyStorageType, NULL, kJSPropertyAttributeReadOnly},
66     { TIZEN_CONTENT_FOLDER_MODIFIEDDATE, getPropertyModifiedDate, NULL, kJSPropertyAttributeReadOnly}, 
67     { 0, 0, 0, 0 }
68 };
69
70 JSStaticFunction JSFolder::m_function[] =
71 {
72     { 0, 0, 0 }
73 };
74
75
76 JSClassRef JSFolder::m_jsClassRef = JSClassCreate(JSFolder::getClassInfo());
77
78 void JSFolder::initialize(JSContextRef context, JSObjectRef object)
79 {
80     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) );
81     if (!priv) 
82     {
83         MediacontentFolderPtr privateData(new MediacontentFolder());
84         priv = new FolderPrivObject(context, privateData);
85         JSObjectSetPrivate(object, static_cast<void*>(priv));
86     }
87     else
88     {
89         LoggerD("private object already exists");
90     }
91 }
92
93
94 void JSFolder::finalize(JSObjectRef object)
95 {
96     FolderPrivObject *priv = static_cast<FolderPrivObject*>( JSObjectGetPrivate( object ) ) ;
97     if (priv != NULL)
98     {
99         delete (priv);
100         priv = NULL;
101         JSObjectSetPrivate(object, NULL);               
102     }
103 }
104
105
106
107 const JSClassRef JSFolder::getClassRef()
108 {
109     if (!m_jsClassRef) 
110     {
111         m_jsClassRef = JSClassCreate(&m_classInfo);
112     }
113     return m_jsClassRef;
114 }
115
116 const JSClassDefinition* JSFolder::getClassInfo()
117 {
118     return &m_classInfo;
119 }
120
121
122 MediacontentFolderPtr JSFolder::getFolderObject(JSObjectRef object)
123 {
124     FolderPrivObject *priv = static_cast<FolderPrivObject*>(JSObjectGetPrivate(object));
125     if(!priv) {
126         ThrowMsg(NullPointerException, "Private object is null");
127     }
128     MediacontentFolderPtr result = priv->getObject();
129     if (!result) {
130         ThrowMsg(NullPointerException, "Private object is null");
131     }
132     return result;
133 }
134
135
136
137 JSValueRef JSFolder::getPropertyId(
138                 JSContextRef context, 
139                 JSObjectRef object,
140                 JSStringRef propertyName,
141                 JSValueRef* exception)
142 {
143     Try
144     {
145         Converter converter(context);
146         MediacontentFolderPtr folder = getFolderObject(object);
147         return converter.toJSValueRef(folder->getFolderUUID());
148     }
149     Catch(Exception)
150     {
151         LoggerW("trying to get incorrect value");
152     }
153     return JSValueMakeUndefined(context);
154
155 }
156                                         
157 JSValueRef JSFolder::getPropertyName(
158                 JSContextRef context,
159                 JSObjectRef object,
160                 JSStringRef propertyName,
161                 JSValueRef* exception)
162 {
163     Try
164     {
165         Converter converter(context);
166         MediacontentFolderPtr folder = getFolderObject(object);
167         return converter.toJSValueRef(folder->getFolderName());
168     }
169     Catch(Exception)
170     {
171         LoggerW("trying to get incorrect value");
172     }
173     return JSValueMakeUndefined(context);
174 }
175
176
177 JSValueRef JSFolder::getPropertyPath(
178                 JSContextRef context,
179             JSObjectRef object, 
180             JSStringRef propertyName, 
181             JSValueRef* exception)
182 {
183     Try
184     {
185         Converter converter(context);
186         MediacontentFolderPtr folder = getFolderObject(object);
187         return converter.toJSValueRef(folder->getFolderPath());
188     }
189     Catch(Exception)
190     {
191         LoggerW("trying to get incorrect value");
192     }
193     return JSValueMakeNull(context);
194 }
195
196
197 JSValueRef JSFolder::getPropertyStorageType(
198                 JSContextRef context,
199             JSObjectRef object,
200             JSStringRef propertyName,
201             JSValueRef* exception)
202 {
203     Try
204     {
205         Converter converter(context);
206         MediacontentFolderPtr folder = getFolderObject(object);
207         return converter.toJSValueRef(folder->getFolderStorageType());
208     }
209     Catch(Exception)
210     {
211         LoggerW("trying to get incorrect value");
212     }
213     return JSValueMakeUndefined(context);
214 }
215
216
217 JSValueRef JSFolder::getPropertyModifiedDate(
218                 JSContextRef context,
219                 JSObjectRef object,
220                 JSStringRef propertyName,
221                 JSValueRef* exception)
222 {
223     Try
224     {
225         Converter converter(context);
226         MediacontentFolderPtr folder = getFolderObject(object);
227         if(folder->getFolderModifiedDate() != NULL)
228         {
229             return converter.toJSValueRef(*(folder->getFolderModifiedDate()));
230         }
231     }
232     Catch(Exception)
233     {
234         LoggerW("trying to get incorrect value");
235     }
236     return JSValueMakeNull(context);
237 }
238
239
240 JSValueRef      JSFolder::getPropertyMediaId(
241                 JSContextRef context,
242                 JSObjectRef object,
243                 JSStringRef propertyName,
244                 JSValueRef* exception)
245 {
246     Try
247     {
248         Converter converter(context);
249         MediacontentFolderPtr folder = getFolderObject(object);
250         MediaIdListPtr mediaIdLstPtr = folder->getMediaIdList();
251         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
252
253         if(mediaIdLstPtr)
254         {
255             if (NULL == jsResult) 
256             {
257                 ThrowMsg(NullPointerException, "Could not create js array object");
258             }
259             for(unsigned int i=0; i<mediaIdLstPtr->size(); i++) 
260             {
261                 JSValueRef val = converter.toJSValueRef(mediaIdLstPtr->at(i));
262                 if(!JSSetArrayElement(context, jsResult, i, val)) 
263                 {
264                     ThrowMsg(UnknownException, "Could not insert value into js array");
265                 }
266             }
267         }
268         return jsResult;
269     }
270     Catch(Exception)
271     {
272         LoggerW("trying to get incorrect value");
273     }
274     return JSValueMakeUndefined(context);
275
276 }
277
278
279
280 }
281 }
282