1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Copyright (C) 2001-2004 Novell, Inc.
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.
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.
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.
24 #include "e2k-user-dialog.h"
25 #include "e2k-types.h"
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"
35 struct _E2kUserDialogPrivate {
37 ENameSelector *name_selector;
38 GtkWidget *entry, *parent_window;
41 #define PARENT_TYPE GTK_TYPE_DIALOG
42 static GtkDialogClass *parent_class;
44 static void parent_window_destroyed (gpointer dialog, GObject *where_parent_window_was);
47 finalize (GObject *object)
49 E2kUserDialog *dialog = E2K_USER_DIALOG (object);
51 g_free (dialog->priv->section_name);
52 g_free (dialog->priv);
54 G_OBJECT_CLASS (parent_class)->finalize (object);
58 dispose (GObject *object)
60 E2kUserDialog *dialog = E2K_USER_DIALOG (object);
62 if (dialog->priv->name_selector != NULL) {
63 g_object_unref (dialog->priv->name_selector);
64 dialog->priv->name_selector = NULL;
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;
73 G_OBJECT_CLASS (parent_class)->dispose (object);
77 class_init (E2kUserDialogClass *class)
79 GObjectClass *object_class = (GObjectClass *) class;
81 parent_class = g_type_class_ref (GTK_TYPE_DIALOG);
83 object_class->dispose = dispose;
84 object_class->finalize = finalize;
88 init (E2kUserDialog *dialog)
90 dialog->priv = g_new0 (E2kUserDialogPrivate, 1);
92 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 6);
95 E2K_MAKE_TYPE (e2k_user_dialog, E2kUserDialog, class_init, init, PARENT_TYPE)
100 parent_window_destroyed (gpointer user_data, GObject *where_parent_window_was)
102 E2kUserDialog *dialog = user_data;
104 dialog->priv->parent_window = NULL;
105 gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_CANCEL);
109 addressbook_dialog_response (ENameSelectorDialog *name_selector_dialog, gint response, gpointer data)
111 gtk_widget_hide (GTK_WIDGET (name_selector_dialog));
115 addressbook_clicked_cb (GtkWidget *widget, gpointer data)
117 E2kUserDialog *dialog = data;
118 E2kUserDialogPrivate *priv;
119 ENameSelectorDialog *name_selector_dialog;
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));
129 e2k_user_dialog_construct (E2kUserDialog *dialog,
130 GtkWidget *parent_window,
131 const char *label_text,
132 const char *section_name)
134 E2kUserDialogPrivate *priv;
135 GtkWidget *hbox, *vbox, *label, *button;
136 ENameSelectorModel *name_selector_model;
137 ENameSelectorDialog *name_selector_dialog;
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,
145 // SURF :e_dialog_set_transient_for (GTK_WINDOW (dialog), parent_window);
148 priv->section_name = g_strdup (section_name);
150 priv->parent_window = parent_window;
151 g_object_weak_ref (G_OBJECT (parent_window),
152 parent_window_destroyed, dialog);
154 /* Set up the actual select names bits */
155 priv->name_selector = e_name_selector_new ();
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);
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);
166 hbox = gtk_hbox_new (FALSE, 6);
168 label = gtk_label_new (label_text);
169 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 6);
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);
177 button = gtk_button_new_with_label (_("Addressbook..."));
178 g_signal_connect (button, "clicked",
179 G_CALLBACK (addressbook_clicked_cb),
181 gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 6);
183 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
185 gtk_widget_show_all (hbox);
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
196 * Creates a new user selection dialog.
198 * Return value: A newly-created user selection dialog, or %NULL if
199 * the dialog could not be created.
202 e2k_user_dialog_new (GtkWidget *parent_window,
203 const char *label_text, const char *section_name)
205 E2kUserDialog *dialog;
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);
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));
218 return GTK_WIDGET (dialog);
222 * e2k_user_dialog_get_user:
223 * @dialog: the dialog
225 * Gets the email address of the selected user from the dialog.
227 * Return value: the email address, which must be freed with g_free().
230 e2k_user_dialog_get_user (E2kUserDialog *dialog)
232 E2kUserDialogPrivate *priv;
233 EDestinationStore *destination_store;
235 EDestination *destination;
236 gchar *result = NULL;
238 g_return_val_if_fail (E2K_IS_USER_DIALOG (dialog), NULL);
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);
247 destination = destinations->data;
248 result = g_strdup (e_destination_get_email (destination));
249 g_list_free (destinations);