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

exporthtml.c

Go to the documentation of this file.
00001 
00008 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00009  * Copyright (C) 1998-2003  Lionel ULMER, Darryl Luff.
00010  *
00011  * This program is free software; you can redistribute it andraw/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024  */
00025 
00026 #include <string.h>
00027 #include "buf.h"
00028 #include "guiapi.h"
00029 #include "queries.h"
00030 #include "status.h"
00031 
00033 typedef struct _html_export_data {
00034     GtkWidget *dialog;          
00035     char *filename;             
00036     char *raw_filename;         
00038     GtkWidget *query;           
00039     GtkWidget *recPerPage;      
00040     GtkWidget *titles;          
00041     GtkWidget *links;           
00042 } HtmlExportData;
00043 
00045 void export_html_dialog_hide();
00046 
00048 void export_html_dialog_show(char *title, char *filename, char *name,
00049                              GtkWidget * query, char *query_name);
00050 
00052 void export_html_dialog_create();
00053 
00055 static void export_html_cancel_callback(GtkWidget * widget, gpointer data);
00056 
00058 static void export_html_ok_callback(GtkWidget * widget, gpointer data);
00059 
00061 static GtkWidget *exportHtmlDialog = NULL;
00062 
00064 static int exportHtmlDialogDisplayed = 0;
00065 
00067 HtmlExportData exportHtmlData = {
00068     NULL,                              /* GtkWidget *dialog; */
00069     NULL,                              /* char *filename; */
00070     NULL,                              /* char *raw_filename; */
00071     NULL,                              /* GtkWidget *query; */
00072     NULL,                              /* GtkWidget *rec_per_page; */
00073     NULL,                              /* GtkWidget *titles; */
00074     NULL                               /* GtkWidget *links; */
00075 };
00076 
00078 void export_html_dialog_create()
00079 {
00080     GtkWidget *table;
00081     GtkWidget *vbox, *hbox;
00082     GtkWidget *titles, *links, *label;
00083     GtkWidget *recPerPage;
00084     GtkWidget *ok_button, *cancel_button;
00085 
00086     if (exportHtmlDialog)
00087         return;
00088 
00089     exportHtmlDialog = gtk_dialog_new();
00090     gtk_window_set_title(GTK_WINDOW(exportHtmlDialog), "Export html...");
00091     gtk_window_position(GTK_WINDOW(exportHtmlDialog), GTK_WIN_POS_MOUSE);
00092     gtk_window_set_policy(GTK_WINDOW(exportHtmlDialog), FALSE, FALSE, FALSE);
00093 
00094     vbox = gtk_vbox_new(FALSE, 5);
00095     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportHtmlDialog)->vbox), vbox,
00096                        FALSE, FALSE, 0);
00097     gtk_widget_show(vbox);
00098 
00099     table = gtk_table_new(2, 2, FALSE);
00100 
00101     hbox = gtk_hbox_new(FALSE, 5);
00102     label = gtk_label_new("Records per page: ");
00103     gtk_widget_show(label);
00104     gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 5);
00105 
00106     recPerPage = gtk_entry_new();
00107     gtk_entry_set_text(GTK_ENTRY(recPerPage), "40");
00108     gtk_widget_show(recPerPage);
00109 
00110     gtk_box_pack_start(GTK_BOX(hbox), recPerPage, TRUE, FALSE, 5);
00111     gtk_widget_show(hbox);
00112     gtk_table_attach_defaults(GTK_TABLE(table), hbox, 0, 2, 0, 1);
00113 
00114     links = gtk_check_button_new_with_label("Include links");
00115     gtk_widget_show(links);
00116     gtk_table_attach_defaults(GTK_TABLE(table), links, 0, 1, 1, 2);
00117 
00118     titles = gtk_check_button_new_with_label("Include columns titles");
00119     gtk_widget_show(titles);
00120     gtk_table_attach_defaults(GTK_TABLE(table), titles, 1, 2, 1, 2);
00121 
00122     gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, FALSE, 5);
00123     gtk_widget_show(table);
00124 
00125     exportHtmlData.titles = titles;
00126     exportHtmlData.links = links;
00127     exportHtmlData.recPerPage = recPerPage;
00128 
00129     ok_button = gtk_button_new_with_label("Ok");
00130     gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
00131                        GTK_SIGNAL_FUNC(export_html_ok_callback),
00132                        &exportHtmlData);
00133     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportHtmlDialog)->action_area),
00134                        ok_button, FALSE, FALSE, 0);
00135     GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
00136     gtk_widget_grab_default(ok_button);
00137     gtk_widget_show(ok_button);
00138     cancel_button = gtk_button_new_with_label("Cancel");
00139     gtk_signal_connect(GTK_OBJECT(cancel_button), "clicked",
00140                        GTK_SIGNAL_FUNC(export_html_cancel_callback),
00141                        exportHtmlDialog);
00142     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(exportHtmlDialog)->action_area),
00143                        cancel_button, FALSE, FALSE, 0);
00144     gtk_widget_show(cancel_button);
00145     // gtk_widget_show(exportHtmlDialog);
00146 }
00147 
00149 void export_html_dialog_hide()
00150 {
00151     exportHtmlDialogDisplayed = 0;
00152     gtk_widget_hide(GTK_WIDGET(exportHtmlDialog));
00153 }
00154 
00161 void export_html_dialog_show(char *title, char *filename, char *rawfilename,
00162                              GtkWidget * query, char *query_name)
00163 {
00164     ABuf *buf = NULL;
00165     void *result;
00166     DBConnection *conn;
00167 
00168     if (exportHtmlDialogDisplayed)
00169         return;
00170     
00171     if (exportHtmlDialog == NULL) {
00172         export_html_dialog_create();
00173         if (exportHtmlDialog == NULL)
00174             return;
00175     }
00176 
00177     /* Find the number of rows in the result.   */
00178     conn = gui_get_connection();
00179     result = query_get_result(GTK_WIDGET(query));
00180     if (conn->DBget_query_status(conn, result) != DB_QUERY_RECORDS_OK)
00181         return;
00182 
00183     buf = buf_check(buf, 50);
00184     snprintf(buf->b_dat, buf->b_len, "%d",
00185              conn->DBget_record_number(conn, result));
00186     gtk_entry_set_text(GTK_ENTRY(exportHtmlData.recPerPage), buf->b_dat);
00187 
00188     if (exportHtmlData.filename)
00189         g_free(exportHtmlData.filename);
00190     exportHtmlData.filename = g_strdup(filename);
00191     
00192     if (exportHtmlData.raw_filename)
00193         g_free(exportHtmlData.raw_filename);
00194     exportHtmlData.raw_filename = g_strdup(rawfilename);
00195 
00196     exportHtmlData.query = query;
00197 
00198     gtk_window_set_title(GTK_WINDOW(exportHtmlDialog), title);
00199     exportHtmlDialogDisplayed = 1;
00200     gtk_widget_show(GTK_WIDGET(exportHtmlDialog));
00201 }
00202 
00206 static void export_html_ok_callback(GtkWidget * widget, gpointer data)
00207 {
00209 #define HTMLFILE_S  "%s%03d.html"
00210     HtmlExportData *cd = (HtmlExportData *) data;
00211     FILE *f = NULL;
00212     void *result;
00213     char *basename, *lbase = NULL;
00214     ABuf *current_file = NULL;
00215     int currentPage, recPerPage;
00216     int links, titles;
00217     int nb_tuples;
00218     int nb_cols;
00219     int i, j;
00220     int col;
00221     gchar *queryText = NULL;
00222     DBConnection *conn = gui_get_connection();
00223 
00224     /* Gets all the info we need before exporting */
00225     result = query_get_result(GTK_WIDGET(cd->query));
00226 
00227     if (conn->DBget_query_status(conn, result) != DB_QUERY_RECORDS_OK)
00228         return;
00229     nb_tuples = conn->DBget_record_number(conn, result);
00230     nb_cols = conn->DBget_field_number(conn, result);
00231 
00232     basename = gtk_entry_get_text(GTK_ENTRY(cd->recPerPage));
00233     recPerPage = atoi(basename);
00234     if (recPerPage < 1) {
00235         g_warning("Invalid records per page\n");
00236         return;
00237     }
00238     basename = NULL;
00239     links = GTK_TOGGLE_BUTTON(cd->links)->active;
00240     titles = GTK_TOGGLE_BUTTON(cd->titles)->active;
00241 
00242     if (recPerPage < nb_tuples) {
00243         basename = strdup(cd->filename);
00244         for (i = strlen(basename); (i > 0) && (basename[i] != '.'); i--) ;
00245         basename[i] = '\0';
00246         for (j = i; (j > 0) && (basename[j] != '/'); j--) ;
00247         lbase = strdup(basename + j + 1);
00248     }
00249 
00250     /* Start exporting */
00251     status_push("Exporting HTML...");
00252     progress_reset();
00253     currentPage = 0;
00254 
00255     queryText = query_get_text(GTK_WIDGET(cd->query));
00256 
00257     for (i = 0; i < nb_tuples; i++) {
00258         if ((i % recPerPage) == 0) {
00259             if (recPerPage < nb_tuples) {
00260                 if (currentPage != 0) {
00261                     fprintf(f, "</TABLE>\n");
00262                     fclose(f);
00263                 }
00264                 current_file =
00265                     buf_check(current_file,
00266                               strlen(HTMLFILE_S) + strlen(basename) + 50);
00267                 snprintf(current_file->b_dat, current_file->b_len, HTMLFILE_S,
00268                          basename, currentPage++);
00269                 f = fopen(current_file->b_dat, "w");
00270                 if (links) {
00271                     if (currentPage != 1)
00272                         fprintf(f, "<A HREF=\"%s%03d.html\">Previous</A> ",
00273                                 lbase, currentPage - 2);
00274                     if (nb_tuples > (currentPage + 1) * recPerPage)
00275                         fprintf(f, " <A HREF=\"%s%03d.html\">Next</A>", lbase,
00276                                 currentPage);
00277                 }
00278                 fprintf(f, "%s", "<TABLE border>\n");
00279                 if (currentPage == 0)
00280                     fprintf(f, "<CAPTION>%s</CAPTION>\n", queryText);
00281                 else
00282                     fprintf(f, "<CAPTION>%s (continued...)</CAPTION>\n", queryText);
00283 
00284                 if (titles) {
00285                     fprintf(f, "%s", " <TR>\n");
00286                     for (col = 0; col < nb_cols; col++) {
00287                         fprintf(f, "  <TH>%s</TH>\n",
00288                                 conn->DBget_field_name(conn, result, col));
00289                     }
00290                     fprintf(f, "%s", " </TR>\n");
00291                 }
00292             }
00293             else if (i == 0) {
00294                 f = fopen(cd->filename, "w");
00295                 fprintf(f, "%s", "<TABLE border>\n");
00296                 fprintf(f, "<CAPTION>%s</CAPTION>\n", queryText);
00297                 if (titles) {
00298                     fprintf(f, "%s", " <TR>\n");
00299                     for (col = 0; col < nb_cols; col++) {
00300                         fprintf(f, "  <TH>%s</TH>\n",
00301                                 conn->DBget_field_name(conn, result, col));
00302                     }
00303                     fprintf(f, "%s", " </TR>\n");
00304                 }
00305             }
00306         }
00307 
00308         /* Exports the line */
00309         fprintf(f, "%s", " <TR>\n");
00310         for (j = 0; j < nb_cols; j++) {
00311             fprintf(f, "  <TD> %s </TD>\n",
00312                     conn->DBget_field_value(conn, result, i, j));
00313         }
00314         progress_update(i / (float) nb_tuples);
00315         fprintf(f, "%s", " </TR>\n");
00316     }
00317 
00318     /* Closes the file */
00319     fprintf(f, "%s", "</TABLE>\n");
00320     fclose(f);
00321 
00322     progress_reset();
00323     status_pop();
00324 
00325     /* Memory cleaning */
00326     g_free(basename);
00327     if (queryText != NULL)
00328         g_free(queryText);
00329 
00330     buf_free(current_file);
00331 
00332     export_html_dialog_hide();
00333 }
00334 
00338 static void export_html_cancel_callback(GtkWidget * widget, gpointer data)
00339 {
00340     gtk_widget_hide(GTK_WIDGET(data));
00341 }
00342 
00348 void export_as_html(char *filename, char *rawfilename,
00349                            GtkWidget * query, char *query_name)
00350 {
00352 #define EXPORTHTML_S    "Export '%s' as HTML"
00353     ABuf *buf = NULL;
00354 
00355     export_html_dialog_create();
00356     buf = buf_new(strlen(EXPORTHTML_S) + strlen(query_name));
00357     if (!buf) {
00358         g_error("ExportAsHtml() could not allocate memory for a buffer\n");
00359         return;
00360     }
00361 
00362     snprintf(buf->b_dat, buf->b_len, EXPORTHTML_S, query_name);
00363     export_html_dialog_show(buf->b_dat, filename, rawfilename, query,
00364                             query_name);
00365     buf_free(buf);
00366 }

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