00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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,
00091 NULL,
00092 NULL
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
00112
00113
00114
00115
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
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
00189 filename = g_strdup(filename);
00190 raw_filename = g_strdup(raw_filename);
00191
00192
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
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
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
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
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
00254 export_cancel_callback(widget, data);
00255
00256
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
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
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
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
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
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 }