fixed compile issues
[profile/ivi/wrt-plugins-ivi.git] / JSVehicle.cpp
1 #include "JSVehicle.h"
2 #include "Vehicle.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 <abstractpropertytype.h>
14
15 namespace DeviceAPI {
16 namespace Vehicle {
17
18 using namespace DPL;
19 using namespace DeviceAPI::Common;
20 using namespace WrtDeviceApis::Commons;
21 using namespace WrtDeviceApis::CommonsJavaScript;
22
23 JSClassDefinition JSVehicle::m_classInfo = {
24         0,
25         kJSClassAttributeNone,
26         "Vehicle",
27         0,
28         NULL,
29         m_function,
30         initialize,
31         finalize,
32         NULL, //HasProperty,
33         NULL, //GetProperty,
34         NULL, //SetProperty,
35         NULL, //DeleteProperty,
36         NULL, //GetPropertyNames,
37         NULL, //CallAsFunction,
38         NULL, //CallAsConstructor,
39         hasInstance,
40         NULL, //ConvertToType
41 };
42
43 JSStaticFunction JSVehicle::m_function[] = {
44         { "get", JSVehicle::get, kJSPropertyAttributeNone },
45         { 0, 0, 0 }
46 };
47
48 const JSClassRef JSVehicle::getClassRef()
49 {
50         if (!m_jsClassRef)
51         {
52                 m_jsClassRef = JSClassCreate(&m_classInfo);
53         }
54         return m_jsClassRef;
55 }
56
57 const JSClassDefinition* JSVehicle::getClassInfo()
58 {
59         return &m_classInfo;
60 }
61
62 JSClassRef JSVehicle::m_jsClassRef = JSClassCreate(JSVehicle::getClassInfo());
63
64 void JSVehicle::initialize(JSContextRef context, JSObjectRef object)
65 {
66         VehiclePrivObject* priv = static_cast<VehiclePrivObject*>(JSObjectGetPrivate(object));
67         if (!priv)
68         {
69                 VehiclePtr vehicle(new VehicleMaster());
70                 priv = new VehiclePrivObject( context, vehicle);
71                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv)))
72                 {
73                         LoggerE("Object can't store private data.");
74                         delete priv;
75                 }
76         }
77
78         LoggerD("JSVehicle::initialize ");
79 }
80
81 void JSVehicle::finalize(JSObjectRef object)
82 {
83         VehiclePrivObject* priv = static_cast<VehiclePrivObject*>(JSObjectGetPrivate(object));
84         JSObjectSetPrivate(object, NULL);
85         LoggerD("Deleting timeutil");
86         delete priv;
87 }
88
89 bool JSVehicle::hasInstance(JSContextRef context,
90                 JSObjectRef constructor,
91                 JSValueRef possibleInstance,
92                 JSValueRef* exception)
93 {
94         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
95 }
96
97
98 JSValueRef JSVehicle::get(JSContextRef context,
99                 JSObjectRef object,
100                 JSObjectRef thisObject,
101                 size_t argumentCount,
102                 const JSValueRef arguments[],
103                 JSValueRef* exception)
104 {
105         LoggerD("Entered ");
106         //Try {
107                 VehiclePrivObject* privateObject = static_cast<VehiclePrivObject*>(JSObjectGetPrivate(thisObject));
108                 if (NULL == privateObject)
109                 {
110                         LoggerE("private object is null");
111                         //return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
112                 }
113
114                 ArgumentValidator validator(context, argumentCount, arguments);
115                 std::string property = validator.toString(0);
116
117                 VehiclePtr vehicle(privateObject->getObject());
118
119                 std::list<AbstractPropertyType*> properties = vehicle->get(property);
120
121                 std::stringstream json;
122
123
124                 json<<"{";
125
126                 for(auto itr = properties.begin(); itr != properties.end(); itr++)
127                 {
128                         if(json.str() != "{") json<<",";
129
130                         AbstractPropertyType* p = *itr;
131
132                         json<<"\""<<p->name<<"\" : "<<p->toString();
133
134                 }
135
136                 json << "}";
137
138                 Converter converter(context);
139
140                 return converter.toJSValueRef(json.str());
141
142
143         /*} Catch (PlatformException) {
144                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
145         } Catch(NullPointerException) {
146                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
147                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
148         } Catch (WrtDeviceApis::Commons::Exception) {
149                 LoggerE("Exception: " << _rethrown_exception.GetMessage());
150         }*/
151
152         //return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
153 }
154 }
155 }