1 /*
2 * approver
3 *
4 * Copyright © 2010 Collabora Ltd. <http://www.collabora.co.uk/>
5 *
6 * Copying and distribution of this file, with or without modification,
7 * are permitted in any medium without royalty provided the copyright
8 * notice and this notice are preserved.
9 */
10
11 #include "config.h"
12
13 #include <glib.h>
14 #include <stdio.h>
15
16 #include <telepathy-glib/telepathy-glib.h>
17
18 GMainLoop *mainloop = NULL;
19
20 static void
cdo_finished_cb(TpProxy * self,guint domain,gint code,gchar * message,gpointer user_data)21 cdo_finished_cb (TpProxy *self,
22 guint domain,
23 gint code,
24 gchar *message,
25 gpointer user_data)
26 {
27 g_print ("ChannelDispatchOperation has been invalidated\n");
28
29 g_object_unref (self);
30 g_main_loop_quit (mainloop);
31 }
32
33 static void
handle_with_cb(GObject * source,GAsyncResult * result,gpointer user_data)34 handle_with_cb (GObject *source,
35 GAsyncResult *result,
36 gpointer user_data)
37 {
38 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
39 GError *error;
40
41 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
42 {
43 g_print ("HandleWith() failed: %s\n", error->message);
44 g_error_free (error);
45 return;
46 }
47
48 g_print ("HandleWith() succeeded\n");
49 }
50
51 static void
close_cb(GObject * source,GAsyncResult * result,gpointer user_data)52 close_cb (GObject *source,
53 GAsyncResult *result,
54 gpointer user_data)
55
56 {
57 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
58 GError *error;
59
60 if (!tp_channel_dispatch_operation_close_channels_finish (cdo, result, &error))
61 {
62 g_print ("Rejecting channels failed: %s\n", error->message);
63 g_error_free (error);
64 return;
65 }
66
67 g_print ("Rejected all the things!\n");
68 }
69
70
71 static void
add_dispatch_operation_cb(TpSimpleApprover * self,TpAccount * account,TpConnection * connection,GList * channels,TpChannelDispatchOperation * cdo,TpAddDispatchOperationContext * context,gpointer user_data)72 add_dispatch_operation_cb (TpSimpleApprover *self,
73 TpAccount *account,
74 TpConnection *connection,
75 GList *channels,
76 TpChannelDispatchOperation *cdo,
77 TpAddDispatchOperationContext *context,
78 gpointer user_data)
79 {
80 GList *l;
81 GStrv possible_handlers;
82 gchar c;
83
84 g_print ("Approving this batch of channels:\n");
85
86 g_signal_connect (cdo, "invalidated", G_CALLBACK (cdo_finished_cb), NULL);
87
88 for (l = channels; l != NULL; l = g_list_next (l))
89 {
90 TpChannel *channel = l->data;
91
92 g_print ("%s channel with %s\n", tp_channel_get_channel_type (channel),
93 tp_channel_get_identifier (channel));
94 }
95
96 possible_handlers = tp_channel_dispatch_operation_get_possible_handlers (
97 cdo);
98 if (possible_handlers[0] == NULL)
99 {
100 g_print ("\nNo possible handler suggested\n");
101 }
102 else
103 {
104 guint i;
105
106 g_print ("\npossible handlers:\n");
107 for (i = 0; possible_handlers[i] != NULL; i++)
108 g_print (" %s\n", possible_handlers[i]);
109 }
110
111 g_object_ref (cdo);
112
113 tp_add_dispatch_operation_context_accept (context);
114
115 g_print ("Approve? [y/n]\n");
116
117 c = fgetc (stdin);
118
119 if (c == 'y' || c == 'Y')
120 {
121 g_print ("Approve channels\n");
122
123 tp_channel_dispatch_operation_handle_with_async (cdo, NULL,
124 handle_with_cb, NULL);
125 }
126 else if (c == 'n' || c == 'N')
127 {
128 g_print ("Reject channels\n");
129
130 tp_channel_dispatch_operation_close_channels_async (cdo, close_cb, NULL);
131 }
132 else
133 {
134 g_print ("Ignore channels\n");
135 }
136 }
137
138 int
main(int argc,char ** argv)139 main (int argc,
140 char **argv)
141 {
142 TpAccountManager *manager;
143 GError *error = NULL;
144 TpBaseClient *approver;
145
146 tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG"));
147
148 manager = tp_account_manager_dup ();
149 approver = tp_simple_approver_new_with_am (manager, "ExampleApprover",
150 FALSE, add_dispatch_operation_cb, NULL, NULL);
151
152 /* contact text chat */
153 tp_base_client_take_approver_filter (approver, tp_asv_new (
154 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
155 TP_IFACE_CHANNEL_TYPE_TEXT,
156 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
157 TP_HANDLE_TYPE_CONTACT,
158 NULL));
159
160 /* call */
161 tp_base_client_take_approver_filter (approver, tp_asv_new (
162 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
163 TP_IFACE_CHANNEL_TYPE_CALL,
164 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
165 TP_HANDLE_TYPE_CONTACT,
166 NULL));
167
168 /* room text chat */
169 tp_base_client_take_approver_filter (approver, tp_asv_new (
170 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
171 TP_IFACE_CHANNEL_TYPE_TEXT,
172 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
173 TP_HANDLE_TYPE_ROOM,
174 NULL));
175
176 /* file transfer */
177 tp_base_client_take_approver_filter (approver, tp_asv_new (
178 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
179 TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
180 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
181 TP_HANDLE_TYPE_CONTACT,
182 NULL));
183
184 if (!tp_base_client_register (approver, &error))
185 {
186 g_warning ("Failed to register Approver: %s\n", error->message);
187 g_error_free (error);
188 goto out;
189 }
190
191 g_print ("Start approving\n");
192
193 mainloop = g_main_loop_new (NULL, FALSE);
194 g_main_loop_run (mainloop);
195
196 if (mainloop != NULL)
197 g_main_loop_unref (mainloop);
198
199 out:
200 g_object_unref (manager);
201 g_object_unref (approver);
202
203 return 0;
204 }
205