]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Merge remote branch 'vminko/fix-573283'
[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_window_set_resizable (GTK_WINDOW (dialog), FALSE);
182         gtk_window_set_title (GTK_WINDOW (dialog),
183                 empathy_contact_get_alias (contact));
184
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_set_can_default (button, TRUE);
192         gtk_window_set_default (GTK_WINDOW (dialog), button);
193         gtk_widget_show (button);
194
195         /* Contact info widget */
196         contact_widget = empathy_contact_widget_new (contact,
197                 EMPATHY_CONTACT_WIDGET_SHOW_LOCATION |
198                 EMPATHY_CONTACT_WIDGET_SHOW_DETAILS);
199         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
200         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
201                             contact_widget,
202                             TRUE, TRUE, 0);
203         gtk_widget_show (contact_widget);
204
205         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
206         information_dialogs = g_list_prepend (information_dialogs, dialog);
207
208         g_signal_connect (dialog, "response",
209                           G_CALLBACK (contact_dialogs_response_cb),
210                           &information_dialogs);
211
212         if (parent) {
213                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
214         }
215
216         gtk_widget_show (dialog);
217 }
218
219 void
220 empathy_contact_edit_dialog_show (EmpathyContact *contact,
221                                   GtkWindow      *parent)
222 {
223         GtkWidget *dialog;
224         GtkWidget *button;
225         GtkWidget *contact_widget;
226         GList     *l;
227
228         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
229
230         l = g_list_find_custom (edit_dialogs,
231                                 contact,
232                                 (GCompareFunc) contact_dialogs_find);
233         if (l) {
234                 gtk_window_present (GTK_WINDOW (l->data));
235                 return;
236         }
237
238         /* Create dialog */
239         dialog = gtk_dialog_new ();
240         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
241         gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
242
243         /* Close button */
244         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
245         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
246         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
247                                       button,
248                                       GTK_RESPONSE_CLOSE);
249         gtk_widget_set_can_default (button, TRUE);
250         gtk_window_set_default (GTK_WINDOW (dialog), button);
251         gtk_widget_show (button);
252
253         /* Contact info widget */
254         contact_widget = empathy_contact_widget_new (contact,
255                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
256                 EMPATHY_CONTACT_WIDGET_EDIT_GROUPS |
257                 EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE);
258         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
259         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
260                             contact_widget,
261                             TRUE, TRUE, 0);
262         gtk_widget_show (contact_widget);
263
264         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
265         edit_dialogs = g_list_prepend (edit_dialogs, dialog);
266
267         g_signal_connect (dialog, "response",
268                           G_CALLBACK (contact_dialogs_response_cb),
269                           &edit_dialogs);
270
271         if (parent) {
272                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
273         }
274
275         gtk_widget_show (dialog);
276 }
277
278 void
279 empathy_contact_personal_dialog_show (GtkWindow *parent)
280 {
281         GtkWidget *button;
282         GtkWidget *contact_widget;
283
284         if (personal_dialog) {
285                 gtk_window_present (GTK_WINDOW (personal_dialog));
286                 return;
287         }
288
289         /* Create dialog */
290         personal_dialog = gtk_dialog_new ();
291         gtk_window_set_resizable (GTK_WINDOW (personal_dialog), FALSE);
292         gtk_window_set_title (GTK_WINDOW (personal_dialog), _("Personal Information"));
293
294         /* Close button */
295         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
296         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
297         gtk_dialog_add_action_widget (GTK_DIALOG (personal_dialog),
298                                       button,
299                                       GTK_RESPONSE_CLOSE);
300         gtk_widget_set_can_default (button, TRUE);
301         gtk_window_set_default (GTK_WINDOW (personal_dialog), button);
302         gtk_widget_show (button);
303
304         /* Contact info widget */
305         contact_widget = empathy_contact_widget_new (NULL,
306                 EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
307                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
308                 EMPATHY_CONTACT_WIDGET_EDIT_AVATAR |
309                 EMPATHY_CONTACT_WIDGET_EDIT_DETAILS);
310         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
311         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (personal_dialog))),
312                             contact_widget,
313                             TRUE, TRUE, 0);
314         empathy_contact_widget_set_account_filter (contact_widget,
315                 empathy_account_chooser_filter_is_connected, NULL);
316         gtk_widget_show (contact_widget);
317
318         g_signal_connect (personal_dialog, "response",
319                           G_CALLBACK (gtk_widget_destroy), NULL);
320         g_object_add_weak_pointer (G_OBJECT (personal_dialog),
321                                    (gpointer) &personal_dialog);
322
323         if (parent) {
324                 gtk_window_set_transient_for (GTK_WINDOW (personal_dialog), parent);
325         }
326
327         gtk_widget_show (personal_dialog);
328 }
329
330 /*
331  *  New contact dialog
332  */
333
334 static gboolean
335 can_add_contact_to_account (TpAccount *account,
336                             gpointer   user_data)
337 {
338         EmpathyContactManager *contact_manager;
339         TpConnection          *connection;
340         gboolean               result;
341
342         connection = tp_account_get_connection (account);
343         if (connection == NULL)
344                 return FALSE;
345
346         contact_manager = empathy_contact_manager_dup_singleton ();
347         result = empathy_contact_manager_get_flags_for_connection (
348                 contact_manager, connection) & EMPATHY_CONTACT_LIST_CAN_ADD;
349         g_object_unref (contact_manager);
350
351         return result;
352 }
353
354 static void
355 new_contact_response_cb (GtkDialog *dialog,
356                          gint       response,
357                          GtkWidget *contact_widget)
358 {
359         EmpathyContactManager *manager;
360         EmpathyContact         *contact;
361
362         manager = empathy_contact_manager_dup_singleton ();
363         contact = empathy_contact_widget_get_contact (contact_widget);
364
365         if (contact && response == GTK_RESPONSE_OK) {
366                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
367                                           contact, "");
368         }
369
370         new_contact_dialog = NULL;
371         gtk_widget_destroy (GTK_WIDGET (dialog));
372         g_object_unref (manager);
373 }
374
375 void
376 empathy_new_contact_dialog_show (GtkWindow *parent)
377 {
378         empathy_new_contact_dialog_show_with_contact (parent, NULL);
379 }
380
381 void
382 empathy_new_contact_dialog_show_with_contact (GtkWindow *parent,
383                                               EmpathyContact *contact)
384 {
385         GtkWidget *dialog;
386         GtkWidget *button;
387         GtkWidget *contact_widget;
388
389         if (new_contact_dialog) {
390                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
391                 return;
392         }
393
394         /* Create dialog */
395         dialog = gtk_dialog_new ();
396         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
397         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
398
399         /* Cancel button */
400         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
401         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
402         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
403                                       button,
404                                       GTK_RESPONSE_CANCEL);
405         gtk_widget_show (button);
406
407         /* Add button */
408         button = gtk_button_new_with_label (GTK_STOCK_ADD);
409         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
410         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
411                                       button,
412                                       GTK_RESPONSE_OK);
413         gtk_widget_show (button);
414
415         /* Contact info widget */
416         contact_widget = empathy_contact_widget_new (contact,
417                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
418                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
419                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
420                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
421         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
422         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
423                             contact_widget,
424                             TRUE, TRUE, 0);
425         empathy_contact_widget_set_account_filter (contact_widget,
426                                                    can_add_contact_to_account,
427                                                    NULL);
428         gtk_widget_show (contact_widget);
429
430         new_contact_dialog = dialog;
431
432         g_signal_connect (dialog, "response",
433                           G_CALLBACK (new_contact_response_cb),
434                           contact_widget);
435
436         if (parent) {
437                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
438         }
439
440         gtk_widget_show (dialog);
441 }
442