diff options
| author | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
| commit | 0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch) | |
| tree | af916a30553c78ce9d4f2d8658656a175c5b6921 /src/editldap_basedn.c | |
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'src/editldap_basedn.c')
| -rw-r--r-- | src/editldap_basedn.c | 342 |
1 files changed, 342 insertions, 0 deletions
diff --git a/src/editldap_basedn.c b/src/editldap_basedn.c new file mode 100644 index 0000000..2e59b25 --- /dev/null +++ b/src/editldap_basedn.c | |||
| @@ -0,0 +1,342 @@ | |||
| 1 | /* | ||
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client | ||
| 3 | * Copyright (C) 2001 Match Grun | ||
| 4 | * | ||
| 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 2 of the License, or | ||
| 8 | * (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 | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 18 | */ | ||
| 19 | |||
| 20 | /* | ||
| 21 | * LDAP Base DN selection dialog. | ||
| 22 | */ | ||
| 23 | |||
| 24 | #ifdef HAVE_CONFIG_H | ||
| 25 | # include "config.h" | ||
| 26 | #endif | ||
| 27 | |||
| 28 | #ifdef USE_LDAP | ||
| 29 | |||
| 30 | #include "defs.h" | ||
| 31 | |||
| 32 | #include <glib.h> | ||
| 33 | #include <glib/gi18n.h> | ||
| 34 | #include <gdk/gdkkeysyms.h> | ||
| 35 | #include <gtk/gtkwindow.h> | ||
| 36 | #include <gtk/gtksignal.h> | ||
| 37 | #include <gtk/gtkhbox.h> | ||
| 38 | #include <gtk/gtkvbox.h> | ||
| 39 | #include <gtk/gtktable.h> | ||
| 40 | #include <gtk/gtklabel.h> | ||
| 41 | #include <gtk/gtkentry.h> | ||
| 42 | #include <gtk/gtkhbbox.h> | ||
| 43 | #include <gtk/gtkbutton.h> | ||
| 44 | #include <gtk/gtkstatusbar.h> | ||
| 45 | #include <gtk/gtkhseparator.h> | ||
| 46 | |||
| 47 | #include "prefs_common.h" | ||
| 48 | #include "syldap.h" | ||
| 49 | #include "mgutils.h" | ||
| 50 | #include "gtkutils.h" | ||
| 51 | #include "manage_window.h" | ||
| 52 | |||
| 53 | static struct _LDAPEdit_basedn { | ||
| 54 | GtkWidget *window; | ||
| 55 | GtkWidget *host_label; | ||
| 56 | GtkWidget *port_label; | ||
| 57 | GtkWidget *basedn_entry; | ||
| 58 | GtkWidget *basedn_list; | ||
| 59 | GtkWidget *hbbox; | ||
| 60 | GtkWidget *ok_btn; | ||
| 61 | GtkWidget *cancel_btn; | ||
| 62 | GtkWidget *statusbar; | ||
| 63 | gint status_cid; | ||
| 64 | } ldapedit_basedn; | ||
| 65 | |||
| 66 | static gboolean ldapedit_basedn_cancelled; | ||
| 67 | static gboolean ldapedit_basedn_bad_server; | ||
| 68 | |||
| 69 | /* | ||
| 70 | * Edit functions. | ||
| 71 | */ | ||
| 72 | static void edit_ldap_bdn_status_show( gchar *msg ) { | ||
| 73 | if( ldapedit_basedn.statusbar != NULL ) { | ||
| 74 | gtk_statusbar_pop( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid ); | ||
| 75 | if( msg ) { | ||
| 76 | gtk_statusbar_push( GTK_STATUSBAR(ldapedit_basedn.statusbar), ldapedit_basedn.status_cid, msg ); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | static gint edit_ldap_bdn_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) { | ||
| 82 | ldapedit_basedn_cancelled = TRUE; | ||
| 83 | gtk_main_quit(); | ||
| 84 | return TRUE; | ||
| 85 | } | ||
| 86 | |||
| 87 | static gboolean edit_ldap_bdn_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) { | ||
| 88 | if (event && event->keyval == GDK_Escape) { | ||
| 89 | ldapedit_basedn_cancelled = TRUE; | ||
| 90 | gtk_main_quit(); | ||
| 91 | } | ||
| 92 | return FALSE; | ||
| 93 | } | ||
| 94 | |||
| 95 | static void edit_ldap_bdn_ok( GtkWidget *widget, gboolean *cancelled ) { | ||
| 96 | ldapedit_basedn_cancelled = FALSE; | ||
| 97 | gtk_main_quit(); | ||
| 98 | } | ||
| 99 | |||
| 100 | static void edit_ldap_bdn_cancel( GtkWidget *widget, gboolean *cancelled ) { | ||
| 101 | ldapedit_basedn_cancelled = TRUE; | ||
| 102 | gtk_main_quit(); | ||
| 103 | } | ||
| 104 | |||
| 105 | static void edit_ldap_bdn_list_select( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) { | ||
| 106 | gchar *text = NULL; | ||
| 107 | |||
| 108 | if( gtk_clist_get_text( clist, row, 0, &text ) ) { | ||
| 109 | if( text ) { | ||
| 110 | gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), text ); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | static gboolean edit_ldap_bdn_list_button( GtkCList *clist, GdkEventButton *event, gpointer data ) { | ||
| 116 | if( ! event ) return FALSE; | ||
| 117 | if( event->button == 1 ) { | ||
| 118 | if( event->type == GDK_2BUTTON_PRESS ) { | ||
| 119 | ldapedit_basedn_cancelled = FALSE; | ||
| 120 | gtk_main_quit(); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | return FALSE; | ||
| 124 | } | ||
| 125 | |||
| 126 | static void edit_ldap_bdn_create(void) { | ||
| 127 | GtkWidget *window; | ||
| 128 | GtkWidget *vbox; | ||
| 129 | GtkWidget *table; | ||
| 130 | GtkWidget *label; | ||
| 131 | GtkWidget *host_label; | ||
| 132 | GtkWidget *port_label; | ||
| 133 | GtkWidget *basedn_list; | ||
| 134 | GtkWidget *vlbox; | ||
| 135 | GtkWidget *lwindow; | ||
| 136 | GtkWidget *basedn_entry; | ||
| 137 | GtkWidget *hbbox; | ||
| 138 | GtkWidget *hsep; | ||
| 139 | GtkWidget *ok_btn; | ||
| 140 | GtkWidget *cancel_btn; | ||
| 141 | GtkWidget *hsbox; | ||
| 142 | GtkWidget *statusbar; | ||
| 143 | gint top; | ||
| 144 | |||
| 145 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | ||
| 146 | gtk_widget_set_size_request(window, 300, 270); | ||
| 147 | gtk_container_set_border_width(GTK_CONTAINER(window), 0); | ||
| 148 | gtk_window_set_title(GTK_WINDOW(window), _("Edit LDAP - Select Search Base")); | ||
| 149 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); | ||
| 150 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); | ||
| 151 | g_signal_connect(G_OBJECT(window), "delete_event", | ||
| 152 | G_CALLBACK(edit_ldap_bdn_delete_event), NULL); | ||
| 153 | g_signal_connect(G_OBJECT(window), "key_press_event", | ||
| 154 | G_CALLBACK(edit_ldap_bdn_key_pressed), NULL); | ||
| 155 | |||
| 156 | vbox = gtk_vbox_new(FALSE, 8); | ||
| 157 | gtk_container_add(GTK_CONTAINER(window), vbox); | ||
| 158 | gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 ); | ||
| 159 | |||
| 160 | table = gtk_table_new(3, 2, FALSE); | ||
| 161 | gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0); | ||
| 162 | gtk_container_set_border_width( GTK_CONTAINER(table), 8 ); | ||
| 163 | gtk_table_set_row_spacings(GTK_TABLE(table), 8); | ||
| 164 | gtk_table_set_col_spacings(GTK_TABLE(table), 8); | ||
| 165 | |||
| 166 | /* First row */ | ||
| 167 | top = 0; | ||
| 168 | label = gtk_label_new(_("Hostname")); | ||
| 169 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); | ||
| 170 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | ||
| 171 | |||
| 172 | host_label = gtk_label_new(""); | ||
| 173 | gtk_table_attach(GTK_TABLE(table), host_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0); | ||
| 174 | gtk_misc_set_alignment(GTK_MISC(host_label), 0, 0.5); | ||
| 175 | |||
| 176 | /* Second row */ | ||
| 177 | top = 1; | ||
| 178 | label = gtk_label_new(_("Port")); | ||
| 179 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); | ||
| 180 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0); | ||
| 181 | |||
| 182 | port_label = gtk_label_new(""); | ||
| 183 | gtk_table_attach(GTK_TABLE(table), port_label, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0); | ||
| 184 | gtk_misc_set_alignment(GTK_MISC(port_label), 0, 0.5); | ||
| 185 | |||
| 186 | /* Third row */ | ||
| 187 | top = 2; | ||
| 188 | label = gtk_label_new(_("Search Base")); | ||
| 189 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); | ||
| 190 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | ||
| 191 | |||
| 192 | basedn_entry = gtk_entry_new(); | ||
| 193 | gtk_table_attach(GTK_TABLE(table), basedn_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); | ||
| 194 | |||
| 195 | /* Basedn list */ | ||
| 196 | vlbox = gtk_vbox_new(FALSE, 8); | ||
| 197 | gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0); | ||
| 198 | gtk_container_set_border_width( GTK_CONTAINER(vlbox), 8 ); | ||
| 199 | |||
| 200 | lwindow = gtk_scrolled_window_new(NULL, NULL); | ||
| 201 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(lwindow), | ||
| 202 | GTK_POLICY_AUTOMATIC, | ||
| 203 | GTK_POLICY_ALWAYS); | ||
| 204 | gtk_box_pack_start(GTK_BOX(vlbox), lwindow, TRUE, TRUE, 0); | ||
| 205 | |||
| 206 | basedn_list = gtk_clist_new(1); | ||
| 207 | gtk_container_add(GTK_CONTAINER(lwindow), basedn_list); | ||
| 208 | gtk_clist_column_titles_show( GTK_CLIST(basedn_list) ); | ||
| 209 | gtk_clist_set_column_title( GTK_CLIST(basedn_list), 0, _( "Available Search Base(s)" ) ); | ||
| 210 | gtk_clist_set_selection_mode(GTK_CLIST(basedn_list), GTK_SELECTION_BROWSE); | ||
| 211 | gtkut_clist_set_redraw( GTK_CLIST(basedn_list) ); | ||
| 212 | |||
| 213 | /* Status line */ | ||
| 214 | hsbox = gtk_hbox_new(FALSE, 0); | ||
| 215 | gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, 0); | ||
| 216 | statusbar = gtk_statusbar_new(); | ||
| 217 | gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, 0); | ||
| 218 | |||
| 219 | /* Button panel */ | ||
| 220 | gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK, | ||
| 221 | &cancel_btn, GTK_STOCK_CANCEL, | ||
| 222 | NULL, NULL); | ||
| 223 | gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0); | ||
| 224 | gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 ); | ||
| 225 | gtk_widget_grab_default(ok_btn); | ||
| 226 | |||
| 227 | hsep = gtk_hseparator_new(); | ||
| 228 | gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0); | ||
| 229 | |||
| 230 | g_signal_connect(G_OBJECT(ok_btn), "clicked", | ||
| 231 | G_CALLBACK(edit_ldap_bdn_ok), NULL); | ||
| 232 | g_signal_connect(G_OBJECT(cancel_btn), "clicked", | ||
| 233 | G_CALLBACK(edit_ldap_bdn_cancel), NULL); | ||
| 234 | g_signal_connect(G_OBJECT(basedn_list), "select_row", | ||
| 235 | G_CALLBACK(edit_ldap_bdn_list_select), NULL); | ||
| 236 | g_signal_connect(G_OBJECT(basedn_list), "button_press_event", | ||
| 237 | G_CALLBACK(edit_ldap_bdn_list_button), NULL); | ||
| 238 | |||
| 239 | gtk_widget_show_all(vbox); | ||
| 240 | |||
| 241 | ldapedit_basedn.window = window; | ||
| 242 | ldapedit_basedn.host_label = host_label; | ||
| 243 | ldapedit_basedn.port_label = port_label; | ||
| 244 | ldapedit_basedn.basedn_entry = basedn_entry; | ||
| 245 | ldapedit_basedn.basedn_list = basedn_list; | ||
| 246 | ldapedit_basedn.hbbox = hbbox; | ||
| 247 | ldapedit_basedn.ok_btn = ok_btn; | ||
| 248 | ldapedit_basedn.cancel_btn = cancel_btn; | ||
| 249 | ldapedit_basedn.statusbar = statusbar; | ||
| 250 | ldapedit_basedn.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit LDAP Select Base DN" ); | ||
| 251 | } | ||
| 252 | |||
| 253 | void edit_ldap_bdn_load_data( const gchar *hostName, const gint iPort, const gint tov, const gchar* bindDN, | ||
| 254 | const gchar *bindPW ) { | ||
| 255 | gchar *sHost; | ||
| 256 | gchar *sMsg = NULL; | ||
| 257 | gchar sPort[20]; | ||
| 258 | gboolean flgConn; | ||
| 259 | gboolean flgDN; | ||
| 260 | |||
| 261 | edit_ldap_bdn_status_show( "" ); | ||
| 262 | gtk_clist_clear(GTK_CLIST(ldapedit_basedn.basedn_list)); | ||
| 263 | ldapedit_basedn_bad_server = TRUE; | ||
| 264 | flgConn = flgDN = FALSE; | ||
| 265 | sHost = g_strdup( hostName ); | ||
| 266 | sprintf( sPort, "%d", iPort ); | ||
| 267 | gtk_label_set_text(GTK_LABEL(ldapedit_basedn.host_label), hostName); | ||
| 268 | gtk_label_set_text(GTK_LABEL(ldapedit_basedn.port_label), sPort); | ||
| 269 | if( *sHost != '\0' ) { | ||
| 270 | /* Test connection to server */ | ||
| 271 | if( syldap_test_connect_s( sHost, iPort ) ) { | ||
| 272 | /* Attempt to read base DN */ | ||
| 273 | GList *baseDN = syldap_read_basedn_s( sHost, iPort, bindDN, bindPW, tov ); | ||
| 274 | if( baseDN ) { | ||
| 275 | GList *node = baseDN; | ||
| 276 | gchar *text[2] = { NULL, NULL }; | ||
| 277 | |||
| 278 | while( node ) { | ||
| 279 | text[0] = (gchar *)node->data; | ||
| 280 | gtk_clist_append(GTK_CLIST(ldapedit_basedn.basedn_list), text); | ||
| 281 | node = g_list_next( node ); | ||
| 282 | flgDN = TRUE; | ||
| 283 | } | ||
| 284 | mgu_free_dlist( baseDN ); | ||
| 285 | baseDN = node = NULL; | ||
| 286 | } | ||
| 287 | ldapedit_basedn_bad_server = FALSE; | ||
| 288 | flgConn = TRUE; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | g_free( sHost ); | ||
| 292 | |||
| 293 | /* Display appropriate message */ | ||
| 294 | if( flgConn ) { | ||
| 295 | if( ! flgDN ) { | ||
| 296 | sMsg = _( "Could not read Search Base(s) from server - please set manually" ); | ||
| 297 | } | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | sMsg = _( "Could not connect to server" ); | ||
| 301 | } | ||
| 302 | edit_ldap_bdn_status_show( sMsg ); | ||
| 303 | } | ||
| 304 | |||
| 305 | gchar *edit_ldap_basedn_selection( const gchar *hostName, const gint port, gchar *baseDN, const gint tov, | ||
| 306 | const gchar* bindDN, const gchar *bindPW ) { | ||
| 307 | gchar *retVal = NULL; | ||
| 308 | |||
| 309 | ldapedit_basedn_cancelled = FALSE; | ||
| 310 | if( ! ldapedit_basedn.window ) edit_ldap_bdn_create(); | ||
| 311 | gtkut_box_set_reverse_order(GTK_BOX(ldapedit_basedn.hbbox), | ||
| 312 | !prefs_common.comply_gnome_hig); | ||
| 313 | gtk_widget_grab_focus(ldapedit_basedn.ok_btn); | ||
| 314 | gtk_widget_show(ldapedit_basedn.window); | ||
| 315 | manage_window_set_transient(GTK_WINDOW(ldapedit_basedn.window)); | ||
| 316 | |||
| 317 | edit_ldap_bdn_status_show( "" ); | ||
| 318 | edit_ldap_bdn_load_data( hostName, port, tov, bindDN, bindPW ); | ||
| 319 | gtk_widget_show(ldapedit_basedn.window); | ||
| 320 | |||
| 321 | gtk_entry_set_text(GTK_ENTRY(ldapedit_basedn.basedn_entry), baseDN); | ||
| 322 | |||
| 323 | gtk_main(); | ||
| 324 | gtk_widget_hide(ldapedit_basedn.window); | ||
| 325 | if( ldapedit_basedn_cancelled ) return NULL; | ||
| 326 | if( ldapedit_basedn_bad_server ) return NULL; | ||
| 327 | |||
| 328 | retVal = gtk_editable_get_chars( GTK_EDITABLE(ldapedit_basedn.basedn_entry), 0, -1 ); | ||
| 329 | g_strchomp( retVal ); g_strchug( retVal ); | ||
| 330 | if( *retVal == '\0' ) { | ||
| 331 | g_free( retVal ); | ||
| 332 | retVal = NULL; | ||
| 333 | } | ||
| 334 | return retVal; | ||
| 335 | } | ||
| 336 | |||
| 337 | #endif /* USE_LDAP */ | ||
| 338 | |||
| 339 | /* | ||
| 340 | * End of Source. | ||
| 341 | */ | ||
| 342 | |||
