]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Merge branch 'sasl'
[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 <glib/gi18n-lib.h>
29
30 #include <telepathy-glib/account-manager.h>
31
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-contact-list.h>
34 #include <libempathy/empathy-tp-contact-factory.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 GList *edit_dialogs = NULL;
44 static GtkWidget *personal_dialog = NULL;
45 static GtkWidget *new_contact_dialog = NULL;
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 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_dup_singleton ();
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                 empathy_contact_set_alias (contact,
80                         empathy_contact_widget_get_alias (contact_widget));
81         }
82         else if (response == GTK_RESPONSE_NO) {
83                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
84                                              contact, "");
85         }
86
87         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
88         gtk_widget_destroy (GTK_WIDGET (dialog));
89         g_object_unref (manager);
90 }
91
92 void
93 empathy_subscription_dialog_show (EmpathyContact *contact,
94                                   const gchar *message,
95                                   GtkWindow     *parent)
96 {
97         GtkBuilder *gui;
98         GtkWidget *dialog;
99         GtkWidget *hbox_subscription;
100         GtkWidget *vbox;
101         GtkWidget *contact_widget;
102         GList     *l;
103         gchar     *filename;
104
105         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
106
107         l = g_list_find_custom (subscription_dialogs,
108                                 contact,
109                                 (GCompareFunc) contact_dialogs_find);
110         if (l) {
111                 gtk_window_present (GTK_WINDOW (l->data));
112                 return;
113         }
114
115         filename = empathy_file_lookup ("empathy-contact-dialogs.ui",
116                                         "libempathy-gtk");
117         gui = empathy_builder_get_file (filename,
118                                       "subscription_request_dialog", &dialog,
119                                       "hbox_subscription", &hbox_subscription,
120                                       NULL);
121         g_free (filename);
122         g_object_unref (gui);
123
124         vbox = gtk_vbox_new (FALSE, 6);
125
126         gtk_box_pack_end (GTK_BOX (hbox_subscription), vbox,
127                           TRUE, TRUE, 0);
128
129         /* Contact info widget */
130         contact_widget = empathy_contact_widget_new (contact,
131                                                      EMPATHY_CONTACT_WIDGET_NO_SET_ALIAS |
132                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
133                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
134         gtk_box_pack_start (GTK_BOX (vbox),
135                           contact_widget,
136                           TRUE, TRUE,
137                           0);
138
139         if (!tp_str_empty (message)) {
140                 GtkWidget *label;
141                 gchar *tmp;
142
143                 label = gtk_label_new ("");
144                 tmp = g_strdup_printf ("<i>%s</i>", message);
145
146                 gtk_label_set_markup (GTK_LABEL (label), tmp);
147                 g_free (tmp);
148                 gtk_widget_show (label);
149
150                 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
151         }
152
153         gtk_widget_show (contact_widget);
154         gtk_widget_show (vbox);
155
156         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
157         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
158
159         g_signal_connect (dialog, "response",
160                           G_CALLBACK (subscription_dialog_response_cb),
161                           contact_widget);
162
163         if (parent) {
164                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
165         }
166
167         gtk_widget_show (dialog);
168 }
169
170 /*
171  *  Information dialog
172  */
173
174 static void
175 contact_dialogs_response_cb (GtkDialog *dialog,
176                              gint       response,
177                              GList    **dialogs)
178 {
179         *dialogs = g_list_remove (*dialogs, dialog);
180         gtk_widget_destroy (GTK_WIDGET (dialog));
181 }
182
183 void
184 empathy_contact_information_dialog_show (EmpathyContact *contact,
185                                          GtkWindow      *parent)
186 {
187         GtkWidget *dialog;
188         GtkWidget *button;
189         GtkWidget *contact_widget;
190         GList     *l;
191
192         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
193
194         l = g_list_find_custom (information_dialogs,
195                                 contact,
196                                 (GCompareFunc) contact_dialogs_find);
197         if (l) {
198                 gtk_window_present (GTK_WINDOW (l->data));
199                 return;
200         }
201
202         /* Create dialog */
203         dialog = gtk_dialog_new ();
204         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
205         gtk_window_set_title (GTK_WINDOW (dialog),
206                 empathy_contact_get_alias (contact));
207
208         /* Close button */
209         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
210         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
211         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
212                                       button,
213                                       GTK_RESPONSE_CLOSE);
214         gtk_widget_set_can_default (button, TRUE);
215         gtk_window_set_default (GTK_WINDOW (dialog), button);
216         gtk_widget_show (button);
217
218         /* Contact info widget */
219         contact_widget = empathy_contact_widget_new (contact,
220                 EMPATHY_CONTACT_WIDGET_SHOW_LOCATION |
221                 EMPATHY_CONTACT_WIDGET_SHOW_DETAILS);
222         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
223         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
224                             contact_widget,
225                             TRUE, TRUE, 0);
226         gtk_widget_show (contact_widget);
227
228         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
229         information_dialogs = g_list_prepend (information_dialogs, dialog);
230
231         g_signal_connect (dialog, "response",
232                           G_CALLBACK (contact_dialogs_response_cb),
233                           &information_dialogs);
234
235         if (parent) {
236                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
237         }
238
239         gtk_widget_show (dialog);
240 }
241
242 void
243 empathy_contact_edit_dialog_show (EmpathyContact *contact,
244                                   GtkWindow      *parent)
245 {
246         GtkWidget *dialog;
247         GtkWidget *button;
248         GtkWidget *contact_widget;
249         GList     *l;
250
251         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
252
253         l = g_list_find_custom (edit_dialogs,
254                                 contact,
255                                 (GCompareFunc) contact_dialogs_find);
256         if (l) {
257                 gtk_window_present (GTK_WINDOW (l->data));
258                 return;
259         }
260
261         /* Create dialog */
262         dialog = gtk_dialog_new ();
263         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
264         gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
265
266         /* Close button */
267         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
268         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
269         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
270                                       button,
271                                       GTK_RESPONSE_CLOSE);
272         gtk_widget_set_can_default (button, TRUE);
273         gtk_window_set_default (GTK_WINDOW (dialog), button);
274         gtk_widget_show (button);
275
276         /* Contact info widget */
277         contact_widget = empathy_contact_widget_new (contact,
278                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
279                 EMPATHY_CONTACT_WIDGET_EDIT_GROUPS |
280                 EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE);
281         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
282         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
283                             contact_widget,
284                             TRUE, TRUE, 0);
285         gtk_widget_show (contact_widget);
286
287         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
288         edit_dialogs = g_list_prepend (edit_dialogs, dialog);
289
290         g_signal_connect (dialog, "response",
291                           G_CALLBACK (contact_dialogs_response_cb),
292                           &edit_dialogs);
293
294         if (parent) {
295                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
296         }
297
298         gtk_widget_show (dialog);
299 }
300
301 void
302 empathy_contact_personal_dialog_show (GtkWindow *parent)
303 {
304         GtkWidget *button;
305         GtkWidget *contact_widget;
306
307         if (personal_dialog) {
308                 gtk_window_present (GTK_WINDOW (personal_dialog));
309                 return;
310         }
311
312         /* Create dialog */
313         personal_dialog = gtk_dialog_new ();
314         gtk_window_set_resizable (GTK_WINDOW (personal_dialog), FALSE);
315         gtk_window_set_title (GTK_WINDOW (personal_dialog), _("Personal Information"));
316
317         /* Close button */
318         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
319         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
320         gtk_dialog_add_action_widget (GTK_DIALOG (personal_dialog),
321                                       button,
322                                       GTK_RESPONSE_CLOSE);
323         gtk_widget_set_can_default (button, TRUE);
324         gtk_window_set_default (GTK_WINDOW (personal_dialog), button);
325         gtk_widget_show (button);
326
327         /* Contact info widget */
328         contact_widget = empathy_contact_widget_new (NULL,
329                 EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
330                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
331                 EMPATHY_CONTACT_WIDGET_EDIT_AVATAR |
332                 EMPATHY_CONTACT_WIDGET_EDIT_DETAILS);
333         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
334         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (personal_dialog))),
335                             contact_widget,
336                             TRUE, TRUE, 0);
337         empathy_contact_widget_set_account_filter (contact_widget,
338                 empathy_account_chooser_filter_is_connected, NULL);
339         gtk_widget_show (contact_widget);
340
341         g_signal_connect (personal_dialog, "response",
342                           G_CALLBACK (gtk_widget_destroy), NULL);
343         g_object_add_weak_pointer (G_OBJECT (personal_dialog),
344                                    (gpointer) &personal_dialog);
345
346         if (parent) {
347                 gtk_window_set_transient_for (GTK_WINDOW (personal_dialog), parent);
348         }
349
350         gtk_widget_show (personal_dialog);
351 }
352
353 /*
354  *  New contact dialog
355  */
356
357 static void
358 can_add_contact_to_account (TpAccount                                 *account,
359                             EmpathyAccountChooserFilterResultCallback  callback,
360                             gpointer                                   callback_data,
361                             gpointer                                   user_data)
362 {
363         EmpathyContactManager *contact_manager;
364         TpConnection          *connection;
365         gboolean               result;
366
367         connection = tp_account_get_connection (account);
368         if (connection == NULL) {
369                 callback (FALSE, callback_data);
370                 return;
371         }
372
373         contact_manager = empathy_contact_manager_dup_singleton ();
374         result = empathy_contact_manager_get_flags_for_connection (
375                 contact_manager, connection) & EMPATHY_CONTACT_LIST_CAN_ADD;
376         g_object_unref (contact_manager);
377
378         callback (result, callback_data);
379 }
380
381 static void
382 new_contact_response_cb (GtkDialog *dialog,
383                          gint       response,
384                          GtkWidget *contact_widget)
385 {
386         EmpathyContactManager *manager;
387         EmpathyContact         *contact;
388
389         manager = empathy_contact_manager_dup_singleton ();
390         contact = empathy_contact_widget_get_contact (contact_widget);
391
392         if (contact && response == GTK_RESPONSE_OK) {
393                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
394                                           contact, "");
395         }
396
397         new_contact_dialog = NULL;
398         gtk_widget_destroy (GTK_WIDGET (dialog));
399         g_object_unref (manager);
400 }
401
402 void
403 empathy_new_contact_dialog_show (GtkWindow *parent)
404 {
405         empathy_new_contact_dialog_show_with_contact (parent, NULL);
406 }
407
408 void
409 empathy_new_contact_dialog_show_with_contact (GtkWindow *parent,
410                                               EmpathyContact *contact)
411 {
412         GtkWidget *dialog;
413         GtkWidget *button;
414         GtkWidget *contact_widget;
415
416         if (new_contact_dialog) {
417                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
418                 return;
419         }
420
421         /* Create dialog */
422         dialog = gtk_dialog_new ();
423         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
424         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
425
426         /* Cancel button */
427         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
428         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
429         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
430                                       button,
431                                       GTK_RESPONSE_CANCEL);
432         gtk_widget_show (button);
433
434         /* Add button */
435         button = gtk_button_new_with_label (GTK_STOCK_ADD);
436         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
437         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
438                                       button,
439                                       GTK_RESPONSE_OK);
440         gtk_widget_show (button);
441
442         /* Contact info widget */
443         contact_widget = empathy_contact_widget_new (contact,
444                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
445                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
446                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
447                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
448         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
449         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
450                             contact_widget,
451                             TRUE, TRUE, 0);
452         empathy_contact_widget_set_account_filter (contact_widget,
453                                                    can_add_contact_to_account,
454                                                    NULL);
455         gtk_widget_show (contact_widget);
456
457         new_contact_dialog = dialog;
458
459         g_signal_connect (dialog, "response",
460                           G_CALLBACK (new_contact_response_cb),
461                           contact_widget);
462
463         if (parent) {
464                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
465         }
466
467         gtk_widget_show (dialog);
468 }
469