Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / exchange / lib / e2k-user-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2001-2004 Novell, Inc.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU Lesser General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "e2k-user-dialog.h"
25 #include "e2k-types.h"
26
27 #include <gtk/gtkbutton.h>
28 #include <gtk/gtkhbox.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkstock.h>
31 #include <gtk/gtkvbox.h>
32 #include <libedataserverui/e-name-selector.h>
33 #include "e2k-xml-utils.h"
34
35 struct _E2kUserDialogPrivate {
36         char *section_name;
37         ENameSelector *name_selector;
38         GtkWidget *entry, *parent_window;
39 };
40
41 #define PARENT_TYPE GTK_TYPE_DIALOG
42 static GtkDialogClass *parent_class;
43
44 static void parent_window_destroyed (gpointer dialog, GObject *where_parent_window_was);
45
46 static void
47 finalize (GObject *object)
48 {
49         E2kUserDialog *dialog = E2K_USER_DIALOG (object);
50
51         g_free (dialog->priv->section_name);
52         g_free (dialog->priv);
53
54         G_OBJECT_CLASS (parent_class)->finalize (object);
55 }
56
57 static void
58 dispose (GObject *object)
59 {
60         E2kUserDialog *dialog = E2K_USER_DIALOG (object);
61
62         if (dialog->priv->name_selector != NULL) {
63                 g_object_unref (dialog->priv->name_selector);
64                 dialog->priv->name_selector = NULL;
65         }
66
67         if (dialog->priv->parent_window) {
68                 g_object_weak_unref (G_OBJECT (dialog->priv->parent_window),
69                                      parent_window_destroyed, dialog);
70                 dialog->priv->parent_window = NULL;
71         }
72
73         G_OBJECT_CLASS (parent_class)->dispose (object);
74 }
75
76 static void
77 class_init (E2kUserDialogClass *class)
78 {
79         GObjectClass *object_class = (GObjectClass *) class;
80
81         parent_class = g_type_class_ref (GTK_TYPE_DIALOG);
82
83         object_class->dispose = dispose;
84         object_class->finalize = finalize;
85 }
86
87 static void
88 init (E2kUserDialog *dialog)
89 {
90         dialog->priv = g_new0 (E2kUserDialogPrivate, 1);
91
92         gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 6);
93 }
94
95 E2K_MAKE_TYPE (e2k_user_dialog, E2kUserDialog, class_init, init, PARENT_TYPE)
96
97
98
99 static void
100 parent_window_destroyed (gpointer user_data, GObject *where_parent_window_was)
101 {
102         E2kUserDialog *dialog = user_data;
103
104         dialog->priv->parent_window = NULL;
105         gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
106 }
107
108 static void
109 addressbook_dialog_response (ENameSelectorDialog *name_selector_dialog, gint response, gpointer data)
110 {
111         gtk_widget_hide (GTK_WIDGET (name_selector_dialog));
112 }
113
114 static void
115 addressbook_clicked_cb (GtkWidget *widget, gpointer data)
116 {
117         E2kUserDialog *dialog = data;
118         E2kUserDialogPrivate *priv;
119         ENameSelectorDialog *name_selector_dialog;
120
121         priv = dialog->priv;
122
123         name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
124         gtk_window_set_modal (GTK_WINDOW (dialog), FALSE);
125         gtk_widget_show (GTK_WIDGET (name_selector_dialog));
126 }
127
128 static gboolean
129 e2k_user_dialog_construct (E2kUserDialog *dialog,
130                            GtkWidget *parent_window,
131                            const char *label_text,
132                            const char *section_name)
133 {
134         E2kUserDialogPrivate *priv;
135         GtkWidget *hbox, *vbox, *label, *button;
136         ENameSelectorModel *name_selector_model;
137         ENameSelectorDialog *name_selector_dialog;
138
139         gtk_window_set_title (GTK_WINDOW (dialog), _("Select User"));
140         gtk_dialog_add_buttons (GTK_DIALOG (dialog),
141                                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
142                                 GTK_STOCK_OK, GTK_RESPONSE_OK,
143                                 NULL);
144
145         // SURF :e_dialog_set_transient_for (GTK_WINDOW (dialog), parent_window);
146
147         priv = dialog->priv;
148         priv->section_name = g_strdup (section_name);
149
150         priv->parent_window = parent_window;
151         g_object_weak_ref (G_OBJECT (parent_window),
152                            parent_window_destroyed, dialog);
153
154         /* Set up the actual select names bits */
155         priv->name_selector = e_name_selector_new ();
156
157         /* Listen for responses whenever the dialog is shown */
158         name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);
159         g_signal_connect (name_selector_dialog, "response",
160                           G_CALLBACK (addressbook_dialog_response), dialog);
161
162         name_selector_model = e_name_selector_peek_model (priv->name_selector);
163         /* FIXME Limit to one user */
164         e_name_selector_model_add_section (name_selector_model, section_name, section_name, NULL);
165
166         hbox = gtk_hbox_new (FALSE, 6);
167
168         label = gtk_label_new (label_text);
169         gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 6);
170
171         /* The vbox is a workaround for bug 43315 */
172         vbox = gtk_vbox_new (FALSE, 0);
173         priv->entry = GTK_WIDGET (e_name_selector_peek_section_entry (priv->name_selector, section_name));
174         gtk_box_pack_start (GTK_BOX (vbox), priv->entry, TRUE, FALSE, 0);
175         gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 6);
176
177         button = gtk_button_new_with_label (_("Addressbook..."));
178         g_signal_connect (button, "clicked",
179                           G_CALLBACK (addressbook_clicked_cb),
180                           dialog);
181         gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 6);
182
183         gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
184                             TRUE, TRUE, 6);
185         gtk_widget_show_all (hbox);
186
187         return TRUE;
188 }
189
190 /**
191  * e2k_user_dialog_new:
192  * @parent_window: The window invoking the dialog.
193  * @label_text: Text to label the entry in the initial dialog with
194  * @section_name: The section name for the select-names dialog
195  *
196  * Creates a new user selection dialog.
197  *
198  * Return value: A newly-created user selection dialog, or %NULL if
199  * the dialog could not be created.
200  **/
201 GtkWidget *
202 e2k_user_dialog_new (GtkWidget *parent_window,
203                      const char *label_text, const char *section_name)
204 {
205         E2kUserDialog *dialog;
206
207         g_return_val_if_fail (GTK_IS_WINDOW (parent_window), NULL);
208         g_return_val_if_fail (label_text != NULL, NULL);
209         g_return_val_if_fail (section_name != NULL, NULL);
210
211         dialog = g_object_new (E2K_TYPE_USER_DIALOG, NULL);
212         if (!e2k_user_dialog_construct (dialog, parent_window,
213                                         label_text, section_name)) {
214                 gtk_widget_destroy (GTK_WIDGET (dialog));
215                 return NULL;
216         }
217
218         return GTK_WIDGET (dialog);
219 }
220
221 /**
222  * e2k_user_dialog_get_user:
223  * @dialog: the dialog
224  *
225  * Gets the email address of the selected user from the dialog.
226  *
227  * Return value: the email address, which must be freed with g_free().
228  **/
229 char *
230 e2k_user_dialog_get_user (E2kUserDialog *dialog)
231 {
232         E2kUserDialogPrivate *priv;
233         EDestinationStore *destination_store;
234         GList *destinations;
235         EDestination *destination;
236         gchar *result = NULL;
237
238         g_return_val_if_fail (E2K_IS_USER_DIALOG (dialog), NULL);
239
240         priv = dialog->priv;
241
242         destination_store = e_name_selector_entry_peek_destination_store (E_NAME_SELECTOR_ENTRY (priv->entry));
243         destinations = e_destination_store_list_destinations (destination_store);
244         if (!destinations)
245                 return NULL;
246
247         destination = destinations->data;
248         result = g_strdup (e_destination_get_email (destination));
249         g_list_free (destinations);
250
251         return result;
252 }