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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* empathy-dispatch-operation.c - Source for EmpathyDispatchOperation
* Copyright (C) 2008 Collabora Ltd.
* @author Sjoerd Simons <sjoerd.simons@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
*/
#include <stdio.h>
#include <stdlib.h>
#include "empathy-dispatch-operation.h"
#include <libempathy/empathy-enum-types.h>
#include <libempathy/empathy-tp-chat.h>
#include <libempathy/empathy-tp-call.h>
#include <libempathy/empathy-tp-file.h>
#include "empathy-marshal.h"
#include "extensions/extensions.h"
#define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
#include <libempathy/empathy-debug.h>
G_DEFINE_TYPE(EmpathyDispatchOperation, empathy_dispatch_operation,
G_TYPE_OBJECT)
static void empathy_dispatch_operation_set_status (
EmpathyDispatchOperation *self, EmpathyDispatchOperationState status);
static void empathy_dispatch_operation_channel_ready_cb (TpChannel *channel,
const GError *error, gpointer user_data);
/* signal enum */
enum
{
/* Ready for dispatching */
READY,
/* Approved by an approver, can only happens on incoming operations */
APPROVED,
/* Claimed by a handler */
CLAIMED,
/* Error, channel went away, inspecting it failed etc */
INVALIDATED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = {0};
/* properties */
enum {
PROP_CONNECTION = 1,
PROP_CHANNEL,
PROP_CHANNEL_WRAPPER,
PROP_CONTACT,
PROP_INCOMING,
PROP_STATUS,
};
/* private structure */
typedef struct _EmpathyDispatchOperationPriv \
EmpathyDispatchOperationPriv;
struct _EmpathyDispatchOperationPriv
{
gboolean dispose_has_run;
TpConnection *connection;
TpChannel *channel;
GObject *channel_wrapper;
EmpathyContact *contact;
EmpathyDispatchOperationState status;
gboolean incoming;
gboolean approved;
gulong invalidated_handler;
gulong ready_handler;
};
#define GET_PRIV(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_DISPATCH_OPERATION, \
EmpathyDispatchOperationPriv))
static void
empathy_dispatch_operation_init (EmpathyDispatchOperation *obj)
{
//EmpathyDispatchOperationPriv *priv =
// GET_PRIV (obj);
/* allocate any data required by the object here */
}
static void empathy_dispatch_operation_dispose (GObject *object);
static void empathy_dispatch_operation_finalize (GObject *object);
static void
empathy_dispatch_operation_set_property (GObject *object,
guint property_id, const GValue *value, GParamSpec *pspec)
{
EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (object);
EmpathyDispatchOperationPriv *priv = GET_PRIV (operation);
switch (property_id)
{
case PROP_CONNECTION:
priv->connection = g_value_dup_object (value);
break;
case PROP_CHANNEL:
priv->channel = g_value_dup_object (value);
break;
case PROP_CHANNEL_WRAPPER:
priv->channel_wrapper = g_value_dup_object (value);
break;
case PROP_CONTACT:
if (priv->contact != NULL)
g_object_unref (priv->contact);
priv->contact = g_value_dup_object (value);
break;
case PROP_INCOMING:
priv->incoming = g_value_get_boolean (value);
break;
}
}
static void
empathy_dispatch_operation_get_property (GObject *object,
guint property_id, GValue *value, GParamSpec *pspec)
{
EmpathyDispatchOperation *operation = EMPATHY_DISPATCH_OPERATION (object);
EmpathyDispatchOperationPriv *priv = GET_PRIV (operation);
switch (property_id)
{
case PROP_CONNECTION:
g_value_set_object (value, priv->connection);
break;
case PROP_CHANNEL:
g_value_set_object (value, priv->channel);
break;
case PROP_CHANNEL_WRAPPER:
g_value_set_object (value, priv->channel_wrapper);
break;
case PROP_CONTACT:
g_value_set_object (value, priv->contact);
break;
case PROP_INCOMING:
g_value_set_boolean (value, priv->incoming);
break;
case PROP_STATUS:
g_value_set_enum (value, priv->status);
break;
}
}
static void
empathy_dispatch_operation_invalidated (TpProxy *proxy, guint domain,
gint code, char *message, EmpathyDispatchOperation *self)
{
empathy_dispatch_operation_set_status (self,
EMPATHY_DISPATCHER_OPERATION_STATE_INVALIDATED);
g_signal_emit (self, signals[INVALIDATED], 0, domain, code, message);
}
static void
empathy_dispatch_operation_constructed (GObject *object)
{
EmpathyDispatchOperation *self = EMPATHY_DISPATCH_OPERATION (object);
EmpathyDispatchOperationPriv *priv = GET_PRIV (self);
empathy_dispatch_operation_set_status (self,
EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING);
priv->invalidated_handler =
g_signal_connect (priv->channel, "invalidated",
G_CALLBACK (empathy_dispatch_operation_invalidated), self);
tp_channel_call_when_ready (priv->channel,
empathy_dispatch_operation_channel_ready_cb, self);
}
static void
empathy_dispatch_operation_class_init (
EmpathyDispatchOperationClass *empathy_dispatch_operation_class)
{
GObjectClass *object_class =
G_OBJECT_CLASS (empathy_dispatch_operation_class);
GParamSpec *param_spec;
g_type_class_add_private (empathy_dispatch_operation_class,
sizeof (EmpathyDispatchOperationPriv));
object_class->set_property = empathy_dispatch_operation_set_property;
object_class->get_property = empathy_dispatch_operation_get_property;
object_class->dispose = empathy_dispatch_operation_dispose;
object_class->finalize = empathy_dispatch_operation_finalize;
object_class->constructed = empathy_dispatch_operation_constructed;
signals[READY] = g_signal_new ("ready",
G_OBJECT_CLASS_TYPE(empathy_dispatch_operation_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[APPROVED] = g_signal_new ("approved",
G_OBJECT_CLASS_TYPE(empathy_dispatch_operation_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[CLAIMED] = g_signal_new ("claimed",
G_OBJECT_CLASS_TYPE(empathy_dispatch_operation_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
signals[INVALIDATED] = g_signal_new ("invalidated",
G_OBJECT_CLASS_TYPE(empathy_dispatch_operation_class),
G_SIGNAL_RUN_LAST,
0,
NULL, NULL,
_empathy_marshal_VOID__UINT_INT_STRING,
G_TYPE_NONE, 3, G_TYPE_UINT, G_TYPE_INT, G_TYPE_STRING);
param_spec = g_param_spec_object ("connection",
"connection", "The telepathy connection",
TP_TYPE_CONNECTION,
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CONNECTION,
param_spec);
param_spec = g_param_spec_object ("channel",
"channel", "The telepathy channel",
TP_TYPE_CHANNEL,
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CHANNEL,
param_spec);
param_spec = g_param_spec_object ("channel-wrapper",
"channel wrapper", "The empathy specific channel wrapper",
G_TYPE_OBJECT,
G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CHANNEL_WRAPPER,
param_spec);
param_spec = g_param_spec_object ("contact",
"contact", "The empathy contact",
EMPATHY_TYPE_CONTACT,
G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_CONTACT,
param_spec);
param_spec = g_param_spec_boolean ("incoming",
"incoming", "Whether or not the channel is incoming",
FALSE,
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_INCOMING,
param_spec);
param_spec = g_param_spec_enum ("status",
"status", "Status of the dispatch operation",
EMPATHY_TYPE_DISPATCH_OPERATION_STATE, 0,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_STATUS, param_spec);
}
void
empathy_dispatch_operation_dispose (GObject *object)
{
EmpathyDispatchOperation *self = EMPATHY_DISPATCH_OPERATION (object);
EmpathyDispatchOperationPriv *priv =
GET_PRIV (self);
if (priv->dispose_has_run)
return;
priv->dispose_has_run = TRUE;
g_object_unref (priv->connection);
if (priv->channel_wrapper != NULL)
g_object_unref (priv->channel_wrapper);
if (priv->ready_handler != 0)
g_signal_handler_disconnect (priv->channel_wrapper,
priv->invalidated_handler);
g_signal_handler_disconnect (priv->channel, priv->invalidated_handler);
g_object_unref (priv->channel);
if (priv->contact != NULL)
g_object_unref (priv->contact);
if (G_OBJECT_CLASS (empathy_dispatch_operation_parent_class)->dispose)
G_OBJECT_CLASS (empathy_dispatch_operation_parent_class)->dispose (object);
}
void
empathy_dispatch_operation_finalize (GObject *object)
{
/* free any data held directly by the object here */
G_OBJECT_CLASS (empathy_dispatch_operation_parent_class)->finalize (object);
}
static void
empathy_dispatch_operation_set_status (EmpathyDispatchOperation *self,
EmpathyDispatchOperationState status)
{
EmpathyDispatchOperationPriv *priv = GET_PRIV (self);
g_assert (status >= priv->status);
if (priv->status != status)
{
DEBUG ("Dispatch operation %s status: %d -> %d",
empathy_dispatch_operation_get_object_path (self),
priv->status, status);
priv->status = status;
g_object_notify (G_OBJECT (self), "status");
if (status == EMPATHY_DISPATCHER_OPERATION_STATE_PENDING)
g_signal_emit (self, signals[READY], 0);
}
}
static void
empathy_dispatcher_operation_tp_chat_ready_cb (GObject *object,
GParamSpec *spec, gpointer user_data)
{
EmpathyDispatchOperation *self = EMPATHY_DISPATCH_OPERATION (user_data);
EmpathyDispatchOperationPriv *priv = GET_PRIV (self);
if (!empathy_tp_chat_is_ready (EMPATHY_TP_CHAT (priv->channel_wrapper)))
return;
g_signal_handler_disconnect (priv->channel_wrapper, priv->ready_handler);
priv->ready_handler = 0;
empathy_dispatch_operation_set_status (self,
EMPATHY_DISPATCHER_OPERATION_STATE_PENDING);
}
static void
empathy_dispatch_operation_channel_ready_cb (TpChannel *channel,
const GError *error, gpointer user_data)
{
EmpathyDispatchOperation *self = EMPATHY_DISPATCH_OPERATION (user_data);
EmpathyDispatchOperationPriv *priv = GET_PRIV (self);
GQuark channel_type;
/* The error will be handled in empathy_dispatch_operation_invalidated */
if (error != NULL)
return;
g_assert (channel == priv->channel);
/* If the channel wrapper is defined, we assume it's ready */
if (priv->channel_wrapper != NULL)
goto ready;
channel_type = tp_channel_get_channel_type_id (channel);
if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
{
EmpathyTpChat *chat= empathy_tp_chat_new (channel);
priv->channel_wrapper = G_OBJECT (chat);
if (!empathy_tp_chat_is_ready (chat))
{
priv->ready_handler = g_signal_connect (chat, "notify::ready",
G_CALLBACK (empathy_dispatcher_operation_tp_chat_ready_cb), self);
return;
}
}
else if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
{
EmpathyTpCall *call = empathy_tp_call_new (channel);
priv->channel_wrapper = G_OBJECT (call);
}
else if (channel_type == EMP_IFACE_QUARK_CHANNEL_TYPE_FILE_TRANSFER)
{
EmpathyTpFile *file = empathy_tp_file_new (channel);
priv->channel_wrapper = G_OBJECT (file);
}
ready:
empathy_dispatch_operation_set_status (self,
EMPATHY_DISPATCHER_OPERATION_STATE_PENDING);
}
EmpathyDispatchOperation *
empathy_dispatch_operation_new (TpConnection *connection, TpChannel *channel,
EmpathyContact *contact, gboolean incoming)
{
return empathy_dispatch_operation_new_with_wrapper (connection, channel,
contact, incoming, NULL);
}
EmpathyDispatchOperation *
empathy_dispatch_operation_new_with_wrapper (TpConnection *connection,
TpChannel *channel, EmpathyContact *contact, gboolean incoming,
GObject *wrapper)
{
g_return_val_if_fail (connection != NULL, NULL);
g_return_val_if_fail (channel != NULL, NULL);
return EMPATHY_DISPATCH_OPERATION (
g_object_new (EMPATHY_TYPE_DISPATCH_OPERATION,
"connection", connection,
"channel", channel,
"channel-wrapper", wrapper,
"contact", contact,
"incoming", incoming,
NULL));
}
void
empathy_dispatch_operation_start (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
priv = GET_PRIV (operation);
g_return_if_fail (
priv->status == EMPATHY_DISPATCHER_OPERATION_STATE_PENDING);
if (priv->incoming && !priv->approved)
empathy_dispatch_operation_set_status (operation,
EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING);
else
empathy_dispatch_operation_set_status (operation,
EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
}
void
empathy_dispatch_operation_approve (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation));
priv = GET_PRIV (operation);
if (priv->status == EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
{
DEBUG ("Approving operation %s",
empathy_dispatch_operation_get_object_path (operation));
empathy_dispatch_operation_set_status (operation,
EMPATHY_DISPATCHER_OPERATION_STATE_DISPATCHING);
g_signal_emit (operation, signals[APPROVED], 0);
}
else if (priv->status < EMPATHY_DISPATCHER_OPERATION_STATE_APPROVING)
{
DEBUG ("Pre-approving operation %s",
empathy_dispatch_operation_get_object_path (operation));
priv->approved = TRUE;
}
else
{
DEBUG (
"Ignoring approval for %s as it's already past the approval stage",
empathy_dispatch_operation_get_object_path (operation));
}
}
/* Returns whether or not the operation was successfully claimed */
gboolean
empathy_dispatch_operation_claim (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), FALSE);
priv = GET_PRIV (operation);
if (priv->status == EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED)
return FALSE;
empathy_dispatch_operation_set_status (operation,
EMPATHY_DISPATCHER_OPERATION_STATE_CLAIMED);
g_signal_emit (operation, signals[CLAIMED], 0);
return TRUE;
}
TpConnection *
empathy_dispatch_operation_get_tp_connection (
EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), NULL);
priv = GET_PRIV (operation);
return g_object_ref (priv->connection);
}
TpChannel *
empathy_dispatch_operation_get_channel (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), NULL);
priv = GET_PRIV (operation);
return priv->channel;
}
GObject *
empathy_dispatch_operation_get_channel_wrapper (
EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), NULL);
priv = GET_PRIV (operation);
return priv->channel_wrapper;
}
const gchar *
empathy_dispatch_operation_get_channel_type (
EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), NULL);
priv = GET_PRIV (operation);
return tp_channel_get_channel_type (priv->channel);
}
GQuark
empathy_dispatch_operation_get_channel_type_id (
EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), 0);
priv = GET_PRIV (operation);
return tp_channel_get_channel_type_id (priv->channel);
}
const gchar *
empathy_dispatch_operation_get_object_path (
EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), NULL);
priv = GET_PRIV (operation);
return tp_proxy_get_object_path (TP_PROXY (priv->channel));
}
EmpathyDispatchOperationState
empathy_dispatch_operation_get_status (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation),
EMPATHY_DISPATCHER_OPERATION_STATE_PREPARING);
priv = GET_PRIV (operation);
return priv->status;
}
gboolean
empathy_dispatch_operation_is_incoming (EmpathyDispatchOperation *operation)
{
EmpathyDispatchOperationPriv *priv;
g_return_val_if_fail (EMPATHY_IS_DISPATCH_OPERATION (operation), FALSE);
priv = GET_PRIV (operation);
return priv->incoming;
}
|