]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-dialogs.c
Only destroy block confirmation dialogs after we've read out the abusive state
[empathy.git] / libempathy-gtk / empathy-individual-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2010 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  */
21
22 #include <config.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include <gtk/gtk.h>
28 #include <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/util.h>
31 #include <folks/folks.h>
32 #include <folks/folks-telepathy.h>
33
34 #include <libempathy/empathy-individual-manager.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-contact-manager.h>
37
38 #include "empathy-individual-dialogs.h"
39 #include "empathy-contact-widget.h"
40 #include "empathy-ui-utils.h"
41
42 #define BULLET_POINT "\342\200\242"
43
44 static GtkWidget *new_individual_dialog = NULL;
45
46 /*
47  *  New contact dialog
48  */
49
50 static void
51 can_add_contact_to_account (TpAccount *account,
52     EmpathyAccountChooserFilterResultCallback callback,
53     gpointer callback_data,
54     gpointer user_data)
55 {
56   EmpathyIndividualManager *individual_manager;
57   TpConnection *connection;
58   gboolean result;
59
60   connection = tp_account_get_connection (account);
61   if (connection == NULL)
62     {
63       callback (FALSE, callback_data);
64       return;
65     }
66
67   individual_manager = empathy_individual_manager_dup_singleton ();
68   result = empathy_connection_can_add_personas (connection);
69   g_object_unref (individual_manager);
70
71   callback (result, callback_data);
72 }
73
74 static void
75 new_individual_response_cb (GtkDialog *dialog,
76     gint response,
77     GtkWidget *contact_widget)
78 {
79   EmpathyIndividualManager *individual_manager;
80   EmpathyContact *contact;
81
82   individual_manager = empathy_individual_manager_dup_singleton ();
83   contact = empathy_contact_widget_get_contact (contact_widget);
84
85   if (contact && response == GTK_RESPONSE_OK)
86     empathy_individual_manager_add_from_contact (individual_manager, contact);
87
88   new_individual_dialog = NULL;
89   gtk_widget_destroy (GTK_WIDGET (dialog));
90   g_object_unref (individual_manager);
91 }
92
93 void
94 empathy_new_individual_dialog_show (GtkWindow *parent)
95 {
96   empathy_new_individual_dialog_show_with_individual (parent, NULL);
97 }
98
99 void
100 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
101     FolksIndividual *individual)
102 {
103   GtkWidget *dialog;
104   GtkWidget *button;
105   EmpathyContact *contact = NULL;
106   GtkWidget *contact_widget;
107
108   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
109
110   if (new_individual_dialog)
111     {
112       gtk_window_present (GTK_WINDOW (new_individual_dialog));
113       return;
114     }
115
116   /* Create dialog */
117   dialog = gtk_dialog_new ();
118   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
119   gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
120
121   /* Cancel button */
122   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
123   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
124   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
125       GTK_RESPONSE_CANCEL);
126   gtk_widget_show (button);
127
128   /* Add button */
129   button = gtk_button_new_with_label (GTK_STOCK_ADD);
130   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
131   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
132   gtk_widget_show (button);
133
134   /* Contact info widget */
135   if (individual != NULL)
136     contact = empathy_contact_dup_from_folks_individual (individual);
137
138   contact_widget = empathy_contact_widget_new (contact,
139       EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
140       EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
141       EMPATHY_CONTACT_WIDGET_EDIT_ID |
142       EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
143   gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
144   gtk_box_pack_start (
145       GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
146       contact_widget, TRUE, TRUE, 0);
147   empathy_contact_widget_set_account_filter (contact_widget,
148       can_add_contact_to_account, NULL);
149   gtk_widget_show (contact_widget);
150
151   new_individual_dialog = dialog;
152
153   g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
154       contact_widget);
155
156   if (parent != NULL)
157     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
158
159   gtk_widget_show (dialog);
160
161   tp_clear_object (&contact);
162 }
163
164 /*
165  * Block contact dialog
166  */
167 gboolean
168 empathy_block_individual_dialog_show (GtkWindow *parent,
169     FolksIndividual *individual,
170     gboolean *abusive)
171 {
172   EmpathyContactManager *contact_manager =
173     empathy_contact_manager_dup_singleton ();
174   GtkWidget *dialog;
175   GtkWidget *abusive_check = NULL;
176   GList *personas, *l;
177   GString *str = g_string_new ("");
178   guint npersonas = 0;
179   gboolean can_report_abuse = FALSE;
180   int res;
181
182   dialog = gtk_message_dialog_new (parent,
183       GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
184       _("Block %s?"),
185       folks_aliasable_get_alias (FOLKS_ALIASABLE (individual)));
186
187   /* build a list of personas that support blocking */
188   personas = folks_individual_get_personas (individual);
189
190   for (l = personas; l != NULL; l = l->next)
191     {
192       TpfPersona *persona = l->data;
193       TpContact *contact;
194       EmpathyContactListFlags flags;
195
196       if (!TPF_IS_PERSONA (persona))
197           continue;
198
199       contact = tpf_persona_get_contact (persona);
200       flags = empathy_contact_manager_get_flags_for_connection (
201           contact_manager, tp_contact_get_connection (contact));
202
203       if (!(flags & EMPATHY_CONTACT_LIST_CAN_BLOCK))
204         continue;
205       else if (flags & EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE)
206         can_report_abuse = TRUE;
207
208       g_string_append_printf (str, "\n " BULLET_POINT " %s",
209           tp_contact_get_identifier (contact));
210       npersonas++;
211     }
212
213   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
214     "%s\n%s",
215     ngettext ("Are you sure you want to block the following contact?",
216               "Are you sure you want to block the following contacts?",
217               npersonas),
218     str->str);
219
220   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
221       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
222       _("_Block"), GTK_RESPONSE_REJECT,
223       NULL);
224
225   if (can_report_abuse)
226     {
227       GtkWidget *vbox;
228
229       vbox = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));
230       abusive_check = gtk_check_button_new_with_mnemonic (
231           ngettext ("_Report this contact as abusive",
232                     "_Report these contacts as abusive",
233                     npersonas));
234
235       gtk_box_pack_start (GTK_BOX (vbox), abusive_check, FALSE, TRUE, 0);
236       gtk_widget_show (abusive_check);
237     }
238
239   g_object_unref (contact_manager);
240   g_string_free (str, TRUE);
241
242   res = gtk_dialog_run (GTK_DIALOG (dialog));
243
244   if (abusive != NULL)
245     {
246       if (abusive_check != NULL)
247         *abusive = gtk_toggle_button_get_active (
248             GTK_TOGGLE_BUTTON (abusive_check));
249       else
250         *abusive = FALSE;
251     }
252
253   gtk_widget_destroy (dialog);
254
255   return res == GTK_RESPONSE_REJECT;
256 }