summaryrefslogtreecommitdiff
path: root/examples/client/approver.c
blob: 3cc6f88c0d5055420d1c4d4bac1573b495be313d (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
/*
 * approver
 *
 * Copyright © 2010 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "config.h"

#include <glib.h>
#include <stdio.h>

#include <telepathy-glib/telepathy-glib.h>

GMainLoop *mainloop = NULL;

static void
cdo_finished_cb (TpProxy *self,
    guint domain,
    gint code,
    gchar *message,
    gpointer user_data)
{
  g_print ("ChannelDispatchOperation has been invalidated\n");

  g_object_unref (self);
  g_main_loop_quit (mainloop);
}

static void
handle_with_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
  GError *error;

  if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
    {
      g_print ("HandleWith() failed: %s\n", error->message);
      g_error_free (error);
      return;
    }

  g_print ("HandleWith() succeeded\n");
}

static void
close_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)

{
  TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
  GError *error;

  if (!tp_channel_dispatch_operation_close_channels_finish (cdo, result, &error))
    {
      g_print ("Rejecting channels failed: %s\n", error->message);
      g_error_free (error);
      return;
    }

  g_print ("Rejected all the things!\n");
}


static void
add_dispatch_operation_cb (TpSimpleApprover *self,
    TpAccount *account,
    TpConnection *connection,
    GList *channels,
    TpChannelDispatchOperation *cdo,
    TpAddDispatchOperationContext *context,
    gpointer user_data)
{
  GList *l;
  GStrv possible_handlers;
  gchar c;

  g_print ("Approving this batch of channels:\n");

  g_signal_connect (cdo, "invalidated", G_CALLBACK (cdo_finished_cb), NULL);

  for (l = channels; l != NULL; l = g_list_next (l))
    {
      TpChannel *channel = l->data;

      g_print ("%s channel with %s\n", tp_channel_get_channel_type (channel),
          tp_channel_get_identifier (channel));
    }

  possible_handlers = tp_channel_dispatch_operation_get_possible_handlers (
      cdo);
  if (possible_handlers[0] == NULL)
    {
      g_print ("\nNo possible handler suggested\n");
    }
  else
    {
      guint i;

      g_print ("\npossible handlers:\n");
      for (i = 0; possible_handlers[i] != NULL; i++)
        g_print ("  %s\n", possible_handlers[i]);
    }

  g_object_ref (cdo);

  tp_add_dispatch_operation_context_accept (context);

  g_print ("Approve? [y/n]\n");

  c = fgetc (stdin);

  if (c == 'y' || c == 'Y')
    {
      g_print ("Approve channels\n");

      tp_channel_dispatch_operation_handle_with_async (cdo, NULL,
          handle_with_cb, NULL);
    }
  else if (c == 'n' || c == 'N')
    {
      g_print ("Reject channels\n");

      tp_channel_dispatch_operation_close_channels_async (cdo, close_cb, NULL);
    }
  else
    {
      g_print ("Ignore channels\n");
    }
}

int
main (int argc,
      char **argv)
{
  TpAccountManager *manager;
  GError *error = NULL;
  TpBaseClient *approver;

  tp_debug_set_flags (g_getenv ("EXAMPLE_DEBUG"));

  manager = tp_account_manager_dup ();
  approver = tp_simple_approver_new_with_am (manager, "ExampleApprover",
      FALSE, add_dispatch_operation_cb, NULL, NULL);

  /* contact text chat */
  tp_base_client_take_approver_filter (approver, tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_TEXT,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_CONTACT,
        NULL));

  /* call */
  tp_base_client_take_approver_filter (approver, tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_CALL,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_CONTACT,
        NULL));

  /* room text chat */
  tp_base_client_take_approver_filter (approver, tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_TEXT,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_ROOM,
        NULL));

  /* file transfer */
  tp_base_client_take_approver_filter (approver, tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_FILE_TRANSFER,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_CONTACT,
        NULL));

  if (!tp_base_client_register (approver, &error))
    {
      g_warning ("Failed to register Approver: %s\n", error->message);
      g_error_free (error);
      goto out;
    }

  g_print ("Start approving\n");

  mainloop = g_main_loop_new (NULL, FALSE);
  g_main_loop_run (mainloop);

  if (mainloop != NULL)
    g_main_loop_unref (mainloop);

out:
  g_object_unref (manager);
  g_object_unref (approver);

  return 0;
}