]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Updated Polish translation
[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                 EMPATHY_CONTACT_WIDGET_EDIT_FAVOURITE);
266         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
267         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
268                             contact_widget,
269                             TRUE, TRUE, 0);
270         gtk_widget_show (contact_widget);
271
272         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
273         edit_dialogs = g_list_prepend (edit_dialogs, dialog);
274
275         g_signal_connect (dialog, "response",
276                           G_CALLBACK (contact_dialogs_response_cb),
277                           &edit_dialogs);
278
279         if (parent) {
280                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
281         }
282
283         gtk_widget_show (dialog);
284 }
285
286 void
287 empathy_contact_personal_dialog_show (GtkWindow *parent)
288 {
289         GtkWidget *button;
290         GtkWidget *contact_widget;
291
292         if (personal_dialog) {
293                 gtk_window_present (GTK_WINDOW (personal_dialog));
294                 return;
295         }
296
297         /* Create dialog */
298         personal_dialog = gtk_dialog_new ();
299         gtk_dialog_set_has_separator (GTK_DIALOG (personal_dialog), FALSE);
300         gtk_window_set_resizable (GTK_WINDOW (personal_dialog), FALSE);
301         gtk_window_set_title (GTK_WINDOW (personal_dialog), _("Personal Information"));
302
303         /* Close button */
304         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
305         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
306         gtk_dialog_add_action_widget (GTK_DIALOG (personal_dialog),
307                                       button,
308                                       GTK_RESPONSE_CLOSE);
309         gtk_widget_set_can_default (button, TRUE);
310         gtk_window_set_default (GTK_WINDOW (personal_dialog), button);
311         gtk_widget_show (button);
312
313         /* Contact info widget */
314         contact_widget = empathy_contact_widget_new (NULL,
315                 EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
316                 EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
317                 EMPATHY_CONTACT_WIDGET_EDIT_AVATAR);
318         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
319         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (personal_dialog))),
320                             contact_widget,
321                             TRUE, TRUE, 0);
322         empathy_contact_widget_set_account_filter (contact_widget,
323                 empathy_account_chooser_filter_is_connected, NULL);
324         gtk_widget_show (contact_widget);
325
326         g_signal_connect (personal_dialog, "response",
327                           G_CALLBACK (gtk_widget_destroy), NULL);
328         g_object_add_weak_pointer (G_OBJECT (personal_dialog),
329                                    (gpointer) &personal_dialog);
330
331         if (parent) {
332                 gtk_window_set_transient_for (GTK_WINDOW (personal_dialog), parent);
333         }
334
335         gtk_widget_show (personal_dialog);
336 }
337
338 /*
339  *  New contact dialog
340  */
341
342 static gboolean
343 can_add_contact_to_account (TpAccount *account,
344                             gpointer   user_data)
345 {
346         EmpathyContactManager *contact_manager;
347         TpConnection          *connection;
348         gboolean               result;
349
350         connection = tp_account_get_connection (account);
351         if (connection == NULL)
352                 return FALSE;
353
354         contact_manager = empathy_contact_manager_dup_singleton ();
355         result = empathy_contact_manager_get_flags_for_connection (
356                 contact_manager, connection) & EMPATHY_CONTACT_LIST_CAN_ADD;
357         g_object_unref (contact_manager);
358
359         return result;
360 }
361
362 static void
363 new_contact_response_cb (GtkDialog *dialog,
364                          gint       response,
365                          GtkWidget *contact_widget)
366 {
367         EmpathyContactManager *manager;
368         EmpathyContact         *contact;
369
370         manager = empathy_contact_manager_dup_singleton ();
371         contact = empathy_contact_widget_get_contact (contact_widget);
372
373         if (contact && response == GTK_RESPONSE_OK) {
374                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
375                                           contact, "");
376         }
377
378         new_contact_dialog = NULL;
379         gtk_widget_destroy (GTK_WIDGET (dialog));
380         g_object_unref (manager);
381 }
382
383 void
384 empathy_new_contact_dialog_show (GtkWindow *parent)
385 {
386         empathy_new_contact_dialog_show_with_contact (parent, NULL);
387 }
388
389 void
390 empathy_new_contact_dialog_show_with_contact (GtkWindow *parent,
391                                               EmpathyContact *contact)
392 {
393         GtkWidget *dialog;
394         GtkWidget *button;
395         GtkWidget *contact_widget;
396
397         if (new_contact_dialog) {
398                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
399                 return;
400         }
401
402         /* Create dialog */
403         dialog = gtk_dialog_new ();
404         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
405         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
406         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
407
408         /* Cancel button */
409         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
410         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
411         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
412                                       button,
413                                       GTK_RESPONSE_CANCEL);
414         gtk_widget_show (button);
415
416         /* Add button */
417         button = gtk_button_new_with_label (GTK_STOCK_ADD);
418         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
419         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
420                                       button,
421                                       GTK_RESPONSE_OK);
422         gtk_widget_show (button);
423
424         /* Contact info widget */
425         contact_widget = empathy_contact_widget_new (contact,
426                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
427                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
428                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
429                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
430         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
431         gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
432                             contact_widget,
433                             TRUE, TRUE, 0);
434         empathy_contact_widget_set_account_filter (contact_widget,
435                                                    can_add_contact_to_account,
436                                                    NULL);
437         gtk_widget_show (contact_widget);
438
439         new_contact_dialog = dialog;
440
441         g_signal_connect (dialog, "response",
442                           G_CALLBACK (new_contact_response_cb),
443                           contact_widget);
444
445         if (parent) {
446                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
447         }
448
449         gtk_widget_show (dialog);
450 }
451