[Gnucash-changes] Update file selection boxes to use a GtkFileChooserDialog.

David Hampton hampton at cvs.gnucash.org
Tue Oct 25 22:44:12 EDT 2005


Log Message:
-----------
Update file selection boxes to use a GtkFileChooserDialog.  Reinstate
file filtering in the dialog.  Patch from Arnout 'raboof' Engelen.
Fixes #116205.

Tags:
----
gnucash-gnome2-dev

Modified Files:
--------------
    gnucash/src/business/business-gnome:
        gnc-plugin-business.c
    gnucash/src/gnome:
        dialog-chart-export.c
    gnucash/src/gnome-utils:
        gnc-file.c
        gnc-file.h
    gnucash/src/import-export/log-replay:
        gnc-log-replay.c
    gnucash/src/import-export/mt940:
        gnc-mt940-import.c
    gnucash/src/import-export/ofx:
        gnc-ofx-import.c
    gnucash/src/import-export/qif-import:
        druid-qif-import.c
    gnucash/src/report/report-gnome:
        gnc-plugin-page-report.c

Revision Data
-------------
Index: gnc-plugin-business.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/business/business-gnome/Attic/gnc-plugin-business.c,v
retrieving revision 1.1.2.16
retrieving revision 1.1.2.17
diff -Lsrc/business/business-gnome/gnc-plugin-business.c -Lsrc/business/business-gnome/gnc-plugin-business.c -u -r1.1.2.16 -r1.1.2.17
--- src/business/business-gnome/gnc-plugin-business.c
+++ src/business/business-gnome/gnc-plugin-business.c
@@ -680,7 +680,8 @@
 	book = qof_session_get_book(current_session);
 	chart_session = qof_session_new();
 	success = FALSE;
