Branch data Line data Source code
1 : : /********************************************************************\
2 : : * dialog-file-access.c -- dialog for opening a file or making a *
3 : : * connection to a libdbi database *
4 : : * *
5 : : * Copyright (C) 2009 Phil Longstaff (plongstaff@rogers.com) *
6 : : * *
7 : : * This program is free software; you can redistribute it and/or *
8 : : * modify it under the terms of the GNU General Public License as *
9 : : * published by the Free Software Foundation; either version 2 of *
10 : : * the License, or (at your option) any later version. *
11 : : * *
12 : : * This program is distributed in the hope that it will be useful, *
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 : : * GNU General Public License for more details. *
16 : : * *
17 : : * You should have received a copy of the GNU General Public License*
18 : : * along with this program; if not, contact: *
19 : : * *
20 : : * Free Software Foundation Voice: +1-617-542-5942 *
21 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
23 : : \********************************************************************/
24 : :
25 : : #include <config.h>
26 : :
27 : : #include <gtk/gtk.h>
28 : : #include <glib/gi18n.h>
29 : :
30 : : #include "gnc-ui.h"
31 : : #include "gnc-ui-util.h"
32 : : #include "gnc-uri-utils.h"
33 : : #include "dialog-utils.h"
34 : : #include "dialog-file-access.h"
35 : : #include "gnc-file.h"
36 : : #include "gnc-plugin-file-history.h"
37 : : #include "gnc-session.h"
38 : :
39 : : static QofLogModule log_module = GNC_MOD_GUI;
40 : :
41 : : /* MariaDB/MySQL/Postgres optimize localhost to a unix socket but
42 : : * flatpak won't connect to unix sockets without gymnastics default to
43 : : * the localhost IP to force a network connection.
44 : : */
45 : : #define DEFAULT_HOST "127.0.0.1"
46 : : #define DEFAULT_DATABASE PROJECT_NAME
47 : : #define FILE_ACCESS_OPEN 0
48 : : #define FILE_ACCESS_SAVE_AS 1
49 : : #define FILE_ACCESS_EXPORT 2
50 : :
51 : : typedef struct FileAccessWindow
52 : : {
53 : : /* Parts of the dialog */
54 : : int type;
55 : :
56 : : GtkWidget *dialog;
57 : : GtkWidget *frame_file;
58 : : GtkWidget *frame_database;
59 : : GtkWidget *readonly_checkbutton;
60 : : GtkFileChooser *fileChooser;
61 : : gchar *starting_dir;
62 : : GtkComboBoxText *cb_uri_type;
63 : : GtkEntry *tf_host;
64 : : GtkEntry *tf_database;
65 : : GtkEntry *tf_username;
66 : : GtkEntry *tf_password;
67 : : } FileAccessWindow;
68 : :
69 : : void gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser,
70 : : FileAccessWindow *faw );
71 : : void gnc_ui_file_access_response_cb( GtkDialog *, gint, GtkDialog * );
72 : : static void cb_uri_type_changed_cb( GtkComboBoxText* cb );
73 : :
74 : : static gchar*
75 : 0 : geturl( FileAccessWindow* faw )
76 : : {
77 : 0 : gchar* url = NULL;
78 : 0 : const gchar* host = NULL;
79 : 0 : const gchar* username = NULL;
80 : 0 : const gchar* password = NULL;
81 : : /* Not const as return value of gtk_combo_box_text_get_active_text must be freed */
82 : 0 : gchar* type = NULL;
83 : : /* Not const as return value of gtk_file_chooser_get_filename must be freed */
84 : 0 : gchar* path = NULL;
85 : :
86 : 0 : type = gtk_combo_box_text_get_active_text (faw->cb_uri_type);
87 : 0 : if (gnc_uri_is_file_scheme (type))
88 : : {
89 : 0 : path = gtk_file_chooser_get_filename (faw->fileChooser);
90 : 0 : if ( !path ) /* file protocol was chosen but no filename was set */
91 : : {
92 : 0 : g_free (type);
93 : 0 : return NULL;
94 : : }
95 : : }
96 : : else /* db protocol was chosen */
97 : : {
98 : 0 : host = gtk_entry_get_text( faw->tf_host );
99 : 0 : path = g_strdup(gtk_entry_get_text(faw->tf_database));
100 : 0 : username = gtk_entry_get_text( faw->tf_username );
101 : 0 : password = gtk_entry_get_text( faw->tf_password );
102 : : }
103 : :
104 : 0 : url = gnc_uri_create_uri (type, host, 0, username, password, path);
105 : :
106 : 0 : g_free (type);
107 : 0 : g_free (path);
108 : :
109 : 0 : return url;
110 : : }
111 : :
112 : : void
113 : 0 : gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser, FileAccessWindow *faw )
114 : : {
115 : 0 : g_return_if_fail( chooser != NULL );
116 : :
117 : 0 : gnc_ui_file_access_response_cb( GTK_DIALOG(faw->dialog), GTK_RESPONSE_OK, NULL );
118 : : }
119 : :
120 : : void
121 : 0 : gnc_ui_file_access_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
122 : : {
123 : : FileAccessWindow* faw;
124 : : gchar* url;
125 : :
126 : 0 : g_return_if_fail( dialog != NULL );
127 : :
128 : 0 : faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
129 : 0 : g_return_if_fail( faw != NULL );
130 : :
131 : 0 : switch ( response )
132 : : {
133 : 0 : case GTK_RESPONSE_HELP:
134 : 0 : gnc_gnome_help (GTK_WINDOW(dialog), DF_MANUAL, DL_GLOBPREFS );
135 : 0 : break;
136 : :
137 : 0 : case GTK_RESPONSE_OK:
138 : 0 : url = geturl( faw );
139 : 0 : if ( url == NULL )
140 : : {
141 : 0 : return;
142 : : }
143 : 0 : if (g_str_has_prefix (url, "file://"))
144 : : {
145 : 0 : if ( g_file_test (gnc_uri_get_path (url), G_FILE_TEST_IS_DIR))
146 : : {
147 : 0 : gtk_file_chooser_set_current_folder_uri( faw->fileChooser, url );
148 : 0 : return;
149 : : }
150 : : }
151 : 0 : if ( faw->type == FILE_ACCESS_OPEN )
152 : : {
153 : 0 : gboolean open_readonly = faw->readonly_checkbutton
154 : 0 : ? gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(faw->readonly_checkbutton))
155 : 0 : : FALSE;
156 : 0 : gnc_file_open_file (GTK_WINDOW(dialog), url, open_readonly);
157 : : }
158 : 0 : else if ( faw->type == FILE_ACCESS_SAVE_AS )
159 : : {
160 : 0 : gnc_file_do_save_as (GTK_WINDOW(dialog), url);
161 : : }
162 : 0 : else if ( faw->type == FILE_ACCESS_EXPORT )
163 : : {
164 : 0 : gnc_file_do_export (GTK_WINDOW(dialog), url);
165 : : }
166 : 0 : break;
167 : :
168 : 0 : case GTK_RESPONSE_CANCEL:
169 : : case GTK_RESPONSE_DELETE_EVENT:
170 : 0 : break;
171 : :
172 : 0 : default:
173 : 0 : PERR( "Invalid response" );
174 : 0 : break;
175 : : }
176 : :
177 : 0 : if ( response != GTK_RESPONSE_HELP )
178 : : {
179 : 0 : gtk_widget_destroy( GTK_WIDGET(dialog) );
180 : : }
181 : : }
182 : :
183 : : /* Activate the file chooser and deactivate the db selection fields */
184 : : static void
185 : 0 : set_widget_sensitivity( FileAccessWindow* faw, gboolean is_file_based_uri )
186 : : {
187 : 0 : if (is_file_based_uri)
188 : : {
189 : 0 : gtk_widget_show(faw->frame_file);
190 : 0 : gtk_widget_hide(faw->frame_database);
191 : 0 : gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
192 : : }
193 : : else
194 : : {
195 : 0 : gtk_widget_show(faw->frame_database);
196 : 0 : gtk_widget_hide(faw->frame_file);
197 : : }
198 : : // gtk_widget_set_sensitive( faw->frame_file, is_file_based_uri );
199 : : // gtk_widget_set_sensitive( faw->frame_database, !is_file_based_uri );
200 : 0 : }
201 : :
202 : : static void
203 : 0 : set_widget_sensitivity_for_uri_type( FileAccessWindow* faw, const gchar* uri_type )
204 : : {
205 : 0 : if ( strcmp( uri_type, "file" ) == 0 || strcmp( uri_type, "xml" ) == 0
206 : 0 : || strcmp( uri_type, "sqlite3" ) == 0 )
207 : : {
208 : 0 : set_widget_sensitivity( faw, /* is_file_based_uri */ TRUE );
209 : : }
210 : 0 : else if ( strcmp( uri_type, "mysql" ) == 0 || strcmp( uri_type, "postgres" ) == 0 )
211 : : {
212 : 0 : set_widget_sensitivity( faw, /* is_file_based_uri */ FALSE );
213 : : }
214 : : else
215 : : {
216 : 0 : g_assert( FALSE );
217 : : }
218 : 0 : }
219 : :
220 : : static void
221 : 0 : cb_uri_type_changed_cb( GtkComboBoxText* cb )
222 : : {
223 : : GtkWidget* dialog;
224 : : FileAccessWindow* faw;
225 : : const gchar* type;
226 : :
227 : 0 : g_return_if_fail( cb != NULL );
228 : :
229 : 0 : dialog = gtk_widget_get_toplevel( GTK_WIDGET(cb) );
230 : 0 : g_return_if_fail( dialog != NULL );
231 : 0 : faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
232 : 0 : g_return_if_fail( faw != NULL );
233 : :
234 : 0 : type = gtk_combo_box_text_get_active_text( cb );
235 : 0 : set_widget_sensitivity_for_uri_type( faw, type );
236 : : }
237 : :
238 : : static const char*
239 : 0 : get_default_database( void )
240 : : {
241 : : const gchar* default_db;
242 : :
243 : 0 : default_db = g_getenv( "GNC_DEFAULT_DATABASE" );
244 : 0 : if ( default_db == NULL )
245 : : {
246 : 0 : default_db = DEFAULT_DATABASE;
247 : : }
248 : :
249 : 0 : return default_db;
250 : : }
251 : :
252 : 0 : static void free_file_access_window (FileAccessWindow *faw)
253 : : {
254 : 0 : g_free (faw->starting_dir);
255 : 0 : g_free (faw);
256 : 0 : }
257 : :
258 : : static void
259 : 0 : gnc_ui_file_access (GtkWindow *parent, int type)
260 : : {
261 : : FileAccessWindow *faw;
262 : : GtkBuilder* builder;
263 : : GtkButton* op;
264 : : GtkWidget* file_chooser;
265 : : GtkFileChooserWidget* fileChooser;
266 : 0 : GtkFileChooserAction fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
267 : : GList* list;
268 : : GList* node;
269 : : GtkWidget* uri_type_container;
270 : 0 : gboolean need_access_method_file = FALSE;
271 : 0 : gboolean need_access_method_mysql = FALSE;
272 : 0 : gboolean need_access_method_postgres = FALSE;
273 : 0 : gboolean need_access_method_sqlite3 = FALSE;
274 : 0 : gboolean need_access_method_xml = FALSE;
275 : 0 : gint access_method_index = -1;
276 : 0 : gint active_access_method_index = -1;
277 : : const gchar* default_db;
278 : 0 : const gchar *button_label = NULL;
279 : 0 : const gchar *settings_section = NULL;
280 : : gchar *last;
281 : :
282 : 0 : g_return_if_fail( type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS || type == FILE_ACCESS_EXPORT );
283 : :
284 : 0 : faw = g_new0(FileAccessWindow, 1);
285 : 0 : g_return_if_fail( faw != NULL );
286 : :
287 : 0 : faw->type = type;
288 : 0 : faw->starting_dir = NULL;
289 : :
290 : : /* Open the dialog */
291 : 0 : builder = gtk_builder_new();
292 : 0 : gnc_builder_add_from_file (builder, "dialog-file-access.glade", "file_access_dialog" );
293 : 0 : faw->dialog = GTK_WIDGET(gtk_builder_get_object (builder, "file_access_dialog" ));
294 : 0 : gtk_window_set_transient_for (GTK_WINDOW (faw->dialog), parent);
295 : 0 : g_object_set_data_full (G_OBJECT(faw->dialog), "FileAccessWindow", faw,
296 : : (GDestroyNotify)free_file_access_window);
297 : :
298 : : // Set the name for this dialog so it can be easily manipulated with css
299 : 0 : gtk_widget_set_name (GTK_WIDGET(faw->dialog), "gnc-id-file-access");
300 : :
301 : 0 : faw->frame_file = GTK_WIDGET(gtk_builder_get_object (builder, "frame_file" ));
302 : 0 : faw->frame_database = GTK_WIDGET(gtk_builder_get_object (builder, "frame_database" ));
303 : 0 : faw->readonly_checkbutton = GTK_WIDGET(gtk_builder_get_object (builder, "readonly_checkbutton"));
304 : 0 : faw->tf_host = GTK_ENTRY(gtk_builder_get_object (builder, "tf_host" ));
305 : 0 : gtk_entry_set_text( faw->tf_host, DEFAULT_HOST );
306 : 0 : faw->tf_database = GTK_ENTRY(gtk_builder_get_object (builder, "tf_database" ));
307 : 0 : default_db = get_default_database();
308 : 0 : gtk_entry_set_text( faw->tf_database, default_db );
309 : 0 : faw->tf_username = GTK_ENTRY(gtk_builder_get_object (builder, "tf_username" ));
310 : 0 : faw->tf_password = GTK_ENTRY(gtk_builder_get_object (builder, "tf_password" ));
311 : :
312 : 0 : switch ( type )
313 : : {
314 : 0 : case FILE_ACCESS_OPEN:
315 : 0 : gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Open…"));
316 : 0 : button_label = _("_Open");
317 : 0 : fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
318 : 0 : settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
319 : 0 : break;
320 : :
321 : 0 : case FILE_ACCESS_SAVE_AS:
322 : 0 : gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Save As…"));
323 : 0 : button_label = _("_Save As");
324 : 0 : fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
325 : 0 : settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
326 : 0 : gtk_widget_destroy(faw->readonly_checkbutton);
327 : 0 : faw->readonly_checkbutton = NULL;
328 : 0 : break;
329 : :
330 : 0 : case FILE_ACCESS_EXPORT:
331 : 0 : gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Export"));
332 : 0 : button_label = _("_Save As");
333 : 0 : fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
334 : 0 : settings_section = GNC_PREFS_GROUP_EXPORT;
335 : 0 : gtk_widget_destroy(faw->readonly_checkbutton);
336 : 0 : faw->readonly_checkbutton = NULL;
337 : 0 : break;
338 : : }
339 : :
340 : 0 : op = GTK_BUTTON(gtk_builder_get_object (builder, "pb_op" ));
341 : 0 : if ( op != NULL )
342 : 0 : gtk_button_set_label( op, button_label );
343 : :
344 : 0 : file_chooser = GTK_WIDGET(gtk_builder_get_object (builder, "file_chooser" ));
345 : 0 : fileChooser = GTK_FILE_CHOOSER_WIDGET(gtk_file_chooser_widget_new( fileChooserAction ));
346 : 0 : faw->fileChooser = GTK_FILE_CHOOSER(fileChooser);
347 : 0 : gtk_box_pack_start( GTK_BOX(file_chooser), GTK_WIDGET(fileChooser), TRUE, TRUE, 6 );
348 : :
349 : 0 : gnc_file_chooser_add_filters (faw->fileChooser,
350 : : gnc_file_chooser_get_datafile_filters ());
351 : :
352 : : /* Set the default directory */
353 : 0 : if (type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS)
354 : : {
355 : 0 : last = gnc_history_get_last();
356 : 0 : if ( last && *last && gnc_uri_targets_local_fs (last))
357 : : {
358 : 0 : gchar *filepath = gnc_uri_get_path ( last );
359 : 0 : faw->starting_dir = g_path_get_dirname( filepath );
360 : 0 : g_free ( filepath );
361 : : }
362 : 0 : g_free (last);
363 : : }
364 : 0 : if (!faw->starting_dir)
365 : 0 : faw->starting_dir = gnc_get_default_directory(settings_section);
366 : 0 : gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
367 : :
368 : 0 : g_object_connect( G_OBJECT(faw->fileChooser), "signal::file-activated",
369 : : gnc_ui_file_access_file_activated_cb, faw, NULL );
370 : :
371 : 0 : uri_type_container = GTK_WIDGET(gtk_builder_get_object (builder, "vb_uri_type_container" ));
372 : 0 : faw->cb_uri_type = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
373 : 0 : gtk_container_add( GTK_CONTAINER(uri_type_container), GTK_WIDGET(faw->cb_uri_type) );
374 : 0 : gtk_box_set_child_packing( GTK_BOX(uri_type_container), GTK_WIDGET(faw->cb_uri_type),
375 : : /*expand*/TRUE, /*fill*/FALSE, /*padding*/0, GTK_PACK_START );
376 : 0 : g_object_connect( G_OBJECT(faw->cb_uri_type),
377 : : "signal::changed", cb_uri_type_changed_cb, NULL,
378 : : NULL );
379 : :
380 : : /* Autoconnect signals */
381 : 0 : gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, faw);
382 : :
383 : : /* See what qof backends are available and add appropriate ones to the combo box */
384 : 0 : list = qof_backend_get_registered_access_method_list();
385 : 0 : for ( node = list; node != NULL; node = node->next )
386 : : {
387 : 0 : const gchar* access_method = node->data;
388 : :
389 : : /* For the different access methods, "mysql" and "postgres" are added if available. Access
390 : : methods "xml" and "sqlite3" are compressed to "file" if opening a file, but when saving a file,
391 : : both access methods are added. */
392 : 0 : if ( strcmp( access_method, "mysql" ) == 0 )
393 : : {
394 : 0 : need_access_method_mysql = TRUE;
395 : : }
396 : 0 : else if ( strcmp( access_method, "postgres" ) == 0 )
397 : : {
398 : 0 : need_access_method_postgres = TRUE;
399 : : }
400 : 0 : else if ( strcmp( access_method, "xml" ) == 0 )
401 : : {
402 : 0 : if ( type == FILE_ACCESS_OPEN )
403 : : {
404 : 0 : need_access_method_file = TRUE;
405 : : }
406 : : else
407 : : {
408 : 0 : need_access_method_xml = TRUE;
409 : : }
410 : : }
411 : 0 : else if ( strcmp( access_method, "sqlite3" ) == 0 )
412 : : {
413 : 0 : if ( type == FILE_ACCESS_OPEN )
414 : : {
415 : 0 : need_access_method_file = TRUE;
416 : : }
417 : : else
418 : : {
419 : 0 : need_access_method_sqlite3 = TRUE;
420 : : }
421 : : }
422 : : }
423 : 0 : g_list_free(list);
424 : :
425 : : /* Now that the set of access methods has been ascertained, add them to the list, and set the
426 : : default. */
427 : 0 : access_method_index = -1;
428 : 0 : if ( need_access_method_file )
429 : : {
430 : 0 : gtk_combo_box_text_append_text( faw->cb_uri_type, "file" );
431 : 0 : active_access_method_index = ++access_method_index;
432 : : }
433 : 0 : if ( need_access_method_mysql )
434 : : {
435 : 0 : gtk_combo_box_text_append_text( faw->cb_uri_type, "mysql" );
436 : 0 : ++access_method_index;
437 : : }
438 : 0 : if ( need_access_method_postgres )
439 : : {
440 : 0 : gtk_combo_box_text_append_text( faw->cb_uri_type, "postgres" );
441 : 0 : ++access_method_index;
442 : : }
443 : 0 : if ( need_access_method_sqlite3 )
444 : : {
445 : 0 : gtk_combo_box_text_append_text( faw->cb_uri_type, "sqlite3" );
446 : 0 : active_access_method_index = ++access_method_index;
447 : : }
448 : 0 : if ( need_access_method_xml )
449 : : {
450 : 0 : gtk_combo_box_text_append_text( faw->cb_uri_type, "xml" );
451 : 0 : ++access_method_index;
452 : :
453 : : // Set XML as default if it is offered (which mean we are in
454 : : // the "Save As" dialog)
455 : 0 : active_access_method_index = access_method_index;
456 : : }
457 : 0 : g_assert( active_access_method_index >= 0 );
458 : :
459 : 0 : g_object_unref(G_OBJECT(builder));
460 : :
461 : : /* Run the dialog */
462 : 0 : gtk_widget_show_all( faw->dialog );
463 : :
464 : : /* Hide the frame that's not required for the active access method so either only
465 : : * the File or only the Database frame are presented. */
466 : 0 : gtk_combo_box_set_active(GTK_COMBO_BOX(faw->cb_uri_type), active_access_method_index );
467 : 0 : set_widget_sensitivity_for_uri_type( faw, gtk_combo_box_text_get_active_text( faw->cb_uri_type ));
468 : : }
469 : :
470 : : void
471 : 0 : gnc_ui_file_access_for_open (GtkWindow *parent)
472 : : {
473 : 0 : gnc_ui_file_access (parent, FILE_ACCESS_OPEN);
474 : 0 : }
475 : :
476 : :
477 : : void
478 : 0 : gnc_ui_file_access_for_save_as (GtkWindow *parent)
479 : : {
480 : 0 : gnc_ui_file_access (parent, FILE_ACCESS_SAVE_AS);
481 : 0 : }
482 : :
483 : :
484 : : void
485 : 0 : gnc_ui_file_access_for_export (GtkWindow *parent)
486 : : {
487 : 0 : gnc_ui_file_access (parent, FILE_ACCESS_EXPORT);
488 : 0 : }
|