00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "config.h"
00026
00027 #include <gtk/gtk.h>
00028 #include <glib/gi18n.h>
00029 #include <glade/glade.h>
00030
00031 #include "gnc-ui.h"
00032 #include "gnc-ui-util.h"
00033 #include "gnc-uri-utils.h"
00034 #include "dialog-utils.h"
00035 #include "dialog-file-access.h"
00036 #include "gnc-file.h"
00037 #include "gnc-plugin-file-history.h"
00038 #include "gnc-session.h"
00039
00040 static QofLogModule log_module = GNC_MOD_GUI;
00041
00042 #define DEFAULT_HOST "localhost"
00043 #define DEFAULT_DATABASE "gnucash"
00044 #define FILE_ACCESS_OPEN 0
00045 #define FILE_ACCESS_SAVE_AS 1
00046 #define FILE_ACCESS_EXPORT 2
00047
00048 typedef struct FileAccessWindow
00049 {
00050
00051 int type;
00052
00053 GtkWidget* dialog;
00054 GtkWidget* frame_file;
00055 GtkWidget* frame_database;
00056 GtkFileChooser* fileChooser;
00057 GtkComboBox* cb_uri_type;
00058 GtkEntry* tf_host;
00059 GtkEntry* tf_database;
00060 GtkEntry* tf_username;
00061 GtkEntry* tf_password;
00062 } FileAccessWindow;
00063
00064 void gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser,
00065 FileAccessWindow *faw );
00066 void gnc_ui_file_access_response_cb( GtkDialog *, gint, GtkDialog * );
00067 static void cb_uri_type_changed_cb( GtkComboBox* cb );
00068
00069 static gchar*
00070 geturl( FileAccessWindow* faw )
00071 {
00072 gchar* url = NULL;
00073 const gchar* host;
00074 const gchar* database;
00075 const gchar* username;
00076 const gchar* password;
00077 const gchar* type;
00078 const gchar* file;
00079 const gchar* path;
00080
00081 host = gtk_entry_get_text( faw->tf_host );
00082 database = gtk_entry_get_text( faw->tf_database );
00083 username = gtk_entry_get_text( faw->tf_username );
00084 password = gtk_entry_get_text( faw->tf_password );
00085 file = gtk_file_chooser_get_filename( faw->fileChooser );
00086
00087 type = gtk_combo_box_get_active_text( faw->cb_uri_type );
00088 if ( gnc_uri_is_file_protocol( type ) )
00089 {
00090 if ( file == NULL )
00091 return NULL;
00092 else
00093 path = file;
00094 }
00095 else
00096 path = database;
00097
00098 url = gnc_uri_create_uri (type, host, 0, username, password, path);
00099
00100 return url;
00101 }
00102
00103 void
00104 gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser, FileAccessWindow *faw )
00105 {
00106 g_return_if_fail( chooser != NULL );
00107
00108 gnc_ui_file_access_response_cb( GTK_DIALOG(faw->dialog), GTK_RESPONSE_OK, NULL );
00109 }
00110
00111 void
00112 gnc_ui_file_access_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
00113 {
00114 FileAccessWindow* faw;
00115 gchar* url;
00116
00117 g_return_if_fail( dialog != NULL );
00118
00119 faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
00120 g_return_if_fail( faw != NULL );
00121
00122 switch ( response )
00123 {
00124 case GTK_RESPONSE_HELP:
00125 gnc_gnome_help( HF_HELP, HL_GLOBPREFS );
00126 break;
00127
00128 case GTK_RESPONSE_OK:
00129 url = geturl( faw );
00130 if ( url == NULL )
00131 {
00132 return;
00133 }
00134 if ( faw->type == FILE_ACCESS_OPEN )
00135 {
00136 gnc_file_open_file( url );
00137 }
00138 else if ( faw->type == FILE_ACCESS_SAVE_AS )
00139 {
00140 gnc_file_do_save_as( url );
00141 }
00142 else if ( faw->type == FILE_ACCESS_EXPORT )
00143 {
00144 gnc_file_do_export( url );
00145 }
00146 break;
00147
00148 case GTK_RESPONSE_CANCEL:
00149 break;
00150
00151 default:
00152 PERR( "Invalid response" );
00153 break;
00154 }
00155
00156 if ( response != GTK_RESPONSE_HELP )
00157 {
00158 gtk_widget_destroy( GTK_WIDGET(dialog) );
00159 }
00160 }
00161
00162
00163 static void
00164 set_widget_sensitivity( FileAccessWindow* faw, gboolean is_file_based_uri )
00165 {
00166 if (is_file_based_uri)
00167 {
00168 gtk_widget_show(faw->frame_file);
00169 gtk_widget_hide(faw->frame_database);
00170 }
00171 else
00172 {
00173 gtk_widget_show(faw->frame_database);
00174 gtk_widget_hide(faw->frame_file);
00175 }
00176
00177
00178 }
00179
00180 static void
00181 set_widget_sensitivity_for_uri_type( FileAccessWindow* faw, const gchar* uri_type )
00182 {
00183 if ( strcmp( uri_type, "file" ) == 0 || strcmp( uri_type, "xml" ) == 0
00184 || strcmp( uri_type, "sqlite3" ) == 0 )
00185 {
00186 set_widget_sensitivity( faw, TRUE );
00187 }
00188 else if ( strcmp( uri_type, "mysql" ) == 0 || strcmp( uri_type, "postgres" ) == 0 )
00189 {
00190 set_widget_sensitivity( faw, FALSE );
00191 }
00192 else
00193 {
00194 g_assert( FALSE );
00195 }
00196 }
00197
00198 static void
00199 cb_uri_type_changed_cb( GtkComboBox* cb )
00200 {
00201 GtkWidget* dialog;
00202 FileAccessWindow* faw;
00203 const gchar* type;
00204
00205 g_return_if_fail( cb != NULL );
00206
00207 dialog = gtk_widget_get_toplevel( GTK_WIDGET(cb) );
00208 g_return_if_fail( dialog != NULL );
00209 faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
00210 g_return_if_fail( faw != NULL );
00211
00212 type = gtk_combo_box_get_active_text( cb );
00213 set_widget_sensitivity_for_uri_type( faw, type );
00214 }
00215
00216 static const char*
00217 get_default_database( void )
00218 {
00219 const gchar* default_db;
00220
00221 default_db = g_getenv( "GNC_DEFAULT_DATABASE" );
00222 if ( default_db == NULL )
00223 {
00224 default_db = DEFAULT_DATABASE;
00225 }
00226
00227 return default_db;
00228 }
00229
00230 static void
00231 gnc_ui_file_access( int type )
00232 {
00233 FileAccessWindow *faw;
00234 GladeXML* xml;
00235 GtkWidget* box;
00236 GList* ds_node;
00237 GtkButton* op;
00238 GtkWidget* align;
00239 GtkFileChooserWidget* fileChooser;
00240 GtkFileChooserAction fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
00241 GList* list;
00242 GList* node;
00243 GtkWidget* uri_type_container;
00244 gboolean need_access_method_file = FALSE;
00245 gboolean need_access_method_mysql = FALSE;
00246 gboolean need_access_method_postgres = FALSE;
00247 gboolean need_access_method_sqlite3 = FALSE;
00248 gboolean need_access_method_xml = FALSE;
00249 gint access_method_index = -1;
00250 gint active_access_method_index = -1;
00251 const gchar* default_db;
00252 const gchar *button_label = NULL;
00253 const gchar *gconf_section = NULL;
00254 gchar *last;
00255 gchar *starting_dir = NULL;
00256
00257 g_return_if_fail( type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS || type == FILE_ACCESS_EXPORT );
00258
00259 faw = g_new0(FileAccessWindow, 1);
00260 g_return_if_fail( faw != NULL );
00261
00262 faw->type = type;
00263
00264
00265 xml = gnc_glade_xml_new( "dialog-file-access.glade", "File Access" );
00266 faw->dialog = glade_xml_get_widget( xml, "File Access" );
00267 g_object_set_data_full( G_OBJECT(faw->dialog), "FileAccessWindow", faw,
00268 g_free );
00269
00270 faw->frame_file = glade_xml_get_widget( xml, "frame_file" );
00271 faw->frame_database = glade_xml_get_widget( xml, "frame_database" );
00272 faw->tf_host = GTK_ENTRY(glade_xml_get_widget( xml, "tf_host" ));
00273 gtk_entry_set_text( faw->tf_host, DEFAULT_HOST );
00274 faw->tf_database = GTK_ENTRY(glade_xml_get_widget( xml, "tf_database" ));
00275 default_db = get_default_database();
00276 gtk_entry_set_text( faw->tf_database, default_db );
00277 faw->tf_username = GTK_ENTRY(glade_xml_get_widget( xml, "tf_username" ));
00278 faw->tf_password = GTK_ENTRY(glade_xml_get_widget( xml, "tf_password" ));
00279
00280 switch ( type )
00281 {
00282 case FILE_ACCESS_OPEN:
00283 gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Open..."));
00284 button_label = "gtk-open";
00285 fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
00286 gconf_section = GCONF_DIR_OPEN_SAVE;
00287 break;
00288
00289 case FILE_ACCESS_SAVE_AS:
00290 gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Save As..."));
00291 button_label = "gtk-save-as";
00292 fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
00293 gconf_section = GCONF_DIR_OPEN_SAVE;
00294 break;
00295
00296 case FILE_ACCESS_EXPORT:
00297 gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Export"));
00298 button_label = "gtk-save-as";
00299 fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
00300 gconf_section = GCONF_DIR_EXPORT;
00301 break;
00302 }
00303
00304 op = GTK_BUTTON(glade_xml_get_widget( xml, "pb_op" ));
00305 if ( op != NULL )
00306 {
00307 gtk_button_set_label( op, button_label );
00308 gtk_button_set_use_stock( op, TRUE );
00309 }
00310
00311 align = glade_xml_get_widget( xml, "alignment_file_chooser" );
00312 fileChooser = GTK_FILE_CHOOSER_WIDGET(gtk_file_chooser_widget_new( fileChooserAction ));
00313 faw->fileChooser = GTK_FILE_CHOOSER(fileChooser);
00314 gtk_container_add( GTK_CONTAINER(align), GTK_WIDGET(fileChooser) );
00315
00316
00317 if (type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS)
00318 {
00319 last = gnc_history_get_last();
00320 if ( last && gnc_uri_is_file_uri ( last ) )
00321 {
00322 gchar *filepath = gnc_uri_get_path ( last );
00323 starting_dir = g_path_get_dirname( filepath );
00324 g_free ( filepath );
00325 }
00326 }
00327 if (!starting_dir)
00328 starting_dir = gnc_get_default_directory(gconf_section);
00329 gtk_file_chooser_set_current_folder(faw->fileChooser, starting_dir);
00330
00331 g_object_connect( G_OBJECT(faw->fileChooser), "signal::file-activated",
00332 gnc_ui_file_access_file_activated_cb, faw, NULL );
00333
00334 uri_type_container = glade_xml_get_widget( xml, "vb_uri_type_container" );
00335 faw->cb_uri_type = GTK_COMBO_BOX(gtk_combo_box_new_text());
00336 gtk_container_add( GTK_CONTAINER(uri_type_container), GTK_WIDGET(faw->cb_uri_type) );
00337 gtk_box_set_child_packing( GTK_BOX(uri_type_container), GTK_WIDGET(faw->cb_uri_type),
00338 TRUE, FALSE, 0, GTK_PACK_START );
00339 g_object_connect( G_OBJECT(faw->cb_uri_type),
00340 "signal::changed", cb_uri_type_changed_cb, NULL,
00341 NULL );
00342
00343
00344 glade_xml_signal_autoconnect_full( xml, gnc_glade_autoconnect_full_func,
00345 faw->dialog );
00346
00347
00348 list = qof_backend_get_registered_access_method_list();
00349 for ( node = list; node != NULL; node = node->next )
00350 {
00351 const gchar* access_method = node->data;
00352
00353
00354
00355
00356 if ( strcmp( access_method, "mysql" ) == 0 )
00357 {
00358 need_access_method_mysql = TRUE;
00359 }
00360 else if ( strcmp( access_method, "postgres" ) == 0 )
00361 {
00362 need_access_method_postgres = TRUE;
00363 }
00364 else if ( strcmp( access_method, "xml" ) == 0 )
00365 {
00366 if ( type == FILE_ACCESS_OPEN )
00367 {
00368 need_access_method_file = TRUE;
00369 }
00370 else
00371 {
00372 need_access_method_xml = TRUE;
00373 }
00374 }
00375 else if ( strcmp( access_method, "sqlite3" ) == 0 )
00376 {
00377 if ( type == FILE_ACCESS_OPEN )
00378 {
00379 need_access_method_file = TRUE;
00380 }
00381 else
00382 {
00383 need_access_method_sqlite3 = TRUE;
00384 }
00385 }
00386 }
00387 g_list_free(list);
00388
00389
00390
00391 access_method_index = -1;
00392 if ( need_access_method_file )
00393 {
00394 gtk_combo_box_append_text( faw->cb_uri_type, "file" );
00395 active_access_method_index = ++access_method_index;
00396 }
00397 if ( need_access_method_mysql )
00398 {
00399 gtk_combo_box_append_text( faw->cb_uri_type, "mysql" );
00400 ++access_method_index;
00401 }
00402 if ( need_access_method_postgres )
00403 {
00404 gtk_combo_box_append_text( faw->cb_uri_type, "postgres" );
00405 ++access_method_index;
00406 }
00407 if ( need_access_method_sqlite3 )
00408 {
00409 gtk_combo_box_append_text( faw->cb_uri_type, "sqlite3" );
00410 active_access_method_index = ++access_method_index;
00411 }
00412 if ( need_access_method_xml )
00413 {
00414 gtk_combo_box_append_text( faw->cb_uri_type, "xml" );
00415 ++access_method_index;
00416
00417
00418
00419 active_access_method_index = access_method_index;
00420 }
00421 g_assert( active_access_method_index >= 0 );
00422
00423
00424 g_object_set_data_full( G_OBJECT(faw->dialog), "dialog-file-access.glade",
00425 xml, g_object_unref );
00426
00427
00428 gtk_widget_show_all( faw->dialog );
00429
00430
00431
00432 gtk_combo_box_set_active( faw->cb_uri_type, active_access_method_index );
00433 set_widget_sensitivity_for_uri_type( faw, gtk_combo_box_get_active_text( faw->cb_uri_type ) );
00434 }
00435
00436 void
00437 gnc_ui_file_access_for_open( void )
00438 {
00439 gnc_ui_file_access( FILE_ACCESS_OPEN );
00440 }
00441
00442
00443 void
00444 gnc_ui_file_access_for_save_as( void )
00445 {
00446 gnc_ui_file_access( FILE_ACCESS_SAVE_AS );
00447 }
00448
00449
00450 void
00451 gnc_ui_file_access_for_export( void )
00452 {
00453 gnc_ui_file_access( FILE_ACCESS_EXPORT );
00454 }