-	filename = gnc_file_dialog(_("Export Invoices to XML"), NULL, "/tmp/qsf-invoices.xml");
+	filename = gnc_file_dialog(_("Export Invoices to XML"), NULL, 
+			"/tmp/qsf-invoices.xml", GNC_FILE_DIALOG_EXPORT);
 	if (filename)
 	{
 		qof_session_begin(chart_session, filename, TRUE, TRUE);
@@ -713,7 +714,8 @@
 	book = qof_session_get_book(current_session);
 	chart_session = qof_session_new();
 	success = FALSE;
-	filename = gnc_file_dialog(_("Export Customers to XML"), NULL, "/tmp/qsf-customers.xml");
+	filename = gnc_file_dialog(_("Export Customers to XML"), NULL, 
+			"/tmp/qsf-customers.xml", GNC_FILE_DIALOG_EXPORT);
 	if (filename)
 	{
 		qof_session_begin(chart_session, filename, TRUE, TRUE);
@@ -743,7 +745,8 @@
 	book = qof_session_get_book(current_session);
 	chart_session = qof_session_new();
 	success = FALSE;
-	filename = gnc_file_dialog(_("Export Vendors to XML"), NULL, "/tmp/qsf-vendors.xml");
+	filename = gnc_file_dialog(_("Export Vendors to XML"), NULL, 
+			"/tmp/qsf-vendors.xml", GNC_FILE_DIALOG_EXPORT);
 	if (filename)
 	{
 		qof_session_begin(chart_session, filename, TRUE, TRUE);
@@ -773,7 +776,8 @@
 	book = qof_session_get_book(current_session);
 	chart_session = qof_session_new();
 	success = FALSE;
-	filename = gnc_file_dialog(_("Export Employees to XML"), NULL, "/tmp/qsf-employee.xml");
+	filename = gnc_file_dialog(_("Export Employees to XML"), NULL, 
+			"/tmp/qsf-employee.xml", GNC_FILE_DIALOG_EXPORT);
 	if (filename)
 	{
 		qof_session_begin(chart_session, filename, TRUE, TRUE);
Index: dialog-chart-export.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome/Attic/dialog-chart-export.c,v
retrieving revision 1.1.2.8
retrieving revision 1.1.2.9
diff -Lsrc/gnome/dialog-chart-export.c -Lsrc/gnome/dialog-chart-export.c -u -r1.1.2.8 -r1.1.2.9
--- src/gnome/dialog-chart-export.c
+++ src/gnome/dialog-chart-export.c
@@ -232,7 +232,7 @@
 	filename = g_strdup("/tmp/qsf-chartofaccounts.xml");
 	chart_session = qof_session_new();
 	filename = gnc_file_dialog(_("Export Chart of Accounts to QSF XML"),
-				   NULL, NULL);
+				   NULL, NULL, GNC_FILE_DIALOG_EXPORT);
 	if (filename)
 	{
 		gnc_engine_suspend_events();
Index: gnc-file.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome-utils/Attic/gnc-file.c,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -Lsrc/gnome-utils/gnc-file.c -Lsrc/gnome-utils/gnc-file.c -u -r1.1.2.4 -r1.1.2.5
--- src/gnome-utils/gnc-file.c
+++ src/gnome-utils/gnc-file.c
@@ -65,49 +65,106 @@
  *                                                                  * 
  * Args:   title        - the title of the window                   *
  *         filter       - the file filter to use                    * 
- *         default_name - the default name to use                   *
+ *         default_dir  - start the chooser in this directory       *
+ *         type         - what type of dialog (open, save, etc.)    *
  * Return: containing the name of the file the user selected        *
 \********************************************************************/
 
 char *
 gnc_file_dialog (const char * title,
                  const char * filter,
-                 const char *default_name)
+                 const char * starting_dir,
+		 GNCFileDialogType type
+		 )
 {
-  GtkFileSelection *file_box;
+  GtkWidget *file_box;
   const char *internal_name;
   char *file_name = NULL;
+  gchar * okbutton = GTK_STOCK_OPEN;
+  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; 
   gint response;
 
-  ENTER("\n");
-
-  /* Set a default title if nothing was passed in */  
-  if (title == NULL)
-    title = _("Open");
-
-  file_box = GTK_FILE_SELECTION(gtk_file_selection_new(title));
+  ENTER(" ");
 
-  if (default_name)
-    gtk_file_selection_set_filename(file_box, default_name);
+  switch (type) {
+	case GNC_FILE_DIALOG_OPEN:
+		  action = GTK_FILE_CHOOSER_ACTION_OPEN;
+		  okbutton = GTK_STOCK_OPEN;
+		  if (title == NULL)
+			  title = _("Open");
+		  break;
+	case GNC_FILE_DIALOG_IMPORT:
+		  action = GTK_FILE_CHOOSER_ACTION_OPEN;
+		  okbutton = _("Import");
+		  if (title == NULL)
+			  title = _("Import");
+		  break;
+	case GNC_FILE_DIALOG_SAVE:
+		  action = GTK_FILE_CHOOSER_ACTION_SAVE;
+		  okbutton = GTK_STOCK_SAVE;
+		  if (title == NULL)
+			  title = _("Save");
+		  break;
+	case GNC_FILE_DIALOG_EXPORT:
+		  action = GTK_FILE_CHOOSER_ACTION_SAVE;
+		  okbutton = _("Export");
+		  if (title == NULL)
+			  title = _("Export");
+		  break;
+	
+  }
 
-  /* hack alert - this was filtering directory names as well as file 
-   * names, so I think we should not do this by default (rgmerk) */
-#if 0
-  if (filter != NULL)
-    gtk_file_selection_complete(file_box, filter);
-#endif
+  file_box = gtk_file_chooser_dialog_new(
+			  title,
+			  NULL,
+			  GTK_FILE_CHOOSER_ACTION_OPEN,
+			  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+			  okbutton, GTK_RESPONSE_ACCEPT,
+			  NULL);
+
+  if (starting_dir) {
+    gchar *dir_name;
+
+    /* Ensure we have a directory name.  The set function fails otherwise. */
+    dir_name = g_path_get_dirname(starting_dir);
+    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER (file_box), dir_name);
+    g_free(dir_name);
+  }
 
   gtk_window_set_modal(GTK_WINDOW(file_box), TRUE);
+  /*
   gtk_window_set_transient_for(GTK_WINDOW(file_box),
 			       GTK_WINDOW(gnc_ui_get_toplevel()));
+  */
+
+  if (filter != NULL)
+  {
+    GtkFileFilter* g_filter = gtk_file_filter_new();
+    GtkFileFilter* all_filter = gtk_file_filter_new();
+
+    gtk_file_filter_set_name (g_filter, filter);
+    gtk_file_filter_add_pattern (g_filter, filter);
+    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_box), g_filter);
+
+    gtk_file_filter_set_name (all_filter, "All files");
+    gtk_file_filter_add_pattern (all_filter, "*");
+    gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_box), all_filter);
+
+    /* Note: You cannot set a file filter and pre-select a file name.
+     * The latter wins, and the filter ends up diabled.  Since we are
+     * only settin the starting directory for the chooser dialog,
+     * everything works as expected. */
+    gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (file_box), g_filter);
+  }
+
   response = gtk_dialog_run(GTK_DIALOG(file_box));
 
