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