Updated for release
[empathy-mirror.git] / libempathy-gtk / empathy-contact-dialogs.c
blob54d62a3d074fec674e66460e47688e6a775f4b2c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007 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>
22 #include <config.h>
24 #include <string.h>
25 #include <stdlib.h>
27 #include <gtk/gtk.h>
28 #include <glade/glade.h>
29 #include <glib/gi18n.h>
31 #include <libmissioncontrol/mission-control.h>
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-contact-list.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-contact-dialogs.h"
38 #include "empathy-contact-widget.h"
39 #include "empathy-ui-utils.h"
41 static GList *subscription_dialogs = NULL;
42 static GList *information_dialogs = NULL;
43 static GtkWidget *new_contact_dialog = NULL;
46 static gint
47 contact_dialogs_find (GtkDialog *dialog,
48 EmpathyContact *contact)
50 GtkWidget *contact_widget;
51 EmpathyContact *this_contact;
53 contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
54 this_contact = empathy_contact_widget_get_contact (contact_widget);
56 return !empathy_contact_equal (contact, this_contact);
60 * Subscription dialog
63 static void
64 subscription_dialog_response_cb (GtkDialog *dialog,
65 gint response,
66 GtkWidget *contact_widget)
68 EmpathyContactManager *manager;
69 EmpathyContact *contact;
71 manager = empathy_contact_manager_new ();
72 contact = empathy_contact_widget_get_contact (contact_widget);
74 if (response == GTK_RESPONSE_YES) {
75 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
76 contact, "");
78 else if (response == GTK_RESPONSE_NO) {
79 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
80 contact, "");
83 subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
84 gtk_widget_destroy (GTK_WIDGET (dialog));
85 g_object_unref (manager);
88 void
89 empathy_subscription_dialog_show (EmpathyContact *contact,
90 GtkWindow *parent)
92 GtkWidget *dialog;
93 GtkWidget *hbox_subscription;
94 GtkWidget *contact_widget;
95 GList *l;
97 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
99 l = g_list_find_custom (subscription_dialogs,
100 contact,
101 (GCompareFunc) contact_dialogs_find);
102 if (l) {
103 gtk_window_present (GTK_WINDOW (l->data));
104 return;
107 empathy_glade_get_file_simple ("empathy-contact-dialogs.glade",
108 "subscription_request_dialog",
109 NULL,
110 "subscription_request_dialog", &dialog,
111 "hbox_subscription", &hbox_subscription,
112 NULL);
114 contact_widget = empathy_contact_widget_new (contact,
115 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
116 EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
117 gtk_box_pack_end (GTK_BOX (hbox_subscription),
118 contact_widget,
119 TRUE, TRUE,
122 g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
123 subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
125 g_signal_connect (dialog, "response",
126 G_CALLBACK (subscription_dialog_response_cb),
127 contact_widget);
129 if (parent) {
130 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
133 gtk_widget_show (dialog);
137 * Information dialog
140 static void
141 contact_information_response_cb (GtkDialog *dialog,
142 gint response,
143 GtkWidget *contact_widget)
145 information_dialogs = g_list_remove (information_dialogs, dialog);
146 gtk_widget_destroy (GTK_WIDGET (dialog));
149 void
150 empathy_contact_information_dialog_show (EmpathyContact *contact,
151 GtkWindow *parent,
152 gboolean edit,
153 gboolean is_user)
155 GtkWidget *dialog;
156 GtkWidget *button;
157 GtkWidget *contact_widget;
158 GList *l;
159 EmpathyContactWidgetFlags flags = 0;
161 g_return_if_fail (EMPATHY_IS_CONTACT (contact));
163 l = g_list_find_custom (information_dialogs,
164 contact,
165 (GCompareFunc) contact_dialogs_find);
166 if (l) {
167 gtk_window_present (GTK_WINDOW (l->data));
168 return;
171 /* Create dialog */
172 dialog = gtk_dialog_new ();
173 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
174 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
175 if (is_user) {
176 gtk_window_set_title (GTK_WINDOW (dialog), _("Personal Information"));
178 else if (edit) {
179 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
181 else {
182 gtk_window_set_title (GTK_WINDOW (dialog), _("Contact Information"));
185 /* Close button */
186 button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
187 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
188 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
189 button,
190 GTK_RESPONSE_CLOSE);
191 gtk_widget_show (button);
193 /* Contact info widget */
194 if (edit) {
195 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ALIAS;
197 if (is_user) {
198 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT;
199 flags |= EMPATHY_CONTACT_WIDGET_EDIT_AVATAR;
201 if (!is_user && edit) {
202 flags |= EMPATHY_CONTACT_WIDGET_EDIT_GROUPS;
204 contact_widget = empathy_contact_widget_new (contact, flags);
205 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
206 contact_widget,
207 TRUE, TRUE, 0);
208 if (flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
209 empathy_contact_widget_set_account_filter (contact_widget,
210 empathy_account_chooser_filter_is_connected,
211 NULL);
214 g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
215 information_dialogs = g_list_prepend (information_dialogs, dialog);
217 g_signal_connect (dialog, "response",
218 G_CALLBACK (contact_information_response_cb),
219 contact_widget);
221 if (parent) {
222 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
225 gtk_widget_show (dialog);
229 * New contact dialog
232 static gboolean
233 can_add_contact_to_account (McAccount *account,
234 gpointer user_data)
236 MissionControl *mc;
237 TelepathyConnectionStatus status;
238 McProfile *profile;
239 const gchar *protocol_name;
241 mc = empathy_mission_control_new ();
242 status = mission_control_get_connection_status (mc, account, NULL);
243 g_object_unref (mc);
244 if (status != TP_CONN_STATUS_CONNECTED) {
245 /* Account is disconnected */
246 return FALSE;
249 profile = mc_account_get_profile (account);
250 protocol_name = mc_profile_get_protocol_name (profile);
251 if (strcmp (protocol_name, "local-xmpp") == 0) {
252 /* We can't add accounts to a XMPP LL connection
253 * FIXME: We should inspect the flags of the contact list group interface
255 g_object_unref (profile);
256 return FALSE;
259 g_object_unref (profile);
260 return TRUE;
263 static void
264 new_contact_response_cb (GtkDialog *dialog,
265 gint response,
266 GtkWidget *contact_widget)
268 EmpathyContactManager *manager;
269 EmpathyContact *contact;
271 manager = empathy_contact_manager_new ();
272 contact = empathy_contact_widget_get_contact (contact_widget);
274 if (contact && response == GTK_RESPONSE_OK) {
275 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
276 contact,
277 _("I would like to add you to my contact list."));
280 new_contact_dialog = NULL;
281 gtk_widget_destroy (GTK_WIDGET (dialog));
282 g_object_unref (manager);
285 void
286 empathy_new_contact_dialog_show (GtkWindow *parent)
288 GtkWidget *dialog;
289 GtkWidget *button;
290 GtkWidget *contact_widget;
292 if (new_contact_dialog) {
293 gtk_window_present (GTK_WINDOW (new_contact_dialog));
294 return;
297 /* Create dialog */
298 dialog = gtk_dialog_new ();
299 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
300 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
301 gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
303 /* Cancel button */
304 button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
305 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
306 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
307 button,
308 GTK_RESPONSE_CANCEL);
309 gtk_widget_show (button);
311 /* Add button */
312 button = gtk_button_new_with_label (GTK_STOCK_ADD);
313 gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
314 gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
315 button,
316 GTK_RESPONSE_OK);
317 gtk_widget_show (button);
319 /* Contact info widget */
320 contact_widget = empathy_contact_widget_new (NULL,
321 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
322 EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
323 EMPATHY_CONTACT_WIDGET_EDIT_ID |
324 EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
325 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
326 contact_widget,
327 TRUE, TRUE, 0);
328 empathy_contact_widget_set_account_filter (contact_widget,
329 can_add_contact_to_account,
330 NULL);
332 new_contact_dialog = dialog;
334 g_signal_connect (dialog, "response",
335 G_CALLBACK (new_contact_response_cb),
336 contact_widget);
338 if (parent) {
339 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
342 gtk_widget_show (dialog);