summaryrefslogtreecommitdiff
path: root/docs/examples/glib_blinkenlight_observer/observer.c
blob: bcace27baee01ed894f248079721b1b93399dd8b (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
/*
 * observer.c - a Telepathy Observer, observes text channels to count their
 *              unread messages
 *
 * Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * 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
 *
 * Authors:
 *   Danielle Madeley <danielle.madeley@collabora.co.uk>
 */

#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/svc-client.h>

#include "observer.h"
#include "channel.h"

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_OBSERVER, ObserverPrivate))

G_DEFINE_TYPE_WITH_CODE (Observer, observer, 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_OBSERVER, observer_iface_init);
    );

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

enum /* properties */
{
  PROP_0,
  PROP_INTERFACES,
  PROP_CHANNEL_FILTER
};

typedef struct
{
  GList *channels;
} ObserverPrivate;

typedef struct
{
  Observer *self;
  GList *pending;
  DBusGMethodInvocation *context;
} ReadyCallbackData;

static void
_update_count (Observer *self)
{
  ObserverPrivate *priv = GET_PRIVATE (self);
  GList *ptr;
  guint total = 0;

  for (ptr = priv->channels; ptr != NULL; ptr = ptr->next)
    {
      guint unread;

      g_object_get (ptr->data,
          "unread", &unread,
          NULL);

      total += unread;
    }

  /* this is where code to interface with the blinking light/whatever goes */
  g_print ("TOTAL UNREAD MESSAGES: %u\n", total);
}

static void
_channel_update_count (GObject *channel,
    GParamSpec *pspec,
    Observer *self)
{
  _update_count (self);
}

static void
_channel_closed (TpProxy *channel,
    guint domain,
    guint code,
    char *message,
    Observer *self)
{
  ObserverPrivate *priv = GET_PRIVATE (self);

  g_debug ("Channel closed: %s", tp_proxy_get_object_path (channel));

  /* remove this channel from the list of active channels */
  priv->channels = g_list_remove (priv->channels, channel);
  _update_count (self);

  g_object_unref (channel);
}

static void
_channel_ready (TpChannel *channel,
    const GError *error,
    gpointer user_data)
{
  ReadyCallbackData *data = user_data;
  Observer *self = data->self;
  ObserverPrivate *priv = GET_PRIVATE (self);

  /* remove the channel from the pending list */
  data->pending = g_list_remove (data->pending, channel);

  if (error == NULL)
    {
      g_debug ("Channel ready: %s", tp_proxy_get_object_path (channel));

      /* put this channel in the list of active channels */
      priv->channels = g_list_prepend (priv->channels, channel);

      tp_g_signal_connect_object (channel, "notify::unread",
          G_CALLBACK (_channel_update_count), self, 0);
      tp_g_signal_connect_object (channel, "invalidated",
          G_CALLBACK (_channel_closed), self, 0);

      _update_count (self);
    }
  else
    {
      g_warning ("Channel ready failed: %s",
          tp_proxy_get_object_path (channel));

      /* drop the ref, this channel is dead */
      g_object_unref (channel);
    }

  if (data->pending == NULL)
    {
      g_debug ("All channels ready");

      /* if all channels for this dispatch are ready, we can return from
       * ObserveChannels */
      tp_svc_client_observer_return_from_observe_channels (data->context);

      g_slice_free (ReadyCallbackData, data);
    }
}

static void
observer_observe_channels (TpSvcClientObserver *self,
    const char *account_path,
    const char *connection_path,
    const GPtrArray *channels,
    const char *dispatch_op_path,
    const GPtrArray *requests_satisfied,
    GHashTable *observer_info,
    DBusGMethodInvocation *context)
{
  TpDBusDaemon *dbus = NULL;
  TpConnection *conn = NULL;
  ReadyCallbackData *data = NULL;
  GError *error = NULL;
  guint i;

  dbus = tp_dbus_daemon_dup (&error);
  if (error != NULL)
    goto error;

  conn = tp_connection_new (dbus, NULL, connection_path, &error);
  if (error != NULL)
    goto error;

  /* returning from the D-Bus method ObserveChannels passes the channels
   * onwards towards the Handler. We want to have the chance to read the
   * initial pending message queue before it gets acknowledged, so we defer
   * returning from ObserveChannels until all the channels are ready */
  data = g_slice_new0 (ReadyCallbackData);
  data->self = OBSERVER (self);
  data->context = context;

  /* build a list of channels and queue them for preparation */
  for (i = 0; i < channels->len; i++)
    {
      GValueArray *channel = g_ptr_array_index (channels, i);
      Channel *chan;
      char *path;
      GHashTable *map;

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

      chan = channel_new (conn, path, map, &error);
      if (error != NULL)
        continue;

      /* place this channel in the list of channels pending preparation
       * for this dispatch */
      data->pending = g_list_prepend (data->pending, chan);
      channel_call_when_ready (chan, _channel_ready, data);
    }

  goto finally;

error:
  dbus_g_method_return_error (context, error);

  g_error_free (error);

finally:
  if (dbus != NULL)
    g_object_unref (dbus);

  if (conn != NULL)
    g_object_unref (conn);
}

static void
observer_get_property (GObject *self,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  switch (property_id)
    {
      case PROP_INTERFACES:
        g_value_set_boxed (value, client_interfaces);
        break;

      case PROP_CHANNEL_FILTER:
        {
          GPtrArray *array = g_ptr_array_new ();

          /* Observe all text channels */
          g_ptr_array_add (array, tp_asv_new (
                TP_IFACE_CHANNEL ".ChannelType",
                G_TYPE_STRING,
                TP_IFACE_CHANNEL_TYPE_TEXT,

                NULL));

          g_value_take_boxed (value, array);
          break;
        }

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

static void
observer_class_init (ObserverClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = observer_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.Observer interface */
  static TpDBusPropertiesMixinPropImpl client_observer_props[] = {
        { "ObserverChannelFilter", "channel-filter", NULL },
        /* NB: eventually we'll want to support the Recover property:
         *     https://bugs.freedesktop.org/show_bug.cgi?id=24768 */
        { 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_OBSERVER,
          tp_dbus_properties_mixin_getter_gobject_properties,
          NULL,
          client_observer_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_PARAM_STATIC_STRINGS));

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

  g_type_class_add_private (object_class, sizeof (ObserverPrivate));
}

static void
observer_init (Observer *self)
{
}

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

#define IMPLEMENT(x) tp_svc_client_observer_implement_##x (klass, \
    observer_##x)
  IMPLEMENT (observe_channels);
#undef IMPLEMENT
}

Observer *
observer_new (void)
{
  return g_object_new (TYPE_OBSERVER, NULL);
}