]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-individual-dialogs.c
Merge branch 'gnome-3-8'
[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 #include "empathy-individual-dialogs.h"
24
25 #include <glib/gi18n-lib.h>
26
27 #include "empathy-contact-widget.h"
28 #include "empathy-individual-manager.h"
29 #include "empathy-utils.h"
30
31 #define BULLET_POINT "\342\200\242"
32
33 static GtkWidget *new_individual_dialog = NULL;
34
35 /*
36  *  New contact dialog
37  */
38
39 static void
40 can_add_contact_to_account (TpAccount *account,
41     EmpathyAccountChooserFilterResultCallback callback,
42     gpointer callback_data,
43     gpointer user_data)
44 {
45   EmpathyIndividualManager *individual_manager;
46   TpConnection *connection;
47   gboolean result;
48
49   connection = tp_account_get_connection (account);
50   if (connection == NULL)
51     {
52       callback (FALSE, callback_data);
53       return;
54     }
55
56   individual_manager = empathy_individual_manager_dup_singleton ();
57   result = empathy_connection_can_add_personas (connection);
58   g_object_unref (individual_manager);
59
60   callback (result, callback_data);
61 }
62
63 static void
64 new_individual_response_cb (GtkDialog *dialog,
65     gint response,
66     GtkWidget *contact_widget)
67 {
68   EmpathyIndividualManager *individual_manager;
69   EmpathyContact *contact;
70
71   individual_manager = empathy_individual_manager_dup_singleton ();
72   contact = empathy_contact_widget_get_contact (contact_widget);
73
74   if (contact && response == GTK_RESPONSE_OK)
75     empathy_individual_manager_add_from_contact (individual_manager, contact);
76
77   new_individual_dialog = NULL;
78   gtk_widget_destroy (GTK_WIDGET (dialog));
79   g_object_unref (individual_manager);
80 }
81
82 void
83 empathy_new_individual_dialog_show (GtkWindow *parent)
84 {
85   empathy_new_individual_dialog_show_with_individual (parent, NULL);
86 }
87
88 void
89 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
90     FolksIndividual *individual)
91 {
92   GtkWidget *dialog;
93   GtkWidget *button;
94   EmpathyContact *contact = NULL;
95   GtkWidget *contact_widget;
96
97   g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
98
99   if (new_individual_dialog)
100     {
101       gtk_window_present (GTK_WINDOW (new_individual_dialog));
102       return;
103     }
104
105   /* Create dialog */
106   dialog = gtk_dialog_new ();
107   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
108   gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
109
110   /* Cancel button */
111   button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
112   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
113   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
114       GTK_RESPONSE_CANCEL);
115   gtk_widget_show (button);
116
117   /* Add button */
118   button = gtk_button_new_with_label (GTK_STOCK_ADD);
119   gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
120   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
121   gtk_widget_show (button);
122
123   /* Contact info widget */
124   if (individual != NULL)
125     contact = empathy_contact_dup_from_folks_individual (individual);
126
127   contact_widget = empathy_contact_widget_new (contact);
128   gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
129   gtk_box_pack_start (
130       GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
131       contact_widget, TRUE, TRUE, 0);
132   empathy_contact_widget_set_account_filter (contact_widget,
133       can_add_contact_to_account, NULL);
134   gtk_widget_show (contact_widget);
135
136   new_individual_dialog = dialog;
137
138   g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
139       contact_widget);
140
141   if (parent != NULL)
142     gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
143
144   gtk_widget_show (dialog);
145
146   tp_clear_object (&contact);
147 }
148
149 static char *
150 contact_pretty_name (TpContact *contact)
151 {
152   const char *alias = tp_contact_get_alias (contact);
153   const char *identifier = tp_contact_get_identifier (contact);
154
155   if (tp_strdiff (alias, identifier))
156     return g_strdup_printf ("%s (%s)", alias, identifier);
157   else
158     return g_strdup (alias);
159 }
160
161 /*
162  * Block contact dialog
163  */
164 gboolean
165 empathy_block_individual_dialog_show (GtkWindow *parent,
166     FolksIndividual *individual,
167     GdkPixbuf *avatar,
168     gboolean *abusive)
169 {
170   GtkWidget *dialog;
171   GtkWidget *abusive_check = NULL;
172   GeeSet *personas;
173   GeeIterator *iter;
174   GString *text = g_string_new ("");
175   GString *blocked_str = g_string_new ("");
176   GString *notblocked_str = g_string_new ("");
177   guint npersonas_blocked = 0, npersonas_notblocked = 0;
178   gboolean can_report_abuse = FALSE;
179   int res;
180
181   dialog = gtk_message_dialog_new (parent,
182       GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
183       _("Block %s?"),
184       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
185
186   if (avatar != NULL)
187     {
188       GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
189       gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
190       gtk_widget_show (image);
191     }
192
193   /* build a list of personas that support blocking */
194   personas = folks_individual_get_personas (individual);
195   iter = gee_iterable_iterator (GEE_ITERABLE (personas));
196   while (gee_iterator_next (iter))
197     {
198       TpfPersona *persona = gee_iterator_get (iter);
199       TpContact *contact;
200       GString *s;
201       char *str;
202       TpConnection *conn;
203
204       if (!TPF_IS_PERSONA (persona))
205           goto while_finish;
206
207       contact = tpf_persona_get_contact (persona);
208       if (contact == NULL)
209         goto while_finish;
210
211       conn = tp_contact_get_connection (contact);
212
213       if (tp_proxy_has_interface_by_id (conn,
214             TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
215         {
216           s = blocked_str;
217           npersonas_blocked++;
218         }
219       else
220         {
221           s = notblocked_str;
222           npersonas_notblocked++;
223         }
224
225       if (tp_connection_can_report_abusive (conn))
226         can_report_abuse = TRUE;
227
228       str = contact_pretty_name (contact);
229       g_string_append_printf (s, "\n " BULLET_POINT " %s", str);
230       g_free (str);
231
232 while_finish:
233       g_clear_object (&persona);
234     }
235   g_clear_object (&iter);
236
237   g_string_append_printf (text,
238       _("Are you sure you want to block '%s' from contacting you again?"),
239       folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
240
241   if (npersonas_blocked > 0)
242     g_string_append_printf (text, "\n\n%s\n%s",
243         ngettext ("The following identity will be blocked:",
244                   "The following identities will be blocked:",
245                   npersonas_blocked),
246         blocked_str->str);
247
248   if (npersonas_notblocked > 0)
249     g_string_append_printf (text, "\n\n%s\n%s",
250         ngettext ("The following identity can not be blocked:",
251                   "The following identities can not be blocked:",
252                   npersonas_notblocked),
253         notblocked_str->str);
254
255   gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
256     "%s", text->str);
257
258   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
259       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
260       _("_Block"), GTK_RESPONSE_REJECT,
261       NULL);
262
263   if (can_report_abuse)
264     {
265       GtkWidget *vbox;
266
267       vbox = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));
268       abusive_check = gtk_check_button_new_with_mnemonic (
269           ngettext ("_Report this contact as abusive",
270                     "_Report these contacts as abusive",
271                     npersonas_blocked));
272
273       gtk_box_pack_start (GTK_BOX (vbox), abusive_check, FALSE, TRUE, 0);
274       gtk_widget_show (abusive_check);
275     }
276
277   g_string_free (text, TRUE);
278   g_string_free (blocked_str, TRUE);
279   g_string_free (notblocked_str, TRUE);
280
281   res = gtk_dialog_run (GTK_DIALOG (dialog));
282
283   if (abusive != NULL)
284     {
285       if (abusive_check != NULL)
286         *abusive = gtk_toggle_button_get_active (
287             GTK_TOGGLE_BUTTON (abusive_check));
288       else
289         *abusive = FALSE;
290     }
291
292   gtk_widget_destroy (dialog);
293
294   return res == GTK_RESPONSE_REJECT;
295 }