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 <stdbool.h>
26 #include <config.h>
27 
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 
31 #include "gnc-ui.h"
32 #include "gnc-ui-util.h"
33 #include "gnc-uri-utils.h"
34 #include "dialog-utils.h"
35 #include "dialog-file-access.h"
36 #include "gnc-file.h"
37 #include "gnc-filepath-utils.h"
39 #include "gnc-session.h"
40 
41 static QofLogModule log_module = GNC_MOD_GUI;
42 
43 #define DEFAULT_HOST "localhost"
44 #define DEFAULT_DATABASE PROJECT_NAME
45 #define FILE_ACCESS_OPEN 0
46 #define FILE_ACCESS_SAVE_AS 1
47 #define FILE_ACCESS_EXPORT 2
48 
49 typedef struct FileAccessWindow
50 {
51  /* Parts of the dialog */
52  int type;
53 
54  GtkWidget *dialog;
55  GtkWidget *frame_file;
56  GtkWidget *frame_database;
57  GtkWidget *readonly_checkbutton;
58  GtkFileChooser *fileChooser;
59  gchar *starting_dir;
60  GtkComboBoxText *cb_uri_type;
61  GtkEntry *tf_host;
62  GtkEntry *tf_database;
63  GtkEntry *tf_username;
64  GtkEntry *tf_password;
66 
67 void gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser,
68  FileAccessWindow *faw );
69 void gnc_ui_file_access_response_cb( GtkDialog *, gint, GtkDialog * );
70 static void cb_uri_type_changed_cb( GtkComboBoxText* cb );
71 
72 static gchar*
73 geturl( FileAccessWindow* faw )
74 {
75  gchar* url = NULL;
76  const gchar* host = NULL;
77  const gchar* username = NULL;
78  const gchar* password = NULL;
79  /* Not const as return value of gtk_combo_box_text_get_active_text must be freed */
80  gchar* type = NULL;
81  /* Not const as return value of gtk_file_chooser_get_filename must be freed */
82  gchar* path = NULL;
83 
84  type = gtk_combo_box_text_get_active_text (faw->cb_uri_type);
85  if (gnc_uri_is_file_scheme (type))
86  {
87  path = gtk_file_chooser_get_filename (faw->fileChooser);
88  if ( !path ) /* file protocol was chosen but no filename was set */
89  {
90  g_free (type);
91  return NULL;
92  }
93  }
94  else /* db protocol was chosen */
95  {
96  host = gtk_entry_get_text( faw->tf_host );
97  path = g_strdup(gtk_entry_get_text(faw->tf_database));
98  username = gtk_entry_get_text( faw->tf_username );
99  password = gtk_entry_get_text( faw->tf_password );
100  }
101 
102  url = gnc_uri_create_uri (type, host, 0, username, password, path);
103 
104  g_free (type);
105  g_free (path);
106 
107  return url;
108 }
109 
110 void
111 gnc_ui_file_access_file_activated_cb( GtkFileChooser *chooser, FileAccessWindow *faw )
112 {
113  g_return_if_fail( chooser != NULL );
114 
115  gnc_ui_file_access_response_cb( GTK_DIALOG(faw->dialog), GTK_RESPONSE_OK, NULL );
116 }
117 
118 void
119 gnc_ui_file_access_response_cb(GtkDialog *dialog, gint response, GtkDialog *unused)
120 {
121  FileAccessWindow* faw;
122  gchar* url;
123 
124  g_return_if_fail( dialog != NULL );
125 
126  faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
127  g_return_if_fail( faw != NULL );
128 
129  switch ( response )
130  {
131  case GTK_RESPONSE_HELP:
132  gnc_gnome_help (GTK_WINDOW(dialog), DF_MANUAL, DL_GLOBPREFS );
133  break;
134 
135  case GTK_RESPONSE_OK:
136  url = geturl( faw );
137  if ( url == NULL )
138  {
139  return;
140  }
141  if (g_str_has_prefix (url, "file://"))
142  {
143  if ( g_file_test( g_filename_from_uri( url, NULL, NULL ),
144  G_FILE_TEST_IS_DIR ))
145  {
146  gtk_file_chooser_set_current_folder_uri( faw->fileChooser, url );
147  return;
148  }
149  }
150  if ( faw->type == FILE_ACCESS_OPEN )
151  {
152  gboolean open_readonly = faw->readonly_checkbutton
153  ? gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(faw->readonly_checkbutton))
154  : FALSE;
155  gnc_file_open_file (GTK_WINDOW(dialog), url, open_readonly);
156  }
157  else if ( faw->type == FILE_ACCESS_SAVE_AS )
158  {
159  gnc_file_do_save_as (GTK_WINDOW(dialog), url);
160  }
161  else if ( faw->type == FILE_ACCESS_EXPORT )
162  {
163  gnc_file_do_export (GTK_WINDOW(dialog), url);
164  }
165  break;
166 
167  case GTK_RESPONSE_CANCEL:
168  case GTK_RESPONSE_DELETE_EVENT:
169  break;
170 
171  default:
172  PERR( "Invalid response" );
173  break;
174  }
175 
176  if ( response != GTK_RESPONSE_HELP )
177  {
178  gtk_widget_destroy( GTK_WIDGET(dialog) );
179  }
180 }
181 
182 /* Activate the file chooser and deactivate the db selection fields */
183 static void
184 set_widget_sensitivity( FileAccessWindow* faw, gboolean is_file_based_uri )
185 {
186  if (is_file_based_uri)
187  {
188  gtk_widget_show(faw->frame_file);
189  gtk_widget_hide(faw->frame_database);
190  gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
191  }
192  else
193  {
194  gtk_widget_show(faw->frame_database);
195  gtk_widget_hide(faw->frame_file);
196  }
197 // gtk_widget_set_sensitive( faw->frame_file, is_file_based_uri );
198 // gtk_widget_set_sensitive( faw->frame_database, !is_file_based_uri );
199 }
200 
201 static void
202 set_widget_sensitivity_for_uri_type( FileAccessWindow* faw, const gchar* uri_type )
203 {
204  if ( strcmp( uri_type, "file" ) == 0 || strcmp( uri_type, "xml" ) == 0
205  || strcmp( uri_type, "sqlite3" ) == 0 )
206  {
207  set_widget_sensitivity( faw, /* is_file_based_uri */ TRUE );
208  }
209  else if ( strcmp( uri_type, "mysql" ) == 0 || strcmp( uri_type, "postgres" ) == 0 )
210  {
211  set_widget_sensitivity( faw, /* is_file_based_uri */ FALSE );
212  }
213  else
214  {
215  g_assert( FALSE );
216  }
217 }
218 
219 static void
220 cb_uri_type_changed_cb( GtkComboBoxText* cb )
221 {
222  GtkWidget* dialog;
223  FileAccessWindow* faw;
224  const gchar* type;
225 
226  g_return_if_fail( cb != NULL );
227 
228  dialog = gtk_widget_get_toplevel( GTK_WIDGET(cb) );
229  g_return_if_fail( dialog != NULL );
230  faw = g_object_get_data( G_OBJECT(dialog), "FileAccessWindow" );
231  g_return_if_fail( faw != NULL );
232 
233  type = gtk_combo_box_text_get_active_text( cb );
234  set_widget_sensitivity_for_uri_type( faw, type );
235 }
236 
237 static const char*
238 get_default_database( void )
239 {
240  const gchar* default_db;
241 
242  default_db = g_getenv( "GNC_DEFAULT_DATABASE" );
243  if ( default_db == NULL )
244  {
245  default_db = DEFAULT_DATABASE;
246  }
247 
248  return default_db;
249 }
250 
251 typedef bool (*CharToBool)(const char*);
252 
253 static bool datafile_filter (const GtkFileFilterInfo* filter_info,
254  CharToBool filename_checker)
255 {
256  return filter_info && filter_info->filename &&
257  filename_checker (filter_info->filename);
258 }
259 
260 static void free_file_access_window (FileAccessWindow *faw)
261 {
262  g_free (faw->starting_dir);
263  g_free (faw);
264 }
265 
266 static void
267 gnc_ui_file_access (GtkWindow *parent, int type)
268 {
269  FileAccessWindow *faw;
270  GtkBuilder* builder;
271  GtkButton* op;
272  GtkWidget* file_chooser;
273  GtkFileChooserWidget* fileChooser;
274  GtkFileChooserAction fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
275  GList* list;
276  GList* node;
277  GtkWidget* uri_type_container;
278  gboolean need_access_method_file = FALSE;
279  gboolean need_access_method_mysql = FALSE;
280  gboolean need_access_method_postgres = FALSE;
281  gboolean need_access_method_sqlite3 = FALSE;
282  gboolean need_access_method_xml = FALSE;
283  gint access_method_index = -1;
284  gint active_access_method_index = -1;
285  const gchar* default_db;
286  const gchar *button_label = NULL;
287  const gchar *settings_section = NULL;
288  gchar *last;
289 
290  g_return_if_fail( type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS || type == FILE_ACCESS_EXPORT );
291 
292  faw = g_new0(FileAccessWindow, 1);
293  g_return_if_fail( faw != NULL );
294 
295  faw->type = type;
296  faw->starting_dir = NULL;
297 
298  /* Open the dialog */
299  builder = gtk_builder_new();
300  gnc_builder_add_from_file (builder, "dialog-file-access.glade", "file_access_dialog" );
301  faw->dialog = GTK_WIDGET(gtk_builder_get_object (builder, "file_access_dialog" ));
302  gtk_window_set_transient_for (GTK_WINDOW (faw->dialog), parent);
303  g_object_set_data_full (G_OBJECT(faw->dialog), "FileAccessWindow", faw,
304  (GDestroyNotify)free_file_access_window);
305 
306  // Set the name for this dialog so it can be easily manipulated with css
307  gtk_widget_set_name (GTK_WIDGET(faw->dialog), "gnc-id-file-access");
308 
309  faw->frame_file = GTK_WIDGET(gtk_builder_get_object (builder, "frame_file" ));
310  faw->frame_database = GTK_WIDGET(gtk_builder_get_object (builder, "frame_database" ));
311  faw->readonly_checkbutton = GTK_WIDGET(gtk_builder_get_object (builder, "readonly_checkbutton"));
312  faw->tf_host = GTK_ENTRY(gtk_builder_get_object (builder, "tf_host" ));
313  gtk_entry_set_text( faw->tf_host, DEFAULT_HOST );
314  faw->tf_database = GTK_ENTRY(gtk_builder_get_object (builder, "tf_database" ));
315  default_db = get_default_database();
316  gtk_entry_set_text( faw->tf_database, default_db );
317  faw->tf_username = GTK_ENTRY(gtk_builder_get_object (builder, "tf_username" ));
318  faw->tf_password = GTK_ENTRY(gtk_builder_get_object (builder, "tf_password" ));
319 
320  switch ( type )
321  {
322  case FILE_ACCESS_OPEN:
323  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Open…"));
324  button_label = _("_Open");
325  fileChooserAction = GTK_FILE_CHOOSER_ACTION_OPEN;
326  settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
327  break;
328 
329  case FILE_ACCESS_SAVE_AS:
330  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Save As…"));
331  button_label = _("_Save As");
332  fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
333  settings_section = GNC_PREFS_GROUP_OPEN_SAVE;
334  gtk_widget_destroy(faw->readonly_checkbutton);
335  faw->readonly_checkbutton = NULL;
336  break;
337 
338  case FILE_ACCESS_EXPORT:
339  gtk_window_set_title(GTK_WINDOW(faw->dialog), _("Export"));
340  button_label = _("_Save As");
341  fileChooserAction = GTK_FILE_CHOOSER_ACTION_SAVE;
342  settings_section = GNC_PREFS_GROUP_EXPORT;
343  gtk_widget_destroy(faw->readonly_checkbutton);
344  faw->readonly_checkbutton = NULL;
345  break;
346  }
347 
348  op = GTK_BUTTON(gtk_builder_get_object (builder, "pb_op" ));
349  if ( op != NULL )
350  gtk_button_set_label( op, button_label );
351 
352  file_chooser = GTK_WIDGET(gtk_builder_get_object (builder, "file_chooser" ));
353  fileChooser = GTK_FILE_CHOOSER_WIDGET(gtk_file_chooser_widget_new( fileChooserAction ));
354  faw->fileChooser = GTK_FILE_CHOOSER(fileChooser);
355  gtk_box_pack_start( GTK_BOX(file_chooser), GTK_WIDGET(fileChooser), TRUE, TRUE, 6 );
356 
357  /* set up .gnucash filters for Datafile operations */
358  GtkFileFilter *filter = gtk_file_filter_new ();
359  gtk_file_filter_set_name (filter, _("All files"));
360  gtk_file_filter_add_pattern (filter, "*");
361  gtk_file_chooser_add_filter (faw->fileChooser, filter);
362 
363  filter = gtk_file_filter_new ();
364  /* Translators: *.gnucash and *.xac are file patterns and must not
365  be translated*/
366  gtk_file_filter_set_name (filter, _("Datafiles only (*.gnucash, *.xac)"));
367  gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME,
368  (GtkFileFilterFunc)datafile_filter,
369  gnc_filename_is_datafile, NULL);
370  gtk_file_chooser_add_filter (faw->fileChooser, filter);
371  gtk_file_chooser_set_filter (faw->fileChooser, filter);
372 
373  filter = gtk_file_filter_new ();
374  /* Translators: *.gnucash.*.gnucash, *.xac.*.xac are file
375  patterns and must not be translated*/
376  gtk_file_filter_set_name (filter, _("Backups only (*.gnucash.*.gnucash, *.xac.*.xac)"));
377  gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_FILENAME,
378  (GtkFileFilterFunc)datafile_filter,
379  gnc_filename_is_backup, NULL);
380  gtk_file_chooser_add_filter (faw->fileChooser, filter);
381 
382  /* Set the default directory */
383  if (type == FILE_ACCESS_OPEN || type == FILE_ACCESS_SAVE_AS)
384  {
385  last = gnc_history_get_last();
386  if ( last && *last && gnc_uri_targets_local_fs (last))
387  {
388  gchar *filepath = gnc_uri_get_path ( last );
389  faw->starting_dir = g_path_get_dirname( filepath );
390  g_free ( filepath );
391  }
392  g_free (last);
393  }
394  if (!faw->starting_dir)
395  faw->starting_dir = gnc_get_default_directory(settings_section);
396  gtk_file_chooser_set_current_folder(faw->fileChooser, faw->starting_dir);
397 
398  g_object_connect( G_OBJECT(faw->fileChooser), "signal::file-activated",
399  gnc_ui_file_access_file_activated_cb, faw, NULL );
400 
401  uri_type_container = GTK_WIDGET(gtk_builder_get_object (builder, "vb_uri_type_container" ));
402  faw->cb_uri_type = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
403  gtk_container_add( GTK_CONTAINER(uri_type_container), GTK_WIDGET(faw->cb_uri_type) );
404  gtk_box_set_child_packing( GTK_BOX(uri_type_container), GTK_WIDGET(faw->cb_uri_type),
405  /*expand*/TRUE, /*fill*/FALSE, /*padding*/0, GTK_PACK_START );
406  g_object_connect( G_OBJECT(faw->cb_uri_type),
407  "signal::changed", cb_uri_type_changed_cb, NULL,
408  NULL );
409 
410  /* Autoconnect signals */
411  gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, faw);
412 
413  /* See what qof backends are available and add appropriate ones to the combo box */
415  for ( node = list; node != NULL; node = node->next )
416  {
417  const gchar* access_method = node->data;
418 
419  /* For the different access methods, "mysql" and "postgres" are added if available. Access
420  methods "xml" and "sqlite3" are compressed to "file" if opening a file, but when saving a file,
421  both access methods are added. */
422  if ( strcmp( access_method, "mysql" ) == 0 )
423  {
424  need_access_method_mysql = TRUE;
425  }
426  else if ( strcmp( access_method, "postgres" ) == 0 )
427  {
428  need_access_method_postgres = TRUE;
429  }
430  else if ( strcmp( access_method, "xml" ) == 0 )
431  {
432  if ( type == FILE_ACCESS_OPEN )
433  {
434  need_access_method_file = TRUE;
435  }
436  else
437  {
438  need_access_method_xml = TRUE;
439  }
440  }
441  else if ( strcmp( access_method, "sqlite3" ) == 0 )
442  {
443  if ( type == FILE_ACCESS_OPEN )
444  {
445  need_access_method_file = TRUE;
446  }
447  else
448  {
449  need_access_method_sqlite3 = TRUE;
450  }
451  }
452  }
453  g_list_free(list);
454 
455  /* Now that the set of access methods has been ascertained, add them to the list, and set the
456  default. */
457  access_method_index = -1;
458  if ( need_access_method_file )
459  {
460  gtk_combo_box_text_append_text( faw->cb_uri_type, "file" );
461  active_access_method_index = ++access_method_index;
462  }
463  if ( need_access_method_mysql )
464  {
465  gtk_combo_box_text_append_text( faw->cb_uri_type, "mysql" );
466  ++access_method_index;
467  }
468  if ( need_access_method_postgres )
469  {
470  gtk_combo_box_text_append_text( faw->cb_uri_type, "postgres" );
471  ++access_method_index;
472  }
473  if ( need_access_method_sqlite3 )
474  {
475  gtk_combo_box_text_append_text( faw->cb_uri_type, "sqlite3" );
476  active_access_method_index = ++access_method_index;
477  }
478  if ( need_access_method_xml )
479  {
480  gtk_combo_box_text_append_text( faw->cb_uri_type, "xml" );
481  ++access_method_index;
482 
483  // Set XML as default if it is offered (which mean we are in
484  // the "Save As" dialog)
485  active_access_method_index = access_method_index;
486  }
487  g_assert( active_access_method_index >= 0 );
488 
489  g_object_unref(G_OBJECT(builder));
490 
491  /* Run the dialog */
492  gtk_widget_show_all( faw->dialog );
493 
494  /* Hide the frame that's not required for the active access method so either only
495  * the File or only the Database frame are presented. */
496  gtk_combo_box_set_active(GTK_COMBO_BOX(faw->cb_uri_type), active_access_method_index );
497  set_widget_sensitivity_for_uri_type( faw, gtk_combo_box_text_get_active_text( faw->cb_uri_type ));
498 }
499 
500 void
501 gnc_ui_file_access_for_open (GtkWindow *parent)
502 {
503  gnc_ui_file_access (parent, FILE_ACCESS_OPEN);
504 }
505 
506 
507 void
508 gnc_ui_file_access_for_save_as (GtkWindow *parent)
509 {
510  gnc_ui_file_access (parent, FILE_ACCESS_SAVE_AS);
511 }
512 
513 
514 void
515 gnc_ui_file_access_for_export (GtkWindow *parent)
516 {
517  gnc_ui_file_access (parent, FILE_ACCESS_EXPORT);
518 }
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:103
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.
File path resolution utility functions.
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.