Tizen 2.0 Release
[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/log/log.h>
24 #include <dpl/assert.h>
25 #include <dpl/exception.h>
26 #include <dpl/dbus/exception.h>
27 #include <dpl/dbus/interface.h>
28
29 namespace DPL {
30 namespace DBus {
31
32 const GDBusInterfaceVTable Interface::m_vTable =
33 {
34     Interface::onMethodCallFunc,
35     Interface::onPropertyGetFunc,
36     Interface::onPropertySetFunc,
37     {0, 0, 0, 0, 0, 0, 0, 0}
38 };
39
40 std::vector<InterfacePtr> Interface::fromXMLString(const std::string& xmlString)
41 {
42     GError* error = NULL;
43
44     GDBusNodeInfo* nodeInfo = g_dbus_node_info_new_for_xml(xmlString.c_str(),
45                                                            &error);
46     if (NULL == nodeInfo)
47     {
48         std::string message;
49         if (NULL != error)
50         {
51             message = error->message;
52             g_error_free(error);
53         }
54         ThrowMsg(DPL::DBus::Exception,
55                  "Error parsing node info <" << message << ">");
56     }
57
58     std::vector<InterfacePtr> result;
59
60     GDBusInterfaceInfo** interface = nodeInfo->interfaces;
61     while (NULL != *interface)
62     {
63         result.push_back(InterfacePtr(new Interface(*interface)));
64         ++interface;
65     }
66
67     g_dbus_node_info_unref(nodeInfo);
68
69     return result;
70 }
71
72 Interface::Interface(GDBusInterfaceInfo* info)
73     : m_info(info)
74 {
75     g_dbus_interface_info_ref(m_info);
76 }
77
78 Interface::~Interface()
79 {
80     g_dbus_interface_info_unref(m_info);
81 }
82
83 const GDBusInterfaceVTable* Interface::getVTable() const
84 {
85     return &m_vTable;
86 }
87
88 GDBusInterfaceInfo* Interface::getInfo() const
89 {
90     return m_info;
91 }
92
93 void Interface::setDispatcher(Dispatcher* dispatcher)
94 {
95     m_dispatcher = dispatcher;
96 }
97
98 void Interface::onMethodCallFunc(GDBusConnection *connection,
99                                  const gchar *sender,
100                                  const gchar *objectPath,
101                                  const gchar *interfaceName,
102                                  const gchar *methodName,
103                                  GVariant *parameters,
104                                  GDBusMethodInvocation *invocation,
105                                  gpointer data)
106 {
107     Assert(NULL != data && "Interface cannot be NULL.");
108     Interface* self = static_cast<Interface*>(data);
109
110     // TODO Verify interface name.
111
112     if (NULL != self->m_dispatcher)
113     {
114         try
115         {
116             self->m_dispatcher->onMethodCall(connection,
117                                              sender,
118                                              objectPath,
119                                              interfaceName,
120                                              methodName,
121                                              parameters,
122                                              invocation);
123         }
124         catch (const DPL::Exception& /*ex*/)
125         {
126             // TODO Support for errors.
127         }
128     }
129 }
130
131 GVariant* Interface::onPropertyGetFunc(GDBusConnection *connection,
132                                        const gchar *sender,
133                                        const gchar *objectPath,
134                                        const gchar *interfaceName,
135                                        const gchar *propertyName,
136                                        GError **error,
137                                        gpointer data)
138 {
139     Assert(NULL != data && "Interface cannot be NULL.");
140     Interface* self = static_cast<Interface*>(data);
141
142     // TODO Verify interface name.
143
144     if (NULL != self->m_dispatcher)
145     {
146         try
147         {
148             // TODO Check if NULL is returned, if so set error variable.
149             return self->m_dispatcher->onPropertyGet(connection,
150                                                      sender,
151                                                      objectPath,
152                                                      interfaceName,
153                                                      propertyName,
154                                                      error);
155         }
156         catch (const DPL::Exception& /*ex*/)
157         {
158             // TODO Support for errors.
159         }
160     }
161
162     // TODO Set error.
163
164     return NULL;
165 }
166
167 gboolean Interface::onPropertySetFunc(GDBusConnection *connection,
168                                       const gchar *sender,
169                                       const gchar *objectPath,
170                                       const gchar *interfaceName,
171                                       const gchar *propertyName,
172                                       GVariant *value,
173                                       GError **error,
174                                       gpointer data)
175 {
176     Assert(NULL != data && "Interface cannot be NULL.");
177     Interface* self = static_cast<Interface*>(data);
178
179     // TODO Verify interface name.
180
181     if (NULL != self->m_dispatcher)
182     {
183         try
184         {
185             return self->m_dispatcher->onPropertySet(connection,
186                                                      sender,
187                                                      objectPath,
188                                                      interfaceName,
189                                                      propertyName,
190                                                      value,
191                                                      error);
192         }
193         catch (const DPL::Exception& /*ex*/)
194         {
195             // TODO Support for errors.
196         }
197     }
198
199     // TODO Set error.
200
201     return false;
202 }
203
204 }
205 }