[gq-commit] gq/src browse-export.c,NONE,1.1 browse-export.h,NONE,1.1 Makefile.am,1.31,1.32 dn-browse
Status: Beta
Brought to you by:
sur5r
From: <sta...@us...> - 2003-10-23 06:03:16
|
Update of /cvsroot/gqclient/gq/src In directory sc8-pr-cvs1:/tmp/cvs-serv13522 Modified Files: Makefile.am dn-browse.c ref-browse.c server-browse.c Added Files: browse-export.c browse-export.h Log Message: * TODO item implemented: Re-introduce Export to LDIF functionality for servers --- NEW FILE: browse-export.c --- /* GQ -- a GTK-based LDAP client Copyright (C) 1998-2003 Bert Vermeulen Parts: Copyright (C) 2002-2003 Peter Stamfest <pe...@st...> This program is released under the Gnu General Public License with the additional exemption that compiling, linking, and/or using OpenSSL is allowed. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: browse-export.c,v 1.1 2003/10/23 05:18:36 stamfest Exp $ */ #include <glib.h> #include <gtk/gtk.h> #include <string.h> #include <errno.h> /* errno */ #include <stdio.h> /* FILE */ #include <stdlib.h> /* free - MUST get rid of malloc/free */ #include "config.h" #include "common.h" #include "dn-browse.h" #include "ref-browse.h" #include "input.h" /* new_from_entry */ #include "search.h" /* fill_out_search */ #include "template.h" /* struct gq_template */ #include "formfill.h" /* formlist_from_entry */ #include "tinput.h" /* formfill_from_template */ #include "browse-dnd.h" /* copy_entry et al */ #include "configfile.h" /* config */ #include "errorchain.h" #include "util.h" #include "encode.h" #include "i18n.h" #include "utf8-compat.h" #include "ldif.h" struct export { struct ldapserver *server; GList *bases; GtkWidget *filesel; }; static struct export *new_export() { struct export *ex = g_malloc0(sizeof(struct export)); return ex; } static void free_export(struct export *ex) { if (ex) { g_list_foreach(ex->bases, (GFunc) g_free, NULL); g_list_free(ex->bases); ex->bases = NULL; ldapserver_unref(ex->server); ex->server = NULL; g_free(ex); } } static void dump_subtree_ok_callback(struct export *ex) { LDAPMessage *res = NULL, *e; LDAP *ld = NULL; GList *I; int num_entries; const char *filename; FILE *outfile = NULL; GString *out = NULL; GString *gmessage = NULL; /* GString *bigmessage = NULL; */ int written; int ctx; out = g_string_sized_new(2048); /* if (IS_SERVER_ENTRY(entry)) { */ /* bases = get_suffixes(((server_browse_entry *)entry)->server); */ /* } else { */ /* bases = g_list_append(bases, */ /* strdup(((dn_browse_entry *)entry)->dn)); */ /* } */ ctx = error_new_context(_("Dump subtree")); if(g_list_length(ex->bases) == 0) { error_push(ctx, _("Nothing to dump!")); goto fail; } set_busycursor(); /* obtain filename and open file for reading */ filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(ex->filesel)); if( (outfile = fopen(filename, "w")) == NULL) { error_push(ctx, _("Could not open output file '%1$s': %2$s"), filename, strerror(errno)); goto fail; /* error_popup(_("Save failed"), strerror(errno)); */ } else { /* AFAIK, the UMich LDIF format doesn't take comments or a version string */ if (config->ldifformat != LDIF_UMICH) { g_string_truncate(out, 0); prepend_ldif_header(out, ex->server, ex->bases); written = fwrite(out->str, 1, out->len, outfile); if(written != out->len) { error_push(ctx, _("Save to '%3$s' failed: Only %1$d of %2$d bytes written"), written, out->len, filename); goto fail; /* sometimes goto is useful */ } } if( (ld = open_connection(ex->server)) == NULL) { /* no extra error, open_connection does error reporting itself... */ goto fail; } num_entries = 0; gmessage = g_string_sized_new(256); for (I = g_list_first(ex->bases) ; I ; I = g_list_next(I)) { LDAPControl ct; LDAPControl *ctrls[2] = { NULL, NULL } ; char *attrs[] = { LDAP_ALL_USER_ATTRIBUTES, "ref", NULL }; int rc; ct.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT; ct.ldctl_value.bv_val = NULL; ct.ldctl_value.bv_len = 0; ct.ldctl_iscritical = 1; ctrls[0] = &ct; statusbar_msg(_("Subtree search on %s"), (char *) I->data); rc = ldap_search_ext_s(ld, (char *) I->data, LDAP_SCOPE_SUBTREE, "(objectClass=*)", attrs, 0, ctrls, /* serverctrls */ NULL, /* clientctrls */ NULL, /* timeout */ LDAP_NO_LIMIT, /* sizelimit */ &res); if (rc == LDAP_SUCCESS) { for(e = ldap_first_entry(ld, res); e; e = ldap_next_entry(ld, e)) { g_string_truncate(out, 0); ldif_entry_out(out, ld, e, ctx); num_entries++; written = fwrite(out->str, 1, out->len, outfile); if(written != out->len) { g_string_sprintf(gmessage, _("%1$d of %2$d bytes written"), written, out->len); error_popup(_("Save failed"), gmessage->str); ldap_msgfree(res); close_connection(ex->server, FALSE); goto fail; } } ldap_msgfree(res); } else if (rc == LDAP_SERVER_DOWN) { ex->server->server_down++; error_push(ctx, _("Server '%s' down. Export may be incomplete!"), ex->server->name); push_ldap_addl_error(ld, ctx); goto fail; } else { /* report error */ error_push(ctx, _("LDAP error while searching below '%s'." " Export may be incomplete!"), (char *) I->data); push_ldap_addl_error(ld, ctx); goto fail; } } statusbar_msg(ngettext("One entry exported to %2$s", "%1$d entries exported to %2$s", num_entries), num_entries, filename); } fail: /* labels are only good for cleaning up, really */ if (outfile) fclose(outfile); set_normalcursor(); if (out) g_string_free(out, TRUE); if (gmessage) g_string_free(gmessage, TRUE); if (ld) close_connection(ex->server, FALSE); gtk_widget_destroy(ex->filesel); error_flush(ctx); } void export_many(GtkWidget *transient_for, GList *bases, struct ldapserver *server) { GtkWidget *filesel; struct export *ex = new_export(); filesel = gtk_file_selection_new(_("Save LDIF")); ex->server = server; ldapserver_ref(server); ex->bases = bases; ex->filesel = filesel; gtk_object_set_data_full(GTK_OBJECT(filesel), "export", ex, (GtkDestroyNotify) free_export); gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), "clicked", (GtkSignalFunc) dump_subtree_ok_callback, ex); gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), "clicked", (GtkSignalFunc) gtk_widget_destroy, GTK_OBJECT(filesel)); gtk_signal_connect_object(GTK_OBJECT(filesel), "key_press_event", (GtkSignalFunc) close_on_esc, (gpointer) filesel); gtk_widget_show(filesel); } /* Local Variables: c-basic-offset: 5 End: */ --- NEW FILE: browse-export.h --- /* GQ -- a GTK-based LDAP client Copyright (C) 1998-2003 Bert Vermeulen Copyright (C) 2002-2003 Peter Stamfest This program is released under the Gnu General Public License with the additional exemption that compiling, linking, and/or using OpenSSL is allowed. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ /* $Id: browse-export.h,v 1.1 2003/10/23 05:18:36 stamfest Exp $ */ #ifndef GQ_BROWSE_EXPORT_H_INCLUDED #define GQ_BROWSE_EXPORT_H_INCLUDED #include <glib.h> /* GList */ #include <gtk/gtk.h> /* GtkWidget */ #include "config.h" #include "common.h" /* struct ldapserver */ void export_many(GtkWidget *widget, GList *bases, struct ldapserver *server); #endif /* Local Variables: c-basic-offset: 5 End: */ Index: Makefile.am =================================================================== RCS file: /cvsroot/gqclient/gq/src/Makefile.am,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** Makefile.am 21 Oct 2003 04:46:41 -0000 1.31 --- Makefile.am 23 Oct 2003 05:18:36 -0000 1.32 *************** *** 36,39 **** --- 36,40 ---- server-browse.c \ ref-browse.c \ + browse-export.c \ schema.c \ schemabrowse.c \ *************** *** 85,88 **** --- 86,90 ---- server-browse.h \ ref-browse.h \ + browse-export.h \ schema.h \ schemabrowse.h \ Index: dn-browse.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/dn-browse.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** dn-browse.c 19 Oct 2003 12:08:58 -0000 1.4 --- dn-browse.c 23 Oct 2003 05:18:36 -0000 1.5 *************** *** 2,6 **** GQ -- a GTK-based LDAP client Copyright (C) 1998-2003 Bert Vermeulen ! Parts: Copyright (C) 2002-2003 Peter Stamfest <pe...@st...> This program is released under the Gnu General Public License with --- 2,6 ---- GQ -- a GTK-based LDAP client Copyright (C) 1998-2003 Bert Vermeulen ! Copyright (C) 2002-2003 Peter Stamfest <pe...@st...> This program is released under the Gnu General Public License with *************** *** 55,59 **** #include "utf8-compat.h" ! #include "ldif.h" static void tree_row_search_below(GtkMenuItem *menuitem, struct tab *tab) --- 55,59 ---- #include "utf8-compat.h" ! #include "browse-export.h" static void tree_row_search_below(GtkMenuItem *menuitem, struct tab *tab) *************** *** 186,404 **** } - static void dump_subtree_ok_callback(GtkWidget *button, GtkWidget *filesel) - { - - LDAPMessage *res = NULL, *e; - LDAP *ld = NULL; - GList *bases, *I; - struct ldapserver *server; - browse_entry *entry; - int msg, num_entries; - const char *filename; - FILE *outfile = NULL; - GString *out = NULL; - GString *gmessage = NULL; - /* GString *bigmessage = NULL; */ - int written; - int ctx; - - entry = gtk_object_get_data(GTK_OBJECT(filesel), "entry"); - server = gtk_object_get_data(GTK_OBJECT(filesel), "server"); - - out = g_string_sized_new(2048); - - bases = NULL; - - /* if (IS_SERVER_ENTRY(entry)) { */ - /* bases = get_suffixes(((server_browse_entry *)entry)->server); */ - /* } else { */ - bases = g_list_append(bases, - strdup(((dn_browse_entry *)entry)->dn)); - /* } */ - - ctx = error_new_context(_("Dump subtree")); - - if(g_list_length(bases) == 0) { - error_push(ctx, _("Nothing to dump!")); - goto fail; - } - - set_busycursor(); - - /* obtain filename and open file for reading */ - filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(filesel)); - - if( (outfile = fopen(filename, "w")) == NULL) { - error_push(ctx, _("Could not open output file '%1$s': %2$s"), - filename, strerror(errno)); - - goto fail; - /* error_popup(_("Save failed"), strerror(errno)); */ - } else { - /* AFAIK, the UMich LDIF format doesn't take comments or a - version string */ - if (config->ldifformat != LDIF_UMICH) { - g_string_truncate(out, 0); - - prepend_ldif_header(out, server, bases); - - written = fwrite(out->str, 1, out->len, outfile); - if(written != out->len) { - error_push(ctx, - _("Save to '%3$s' failed: Only %1$d of %2$d bytes written"), - written, out->len, filename); - goto fail; /* sometimes goto is useful */ - } - } - - if( (ld = open_connection(server)) == NULL) { - /* no extra error, open_connection does error reporting - itself... */ - goto fail; - } - - num_entries = 0; - gmessage = g_string_sized_new(256); - for (I = g_list_first(bases) ; I ; I = g_list_next(I)) { - LDAPControl ct; - LDAPControl *ctrls[2] = { NULL, NULL } ; - int rc; - - statusbar_msg(_("Subtree search on %s"), (char *) I->data); - - msg = ldap_search_s(ld, (char *) I->data, LDAP_SCOPE_SUBTREE, - "(objectclass=*)", NULL, 0, &res); - - ct.ldctl_oid = LDAP_CONTROL_MANAGEDSAIT; - ct.ldctl_value.bv_val = NULL; - ct.ldctl_value.bv_len = 0; - ct.ldctl_iscritical = 1; - - ctrls[0] = &ct; - - rc = ldap_search_ext(ld, (char *) I->data, - LDAP_SCOPE_SUBTREE, - "(objectClass=*)", NULL, 0, - ctrls, /* serverctrls */ - NULL, /* clientctrls */ - NULL, /* timeout */ - LDAP_NO_LIMIT, /* sizelimit */ - &msg); - - - if (rc != -1) { - rc = ldap_result(ld, msg, 1, NULL, &res); - } - - if (rc != -1) { - for(e = ldap_first_entry(ld, res); e; e = ldap_next_entry(ld, e)) { - g_string_truncate(out, 0); - ldif_entry_out(out, ld, e, ctx); - num_entries++; - - written = fwrite(out->str, 1, out->len, outfile); - if(written != out->len) { - g_string_sprintf(gmessage, - _("%1$d of %2$d bytes written"), - written, out->len); - error_popup(_("Save failed"), gmessage->str); - - ldap_msgfree(res); - close_connection(server, FALSE); - goto fail; - } - - } - ldap_msgfree(res); - rc = LDAP_SUCCESS; - } else { - rc = ldap_result2error(ld, res, TRUE); - if (msg == LDAP_SERVER_DOWN) { - server->server_down++; - - error_push(ctx, - _("Server '%s' down. Export may be incomplete!"), - server->name); - push_ldap_addl_error(ld, ctx); - goto fail; - } else { - /* report error */ - error_push(ctx, - _("LDAP error while searching below '%s'." - " Export may be incomplete!"), - (char *) I->data); - push_ldap_addl_error(ld, ctx); - goto fail; - } - } - } - - statusbar_msg(ngettext("One entry exported to %2$s", - "%1$d entries exported to %2$s", num_entries), - num_entries, filename); - } - - fail: /* labels are only good for cleaning up, really */ - if (outfile) fclose(outfile); - - if (bases) { - for (I = g_list_first(bases) ; I ; I = g_list_next(I)) { - free(I->data); - } - g_list_free(bases); - } - - set_normalcursor(); - if (out) g_string_free(out, TRUE); - if (gmessage) g_string_free(gmessage, TRUE); - if (ld) close_connection(server, FALSE); - - gtk_widget_destroy(filesel); - - error_flush(ctx); - } - static void dump_subtree(GtkWidget *widget, struct tab *tab) { - GtkWidget *filesel; - GtkCTree *ctree; ! GtkCTreeNode *ctree_node; ! ! browse_entry *entry; struct ldapserver *server; ctree = BROWSETAB(tab)->ctreeroot; ! ctree_node = BROWSETAB(tab)->selected_ctree_node; ! ! entry = (browse_entry *) gtk_ctree_node_get_row_data(ctree, ctree_node); ! if (entry == NULL) ! return; ! ! if ((server = server_from_node(ctree, ctree_node)) == NULL) ! return; ! filesel = gtk_file_selection_new(_("Save LDIF")); ! gtk_object_set_data_full(GTK_OBJECT(filesel), "server", ! server, (GtkDestroyNotify) ldapserver_unref); ! ldapserver_ref(server); ! gtk_object_set_data(GTK_OBJECT(filesel), "entry", entry); ! gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->ok_button), ! "clicked", ! (GtkSignalFunc) dump_subtree_ok_callback, ! filesel); ! gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(filesel)->cancel_button), ! "clicked", ! (GtkSignalFunc) gtk_widget_destroy, ! GTK_OBJECT(filesel)); ! gtk_signal_connect_object(GTK_OBJECT(filesel), "key_press_event", ! (GtkSignalFunc) close_on_esc, ! (gpointer) filesel); ! gtk_widget_show(filesel); } - static void delete_browse_entry(GtkWidget *widget, struct tab *tab) --- 186,213 ---- } static void dump_subtree(GtkWidget *widget, struct tab *tab) { GtkCTree *ctree; ! GtkCTreeNode *node; ! browse_entry *e; struct ldapserver *server; + GList *bases = NULL; ctree = BROWSETAB(tab)->ctreeroot; ! node = BROWSETAB(tab)->tree_row_popped_up; ! e = (browse_entry *) gtk_ctree_node_get_row_data(ctree, node); ! assert(IS_DN_ENTRY(e)); ! server = server_from_node(ctree, node); ! if (e == NULL || server == NULL) ! return; + bases = g_list_append(bases, + g_strdup(((dn_browse_entry *)e)->dn)); + + export_many(tab->win->mainwin, bases, server); } static void delete_browse_entry(GtkWidget *widget, struct tab *tab) Index: ref-browse.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/ref-browse.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ref-browse.c 20 Oct 2003 08:26:55 -0000 1.5 --- ref-browse.c 23 Oct 2003 05:18:36 -0000 1.6 *************** *** 48,51 **** --- 48,52 ---- #include "errorchain.h" #include "encode.h" + #include "browse-export.h" /**************************************************************************/ *************** *** 370,374 **** } ! /* popup method for BOTH server AND ref entries (the same for now...) */ static void ref_browse_entry_popup(dn_browse_entry *entry, GtkWidget *menu, --- 371,399 ---- } ! static void dump_ref(GtkWidget *widget, struct tab *tab) ! { ! GtkCTree *ctree; ! GtkCTreeNode *node; ! browse_entry *e; ! struct ldapserver *server; ! GList *bases = NULL; ! ! ctree = BROWSETAB(tab)->ctreeroot; ! node = BROWSETAB(tab)->tree_row_popped_up; ! e = (browse_entry *) gtk_ctree_node_get_row_data(ctree, node); ! ! assert(IS_REF_ENTRY(e)); ! ! server = server_from_node(ctree, node); ! ! if (e == NULL || server == NULL) ! return; ! ! bases = get_suffixes(((ref_browse_entry *)e)->server); ! ! export_many(tab->win->mainwin, bases, server); ! } ! ! static void ref_browse_entry_popup(dn_browse_entry *entry, GtkWidget *menu, *************** *** 426,438 **** } /* Close connection */ menu_item = gtk_menu_item_new_with_label(_("Close Connection")); gtk_menu_append(GTK_MENU(menu), menu_item); - gtk_widget_show(menu_item); - gtk_signal_connect(GTK_OBJECT(menu_item), "activate", GTK_SIGNAL_FUNC(tree_row_close_connection), (gpointer) tab); - gtk_widget_show(menu_item); --- 451,468 ---- } + /* Export to LDIF */ + menu_item = gtk_menu_item_new_with_label(_("Export to LDIF")); + gtk_menu_append(GTK_MENU(menu), menu_item); + gtk_signal_connect(GTK_OBJECT(menu_item), "activate", + GTK_SIGNAL_FUNC(dump_ref), + (gpointer) tab); + gtk_widget_show(menu_item); + /* Close connection */ menu_item = gtk_menu_item_new_with_label(_("Close Connection")); gtk_menu_append(GTK_MENU(menu), menu_item); gtk_signal_connect(GTK_OBJECT(menu_item), "activate", GTK_SIGNAL_FUNC(tree_row_close_connection), (gpointer) tab); gtk_widget_show(menu_item); Index: server-browse.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/server-browse.c,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** server-browse.c 19 Oct 2003 12:01:32 -0000 1.3 --- server-browse.c 23 Oct 2003 05:18:36 -0000 1.4 *************** *** 40,43 **** --- 40,44 ---- #include "util.h" /* get_suffixes */ + #include "browse-export.h" #include "i18n.h" *************** *** 511,515 **** } ! /* popup method for BOTH server AND ref entries (the same for now...) */ static void server_browse_entry_popup(dn_browse_entry *entry, GtkWidget *menu, --- 512,539 ---- } ! static void dump_server(GtkWidget *widget, struct tab *tab) ! { ! GtkCTree *ctree; ! GtkCTreeNode *node; ! browse_entry *e; ! struct ldapserver *server; ! GList *bases = NULL; ! ! ctree = BROWSETAB(tab)->ctreeroot; ! node = BROWSETAB(tab)->tree_row_popped_up; ! e = (browse_entry *) gtk_ctree_node_get_row_data(ctree, node); ! ! assert(IS_SERVER_ENTRY(e)); ! ! server = server_from_node(ctree, node); ! ! if (e == NULL || server == NULL) ! return; ! ! bases = get_suffixes(((server_browse_entry *)e)->server); ! ! export_many(tab->win->mainwin, bases, server); ! } ! static void server_browse_entry_popup(dn_browse_entry *entry, GtkWidget *menu, *************** *** 552,564 **** } /* Close connection */ menu_item = gtk_menu_item_new_with_label(_("Close Connection")); gtk_menu_append(GTK_MENU(menu), menu_item); - gtk_widget_show(menu_item); - gtk_signal_connect(GTK_OBJECT(menu_item), "activate", GTK_SIGNAL_FUNC(tree_row_close_connection), (gpointer) tab); - gtk_widget_show(menu_item); --- 576,593 ---- } + /* Export to LDIF */ + menu_item = gtk_menu_item_new_with_label(_("Export to LDIF")); + gtk_menu_append(GTK_MENU(menu), menu_item); + gtk_signal_connect(GTK_OBJECT(menu_item), "activate", + GTK_SIGNAL_FUNC(dump_server), + (gpointer) tab); + gtk_widget_show(menu_item); + /* Close connection */ menu_item = gtk_menu_item_new_with_label(_("Close Connection")); gtk_menu_append(GTK_MENU(menu), menu_item); gtk_signal_connect(GTK_OBJECT(menu_item), "activate", GTK_SIGNAL_FUNC(tree_row_close_connection), (gpointer) tab); gtk_widget_show(menu_item); |