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

export.c

Go to the documentation of this file.
00001 
00010 /* GtkSQL -- an interactive graphical query tool for PostgreSQL
00011  * Copyright (C) 1998-2003  Lionel ULMER, Darryl Luff.
00012  *
00013  * This program is free software; you can redistribute it andraw/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00026  */
00027 
00028 #include <string.h>
00029 #include "buf.h"
00030 #include "dialogs.h"
00031 #include "exporthtml.h"
00032 #include "exporttext.h"
00033 #include "gui_common.h"
00034 #include "queries.h"
00035 
00037 #define TYPE_KEY "Type key"
00038 
00040 typedef struct _export_data {
00041     GtkWidget *type_menu;   
00042     GtkWidget *query;       
00043     char *query_name;       
00044 } ExportData;
00045 
00047 static char *types[] = {
00048     "Choose from extension",
00049     "",
00050     "Html",
00051     "Text",
00052     NULL
00053 };
00054 
00055 static char *noExtensions[] = { NULL }; 
00056 static char *htmlExtensions[] = { "html", "htm", NULL };    
00057 static char *textExtensions[] = { "txt", "", NULL };        
00058 static char **typesExtensions[] = {
00059     noExtensions, noExtensions,
00060     htmlExtensions, textExtensions,
00061     NULL
00062 };
00063 
00065 void export_dialog_hide();
00066 
00068 void export_dialog_show(char *title);
00069 
00071 void export_dialog_create();
00072 
00074 static void (*exportFunctions[]) () = {
00075     NULL,
00076     NULL,
00077     export_as_html,
00078     export_as_text,
00079     NULL
00080 };
00081 
00083 static GtkWidget *exportDialog = NULL;
00084 
00086 static int exportDialogDisplayed = 0;
00087 
00089 ExportData exportData = {
00090     NULL,                              /* GtkWidget *type_menu; */
00091     NULL,                              /* GtkWidget *query; */
00092     NULL                               /* char *query_name; */
00093 };
00094 
00096 void add_entry_line(char *labeltext, GtkWidget * widget,
00097                            GtkWidget * table, int height)
00098 {
00099     GtkWidget *label;
00100 
00101     label = gtk_label_new(labeltext);
00102     gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1,
00103                               height, height + 1);
00104     gtk_widget_show(label);
00105 
00106     gtk_table_attach_defaults(GTK_TABLE(table), widget, 1, 2,
00107                               height, height + 1);
00108 }
00109 
00111 //static void common_cancel_callback(GtkWidget * widget, gpointer data)
00112 //{
00113 //  CommonExportData *cd = (CommonExportData *) data;
00114 //
00115 //  gtk_widget_hide(GTK_WIDGET(cd->dialog));
00116 //}
00117 
00120 int export_in_progress()
00121 {
00122     return exportDialogDisplayed;
00123 }
00124 
00127 static GtkWidget *create_type_menu()
00128 {
00129     GtkWidget *optionmenu, *menu;
00130     int i;
00131 
00132     optionmenu = gtk_option_menu_new();
00133     menu = gtk_menu_new();
00134 
00135     i = 0;
00136     while (types[i] != NULL) {
00137         GtkWidget *menuitem;
00138 
00139         if (strlen(types[i]) == 0)
00140             menuitem = gtk_menu_item_new();
00141         else
00142             menuitem = gtk_menu_item_new_with_label(types[i]);
00143 
00144         gtk_object_set_data(GTK_OBJECT(menuitem), TYPE_KEY, (gpointer) i);
00145 
00146         gtk_menu_append(GTK_MENU(menu), menuitem);
00147         gtk_widget_show(menuitem);
00148 
00149         i++;
00150     }
00151 
00152     gtk_option_menu_set_menu(GTK_OPTION_MENU(optionmenu), menu);
00153 
00154     return optionmenu;
00155 }
00156 
00160 void export_cancel_callback(GtkWidget * widget, gpointer data)
00161 {
00162     gtk_widget_hide(GTK_WIDGET(exportDialog));
00163     exportDialogDisplayed = 0;
00164     update_button_sensitivity();
00165 }
00166 
00170 void export_ok_callback(GtkWidget * widget, gpointer data)
00171 {
00172     GtkFileSelection *fs = GTK_FILE_SELECTION(exportDialog);
00173     GtkWidget *type_w;
00174     char *filename, *raw_filename;
00175     int type;
00176 
00177     /* We get the file name */
00178     filename = gtk_file_selection_get_filename(fs);
00179     raw_filename = gtk_entry_get_text(GTK_ENTRY(fs->selection_entry));
00180 
00181     if (((filename == NULL) || (strlen(filename) == 0)) ||
00182         ((raw_filename == NULL) || (strlen(raw_filename) == 0))) {
00183         display_error("Export Error", "No filename specified.");
00184         export_cancel_callback(widget, data);
00185         return;
00186     }
00187 
00188     /* Makes a copy of the resources. Make sure these are freed later. */
00189     filename = g_strdup(filename);
00190     raw_filename = g_strdup(raw_filename);
00191 
00192     /* First, we need to find what is in the menu */
00193     type_w = gtk_menu_get_active(GTK_MENU(exportData.type_menu));
00194     type = (int) gtk_object_get_data(GTK_OBJECT(type_w), TYPE_KEY);
00195     if (type == 0) {
00196         /* Need to find extension */
00197         int l = strlen(raw_filename), j;
00198         char *ext;
00199 
00200         while ((l >= 0) && (raw_filename[l] != '.'))
00201             l--;
00202 
00203         if (l < 0)
00204             ext = raw_filename + strlen(raw_filename);
00205         else
00206             ext = raw_filename + l + 1;
00207 
00208         /* Need to find corresponding code */
00209         type = 0;
00210         while (typesExtensions[type] != NULL) {
00211             j = 0;
00212             while (typesExtensions[type][j] != NULL) {
00213                 if (!strcasecmp(typesExtensions[type][j], ext))
00214                     goto end;
00215                 j++;
00216             }
00217             type++;
00218         }
00219 
00220       end:
00221         if (typesExtensions[type] == NULL) {
00222             g_free(filename);
00223             g_free(raw_filename);
00224             return;
00225         }
00226     }
00227 
00228     /* We call the corresponding dialog */
00229     if (exportFunctions[type] == NULL) {
00230         g_warning("Internal error. Export function not specified.\n");
00231         return;
00232     }
00233     (exportFunctions[type]) (filename, raw_filename,
00234                              GTK_TEXT(exportData.query),
00235                              exportData.query_name);
00236 
00237     /* Now, we can hide the dialog */
00238     export_dialog_hide();
00239     update_button_sensitivity();
00240     if (filename)
00241         g_free(filename);
00242 
00243     if (raw_filename)
00244         g_free(raw_filename);
00245 }
00246 
00251 gint delete_event(GtkWidget * widget, GdkEvent * event, gpointer data)
00252 {
00253     /* We call the cancel callback to hide the dialog.  */
00254     export_cancel_callback(widget, data);
00255 
00256     /* Don't delete it. It will be re-used. */
00257     return (TRUE);
00258 }
00259 
00261 void result_export()
00262 {
00264 #define EXPORTINGQUERY_S    "Exporting query '%s'"
00265     ABuf *buf = NULL;
00266     char *qname;
00267 
00268     if (exportDialogDisplayed)
00269         /* Should never happen */
00270         return;
00271 
00272     qname = query_get_current_name();
00273     buf = buf_new(strlen(EXPORTINGQUERY_S) + strlen(qname));
00274     if (!buf) {
00275         g_warning("ExportQueryDialog() could not allocate a buffer\n");
00276         return;
00277     }
00278 
00279     snprintf(buf->b_dat, buf->b_len, EXPORTINGQUERY_S, qname);
00280     export_dialog_show(buf->b_dat);
00281     buf_free(buf);
00282 }
00283 
00285 void export_dialog_hide()
00286 {
00287     if (exportDialog == NULL)
00288         return;
00289 
00290     exportDialogDisplayed = 0;
00291     gtk_widget_hide(exportDialog);
00292 }
00293 
00296 void export_dialog_show(char *title)
00297 {
00298     if (exportDialog == NULL) {
00299         export_dialog_create();
00300         if (exportDialog == NULL)
00301             return;
00302     }
00303 
00304     gtk_window_set_title(GTK_WINDOW(exportDialog), title);
00305 
00306     /* Setup the dialog data. */
00307     exportData.query = query_get_current();
00308     exportData.query_name = query_get_current_name();
00309     exportDialogDisplayed = 1;
00310     gtk_widget_show(exportDialog);
00311 }
00312 
00314 void export_dialog_create()
00315 {
00316     GtkWidget *frame, *menu;
00317     if (exportDialog != NULL)
00318         return;
00319 
00320     exportDialog = gtk_file_selection_new("Export...");
00321     gtk_file_selection_show_fileop_buttons(GTK_FILE_SELECTION(exportDialog));
00322     gtk_file_selection_set_filename(GTK_FILE_SELECTION(exportDialog), getenv("HOME"));
00323     exportDialogDisplayed = 0;
00324 
00325     /* Now, we create the save options */
00326     frame = gtk_frame_new("Export Type");
00327     gtk_container_border_width(GTK_CONTAINER(frame), 5);
00328 
00329     menu = create_type_menu();
00330     gtk_widget_show(menu);
00331     gtk_container_add(GTK_CONTAINER(frame), menu);
00332 
00333     gtk_widget_show(frame);
00334     gtk_box_pack_end(GTK_BOX(GTK_FILE_SELECTION(exportDialog)->main_vbox),
00335                      frame, FALSE, FALSE, 5);
00336 
00337     /* Setup the dialog data. */
00338     exportData.type_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(menu));
00339     exportData.query = query_get_current();
00340     exportData.query_name = query_get_current_name();
00341     exportData.type_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(menu));
00342 
00343     /* Now, we connect the signals */
00344     gtk_signal_connect(GTK_OBJECT(exportDialog), "delete_event",
00345                        GTK_SIGNAL_FUNC(delete_event), NULL);
00346     gtk_signal_connect(GTK_OBJECT
00347                        (GTK_FILE_SELECTION(exportDialog)->cancel_button),
00348                        "clicked", GTK_SIGNAL_FUNC(export_cancel_callback),
00349                        NULL);
00350     gtk_signal_connect(GTK_OBJECT
00351                        (GTK_FILE_SELECTION(exportDialog)->ok_button),
00352                        "clicked", GTK_SIGNAL_FUNC(export_ok_callback), NULL);
00353 }

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