GnuCash  5.6-150-g038405b370+
dialog-file-access.c
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"
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;
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 geturl( FileAccessWindow* faw )
76 {
77  gchar* url = NULL;
78  const gchar* host = NULL;
79  const gchar* username = NULL;
80  const gchar* password = NULL;
81  /* Not const as return value of gtk_combo_box_text_get_active_text must be freed */
82  gchar* type = NULL;
83  /* Not const as return value of gtk_file_chooser_get_filename must be freed */
84  gchar* path = NULL;
85 
86  type = gtk_combo_box_text_get_active_text (faw->cb_uri_type);
87  if (gnc_uri_is_file_scheme (type))
88  {
89  path = gtk_file_chooser_get_filename (faw->fileChooser);
90  if ( !path ) /* file protocol was chosen but no filename was set */
91  {
92  g_free (type);
93  return NULL;
94  }
95  }
96  else /* db protocol was chosen */
97  {
98  host = gtk_entry_get_text( faw->tf_host );
99  path = g_strdup(gtk_entry_get_text(faw->tf_database));
100  username = gtk_entry_get_text( faw->tf_username );
101  password = gtk_entry_get_text( faw->tf_password );
102  }
103 
104  url = gnc_uri_create_uri (type, host, 0, username, password, path);
105 
106  g_free (type);
107  g_free (path);
108 
109  return url;
110 }
111 
112 void
113 gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser, FileAccessWindow *faw )
114 {
115  g_return_if_fail( chooser != NULL );
116 
117  gnc_ui_file_access_response_cb( GTK_DIALOG(faw->dialog), GTK_RESPONSE_OK, NULL );
118 }
119 
120 void
121 gnc_ui_file_access_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
122 {
123  FileAccessWindow* faw;
124  gchar* url;
125 
126  g_return_if_fail( dialog != NULL );
127 
128  faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
129  g_return_if_fail( faw != NULL );
130 
131  switch ( response )
132  {
133  case GTK_RESPONSE_HELP:
134  gnc_gnome_help (GTK_WINDOW(dialog), DF_MANUAL, DL_GLOBPREFS );
135  break;
136 
137  case GTK_RESPONSE_OK:
138  url = geturl( faw );
139  if ( url == NULL )
140  {
141  return;
142  }
143  if (g_str_has_prefix (url, "file://"))
144  {
145  if ( g_file_test (gnc_uri_get_path (url), G_FILE_TEST_IS_DIR))
146  {
147  gtk_file_chooser_set_current_folder_uri( faw->fileChooser, url );
148  return;
149  }
150  }
151  if ( faw->type == FILE_ACCESS_OPEN )
152  {
153  gboolean open_readonly = faw->readonly_checkbutton
154  ? gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(faw->readonly_checkbutton))
155  : FALSE;
156  gnc_file_open_file (GTK_WINDOW(dialog), url, open_readonly);
157  }
158  else if ( faw->type == FILE_ACCESS_SAVE_AS )
159  {
160  gnc_file_do_save_as (GTK_WINDOW(dialog), url);
161  }
162  else if ( faw->type == FILE_ACCESS_EXPORT )
163  {
164  gnc_file_do_export (GTK_WINDOW(dialog), url);
165  }
166  break;
167 
168  case GTK_RESPONSE_CANCEL:
169  case GTK_RESPONSE_DELETE_EVENT:
170  break;
171 
172  default:
173  PERR( "Invalid response" );
174  break;
175  }
176 
177  if ( response != GTK_RESPONSE_HELP )
178  {
179  gtk_widget_destroy( GTK_WIDGET(dialog) );
180  }
181 }
182 
183 /* Activate the file chooser and deactivate the db selection fields */
184 static void
185 set_widget_sensitivity( FileAccessWindow* faw, gboolean is_file_based_uri )
186 {
187  if (is_file_based_uri)
188  {
189  gtk_widget_show(faw->frame_file);
190  gtk_widget_hide(faw->frame_database);
191  gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
192  }
193  else
194  {
195  gtk_widget_show(faw->frame_database);
196  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 }
201 
202 static void
203 set_widget_sensitivity_for_uri_type( FileAccessWindow* faw, const gchar* uri_type )
204 {
205  if ( strcmp( uri_type, "file" ) == 0 || strcmp( uri_type, "xml" ) == 0
206  || strcmp( uri_type, "sqlite3" ) == 0 )
207  {
208  set_widget_sensitivity( faw, /* is_file_based_uri */ TRUE );
209  }
210  else if ( strcmp( uri_type, "mysql" ) == 0 || strcmp( uri_type, "postgres" ) == 0 )
211  {
212  set_widget_sensitivity( faw, /* is_file_based_uri */ FALSE );
213  }
214  else
215  {
216  g_assert( FALSE );
217  }
218 }
219 
220 static void
221 cb_uri_type_changed_cb( GtkComboBoxText* cb )
222 {
223  GtkWidget* dialog;
224  FileAccessWindow* faw;
225  const gchar* type;
226 
227  g_return_if_fail( cb != NULL );
228 
229  dialog = gtk_widget_get_toplevel( GTK_WIDGET(cb) );
230  g_return_if_fail( dialog != NULL );
231  faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
232  g_return_if_fail( faw != NULL );
233 
234  type = gtk_combo_box_text_get_active_text( cb );
235  set_widget_sensitivity_for_uri_type( faw, type );
236 }
237 
238 static const char*
239 get_default_database( void )
240 {
241  const gchar* default_db;
242 
243  default_db = g_getenv( "GNC_DEFAULT_DATABASE" );
244  if ( default_db == NULL )
245  {
246  default_db = DEFAULT_DATABASE;
247  }
248 
249  return default_db;
250 }
251 
252 static void free_file_access_window (FileAccessWindow *faw)
253 {
254  g_free (faw->starting_dir);
255  g_free (faw);
256 }
257 
258 static void
259 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  GtkFileChooserAction fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
267  GList* list;
268  GList* node;
269  GtkWidget* uri_type_container;
270  gboolean need_access_method_file = FALSE;
271  gboolean need_access_method_mysql = FALSE;
272  gboolean need_access_method_postgres = FALSE;
273  gboolean need_access_method_sqlite3 = FALSE;
274  gboolean need_access_method_xml = FALSE;
275  gint access_method_index = -1;
276  gint active_access_method_index = -1;
277  const gchar* default_db;
278  const gchar *button_label = NULL;
279  const gchar *settings_section = NULL;
280  gchar *last;
281 
282  g_return_if_fail( type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS || type == FILE_ACCESS_EXPORT );
283 
284  faw = g_new0(FileAccessWindow, 1);
285  g_return_if_fail( faw != NULL );
286 
287  faw->type = type;
288  faw->starting_dir = NULL;
289 
290  /* Open the dialog */
291  builder = gtk_builder_new();
292  gnc_builder_add_from_file (builder, "dialog-file-access.glade", "file_access_dialog" );
293  faw->dialog = GTK_WIDGET(gtk_builder_get_object (builder, "file_access_dialog" ));
294  gtk_window_set_transient_for (GTK_WINDOW (faw->dialog), parent);
295  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  gtk_widget_set_name (GTK_WIDGET(faw->dialog), "gnc-id-file-access");
300 
301  faw->frame_file = GTK_WIDGET(gtk_builder_get_object (builder, "frame_file" ));
302  faw->frame_database = GTK_WIDGET(gtk_builder_get_object (builder, "frame_database" ));
303  faw->readonly_checkbutton = GTK_WIDGET(gtk_builder_get_object (builder, "readonly_checkbutton"));
304  faw->tf_host = GTK_ENTRY(gtk_builder_get_object (builder, "tf_host" ));
305  gtk_entry_set_text( faw->tf_host, DEFAULT_HOST );
306  faw->tf_database = GTK_ENTRY(gtk_builder_get_object (builder, "tf_database" ));
307  default_db = get_default_database();
308  gtk_entry_set_text( faw->tf_database, default_db );
309  faw->tf_username = GTK_ENTRY(gtk_builder_get_object (builder, "tf_username" ));
310  faw->tf_password = GTK_ENTRY(gtk_builder_get_object (builder, "tf_password" ));
311 
312  switch ( type )
313  {
314  case FILE_ACCESS_OPEN:
315  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Open…"));
316  button_label = _("_Open");
317  fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
318  settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
319  break;
320 
321  case FILE_ACCESS_SAVE_AS:
322  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Save As…"));
323  button_label = _("_Save As");
324  fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
325  settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
326  gtk_widget_destroy(faw->readonly_checkbutton);
327  faw->readonly_checkbutton = NULL;
328  break;
329 
330  case FILE_ACCESS_EXPORT:
331  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Export"));
332  button_label = _("_Save As");
333  fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
334  settings_section = GNC_PREFS_GROUP_EXPORT;
335  gtk_widget_destroy(faw->readonly_checkbutton);
336  faw->readonly_checkbutton = NULL;
337  break;
338  }
339 
340  op = GTK_BUTTON(gtk_builder_get_object (builder, "pb_op" ));
341  if ( op != NULL )
342  gtk_button_set_label( op, button_label );
343 
344  file_chooser = GTK_WIDGET(gtk_builder_get_object (builder, "file_chooser" ));
345  fileChooser = GTK_FILE_CHOOSER_WIDGET(gtk_file_chooser_widget_new( fileChooserAction ));
346  faw->fileChooser = GTK_FILE_CHOOSER(fileChooser);
347  gtk_box_pack_start( GTK_BOX(file_chooser), GTK_WIDGET(fileChooser), TRUE, TRUE, 6 );
348 
349  gnc_file_chooser_add_filters (faw->fileChooser,
350  gnc_file_chooser_get_datafile_filters ());
351 
352  /* Set the default directory */
353  if (type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS)
354  {
355  last = gnc_history_get_last();
356  if ( last && *last && gnc_uri_targets_local_fs (last))
357  {
358  gchar *filepath = gnc_uri_get_path ( last );
359  faw->starting_dir = g_path_get_dirname( filepath );
360  g_free ( filepath );
361  }
362  g_free (last);
363  }
364  if (!faw->starting_dir)
365  faw->starting_dir = gnc_get_default_directory(settings_section);
366  gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
367 
368  g_object_connect( G_OBJECT(faw->fileChooser), "signal::file-activated",
369  gnc_ui_file_access_file_activated_cb, faw, NULL );
370 
371  uri_type_container = GTK_WIDGET(gtk_builder_get_object (builder, "vb_uri_type_container" ));
372  faw->cb_uri_type = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
373  gtk_container_add( GTK_CONTAINER(uri_type_container), GTK_WIDGET(faw->cb_uri_type) );
374  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  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  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 */
385  for ( node = list; node != NULL; node = node->next )
386  {
387  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  if ( strcmp( access_method, "mysql" ) == 0 )
393  {
394  need_access_method_mysql = TRUE;
395  }
396  else if ( strcmp( access_method, "postgres" ) == 0 )
397  {
398  need_access_method_postgres = TRUE;
399  }
400  else if ( strcmp( access_method, "xml" ) == 0 )
401  {
402  if ( type == FILE_ACCESS_OPEN )
403  {
404  need_access_method_file = TRUE;
405  }
406  else
407  {
408  need_access_method_xml = TRUE;
409  }
410  }
411  else if ( strcmp( access_method, "sqlite3" ) == 0 )
412  {
413  if ( type == FILE_ACCESS_OPEN )
414  {
415  need_access_method_file = TRUE;
416  }
417  else
418  {
419  need_access_method_sqlite3 = TRUE;
420  }
421  }
422  }
423  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  access_method_index = -1;
428  if ( need_access_method_file )
429  {
430  gtk_combo_box_text_append_text( faw->cb_uri_type, "file" );
431  active_access_method_index = ++access_method_index;
432  }
433  if ( need_access_method_mysql )
434  {
435  gtk_combo_box_text_append_text( faw->cb_uri_type, "mysql" );
436  ++access_method_index;
437  }
438  if ( need_access_method_postgres )
439  {
440  gtk_combo_box_text_append_text( faw->cb_uri_type, "postgres" );
441  ++access_method_index;
442  }
443  if ( need_access_method_sqlite3 )
444  {
445  gtk_combo_box_text_append_text( faw->cb_uri_type, "sqlite3" );
446  active_access_method_index = ++access_method_index;
447  }
448  if ( need_access_method_xml )
449  {
450  gtk_combo_box_text_append_text( faw->cb_uri_type, "xml" );
451  ++access_method_index;
452 
453  // Set XML as default if it is offered (which mean we are in
454  // the "Save As" dialog)
455  active_access_method_index = access_method_index;
456  }
457  g_assert( active_access_method_index >= 0 );
458 
459  g_object_unref(G_OBJECT(builder));
460 
461  /* Run the dialog */
462  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  gtk_combo_box_set_active(GTK_COMBO_BOX(faw->cb_uri_type), active_access_method_index );
467  set_widget_sensitivity_for_uri_type( faw, gtk_combo_box_text_get_active_text( faw->cb_uri_type ));
468 }
469 
470 void
471 gnc_ui_file_access_for_open (GtkWindow *parent)
472 {
473  gnc_ui_file_access (parent, FILE_ACCESS_OPEN);
474 }
475 
476 
477 void
478 gnc_ui_file_access_for_save_as (GtkWindow *parent)
479 {
480  gnc_ui_file_access (parent, FILE_ACCESS_SAVE_AS);
481 }
482 
483 
484 void
485 gnc_ui_file_access_for_export (GtkWindow *parent)
486 {
487  gnc_ui_file_access (parent, FILE_ACCESS_EXPORT);
488 }
utility functions for the GnuCash UI
gboolean gnc_uri_is_file_scheme(const gchar *scheme)
Checks if the given scheme is used to refer to a file (as opposed to a network service like a databas...
Definition: gnc-uri-utils.c:90
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
char * gnc_history_get_last(void)
Retrieve the name of the file most recently accessed.
void gnc_gnome_help(GtkWindow *parent, const char *file_name, const char *anchor)
Launch the systems default help browser, gnome&#39;s yelp for linux, and open to a given link within a gi...
Functions providing the file history menu.
GList * qof_backend_get_registered_access_method_list(void)
Return a list of strings for the registered access methods.
Definition: qofsession.cpp:104
gboolean gnc_uri_targets_local_fs(const gchar *uri)
Checks if the given uri is either a valid file uri or a local filesystem path.
This file contains the functions to present a GUI to select a file or a database connection.
Utility functions for convert uri in separate components and back.
gchar * gnc_uri_create_uri(const gchar *scheme, const gchar *hostname, gint32 port, const gchar *username, const gchar *password, const gchar *path)
Composes a normalized uri starting from its separate components.