]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Made the Close button the default widget in the contact edit dialog. (Jonny Lamb)
[empathy.git] / libempathy-gtk / empathy-contact-dialogs.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007-2008 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 <glade/glade.h>
29 #include <glib/gi18n.h>
30
31 #include <libmissioncontrol/mission-control.h>
32
33 #include <libempathy/empathy-contact-manager.h>
34 #include <libempathy/empathy-contact-list.h>
35 #include <libempathy/empathy-utils.h>
36
37 #include "empathy-contact-dialogs.h"
38 #include "empathy-contact-widget.h"
39 #include "empathy-ui-utils.h"
40
41 static GList *subscription_dialogs = NULL;
42 static GList *information_dialogs = NULL;
43 static GtkWidget *new_contact_dialog = NULL;
44
45
46 static gint
47 contact_dialogs_find (GtkDialog      *dialog,
48                       EmpathyContact *contact)
49 {
50         GtkWidget     *contact_widget;
51         EmpathyContact *this_contact;
52
53         contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
54         this_contact = empathy_contact_widget_get_contact (contact_widget);
55
56         return contact != this_contact;
57 }
58
59 /*
60  *  Subscription dialog
61  */
62
63 static void
64 subscription_dialog_response_cb (GtkDialog *dialog,
65                                  gint       response,
66                                  GtkWidget *contact_widget)
67 {
68         EmpathyContactManager *manager;
69         EmpathyContact        *contact;
70
71         manager = empathy_contact_manager_new ();
72         contact = empathy_contact_widget_get_contact (contact_widget);
73
74         if (response == GTK_RESPONSE_YES) {
75                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
76                                           contact, "");
77         }
78         else if (response == GTK_RESPONSE_NO) {
79                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
80                                              contact, "");
81         }
82
83         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
84         gtk_widget_destroy (GTK_WIDGET (dialog));
85         g_object_unref (manager);
86 }
87
88 void
89 empathy_subscription_dialog_show (EmpathyContact *contact,
90                                   GtkWindow     *parent)
91 {
92         GtkWidget *dialog;
93         GtkWidget *hbox_subscription;
94         GtkWidget *contact_widget;
95         GList     *l;
96         gchar     *filename;
97
98         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
99
100         l = g_list_find_custom (subscription_dialogs,
101                                 contact,
102                                 (GCompareFunc) contact_dialogs_find);
103         if (l) {
104                 gtk_window_present (GTK_WINDOW (l->data));
105                 return;
106         }
107
108         filename = empathy_file_lookup ("empathy-contact-dialogs.glade",
109                                         "libempathy-gtk");
110         empathy_glade_get_file_simple (filename,
111                                       "subscription_request_dialog",
112                                       NULL,
113                                       "subscription_request_dialog", &dialog,
114                                       "hbox_subscription", &hbox_subscription,
115                                       NULL);
116         g_free (filename);
117
118         contact_widget = empathy_contact_widget_new (contact,
119                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
120                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
121         gtk_box_pack_end (GTK_BOX (hbox_subscription),
122                           contact_widget,
123                           TRUE, TRUE,
124                           0);
125
126         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
127         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
128
129         g_signal_connect (dialog, "response",
130                           G_CALLBACK (subscription_dialog_response_cb),
131                           contact_widget);
132
133         if (parent) {
134                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
135         }
136
137         gtk_widget_show (dialog);
138 }
139
140 /*
141  *  Information dialog
142  */
143
144 static void
145 contact_information_response_cb (GtkDialog *dialog,
146                                  gint       response,
147                                  GtkWidget *contact_widget)
148 {
149         information_dialogs = g_list_remove (information_dialogs, dialog);
150         gtk_widget_destroy (GTK_WIDGET (dialog));
151 }
152
153 void
154 empathy_contact_information_dialog_show (EmpathyContact *contact,
155                                          GtkWindow      *parent,
156                                          gboolean        edit,
157                                          gboolean        is_user)
158 {
159         GtkWidget                *dialog;
160         GtkWidget                *button;
161         GtkWidget                *contact_widget;
162         GList                    *l;
163         EmpathyContactWidgetFlags flags = 0;
164
165         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
166
167         l = g_list_find_custom (information_dialogs,
168                                 contact,
169                                 (GCompareFunc) contact_dialogs_find);
170         if (l) {
171                 gtk_window_present (GTK_WINDOW (l->data));
172                 return;
173         }
174
175         /* Create dialog */
176         dialog = gtk_dialog_new ();
177         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
178         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
179         if (is_user) {
180                 gtk_window_set_title (GTK_WINDOW (dialog), _("Personal Information"));
181         }
182         else if (edit) {
183                 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
184         }
185         else {
186                 gtk_window_set_title (GTK_WINDOW (dialog), _("Contact Information"));
187         }
188
189         /* Close button */
190         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
191         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
192         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
193                                       button,
194                                       GTK_RESPONSE_CLOSE);
195         GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
196         gtk_window_set_default (GTK_WINDOW (dialog), button);
197         gtk_widget_show (button);
198
199         /* Contact info widget */
200         if (edit) {
201                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ALIAS;
202         }
203         if (is_user) {
204                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT;
205                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_AVATAR;
206         }
207         if (!is_user && edit) {
208                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_GROUPS;
209         }
210         contact_widget = empathy_contact_widget_new (contact, flags);
211         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
212                             contact_widget,
213                             TRUE, TRUE, 0);
214         if (flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
215                 empathy_contact_widget_set_account_filter (contact_widget,
216                                                            empathy_account_chooser_filter_is_connected,
217                                                            NULL);
218         }
219
220         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
221         information_dialogs = g_list_prepend (information_dialogs, dialog);
222
223         g_signal_connect (dialog, "response",
224                           G_CALLBACK (contact_information_response_cb),
225                           contact_widget);
226
227         if (parent) {
228                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
229         }
230
231         gtk_widget_show (dialog);
232 }
233
234 /*
235  *  New contact dialog
236  */
237
238 static gboolean
239 can_add_contact_to_account (McAccount *account,
240                             gpointer   user_data)
241 {
242         MissionControl     *mc;
243         TpConnectionStatus  status;
244         McProfile          *profile;
245         const gchar        *protocol_name;
246
247         mc = empathy_mission_control_new ();
248         status = mission_control_get_connection_status (mc, account, NULL);
249         g_object_unref (mc);
250         if (status != TP_CONNECTION_STATUS_CONNECTED) {
251                 /* Account is disconnected */
252                 return FALSE;
253         }
254
255         profile = mc_account_get_profile (account);
256         protocol_name = mc_profile_get_protocol_name (profile);
257         if (strcmp (protocol_name, "local-xmpp") == 0) {
258                 /* We can't add accounts to a XMPP LL connection
259                  * FIXME: We should inspect the flags of the contact list group interface
260                  */
261                 g_object_unref (profile);
262                 return FALSE;
263         }
264
265         g_object_unref (profile);
266         return TRUE;
267 }
268
269 static void
270 new_contact_response_cb (GtkDialog *dialog,
271                          gint       response,
272                          GtkWidget *contact_widget)
273 {
274         EmpathyContactManager *manager;
275         EmpathyContact         *contact;
276
277         manager = empathy_contact_manager_new ();
278         contact = empathy_contact_widget_get_contact (contact_widget);
279
280         if (contact && response == GTK_RESPONSE_OK) {
281                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
282                                           contact,
283                                           _("I would like to add you to my contact list."));
284         }
285
286         new_contact_dialog = NULL;
287         gtk_widget_destroy (GTK_WIDGET (dialog));
288         g_object_unref (manager);
289 }
290
291 void
292 empathy_new_contact_dialog_show (GtkWindow *parent)
293 {
294         GtkWidget *dialog;
295         GtkWidget *button;
296         GtkWidget *contact_widget;
297
298         if (new_contact_dialog) {
299                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
300                 return;
301         }
302
303         /* Create dialog */
304         dialog = gtk_dialog_new ();
305         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
306         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
307         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
308
309         /* Cancel button */
310         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
311         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
312         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
313                                       button,
314                                       GTK_RESPONSE_CANCEL);
315         gtk_widget_show (button);
316         
317         /* Add button */
318         button = gtk_button_new_with_label (GTK_STOCK_ADD);
319         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
320         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
321                                       button,
322                                       GTK_RESPONSE_OK);
323         gtk_widget_show (button);
324
325         /* Contact info widget */
326         contact_widget = empathy_contact_widget_new (NULL,
327                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
328                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
329                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
330                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
331         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
332                             contact_widget,
333                             TRUE, TRUE, 0);
334         empathy_contact_widget_set_account_filter (contact_widget,
335                                                    can_add_contact_to_account,
336                                                    NULL);
337
338         new_contact_dialog = dialog;
339
340         g_signal_connect (dialog, "response",
341                           G_CALLBACK (new_contact_response_cb),
342                           contact_widget);
343
344         if (parent) {
345                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
346         }
347
348         gtk_widget_show (dialog);
349 }
350