28 #include <glib/gi18n.h>
33 #include "dialog-utils.h"
37 #include "gnc-session.h"
41 #define DEFAULT_HOST "localhost"
42 #define DEFAULT_DATABASE "gnucash"
43 #define FILE_ACCESS_OPEN 0
44 #define FILE_ACCESS_SAVE_AS 1
45 #define FILE_ACCESS_EXPORT 2
53 GtkWidget *frame_file;
54 GtkWidget *frame_database;
55 GtkWidget *readonly_checkbutton;
56 GtkFileChooser *fileChooser;
58 GtkComboBoxText *cb_uri_type;
60 GtkEntry *tf_database;
61 GtkEntry *tf_username;
62 GtkEntry *tf_password;
65 void gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser,
67 void gnc_ui_file_access_response_cb( GtkDialog *, gint, GtkDialog * );
68 static void cb_uri_type_changed_cb( GtkComboBoxText* cb );
75 const gchar* database;
76 const gchar* username;
77 const gchar* password;
82 host = gtk_entry_get_text( faw->tf_host );
83 database = gtk_entry_get_text( faw->tf_database );
84 username = gtk_entry_get_text( faw->tf_username );
85 password = gtk_entry_get_text( faw->tf_password );
86 file = gtk_file_chooser_get_filename( faw->fileChooser );
88 type = gtk_combo_box_text_get_active_text(faw->cb_uri_type );
105 gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser,
FileAccessWindow *faw )
107 g_return_if_fail( chooser != NULL );
109 gnc_ui_file_access_response_cb( GTK_DIALOG(faw->dialog), GTK_RESPONSE_OK, NULL );
113 gnc_ui_file_access_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
118 g_return_if_fail( dialog != NULL );
120 faw = g_object_get_data( G_OBJECT(dialog),
"FileAccessWindow" );
121 g_return_if_fail( faw != NULL );
125 case GTK_RESPONSE_HELP:
129 case GTK_RESPONSE_OK:
136 if ( g_file_test( g_filename_from_uri( url, NULL, NULL ),
137 G_FILE_TEST_IS_DIR ))
139 gtk_file_chooser_set_current_folder_uri( faw->fileChooser, url );
142 if ( faw->type == FILE_ACCESS_OPEN )
144 gboolean open_readonly = faw->readonly_checkbutton
145 ? gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(faw->readonly_checkbutton))
147 gnc_file_open_file( url, open_readonly );
149 else if ( faw->type == FILE_ACCESS_SAVE_AS )
151 gnc_file_do_save_as( url );
153 else if ( faw->type == FILE_ACCESS_EXPORT )
155 gnc_file_do_export( url );
159 case GTK_RESPONSE_CANCEL:
163 PERR(
"Invalid response" );
167 if ( response != GTK_RESPONSE_HELP )
169 gtk_widget_destroy( GTK_WIDGET(dialog) );
177 if (is_file_based_uri)
179 gtk_widget_show(faw->frame_file);
180 gtk_widget_hide(faw->frame_database);
181 gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
185 gtk_widget_show(faw->frame_database);
186 gtk_widget_hide(faw->frame_file);
193 set_widget_sensitivity_for_uri_type(
FileAccessWindow* faw,
const gchar* uri_type )
195 if ( strcmp( uri_type,
"file" ) == 0 || strcmp( uri_type,
"xml" ) == 0
196 || strcmp( uri_type,
"sqlite3" ) == 0 )
198 set_widget_sensitivity( faw, TRUE );
200 else if ( strcmp( uri_type,
"mysql" ) == 0 || strcmp( uri_type,
"postgres" ) == 0 )
202 set_widget_sensitivity( faw, FALSE );
211 cb_uri_type_changed_cb( GtkComboBoxText* cb )
217 g_return_if_fail( cb != NULL );
219 dialog = gtk_widget_get_toplevel( GTK_WIDGET(cb) );
220 g_return_if_fail( dialog != NULL );
221 faw = g_object_get_data( G_OBJECT(dialog),
"FileAccessWindow" );
222 g_return_if_fail( faw != NULL );
224 type = gtk_combo_box_text_get_active_text( cb );
225 set_widget_sensitivity_for_uri_type( faw, type );
229 get_default_database(
void )
231 const gchar* default_db;
233 default_db = g_getenv(
"GNC_DEFAULT_DATABASE" );
234 if ( default_db == NULL )
236 default_db = DEFAULT_DATABASE;
243 gnc_ui_file_access(
int type )
248 GtkWidget* file_chooser;
249 GtkFileChooserWidget* fileChooser;
250 GtkFileChooserAction fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
253 GtkWidget* uri_type_container;
254 gboolean need_access_method_file = FALSE;
255 gboolean need_access_method_mysql = FALSE;
256 gboolean need_access_method_postgres = FALSE;
257 gboolean need_access_method_sqlite3 = FALSE;
258 gboolean need_access_method_xml = FALSE;
259 gint access_method_index = -1;
260 gint active_access_method_index = -1;
261 const gchar* default_db;
262 const gchar *button_label = NULL;
263 const gchar *settings_section = NULL;
266 g_return_if_fail( type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS || type == FILE_ACCESS_EXPORT );
269 g_return_if_fail( faw != NULL );
272 faw->starting_dir = NULL;
275 builder = gtk_builder_new();
276 gnc_builder_add_from_file (builder,
"dialog-file-access.glade",
"File Access" );
277 faw->dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"File Access" ));
278 g_object_set_data_full( G_OBJECT(faw->dialog),
"FileAccessWindow", faw,
281 faw->frame_file = GTK_WIDGET(gtk_builder_get_object (builder,
"frame_file" ));
282 faw->frame_database = GTK_WIDGET(gtk_builder_get_object (builder,
"frame_database" ));
283 faw->readonly_checkbutton = GTK_WIDGET(gtk_builder_get_object (builder,
"readonly_checkbutton"));
284 faw->tf_host = GTK_ENTRY(gtk_builder_get_object (builder,
"tf_host" ));
285 gtk_entry_set_text( faw->tf_host, DEFAULT_HOST );
286 faw->tf_database = GTK_ENTRY(gtk_builder_get_object (builder,
"tf_database" ));
287 default_db = get_default_database();
288 gtk_entry_set_text( faw->tf_database, default_db );
289 faw->tf_username = GTK_ENTRY(gtk_builder_get_object (builder,
"tf_username" ));
290 faw->tf_password = GTK_ENTRY(gtk_builder_get_object (builder,
"tf_password" ));
294 case FILE_ACCESS_OPEN:
295 gtk_window_set_title(GTK_WINDOW(faw->dialog), _(
"Open..."));
296 button_label =
"gtk-open";
297 fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
298 settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
301 case FILE_ACCESS_SAVE_AS:
302 gtk_window_set_title(GTK_WINDOW(faw->dialog), _(
"Save As..."));
303 button_label =
"gtk-save-as";
304 fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
305 settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
306 gtk_widget_destroy(faw->readonly_checkbutton);
307 faw->readonly_checkbutton = NULL;
310 case FILE_ACCESS_EXPORT:
311 gtk_window_set_title(GTK_WINDOW(faw->dialog), _(
"Export"));
312 button_label =
"gtk-save-as";
313 fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
314 settings_section = GNC_PREFS_GROUP_EXPORT;
315 gtk_widget_destroy(faw->readonly_checkbutton);
316 faw->readonly_checkbutton = NULL;
320 op = GTK_BUTTON(gtk_builder_get_object (builder,
"pb_op" ));
323 gtk_button_set_label( op, button_label );
324 gtk_button_set_use_stock( op, TRUE );
327 file_chooser = GTK_WIDGET(gtk_builder_get_object (builder,
"file_chooser" ));
328 fileChooser = GTK_FILE_CHOOSER_WIDGET(gtk_file_chooser_widget_new( fileChooserAction ));
329 faw->fileChooser = GTK_FILE_CHOOSER(fileChooser);
330 gtk_box_pack_start( GTK_BOX(file_chooser), GTK_WIDGET(fileChooser), TRUE, TRUE, 6 );
333 if (type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS)
339 faw->starting_dir = g_path_get_dirname( filepath );
343 if (!faw->starting_dir)
344 faw->starting_dir = gnc_get_default_directory(settings_section);
345 gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
347 g_object_connect( G_OBJECT(faw->fileChooser),
"signal::file-activated",
348 gnc_ui_file_access_file_activated_cb, faw, NULL );
350 uri_type_container = GTK_WIDGET(gtk_builder_get_object (builder,
"vb_uri_type_container" ));
351 faw->cb_uri_type = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
352 gtk_container_add( GTK_CONTAINER(uri_type_container), GTK_WIDGET(faw->cb_uri_type) );
353 gtk_box_set_child_packing( GTK_BOX(uri_type_container), GTK_WIDGET(faw->cb_uri_type),
354 TRUE, FALSE, 0, GTK_PACK_START );
355 g_object_connect( G_OBJECT(faw->cb_uri_type),
356 "signal::changed", cb_uri_type_changed_cb, NULL,
360 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, faw);
364 for ( node = list; node != NULL; node = node->next )
366 const gchar* access_method = node->data;
371 if ( strcmp( access_method,
"mysql" ) == 0 )
373 need_access_method_mysql = TRUE;
375 else if ( strcmp( access_method,
"postgres" ) == 0 )
377 need_access_method_postgres = TRUE;
379 else if ( strcmp( access_method,
"xml" ) == 0 )
381 if ( type == FILE_ACCESS_OPEN )
383 need_access_method_file = TRUE;
387 need_access_method_xml = TRUE;
390 else if ( strcmp( access_method,
"sqlite3" ) == 0 )
392 if ( type == FILE_ACCESS_OPEN )
394 need_access_method_file = TRUE;
398 need_access_method_sqlite3 = TRUE;
406 access_method_index = -1;
407 if ( need_access_method_file )
409 gtk_combo_box_text_append_text( faw->cb_uri_type,
"file" );
410 active_access_method_index = ++access_method_index;
412 if ( need_access_method_mysql )
414 gtk_combo_box_text_append_text( faw->cb_uri_type,
"mysql" );
415 ++access_method_index;
417 if ( need_access_method_postgres )
419 gtk_combo_box_text_append_text( faw->cb_uri_type,
"postgres" );
420 ++access_method_index;
422 if ( need_access_method_sqlite3 )
424 gtk_combo_box_text_append_text( faw->cb_uri_type,
"sqlite3" );
425 active_access_method_index = ++access_method_index;
427 if ( need_access_method_xml )
429 gtk_combo_box_text_append_text( faw->cb_uri_type,
"xml" );
430 ++access_method_index;
434 active_access_method_index = access_method_index;
436 g_assert( active_access_method_index >= 0 );
438 g_object_unref(G_OBJECT(builder));
441 gtk_widget_show_all( faw->dialog );
445 gtk_combo_box_set_active(GTK_COMBO_BOX(faw->cb_uri_type), active_access_method_index );
446 set_widget_sensitivity_for_uri_type( faw, gtk_combo_box_text_get_active_text( faw->cb_uri_type ));
450 gnc_ui_file_access_for_open(
void )
452 gnc_ui_file_access( FILE_ACCESS_OPEN );
457 gnc_ui_file_access_for_save_as(
void )
459 gnc_ui_file_access( FILE_ACCESS_SAVE_AS );
464 gnc_ui_file_access_for_export(
void )
466 gnc_ui_file_access( FILE_ACCESS_EXPORT );
gboolean gnc_uri_is_file_protocol(const gchar *protocol)
gboolean gnc_uri_is_file_uri(const gchar *uri)
utility functions for the GnuCash UI
gchar * gnc_uri_get_path(const gchar *uri)
#define PERR(format, args...)
char * gnc_history_get_last(void)
Functions providing the file history menu.
void gnc_gnome_help(const char *file_name, const char *anchor)
gchar * gnc_uri_create_uri(const gchar *protocol, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
GList * qof_backend_get_registered_access_method_list(void)
Utility functions for convert uri in separate components and back.
const gchar * QofLogModule