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