summaryrefslogtreecommitdiff
path: root/gio/src/dbusinterfacevtable.ccg
blob: 15cf0acea298981df31d8018d7683e23bbdcfded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (C) 2010 The giomm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gio/gio.h>
#include <glibmm/exceptionhandler.h>
#include <giomm/dbusauthobserver.h>
#include <giomm/dbusintrospection.h>
#include <giomm/dbusmethodinvocation.h>
#include <giomm/dbusconnection.h>
#include <giomm/dbuserror.h>
#include <giomm/slot_async.h>

namespace
{

extern "C" {

static void
DBusInterfaceVTable_MethodCall_giomm_callback(GDBusConnection* connection, const char* sender,
  const char* object_path, const char* interface_name, const char* method_name,
  GVariant* parameters, GDBusMethodInvocation* invocation, void* user_data)
{
  Gio::DBus::InterfaceVTable* vtable = static_cast<Gio::DBus::InterfaceVTable*>(user_data);

  Gio::DBus::InterfaceVTable::SlotInterfaceMethodCall* the_slot = vtable->get_slot_method_call();

  try
  {
    // Note that we use VariantContainerBase instead of VariantBase for the
    // GVariant, because it is documented as being a tuple (list of values).
    (*the_slot)(Glib::wrap(connection, true), (sender ? sender : ""), object_path, interface_name,
      method_name, Glib::VariantContainerBase(parameters, true), Glib::wrap(invocation, true));
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
}

static GVariant*
DBusInterfaceVTable_GetProperty_giomm_callback(GDBusConnection* connection, const char* sender,
  const char* object_path, const char* interface_name, const char* property_name, GError** error,
  void* user_data)
{
  Gio::DBus::InterfaceVTable* vtable = static_cast<Gio::DBus::InterfaceVTable*>(user_data);

  Gio::DBus::InterfaceVTable::SlotInterfaceGetProperty* the_slot = vtable->get_slot_get_property();

  try
  {
    Glib::VariantBase result;

    (*the_slot)(
      result, Glib::wrap(connection, true), sender, object_path, interface_name, property_name);
    return result.gobj_copy();
  }
  catch (const Glib::Error& ex)
  {
    if (error)
      *error = g_error_copy(ex.gobj());
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }

  return nullptr;
}

static gboolean
DBusInterfaceVTable_SetProperty_giomm_callback(GDBusConnection* connection, const char* sender,
  const char* object_path, const char* interface_name, const char* property_name, GVariant* value,
  GError** error, void* user_data)
{
  Gio::DBus::InterfaceVTable* vtable = static_cast<Gio::DBus::InterfaceVTable*>(user_data);

  Gio::DBus::InterfaceVTable::SlotInterfaceSetProperty* the_slot = vtable->get_slot_set_property();

  try
  {
    return static_cast<gboolean>((*the_slot)(Glib::wrap(connection, true), sender, object_path,
      interface_name, property_name, Glib::VariantBase(value, true)));
  }
  catch (const Glib::Error& ex)
  {
    if (error)
      *error = g_error_copy(ex.gobj());
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }

  return false;
}

} // extern "C"
}

namespace Gio
{

namespace DBus
{

InterfaceVTable::InterfaceVTable(const SlotInterfaceMethodCall& slot_method_call,
  const SlotInterfaceGetProperty& slot_get_property,
  const SlotInterfaceSetProperty& slot_set_property)
: slot_method_call_(new SlotInterfaceMethodCall(slot_method_call)),
  slot_get_property_(new SlotInterfaceGetProperty(slot_get_property)),
  slot_set_property_(new SlotInterfaceSetProperty(slot_set_property))
{
  gobject_.method_call = &DBusInterfaceVTable_MethodCall_giomm_callback;
  gobject_.get_property = &DBusInterfaceVTable_GetProperty_giomm_callback;
  gobject_.set_property = &DBusInterfaceVTable_SetProperty_giomm_callback;
}

InterfaceVTable::InterfaceVTable(InterfaceVTable&& other) noexcept
  : gobject_(std::move(other.gobject_)),
    slot_method_call_(std::move(other.slot_method_call_)),
    slot_get_property_(std::move(other.slot_get_property_)),
    slot_set_property_(std::move(other.slot_set_property_))
{
  other.slot_method_call_ = nullptr;
  other.slot_get_property_ = nullptr;
  other.slot_set_property_ = nullptr;
}

InterfaceVTable&
InterfaceVTable::operator=(InterfaceVTable&& other) noexcept
{
  delete slot_method_call_;
  delete slot_get_property_;
  delete slot_set_property_;

  gobject_ = std::move(other.gobject_);
  slot_method_call_ = std::move(other.slot_method_call_);
  slot_get_property_ = std::move(other.slot_get_property_);
  slot_set_property_ = std::move(other.slot_set_property_);

  other.slot_method_call_ = nullptr;
  other.slot_get_property_ = nullptr;
  other.slot_set_property_ = nullptr;

  return *this;
}

InterfaceVTable::~InterfaceVTable()
{
  delete slot_method_call_;
  delete slot_get_property_;
  delete slot_set_property_;
}

InterfaceVTable::SlotInterfaceMethodCall*
InterfaceVTable::get_slot_method_call() const
{
  return slot_method_call_;
}

InterfaceVTable::SlotInterfaceGetProperty*
InterfaceVTable::get_slot_get_property() const
{
  return slot_get_property_;
}

InterfaceVTable::SlotInterfaceSetProperty*
InterfaceVTable::get_slot_set_property() const
{
  return slot_set_property_;
}

} // namespace DBus

} // namespace Gio