added tizen engineer mode to system
[profile/ivi/wrt-plugins-ivi.git] / src / MediaServer / JSMediaServer.cpp
1 #include "JSMediaServer.h"
2 #include "MediaServer.h"
3
4 #include <Logger.h>
5 #include <Commons/Exception.h>
6 #include <CommonsJavaScript/Utils.h>
7 #include <CommonsJavaScript/JSCallbackManager.h>
8 #include <JSWebAPIErrorFactory.h>
9 #include <ArgumentValidator.h>
10 #include <CommonsJavaScript/Converter.h>
11 #include <sstream>
12
13 #include <glib.h>
14 #include <json-glib/json-gvariant.h>    
15
16
17 namespace DeviceAPI {
18 namespace MediaServer {
19
20 using namespace DPL;
21 using namespace DeviceAPI::Common;
22 using namespace WrtDeviceApis::Commons;
23 using namespace WrtDeviceApis::CommonsJavaScript;
24
25 JSClassDefinition JSMediaServer::m_classInfo = {
26         0,
27         kJSClassAttributeNone,
28         "MediaServer",
29         0,
30         m_property,
31         m_function,
32         initialize,
33         finalize,
34         NULL, //HasProperty,
35         NULL, //GetProperty,
36         NULL, //SetProperty,
37         NULL, //DeleteProperty,
38         NULL, //GetPropertyNames,
39         NULL, //CallAsFunction,
40         NULL, //CallAsConstructor,
41         hasInstance,
42         NULL, //ConvertToType
43 };
44
45 JSStaticValue JSMediaServer::m_property[] =
46 {
47         {"id",  getProperty, NULL, kJSPropertyAttributeReadOnly},
48         {"friendlyName",  getProperty, NULL, kJSPropertyAttributeReadOnly},
49         {"root",  getProperty, NULL, kJSPropertyAttributeReadOnly},
50         { 0, 0, 0, 0 }
51 };
52
53 JSStaticFunction JSMediaServer::m_function[] = {
54         {"browse", JSMediaServer::browse, kJSPropertyAttributeNone},
55         {"find", JSMediaServer::find, kJSPropertyAttributeNone},
56         { 0, 0, 0 }
57 };
58
59 const JSClassRef JSMediaServer::getClassRef()
60 {
61         if (!m_jsClassRef)
62         {
63                 m_jsClassRef = JSClassCreate(&m_classInfo);
64         }
65         return m_jsClassRef;
66 }
67
68 const JSClassDefinition* JSMediaServer::getClassInfo()
69 {
70         return &m_classInfo;
71 }
72
73 JSClassRef JSMediaServer::m_jsClassRef = JSClassCreate(JSMediaServer::getClassInfo());
74
75 JSObjectRef JSMediaServer::createJSObject(JSContextRef context) {
76         LoggerD("Entered JSMediaServer::createJSObject");
77         MediaServerPtr mediaserver(new MediaServer());
78         MediaServerPrivObject* priv = new MediaServerPrivObject(context, mediaserver);
79         return JSObjectMake(context, getClassRef(), priv);
80 }
81
82 JSObjectRef JSMediaServer::createJSObject(JSContextRef context, std::string path) {
83         LoggerD("Entered JSMediaServer::createJSObject : " << path.c_str());
84         MediaServer *ms = new MediaServer();
85         ms->setProxyPath(path);
86         MediaServerPtr mediaserver(ms);
87         MediaServerPrivObject* priv = new MediaServerPrivObject(context, mediaserver);
88         return JSObjectMake(context, getClassRef(), priv);
89 }
90
91 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
92 {
93         LoggerD("Entered JSMediaServer::initialize");
94         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
95         if (!priv)
96         {
97                 LoggerD("Create private object");
98                 MediaServerPtr mediaserver(new MediaServer());
99                 priv = new MediaServerPrivObject(context, mediaserver);
100                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
101                 {
102                         LoggerE("Object can't store private data.");
103                         delete priv;
104                 }
105         }
106
107         LoggerD("JSMediaServer::initialize done");
108 }
109
110 void JSMediaServer::finalize(JSObjectRef object)
111 {
112         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
113         JSObjectSetPrivate(object, NULL);
114         LoggerD("JSMediaServer::finalize");
115         delete priv;
116 }
117
118 bool JSMediaServer::hasInstance(JSContextRef context,
119                 JSObjectRef constructor,
120                 JSValueRef possibleInstance,
121                 JSValueRef* exception)
122 {
123         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
124 }
125
126
127 JSValueRef JSMediaServer::getProperty(JSContextRef context,
128                 JSObjectRef object,
129         JSStringRef propertyName,
130         JSValueRef* exception)
131 {
132         LoggerD("Entered JSMediaServer::getProperty");
133         // TODO try/catch block
134                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
135                 if (NULL == privateObject)
136                 {
137                         LoggerE("private object is null");
138                         // TODO post exception
139                 }
140                 MediaServerPtr server(privateObject->getObject());
141
142                 Converter converter(context);
143                 std::string name = converter.toString(propertyName);
144                 std::string strVal = server->getPropertyStringValue(name);
145                 LoggerD("JSMediaServer::getProperty : " << name.c_str() << " : " << strVal.c_str());
146                 JSStringRef jsStr = converter.toJSStringRef(strVal);
147
148                 if (JSStringIsEqualToUTF8CString(propertyName, "id"))
149                         return JSValueMakeString(context, jsStr);
150                 else if (JSStringIsEqualToUTF8CString(propertyName, "friendlyName"))
151                         return JSValueMakeString(context, jsStr);
152                 else if (JSStringIsEqualToUTF8CString(propertyName, "root"))
153                         return JSValueMakeFromJSONString(context, jsStr);
154
155     return JSValueMakeUndefined(context);
156 }
157
158
159
160 JSValueRef JSMediaServer::browse(JSContextRef context,
161                 JSObjectRef object,
162                 JSObjectRef thisObject,
163                 size_t argumentCount,
164                 const JSValueRef arguments[],
165                 JSValueRef* exception)
166 {
167         LoggerD("Entered JSMediaServer::browse");
168         // TODO try/catch block
169                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
170                 if (NULL == privateObject)
171                 {
172                         LoggerE("private object is null");
173                         // TODO post exception
174                 }
175
176                 LoggerD("Validating arguments: " << argumentCount);
177
178                 ArgumentValidator validator(context, argumentCount, arguments);
179                 std::string containerId = validator.toString(0);
180                 std::string sortMode = validator.toString(1);
181                 unsigned long count = validator.toULong(2);
182                 unsigned long offset = validator.toULong(3);
183
184                 LoggerD("browse container " << containerId << " sort: " << sortMode
185                                 << " count:" << count << " offset:" << offset);
186
187                 BrowseFindCB *cbP = new BrowseFindCB();
188                 cbP->context = privateObject->getContext();
189                 cbP->object = object;
190                 cbP->thisObject = thisObject;
191                 cbP->successCB = validator.toFunction(4, true);
192                 JSValueProtect(cbP->context, cbP->successCB);
193                 if (argumentCount > 5) {
194                         cbP->errorCB = validator.toFunction(5, true);
195                         JSValueProtect(cbP->context, cbP->errorCB);
196                 } else {
197                         cbP->errorCB = NULL;
198                 }
199
200                 MediaServerPtr server(privateObject->getObject());
201                 server->browse(containerId, sortMode, count, offset, cbP);
202
203         return JSValueMakeUndefined(context);
204 }
205
206
207 JSValueRef JSMediaServer::find(JSContextRef context,
208                 JSObjectRef object,
209                 JSObjectRef thisObject,
210                 size_t argumentCount,
211                 const JSValueRef arguments[],
212                 JSValueRef* exception)
213 {
214         LoggerD("Entered JSMediaServer::find");
215         // TODO try/catch block
216                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
217                 if (NULL == privateObject)
218                 {
219                         LoggerE("private object is null");
220                         // TODO post exception
221                 }
222
223                 LoggerD("Validating arguments: " << argumentCount);
224
225                 ArgumentValidator validator(context, argumentCount, arguments);
226                 std::string containerId = validator.toString(0);
227                 std::string searchFilter = validator.toString(1);
228                 std::string sortMode = validator.toString(2);
229                 unsigned long count = validator.toULong(3);
230                 unsigned long offset = validator.toULong(4);
231
232                 LoggerD("find: " << searchFilter << " in container " << containerId << " sort: " << sortMode
233                                 << " count:" << count << " offset:" << offset);
234
235                 BrowseFindCB *cbP = new BrowseFindCB();
236                 cbP->context = privateObject->getContext();
237                 cbP->object = object;
238                 cbP->thisObject = thisObject;
239                 cbP->successCB = validator.toFunction(5, true);
240                 JSValueProtect(cbP->context, cbP->successCB);
241                 if (argumentCount > 6) {
242                         cbP->errorCB = validator.toFunction(6, true);
243                         JSValueProtect(cbP->context, cbP->errorCB);
244                 } else {
245                         cbP->errorCB = NULL;
246                 }
247
248                 MediaServerPtr server(privateObject->getObject());
249                 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
250
251         return JSValueMakeUndefined(context);
252 }
253
254
255 }
256 }