summaryrefslogtreecommitdiff
path: root/telepathy-glib/simple-observer.c
blob: ee74a84781f916a0507f344e1afac4775956f292 (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 * Simple implementation of an Observer
 *
 * Copyright © 2010 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
 */

/**
 * SECTION: simple-observer
 * @title: TpSimpleObserver
 * @short_description: a subclass of #TpBaseClient implementing
 * a simple Observer
 *
 * This class makes it easier to write #TpSvcClient implementing the
 * TpSvcClientObserver interface.
 *
 * A typical simple observer would look liks this:
 * |[
 * static void
 * my_observe_channels (TpSimpleObserver *observer,
 *    TpAccount *account,
 *    TpConnection *connection,
 *    GList *channels,
 *    TpChannelDispatchOperation *dispatch_operation,
 *    GList *requests,
 *    TpObserveChannelsContext *context,
 *    gpointer user_data)
 * {
 *  /<!-- -->* do something useful with the channels here *<!-- -->/
 *
 *  tp_observe_channels_context_accept (context);
 * }
 *
 * factory = tp_automatic_client_factory_new (dbus);
 * client = tp_simple_observer_new_with_factory (factory, TRUE, "MyObserver",
 *     FALSE, my_observe_channels, user_data);
 * g_object_unref (factory);
 *
 * tp_base_client_take_observer_filter (client, tp_asv_new (
 *      TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING, TP_IFACE_CHANNEL_TYPE_TEXT,
 *      TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
 *      NULL));
 *
 * tp_base_client_register (client, NULL);
 * ]|
 *
 * See examples/client/media-observer.c for a complete example.
 */

/**
 * TpSimpleObserver:
 *
 * Data structure representing a simple Observer implementation.
 *
 * Since: 0.11.5
 */

/**
 * TpSimpleObserverClass:
 *
 * The class of a #TpSimpleObserver.
 *
 * Since: 0.11.5
 */

/**
 * TpSimpleObserverObserveChannelsImpl:
 * @observer: a #TpSimpleObserver instance
 * @account: a #TpAccount having %TP_ACCOUNT_FEATURE_CORE prepared if possible
 * @connection: a #TpConnection having %TP_CONNECTION_FEATURE_CORE prepared
 * if possible
 * @channels: (element-type TelepathyGLib.Channel): a #GList of #TpChannel,
 *  all having %TP_CHANNEL_FEATURE_CORE prepared if possible
 * @dispatch_operation: (allow-none): a #TpChannelDispatchOperation or %NULL;
 *  the dispatch_operation is not guaranteed to be prepared
 * @requests: (element-type TelepathyGLib.ChannelRequest): a #GList of
 *  #TpChannelRequest, all having their object-path defined but are not
 *  guaranteed to be prepared.
 * @context: a #TpObserveChannelsContext representing the context of this
 *  D-Bus call
 * @user_data: arbitrary user-supplied data passed to tp_simple_observer_new()
 *
 * Signature of the implementation of the ObserveChannels method.
 *
 * This function must call either tp_observe_channels_context_accept(),
 * tp_observe_channels_context_delay() or tp_observe_channels_context_fail()
 * on @context before it returns.
 *
 * Since: 0.11.5
 */

#include "config.h"

#include "telepathy-glib/simple-observer.h"

#define DEBUG_FLAG TP_DEBUG_CLIENT
#include "telepathy-glib/debug-internal.h"

G_DEFINE_TYPE(TpSimpleObserver, tp_simple_observer, TP_TYPE_BASE_CLIENT)

enum {
    PROP_RECOVER = 1,
    PROP_CALLBACK,
    PROP_USER_DATA,
    PROP_DESTROY,
    N_PROPS
};

struct _TpSimpleObserverPrivate
{
  TpSimpleObserverObserveChannelsImpl callback;
  gpointer user_data;
  GDestroyNotify destroy;
};

static void
tp_simple_observer_init (TpSimpleObserver *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, TP_TYPE_SIMPLE_OBSERVER,
      TpSimpleObserverPrivate);
}

