]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-blocking-dialog.c
Beginning of contact blocking dialog
[empathy.git] / libempathy-gtk / empathy-contact-blocking-dialog.c
1 /*
2  * empathy-contact-blocking-dialog.c
3  *
4  * EmpathyContactBlockingDialog
5  *
6  * Copyright (C) 2011 Collabora Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  * Authors: Danielle Madeley <danielle.madeley@collabora.co.uk>
23  */
24
25 #include <glib/gi18n.h>
26
27 #include <libempathy/empathy-utils.h>
28
29 #include <libempathy-gtk/empathy-account-chooser.h>
30 #include <libempathy-gtk/empathy-ui-utils.h>
31
32 #include "empathy-contact-blocking-dialog.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 #define GET_PRIVATE(o) (EMPATHY_CONTACT_BLOCKING_DIALOG (o)->priv)
38 #define DECLARE_CALLBACK(func) \
39   static void func (GObject *, GAsyncResult *, gpointer);
40
41 G_DEFINE_TYPE (EmpathyContactBlockingDialog, empathy_contact_blocking_dialog,
42     GTK_TYPE_DIALOG);
43
44 struct _EmpathyContactBlockingDialogPrivate
45 {
46   GHashTable *channels; /* TpConnection* -> TpChannel* */
47   GtkListStore *blocked_contacts;
48
49   GtkWidget *account_chooser;
50 };
51
52 enum /* blocked-contacts columns */
53 {
54   COL_IDENTIFIER,
55   COL_HANDLE,
56   N_COLUMNS
57 };
58
59 static const char *
60 get_pretty_conn_name (TpConnection *conn)
61 {
62   return tp_proxy_get_object_path (conn) + strlen (TP_CONN_OBJECT_PATH_BASE);
63 }
64
65 static void
66 contact_blocking_dialog_filter_account_chooser (TpAccount *account,
67     EmpathyAccountChooserFilterResultCallback callback,
68     gpointer callback_data,
69     gpointer user_data)
70 {
71   EmpathyContactBlockingDialog *self = user_data;
72   TpConnection *conn = tp_account_get_connection (account);
73   gboolean enable;
74
75   enable =
76     conn != NULL &&
77     g_hash_table_lookup (self->priv->channels, conn) != NULL;
78
79   callback (enable, callback_data);
80 }
81
82 static void contact_blocking_dialog_inspected_handles (TpConnection *,
83     const char **, const GError *, gpointer, GObject *);
84
85 static void
86 contact_blocking_dialog_add_contacts_to_list (
87     EmpathyContactBlockingDialog *self,
88     TpConnection *conn,
89     GArray *handles)
90 {
91   if (handles->len > 0)
92     tp_cli_connection_call_inspect_handles (conn, -1,
93         TP_HANDLE_TYPE_CONTACT, handles,
94         contact_blocking_dialog_inspected_handles,
95         g_boxed_copy (DBUS_TYPE_G_UINT_ARRAY, handles),
96         (GDestroyNotify) g_array_unref, G_OBJECT (self));
97 }
98
99 static void
100 contact_blocking_dialog_inspected_handles (TpConnection *conn,
101     const char **identifiers,
102     const GError *in_error,
103     gpointer user_data,
104     GObject *self)
105 {
106   EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
107   GArray *handles = user_data;
108   guint i;
109
110   if (in_error != NULL)
111     {
112       DEBUG ("Failed to inspect handles: %s", in_error->message);
113       return;
114     }
115
116   DEBUG ("Adding %u identifiers", handles->len);
117
118   for (i = 0; i < handles->len; i++)
119     {
120       const char *identifier = identifiers[i];
121       TpHandle handle = g_array_index (handles, TpHandle, i);
122
123       gtk_list_store_insert_with_values (priv->blocked_contacts, NULL, -1,
124           COL_IDENTIFIER, identifier,
125           COL_HANDLE, handle,
126           -1);
127     }
128 }
129
130 DECLARE_CALLBACK (contact_blocking_dialog_connection_prepared);
131
132 static void
133 contact_blocking_dialog_connection_status_changed (TpAccount *account,
134     guint old_status,
135     guint new_status,
136     guint reason,
137     const char *dbus_reason,
138     GHashTable *details,
139     EmpathyContactBlockingDialog *self)
140 {
141   TpConnection *conn = tp_account_get_connection (account);
142
143   switch (new_status)
144     {
145       case TP_CONNECTION_STATUS_DISCONNECTED:
146         DEBUG ("Connection %s invalidated", get_pretty_conn_name (conn));
147
148         /* remove the channel from the hash table */
149         g_hash_table_remove (self->priv->channels, conn);
150
151         /* set the filter again to refilter the account list */
152         empathy_account_chooser_set_filter (
153             EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
154             contact_blocking_dialog_filter_account_chooser, self);
155         break;
156
157       case TP_CONNECTION_STATUS_CONNECTING:
158         break;
159
160       case TP_CONNECTION_STATUS_CONNECTED:
161         DEBUG ("Connection %s reconnected", get_pretty_conn_name (conn));
162
163         tp_proxy_prepare_async (conn, NULL,
164             contact_blocking_dialog_connection_prepared, self);
165     }
166 }
167
168 static void
169 contact_blocking_dialog_deny_channel_members_changed (TpChannel *channel,
170     const char *message,
171     GArray *added,
172     GArray *removed,
173     GArray *local_pending,
174     GArray *remote_pending,
175     TpHandle actor,
176     guint reason,
177     EmpathyContactBlockingDialog *self)
178 {
179   TpConnection *conn = tp_channel_borrow_connection (channel);
180   GtkTreeModel *model = GTK_TREE_MODEL (self->priv->blocked_contacts);
181   GtkTreeIter iter;
182   TpIntset *removed_set;
183   gboolean valid;
184
185   /* we only care about changes to the selected connection */
186   /* FIXME: can we compare proxy pointers directly? */
187   if (tp_strdiff (
188         tp_proxy_get_object_path (tp_channel_borrow_connection (channel)),
189         tp_proxy_get_object_path (empathy_account_chooser_get_connection (
190             EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser)))))
191     return;
192
193   DEBUG ("deny list changed: %u added, %u removed", added->len, removed->len);
194
195   /* add contacts */
196   contact_blocking_dialog_add_contacts_to_list (self, conn, added);
197
198   /* remove contacts */
199   removed_set = tp_intset_from_array (removed);
200
201   valid = gtk_tree_model_get_iter_first (model, &iter);
202   while (valid)
203     {
204       TpHandle handle;
205
206       gtk_tree_model_get (model, &iter,
207           COL_HANDLE, &handle,
208           -1);
209
210       if (tp_intset_is_member (removed_set, handle))
211         valid = gtk_list_store_remove (self->priv->blocked_contacts, &iter);
212       else
213         valid = gtk_tree_model_iter_next (model, &iter);
214     }
215
216   tp_intset_destroy (removed_set);
217 }
218
219 DECLARE_CALLBACK (contact_blocking_dialog_account_prepared);
220
221 static void
222 contact_blocking_dialog_am_prepared (GObject *am,
223     GAsyncResult *result,
224     gpointer user_data)
225 {
226   EmpathyContactBlockingDialog *self = user_data;
227   GList *accounts, *ptr;
228   GError *error = NULL;
229
230   if (!tp_proxy_prepare_finish (am, result, &error))
231     {
232       g_critical ("Could not prepare Account Manager: %s", error->message);
233       g_error_free (error);
234       return;
235     }
236
237   accounts = tp_account_manager_get_valid_accounts (TP_ACCOUNT_MANAGER (am));
238
239   for (ptr = accounts; ptr != NULL; ptr = ptr->next)
240     {
241       TpAccount *account = ptr->data;
242
243       tp_proxy_prepare_async (account, NULL,
244           contact_blocking_dialog_account_prepared, self);
245     }
246
247   g_list_free (accounts);
248 }
249
250 static void
251 contact_blocking_dialog_account_prepared (GObject *account,
252     GAsyncResult *result,
253     gpointer user_data)
254 {
255   EmpathyContactBlockingDialog *self = user_data;
256   TpConnection *conn;
257   GError *error = NULL;
258
259   if (!tp_proxy_prepare_finish (account, result, &error))
260     {
261       DEBUG ("Could not prepare Account: %s", error->message);
262       g_error_free (error);
263       return;
264     }
265
266   g_signal_connect (account, "status-changed",
267       G_CALLBACK (contact_blocking_dialog_connection_status_changed), self);
268
269   conn = tp_account_get_connection (TP_ACCOUNT (account));
270
271   if (conn != NULL)
272     {
273       tp_proxy_prepare_async (conn, NULL,
274           contact_blocking_dialog_connection_prepared, self);
275     }
276 }
277
278 static void contact_blocking_dialog_got_deny_channel (TpConnection *,
279     gboolean, const char *, GHashTable *, const GError *, gpointer, GObject *);
280
281 static void
282 contact_blocking_dialog_connection_prepared (GObject *conn,
283     GAsyncResult *result,
284     gpointer user_data)
285 {
286   EmpathyContactBlockingDialog *self = user_data;
287   GHashTable *request;
288   GError *error = NULL;
289
290   if (!tp_proxy_prepare_finish (conn, result, &error))
291     {
292       DEBUG ("Failed to prepare connection: %s", error->message);
293       g_error_free (error);
294       return;
295     }
296
297   /* request the deny channel */
298   request = tp_asv_new (
299       TP_PROP_CHANNEL_CHANNEL_TYPE,
300       G_TYPE_STRING,
301       TP_IFACE_CHANNEL_TYPE_CONTACT_LIST,
302
303       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
304       G_TYPE_UINT,
305       TP_HANDLE_TYPE_LIST,
306
307       TP_PROP_CHANNEL_TARGET_ID,
308       G_TYPE_STRING,
309       "deny",
310
311       NULL);
312
313   tp_cli_connection_interface_requests_call_ensure_channel (
314       TP_CONNECTION (conn), -1, request,
315       contact_blocking_dialog_got_deny_channel, NULL, NULL, G_OBJECT (self));
316
317   g_hash_table_destroy (request);
318 }
319
320 DECLARE_CALLBACK (contact_blocking_dialog_deny_channel_prepared);
321
322 static void
323 contact_blocking_dialog_got_deny_channel (TpConnection *conn,
324     gboolean yours,
325     const char *channel_path,
326     GHashTable *props,
327     const GError *in_error,
328     gpointer user_data,
329     GObject *self)
330 {
331   TpChannel *channel;
332   GError *error = NULL;
333
334   const GQuark features[] = {
335       TP_CHANNEL_FEATURE_CORE,
336       TP_CHANNEL_FEATURE_GROUP,
337       0 };
338
339   if (in_error != NULL)
340     {
341       DEBUG ("Failed to get 'deny' channel: %s", in_error->message);
342       return;
343     }
344
345   channel = tp_channel_new_from_properties (conn, channel_path, props, &error);
346
347   if (error != NULL)
348     {
349       DEBUG ("Failed to create channel proxy: %s", error->message);
350       g_error_free (error);
351       return;
352     }
353
354   tp_proxy_prepare_async (channel, features,
355       contact_blocking_dialog_deny_channel_prepared, self);
356 }
357
358 static void
359 contact_blocking_dialog_deny_channel_prepared (GObject *channel,
360     GAsyncResult *result,
361     gpointer user_data)
362 {
363   EmpathyContactBlockingDialog *self = user_data;
364   TpConnection *conn;
365   GError *error = NULL;
366
367   if (!tp_proxy_prepare_finish (channel, result, &error))
368     {
369       DEBUG ("Failed to prepare channel: %s", error->message);
370       g_error_free (error);
371       return;
372     }
373
374   conn = tp_channel_borrow_connection (TP_CHANNEL (channel));
375
376   DEBUG ("Channel prepared for connection %s", get_pretty_conn_name (conn));
377
378   g_hash_table_insert (self->priv->channels,
379       g_object_ref (conn), channel);
380
381   /* set the filter again to refilter the account list */
382   empathy_account_chooser_set_filter (
383       EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
384       contact_blocking_dialog_filter_account_chooser, self);
385
386   g_signal_connect (channel, "group-members-changed",
387       G_CALLBACK (contact_blocking_dialog_deny_channel_members_changed), self);
388 }
389
390 static void
391 contact_blocking_dialog_account_changed (GtkWidget *account_chooser,
392     EmpathyContactBlockingDialog *self)
393 {
394   TpConnection *conn = empathy_account_chooser_get_connection (
395       EMPATHY_ACCOUNT_CHOOSER (account_chooser));
396   TpChannel *channel;
397   GArray *blocked;
398
399   if (conn == NULL)
400     return;
401
402   DEBUG ("Account changed: %s", get_pretty_conn_name (conn));
403
404   /* FIXME: clear the completion, get the new blocked list */
405
406   /* clear the list of blocked contacts */
407   gtk_list_store_clear (self->priv->blocked_contacts);
408
409   /* load the deny list */
410   channel = g_hash_table_lookup (self->priv->channels, conn);
411
412   g_return_if_fail (TP_IS_CHANNEL (channel));
413
414   blocked = tp_intset_to_array (tp_channel_group_get_members (channel));
415
416   DEBUG ("%u contacts on blocked list", blocked->len);
417
418   contact_blocking_dialog_add_contacts_to_list (self, conn, blocked);
419
420   g_array_unref (blocked);
421 }
422
423 static void
424 contact_blocking_dialog_dispose (GObject *self)
425 {
426   EmpathyContactBlockingDialogPrivate *priv = GET_PRIVATE (self);
427
428   tp_clear_pointer (&priv->channels, g_hash_table_destroy);
429 }
430
431 static void
432 empathy_contact_blocking_dialog_class_init (
433     EmpathyContactBlockingDialogClass *klass)
434 {
435   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
436
437   gobject_class->dispose = contact_blocking_dialog_dispose;
438
439   g_type_class_add_private (gobject_class,
440       sizeof (EmpathyContactBlockingDialogPrivate));
441 }
442
443 static void
444 empathy_contact_blocking_dialog_init (EmpathyContactBlockingDialog *self)
445 {
446   GtkBuilder *gui;
447   char *filename;
448   GtkWidget *contents;
449   GtkWidget *account_hbox, *add_contact_entry;
450   TpAccountManager *am;
451
452   self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
453       EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
454       EmpathyContactBlockingDialogPrivate);
455
456   self->priv->channels = g_hash_table_new_full (NULL, NULL,
457       g_object_unref, g_object_unref);
458
459   gtk_window_set_title (GTK_WINDOW (self), _("Edit Blocked Contacts"));
460   gtk_dialog_add_button (GTK_DIALOG (self),
461       GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE);
462
463   filename = empathy_file_lookup ("empathy-contact-blocking-dialog.ui",
464       "libempathy-gtk");
465
466   gui = empathy_builder_get_file (filename,
467       "contents", &contents,
468       "account-hbox", &account_hbox,
469       "add-contact-entry", &add_contact_entry,
470       "blocked-contacts", &self->priv->blocked_contacts,
471       NULL);
472
473   /* add the contents to the dialog */
474   gtk_container_add (
475       GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (self))),
476       contents);
477   gtk_widget_show (contents);
478
479   /* add the account chooser */
480   self->priv->account_chooser = empathy_account_chooser_new ();
481   empathy_account_chooser_set_filter (
482       EMPATHY_ACCOUNT_CHOOSER (self->priv->account_chooser),
483       contact_blocking_dialog_filter_account_chooser, self);
484   g_signal_connect (self->priv->account_chooser, "changed",
485       G_CALLBACK (contact_blocking_dialog_account_changed), self);
486
487   gtk_box_pack_start (GTK_BOX (account_hbox), self->priv->account_chooser,
488       TRUE, TRUE, 0);
489   gtk_widget_show (self->priv->account_chooser);
490
491   /* build the contact entry */
492   // FIXME
493
494   /* prepare the account manager */
495   am = tp_account_manager_dup ();
496   tp_proxy_prepare_async (am, NULL, contact_blocking_dialog_am_prepared, self);
497
498   g_free (filename);
499   g_object_unref (gui);
500 }
501
502 GtkWidget *
503 empathy_contact_blocking_dialog_new (GtkWindow *parent)
504 {
505   GtkWidget *self = g_object_new (EMPATHY_TYPE_CONTACT_BLOCKING_DIALOG,
506       NULL);
507
508   if (parent != NULL)
509     {
510       gtk_window_set_transient_for (GTK_WINDOW (self), parent);
511     }
512
513   return self;
514 }