summaryrefslogtreecommitdiff
path: root/docs/examples/glib_mc5_dbus_tube_handler/example-handler.c
blob: d45fcf39e59b4cbd9e44bcdabff2d3efaaf94004 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/enums.h>
#include <telepathy-glib/channel.h>
#include <telepathy-glib/connection.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/svc-client.h>
#include <telepathy-glib/util.h>

#include "example-handler.h"

#define SERVICE_NAME "org.freedesktop.Telepathy.Examples.TubeClient"

static void client_iface_init (gpointer, gpointer);
static void observer_iface_init (gpointer, gpointer);

#define GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_EXAMPLE_HANDLER, ExampleHandlerPrivate))

G_DEFINE_TYPE_WITH_CODE (ExampleHandler, example_handler, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
      tp_dbus_properties_mixin_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT, NULL);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT_HANDLER, observer_iface_init);
    );

static const char *client_interfaces[] = {
    TP_IFACE_CLIENT_HANDLER,
    NULL
};

enum
{
  PROP_0,
  PROP_INTERFACES,
  PROP_CHANNEL_FILTER,
  PROP_BYPASS_APPROVAL,
  PROP_HANDLED_CHANNELS
};

typedef struct _ExampleHandlerPrivate ExampleHandlerPrivate;
struct _ExampleHandlerPrivate
{
  GList *channels;

  TpTubeState state;
  char *address;
};

static void
open_tube (ExampleHandler *self)
{
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  /* we can't be sure what order we will get our address and the open status
   * so check here that we have both */
  if (priv->state != TP_TUBE_STATE_OPEN || priv->address == NULL)
    {
      return;
    }

  g_print ("Ready to connect to D-Bus address = %s\n", priv->address);

  /* make a connection to the named D-Bus bus here.
   * Each member of the MUC is assigned a well-known address by the Connection
   * Manager, track the DBusNamesChanged signal to find out when users appear
   * or disappear on the bus. You can publish objects under this unique
   * address or make method calls to someone elses objects. */
}

static void
tube_state_changed_callback (TpChannel *channel,
                             guint      state,
                             gpointer   user_data,
                             GObject   *weak_obj)
{
  ExampleHandler *self = EXAMPLE_HANDLER (weak_obj);
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  priv->state = state;

  open_tube (self);
}

static void
dbus_names_changed_callback (TpChannel    *channel,
                             GHashTable   *added,
                             const GArray *removed,
                             gpointer      user_data,
                             GObject      *weak_obj)
{
  ExampleHandler *self = EXAMPLE_HANDLER (weak_obj);
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  /* the mapping between Handles and the unique D-Bus addresses of other users
   * has been updated because people have joined or left the MUC */

  g_print ("DBus names changed\n");
}

static void
tube_accept_callback (TpChannel    *channel,
                      const char   *address,
                      const GError *in_error,
                      gpointer      user_data,
                      GObject      *weak_obj)
{
  ExampleHandler *self = EXAMPLE_HANDLER (weak_obj);
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  if (in_error != NULL)
    {
      g_error ("%s", in_error->message);
    }

  priv->address = g_strdup (address);

  open_tube (self);
}

static void
tube_channel_ready (TpChannel    *channel,
                    const GError *in_error,
                    gpointer      user_data)
{
  ExampleHandler *self = EXAMPLE_HANDLER (user_data);
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  if (in_error != NULL)
    {
      g_error ("%s", in_error->message);
    }

  GError *error = NULL;

  g_print ("Channel ready\n");

  /* add the channel to the list */
  priv->channels = g_list_prepend (priv->channels, channel);

  tp_cli_channel_interface_tube_connect_to_tube_channel_state_changed (
      channel, tube_state_changed_callback,
      NULL, NULL, G_OBJECT (self), &error);
  if (error != NULL)
    {
      g_error ("%s", error->message);
    }
  tp_cli_channel_type_dbus_tube_connect_to_dbus_names_changed (
      channel, dbus_names_changed_callback,
      NULL, NULL, G_OBJECT (self), &error);
  if (error != NULL)
    {
      g_error ("%s", error->message);
    }

  tp_cli_channel_type_dbus_tube_call_accept (channel, -1,
      TP_SOCKET_ACCESS_CONTROL_LOCALHOST, tube_accept_callback,
      NULL, NULL, G_OBJECT (self));
}

static void
example_handler_handle_channels (TpSvcClientHandler   *self,
                                 const char            *account,
                                 const char            *connection,
                                 const GPtrArray       *channels,
                                 const GPtrArray       *requests_satisfied,
                                 guint64                user_action_time,
                                 GHashTable            *handler_info,
                                 DBusGMethodInvocation *context)
{
  GError *error = NULL;

  TpDBusDaemon *bus = tp_dbus_daemon_dup (&error);
  if (error != NULL)
    {
      g_error ("%s", error->message);
    }

  TpConnection *conn = tp_connection_new (bus, NULL, connection, &error);
  if (error != NULL)
    {
      g_error ("%s", error->message);
    }

  /* channels is of type a(oa{sv}) */
  int i;
  for (i = 0; i < channels->len; i++)
    {
      GValueArray *channel = g_ptr_array_index (channels, i);

      char *path;
      GHashTable *map;

      tp_value_array_unpack (channel, 2,
          &path,
          &map);

      const char *type = tp_asv_get_string (map,
          TP_IFACE_CHANNEL ".ChannelType");
      const char *service = tp_asv_get_string (map,
          TP_IFACE_CHANNEL_TYPE_DBUS_TUBE ".ServiceName");

      if (tp_strdiff (type, TP_IFACE_CHANNEL_TYPE_DBUS_TUBE) ||
          tp_strdiff (service, SERVICE_NAME))
        {
          continue;
        }

      g_print ("Got tube channel: %s\n", path);

      TpChannel *chan = tp_channel_new_from_properties (conn, path, map,
          &error);
      if (error != NULL)
        {
          g_error ("%s", error->message);
        }
      tp_channel_call_when_ready (chan, tube_channel_ready, self);
    }

  g_object_unref (conn);
  g_object_unref (bus);

  tp_svc_client_handler_return_from_handle_channels (context);
}

