summaryrefslogtreecommitdiff
path: root/telepathy-glib/svc-interface-skeleton.c
blob: b1f6144a7dfcc67e61e3a056ecd39893fce65656 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright © 2014 Collabora Ltd.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <telepathy-glib/svc-interface-skeleton-internal.h>

#include <dbus/dbus-glib.h>

#include <telepathy-glib/asv.h>
#include <telepathy-glib/dbus-properties-mixin.h>
#include <telepathy-glib/variant-util.h>

#define DEBUG_FLAG TP_DEBUG_SVC
#include "debug-internal.h"

struct _TpSvcInterfaceSkeletonPrivate
{
  GWeakRef object;
  TpSvcInterfaceInfo *iinfo;
};

G_DEFINE_TYPE (TpSvcInterfaceSkeleton, _tp_svc_interface_skeleton,
    G_TYPE_DBUS_INTERFACE_SKELETON)

static void
_tp_svc_interface_skeleton_init (TpSvcInterfaceSkeleton *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TP_TYPE_SVC_INTERFACE_SKELETON, TpSvcInterfaceSkeletonPrivate);
}

static void
tp_svc_interface_skeleton_dispose (GObject *obj)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (obj);

  /* not using g_weak_ref_clear() in order to be idempotent */
  g_weak_ref_set (&self->priv->object, NULL);

  G_OBJECT_CLASS (_tp_svc_interface_skeleton_parent_class)->dispose (obj);
}

static GDBusInterfaceInfo *
tp_svc_interface_skeleton_get_info (GDBusInterfaceSkeleton *skel)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (skel);

  return self->priv->iinfo->interface_info;
}

static void
tp_svc_interface_skeleton_method_call (GDBusConnection *connection,
    const gchar *sender,
    const gchar *object_path,
    const gchar *interface_name,
    const gchar *method_name,
    GVariant *parameters,
    GDBusMethodInvocation *invocation,
    gpointer user_data)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (user_data);
  GObject *object;

  DEBUG ("%s.%s on %s %p from %s", interface_name, method_name, object_path,
      self, sender);

  object = g_weak_ref_get (&self->priv->object);
  /* TpDBusDaemon is meant to unexport us automatically when the object
   * goes away */
  g_return_if_fail (object != NULL);

  self->priv->iinfo->vtable->method_call (connection,
      sender, object_path, interface_name, method_name, parameters,
      invocation, object);
  g_object_unref (object);
}

static GVariant *
tp_svc_interface_skeleton_get_property (GDBusConnection *connection,
    const gchar *sender,
    const gchar *object_path,
    const gchar *interface_name,
    const gchar *property_name,
    GError **error,
    gpointer user_data)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (user_data);
  GObject *object;
  GValue value = G_VALUE_INIT;
  GVariant *ret = NULL;

  DEBUG ("Get(%s.%s) on %s %p from %s", interface_name, property_name,
      object_path, self, sender);

  object = g_weak_ref_get (&self->priv->object);
  g_return_val_if_fail (object != NULL, NULL);

  if (tp_dbus_properties_mixin_get (object, interface_name, property_name,
          &value, error))
    {
      ret = dbus_g_value_build_g_variant (&value);
      g_value_unset (&value);
    }

  g_object_unref (object);

  return ret;
}

static gboolean
tp_svc_interface_skeleton_set_property (GDBusConnection *connection,
    const gchar *sender,
    const gchar *object_path,
    const gchar *interface_name,
    const gchar *property_name,
    GVariant *variant,
    GError **error,
    gpointer user_data)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (user_data);
  GObject *object;
  GValue value = G_VALUE_INIT;
  gboolean ret;

  DEBUG ("Set(%s.%s) on %s %p from %s", interface_name, property_name,
      object_path, self, sender);

  object = g_weak_ref_get (&self->priv->object);
  g_return_val_if_fail (object != NULL, FALSE);

  dbus_g_value_parse_g_variant (variant, &value);
  ret = tp_dbus_properties_mixin_set (object, interface_name, property_name,
      &value, error);

  g_value_unset (&value);
  g_object_unref (object);

  return ret;
}

static GDBusInterfaceVTable vtable = {
    tp_svc_interface_skeleton_method_call,
    tp_svc_interface_skeleton_get_property,
    tp_svc_interface_skeleton_set_property
};

static GDBusInterfaceVTable *
tp_svc_interface_skeleton_get_vtable (GDBusInterfaceSkeleton *skel)
{
  return &vtable;
}

