2c1e463016dd3c2df86f4bc81d75dfef83f83388
[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         NULL,
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 JSStaticFunction JSMediaServer::m_function[] = {
46         {"browse", JSMediaServer::browse, kJSPropertyAttributeNone},
47         {"find", JSMediaServer::find, kJSPropertyAttributeNone},
48         { 0, 0, 0 }
49 };
50
51 const JSClassRef JSMediaServer::getClassRef()
52 {
53         if (!m_jsClassRef)
54         {
55                 m_jsClassRef = JSClassCreate(&m_classInfo);
56         }
57         return m_jsClassRef;
58 }
59
60 const JSClassDefinition* JSMediaServer::getClassInfo()
61 {
62         return &m_classInfo;
63 }
64
65 JSClassRef JSMediaServer::m_jsClassRef = JSClassCreate(JSMediaServer::getClassInfo());
66
67 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
68 {
69         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
70         if (!priv)
71         {
72                 MediaServerPtr mediaserver(new MediaServer());
73                 priv = new MediaServerPrivObject( context, mediaserver);
74                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
75                 {
76                         LoggerE("Object can't store private data.");
77                         delete priv;
78                 }
79         }
80
81         LoggerD("JSMediaServer::initialize ");
82 }
83
84 void JSMediaServer::finalize(JSObjectRef object)
85 {
86         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
87         JSObjectSetPrivate(object, NULL);
88         LoggerD("Deleting timeutil");
89         delete priv;
90 }
91
92 bool JSMediaServer::hasInstance(JSContextRef context,
93                 JSObjectRef constructor,
94                 JSValueRef possibleInstance,
95                 JSValueRef* exception)
96 {
97         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
98 }
99
100
101 JSValueRef JSMediaServer::browse(JSContextRef context,
102                 JSObjectRef object,
103                 JSObjectRef thisObject,
104                 size_t argumentCount,
105                 const JSValueRef arguments[],
106                 JSValueRef* exception)
107 {
108         LoggerD("Entered ");
109         // TODO try/catch block
110                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
111                 if (NULL == privateObject)
112                 {
113                         LoggerE("private object is null");
114                         // TODO post exception
115                 }
116
117                 LoggerD("Validating arguments: " << argumentCount);
118
119                 ArgumentValidator validator(context, argumentCount, arguments);
120                 std::string containerId = validator.toString(0);
121                 std::string sortMode = validator.toString(1);
122                 unsigned long count = validator.toULong(2);
123                 unsigned long offset = validator.toULong(3);
124
125                 BrowseFindCB *cbP = new BrowseFindCB();
126                 cbP->context = privateObject->getContext();
127                 cbP->successCB = validator.toFunction(4, true);
128                 if (argumentCount > 5)
129                         cbP->errorCB = validator.toFunction(5, true);
130
131                 MediaServerPtr server(privateObject->getObject());
132                 server->browse(containerId, sortMode, count, offset, cbP);
133
134         return JSValueMakeUndefined(context);
135 }
136
137
138 JSValueRef JSMediaServer::find(JSContextRef context,
139                 JSObjectRef object,
140                 JSObjectRef thisObject,
141                 size_t argumentCount,
142                 const JSValueRef arguments[],
143                 JSValueRef* exception)
144 {
145         LoggerD("Entered ");
146         // TODO try/catch block
147                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
148                 if (NULL == privateObject)
149                 {
150                         LoggerE("private object is null");
151                         // TODO post exception
152                 }
153
154                 LoggerD("Validating arguments: " << argumentCount);
155
156                 ArgumentValidator validator(context, argumentCount, arguments);
157                 std::string containerId = validator.toString(0);
158                 std::string searchFilter = validator.toString(1);
159                 std::string sortMode = validator.toString(2);
160                 unsigned long count = validator.toULong(3);
161                 unsigned long offset = validator.toULong(4);
162
163                 BrowseFindCB *cbP = new BrowseFindCB();
164                 cbP->context = privateObject->getContext();
165                 cbP->successCB = validator.toFunction(5, true);
166                 if (argumentCount > 6)
167                         cbP->errorCB = validator.toFunction(6, true);
168
169                 MediaServerPtr server(privateObject->getObject());
170                 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
171
172         return JSValueMakeUndefined(context);
173 }
174
175
176 }
177 }