-  if (response == GTK_RESPONSE_OK) {
+  if (response == GTK_RESPONSE_ACCEPT) {
     /* look for constructs like postgres://foo */
-    internal_name = gtk_entry_get_text(GTK_ENTRY(file_box->selection_entry));
-    if (strstr (internal_name, "://") == 0) {
+    internal_name = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER (file_box));
+    if (strstr (internal_name, "file://") == internal_name) {
       /* nope, a local file name */
-      internal_name = gtk_file_selection_get_filename(file_box);
+      internal_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER (file_box));
     }
     file_name = g_strdup(internal_name);
   }
@@ -665,7 +722,7 @@
     return FALSE;
 
   lastfile = gnc_history_get_last();
-  newfile = gnc_file_dialog (_("Open"), NULL, lastfile);
+  newfile = gnc_file_dialog (_("Open"), NULL, lastfile, GNC_FILE_DIALOG_OPEN);
   if (lastfile)
     g_free(lastfile);
   result = gnc_post_file_open (newfile);
@@ -703,7 +760,7 @@
     gnc_init_default_directory(&default_dir);
 
   if (!newfile) {
-    newfile =  gnc_file_dialog (_("Export"), NULL, default_dir);
+    newfile =  gnc_file_dialog (_("Export"), NULL, default_dir, GNC_FILE_DIALOG_EXPORT);
     g_free(default_dir);
     default_dir = NULL;
     if (!newfile)
@@ -849,7 +906,8 @@
   } else {
     gnc_init_default_directory(&default_dir);
   }
-  filename = gnc_file_dialog (_("Save"), "*.gnc", default_dir);
+  filename = gnc_file_dialog (_("Save"), NULL, default_dir, 
+		  GNC_FILE_DIALOG_SAVE);
   if (default_dir)
     free(default_dir);
   if (!filename) return;
Index: gnc-file.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/gnome-utils/Attic/gnc-file.h,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -Lsrc/gnome-utils/gnc-file.h -Lsrc/gnome-utils/gnc-file.h -u -r1.1.2.1 -r1.1.2.2
--- src/gnome-utils/gnc-file.h
+++ src/gnome-utils/gnc-file.h
@@ -116,6 +116,14 @@
 #include <glib.h>
 #include "qofbackend.h"
 
+typedef enum
+{
+  GNC_FILE_DIALOG_OPEN,
+  GNC_FILE_DIALOG_IMPORT,
+  GNC_FILE_DIALOG_SAVE,
+  GNC_FILE_DIALOG_EXPORT
+} GNCFileDialogType;
+
 void gnc_file_new (void);
 gboolean gnc_file_open (void);
 void gnc_file_save (void);
@@ -128,7 +136,8 @@
 
 char * gnc_file_dialog (const char * title,
 			const char * filter,
-			const char * default_name);
+			const char * starting_dir,
+			GNCFileDialogType type);
 
 gboolean gnc_file_open_file (const char *filename);
 void gnc_file_export_file(const char * filename);
