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