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