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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
|
/*
* object used to request a channel from a TpAccount
*
* 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: account-channel-request
* @title: TpAccountChannelRequest
* @short_description: Object used to request a channel from a #TpAccount
*
* A #TpAccountChannelRequest object is used to request a channel using the
* ChannelDispatcher. Once created, use one of the create or ensure async
* method to actually request the channel.
*
* Note that each #TpAccountChannelRequest object can only be used to create
* one channel. You can't call a create or ensure method more than once on the
* same #TpAccountChannelRequest.
*
* Once the channel has been created you can use the
* TpAccountChannelRequest::re-handled: signal to be notified when the channel
* has to be re-handled. This can be useful for example to move its window
* to the foreground, if applicable.
*
* Since: 0.11.12
*/
/**
* TpAccountChannelRequest:
*
* Data structure representing a #TpAccountChannelRequest object.
*
* Since: 0.11.12
*/
/**
* TpAccountChannelRequestClass:
*
* The class of a #TpAccountChannelRequest.
*
* Since: 0.11.12
*/
#include "telepathy-glib/account-channel-request.h"
#include <telepathy-glib/channel-dispatcher.h>
#include <telepathy-glib/channel-request.h>
#include <telepathy-glib/channel.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/simple-handler.h>
#include <telepathy-glib/util.h>
#define DEBUG_FLAG TP_DEBUG_CLIENT
#include "telepathy-glib/debug-internal.h"
#include "telepathy-glib/_gen/signals-marshal.h"
struct _TpAccountChannelRequestClass {
/*<private>*/
GObjectClass parent_class;
};
struct _TpAccountChannelRequest {
/*<private>*/
GObject parent;
TpAccountChannelRequestPrivate *priv;
};
G_DEFINE_TYPE(TpAccountChannelRequest,
tp_account_channel_request, G_TYPE_OBJECT)
enum {
PROP_ACCOUNT = 1,
PROP_REQUEST,
PROP_USER_ACTION_TIME,
N_PROPS
};
enum {
SIGNAL_RE_HANDLED,
N_SIGNALS
};
static guint signals[N_SIGNALS] = { 0 };
struct _TpAccountChannelRequestPrivate
{
TpAccount *account;
GHashTable *request;
gint64 user_action_time;
TpBaseClient *handler;
gboolean ensure;
GCancellable *cancellable;
GSimpleAsyncResult *result;
TpChannelRequest *chan_request;
gulong invalidated_sig;
gulong cancel_id;
TpChannel *channel;
TpHandleChannelsContext *handle_context;
TpDBusDaemon *dbus;
/* TRUE if the channel has been requested (an _async function has been called
* on the TpAccountChannelRequest) */
gboolean requested;
/* TRUE if The TpAccountChannelRequest should handle the requested channel
* itself */
gboolean should_handle;
};
static void
tp_account_channel_request_init (TpAccountChannelRequest *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
TP_TYPE_ACCOUNT_CHANNEL_REQUEST,
TpAccountChannelRequestPrivate);
}
static void
request_disconnect (TpAccountChannelRequest *self)
{
if (self->priv->invalidated_sig == 0)
return;
g_assert (self->priv->chan_request != NULL);
g_signal_handler_disconnect (self->priv->chan_request,
self->priv->invalidated_sig);
self->priv->invalidated_sig = 0;
}
static void
tp_account_channel_request_dispose (GObject *object)
{
TpAccountChannelRequest *self = TP_ACCOUNT_CHANNEL_REQUEST (
object);
void (*dispose) (GObject *) =
G_OBJECT_CLASS (tp_account_channel_request_parent_class)->dispose;
request_disconnect (self);
if (self->priv->cancel_id != 0)
g_cancellable_disconnect (self->priv->cancellable, self->priv->cancel_id);
tp_clear_object (&self->priv->account);
tp_clear_pointer (&self->priv->request, g_hash_table_unref);
tp_clear_object (&self->priv->handler);
tp_clear_object (&self->priv->cancellable);
tp_clear_object (&self->priv->result);
tp_clear_object (&self->priv->chan_request);
tp_clear_object (&self->priv->channel);
tp_clear_object (&self->priv->handle_context);
tp_clear_object (&self->priv->dbus);
if (dispose != NULL)
dispose (object);
}
static void
tp_account_channel_request_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
TpAccountChannelRequest *self = TP_ACCOUNT_CHANNEL_REQUEST (
object);
switch (property_id)
{
case PROP_ACCOUNT:
g_value_set_object (value, self->priv->account);
break;
case PROP_REQUEST:
g_value_set_object (value, self->priv->request);
break;
case PROP_USER_ACTION_TIME:
g_value_set_int64 (value, self->priv->user_action_time);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
tp_account_channel_request_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
TpAccountChannelRequest *self = TP_ACCOUNT_CHANNEL_REQUEST (
object);
switch (property_id)
{
case PROP_ACCOUNT:
self->priv->account = g_value_dup_object (value);
break;
case PROP_REQUEST:
self->priv->request = g_value_dup_boxed (value);
break;
case PROP_USER_ACTION_TIME:
self->priv->user_action_time = g_value_get_int64 (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
tp_account_channel_request_constructed (GObject *object)
{
TpAccountChannelRequest *self = TP_ACCOUNT_CHANNEL_REQUEST (
object);
void (*chain_up) (GObject *) =
((GObjectClass *)
tp_account_channel_request_parent_class)->constructed;
if (chain_up != NULL)
chain_up (object);
g_assert (self->priv->account != NULL);
g_assert (self->priv->request != NULL);
self->priv->dbus = g_object_ref (tp_proxy_get_dbus_daemon (
self->priv->account));
}
static void
tp_account_channel_request_class_init (
TpAccountChannelRequestClass *cls)
{
GObjectClass *object_class = G_OBJECT_CLASS (cls);
GParamSpec *param_spec;
g_type_class_add_private (cls, sizeof (TpAccountChannelRequestPrivate));
object_class->get_property = tp_account_channel_request_get_property;
object_class->set_property = tp_account_channel_request_set_property;
object_class->constructed = tp_account_channel_request_constructed;
object_class->dispose = tp_account_channel_request_dispose;
/**
* TpAccountChannelRequest:account:
*
* The #TpAccount used to request the channel.
* Read-only except during construction.
*
* This property can't be %NULL.
*
* Since: 0.11.12
*/
param_spec = g_param_spec_object ("account", "TpAccount",
"The TpAccount used to request the channel",
TP_TYPE_ACCOUNT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_ACCOUNT,
param_spec);
/**
* TpAccountChannelRequest:request:
*
* The desired D-Bus properties for the channel, represented as a
* #GHashTable where the keys are strings and the values are #GValue.
*
* This property can't be %NULL.
*
* Since: 0.11.12
*/
param_spec = g_param_spec_boxed ("request", "GHashTable",
"A dictionary containing desirable properties for the channel",
TP_HASH_TYPE_STRING_VARIANT_MAP,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_REQUEST,
param_spec);
/**
* TpAccountChannelRequest:user-action-time:
*
* The user action time that will be passed to the channel dispatcher when
* requesting the channel.
*
* This may be the time at which user action occurred, or one of the special
* values %TP_USER_ACTION_TIME_NOT_USER_ACTION or
* %TP_USER_ACTION_TIME_CURRENT_TIME.
*
* If %TP_USER_ACTION_TIME_NOT_USER_ACTION, the action doesn't involve any
* user action. Clients should avoid stealing focus when presenting the
* channel.
*
* If %TP_USER_ACTION_TIME_CURRENT_TIME, clients SHOULD behave as though the
* user action happened at the current time, e.g. a client may
* request that its window gains focus.
*
* On X11-based systems, Gdk 2.x, Clutter 1.0 etc.,
* tp_user_action_time_from_x11() can be used to convert an X11 timestamp to
* a Telepathy user action time.
*
* If the channel request succeeds, this user action time will be passed on
* to the channel's handler. If the handler is a GUI, it may use
* tp_user_action_time_should_present() to decide whether to bring its
* window to the foreground.
*
* Since: 0.11.12
*/
param_spec = g_param_spec_int64 ("user-action-time", "user action time",
"UserActionTime",
G_MININT64, G_MAXINT64, 0,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (object_class, PROP_USER_ACTION_TIME,
param_spec);
/**
* TpAccountChannelRequest::re-handled:
* @self: a #TpAccountChannelRequest
* @channel: the #TpChannel being re-handled
* @user_action_time: the time at which user action occurred, or one of the
* special values %TP_USER_ACTION_TIME_NOT_USER_ACTION or
* %TP_USER_ACTION_TIME_CURRENT_TIME; see
* #TpAccountChannelRequest:user-action-time
* @context: a #TpHandleChannelsContext representing the context of
* the HandleChannels() call.
*
* Emitted when the channel created using @self has been "re-handled".
*
* This means that a Telepathy client has made another request for a
* matching channel using an "ensure" API like
* tp_account_channel_request_ensure_channel_async(), while the channel
* still exists. Instead of creating a new channel, the channel dispatcher
* notifies the existing handler of @channel, resulting in this signal.
*
* Since: 0.11.12
*/
signals[SIGNAL_RE_HANDLED] = g_signal_new (
"re-handled", G_OBJECT_CLASS_TYPE (cls),
G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
0,
NULL, NULL,
_tp_marshal_VOID__OBJECT_INT64_OBJECT,
G_TYPE_NONE, 3, TP_TYPE_CHANNEL, G_TYPE_INT64,
TP_TYPE_HANDLE_CHANNELS_CONTEXT);
}
/**
* tp_account_channel_request_new:
* @account: a #TpAccount
* @request: (transfer none) (element-type utf8 GObject.Value): the requested
* properties of the channel
* @user_action_time: the time of the user action that caused this request,
* or one of the special values %TP_USER_ACTION_TIME_NOT_USER_ACTION or
* %TP_USER_ACTION_TIME_CURRENT_TIME
*
* Convenience function to create a new #TpAccountChannelRequest object.
*
* Returns: a new #TpAccountChannelRequest object
*
* Since: 0.11.12
*/
TpAccountChannelRequest *
tp_account_channel_request_new (
TpAccount *account,
GHashTable *request,
gint64 user_action_time)
{
g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
g_return_val_if_fail (request != NULL, NULL);
return g_object_new (TP_TYPE_ACCOUNT_CHANNEL_REQUEST,
"account", account,
"request", request,
"user-action-time", user_action_time,
NULL);
}
/**
* tp_account_channel_request_get_account:
* @self: a #TpAccountChannelRequest
*
* Return the #TpAccountChannelRequest:account construct-only property
*
* Returns: (transfer none): the value of #TpAccountChannelRequest:account
*
* Since: 0.11.12
*/
TpAccount *
tp_account_channel_request_get_account (
TpAccountChannelRequest *self)
{
return self->priv->account;
}
/**
* tp_account_channel_request_get_request:
* @self: a #TpAccountChannelRequest
*
* Return the #TpAccountChannelRequest:request construct-only property
*
* Returns: (transfer none): the value of #TpAccountChannelRequest:request
*
* Since: 0.11.12
*/
GHashTable *
tp_account_channel_request_get_request (
TpAccountChannelRequest *self)
{
return self->priv->request;
}
/**
* tp_account_channel_request_get_user_action_time:
* @self: a #TpAccountChannelRequest
*
* Return the #TpAccountChannelRequest:user-action-time construct-only property
*
* Returns: the value of #TpAccountChannelRequest:user-action-time
*
* Since: 0.11.12
*/
gint64
tp_account_channel_request_get_user_action_time (
TpAccountChannelRequest *self)
{
return self->priv->user_action_time;
}
static void
complete_result (TpAccountChannelRequest *self)
{
g_assert (self->priv->result != NULL);
request_disconnect (self);
g_simple_async_result_complete (self->priv->result);
tp_clear_object (&self->priv->result);
}
static void
request_fail (TpAccountChannelRequest *self,
const GError *error)
{
g_simple_async_result_set_from_error (self->priv->result, error);
complete_result (self);
}
static void
handle_request_complete (TpAccountChannelRequest *self,
TpChannel *channel,
TpHandleChannelsContext *handle_context)
{
self->priv->channel = g_object_ref (channel);
self->priv->handle_context = g_object_ref (handle_context);
complete_result (self);
}
static void
acr_channel_invalidated_cb (TpProxy *chan,
guint domain,
gint code,
gchar *message,
TpAccountChannelRequest *self)
{
/* Channel has been destroyed, we can remove the Handler */
DEBUG ("Channel has been invalidated (%s), unref ourself", message);
g_object_unref (self);
}
static void
handle_channels (TpSimpleHandler *handler,
TpAccount *account,
TpConnection *connection,
GList *channels,
GList *requests_satisfied,
gint64 user_action_time,
TpHandleChannelsContext *context,
gpointer user_data)
{
TpAccountChannelRequest *self = user_data;
TpChannel *channel;
if (G_UNLIKELY (g_list_length (channels) != 1))
{
GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
"We are supposed to handle only one channel" };
tp_handle_channels_context_fail (context, &error);
request_fail (self, &error);
return;
}
if (self->priv->result == NULL)
{
/* We are re-handling the channel, no async request to complete */
g_signal_emit (self, signals[SIGNAL_RE_HANDLED], 0, self->priv->channel,
user_action_time, context);
goto out;
}
/* Request succeeded */
channel = channels->data;
if (tp_proxy_get_invalidated (channel) == NULL)
{
/* Keep the handler alive while the channel is valid so keep a ref on
* ourself until the channel is invalidated */
g_object_ref (self);
g_signal_connect (channel, "invalidated",
G_CALLBACK (acr_channel_invalidated_cb), self);
}
handle_request_complete (self, channel, context);
out:
tp_handle_channels_context_accept (context);
}
static void
channel_request_succeeded (TpAccountChannelRequest *self)
{
if (self->priv->should_handle)
{
GError err = { TP_ERRORS, TP_ERROR_NOT_YOURS,
"Another Handler is handling this channel" };
if (self->priv->result == NULL)
/* Our handler has been called, all good */
return;
/* Our handler hasn't be called but the channel request is complete.
* That means another handler handled the channels so we don't own it. */
request_fail (self, &err);
}
else
{
/* We don't have to handle the channel so we're done */
complete_result (self);
}
}
static void
acr_channel_request_proceed_cb (TpChannelRequest *request,
const GError *error,
gpointer user_data,
GObject *weak_object)
{
TpAccountChannelRequest *self = user_data;
if (error != NULL)
{
DEBUG ("Proceed failed: %s", error->message);
request_fail (self, error);
return;
}
if (self->priv->should_handle)
DEBUG ("Proceed succeeded; waiting for the channel to be handled");
else
DEBUG ("Proceed succeeded; waiting for the Succeeded signal");
}
static void
acr_channel_request_invalidated_cb (TpProxy *proxy,
guint domain,
gint code,
gchar *message,
TpAccountChannelRequest *self)
{
GError error = { domain, code, message };
if (g_error_matches (&error, TP_DBUS_ERRORS, TP_DBUS_ERROR_OBJECT_REMOVED))
{
/* Object has been removed without error, so ChannelRequest succeeded */
channel_request_succeeded (self);
return;
}
DEBUG ("ChannelRequest has been invalidated: %s", message);
request_fail (self, &error);
}
static void
acr_channel_request_cancel_cb (TpChannelRequest *request,
const GError *error,
gpointer user_data,
GObject *weak_object)
{
/* Don't do anything, we rely on the invalidation of the channel request to
* complete the operation */
if (error != NULL)
{
DEBUG ("ChannelRequest.Cancel() failed: %s", error->message);
return;
}
DEBUG ("ChannelRequest.Cancel() succeeded");
}
static void
acr_operation_cancelled_cb (GCancellable *cancellable,
TpAccountChannelRequest *self)
{
if (self->priv->chan_request == NULL)
{
DEBUG ("ChannelRequest has been invalidated, we can't cancel any more");
return;
}
DEBUG ("Operation has been cancelled, cancel the channel request");
tp_cli_channel_request_call_cancel (self->priv->chan_request, -1,
acr_channel_request_cancel_cb, self, NULL, G_OBJECT (self));
}
static void
acr_request_cb (TpChannelDispatcher *cd,
const gchar *channel_request_path,
const GError *error,
gpointer user_data,
GObject *weak_object)
{
TpAccountChannelRequest *self = user_data;
GError *err = NULL;
if (error != NULL)
{
DEBUG ("%s failed: %s",
self->priv->ensure ? "EnsureChannel" : "CreateChannel",
error->message);
request_fail (self, error);
return;
}
DEBUG ("Got ChannelRequest: %s", channel_request_path);
self->priv->chan_request = tp_channel_request_new (
self->priv->dbus, channel_request_path, NULL, &err);
if (self->priv->chan_request == NULL)
{
DEBUG ("Failed to create ChannelRequest: %s", err->message);
goto fail;
}
self->priv->invalidated_sig = g_signal_connect (self->priv->chan_request,
"invalidated", G_CALLBACK (acr_channel_request_invalidated_cb), self);
if (self->priv->cancellable != NULL)
{
self->priv->cancel_id = g_cancellable_connect (self->priv->cancellable,
G_CALLBACK (acr_operation_cancelled_cb), self, NULL);
/* We just aborted the operation so we're done */
if (g_cancellable_is_cancelled (self->priv->cancellable))
return;
}
DEBUG ("Calling ChannelRequest.Proceed()");
tp_cli_channel_request_call_proceed (self->priv->chan_request, -1,
acr_channel_request_proceed_cb, self, NULL, G_OBJECT (self));
return;
fail:
request_fail (self, err);
g_error_free (err);
}
static void
request_and_handle_channel_async (TpAccountChannelRequest *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data,
gboolean ensure)
{
GError *error = NULL;
TpChannelDispatcher *cd;
g_return_if_fail (!self->priv->requested);
self->priv->requested = TRUE;
self->priv->should_handle = TRUE;
if (g_cancellable_is_cancelled (cancellable))
{
g_simple_async_report_error_in_idle (G_OBJECT (self), callback,
user_data, G_IO_ERROR, G_IO_ERROR_CANCELLED,
"Operation has been cancelled");
return;
}
if (cancellable != NULL)
self->priv->cancellable = g_object_ref (cancellable);
self->priv->ensure = ensure;
/* Create a temp handler */
self->priv->handler = tp_simple_handler_new (self->priv->dbus, TRUE, FALSE,
"TpGLibRequestAndHandle", TRUE, handle_channels, self, NULL);
if (!tp_base_client_register (self->priv->handler, &error))
{
DEBUG ("Failed to register temp handler: %s", error->message);
g_simple_async_report_gerror_in_idle (G_OBJECT (self), callback,
user_data, error);
g_error_free (error);
return;
}
cd = tp_channel_dispatcher_new (self->priv->dbus);
if (ensure)
{
self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
user_data,
tp_account_channel_request_ensure_and_handle_channel_async);
tp_cli_channel_dispatcher_call_ensure_channel (cd, -1,
tp_proxy_get_object_path (self->priv->account), self->priv->request,
self->priv->user_action_time,
tp_base_client_get_bus_name (self->priv->handler),
acr_request_cb, self, NULL, G_OBJECT (self));
}
else
{
self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
user_data,
tp_account_channel_request_create_and_handle_channel_async);
tp_cli_channel_dispatcher_call_create_channel (cd, -1,
tp_proxy_get_object_path (self->priv->account), self->priv->request,
self->priv->user_action_time,
tp_base_client_get_bus_name (self->priv->handler),
acr_request_cb, self, NULL, G_OBJECT (self));
}
g_object_unref (cd);
}
static TpChannel *
request_and_handle_channel_finish (TpAccountChannelRequest *self,
GAsyncResult *result,
TpHandleChannelsContext **context,
gpointer source_tag,
GError **error)
{
GSimpleAsyncResult *simple;
g_return_val_if_fail (TP_IS_ACCOUNT_CHANNEL_REQUEST (self), NULL);
g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
g_return_val_if_fail (g_simple_async_result_is_valid (result,
G_OBJECT (self), source_tag),
NULL);
if (context != NULL)
*context = g_object_ref (self->priv->handle_context);
return g_object_ref (self->priv->channel);
}
/**
* tp_account_channel_request_create_and_handle_channel_async:
* @self: a #TpAccountChannelRequest
* @cancellable: optional #GCancellable object, %NULL to ignore
* @callback: a callback to call when the request is satisfied
* @user_data: data to pass to @callback
*
* Asynchronously calls CreateChannel on the ChannelDispatcher to create a
* channel with the properties defined in #TpAccountChannelRequest:request
* that you are going to handle yourself.
* When the operation is finished, @callback will be called. You can then call
* tp_account_channel_request_create_and_handle_channel_finish() to get the
* result of the operation.
*
* Since: 0.11.12
*/
void
tp_account_channel_request_create_and_handle_channel_async (
TpAccountChannelRequest *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
request_and_handle_channel_async (self, cancellable, callback, user_data,
FALSE);
}
/**
* tp_account_channel_request_create_and_handle_channel_finish:
* @self: a #TpAccountChannelRequest
* @result: a #GAsyncResult
* @context: (out) (allow-none) (transfer full): pointer used to return a
* on the context of the HandleChannels() call, or %NULL
* @error: a #GError to fill
*
* Finishes an async channel creation started using
* tp_account_channel_request_create_and_handle_channel_async().
*
* Returns: (transfer full) (allow-none): a new reference on a #TpChannel if the
* channel was successfully created and you are handling it, otherwise %NULL.
*
* Since: 0.11.12
*/
TpChannel *
tp_account_channel_request_create_and_handle_channel_finish (
TpAccountChannelRequest *self,
GAsyncResult *result,
TpHandleChannelsContext **context,
GError **error)
{
return request_and_handle_channel_finish (self, result, context,
tp_account_channel_request_create_and_handle_channel_async, error);
}
/**
* tp_account_channel_request_ensure_and_handle_channel_async:
* @self: a #TpAccountChannelRequest
* @cancellable: optional #GCancellable object, %NULL to ignore
* @callback: a callback to call when the request is satisfied
* @user_data: data to pass to @callback
*
* Asynchronously calls EnsureChannel on the ChannelDispatcher to create a
* channel with the properties defined in #TpAccountChannelRequest:request
* that you are going to handle yourself.
* When the operation is finished, @callback will be called. You can then call
* tp_account_channel_request_ensure_and_handle_channel_finish() to get the
* result of the operation.
*
* If the channel already exists and is already being handled, or if a
* newly created channel is sent to a different handler, this operation
* will fail with the error %TP_ERROR_NOT_YOURS. The other handler
* will be notified that the channel was requested again (for instance
* with #TpAccountChannelRequest::re-handled,
* #TpBaseClientClassHandleChannelsImpl or #TpSimpleHandler:callback),
* and can move its window to the foreground, if applicable.
*
* Since: 0.11.12
*/
void
tp_account_channel_request_ensure_and_handle_channel_async (
TpAccountChannelRequest *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
request_and_handle_channel_async (self, cancellable, callback, user_data,
TRUE);
}
/**
* tp_account_channel_request_ensure_and_handle_channel_finish:
* @self: a #TpAccountChannelRequest
* @result: a #GAsyncResult
* @context: (out) (allow-none) (transfer full): pointer used to return a
* on the context of the HandleChannels() call, or %NULL
* @error: a #GError to fill
*
* Finishes an async channel creation started using
* tp_account_channel_request_ensure_and_handle_channel_async().
*
* If the channel already exists and is already being handled, or if a
* newly created channel is sent to a different handler, this operation
* will fail with the error %TP_ERROR_NOT_YOURS.
*
* Returns: (transfer full) (allow-none): a new reference on a #TpChannel if the
* channel was successfully created and you are handling it, otherwise %NULL.
*
* Since: 0.11.12
*/
TpChannel *
tp_account_channel_request_ensure_and_handle_channel_finish (
TpAccountChannelRequest *self,
GAsyncResult *result,
TpHandleChannelsContext **context,
GError **error)
{
return request_and_handle_channel_finish (self, result, context,
tp_account_channel_request_ensure_and_handle_channel_async, error);
}
/* Request and forget API */
static void
request_channel_async (TpAccountChannelRequest *self,
const gchar *preferred_handler,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data,
gboolean ensure)
{
TpChannelDispatcher *cd;
g_return_if_fail (!self->priv->requested);
self->priv->requested = TRUE;
self->priv->should_handle = FALSE;
if (g_cancellable_is_cancelled (cancellable))
{
g_simple_async_report_error_in_idle (G_OBJECT (self), callback,
user_data, G_IO_ERROR, G_IO_ERROR_CANCELLED,
"Operation has been cancelled");
return;
}
if (cancellable != NULL)
self->priv->cancellable = g_object_ref (cancellable);
self->priv->ensure = ensure;
cd = tp_channel_dispatcher_new (self->priv->dbus);
if (ensure)
{
self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
user_data, tp_account_channel_request_ensure_channel_async);
tp_cli_channel_dispatcher_call_ensure_channel (cd, -1,
tp_proxy_get_object_path (self->priv->account), self->priv->request,
self->priv->user_action_time,
preferred_handler == NULL ? "" : preferred_handler,
acr_request_cb, self, NULL, G_OBJECT (self));
}
else
{
self->priv->result = g_simple_async_result_new (G_OBJECT (self), callback,
user_data, tp_account_channel_request_create_channel_async);
tp_cli_channel_dispatcher_call_create_channel (cd, -1,
tp_proxy_get_object_path (self->priv->account), self->priv->request,
self->priv->user_action_time,
preferred_handler == NULL ? "" : preferred_handler,
acr_request_cb, self, NULL, G_OBJECT (self));
}
g_object_unref (cd);
}
/**
* tp_account_channel_request_create_channel_async:
* @self: a #TpAccountChannelRequest
* @preferred_handler: Either the well-known bus name (starting with
* %TP_CLIENT_BUS_NAME_BASE) of the preferred handler for the channel,
* or %NULL to indicate that any handler would be acceptable.
* @cancellable: optional #GCancellable object, %NULL to ignore
* @callback: a callback to call when the request is satisfied
* @user_data: data to pass to @callback
*
* Asynchronously calls CreateChannel on the ChannelDispatcher to create a
* channel with the properties defined in #TpAccountChannelRequest:request
* and let the ChannelDispatcher dispatch it to an handler.
* @callback will be called when the channel has been created and dispatched,
* or the request has failed.
* You can then call tp_account_channel_request_create_channel_finish() to
* get the result of the operation.
*
* Since: 0.11.12
*/
void
tp_account_channel_request_create_channel_async (
TpAccountChannelRequest *self,
const gchar *preferred_handler,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
request_channel_async (self, preferred_handler, cancellable, callback,
user_data, FALSE);
}
static gboolean
request_channel_finish (TpAccountChannelRequest *self,
GAsyncResult *result,
gpointer source_tag,
GError **error)
{
GSimpleAsyncResult *simple;
g_return_val_if_fail (TP_IS_ACCOUNT_CHANNEL_REQUEST (self), FALSE);
g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
simple = G_SIMPLE_ASYNC_RESULT (result);
if (g_simple_async_result_propagate_error (simple, error))
return FALSE;
g_return_val_if_fail (g_simple_async_result_is_valid (result,
G_OBJECT (self), source_tag),
FALSE);
return TRUE;
}
/**
* tp_account_channel_request_create_channel_finish:
* @self: a #TpAccountChannelRequest
* @result: a #GAsyncResult
* @error: a #GError to fill
*
* Finishes an async channel creation started using
* tp_account_channel_request_create_channel_async().
*
* Returns: %TRUE if the channel was successfully created and dispatched,
* otherwise %FALSE.
*
* Since: 0.11.12
*/
gboolean
tp_account_channel_request_create_channel_finish (
TpAccountChannelRequest *self,
GAsyncResult *result,
GError **error)
{
return request_channel_finish (self, result,
tp_account_channel_request_create_channel_async, error);
}
/**
* tp_account_channel_request_ensure_channel_async:
* @self: a #TpAccountChannelRequest
* @preferred_handler: Either the well-known bus name (starting with
* %TP_CLIENT_BUS_NAME_BASE) of the preferred handler for the channel,
* or %NULL to indicate that any handler would be acceptable.
* @cancellable: optional #GCancellable object, %NULL to ignore
* @callback: a callback to call when the request is satisfied
* @user_data: data to pass to @callback
*
* Asynchronously calls EnsureChannel on the ChannelDispatcher to create a
* channel with the properties defined in #TpAccountChannelRequest:request
* and let the ChannelDispatcher dispatch it to an handler.
* @callback will be called when an existing channel's handler has been
* notified, a new channel has been created and dispatched, or the request
* has failed.
* You can then call tp_account_channel_request_ensure_channel_finish() to
* get the result of the operation.
*
* Since: 0.11.12
*/
void
tp_account_channel_request_ensure_channel_async (
TpAccountChannelRequest *self,
const gchar *preferred_handler,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
request_channel_async (self, preferred_handler, cancellable, callback,
user_data, TRUE);
}
/**
* tp_account_channel_request_ensure_channel_finish:
* @self: a #TpAccountChannelRequest
* @result: a #GAsyncResult
* @error: a #GError to fill
*
* Finishes an async channel creation started using
* tp_account_channel_request_ensure_channel_async().
*
* Returns: %TRUE if the channel was successfully ensured and (re-)dispatched,
* otherwise %FALSE.
*
* Since: 0.11.12
*/
gboolean
tp_account_channel_request_ensure_channel_finish (
TpAccountChannelRequest *self,
GAsyncResult *result,
GError **error)
{
return request_channel_finish (self, result,
tp_account_channel_request_ensure_channel_async, error);
}
|