Bump version to 4.3-4
[LibreOffice.git] / tubes / source / approver.c
blobd8d8901e4a266490ae2e9d6b7d90def1e70f1bd4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <glib.h>
11 #include <stdio.h>
13 #include <gtk/gtk.h>
15 #include <telepathy-glib/telepathy-glib.h>
16 #include <telepathy-glib/debug.h>
17 #include <telepathy-glib/simple-approver.h>
19 #include <tubes/constants.h>
21 GMainLoop *mainloop = NULL;
23 static void
24 handle_with_cb (GObject *source,
25 GAsyncResult *result,
26 gpointer user_data)
28 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
29 GtkMessageDialog *dialog = GTK_MESSAGE_DIALOG (user_data);
30 GError *error = NULL;
32 if (!tp_channel_dispatch_operation_handle_with_finish (cdo, result, &error))
34 g_print ("HandleWith() failed: %s\n", error->message);
35 gtk_message_dialog_format_secondary_markup (dialog,
36 "<b>Error</b>\n\nAsking LibreOffice to accept the session failed: <i>%s</i>",
37 error->message);
38 g_error_free (error);
39 return;
42 g_print ("HandleWith() succeeded\n");
43 gtk_widget_destroy (GTK_WIDGET (dialog));
46 static void
47 close_cb (GObject *source,
48 GAsyncResult *result,
49 gpointer user_data)
52 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (source);
53 GError *error = NULL;
55 (void)user_data; /* suppress unused-parameter warning */
57 if (!tp_channel_dispatch_operation_close_channels_finish (cdo, result, &error))
59 g_print ("Rejecting channels failed: %s\n", error->message);
60 g_error_free (error);
61 return;
64 g_print ("Rejected all the things!\n");
67 static void
68 dialog_response_cb (
69 GtkWidget *dialog,
70 gint response_id,
71 gpointer user_data)
73 TpSimpleApprover *self = TP_SIMPLE_APPROVER (g_object_get_data (G_OBJECT (dialog), "client"));
74 TpChannelDispatchOperation *cdo = TP_CHANNEL_DISPATCH_OPERATION (user_data);
76 (void)self; /* suppress unused-parameter warning (could remove TP_SIMPLE_APPROVER above?) */
78 if (response_id == GTK_RESPONSE_ACCEPT)
80 g_print ("Approve channels\n");
82 tp_channel_dispatch_operation_handle_with_async (cdo, NULL,
83 handle_with_cb, dialog);
85 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT, FALSE);
86 gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_REJECT, FALSE);
88 else
90 g_print ("Reject channels\n");
92 tp_channel_dispatch_operation_close_channels_async (cdo, close_cb, dialog);
93 gtk_widget_destroy (dialog);
96 g_object_unref (cdo);
99 static void
100 show_dialog (
101 TpSimpleApprover *self,
102 TpChannelDispatchOperation *cdo,
103 TpContact *target)
105 GFile *avatar_file = tp_contact_get_avatar_file (target);
106 GtkWidget *dialog = gtk_message_dialog_new_with_markup (NULL,
107 0, /* flags */
108 GTK_MESSAGE_QUESTION,
109 GTK_BUTTONS_NONE,
110 "<b>%s</b> (<i>%s</i>) would like to edit a spreadsheet in LibreOffice "
111 "with you.",
112 tp_contact_get_alias (target),
113 tp_contact_get_identifier (target));
115 if (avatar_file != NULL)
117 GtkWidget *avatar = gtk_image_new_from_file (g_file_get_path (avatar_file));
119 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), avatar);
122 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
123 "_Reject", GTK_RESPONSE_REJECT,
124 "_Accept", GTK_RESPONSE_ACCEPT,
125 NULL);
127 g_object_set_data_full (G_OBJECT (dialog), "client", g_object_ref (self), g_object_unref);
128 g_signal_connect (dialog, "response", G_CALLBACK (dialog_response_cb), g_object_ref (cdo));
130 gtk_window_set_skip_taskbar_hint (GTK_WINDOW (dialog), FALSE);
132 gtk_widget_show_all (dialog);
135 static void
136 add_dispatch_operation_cb (TpSimpleApprover *self,
137 TpAccount *account,
138 TpConnection *connection,
139 GList *channels,
140 TpChannelDispatchOperation *cdo,
141 TpAddDispatchOperationContext *context,
142 gpointer user_data)
144 TpContact *target = NULL;
145 GList *l;
147 (void)account; /* suppress unused-parameter warning */
148 (void)connection; /* suppress unused-parameter warning */
149 (void)user_data; /* suppress unused-parameter warning */
151 g_print ("Approving this batch of channels:\n");
153 for (l = channels; l != NULL; l = g_list_next (l))
155 TpChannel *channel = l->data;
157 if (TP_IS_DBUS_TUBE_CHANNEL (channel))
159 target = tp_channel_get_target_contact (channel);
160 break;
164 if (target == NULL)
166 g_critical ("Hmm. No 1-1 D-Bus tube in cdo %s, so why did we get it?",
167 tp_proxy_get_object_path (cdo));
168 g_return_if_reached ();
171 tp_add_dispatch_operation_context_accept (context);
172 show_dialog (self, cdo, target);
176 main (int argc,
177 char **argv)
179 TpAccountManager *manager;
180 TpSimpleClientFactory *factory;
181 TpBaseClient *approver;
182 GError *error = NULL;
184 gtk_init (&argc, &argv);
185 tp_debug_set_flags (g_getenv ("LIBO_APPROVER_DEBUG"));
187 manager = tp_account_manager_dup ();
189 factory = tp_proxy_get_factory (manager);
190 /* We want the target contact on channels to be available... */
191 tp_simple_client_factory_add_channel_features_varargs (factory,
192 TP_CHANNEL_FEATURE_CONTACTS,
194 /* ...and for it to have its alias and avatar available */
195 tp_simple_client_factory_add_contact_features_varargs (factory,
196 TP_CONTACT_FEATURE_ALIAS,
197 TP_CONTACT_FEATURE_AVATAR_DATA,
198 TP_CONTACT_FEATURE_INVALID);
200 approver = tp_simple_approver_new_with_am (manager, "LibreOfficeApprover",
201 FALSE, add_dispatch_operation_cb, NULL, NULL);
203 tp_base_client_take_approver_filter (approver, tp_asv_new (
204 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
205 TP_IFACE_CHANNEL_TYPE_DBUS_TUBE,
206 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
207 TP_HANDLE_TYPE_CONTACT,
208 TP_PROP_CHANNEL_TYPE_DBUS_TUBE_SERVICE_NAME, G_TYPE_STRING,
209 LIBO_DTUBE_SERVICE,
210 NULL));
212 if (!tp_base_client_register (approver, &error))
214 g_warning ("Failed to register Approver: %s\n", error->message);
215 g_error_free (error);
216 goto out;
219 g_print ("Start approving\n");
221 mainloop = g_main_loop_new (NULL, FALSE);
222 g_main_loop_run (mainloop);
223 /* TODO: time out after 5 seconds of inactivity? */
225 if (mainloop != NULL)
226 g_main_loop_unref (mainloop);
228 out:
229 g_object_unref (manager);
230 g_object_unref (approver);
232 return 0;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */