[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: gEDA-user: PCB: Change default file-filter in open-dialog



 Hello,

here is (a first version) of my patch to store the last used file-filter of the file-open-dialog. It recovers the last used filter-setting on start-up of the file-open-dialog. It uses GKeyFiles to store this information. I am not sure if this is a good choice, because there already is something implemented to store settings. But - I did not completely understand it. For this reason I introduce this (new) g(tk)-type of setting-handling.

If the main-developer like this GKeyFiles, perhaps the actual setting-handling can be migrated to use this functions. Perhaps - as written, I have not understand the code completely - this will reduce some code size (especially for the parser) and makes it easier for new developer to understand the code. Perhaps, they had already used this functions in other projects. If we will migrate to this GKeyFiles we have to pay attention to storing double-values. This is only available since GTK 2.12 :-(.

If the main-developer don't like this GKeyFiles, perhaps you can give me a hint to a documentation of the actual system or a point where I can easily start to learn it?


It would be very welcome to me, if some of you 'out there' can test my patch and give some feedback!

Kind regards,
Felix
diff --git a/src/hid/gtk/gui-config.c b/src/hid/gtk/gui-config.c
index 36277eb..ab71072 100644
--- a/src/hid/gtk/gui-config.c
+++ b/src/hid/gtk/gui-config.c
@@ -176,6 +176,83 @@ static ConfigAttribute config_attributes[] = {
   {"layer-name-8", CONFIG_Unused, NULL},
 };
 
+/*
+ * Get path for key-file
+ */
+const gchar*
+key_file_path() 
+{
+  gchar *homedir;
+
+  homedir = (gchar *) g_get_home_dir ();
+  if (!homedir)
+    {
+      g_message ("key_file_path: Can't get home directory!");
+      return NULL;
+    }
+
+  if (!config_dir)
+    {
+      config_dir =
+        g_build_path (G_DIR_SEPARATOR_S, homedir, PCB_CONFIG_DIR, NULL);
+      if (!g_file_test (config_dir, G_FILE_TEST_IS_DIR)
+	  && mkdir (config_dir, 0755) < 0)
+        {
+          g_message("key_file_path: Can't make \"%s\" directory!",
+	            config_dir);
+          g_free (config_dir);
+          config_dir = NULL;
+          return NULL;
+        }
+    }
+
+  return g_build_path (G_DIR_SEPARATOR_S, config_dir, "gpcb_gui.conf", NULL);
+}
+
+/*
+ * Initialize GKeyFile-Stucture.
+ */
+void
+key_file_open() {
+  GKeyFileFlags flags;
+  GError *error = NULL;
+  const gchar *filepath;
+  
+  /* Create a new GKeyFile object */
+  key_file = g_key_file_new ();
+  flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
+  g_key_file_set_list_separator (key_file, ';');
+  
+  filepath = key_file_path();
+  if ((filepath == NULL
+      || g_key_file_load_from_file (key_file, filepath, flags, &error) == FALSE))
+    {
+      // If file not found, create a new file when saving.
+      // Therefore add comments at the beginning of the file here.
+      const gchar *comment;
+      comment = "This file contains (gtk-gui-related) settings for pcb.\nYou can reset each setting to the default by removing the propreate key\nor delete the whole file to reset all values.";
+      g_key_file_set_comment(key_file, NULL, NULL, comment, NULL);
+    }
+}
+
+/*
+ * Save keys to file.
+ */
+void 
+key_file_save() 
+{ 
+  GString *output;
+  const gchar* file = key_file_path();
+  
+  if(file != NULL)
+    {
+      output = g_string_new(g_key_file_to_data(key_file, NULL, NULL));
+      g_file_set_contents(file, output->str, output->len, NULL);
+      g_string_free(output, TRUE);
+    }
+  g_key_file_free(key_file);
+}
+
 
 static FILE *
 config_file_open (gchar * mode)
@@ -697,6 +774,7 @@ ghid_config_files_read (gint * argc, gchar *** argv)
 
   ghidgui = &_ghidgui;
 
+  key_file_open();
   ghid_config_init ();
   load_rc_files ();
   config_file_read ();
@@ -731,6 +809,8 @@ ghid_config_files_write (void)
   FILE *f;
   ConfigAttribute *ca;
 
+  key_file_save();
+
   if (!ghidgui->config_modified || (f = config_file_open ("w")) == NULL)
     return;
 
diff --git a/src/hid/gtk/gui-dialog.c b/src/hid/gtk/gui-dialog.c
index ec91233..9c3d64e 100644
--- a/src/hid/gtk/gui-dialog.c
+++ b/src/hid/gtk/gui-dialog.c
@@ -270,6 +270,7 @@ ghid_dialog_file_select_open (gchar * title, gchar ** path, gchar * shortcuts)
   gchar *result = NULL, *folder, *seed;
   GHidPort *out = &ghid_port;
   GtkFileFilter *no_filter;
+  gint last_filter_index = 0;
 
   dialog = gtk_file_chooser_dialog_new (title,
 					GTK_WINDOW (out->top_window),
@@ -298,6 +299,9 @@ ghid_dialog_file_select_open (gchar * title, gchar ** path, gchar * shortcuts)
     gtk_file_filter_add_pattern (fp_filter, "*.fp");
     gtk_file_filter_add_pattern (fp_filter, "*.FP");
     gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), fp_filter);
+    if(g_key_file_get_integer(key_file, "OpenDialog", "FilterIndexLoadFootprint", NULL) == 1) {
+      gtk_file_chooser_set_filter(GTK_FILE_CHOOSER (dialog), fp_filter);
+    }
   }
 
   /* in case we have a dialog for loading a layout file */
