]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Port EmpathyContactDialogs to new API.
[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 GtkWidget *new_contact_dialog = NULL;
44
45
46 static gint
47 contact_dialogs_find (GtkDialog      *dialog,
48                       EmpathyContact *contact)
49 {
50         GtkWidget     *contact_widget;
51         EmpathyContact *this_contact;
52
53         contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
54         this_contact = empathy_contact_widget_get_contact (contact_widget);
55
56         return contact != this_contact;
57 }
58
59 /*
60  *  Subscription dialog
61  */
62
63 static void
64 subscription_dialog_response_cb (GtkDialog *dialog,
65                                  gint       response,
66                                  GtkWidget *contact_widget)
67 {
68         EmpathyContactManager *manager;
69         EmpathyContact        *contact;
70
71         manager = empathy_contact_manager_dup_singleton ();
72         contact = empathy_contact_widget_get_contact (contact_widget);
73
74         if (response == GTK_RESPONSE_YES) {
75                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
76                                           contact, "");
77         }
78         else if (response == GTK_RESPONSE_NO) {
79                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
80                                              contact, "");
81         }
82
83         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
84         gtk_widget_destroy (GTK_WIDGET (dialog));
85         g_object_unref (manager);
86 }
87
88 void
89 empathy_subscription_dialog_show (EmpathyContact *contact,
90                                   GtkWindow     *parent)
91 {
92         GtkBuilder *gui;
93         GtkWidget *dialog;
94         GtkWidget *hbox_subscription;
95         GtkWidget *contact_widget;
96         GList     *l;
97         gchar     *filename;
98
99         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
100
101         l = g_list_find_custom (subscription_dialogs,
102                                 contact,
103                                 (GCompareFunc) contact_dialogs_find);
104         if (l) {
105                 gtk_window_present (GTK_WINDOW (l->data));
106                 return;
107         }
108
109         filename = empathy_file_lookup ("empathy-contact-dialogs.ui",
110                                         "libempathy-gtk");
111         gui = empathy_builder_get_file (filename,
112                                       "subscription_request_dialog", &dialog,
113                                       "hbox_subscription", &hbox_subscription,
114                                       NULL);
115         g_free (filename);
116         g_object_unref (gui);
117
118         contact_widget = empathy_contact_widget_new (contact,
119                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
120                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
121         gtk_box_pack_end (GTK_BOX (hbox_subscription),
122                           contact_widget,
123                           TRUE, TRUE,
124                           0);
125         gtk_widget_show (contact_widget);
126
127
128         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
129         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
130
131         g_signal_connect (dialog, "response",
132                           G_CALLBACK (subscription_dialog_response_cb),
133                           contact_widget);
134
135         if (parent) {
136                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
137         }
138
139         gtk_widget_show (dialog);
140 }
141
142 /*
143  *  Information dialog
144  */
145
146 static void
147 contact_information_response_cb (GtkDialog *dialog,
148                                  gint       response,
149                                  GtkWidget *contact_widget)
150 {
151         information_dialogs = g_list_remove (information_dialogs, dialog);
152         gtk_widget_destroy (GTK_WIDGET (dialog));
153 }
154
155 void
156 empathy_contact_information_dialog_show (EmpathyContact *contact,
157                                          GtkWindow      *parent,
158                                          gboolean        edit,
159                                          gboolean        is_user)
160 {
161         GtkWidget                *dialog;
162         GtkWidget                *button;
163         GtkWidget                *contact_widget;
164         GList                    *l;
165         EmpathyContactWidgetFlags flags = 0;
166
167         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
168
169         l = g_list_find_custom (information_dialogs,
170                                 contact,
171                                 (GCompareFunc) contact_dialogs_find);
172         if (l) {
173                 gtk_window_present (GTK_WINDOW (l->data));
174                 return;
175         }
176
177         /* Create dialog */
178         dialog = gtk_dialog_new ();
179         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
180         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
181         if (is_user) {
182                 gtk_window_set_title (GTK_WINDOW (dialog), _("Personal Information"));
183         }
184         else if (edit) {
185                 gtk_window_set_title (GTK_WINDOW (dialog), _("Edit Contact Information"));
186         }
187         else {
188                 gtk_window_set_title (GTK_WINDOW (dialog), _("Contact Information"));
189         }
190
191         /* Close button */
192         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
193         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
194         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
195                                       button,
196                                       GTK_RESPONSE_CLOSE);
197         GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
198         gtk_window_set_default (GTK_WINDOW (dialog), button);
199         gtk_widget_show (button);
200
201         /* Contact info widget */
202         if (edit) {
203                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ALIAS;
204         }
205         if (is_user) {
206                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT;
207                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_AVATAR;
208         }
209         if (!is_user && edit) {
210                 flags |= EMPATHY_CONTACT_WIDGET_EDIT_GROUPS;
211         }
212         contact_widget = empathy_contact_widget_new (contact, flags);
213         gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
214         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
215                             contact_widget,
216                             TRUE, TRUE, 0);
217         if (flags & EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT) {
218                 empathy_contact_widget_set_account_filter (contact_widget,
219                                                            empathy_account_chooser_filter_is_connected,
220                                                            NULL);
221         }
222         gtk_widget_show (contact_widget);
223
224         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
225         information_dialogs = g_list_prepend (information_dialogs, dialog);
226
227         g_signal_connect (dialog, "response",
228                           G_CALLBACK (contact_information_response_cb),
229                           contact_widget);
230
231         if (parent) {
232                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
233         }
234
235         gtk_widget_show (dialog);
236 }
237
238 /*
239  *  New contact dialog
240  */
241
242 static gboolean
243 can_add_contact_to_account (McAccount *account,
244                             gpointer   user_data)
245 {
246         EmpathyAccountManager *account_manager;
247         EmpathyContactManager *contact_manager;
248         TpConnection          *connection;
249         gboolean               result;
250
251         account_manager = empathy_account_manager_dup_singleton ();
252         connection = empathy_account_manager_get_connection (account_manager,
253                                                              account);
254         if (!connection) {
255                 g_object_unref (account_manager);
256                 return FALSE;
257         }
258
259         contact_manager = empathy_contact_manager_dup_singleton ();
260         result = empathy_contact_manager_can_add (contact_manager, connection);
261         g_object_unref (contact_manager);
262         g_object_unref (account_manager);
263
264         return result;
265 }
266
267 static void
268 new_contact_response_cb (GtkDialog *dialog,
269                          gint       response,
270                          GtkWidget *contact_widget)
271 {
272         EmpathyContactManager *manager;
273         EmpathyContact         *contact;
274
275         manager = empathy_contact_manager_dup_singleton ();
276         contact = empathy_contact_widget_get_contact (contact_widget);
277
278         if (contact && response == GTK_RESPONSE_OK) {
279                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
280                                           contact,
281                                           _("I would like to add you to my contact list."));
282         }
283
284         new_contact_dialog = NULL;
285         gtk_widget_destroy (GTK_WIDGET (dialog));
286         g_object_unref (manager);
287 }
288
289 void
290 empathy_new_contact_dialog_show (GtkWindow *parent)
291 {
292         GtkWidget *dialog;
293         GtkWidget *button;
294         GtkWidget *contact_widget;
295
296         if (new_contact_dialog) {
297                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
298                 return;
299         }
300
301         /* Create dialog */
302         dialog = gtk_dialog_new ();
303         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
304         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
305         gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
306
307         /* Cancel button */
308         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
309         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
310         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
311                                       button,
312                                       GTK_RESPONSE_CANCEL);
313         gtk_widget_show (button);
314         
315         /* Add button */
316         button = gtk_button_new_with_label (GTK_STOCK_ADD);
317         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
318         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
319                                       button,
320                                       GTK_RESPONSE_OK);
321         gtk_widget_show (button);
322
323         /* Contact info widget */
324         contact_widget = empathy_contact_widget_new (NULL,
325                                                      EMPATHY_CONTACT_WIDGET_EDIT_ALIAS |
326                                                      EMPATHY_CONTACT_WIDGET_EDIT_ACCOUNT |
327                                                      EMPATHY_CONTACT_WIDGET_EDIT_ID |
328                                                      EMPATHY_CONTACT_WIDGET_EDIT_GROUPS);
329         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
330                             contact_widget,
331                             TRUE, TRUE, 0);
332         empathy_contact_widget_set_account_filter (contact_widget,
333                                                    can_add_contact_to_account,
334                                                    NULL);
335         gtk_widget_show (contact_widget);
336
337         new_contact_dialog = dialog;
338
339         g_signal_connect (dialog, "response",
340                           G_CALLBACK (new_contact_response_cb),
341                           contact_widget);
342
343         if (parent) {
344                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
345         }
346
347         gtk_widget_show (dialog);
348 }
349