static void
example_handler_get_property (GObject    *self,
                              guint       property_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  ExampleHandlerPrivate *priv = GET_PRIVATE (self);

  switch (property_id)
    {
      case PROP_INTERFACES:
        g_print (" :: interfaces\n");
        g_value_set_boxed (value, client_interfaces);
        break;

      case PROP_CHANNEL_FILTER:
        g_print (" :: channel-filter\n");

        /* this is the same map as the Python handler example */
        GPtrArray *array = g_ptr_array_new ();
        GHashTable *map = tp_asv_new (
              TP_IFACE_CHANNEL ".ChannelType", G_TYPE_STRING,
                  TP_IFACE_CHANNEL_TYPE_DBUS_TUBE,
              TP_IFACE_CHANNEL ".TargetHandleType", G_TYPE_UINT,
                  TP_HANDLE_TYPE_ROOM,
              TP_IFACE_CHANNEL ".Requested", G_TYPE_BOOLEAN,
                  FALSE,
              TP_IFACE_CHANNEL_TYPE_DBUS_TUBE ".ServiceName", G_TYPE_STRING,
                  SERVICE_NAME,
              NULL
            );

        g_ptr_array_add (array, map);
        g_value_take_boxed (value, array);
        break;

      case PROP_BYPASS_APPROVAL:
        g_print (" :: bypass-approval\n");
        g_value_set_boolean (value, FALSE);
        break;

      case PROP_HANDLED_CHANNELS:
        g_print (" :: handled-channels\n");

          {
            GPtrArray *array = g_ptr_array_new ();
            GList *ptr;

            for (ptr = priv->channels; ptr; ptr = ptr->next)
              {
                g_ptr_array_add (array,
                    g_strdup (tp_proxy_get_object_path (TP_PROXY (ptr->data))));
              }

            g_value_take_boxed (value, array);
          }

        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
        break;
    }
}

static void
example_handler_class_init (ExampleHandlerClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = example_handler_get_property;

  /* D-Bus properties are exposed as GObject properties through the
   * TpDBusPropertiesMixin */
  /* properties on the Client interface */
  static TpDBusPropertiesMixinPropImpl client_props[] = {
        { "Interfaces", "interfaces", NULL },
        { NULL }
  };

  /* properties on the Client.Handler interface */
  static TpDBusPropertiesMixinPropImpl client_handler_props[] = {
        { "HandlerChannelFilter", "channel-filter", NULL },
        { "BypassApproval", "bypass-approval", NULL },
        { "HandledChannels", "handled-channels", NULL },
        { NULL }
  };

  /* complete list of interfaces with properties */
  static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
        { TP_IFACE_CLIENT,
          tp_dbus_properties_mixin_getter_gobject_properties,
          NULL,
          client_props
        },
        { TP_IFACE_CLIENT_HANDLER,
          tp_dbus_properties_mixin_getter_gobject_properties,
          NULL,
          client_handler_props
        },
        { NULL }
  };

  g_object_class_install_property (object_class, PROP_INTERFACES,
      g_param_spec_boxed ("interfaces",
                          "Interfaces",
                          "Available D-Bus Interfaces",
                          G_TYPE_STRV,
                          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_CHANNEL_FILTER,
      g_param_spec_boxed ("channel-filter",
                          "Channel Filter",
                          "Filter for channels we observe",
                          TP_ARRAY_TYPE_CHANNEL_CLASS_LIST,
                          G_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_BYPASS_APPROVAL,
      g_param_spec_boolean ("bypass-approval",
                            "Bypass Approval",
                            "Whether or not this Client should bypass approval",
                            FALSE,
                            G_PARAM_READABLE));

  g_object_class_install_property (object_class, PROP_HANDLED_CHANNELS,
      g_param_spec_boxed ("handled-channels",
                          "Handled Channels",
                          "List of channels we're handling",
                          TP_ARRAY_TYPE_OBJECT_PATH_LIST,
                          G_PARAM_READABLE));

  /* call our mixin class init */
  klass->dbus_props_class.interfaces = prop_interfaces;
  tp_dbus_properties_mixin_class_init (object_class,
      G_STRUCT_OFFSET (ExampleHandlerClass, dbus_props_class));

  g_type_class_add_private (klass, sizeof (ExampleHandlerPrivate));
}

static void
example_handler_init (ExampleHandler *self)
{
}

static void
observer_iface_init (gpointer g_iface, gpointer iface_data)
{
  TpSvcClientHandlerClass *klass = (TpSvcClientHandlerClass *) g_iface;

#define IMPLEMENT(x) tp_svc_client_handler_implement_##x (klass, \
    example_handler_##x)
  IMPLEMENT (handle_channels);
#undef IMPLEMENT
}

ExampleHandler *
example_handler_new (void)
{
  return g_object_new (TYPE_EXAMPLE_HANDLER, NULL);
}