Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

exporttext.c

Go to the documentation of this file.
00001 
00009 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00010  * Copyright (C) 1998-2003  Lionel ULMER, Darryl Luff.
00011  *
00012  * This program is free software; you can redistribute it andraw/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program; if not, write to the Free Software
00024  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00025  */
00026 
00027 #include <gtk/gtk.h>
00028 #include <string.h>
00029 #include "buf.h"
00030 #include "export.h"
00031 #include "guiapi.h"
00032 #include "queries.h"
00033 #include "status.h"
00034 #include "utils.h"
00035 
00037 typedef struct _text_export_data {
00038     GtkWidget *dialog;      
00039     char *filename;         
00040     char *raw_filename;     
00042     GtkWidget *query;       
00043     GtkWidget *separator;   
00044     GtkWidget *pprint;      
00045 } TextExportData;
00046 
00047 
00049 void export_text_dialog_hide();
00050 
00052 void export_text_dialog_show(char *title, char *filename, char *name,
00053                              GtkWidget * query);
00054 
00056 void export_text_dialog_create();
00057 
00059 static GtkWidget *exportTextDialog = NULL;
00060 
00062 static int exportTextDialogDisplayed = 0;
00063 
00065 TextExportData exportTextData = {
00066     NULL,                              /* GtkWidget *dialog; */
00067     NULL,                              /* char *filename; */
00068     NULL,                              /* char *raw_filename; */
00069     NULL,                              /* GtkWidget *query; */
00070     NULL,                              /* GtkWidget *separator; */
00071     NULL                               /* GtkWidget *pprint; */
00072 };
00073 
00077 static void text_export_ok_callback(GtkWidget * widget, gpointer data)
00078 {
00079     TextExportData *cd = (TextExportData *) data;
00080     int pprint;
00081     char *sep;
00082     char *tab = "\t", *space = " ";
00083     FILE *f;
00084     void *result;
00085     char *field;
00086     DBConnection *conn;
00087 
00088     /* Get the parameters */
00089     pprint = GTK_TOGGLE_BUTTON(cd->pprint)->active;
00090     sep = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(cd->separator)->entry));
00091     if (!strcmp(sep, "TAB"))
00092         sep = tab;
00093     else if (!strcmp(sep, "SPACE"))
00094         sep = space;
00095 
00096     /* Opens the file */
00097     if ((cd == NULL) || (cd->filename == NULL)) {
00098         g_warning("Null filename entered\n");
00099         return;
00100     }
00101 
00102     f = fopen(cd->filename, "wb");
00103     if (f == NULL) {
00104         perror("opening export file");
00105         return;
00106     }
00107 
00108     /* Start exporting ... */
00109     result = query_get_result(cd->query);
00110 
00111     conn = gui_get_connection();
00112     if ((result)
00113         && (conn->DBget_query_status(conn, result) == DB_QUERY_RECORDS_OK)) {
00114         int nb_tuples = conn->DBget_record_number(conn, result);
00115         int nb_cols = conn->DBget_field_number(conn, result);
00116         int *col_length = (int *) g_malloc(sizeof(int) * nb_cols);
00117         int i, j;
00118 
00119         if (nb_tuples > 0) {
00120             /* Pretty Printing */
00121             if (pprint) {
00122                 for (j = 0; j < nb_cols; j++)
00123                     col_length[j] = 0;
00124 
00125                 for (i = 0; i < nb_tuples; i++)
00126                     for (j = 0; j < nb_cols; j++) {
00127                         field = conn->DBget_field_value(conn, result, i, j);
00128                         if (field != NULL)
00129                             if (strlen(field) > col_length[j])
00130                                 col_length[j] = strlen(field);
00131                     }
00132             }
00133 
00134             status_push("Exporting Text...");
00135             progress_reset();
00136 
00137             /* Print the data */
00138             for (i = 0; i < nb_tuples; i++) {
00139                 for (j = 0; j < nb_cols; j++) {
00140                     if (j > 0)
00141                         fprintf(f, "%s", sep);
00142 
00143                     field = conn->DBget_field_value(conn, result, i, j);
00144                     if (!field)
00145                         field = "";
00146 
00147                     fprintf(f, "%s", field);
00148 
00149                     if (pprint)
00150                         add_space(f, strlen(field), col_length[j]);
00151                 }
00152                 progress_update(i / (float) nb_tuples);
00153                 fprintf(f, "\n");
00154             }
00155             g_free(col_length);
00156         }
00157     }
00158 
00159     /* Closes the file */
00160     fclose(f);
00161 
00162     /* Close the dialog */
00163     progress_reset();
00164     status_pop();
00165 
00166     export_text_dialog_hide();
00167 }
00168 
00170 static void export_text_cancel_callback(GtkWidget * widget, gpointer data)
00171 {
00172     CommonExportData *cd = (CommonExportData *) data;
00173 
00174     gtk_widget_hide(GTK_WIDGET(cd->dialog));
00175 }
00176 
00178 void export_text_dialog_create()
00179 {
00180 #define EXPORT_S "Export '%s' as text"
00181     GtkWidget *sep_combo;
00182     GtkWidget *table, *vbox;
00183     GtkWidget *pp_button;
00184     GtkWidget *ok_button, *cancel_button;
00185     ABuf *buf = NULL;
00186     char *sep[] = { ",", ";", "TAB", "SPACE", "-", "|", NULL };
00187     int i;
00188     GList *cbitems = NULL;
00189 
00190     if (exportTextDialog != NULL)
00191         return;
00192 
00193     /* The dialog */
00194     exportTextDialog = gtk_dialog_new();
00195     gtk_window_set_title(GTK_WINDOW(exportTextDialog), "Export Text...");
00196     gtk_window_position(GTK_WINDOW(exportTextDialog), GTK_WIN_POS_MOUSE);
00197     gtk_window_set_policy(GTK_WINDOW(exportTextDialog), FALSE, FALSE, FALSE);
00198 
00199     /* The vbox */
00200     vbox = gtk_vbox_new(FALSE, 5);
00201     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportTextDialog)->vbox), vbox,
00202                        FALSE, FALSE, 0);
00203     gtk_widget_show(vbox);
00204 
00205     /* The table to have a nice layout :-) */
00206     table = gtk_table_new(3, 2, FALSE);
00207     gtk_container_border_width(GTK_CONTAINER(table), 6);
00208 
00209     /* The combo box for the separator */
00210     for (i = 0; sep[i] != NULL; i++)
00211         cbitems = g_list_append(cbitems, sep[i]);
00212     sep_combo = gtk_combo_new();
00213     gtk_combo_set_popdown_strings(GTK_COMBO(sep_combo), cbitems);
00214     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(sep_combo)->entry), sep[0]);
00215     gtk_widget_show(sep_combo);
00216     add_entry_line("Separator : ", sep_combo, table, 0);
00217 
00218     /* End of table "layout" */
00219     gtk_widget_show(table);
00220     gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
00221 
00222     /* The pretty print button */
00223     pp_button = gtk_check_button_new_with_label("Pretty print");
00224     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(pp_button), FALSE);
00225     gtk_widget_show(pp_button);
00226     gtk_box_pack_start(GTK_BOX(vbox), pp_button, FALSE, FALSE, 0);
00227 
00228     ok_button = gtk_button_new_with_label("Ok");
00229     gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
00230                        GTK_SIGNAL_FUNC(text_export_ok_callback),
00231                        &exportTextData);
00232     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportTextDialog)->action_area),
00233                        ok_button, FALSE, FALSE, 0);
00234     GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
00235     gtk_widget_grab_default(ok_button);
00236     gtk_widget_show(ok_button);
00237     cancel_button = gtk_button_new_with_label("Cancel");
00238     gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
00239                        GTK_SIGNAL_FUNC(export_text_cancel_callback),
00240                        exportTextDialog);
00241     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportTextDialog)->action_area),
00242                        cancel_button, FALSE, FALSE, 0);
00243     gtk_widget_show(cancel_button);
00244 
00245     /* Now, the dialog data. */
00246     exportTextData.dialog = exportTextDialog;
00247     exportTextData.separator = sep_combo;
00248     exportTextData.pprint = pp_button;
00249 
00250     buf_free(buf);
00251 }
00252 
00254 void export_text_dialog_hide()
00255 {
00256     exportTextDialogDisplayed = 0;
00257     gtk_widget_hide(GTK_WIDGET(exportTextDialog));
00258 }
00259 
00265 void export_text_dialog_show(char *title, char *filename, char *rawfilename,
00266                              GtkWidget * query)
00267 {
00268     if (exportTextDialogDisplayed)
00269         return;
00270 
00271     if (exportTextDialog == NULL) {
00272         export_text_dialog_create();
00273         if (exportTextDialog == NULL)
00274             return;
00275     }
00276 
00277     if (exportTextData.filename)
00278         g_free(exportTextData.filename);
00279     exportTextData.filename = g_strdup(filename);
00280     
00281     if (exportTextData.raw_filename)
00282         g_free(exportTextData.raw_filename);
00283     exportTextData.raw_filename = g_strdup(rawfilename);
00284 
00285     exportTextData.query = query;
00286 
00287     gtk_window_set_title(GTK_WINDOW(exportTextDialog), title);
00288     exportTextDialogDisplayed = 1;
00289     gtk_widget_show(GTK_WIDGET(exportTextDialog));
00290 }
00291 
00297 void export_as_text(char *filename, char *rawfilename,
00298                            GtkWidget * query, char *query_name)
00299 {
00300     ABuf *buf = NULL;
00301 
00302     export_text_dialog_create();
00303     buf = buf_new(strlen(EXPORT_S) + strlen(query_name));
00304     snprintf(buf->b_dat, buf->b_len, "Export '%s' as text", query_name);
00305     export_text_dialog_show(buf->b_dat, filename, rawfilename, query);
00306 
00307     buf_free(buf);
00308 }

Generated on Sun May 9 19:19:16 2004 for GtkSQL by doxygen1.2.18