@@ -312,6 +316,9 @@ ghid_dialog_file_select_open (gchar * title, gchar ** path, gchar * shortcuts)
     gtk_file_filter_add_pattern (pcb_filter, "*.pcb");
     gtk_file_filter_add_pattern (pcb_filter, "*.PCB");
     gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), pcb_filter);
+    if(g_key_file_get_integer(key_file, "OpenDialog", "FilterIndexLoadLayout", NULL) == 1) {
+      gtk_file_chooser_set_filter(GTK_FILE_CHOOSER (dialog), pcb_filter);
+    }
   }
 
   /* in case we have a dialog for loading a netlist file */
@@ -325,6 +332,9 @@ ghid_dialog_file_select_open (gchar * title, gchar ** path, gchar * shortcuts)
     gtk_file_filter_add_pattern (net_filter, "*.net");
     gtk_file_filter_add_pattern (net_filter, "*.NET");
     gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (dialog), net_filter);
+    if(g_key_file_get_integer(key_file, "OpenDialog", "FilterIndexLoadNetlist", NULL) == 1) {
+      gtk_file_chooser_set_filter(GTK_FILE_CHOOSER (dialog), net_filter);
+    }
   }
 
   if (path && *path)
@@ -353,6 +363,30 @@ ghid_dialog_file_select_open (gchar * title, gchar ** path, gchar * shortcuts)
 	  dup_string (path, folder);
 	  g_free (folder);
 	}
+
+      /* save used file-filter */
+      if(gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog)) != no_filter) 
+	last_filter_index = 1;
+      if (strcmp (title, _("Load element to buffer")) == 0)
+	{
+	  g_key_file_set_integer(key_file, "OpenDialog", "FilterIndexLoadFootprint", last_filter_index);
+	}
+	
+      if ((strcmp (title, _("Load layout file")) == 0)
+	|| (strcmp (title, _("Load layout file to buffer")) == 0))
+	{
+	    g_key_file_set_integer(key_file, "OpenDialog", "FilterIndexLoadLayout", last_filter_index);
+	}
+	
+      if (strcmp (title, _("Load netlist file")) == 0)
+	{
+	  g_key_file_set_integer(key_file, "OpenDialog", "FilterIndexLoadNetlist", last_filter_index);
+	}
+	
+      if (strcmp (title, _("Load schematics")) == 0)
+	{
+	  g_key_file_set_integer(key_file, "OpenDialog", "FilterIndexLoadSchematic", last_filter_index);
+	}
     }
   gtk_widget_destroy (dialog);
 
diff --git a/src/hid/gtk/gui.h b/src/hid/gtk/gui.h
index 7686106..41d7a5d 100644
--- a/src/hid/gtk/gui.h
+++ b/src/hid/gtk/gui.h
@@ -99,6 +99,8 @@ extern int ghid_flip_x, ghid_flip_y;
    */
 #define GRID_UNITS_VALUE(mm, mil)   (Settings.grid_units_mm ? (mm) : (mil))
 
+/* Settings */
+GKeyFile *key_file;
 
 typedef struct
 {

_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user