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