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