static void
tp_simple_observer_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  TpBaseClient *base = TP_BASE_CLIENT (object);
  TpSimpleObserver *self = TP_SIMPLE_OBSERVER (object);

  switch (property_id)
    {
      case PROP_RECOVER:
        tp_base_client_set_observer_recover (base, g_value_get_boolean (value));
        break;

      case PROP_CALLBACK:
        self->priv->callback = g_value_get_pointer (value);
        break;

      case PROP_USER_DATA:
        self->priv->user_data = g_value_get_pointer (value);
        break;

      case PROP_DESTROY:
        self->priv->destroy = g_value_get_pointer (value);
        break;

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

static void
tp_simple_observer_constructed (GObject *object)
{
  TpSimpleObserver *self = TP_SIMPLE_OBSERVER (object);
  void (*chain_up) (GObject *) =
    ((GObjectClass *) tp_simple_observer_parent_class)->constructed;

  g_assert (self->priv->callback != NULL);

  if (chain_up != NULL)
    chain_up (object);
}

static void
tp_simple_observer_dispose (GObject *object)
{
  TpSimpleObserver *self = TP_SIMPLE_OBSERVER (object);
  void (*dispose) (GObject *) =
    G_OBJECT_CLASS (tp_simple_observer_parent_class)->dispose;

  if (self->priv->destroy != NULL)
    {
      self->priv->destroy (self->priv->user_data);
      self->priv->destroy = NULL;
    }

  if (dispose != NULL)
    dispose (object);
}

static void
observe_channels (
    TpBaseClient *client,
    TpAccount *account,
    TpConnection *connection,
    GList *channels,
    TpChannelDispatchOperation *dispatch_operation,
    GList *requests,
    TpObserveChannelsContext *context)
{
  TpSimpleObserver *self = TP_SIMPLE_OBSERVER (client);

  self->priv->callback (self, account, connection, channels,
      dispatch_operation, requests, context, self->priv->user_data);
}

static void
tp_simple_observer_class_init (TpSimpleObserverClass *cls)
{
  GObjectClass *object_class = G_OBJECT_CLASS (cls);
  TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (cls);
  GParamSpec *param_spec;

  g_type_class_add_private (cls, sizeof (TpSimpleObserverPrivate));

  object_class->set_property = tp_simple_observer_set_property;
  object_class->constructed = tp_simple_observer_constructed;
  object_class->dispose = tp_simple_observer_dispose;

  /**
   * TpSimpleObserver:recover:
   *
   * The value of the Observer.Recover D-Bus property.
   *
   * Since: 0.11.5
   */
  param_spec = g_param_spec_boolean ("recover", "Recover",
      "Observer.Recover",
      FALSE,
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_RECOVER,
      param_spec);

  /**
   * TpSimpleObserver:callback:
   *
   * The TpSimpleObserverObserveChannelsImpl callback implementing the
   * ObserveChannels D-Bus method.
   *
   * This property can't be %NULL.
   *
   * Since: 0.11.5
   */
  param_spec = g_param_spec_pointer ("callback",
      "Callback",
      "Function called when ObserveChannels is called",
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CALLBACK,
      param_spec);

  /**
   * TpSimpleObserver:user-data:
   *
   * The user-data pointer passed to the callback implementing the
   * ObserveChannels D-Bus method.
   *
   * Since: 0.11.5
   */
  param_spec = g_param_spec_pointer ("user-data", "user data",
      "pointer passed as user-data when ObserveChannels is called",
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_USER_DATA,
      param_spec);

  /**
   * TpSimpleObserver:destroy:
   *
   * The #GDestroyNotify function called to free the user-data pointer when
   * the #TpSimpleObserver is destroyed.
   *
   * Since: 0.11.5
   */
  param_spec = g_param_spec_pointer ("destroy", "destroy",
      "function called to destroy the user-data when destroying the observer",
      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_DESTROY,
      param_spec);

  base_clt_cls->observe_channels = observe_channels;
}

/**
 * tp_simple_observer_new:
 * @dbus: a #TpDBusDaemon object, may not be %NULL
 * @recover: the value of the Observer.Recover D-Bus property
 * @name: the name of the Observer (see #TpBaseClient:name: for details)
 * @uniquify: the value of the #TpBaseClient:uniquify-name: property
 * @callback: the function called when ObserveChannels is called
 * @user_data: arbitrary user-supplied data passed to @callback
 * @destroy: called with the user_data as argument, when the #TpSimpleObserver
 * is destroyed
 *
 * Convenient function to create a new #TpSimpleObserver instance.
 *
 * If @dbus is not the result of tp_dbus_daemon_dup(), you should call
 * tp_simple_observer_new_with_am() instead, so that #TpAccount,
 * #TpConnection and #TpContact instances can be shared between modules.
 *
 * Returns: (type TelepathyGLib.SimpleObserver): a new #TpSimpleObserver
 *
 * Since: 0.11.5
 * Deprecated: New code should use tp_simple_observer_new_with_am() instead.
 */
TpBaseClient *
tp_simple_observer_new (TpDBusDaemon *dbus,
    gboolean recover,
    const gchar *name,
    gboolean uniquify,
    TpSimpleObserverObserveChannelsImpl callback,
    gpointer user_data,
    GDestroyNotify destroy)
{
  return g_object_new (TP_TYPE_SIMPLE_OBSERVER,
      "dbus-daemon", dbus,
      "recover", recover,
      "name", name,
      "uniquify-name", uniquify,
      "callback", callback,
      "user-data", user_data,
      "destroy", destroy,
      NULL);
}

/**
 * tp_simple_observer_new_with_am:
 * @account_manager: an account manager, which may not be %NULL
 * @recover: the value of the Observer.Recover D-Bus property
 * @name: the name of the Observer (see #TpBaseClient:name: for details)
 * @uniquify: the value of the #TpBaseClient:uniquify-name: property
 * @callback: the function called when ObserveChannels is called
 * @user_data: arbitrary user-supplied data passed to @callback
 * @destroy: called with the user_data as argument, when the #TpSimpleObserver
 * is destroyed
 *
 * Convenient function to create a new #TpSimpleObserver instance with a
 * specified #TpAccountManager.
 *
 * It is not necessary to prepare any features on @account_manager before
 * calling this function.
 *
 * Returns: (type TelepathyGLib.SimpleObserver): a new #TpSimpleObserver
 *
 * Since: 0.11.14
 */
TpBaseClient *
tp_simple_observer_new_with_am (TpAccountManager *account_manager,
    gboolean recover,
    const gchar *name,
    gboolean uniquify,
    TpSimpleObserverObserveChannelsImpl callback,
    gpointer user_data,
    GDestroyNotify destroy)
{
  return g_object_new (TP_TYPE_SIMPLE_OBSERVER,
      "account-manager", account_manager,
      "recover", recover,
      "name", name,
      "uniquify-name", uniquify,
      "callback", callback,
      "user-data", user_data,
      "destroy", destroy,
      NULL);
}

/**
 * tp_simple_observer_new_with_factory:
 * @factory: a #TpSimpleClientFactory, which may not be %NULL
 * @recover: the value of the Observer.Recover D-Bus property
 * @name: the name of the Observer (see #TpBaseClient:name: for details)
 * @uniquify: the value of the #TpBaseClient:uniquify-name: property
 * @callback: the function called when ObserveChannels is called
 * @user_data: arbitrary user-supplied data passed to @callback
 * @destroy: called with the user_data as argument, when the #TpSimpleObserver
 * is destroyed
 *
 * Convenient function to create a new #TpSimpleObserver instance with a
 * specified #TpSimpleClientFactory.
 *
 * Returns: (type TelepathyGLib.SimpleObserver): a new #TpSimpleObserver
 *
 * Since: 0.15.5
 */
TpBaseClient *
tp_simple_observer_new_with_factory (TpSimpleClientFactory *factory,
    gboolean recover,
    const gchar *name,
    gboolean uniquify,
    TpSimpleObserverObserveChannelsImpl callback,
    gpointer user_data,
    GDestroyNotify destroy)
{
  return g_object_new (TP_TYPE_SIMPLE_OBSERVER,
      "factory", factory,
      "recover", recover,
      "name", name,
      "uniquify-name", uniquify,
      "callback", callback,
      "user-data", user_data,
      "destroy", destroy,
      NULL);
}