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