]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Factor out common blocking confirmation dialog
[empathy.git] / libempathy-gtk / empathy-contact-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors: Xavier Claessens <xclaesse@gmail.com>
20  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n-lib.h>
30
31 #include <telepathy-glib/account-manager.h>
32
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-contact-list.h>
35 #include <libempathy/empathy-tp-contact-factory.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include "empathy-contact-dialogs.h"
39 #include "empathy-contact-widget.h"
40 #include "empathy-ui-utils.h"
41
42 static GList *subscription_dialogs = NULL;
43 static GList *information_dialogs = NULL;
44 static GList *edit_dialogs = NULL;
45 static GtkWidget *personal_dialog = NULL;
46 static GtkWidget *new_contact_dialog = NULL;
47
48 static gint
49 contact_dialogs_find (GtkDialog      *dialog,
50                       EmpathyContact *contact)
51 {
52         GtkWidget     *contact_widget;
53         EmpathyContact *this_contact;
54
55         contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
56         this_contact = empathy_contact_widget_get_contact (contact_widget);
57
58         return contact != this_contact;
59 }
60
61 /*
62  *  Subscription dialog
63  */
64
65 static void
66 subscription_dialog_response_cb (GtkDialog *dialog,
67                                  gint       response,
68                                  GtkWidget *contact_widget)
69 {
70         EmpathyContactManager *manager;
71         EmpathyContact        *contact;
72
73         manager = empathy_contact_manager_dup_singleton ();
74         contact = empathy_contact_widget_get_contact (contact_widget);
75
76         if (response == GTK_RESPONSE_YES) {
77                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
78                                           contact, "");
79
80                 empathy_contact_set_alias (contact,
81                         empathy_contact_widget_get_alias (contact_widget));
82         }
83         else if (response == GTK_RESPONSE_NO ||
84                  response == GTK_RESPONSE_REJECT) {
85                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
86                                              contact, "");
87
88                 if (response == GTK_RESPONSE_REJECT) {
89                         empathy_contact_list_set_blocked (
90                                 EMPATHY_CONTACT_LIST (manager), contact, TRUE);
91                 }
92         }
93
94         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
95         gtk_widget_destroy (GTK_WIDGET (dialog));
96         g_object_unref (manager);
97 }
98
99 void
100 empathy_subscription_dialog_show (EmpathyContact *contact,
101                                   const gchar *message,
102                                   GtkWindow     *parent)
103 {
104         GtkBuilder *gui;
105         GtkWidget *dialog;
106         GtkWidget *hbox_subscription;
107         GtkWidget *vbox;
108         GtkWidget *contact_widget;
109         GtkWidget *block_user_button;
110         GList     *l;
111         gchar     *filename;
112         EmpathyContactManager *manager;
113         EmpathyContactListFlags flags;
114
115         manager = empathy_contact_manager_dup_singleton ();
116
117         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
118
119         l = g_list_find_custom (subscription_dialogs,
120                                 contact,
121                                 (GCompareFunc) contact_dialogs_find);
122         if (l) {
123                 gtk_window_present (GTK_WINDOW (l->data));
124                 return;
125         }
126
127         filename = empathy_file_lookup ("empathy-contact-dialogs.ui",
128                                         "libempathy-gtk");
129         gui = empathy_builder_get_file (filename,
130                                       "subscription_request_dialog", &dialog,
131                                       "hbox_subscription", &hbox_subscription,
132                                       "block-user-button", &block_user_button,
133                                       NULL);
134         g_free (filename);
135         g_object_unref (gui);
136
137         vbox = gtk_vbox_new (FALSE, 6);
138
139         gtk_box_pack_end (GTK_BOX (hbox_subscription), vbox,
140                           TRUE, TRUE, 0);
141
142         /* Contact info widget */
143         contact_widget = empathy_contact_widget_new (contact,
144                                                      EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS |
145                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
146                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS |
147                                                      EMPATHY_CONTACT_WIDGET_SHOW_DETAILS);
148         gtk_box_pack_start (GTK_BOX (vbox),
149                           contact_widget,
150                           TRUE, TRUE,
151                           0);
152
153         if (!tp_str_empty (message)) {
154                 GtkWidget *label;
155                 gchar *tmp;
156
157                 label = gtk_label_new ("");
158                 tmp = g_strdup_printf ("<i>%s</i>", message);
159
160                 gtk_label_set_markup (GTK_LABEL (label), tmp);
161                 g_free (tmp);
162                 gtk_widget_show (label);
163
164                 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
165         }
166
167         gtk_widget_show (contact_widget);
168         gtk_widget_show (vbox);
169
170         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
171         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
172
173         g_signal_connect (dialog, "response",
174                           G_CALLBACK (subscription_dialog_response_cb),
175                           contact_widget);
176
177         flags = empathy_contact_manager_get_flags_for_connection (manager,
178                                 empathy_contact_get_connection (contact));
179
180         if (flags & EMPATHY_CONTACT_LIST_CAN_BLOCK)
181                 gtk_widget_show (block_user_button);
182
183         if (parent) {
184                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
185         }
186
187         g_object_unref (manager);
188         gtk_widget_show (dialog);
189 }
190
191 /*
192  *  Information dialog
193  */
194
195 static void
196 contact_dialogs_response_cb (GtkDialog *dialog,
197                              gint       response,
198                              GList    **dialogs)
199 {
200         *dialogs = g_list_remove (*dialogs, dialog);
201         gtk_widget_destroy (GTK_WIDGET (dialog));
202 }
203
204 void
205 empathy_contact_information_dialog_show (EmpathyContact *contact,
206                                          GtkWindow      *parent)
207 {
208         GtkWidget *dialog;
209         GtkWidget *button;
210         GtkWidget *contact_widget;
211         GList     *l;
212
213         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
214
215         l = g_list_find_custom (information_dialogs,
216                                 contact,
217                                 (GCompareFunc) contact_dialogs_find);
218         if (l) {
219                 gtk_window_present (GTK_WINDOW (l->data));
220                 return;
221         }
222
223         /* Create dialog */
224         dialog = gtk_dialog_new ();
225         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
226         gtk_window_set_title (GTK_WINDOW (dialog),
227                 empathy_contact_get_alias (contact));
228
229         /* Close button */
230         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
231         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
232         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
233                                       button,
234                                       GTK_RESPONSE_CLOSE);
235         gtk_widget_set_can_default (button, TRUE);
236         gtk_window_set_default (GTK_WINDOW (dialog), button);
237         gtk_widget_show (button);
238
239         /* Contact info widget */
240         contact_widget = empathy_contact_widget_new (contact,
241                 EMPATHY_CONTACT_WIDGET_SHOW_LOCATION |
242                 EMPATHY_CONTACT_WIDGET_SHOW_DETAILS);
243         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
244         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
245                             contact_widget,
246                             TRUE, TRUE, 0);
247         gtk_widget_show (contact_widget);
248
249         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
250         information_dialogs = g_list_prepend (information_dialogs, dialog);
251
252         g_signal_connect (dialog, "response",
253                           G_CALLBACK (contact_dialogs_response_cb),
254                           &information_dialogs);
255
256         if (parent) {
257                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
258         }
259
260         gtk_widget_show (dialog);
261 }
262
263 void
264 empathy_contact_edit_dialog_show (EmpathyContact *contact,
265                                   GtkWindow      *parent)
266 {
267         GtkWidget *dialog;
268         GtkWidget *button;
269         GtkWidget *contact_widget;
270         GList     *l;
271
272         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
273
274         l = g_list_find_custom (edit_dialogs,
275                                 contact,
276                                 (GCompareFunc) contact_dialogs_find);
277         if (l) {
278                 gtk_window_present (GTK_WINDOW (l->data));
279                 return;
280         }
281
282         /* Create dialog */
283         dialog = gtk_dialog_new ();
284         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
285         gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
286
287         /* Close button */
288         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
289         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
290         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
291                                       button,
292                                       GTK_RESPONSE_CLOSE);
293         gtk_widget_set_can_default (button, TRUE);
294         gtk_window_set_default (GTK_WINDOW (dialog), button);
295         gtk_widget_show (button);
296
297         /* Contact info widget */
298         contact_widget = empathy_contact_widget_new (contact,
299                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
300                 EMPATHY_CONTACT_WIDGET_EDIT_GROUPS |
301                 EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE);
302         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
303         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
304                             contact_widget,
305                             TRUE, TRUE, 0);
306         gtk_widget_show (contact_widget);
307
308         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
309         edit_dialogs = g_list_prepend (edit_dialogs, dialog);
310
311         g_signal_connect (dialog, "response",
312                           G_CALLBACK (contact_dialogs_response_cb),
313                           &edit_dialogs);
314
315         if (parent) {
316                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
317         }
318
319         gtk_widget_show (dialog);
320 }
321
322 void
323 empathy_contact_personal_dialog_show (GtkWindow *parent)
324 {
325         GtkWidget *button;
326         GtkWidget *contact_widget;
327
328         if (personal_dialog) {
329                 gtk_window_present (GTK_WINDOW (personal_dialog));
330                 return;
331         }
332
333         /* Create dialog */
334         personal_dialog = gtk_dialog_new ();
335         gtk_window_set_resizable (GTK_WINDOW (personal_dialog), FALSE);
336         gtk_window_set_title (GTK_WINDOW (personal_dialog), _("Personal Information"));
337
338         /* Close button */
339         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
340         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
341         gtk_dialog_add_action_widget (GTK_DIALOG (personal_dialog),
342                                       button,
343                                       GTK_RESPONSE_CLOSE);
344         gtk_widget_set_can_default (button, TRUE);
345         gtk_window_set_default (GTK_WINDOW (personal_dialog), button);
346         gtk_widget_show (button);
347
348         /* Contact info widget */
349         contact_widget = empathy_contact_widget_new (NULL,
350                 EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
351                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
352                 EMPATHY_CONTACT_WIDGET_EDIT_AVATAR |
353                 EMPATHY_CONTACT_WIDGET_EDIT_DETAILS);
354         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
355         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (personal_dialog))),
356                             contact_widget,
357                             TRUE, TRUE, 0);
358         empathy_contact_widget_set_account_filter (contact_widget,
359                 empathy_account_chooser_filter_is_connected, NULL);
360         gtk_widget_show (contact_widget);
361
362         g_signal_connect (personal_dialog, "response",
363                           G_CALLBACK (gtk_widget_destroy), NULL);
364         g_object_add_weak_pointer (G_OBJECT (personal_dialog),
365                                    (gpointer) &personal_dialog);
366
367         if (parent) {
368                 gtk_window_set_transient_for (GTK_WINDOW (personal_dialog), parent);
369         }
370
371         gtk_widget_show (personal_dialog);
372 }
373
374 /*
375  *  New contact dialog
376  */
377
378 static void
379 can_add_contact_to_account (TpAccount                                 *account,
380                             EmpathyAccountChooserFilterResultCallback  callback,
381                             gpointer                                   callback_data,
382                             gpointer                                   user_data)
383 {
384         EmpathyContactManager *contact_manager;
385         TpConnection          *connection;
386         gboolean               result;
387
388         connection = tp_account_get_connection (account);
389         if (connection == NULL) {
390                 callback (FALSE, callback_data);
391                 return;
392         }
393
394         contact_manager = empathy_contact_manager_dup_singleton ();
395         result = empathy_contact_manager_get_flags_for_connection (
396                 contact_manager, connection) & EMPATHY_CONTACT_LIST_CAN_ADD;
397         g_object_unref (contact_manager);
398
399         callback (result, callback_data);
400 }
401
402 static void
403 new_contact_response_cb (GtkDialog *dialog,
404                          gint       response,
405                          GtkWidget *contact_widget)
406 {
407         EmpathyContactManager *manager;
408         EmpathyContact         *contact;
409
410         manager = empathy_contact_manager_dup_singleton ();
411         contact = empathy_contact_widget_get_contact (contact_widget);
412
413         if (contact && response == GTK_RESPONSE_OK) {
414                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
415                                           contact, "");
416         }
417
418         new_contact_dialog = NULL;
419         gtk_widget_destroy (GTK_WIDGET (dialog));
420         g_object_unref (manager);
421 }
422
423 void
424 empathy_new_contact_dialog_show (GtkWindow *parent)
425 {
426         empathy_new_contact_dialog_show_with_contact (parent, NULL);
427 }
428
429 void
430 empathy_new_contact_dialog_show_with_contact (GtkWindow *parent,
431                                               EmpathyContact *contact)
432 {
433         GtkWidget *dialog;
434         GtkWidget *button;
435         GtkWidget *contact_widget;
436
437         if (new_contact_dialog) {
438                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
439                 return;
440         }
441
442         /* Create dialog */
443         dialog = gtk_dialog_new ();
444         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
445         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
446
447         /* Cancel button */
448         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
449         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
450         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
451                                       button,
452                                       GTK_RESPONSE_CANCEL);
453         gtk_widget_show (button);
454
455         /* Add button */
456         button = gtk_button_new_with_label (GTK_STOCK_ADD);
457         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
458         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
459                                       button,
460                                       GTK_RESPONSE_OK);
461         gtk_widget_show (button);
462
463         /* Contact info widget */
464         contact_widget = empathy_contact_widget_new (contact,
465                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
466                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
467                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
468                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
469         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
470         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
471                             contact_widget,
472                             TRUE, TRUE, 0);
473         empathy_contact_widget_set_account_filter (contact_widget,
474                                                    can_add_contact_to_account,
475                                                    NULL);
476         gtk_widget_show (contact_widget);
477
478         new_contact_dialog = dialog;
479
480         g_signal_connect (dialog, "response",
481                           G_CALLBACK (new_contact_response_cb),
482                           contact_widget);
483
484         if (parent) {
485                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
486         }
487
488         gtk_widget_show (dialog);
489 }
490
491 /**
492  * empathy_block_contact_dialog_show:
493  * @parent: the parent of this dialog (or %NULL)
494  * @contact: the contact for this dialog
495  * @abusive: a pointer to store the value of the abusive contact check box
496  *  (or %NULL)
497  *
498  * Returns: %TRUE if the user wishes to block the contact
499  */
500 gboolean
501 empathy_block_contact_dialog_show (GtkWindow      *parent,
502                                    EmpathyContact *contact,
503                                    gboolean       *abusive)
504 {
505         GtkWidget *dialog;
506         int res;
507
508         dialog = gtk_message_dialog_new (parent,
509                         GTK_DIALOG_MODAL,
510                         GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
511                         _("Block %s?"),
512                         empathy_contact_get_id (contact));
513
514         gtk_message_dialog_format_secondary_text (
515                         GTK_MESSAGE_DIALOG (dialog),
516                         _("Are you sure you want to block the contact %s?"),
517                         empathy_contact_get_id (contact));
518         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
519                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
520                         _("_Block"), GTK_RESPONSE_REJECT,
521                         NULL);
522
523         /* FIXME: support reporting abusive contacts */
524
525         res = gtk_dialog_run (GTK_DIALOG (dialog));
526         gtk_widget_destroy (dialog);
527
528         if (abusive != NULL)
529                 *abusive = FALSE;
530
531         return res == GTK_RESPONSE_REJECT;
532 }