]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contact-dialogs.c
Rename all filenames starting with "gossip" by "empathy", change namespace
[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 "empathy-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                       EmpathyContact *contact)
47 {
48         GtkWidget     *contact_widget;
49         EmpathyContact *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 !empathy_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         EmpathyContact         *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_process_pending (EMPATHY_CONTACT_LIST (manager),
74                                                       contact, TRUE);
75         }
76         else if (response == GTK_RESPONSE_NO) {
77                 empathy_contact_list_process_pending (EMPATHY_CONTACT_LIST (manager),
78                                                       contact, FALSE);
79         }
80
81         subscription_dialogs = g_list_remove (subscription_dialogs, dialog);
82         gtk_widget_destroy (GTK_WIDGET (dialog));
83         g_object_unref (manager);
84 }
85
86 void
87 empathy_subscription_dialog_show (EmpathyContact *contact,
88                                   GtkWindow     *parent)
89 {
90         GtkWidget *dialog;
91         GtkWidget *hbox_subscription;
92         GtkWidget *contact_widget;
93         GList     *l;
94
95         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
96
97         l = g_list_find_custom (subscription_dialogs,
98                                 contact,
99                                 (GCompareFunc) contact_dialogs_find);
100         if (l) {
101                 gtk_window_present (GTK_WINDOW (l->data));
102                 return;
103         }
104
105         empathy_glade_get_file_simple ("empathy-contact-dialogs.glade",
106                                       "subscription_request_dialog",
107                                       NULL,
108                                       "subscription_request_dialog", &dialog,
109                                       "hbox_subscription", &hbox_subscription,
110                                       NULL);
111
112         contact_widget = empathy_contact_widget_new (contact, TRUE);
113         gtk_box_pack_end (GTK_BOX (hbox_subscription),
114                           contact_widget,
115                           TRUE, TRUE,
116                           0);
117
118         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
119         subscription_dialogs = g_list_prepend (subscription_dialogs, dialog);
120
121         g_signal_connect (dialog, "response",
122                           G_CALLBACK (subscription_dialog_response_cb),
123                           contact_widget);
124
125         if (parent) {
126                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
127         }
128
129         gtk_widget_show (dialog);
130 }
131
132 /*
133  *  Information dialog
134  */
135
136 static void
137 contact_information_response_cb (GtkDialog *dialog,
138                                  gint       response,
139                                  GtkWidget *contact_widget)
140 {
141         information_dialogs = g_list_remove (information_dialogs, dialog);
142         gtk_widget_destroy (GTK_WIDGET (dialog));
143 }
144
145 void
146 empathy_contact_information_dialog_show (EmpathyContact *contact,
147                                          GtkWindow     *parent,
148                                          gboolean       edit)
149 {
150         GtkWidget *dialog;
151         GtkWidget *button;
152         GtkWidget *contact_widget;
153         GList     *l;
154
155         g_return_if_fail (EMPATHY_IS_CONTACT (contact));
156
157         l = g_list_find_custom (information_dialogs,
158                                 contact,
159                                 (GCompareFunc) contact_dialogs_find);
160         if (l) {
161                 gtk_window_present (GTK_WINDOW (l->data));
162                 return;
163         }
164
165         /* Create dialog */
166         dialog = gtk_dialog_new ();
167         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
168         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
169
170         /* Close button */
171         button = gtk_button_new_with_label (GTK_STOCK_CLOSE);
172         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
173         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
174                                       button,
175                                       GTK_RESPONSE_CLOSE);
176         gtk_widget_show (button);
177         
178         /* Contact infor widget */
179         contact_widget = empathy_contact_widget_new (contact, edit);
180         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
181                             contact_widget,
182                             TRUE, TRUE, 0);
183
184         g_object_set_data (G_OBJECT (dialog), "contact_widget", contact_widget);
185         information_dialogs = g_list_prepend (information_dialogs, dialog);
186
187         g_signal_connect (dialog, "response",
188                           G_CALLBACK (contact_information_response_cb),
189                           contact_widget);
190
191         if (parent) {
192                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
193         }
194
195         gtk_widget_show (dialog);
196 }
197
198 /*
199  *  New contact dialog
200  */
201
202 static void
203 new_contact_response_cb (GtkDialog *dialog,
204                          gint       response,
205                          GtkWidget *contact_widget)
206 {
207         EmpathyContactManager *manager;
208         EmpathyContact         *contact;
209
210         manager = empathy_contact_manager_new ();
211         contact = empathy_contact_widget_get_contact (contact_widget);
212
213         if (contact && response == GTK_RESPONSE_OK) {
214                 empathy_contact_list_add (EMPATHY_CONTACT_LIST (manager),
215                                           contact,
216                                           _("I would like to add you to my contact list."));
217         }
218
219         new_contact_dialog = NULL;
220         gtk_widget_destroy (GTK_WIDGET (dialog));
221         g_object_unref (manager);
222 }
223
224 void
225 empathy_new_contact_dialog_show (GtkWindow *parent)
226 {
227         GtkWidget *dialog;
228         GtkWidget *button;
229         GtkWidget *contact_widget;
230
231         if (new_contact_dialog) {
232                 gtk_window_present (GTK_WINDOW (new_contact_dialog));
233                 return;
234         }
235
236         /* Create dialog */
237         dialog = gtk_dialog_new ();
238         gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
239         gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
240
241         /* Cancel button */
242         button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
243         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
244         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
245                                       button,
246                                       GTK_RESPONSE_CANCEL);
247         gtk_widget_show (button);
248         
249         /* Add button */
250         button = gtk_button_new_with_label (GTK_STOCK_ADD);
251         gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
252         gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
253                                       button,
254                                       GTK_RESPONSE_OK);
255         gtk_widget_show (button);
256
257         /* Contact infor widget */
258         contact_widget = empathy_contact_widget_new (NULL, TRUE);
259         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
260                             contact_widget,
261                             TRUE, TRUE, 0);
262
263         new_contact_dialog = dialog;
264
265         g_signal_connect (dialog, "response",
266                           G_CALLBACK (new_contact_response_cb),
267                           contact_widget);
268
269         if (parent) {
270                 gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);
271         }
272
273         gtk_widget_show (dialog);
274 }
275