Index: gnc-log-replay.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/import-export/log-replay/gnc-log-replay.c,v
retrieving revision 1.4.2.9
retrieving revision 1.4.2.10
diff -Lsrc/import-export/log-replay/gnc-log-replay.c -Lsrc/import-export/log-replay/gnc-log-replay.c -u -r1.4.2.9 -r1.4.2.10
--- src/import-export/log-replay/gnc-log-replay.c
+++ src/import-export/log-replay/gnc-log-replay.c
@@ -512,7 +512,8 @@
     gnc_init_default_directory(&default_dir);
   selected_filename = gnc_file_dialog(_("Select a .log file to replay"),
 				      NULL,
-				      default_dir);
+				      default_dir,
+				      GNC_FILE_DIALOG_OPEN);
   g_free(default_dir);
 
   if(selected_filename!=NULL)
Index: gnc-mt940-import.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/import-export/mt940/gnc-mt940-import.c,v
retrieving revision 1.2.2.6
retrieving revision 1.2.2.7
diff -Lsrc/import-export/mt940/gnc-mt940-import.c -Lsrc/import-export/mt940/gnc-mt940-import.c -u -r1.2.2.6 -r1.2.2.7
--- src/import-export/mt940/gnc-mt940-import.c
+++ src/import-export/mt940/gnc-mt940-import.c
@@ -78,7 +78,8 @@
     gnc_init_default_directory(&default_dir);
   selected_filename = gnc_file_dialog(_("Select an MT940 file to process"),
 				      NULL,
-				      default_dir);
+				      default_dir,
+				      GNC_FILE_DIALOG_IMPORT);
   g_free(default_dir);
 
   if(selected_filename!=NULL)
Index: gnc-ofx-import.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/import-export/ofx/gnc-ofx-import.c,v
retrieving revision 1.28.4.12
retrieving revision 1.28.4.13
diff -Lsrc/import-export/ofx/gnc-ofx-import.c -Lsrc/import-export/ofx/gnc-ofx-import.c -u -r1.28.4.12 -r1.28.4.13
--- src/import-export/ofx/gnc-ofx-import.c
+++ src/import-export/ofx/gnc-ofx-import.c
@@ -624,7 +624,8 @@
     gnc_init_default_directory(&default_dir);
   selected_filename = gnc_file_dialog(_("Select an OFX/QFX file to process"),
 				      NULL,
-				      default_dir);
+				      default_dir,
+				      GNC_FILE_DIALOG_IMPORT);
   g_free(default_dir);
   default_dir = NULL;
 
Index: druid-qif-import.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/import-export/qif-import/druid-qif-import.c,v
retrieving revision 1.22.4.16
retrieving revision 1.22.4.17
diff -Lsrc/import-export/qif-import/druid-qif-import.c -Lsrc/import-export/qif-import/druid-qif-import.c -u -r1.22.4.16 -r1.22.4.17
--- src/import-export/qif-import/druid-qif-import.c
+++ src/import-export/qif-import/druid-qif-import.c
@@ -358,7 +358,8 @@
   default_dir = gnc_gconf_get_string(GCONF_SECTION, KEY_LAST_PATH, NULL);
   if (default_dir == NULL)
     gnc_init_default_directory(&default_dir);
-  new_file_name = gnc_file_dialog (_("Select QIF File"), "*.qif", default_dir);
+  new_file_name = gnc_file_dialog (_("Select QIF File"), "*.qif", 
+		  default_dir, GNC_FILE_DIALOG_IMPORT);
 
   /* Insure valid data, and something that can be freed. */
   if (new_file_name == NULL) {
Index: gnc-plugin-page-report.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/report/report-gnome/Attic/gnc-plugin-page-report.c,v
retrieving revision 1.1.2.22
retrieving revision 1.1.2.23
diff -Lsrc/report/report-gnome/gnc-plugin-page-report.c -Lsrc/report/report-gnome/gnc-plugin-page-report.c -u -r1.1.2.22 -r1.1.2.23
--- src/report/report-gnome/gnc-plugin-page-report.c
+++ src/report/report-gnome/gnc-plugin-page-report.c
@@ -949,7 +949,7 @@
         /* %s is the type of what is about to be saved, e.g. "HTML". */
         title = g_strdup_printf (_("Save %s To File"), type);
 
-        filepath = gnc_file_dialog (title, NULL, NULL);
+        filepath = gnc_file_dialog (title, NULL, NULL, GNC_FILE_DIALOG_EXPORT);
 
         g_free (title);
 


More information about the gnucash-changes mailing list