MediaServer object properties
[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 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 void JSMediaServer::initialize(JSContextRef context, JSObjectRef object)
76 {
77         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
78         if (!priv)
79         {
80                 MediaServerPtr mediaserver(new MediaServer());
81                 priv = new MediaServerPrivObject( context, mediaserver);
82                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
83                 {
84                         LoggerE("Object can't store private data.");
85                         delete priv;
86                 }
87         }
88
89         LoggerD("JSMediaServer::initialize ");
90 }
91
92 void JSMediaServer::finalize(JSObjectRef object)
93 {
94         MediaServerPrivObject* priv = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
95         JSObjectSetPrivate(object, NULL);
96         LoggerD("Deleting timeutil");
97         delete priv;
98 }
99
100 bool JSMediaServer::hasInstance(JSContextRef context,
101                 JSObjectRef constructor,
102                 JSValueRef possibleInstance,
103                 JSValueRef* exception)
104 {
105         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
106 }
107
108
109 JSValueRef JSMediaServer::getProperty(JSContextRef context,
110                 JSObjectRef object,
111         JSStringRef propertyName,
112         JSValueRef* exception)
113 {
114         LoggerD("Entered ");
115         // TODO try/catch block
116                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(object));
117                 if (NULL == privateObject)
118                 {
119                         LoggerE("private object is null");
120                         // TODO post exception
121                 }
122
123                 if (JSStringIsEqualToUTF8CString(propertyName, "id")) {
124                         JSStringRef jsStr = JSStringCreateWithUTF8CString("/com/intel/..."); // FIXME use proxy value
125                         return JSValueMakeString(context, jsStr);
126                 }
127                 if (JSStringIsEqualToUTF8CString(propertyName, "friendlyName")) {
128                         JSStringRef jsStr = JSStringCreateWithUTF8CString("FIXME mediaserver"); // FIXME use proxy value
129                         return JSValueMakeString(context, jsStr);
130                 }
131                 if (JSStringIsEqualToUTF8CString(propertyName, "root")) {
132                         JSStringRef jsStr = JSStringCreateWithUTF8CString("FIXME mediaserver root "); // FIXME use proxy value
133                         return JSValueMakeFromJSONString(context, jsStr);
134                 }
135
136     return JSValueMakeUndefined(context);
137 }
138
139
140
141 JSValueRef JSMediaServer::browse(JSContextRef context,
142                 JSObjectRef object,
143                 JSObjectRef thisObject,
144                 size_t argumentCount,
145                 const JSValueRef arguments[],
146                 JSValueRef* exception)
147 {
148         LoggerD("Entered ");
149         // TODO try/catch block
150                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
151                 if (NULL == privateObject)
152                 {
153                         LoggerE("private object is null");
154                         // TODO post exception
155                 }
156
157                 LoggerD("Validating arguments: " << argumentCount);
158
159                 ArgumentValidator validator(context, argumentCount, arguments);
160                 std::string containerId = validator.toString(0);
161                 std::string sortMode = validator.toString(1);
162                 unsigned long count = validator.toULong(2);
163                 unsigned long offset = validator.toULong(3);
164
165                 BrowseFindCB *cbP = new BrowseFindCB();
166                 cbP->context = privateObject->getContext();
167                 cbP->successCB = validator.toFunction(4, true);
168                 if (argumentCount > 5)
169                         cbP->errorCB = validator.toFunction(5, true);
170
171                 MediaServerPtr server(privateObject->getObject());
172                 server->browse(containerId, sortMode, count, offset, cbP);
173
174         return JSValueMakeUndefined(context);
175 }
176
177
178 JSValueRef JSMediaServer::find(JSContextRef context,
179                 JSObjectRef object,
180                 JSObjectRef thisObject,
181                 size_t argumentCount,
182                 const JSValueRef arguments[],
183                 JSValueRef* exception)
184 {
185         LoggerD("Entered ");
186         // TODO try/catch block
187                 MediaServerPrivObject* privateObject = static_cast<MediaServerPrivObject*>(JSObjectGetPrivate(thisObject));
188                 if (NULL == privateObject)
189                 {
190                         LoggerE("private object is null");
191                         // TODO post exception
192                 }
193
194                 LoggerD("Validating arguments: " << argumentCount);
195
196                 ArgumentValidator validator(context, argumentCount, arguments);
197                 std::string containerId = validator.toString(0);
198                 std::string searchFilter = validator.toString(1);
199                 std::string sortMode = validator.toString(2);
200                 unsigned long count = validator.toULong(3);
201                 unsigned long offset = validator.toULong(4);
202
203                 BrowseFindCB *cbP = new BrowseFindCB();
204                 cbP->context = privateObject->getContext();
205                 cbP->successCB = validator.toFunction(5, true);
206                 if (argumentCount > 6)
207                         cbP->errorCB = validator.toFunction(6, true);
208
209                 MediaServerPtr server(privateObject->getObject());
210                 server->find(containerId, searchFilter, sortMode, count, offset, cbP);
211
212         return JSValueMakeUndefined(context);
213 }
214
215
216 }
217 }