summaryrefslogtreecommitdiff
path: root/src/plugin-dispatch-operation.c
blob: cd9c279cfbb53bfc856a54fcdbec2dc2ccbb6908 (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
410
411
412
413
414
415
416
417
418
/* Representation of a dispatch operation as presented to plugins. This is
 * deliberately a "smaller" API than McdDispatchOperation.
 *
 * Copyright (C) 2009 Nokia Corporation
 * Copyright (C) 2009 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 "plugin-dispatch-operation.h"

#include "mission-control-plugins/implementation.h"

#include "mcd-channel-priv.h"
#include "mcd-debug.h"

/* a larger numeric value will override a smaller one */
typedef enum {
    PLUGIN_ACTION_NONE,
    PLUGIN_ACTION_CLOSE,
    PLUGIN_ACTION_LEAVE,
    PLUGIN_ACTION_DESTROY
} PluginAction;

enum {
    PROP_0,
    PROP_REAL_CDO
};

struct _McdPluginDispatchOperation {
    GObject parent;
    McdDispatchOperation *real_cdo;
    PluginAction after_observers;
    TpChannelGroupChangeReason reason;
    gchar *message;
};

struct _McdPluginDispatchOperationClass {
    GObjectClass parent;
};

static void plugin_iface_init (McpDispatchOperationIface *iface,
    gpointer unused G_GNUC_UNUSED);

G_DEFINE_TYPE_WITH_CODE (McdPluginDispatchOperation,
    _mcd_plugin_dispatch_operation, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (MCP_TYPE_DISPATCH_OPERATION, plugin_iface_init))

static void
_mcd_plugin_dispatch_operation_init (McdPluginDispatchOperation *self)
{
  DEBUG ("%p", self);
}

static void
plugin_do_set_property (GObject *object,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
  McdPluginDispatchOperation *self = (McdPluginDispatchOperation *) object;

  switch (prop_id)
    {
    case PROP_REAL_CDO:
      g_assert (self->real_cdo == NULL); /* construct-only */
      /* The real CDO is borrowed, because this plugin API is owned by it */
      self->real_cdo = g_value_get_object (value);
      break;

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

static void
plugin_do_finalize (GObject *object)
{
  McdPluginDispatchOperation *self = (McdPluginDispatchOperation *) object;
  GObjectFinalizeFunc finalize =
    G_OBJECT_CLASS (_mcd_plugin_dispatch_operation_parent_class)->finalize;

  DEBUG ("%p", object);

  g_free (self->message);

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

static void
_mcd_plugin_dispatch_operation_class_init (
    McdPluginDispatchOperationClass *cls)
{
  GObjectClass *object_class = (GObjectClass *) cls;

  object_class->set_property = plugin_do_set_property;
  object_class->finalize = plugin_do_finalize;

  g_object_class_install_property (object_class, PROP_REAL_CDO,
      g_param_spec_object ("real-cdo", "Real channel dispatch operation",
          "Borrowed pointer to the underlying McdDispatchOperation",
          MCD_TYPE_DISPATCH_OPERATION,
          G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
}

McdPluginDispatchOperation *
_mcd_plugin_dispatch_operation_new (McdDispatchOperation *real_cdo)
{
  McdPluginDispatchOperation *self;

  self = g_object_new (MCD_TYPE_PLUGIN_DISPATCH_OPERATION,
      "real-cdo", real_cdo,
      NULL);
  DEBUG ("%p (for %p)", self, real_cdo);

  return self;
}

static const gchar *
plugin_do_get_account_path (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  g_return_val_if_fail (self != NULL, NULL);
  return _mcd_dispatch_operation_get_account_path (self->real_cdo);
}

static const gchar *
plugin_do_get_connection_path (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  g_return_val_if_fail (self != NULL, NULL);
  return _mcd_dispatch_operation_get_connection_path (self->real_cdo);
}

static const gchar *
plugin_do_get_protocol (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  g_return_val_if_fail (self != NULL, NULL);
  return _mcd_dispatch_operation_get_protocol (self->real_cdo);
}

static const gchar *
plugin_do_get_cm_name (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  g_return_val_if_fail (self != NULL, NULL);
  return _mcd_dispatch_operation_get_cm_name (self->real_cdo);
}


/* Channels */
static guint
plugin_do_get_n_channels (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  g_return_val_if_fail (self != NULL, 0);

  if (_mcd_dispatch_operation_peek_channel (self->real_cdo) != NULL)
    return 1;

  return 0;
}

static const gchar *
plugin_do_get_nth_channel_path (McpDispatchOperation *obj,
    guint n)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);
  McdChannel *channel;

  g_return_val_if_fail (self != NULL, NULL);

  channel = _mcd_dispatch_operation_peek_channel (self->real_cdo);

  if (channel == NULL || n != 0)
    return NULL;

  return mcd_channel_get_object_path (channel);
}

static GHashTable *
plugin_do_ref_nth_channel_properties (McpDispatchOperation *obj,
    guint n)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);
  McdChannel *channel;
  GVariant *variant;
  GValue value = G_VALUE_INIT;
  GHashTable *ret;

  g_return_val_if_fail (self != NULL, NULL);

  channel = _mcd_dispatch_operation_peek_channel (self->real_cdo);

  if (channel == NULL || n != 0)
    return NULL;

  variant = mcd_channel_dup_immutable_properties (channel);

  if (variant == NULL)
    return NULL;

  /* For compatibility, we have to return the older type here. */
  dbus_g_value_parse_g_variant (variant, &value);
  g_assert (G_VALUE_HOLDS (&value, TP_HASH_TYPE_STRING_VARIANT_MAP));
  ret = g_value_dup_boxed (&value);
  g_value_unset (&value);

  return ret;
}


/* Delay the dispatch */

/* an arbitrary constant, to detect use-after-free or wrong pointers */
#define DELAY_MAGIC 0xCD053

typedef struct {
    gsize magic;
    McdPluginDispatchOperation *self;
} RealDelay;

static McpDispatchOperationDelay *
plugin_do_start_delay (McpDispatchOperation *obj)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);
  RealDelay *delay;

  DEBUG ("%p", self);

  g_return_val_if_fail (self != NULL, NULL);
  delay = g_slice_new (RealDelay);
  delay->magic = DELAY_MAGIC;
  delay->self = g_object_ref (obj);
  _mcd_dispatch_operation_start_plugin_delay (self->real_cdo);
  return (McpDispatchOperationDelay *) delay;
}

