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