1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2007-2010 Collabora Ltd.
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.
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.
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
19 * Authors: Xavier Claessens <xclaesse@gmail.com>
28 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/util.h>
31 #include <folks/folks.h>
32 #include <folks/folks-telepathy.h>
34 #include <libempathy/empathy-individual-manager.h>
35 #include <libempathy/empathy-utils.h>
36 #include <libempathy/empathy-contact-manager.h>
38 #include "empathy-individual-dialogs.h"
39 #include "empathy-contact-widget.h"
40 #include "empathy-ui-utils.h"
42 #define BULLET_POINT "\342\200\242"
44 static GtkWidget *new_individual_dialog = NULL;
51 can_add_contact_to_account (TpAccount *account,
52 EmpathyAccountChooserFilterResultCallback callback,
53 gpointer callback_data,
56 EmpathyIndividualManager *individual_manager;
57 TpConnection *connection;
60 connection = tp_account_get_connection (account);
61 if (connection == NULL)
63 callback (FALSE, callback_data);
67 individual_manager = empathy_individual_manager_dup_singleton ();
68 result = empathy_connection_can_add_personas (connection);
69 g_object_unref (individual_manager);
71 callback (result, callback_data);
75 new_individual_response_cb (GtkDialog *dialog,
77 GtkWidget *contact_widget)
79 EmpathyIndividualManager *individual_manager;
80 EmpathyContact *contact;
82 individual_manager = empathy_individual_manager_dup_singleton ();
83 contact = empathy_contact_widget_get_contact (contact_widget);
85 if (contact && response == GTK_RESPONSE_OK)
86 empathy_individual_manager_add_from_contact (individual_manager, contact);
88 new_individual_dialog = NULL;
89 gtk_widget_destroy (GTK_WIDGET (dialog));
90 g_object_unref (individual_manager);
94 empathy_new_individual_dialog_show (GtkWindow *parent)
96 empathy_new_individual_dialog_show_with_individual (parent, NULL);
100 empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
101 FolksIndividual *individual)
105 EmpathyContact *contact = NULL;
106 GtkWidget *contact_widget;
108 g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));
110 if (new_individual_dialog)
112 gtk_window_present (GTK_WINDOW (new_individual_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"));
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);
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);
134 /* Contact info widget */
135 if (individual != NULL)
136 contact = empathy_contact_dup_from_folks_individual (individual);
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);
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);
151 new_individual_dialog = dialog;
153 g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
157 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
159 gtk_widget_show (dialog);
161 tp_clear_object (&contact);
165 contact_pretty_name (TpContact *contact)
167 const char *alias = tp_contact_get_alias (contact);
168 const char *identifier = tp_contact_get_identifier (contact);
170 if (tp_strdiff (alias, identifier))
171 return g_strdup_printf ("%s (%s)", alias, identifier);
173 return g_strdup (alias);
177 * Block contact dialog
180 empathy_block_individual_dialog_show (GtkWindow *parent,
181 FolksIndividual *individual,
185 EmpathyContactManager *contact_manager =
186 empathy_contact_manager_dup_singleton ();
188 GtkWidget *abusive_check = NULL;
190 GString *text = g_string_new ("");
191 GString *blocked_str = g_string_new ("");
192 GString *notblocked_str = g_string_new ("");
193 guint npersonas_blocked = 0, npersonas_notblocked = 0;
194 gboolean can_report_abuse = FALSE;
197 dialog = gtk_message_dialog_new (parent,
198 GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
200 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
204 GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
205 gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
206 gtk_widget_show (image);
209 /* build a list of personas that support blocking */
210 personas = folks_individual_get_personas (individual);
212 for (l = personas; l != NULL; l = l->next)
214 TpfPersona *persona = l->data;
216 EmpathyContactListFlags flags;
220 if (!TPF_IS_PERSONA (persona))
223 contact = tpf_persona_get_contact (persona);
224 flags = empathy_contact_manager_get_flags_for_connection (
225 contact_manager, tp_contact_get_connection (contact));
227 if (flags & EMPATHY_CONTACT_LIST_CAN_BLOCK)
235 npersonas_notblocked++;
238 if (flags & EMPATHY_CONTACT_LIST_CAN_REPORT_ABUSIVE)
239 can_report_abuse = TRUE;
241 str = contact_pretty_name (contact);
242 g_string_append_printf (s, "\n " BULLET_POINT " %s", str);
246 g_string_append_printf (text,
247 _("Are you sure you want to block '%s' from contacting you again?"),
248 folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
250 if (npersonas_blocked > 0)
251 g_string_append_printf (text, "\n\n%s\n%s",
252 ngettext ("The following identity will be blocked:",
253 "The following identities will be blocked:",
257 if (npersonas_notblocked > 0)
258 g_string_append_printf (text, "\n\n%s\n%s",
259 ngettext ("The following identity can not be blocked:",
260 "The following identities can not be blocked:",
261 npersonas_notblocked),
262 notblocked_str->str);
264 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
267 gtk_dialog_add_buttons (GTK_DIALOG (dialog),
268 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
269 _("_Block"), GTK_RESPONSE_REJECT,
272 if (can_report_abuse)
276 vbox = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));
277 abusive_check = gtk_check_button_new_with_mnemonic (
278 ngettext ("_Report this contact as abusive",
279 "_Report these contacts as abusive",
282 gtk_box_pack_start (GTK_BOX (vbox), abusive_check, FALSE, TRUE, 0);
283 gtk_widget_show (abusive_check);
286 g_object_unref (contact_manager);
287 g_string_free (text, TRUE);
288 g_string_free (blocked_str, TRUE);
289 g_string_free (notblocked_str, TRUE);
291 res = gtk_dialog_run (GTK_DIALOG (dialog));
295 if (abusive_check != NULL)
296 *abusive = gtk_toggle_button_get_active (
297 GTK_TOGGLE_BUTTON (abusive_check));
302 gtk_widget_destroy (dialog);
304 return res == GTK_RESPONSE_REJECT;