static GVariant *
tp_svc_interface_skeleton_get_properties (GDBusInterfaceSkeleton *skel)
{
  TpSvcInterfaceSkeleton *self = TP_SVC_INTERFACE_SKELETON (skel);
  GVariant *ret;
  GHashTable *asv;
  const gchar *iface_name = self->priv->iinfo->interface_info->name;
  GObject *object;

  object = g_weak_ref_get (&self->priv->object);
  g_return_val_if_fail (object != NULL, NULL);

  /* For now assume we have the TpDBusPropertiesMixin if we have
   * any properties at all. This never returns NULL. */
  asv = tp_dbus_properties_mixin_dup_all (object, iface_name);

  ret = g_variant_ref_sink (tp_asv_to_vardict (asv));
  g_hash_table_unref (asv);
  g_object_unref (object);
  return ret;
}

static void
tp_svc_interface_skeleton_flush (GDBusInterfaceSkeleton *skel)
{
  /* stub: we emit any changes immediately, and we implement Properties
   * elsewhere anyway */
}

static void
_tp_svc_interface_skeleton_class_init (TpSvcInterfaceSkeletonClass *cls)
{
  GObjectClass *obj_cls = G_OBJECT_CLASS (cls);
  GDBusInterfaceSkeletonClass *skel_cls = G_DBUS_INTERFACE_SKELETON_CLASS (cls);

  g_type_class_add_private (cls, sizeof (TpSvcInterfaceSkeleton));

  obj_cls->dispose = tp_svc_interface_skeleton_dispose;

  skel_cls->get_info = tp_svc_interface_skeleton_get_info;
  skel_cls->get_vtable = tp_svc_interface_skeleton_get_vtable;
  skel_cls->get_properties = tp_svc_interface_skeleton_get_properties;
  skel_cls->flush = tp_svc_interface_skeleton_flush;
}

typedef struct {
    GClosure closure;
    /* (transfer none) - the closure ensures we have a ref */
    TpSvcInterfaceSkeleton *self;
    /* (transfer none) - borrowed from the interface info */
    const gchar *name;
} SignalClosure;

static void
tp_svc_interface_skeleton_emit_signal (GClosure *closure,
    GValue *return_value,
    guint n_param_values,
    const GValue *param_values,
    gpointer invocation_hint,
    gpointer marshal_data)
{
  SignalClosure *sc = (SignalClosure *) closure;
  TpSvcInterfaceSkeleton *self = sc->self;
  GDBusInterfaceSkeleton *skel = G_DBUS_INTERFACE_SKELETON (self);
  const gchar *path = g_dbus_interface_skeleton_get_object_path (skel);
  GVariantBuilder builder;
  guint i;

  DEBUG ("%s.%s from %s %p", self->priv->iinfo->interface_info->name,
      sc->name, path, self);

  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);

  /* Skip parameter 0, which is the GObject. */
  for (i = 1; i < n_param_values; i++)
    g_variant_builder_add_value (&builder,
        dbus_g_value_build_g_variant (param_values + i));

  /* we only support being exported on one connection */
  g_dbus_connection_emit_signal (
      g_dbus_interface_skeleton_get_connection (skel),
      NULL, /* broadcast */
      g_dbus_interface_skeleton_get_object_path (skel),
      self->priv->iinfo->interface_info->name,
      sc->name,
      /* consume floating ref */
      g_variant_builder_end (&builder),
      /* cannot fail unless a parameter is incompatible with D-Bus,
       * so ignore error */
      NULL);
}

/*
 * _tp_svc_interface_skeleton_new: (skip)
 * @object: (type GObject.Object): a #GObject
 * @iface: a `TpSvc` interface on the object
 * @iinfo: a description of the corresponding D-Bus interface
 *
 * Returns: (transfer full): a new interface skeleton wrapping @iface
 *  on @object
 */
TpSvcInterfaceSkeleton *
_tp_svc_interface_skeleton_new (gpointer object,
    GType iface,
    const TpSvcInterfaceInfo *iinfo)
{
  TpSvcInterfaceSkeleton *self;

  /* not bothering to refcount it, it must be static for now */
  g_return_val_if_fail (iinfo->ref_count == -1, NULL);

  self = g_object_new (TP_TYPE_SVC_INTERFACE_SKELETON,
      NULL);
  g_weak_ref_init (&self->priv->object, object);
  self->priv->iinfo = (TpSvcInterfaceInfo *) iinfo;

  if (iinfo->signals != NULL)
    {
      guint i;

      for (i = 0; iinfo->signals[i] != NULL; i++)
        {
          const gchar *glib_name;
          const gchar *dbus_name;
          SignalClosure *closure;

          g_assert (iinfo->interface_info->signals[i] != NULL);

          glib_name = iinfo->signals[i];
          dbus_name = iinfo->interface_info->signals[i]->name;

          closure = (SignalClosure *) g_closure_new_object (sizeof (*closure),
              (GObject *) self);
          g_closure_set_marshal ((GClosure *) closure,
              tp_svc_interface_skeleton_emit_signal);
          closure->self = self;
          closure->name = dbus_name;

          g_signal_connect_closure (object, glib_name, (GClosure *) closure,
              FALSE);
        }
    }

  return self;
}