static void
plugin_do_end_delay (McpDispatchOperation *obj,
    McpDispatchOperationDelay *delay)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);
  RealDelay *real_delay = (RealDelay *) delay;

  DEBUG ("%p", self);

  g_return_if_fail (self != NULL);
  g_return_if_fail (real_delay->self == self);
  g_return_if_fail (real_delay->magic == DELAY_MAGIC);

  real_delay->magic = ~(DELAY_MAGIC);
  real_delay->self = NULL;
  _mcd_dispatch_operation_end_plugin_delay (self->real_cdo);
  g_object_unref (self);
}


/* Close */
static void
plugin_do_leave_channels (McpDispatchOperation *obj,
    gboolean wait_for_observers, TpChannelGroupChangeReason reason,
    const gchar *message)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  DEBUG ("%p (wait=%c reason=%d message=%s)", self,
      wait_for_observers ? 'T' : 'F', reason, message);

  g_return_if_fail (self != NULL);

  if (wait_for_observers)
    {
      if (self->after_observers < PLUGIN_ACTION_LEAVE)
        {
          DEBUG ("Remembering for later");
          self->after_observers = PLUGIN_ACTION_LEAVE;
          self->reason = reason;
          g_free (self->message);
          self->message = g_strdup (message);
        }
    }
  else
    {
      DEBUG ("Leaving now");
      _mcd_dispatch_operation_leave_channels (self->real_cdo, reason, message);
    }
}

static void
plugin_do_close_channels (McpDispatchOperation *obj,
    gboolean wait_for_observers)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  DEBUG ("%p (wait=%c)", self, wait_for_observers ? 'T' : 'F');

  g_return_if_fail (self != NULL);

  if (wait_for_observers)
    {
      if (self->after_observers < PLUGIN_ACTION_CLOSE)
        {
          DEBUG ("Remembering for later");
          self->after_observers = PLUGIN_ACTION_CLOSE;
        }
    }
  else
    {
      DEBUG ("Closing now");
      _mcd_dispatch_operation_close_channels (self->real_cdo);
    }
}

static void
plugin_do_destroy_channels (McpDispatchOperation *obj,
    gboolean wait_for_observers)
{
  McdPluginDispatchOperation *self = MCD_PLUGIN_DISPATCH_OPERATION (obj);

  DEBUG ("%p (wait=%c)", self, wait_for_observers ? 'T' : 'F');

  g_return_if_fail (self != NULL);

  if (wait_for_observers)
    {
      if (self->after_observers < PLUGIN_ACTION_DESTROY)
        self->after_observers = PLUGIN_ACTION_DESTROY;
    }
  else
    {
      _mcd_dispatch_operation_destroy_channels (self->real_cdo);
    }
}

void
_mcd_plugin_dispatch_operation_observers_finished (
    McdPluginDispatchOperation *self)
{
  DEBUG ("%p", self);

  switch (self->after_observers)
    {
    case PLUGIN_ACTION_DESTROY:
      DEBUG ("destroying now");
      _mcd_dispatch_operation_destroy_channels (self->real_cdo);
      break;

    case PLUGIN_ACTION_LEAVE:
      DEBUG ("leaving now: %d %s", self->reason, self->message);
      _mcd_dispatch_operation_leave_channels (self->real_cdo,
          self->reason, self->message);
      break;

    case PLUGIN_ACTION_CLOSE:
      DEBUG ("closing now");
      _mcd_dispatch_operation_close_channels (self->real_cdo);
      break;

    case PLUGIN_ACTION_NONE:
      /* nothing to do */
      break;
    }
}

gboolean
_mcd_plugin_dispatch_operation_will_terminate (
    McdPluginDispatchOperation *self)
{
  return (self->after_observers != PLUGIN_ACTION_NONE);
}

static void
plugin_iface_init (McpDispatchOperationIface *iface,
    gpointer unused G_GNUC_UNUSED)
{
  DEBUG ("called");

  iface->get_account_path = plugin_do_get_account_path;
  iface->get_connection_path = plugin_do_get_connection_path;
  iface->get_protocol = plugin_do_get_protocol;
  iface->get_cm_name = plugin_do_get_cm_name;

  iface->get_n_channels = plugin_do_get_n_channels;
  iface->get_nth_channel_path = plugin_do_get_nth_channel_path;
  iface->ref_nth_channel_properties = plugin_do_ref_nth_channel_properties;

  iface->start_delay = plugin_do_start_delay;
  iface->end_delay = plugin_do_end_delay;

  iface->leave_channels = plugin_do_leave_channels;
  iface->close_channels = plugin_do_close_channels;
  iface->destroy_channels = plugin_do_destroy_channels;
}