2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2022 the Claws Mail team and Match Grun
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * LDAP Base DN selection dialog.
25 #include "claws-features.h"
33 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
37 #include "prefs_common.h"
41 #include "manage_window.h"
43 static struct _LDAPEdit_basedn
{
45 GtkWidget
*host_label
;
46 GtkWidget
*port_label
;
47 GtkWidget
*basedn_entry
;
48 GtkWidget
*basedn_list
;
50 GtkWidget
*cancel_btn
;
55 static gboolean ldapedit_basedn_cancelled
;
56 static gboolean ldapedit_basedn_bad_server
;
61 static void edit_ldap_bdn_status_show( gchar
*msg
) {
62 if( ldapedit_basedn
.statusbar
!= NULL
) {
63 gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn
.statusbar
), ldapedit_basedn
.status_cid
);
65 gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn
.statusbar
), ldapedit_basedn
.status_cid
, msg
);
70 static gint
edit_ldap_bdn_delete_event( GtkWidget
*widget
, GdkEventAny
*event
, gboolean
*cancelled
) {
71 ldapedit_basedn_cancelled
= TRUE
;
76 static gboolean
edit_ldap_bdn_key_pressed( GtkWidget
*widget
, GdkEventKey
*event
, gboolean
*cancelled
) {
77 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
78 ldapedit_basedn_cancelled
= TRUE
;
84 static void edit_ldap_bdn_ok( GtkWidget
*widget
, gboolean
*cancelled
) {
85 ldapedit_basedn_cancelled
= FALSE
;
89 static void edit_ldap_bdn_cancel( GtkWidget
*widget
, gboolean
*cancelled
) {
90 ldapedit_basedn_cancelled
= TRUE
;
94 static void set_selected()
96 GtkWidget
*entry
= ldapedit_basedn
.basedn_entry
;
97 GtkWidget
*view
= ldapedit_basedn
.basedn_list
;
100 if (entry
== NULL
|| view
== NULL
)
103 text
= gtkut_tree_view_get_selected_pointer(
104 GTK_TREE_VIEW(view
), 0, NULL
, NULL
, NULL
);
109 gtk_entry_set_text(GTK_ENTRY(entry
), text
);
113 static void basedn_list_cursor_changed(GtkTreeView
*view
,
119 static void basedn_list_row_activated(GtkTreeView
*view
,
121 GtkTreeViewColumn
*column
,
125 edit_ldap_bdn_ok(NULL
, NULL
);
128 static void edit_ldap_bdn_create(void) {
133 GtkWidget
*host_label
;
134 GtkWidget
*port_label
;
135 GtkWidget
*basedn_list
;
138 GtkWidget
*basedn_entry
;
142 GtkWidget
*cancel_btn
;
144 GtkWidget
*statusbar
;
146 GtkTreeSelection
*sel
;
147 GtkTreeViewColumn
*col
;
148 GtkCellRenderer
*rdr
;
150 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "editldap_basedn");
151 gtk_container_set_border_width(GTK_CONTAINER(window
), 0);
152 gtk_window_set_title(GTK_WINDOW(window
), _("Edit LDAP - Select Search Base"));
153 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
154 gtk_window_set_type_hint(GTK_WINDOW(window
), GDK_WINDOW_TYPE_HINT_DIALOG
);
155 g_signal_connect(G_OBJECT(window
), "delete_event",
156 G_CALLBACK(edit_ldap_bdn_delete_event
), NULL
);
157 g_signal_connect(G_OBJECT(window
), "key_press_event",
158 G_CALLBACK(edit_ldap_bdn_key_pressed
), NULL
);
160 vbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 8);
161 gtk_container_add(GTK_CONTAINER(window
), vbox
);
162 gtk_container_set_border_width( GTK_CONTAINER(vbox
), 0 );
164 table
= gtk_grid_new();
165 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
166 gtk_container_set_border_width( GTK_CONTAINER(table
), 8 );
167 gtk_grid_set_row_spacing(GTK_GRID(table
), 8);
168 gtk_grid_set_column_spacing(GTK_GRID(table
), 8);
171 label
= gtk_label_new(_("Hostname"));
172 gtk_label_set_xalign(GTK_LABEL(label
), 0.0);
173 gtk_grid_attach(GTK_GRID(table
), label
, 0, 0, 1, 1);
175 host_label
= gtk_label_new("");
176 gtk_label_set_xalign(GTK_LABEL(host_label
), 0.0);
177 gtk_grid_attach(GTK_GRID(table
), host_label
, 1, 0, 1, 1);
180 label
= gtk_label_new(_("Port"));
181 gtk_label_set_xalign(GTK_LABEL(label
), 0.0);
182 gtk_grid_attach(GTK_GRID(table
), label
, 0, 1, 1, 1);
184 port_label
= gtk_label_new("");
185 gtk_label_set_xalign(GTK_LABEL(port_label
), 0.0);
186 gtk_grid_attach(GTK_GRID(table
), label
, 1, 1, 1, 1);
189 label
= gtk_label_new(_("Search Base"));
190 gtk_label_set_xalign(GTK_LABEL(label
), 0.0);
191 gtk_grid_attach(GTK_GRID(table
), label
, 0, 2, 1, 1);
193 basedn_entry
= gtk_entry_new();
194 gtk_grid_attach(GTK_GRID(table
), basedn_entry
, 1, 2, 1, 1);
195 gtk_widget_set_hexpand(basedn_entry
, TRUE
);
196 gtk_widget_set_halign(basedn_entry
, GTK_ALIGN_FILL
);
199 vlbox
= gtk_box_new(GTK_ORIENTATION_VERTICAL
, 8);
200 gtk_box_pack_start(GTK_BOX(vbox
), vlbox
, TRUE
, TRUE
, 0);
201 gtk_container_set_border_width( GTK_CONTAINER(vlbox
), 8 );
203 lwindow
= gtk_scrolled_window_new(NULL
, NULL
);
204 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow
),
205 GTK_POLICY_AUTOMATIC
,
206 GTK_POLICY_AUTOMATIC
);
207 gtk_box_pack_start(GTK_BOX(vlbox
), lwindow
, TRUE
, TRUE
, 0);
209 store
= gtk_list_store_new(1, G_TYPE_STRING
, -1);
211 basedn_list
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(store
));
212 g_object_unref(store
);
213 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(basedn_list
), TRUE
);
214 sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(basedn_list
));
215 gtk_tree_selection_set_mode(sel
, GTK_SELECTION_BROWSE
);
217 rdr
= gtk_cell_renderer_text_new();
218 col
= gtk_tree_view_column_new_with_attributes(
219 _("Available Search Base(s)"), rdr
,
221 gtk_tree_view_append_column(GTK_TREE_VIEW(basedn_list
), col
);
223 gtk_container_add(GTK_CONTAINER(lwindow
), basedn_list
);
225 g_signal_connect(G_OBJECT(basedn_list
), "cursor-changed",
226 G_CALLBACK(basedn_list_cursor_changed
), NULL
);
227 g_signal_connect(G_OBJECT(basedn_list
), "row-activated",
228 G_CALLBACK(basedn_list_row_activated
), NULL
);
232 hsbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 0);
233 gtk_box_pack_end(GTK_BOX(vbox
), hsbox
, FALSE
, FALSE
, BORDER_WIDTH
);
234 statusbar
= gtk_statusbar_new();
235 gtk_box_pack_start(GTK_BOX(hsbox
), statusbar
, TRUE
, TRUE
, BORDER_WIDTH
);
238 gtkut_stock_button_set_create(&hbbox
, &cancel_btn
, NULL
, _("_Cancel"),
239 &ok_btn
, NULL
, _("_OK"),
241 gtk_box_pack_end(GTK_BOX(vbox
), hbbox
, FALSE
, FALSE
, 0);
242 gtk_container_set_border_width( GTK_CONTAINER(hbbox
), 0 );
243 gtk_widget_grab_default(ok_btn
);
245 hsep
= gtk_separator_new(GTK_ORIENTATION_HORIZONTAL
);
246 gtk_box_pack_end(GTK_BOX(vbox
), hsep
, FALSE
, FALSE
, 0);
248 g_signal_connect(G_OBJECT(ok_btn
), "clicked",
249 G_CALLBACK(edit_ldap_bdn_ok
), NULL
);
250 g_signal_connect(G_OBJECT(cancel_btn
), "clicked",
251 G_CALLBACK(edit_ldap_bdn_cancel
), NULL
);
253 gtk_widget_show_all(vbox
);
255 ldapedit_basedn
.window
= window
;
256 ldapedit_basedn
.host_label
= host_label
;
257 ldapedit_basedn
.port_label
= port_label
;
258 ldapedit_basedn
.basedn_entry
= basedn_entry
;
259 ldapedit_basedn
.basedn_list
= basedn_list
;
260 ldapedit_basedn
.ok_btn
= ok_btn
;
261 ldapedit_basedn
.cancel_btn
= cancel_btn
;
262 ldapedit_basedn
.statusbar
= statusbar
;
263 ldapedit_basedn
.status_cid
=
264 gtk_statusbar_get_context_id(
265 GTK_STATUSBAR(statusbar
), "Edit LDAP Select Base DN" );
268 static void edit_ldap_bdn_load_data(
269 const gchar
*hostName
, const gint iPort
, const gint tov
,
270 const gchar
* bindDN
, const gchar
*bindPW
, int ssl
, int tls
)
277 GList
*baseDN
= NULL
;
278 GtkWidget
*view
= ldapedit_basedn
.basedn_list
;
279 GtkTreeModel
*model
= gtk_tree_view_get_model(GTK_TREE_VIEW(view
));
281 GtkTreeSelection
*sel
= gtk_tree_view_get_selection(GTK_TREE_VIEW(view
));
283 edit_ldap_bdn_status_show( "" );
285 gtk_list_store_clear(GTK_LIST_STORE(model
));
287 ldapedit_basedn_bad_server
= TRUE
;
288 flgConn
= flgDN
= FALSE
;
289 sHost
= g_strdup( hostName
);
290 sprintf( sPort
, "%d", iPort
);
291 gtk_label_set_text(GTK_LABEL(ldapedit_basedn
.host_label
), hostName
);
292 gtk_label_set_text(GTK_LABEL(ldapedit_basedn
.port_label
), sPort
);
293 if( *sHost
!= '\0' ) {
294 /* Test connection to server */
295 if( ldaputil_test_connect( sHost
, iPort
, ssl
, tls
, tov
) ) {
296 /* Attempt to read base DN */
297 baseDN
= ldaputil_read_basedn( sHost
, iPort
, bindDN
, bindPW
, tov
, ssl
, tls
);
299 GList
*node
= baseDN
;
302 gtk_list_store_append(GTK_LIST_STORE(model
), &iter
);
303 gtk_list_store_set(GTK_LIST_STORE(model
), &iter
,
304 0, (gchar
*)node
->data
, -1);
305 node
= g_list_next( node
);
309 /* Select the first line */
310 if (gtk_tree_model_get_iter_first(model
, &iter
))
311 gtk_tree_selection_select_iter(sel
, &iter
);
313 g_list_free_full( baseDN
, g_free
);
314 baseDN
= node
= NULL
;
316 ldapedit_basedn_bad_server
= FALSE
;
322 /* Display appropriate message */
325 sMsg
= _( "Could not read Search Base(s) from server - please set manually" );
329 sMsg
= _( "Could not connect to server" );
331 edit_ldap_bdn_status_show( sMsg
);
334 gchar
*edit_ldap_basedn_selection( const gchar
*hostName
, const gint port
, gchar
*baseDN
, const gint tov
,
335 const gchar
* bindDN
, const gchar
*bindPW
, int ssl
, int tls
) {
336 gchar
*retVal
= NULL
;
338 ldapedit_basedn_cancelled
= FALSE
;
339 if( ! ldapedit_basedn
.window
) edit_ldap_bdn_create();
340 gtk_widget_grab_focus(ldapedit_basedn
.ok_btn
);
341 gtk_widget_show(ldapedit_basedn
.window
);
342 manage_window_set_transient(GTK_WINDOW(ldapedit_basedn
.window
));
343 gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn
.window
), TRUE
);
344 edit_ldap_bdn_status_show( "" );
345 edit_ldap_bdn_load_data( hostName
, port
, tov
, bindDN
, bindPW
, ssl
, tls
);
346 gtk_widget_show(ldapedit_basedn
.window
);
348 gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn
.basedn_entry
), baseDN
);
351 gtk_widget_hide(ldapedit_basedn
.window
);
352 gtk_window_set_modal(GTK_WINDOW(ldapedit_basedn
.window
), FALSE
);
353 if( ldapedit_basedn_cancelled
) return NULL
;
354 if( ldapedit_basedn_bad_server
) return NULL
;
356 retVal
= gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn
.basedn_entry
), 0, -1 );
357 g_strstrip( retVal
);
358 if( *retVal
== '\0' ) {
365 #endif /* USE_LDAP */