~ubuntu-branches/ubuntu/maverick/telepathy-glib/maverick

« back to all changes in this revision

Viewing changes to examples/client/approver.c

  • Committer: Bazaar Package Importer
  • Author(s): Simon McVittie
  • Date: 2010-05-10 17:59:54 UTC
  • mfrom: (1.6.1 upstream) (27.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100510175954-bxvqq3xx0sy4itmp
Tags: 0.11.5-1
New upstream version with new API/ABI

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <glib.h>
 
12
#include <stdio.h>
 
13
 
 
14
#include <telepathy-glib/telepathy-glib.h>
 
15
#include <telepathy-glib/debug.h>
 
16
#include <telepathy-glib/simple-approver.h>
 
17
 
 
18
GMainLoop *mainloop = NULL;
 
19
 
 
20
static void
 
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
 
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
 
52
claim_cb (GObject *source,
 
53
    GAsyncResult *result,
 
54
    gpointer user_data)
 
55
 
 
56
{
 
57
  TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
 
58
  GError *error;
 
59
  GPtrArray *channels;
 
60
  guint i;
 
61
 
 
62
  if (!tp_channel_dispatch_operation_claim_finish (cdo, result, &error))
 
63
    {
 
64
      g_print ("Claim() failed: %s\n", error->message);
 
65
      g_error_free (error);
 
66
      return;
 
67
    }
 
68
 
 
69
  g_print ("Claim() succeeded, close channels\n");
 
70
 
 
71
  channels = tp_channel_dispatch_operation_borrow_channels (cdo);
 
72
  for (i = 0; i < channels->len; i++)
 
73
    {
 
74
      TpChannel *channel = g_ptr_array_index (channels, i);
 
75
 
 
76
      tp_cli_channel_call_close (channel, -1, NULL, NULL, NULL, NULL);
 
77
    }
 
78
}
 
79
 
 
80
 
 
81
static void
 
82
add_dispatch_operation_cb (TpSimpleApprover *self,
 
83
    TpAccount *account,
 
84
    TpConnection *connection,
 
85
    GList *channels,
 
86
    TpChannelDispatchOperation *cdo,
 
87
    TpAddDispatchOperationContext *context,
 
88
    gpointer user_data)
 
89
{
 
90
  GList *l;
 
91
  GStrv possible_handlers;
 
92
  gchar c;
 
93
 
 
94
  g_print ("Approving this batch of channels:\n");
 
95
 
 
96
  g_signal_connect (cdo, "invalidated", G_CALLBACK (cdo_finished_cb), NULL);
 
97
 
 
98
  for (l = channels; l != NULL; l = g_list_next (l))
 
99
    {
 
100
      TpChannel *channel = l->data;
 
101
 
 
102
      g_print ("%s channel with %s\n", tp_channel_get_channel_type (channel),
 
103
          tp_channel_get_identifier (channel));
 
104
    }
 
105
 
 
106
  possible_handlers = tp_channel_dispatch_operation_borrow_possible_handlers (
 
107
      cdo);
 
108
  if (possible_handlers[0] == NULL)
 
109
    {
 
110
      g_print ("\nNo possible handler suggested\n");
 
111
    }
 
112
  else
 
113
    {
 
114
      guint i;
 
115
 
 
116
      g_print ("\npossible handlers:\n");
 
117
      for (i = 0; possible_handlers[i] != NULL; i++)
 
118
        g_print ("  %s\n", possible_handlers[i]);
 
119
    }
 
120
 
 
121
  g_object_ref (cdo);
 
122
 
 
123
  tp_add_dispatch_operation_context_accept (context);
 
124
 
 
125
  g_print ("Approve? [y/n]\n");
 
126
 
 
127
  c = fgetc (stdin);
 
128
 
 
129
  if (c == 'y' || c == 'Y')
 
130
    {
 
131
      g_print ("Approve channels\n");
 
132
 
 
133
      tp_channel_dispatch_operation_handle_with_async (cdo, NULL,
 
134
          handle_with_cb, NULL);
 
135
    }
 
136
  else if (c == 'n' || c == 'N')
 
137
    {
 
138
      g_print ("Dissaprove channels\n");
 
139
 
 
140
      tp_channel_dispatch_operation_claim_async (cdo, claim_cb, NULL);
 
141
    }
 
142
  else
 
143
    {
 
144
      g_print ("Ignore channels\n");
 
145
    }
 
146
}
 
147
 
 
148
int
 
149
main (int argc,
 
150
      char **argv)
 
151
{
 
152
  TpDBusDaemon *bus_daemon;
 
153
  GError *error = NULL;
 
154
  TpBaseClient *approver;
 
155
 
 
156
  g_type_init ();
 
157
  tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG"));
 
158
 
 
159
  bus_daemon = tp_dbus_daemon_dup (&error);
 
160
 
 
161
  if (bus_daemon == NULL)
 
162
    {
 
163
      g_warning ("%s", error->message);
 
164
      g_error_free (error);
 
165
      return 1;
 
166
    }
 
167
 
 
168
  approver = tp_simple_approver_new (bus_daemon, "ExampleApprover",
 
169
      FALSE, add_dispatch_operation_cb, NULL, NULL);
 
170
 
 
171
  /* contact text chat */
 
172
  tp_base_client_take_approver_filter (approver, tp_asv_new (
 
173
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
 
174
          TP_IFACE_CHANNEL_TYPE_TEXT,
 
175
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
 
176
          TP_HANDLE_TYPE_CONTACT,
 
177
        NULL));
 
178
 
 
179
  /* call */
 
180
  tp_base_client_take_approver_filter (approver, tp_asv_new (
 
181
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
 
182
          TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
 
183
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
 
184
          TP_HANDLE_TYPE_CONTACT,
 
185
        NULL));
 
186
 
 
187
  /* room text chat */
 
188
  tp_base_client_take_approver_filter (approver, tp_asv_new (
 
189
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
 
190
          TP_IFACE_CHANNEL_TYPE_TEXT,
 
191
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
 
192
          TP_HANDLE_TYPE_ROOM,
 
193
        NULL));
 
194
 
 
195
  /* file transfer */
 
196
  tp_base_client_take_approver_filter (approver, tp_asv_new (
 
197
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
 
198
          TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
 
199
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
 
200
          TP_HANDLE_TYPE_CONTACT,
 
201
        NULL));
 
202
 
 
203
  if (!tp_base_client_register (approver, &error))
 
204
    {
 
205
      g_warning ("Failed to register Approver: %s\n", error->message);
 
206
      g_error_free (error);
 
207
      goto out;
 
208
    }
 
209
 
 
210
  g_print ("Start approving\n");
 
211
 
 
212
  mainloop = g_main_loop_new (NULL, FALSE);
 
213
  g_main_loop_run (mainloop);
 
214
 
 
215
  if (mainloop != NULL)
 
216
    g_main_loop_unref (mainloop);
 
217
 
 
218
out:
 
219
  g_object_unref (bus_daemon);
 
220
  g_object_unref (approver);
 
221
 
 
222
  return 0;
 
223
}