]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Don't allow to change account if we are editing information of
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30 #include <glib/gi18n.h>
31
32 #include <libmissioncontrol/mission-control.h>
33
34 #include <libempathy/empathy-contact-manager.h>
35 #include <libempathy/empathy-contact-list.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include "empathy-contact-dialogs.h"
39 #include "empathy-contact-widget.h"
40 #include "empathy-ui-utils.h"
41
42 static GList *subscription_dialogs = NULL;
43 static GList *information_dialogs = NULL;
44 static GtkWidget *new_contact_dialog = NULL;
45
46
47 static gint
48 contact_dialogs_find (GtkDialog      *dialog,
49                       EmpathyContact *contact)
50 {
51         GtkWidget     *contact_widget;
52         EmpathyContact *this_contact;
53
54         contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
55         this_contact = empathy_contact_widget_get_contact (contact_widget);
56
57         return !empathy_contact_equal (contact, this_contact);
58 }
59
60 /*
61  *  Subscription dialog
62  */
63
64 static void
65 subscription_dialog_response_cb (GtkDialog *dialog,
66                                  gint       response,
67                                  GtkWidget *contact_widget)
68 {
69         EmpathyContactManager *manager;
70         EmpathyContact        *contact;
71
72         manager = empathy_contact_manager_new ();
73         contact = empathy_contact_widget_get_contact (contact_widget);
74
75         if (response == GTK_RESPONSE_YES) {
76                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
77                                           contact, "");
78         }
79         else if (response == GTK_RESPONSE_NO) {
80                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
81                                              contact, "");
82         }
83
84         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
85         gtk_widget_destroy (GTK_WIDGET (dialog));
86         g_object_unref (manager);
87 }
88
89 void
90 empathy_subscription_dialog_show (EmpathyContact *contact,
91                                   GtkWindow     *parent)
92 {
93         GtkWidget *dialog;
94         GtkWidget *hbox_subscription;
95         GtkWidget *contact_widget;
96         GList     *l;
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         empathy_glade_get_file_simple ("empathy-contact-dialogs.glade",
109                                       "subscription_request_dialog",
110                                       NULL,
111                                       "subscription_request_dialog", &dialog,
112                                       "hbox_subscription", &hbox_subscription,
113                                       NULL);
114
115         contact_widget = empathy_contact_widget_new (contact,
116                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
117                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
118         gtk_box_pack_end (GTK_BOX (hbox_subscription),
119                           contact_widget,
120                           TRUE, TRUE,
121                           0);
122
123         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
124         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
125
126         g_signal_connect (dialog, "response",
127                           G_CALLBACK (subscription_dialog_response_cb),
128                           contact_widget);
129
130         if (parent) {
131                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
132         }
133
134         gtk_widget_show (dialog);
135 }
136
137 /*
138  *  Information dialog
139  */
140
141 static void
142 contact_information_response_cb (GtkDialog *dialog,
143                                  gint       response,
144                                  GtkWidget *contact_widget)
145 {
146         information_dialogs = g_list_remove (information_dialogs, dialog);
147         gtk_widget_destroy (GTK_WIDGET (dialog));
148 }
149
150 void
151 empathy_contact_information_dialog_show (EmpathyContact *contact,
152                                          GtkWindow      *parent,
153                                          gboolean        edit,
154                                          gboolean        is_user)
155 {
156         GtkWidget                *dialog;
157         GtkWidget                *button;
158         GtkWidget                *contact_widget;
159         GList                    *l;
160         EmpathyContactWidgetFlags flags = 0;
161
162         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
163
164         l = g_list_find_custom (information_dialogs,
165                                 contact,
166                                 (GCompareFunc) contact_dialogs_find);
167         if (l) {
168                 gtk_window_present (GTK_WINDOW (l->data));
169                 return;
170         }
171
172         /* Create dialog */
173         dialog = gtk_dialog_new ();
174         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
175         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
176         gtk_window_set_title (GTK_WINDOW (dialog), _("Contact information"));
177
178         /* Close button */
179         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
180         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
181         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
182                                       button,
183                                       GTK_RESPONSE_CLOSE);
184         gtk_widget_show (button);
185
186         /* Contact info widget */
187         if (edit) {
188                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ALIAS;
189         }
190         if (is_user) {
191                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT;
192                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_AVATAR;
193         }
194         if (!is_user && edit) {
195                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_GROUPS;
196         }
197         contact_widget = empathy_contact_widget_new (contact, flags);
198         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
199                             contact_widget,
200                             TRUE, TRUE, 0);
201         if (flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
202                 empathy_contact_widget_set_account_filter (contact_widget,
203                                                            empathy_account_chooser_filter_is_connected,
204                                                            NULL);
205         }
206
207         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
208         information_dialogs = g_list_prepend (information_dialogs, dialog);
209
210         g_signal_connect (dialog, "response",
211                           G_CALLBACK (contact_information_response_cb),
212                           contact_widget);
213
214         if (parent) {
215                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
216         }
217
218         gtk_widget_show (dialog);
219 }
220
221 /*
222  *  New contact dialog
223  */
224
225 static gboolean
226 can_add_contact_to_account (McAccount *account,
227                             gpointer   user_data)
228 {
229         MissionControl            *mc;
230         TelepathyConnectionStatus  status;
231         McProfile                 *profile;
232         const gchar               *protocol_name;
233
234         mc = empathy_mission_control_new ();
235         status = mission_control_get_connection_status (mc, account, NULL);
236         g_object_unref (mc);
237         if (status != TP_CONN_STATUS_CONNECTED) {
238                 /* Account is disconnected */
239                 return FALSE;
240         }
241
242         profile = mc_account_get_profile (account);
243         protocol_name = mc_profile_get_protocol_name (profile);
244         if (strcmp (protocol_name, "local-xmpp") == 0) {
245                 /* We can't add accounts to a XMPP LL connection
246                  * FIXME: We should inspect the flags of the contact list group interface
247                  */
248                 g_object_unref (profile);
249                 return FALSE;
250         }
251
252         g_object_unref (profile);
253         return TRUE;
254 }
255
256 static void
257 new_contact_response_cb (GtkDialog *dialog,
258                          gint       response,
259                          GtkWidget *contact_widget)
260 {
261         EmpathyContactManager *manager;
262         EmpathyContact         *contact;
263
264         manager = empathy_contact_manager_new ();
265         contact = empathy_contact_widget_get_contact (contact_widget);
266
267         if (contact && response == GTK_RESPONSE_OK) {
268                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
269                                           contact,
270                                           _("I would like to add you to my contact list."));
271         }
272
273         new_contact_dialog = NULL;
274         gtk_widget_destroy (GTK_WIDGET (dialog));
275         g_object_unref (manager);
276 }
277
278 void
279 empathy_new_contact_dialog_show (GtkWindow *parent)
280 {
281         GtkWidget *dialog;
282         GtkWidget *button;
283         GtkWidget *contact_widget;
284
285         if (new_contact_dialog) {
286                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
287                 return;
288         }
289
290         /* Create dialog */
291         dialog = gtk_dialog_new ();
292         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
293         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
294         gtk_window_set_title (GTK_WINDOW (dialog), _("New contact"));
295
296         /* Cancel button */
297         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
298         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
299         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
300                                       button,
301                                       GTK_RESPONSE_CANCEL);
302         gtk_widget_show (button);
303         
304         /* Add button */
305         button = gtk_button_new_with_label (GTK_STOCK_ADD);
306         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
307         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
308                                       button,
309                                       GTK_RESPONSE_OK);
310         gtk_widget_show (button);
311
312         /* Contact info widget */
313         contact_widget = empathy_contact_widget_new (NULL,
314                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
315                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
316                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
317                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
318         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
319                             contact_widget,
320                             TRUE, TRUE, 0);
321         empathy_contact_widget_set_account_filter (contact_widget,
322                                                    can_add_contact_to_account,
323                                                    NULL);
324
325         new_contact_dialog = dialog;
326
327         g_signal_connect (dialog, "response",
328                           G_CALLBACK (new_contact_response_cb),
329                           contact_widget);
330
331         if (parent) {
332                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
333         }
334
335         gtk_widget_show (dialog);
336 }
337