1 /*
2  * Copyright (C) 2011 Vivien Malerba <malerba@gnome-db.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18 
19 #include <glib/gi18n-lib.h>
20 #include <string.h>
21 #include <gtk/gtk.h>
22 #include "vtable-dialog.h"
23 
24 #define SPACING 3
25 
26 struct _VtableDialogPrivate {
27 	BrowserConnection *bcnc;
28 	GtkWidget *tname_entry;
29 	GtkWidget *tname_replace;
30 };
31 
32 static void vtable_dialog_class_init (VtableDialogClass *klass);
33 static void vtable_dialog_init       (VtableDialog *dlg, VtableDialogClass *klass);
34 static void vtable_dialog_dispose   (GObject *object);
35 
36 static GObjectClass *parent_class = NULL;
37 
38 
39 /*
40  * VtableDialog class implementation
41  */
42 
43 static void
44 vtable_dialog_class_init (VtableDialogClass *klass)
45 {
46 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
47 	parent_class = g_type_class_peek_parent (klass);
48 
49 	object_class->dispose = vtable_dialog_dispose;
50 }
51 
52 
53 static void
54 vtable_dialog_init (VtableDialog *dlg, G_GNUC_UNUSED VtableDialogClass *klass)
55 {
56 	dlg->priv = g_new0 (VtableDialogPrivate, 1);
57 }
58 
59 static void
60 vtable_dialog_dispose (GObject *object)
61 {
62 	VtableDialog *dlg = (VtableDialog *) object;
63 
64 	/* free memory */
65 	if (dlg->priv) {
66 		if (dlg->priv->bcnc)
67 			g_object_unref (dlg->priv->bcnc);
68 		g_free (dlg->priv);
69 		dlg->priv = NULL;
70 	}
71 
strip_trailoptnull72 	parent_class->dispose (object);
73 }
74 
75 GType
76 vtable_dialog_get_type (void)
77 {
78 	static GType type = 0;
79 
80 	if (G_UNLIKELY (type == 0)) {
81 		static const GTypeInfo columns = {
82 			sizeof (VtableDialogClass),
83 			(GBaseInitFunc) NULL,
84 			(GBaseFinalizeFunc) NULL,
85 			(GClassInitFunc) vtable_dialog_class_init,
86 			NULL,
87 			NULL,
88 			sizeof (VtableDialog),
89 			0,
90 			(GInstanceInitFunc) vtable_dialog_init,
91 			0
92 		};
93 		type = g_type_register_static (GTK_TYPE_DIALOG, "VtableDialog", &columns, 0);
94 	}
95 	return type;
96 }
97 
98 /**
99  * vtable_dialog_new:
100  *
101  * Returns: a new #GtkWidget
102  */
103 GtkWidget *
104 vtable_dialog_new (GtkWindow *parent, BrowserConnection *bcnc)
105 {
106 	VtableDialog *dlg;
107 	g_return_val_if_fail (BROWSER_IS_CONNECTION (bcnc), NULL);
108 
109 	dlg = VTABLE_DIALOG (g_object_new (VTABLE_DIALOG_TYPE, NULL));
110 	dlg->priv->bcnc = g_object_ref (bcnc);
111 
112 	if (parent)
113 		gtk_window_set_transient_for (GTK_WINDOW (dlg), parent);
114 	gtk_window_set_modal (GTK_WINDOW (dlg), TRUE);
115 	gtk_container_set_border_width (GTK_CONTAINER (dlg), SPACING * 2);
116 	gtk_window_set_title (GTK_WINDOW (dlg), _("Define LDAP search as a virtual table"));
117 
118 	GtkWidget *dcontents;
119 	GtkWidget *label, *entry, *grid, *button;
120 	gchar *str;
121 	dcontents = gtk_dialog_get_content_area (GTK_DIALOG (dlg));
122 	label = gtk_label_new (NULL);
123 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
124 	str = g_markup_printf_escaped ("<b>%s:</b>\n<small>%s</small>",
125 				       _("Name of the virtual LDAP table to create"),
126 				       _("Everytime data is selected from the virtual table which will "
127 					 "be created, the LDAP search will be executed and data "
128 					 "returned as the contents of the table."));
129 	gtk_label_set_markup (GTK_LABEL (label), str);
130 	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
131 	g_free (str);
132 	gtk_box_pack_start (GTK_BOX (dcontents), label, FALSE, FALSE, SPACING);
133 
134 	grid = gtk_grid_new ();
135 	gtk_grid_set_column_spacing (GTK_GRID (grid), SPACING);
136 	gtk_grid_set_row_spacing (GTK_GRID (grid), SPACING);
137 	gtk_box_pack_start (GTK_BOX (dcontents), grid, FALSE, FALSE, SPACING);
138 
139 	label = gtk_label_new (_("Table name:"));
140 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
141 	gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
142 
143 	entry = gtk_entry_new ();
144 	gtk_grid_attach (GTK_GRID (grid), entry, 1, 0, 1, 1);
145 	dlg->priv->tname_entry = entry;
146 
147 	label = gtk_label_new (_("Replace if exists:"));
148 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
149 	gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
150 
151 	button = gtk_check_button_new ();
152 	gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 1, 1);
153 	dlg->priv->tname_replace = button;
154 
155 	gtk_widget_show_all (dcontents);
156 	gtk_dialog_add_buttons (GTK_DIALOG (dlg),
157 				GTK_STOCK_OK, GTK_RESPONSE_OK,
158 				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
159 
160 	return (GtkWidget*) dlg;
161 }
162 
163 /**
164  * vtable_dialog_get_table_name:
165  *
166  */
167 const gchar *
168 vtable_dialog_get_table_name (VtableDialog *dlg)
169 {
170 	g_return_val_if_fail (IS_VTABLE_DIALOG (dlg), NULL);
171 	return gtk_entry_get_text (GTK_ENTRY (dlg->priv->tname_entry));
172 }
173 
174 /**
175  * vtable_dialog_get_replace_if_exists:
176  */
177 gboolean
178 vtable_dialog_get_replace_if_exists (VtableDialog *dlg)
179 {
180 	g_return_val_if_fail (IS_VTABLE_DIALOG (dlg), FALSE);
181 	return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dlg->priv->tname_replace));
182 }
183