Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules / dbus / src / interface.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    interface.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief
21  */
22 #include <stddef.h>
23 #include <dpl/assert.h>
24 #include <dpl/exception.h>
25 #include <dpl/dbus/exception.h>
26 #include <dpl/dbus/interface.h>
27
28 namespace DPL {
29 namespace DBus {
30 const GDBusInterfaceVTable Interface::m_vTable = {
31     Interface::onMethodCallFunc,
32     Interface::onPropertyGetFunc,
33     Interface::onPropertySetFunc,
34     { 0, 0, 0, 0, 0, 0, 0, 0 }
35 };
36
37 std::vector<InterfacePtr> Interface::fromXMLString(const std::string& xmlString)
38 {
39     GError* error = NULL;
40
41     GDBusNodeInfo* nodeInfo = g_dbus_node_info_new_for_xml(xmlString.c_str(),
42                                                            &error);
43     if (NULL == nodeInfo) {
44         std::string message;
45         if (NULL != error) {
46             message = error->message;
47             g_error_free(error);
48         }
49         ThrowMsg(DPL::DBus::Exception,
50                  "Error parsing node info <" << message << ">");
51     }
52
53     std::vector<InterfacePtr> result;
54
55     GDBusInterfaceInfo** interface = nodeInfo->interfaces;
56     while (NULL != *interface) {
57         result.push_back(InterfacePtr(new Interface(*interface)));
58         ++interface;
59     }
60
61     g_dbus_node_info_unref(nodeInfo);
62
63     return result;
64 }
65
66 Interface::Interface(GDBusInterfaceInfo* info) :
67     m_info(info)
68 {
69     g_dbus_interface_info_ref(m_info);
70 }
71
72 Interface::~Interface()
73 {
74     g_dbus_interface_info_unref(m_info);
75 }
76
77 const GDBusInterfaceVTable* Interface::getVTable() const
78 {
79     return &m_vTable;
80 }
81
82 GDBusInterfaceInfo* Interface::getInfo() const
83 {
84     return m_info;
85 }
86
87 void Interface::setDispatcher(Dispatcher* dispatcher)
88 {
89     m_dispatcher = dispatcher;
90 }
91
92 void Interface::onMethodCallFunc(GDBusConnection *connection,
93                                  const gchar *sender,
94                                  const gchar *objectPath,
95                                  const gchar *interfaceName,
96                                  const gchar *methodName,
97                                  GVariant *parameters,
98                                  GDBusMethodInvocation *invocation,
99                                  gpointer data)
100 {
101     AssertMsg(NULL != data, "Interface cannot be NULL.");
102     Interface* self = static_cast<Interface*>(data);
103
104     // TODO Verify interface name.
105
106     if (NULL != self->m_dispatcher) {
107         try {
108             self->m_dispatcher->onMethodCall(connection,
109                                              sender,
110                                              objectPath,
111                                              interfaceName,
112                                              methodName,
113                                              parameters,
114                                              invocation);
115         } catch (const DPL::Exception& /*ex*/) {
116             // TODO Support for errors.
117         }
118     }
119 }
120
121 GVariant* Interface::onPropertyGetFunc(GDBusConnection *connection,
122                                        const gchar *sender,
123                                        const gchar *objectPath,
124                                        const gchar *interfaceName,
125                                        const gchar *propertyName,
126                                        GError **error,
127                                        gpointer data)
128 {
129     AssertMsg(NULL != data, "Interface cannot be NULL.");
130     Interface* self = static_cast<Interface*>(data);
131
132     // TODO Verify interface name.
133
134     if (NULL != self->m_dispatcher) {
135         try {
136             // TODO Check if NULL is returned, if so set error variable.
137             return self->m_dispatcher->onPropertyGet(connection,
138                                                      sender,
139                                                      objectPath,
140                                                      interfaceName,
141                                                      propertyName,
142                                                      error);
143         } catch (const DPL::Exception& /*ex*/) {
144             // TODO Support for errors.
145         }
146     }
147
148     // TODO Set error.
149
150     return NULL;
151 }
152
153 gboolean Interface::onPropertySetFunc(GDBusConnection *connection,
154                                       const gchar *sender,
155                                       const gchar *objectPath,
156                                       const gchar *interfaceName,
157                                       const gchar *propertyName,
158                                       GVariant *value,
159                                       GError **error,
160                                       gpointer data)
161 {
162     AssertMsg(NULL != data, "Interface cannot be NULL.");
163     Interface* self = static_cast<Interface*>(data);
164
165     // TODO Verify interface name.
166
167     if (NULL != self->m_dispatcher) {
168         try {
169             return self->m_dispatcher->onPropertySet(connection,
170                                                      sender,
171                                                      objectPath,
172                                                      interfaceName,
173                                                      propertyName,
174                                                      value,
175                                                      error);
176         } catch (const DPL::Exception& /*ex*/) {
177             // TODO Support for errors.
178         }
179     }
180
181     // TODO Set error.
182
183     return false;
184 }
185 }
186 }