]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
We can now add a new contact. EmpathyContactWidget can change the contact
[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 Collabora Ltd.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include <gtk/gtk.h>
29 #include <glade/glade.h>
30 #include <glib/gi18n.h>
31
32 #include <libempathy/empathy-contact-manager.h>
33 #include <libempathy/empathy-contact-list.h>
34
35 #include "empathy-contact-dialogs.h"
36 #include "empathy-contact-widget.h"
37 #include "gossip-ui-utils.h"
38
39 static GList *subscription_dialogs = NULL;
40 static GList *information_dialogs = NULL;
41 static GtkWidget *new_contact_dialog = NULL;
42
43
44 static gint
45 contact_dialogs_find (GtkDialog     *dialog,
46                       GossipContact *contact)
47 {
48         GtkWidget     *contact_widget;
49         GossipContact *this_contact;
50
51         contact_widget = g_object_get_data (G_OBJECT (dialog), "contact_widget");
52         this_contact = empathy_contact_widget_get_contact (contact_widget);
53
54         return !gossip_contact_equal (contact, this_contact);
55 }
56
57 /*
58  *  Subscription dialog
59  */
60
61 static void
62 subscription_dialog_response_cb (GtkDialog *dialog,
63                                  gint       response,
64                                  GtkWidget *contact_widget)
65 {
66         EmpathyContactManager *manager;
67         GossipContact         *contact;
68
69         manager = empathy_contact_manager_new ();
70         contact = empathy_contact_widget_get_contact (contact_widget);
71
72         if (response == GTK_RESPONSE_YES) {
73                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
74                                           contact,
75                                           _("I would like to add you to my contact list."));
76         }
77         else if (response == GTK_RESPONSE_NO) {
78                 empathy_contact_list_remove (EMPATHY_CONTACT_LIST (manager),
79                                              contact,
80                                              _("Sorry, I don't want you in my contact list."));
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 (GossipContact *contact,
90                                   GtkWindow     *parent)
91 {
92         GtkWidget *dialog;
93         GtkWidget *hbox_subscription;
94         GtkWidget *contact_widget;
95         GList     *l;
96
97         g_return_if_fail (GOSSIP_IS_CONTACT (contact));
98
99         l = g_list_find_custom (subscription_dialogs,
100                                 contact,
101                                 (GCompareFunc) contact_dialogs_find);
102         if (l) {
103                 gtk_window_present (GTK_WINDOW (l->data));
104                 return;
105         }
106
107         gossip_glade_get_file_simple ("empathy-contact-dialogs.glade",
108                                       "subscription_request_dialog",
109                                       NULL,
110                                       "subscription_request_dialog", &dialog,
111                                       "hbox_subscription", &hbox_subscription,
112                                       NULL);
113
114         contact_widget = empathy_contact_widget_new (contact, TRUE);
115         gtk_box_pack_end (GTK_BOX (hbox_subscription),
116                           contact_widget,
117                           TRUE, TRUE,
118                           0);
119
120         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
121         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
122
123         g_signal_connect (dialog, "response",
124                           G_CALLBACK (subscription_dialog_response_cb),
125                           contact_widget);
126
127         if (parent) {
128                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
129         }
130
131         gtk_widget_show (dialog);
132 }
133
134 /*
135  *  Information dialog
136  */
137
138 static void
139 contact_information_response_cb (GtkDialog *dialog,
140                                  gint       response,
141                                  GtkWidget *contact_widget)
142 {
143         information_dialogs = g_list_remove (information_dialogs, dialog);
144         gtk_widget_destroy (GTK_WIDGET (dialog));
145 }
146
147 void
148 empathy_contact_information_dialog_show (GossipContact *contact,
149                                          GtkWindow     *parent,
150                                          gboolean       edit)
151 {
152         GtkWidget *dialog;
153         GtkWidget *button;
154         GtkWidget *contact_widget;
155         GList     *l;
156
157         g_return_if_fail (GOSSIP_IS_CONTACT (contact));
158
159         l = g_list_find_custom (information_dialogs,
160                                 contact,
161                                 (GCompareFunc) contact_dialogs_find);
162         if (l) {
163                 gtk_window_present (GTK_WINDOW (l->data));
164                 return;
165         }
166
167         /* Create dialog */
168         dialog = gtk_dialog_new ();
169         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
170         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
171
172         /* Close button */
173         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
174         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
175         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
176                                       button,
177                                       GTK_RESPONSE_CLOSE);
178         gtk_widget_show (button);
179         
180         /* Contact infor widget */
181         contact_widget = empathy_contact_widget_new (contact, edit);
182         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
183                             contact_widget,
184                             TRUE, TRUE, 0);
185
186         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
187         information_dialogs = g_list_prepend (information_dialogs, dialog);
188
189         g_signal_connect (dialog, "response",
190                           G_CALLBACK (contact_information_response_cb),
191                           contact_widget);
192
193         if (parent) {
194                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
195         }
196
197         gtk_widget_show (dialog);
198 }
199
200 /*
201  *  New contact dialog
202  */
203
204 static void
205 new_contact_response_cb (GtkDialog *dialog,
206                          gint       response,
207                          GtkWidget *contact_widget)
208 {
209         EmpathyContactManager *manager;
210         GossipContact         *contact;
211
212         manager = empathy_contact_manager_new ();
213         contact = empathy_contact_widget_get_contact (contact_widget);
214
215         if (contact && response == GTK_RESPONSE_OK) {
216                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
217                                           contact,
218                                           _("I would like to add you to my contact list."));
219         }
220
221         new_contact_dialog = NULL;
222         gtk_widget_destroy (GTK_WIDGET (dialog));
223         g_object_unref (manager);
224 }
225
226 void
227 empathy_new_contact_dialog_show (GtkWindow *parent)
228 {
229         GtkWidget *dialog;
230         GtkWidget *button;
231         GtkWidget *contact_widget;
232
233         if (new_contact_dialog) {
234                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
235                 return;
236         }
237
238         /* Create dialog */
239         dialog = gtk_dialog_new ();
240         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
241         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
242
243         /* Cancel button */
244         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
245         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
246         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
247                                       button,
248                                       GTK_RESPONSE_CANCEL);
249         gtk_widget_show (button);
250         
251         /* Add button */
252         button = gtk_button_new_with_label (GTK_STOCK_ADD);
253         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
254         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
255                                       button,
256                                       GTK_RESPONSE_OK);
257         gtk_widget_show (button);
258
259         /* Contact infor widget */
260         contact_widget = empathy_contact_widget_new (NULL, TRUE);
261         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
262                             contact_widget,
263                             TRUE, TRUE, 0);
264
265         new_contact_dialog = dialog;
266
267         g_signal_connect (dialog, "response",
268                           G_CALLBACK (new_contact_response_cb),
269                           contact_widget);
270
271         if (parent) {
272                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
273         }
274
275         gtk_widget_show (dialog);
276 }
277