[gnome-latex: 53/205] Save and load preferences



commit f936bfc3108572af3862af151baad224479dff90
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Fri Sep 11 23:18:31 2009 +0200

    Save and load preferences
    
    Now the prefereneces related functions are in prefs.c. The rc file is
    store in $HOME/.config/latexila/latexilarc. There is a structure
    preferences_t wich hold all the preferences, it is easier to store and
    load values than the GKeyFile. The rc file is saved only at exit.
    
    Now all the dimensions of the program are stored: width and height of
    the window, if the window is maximized and the positions of the panes.

 TODO               |   9 +-
 src/CMakeLists.txt |  11 ++-
 src/actions.c      |   4 +-
 src/callbacks.c    |  81 +++++-----------
 src/callbacks.h    |   1 -
 src/main.c         | 131 ++++++-------------------
 src/main.h         |  48 ++++++---
 src/prefs.c        | 279 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/prefs.h        |   8 ++
 9 files changed, 389 insertions(+), 183 deletions(-)
---
diff --git a/TODO b/TODO
index 7350921..55363ec 100644
--- a/TODO
+++ b/TODO
@@ -5,10 +5,11 @@ Wed Sep 9, 2009 to Wed Sep 16, 2009
 [x] document tabs
        x arrows to navigate between tabs if there are a lot of documents opened
 
-[-] save and load
-       - show/hide symbol tables
-       - size of the window
-       - position of the panes
+[x] save and load
+       x show/hide symbol tables
+       x size of the window
+       x if the window is maximised
+       x position of the panes
 
 [-] new toolbar
        - structure: part, chapter, section, subsection, subsubsection, paragraph, subparagraph
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 877d0ef..bef189e 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,8 +1,9 @@
-SET(latexila_src main.c main.h
-                callbacks.c callbacks.h
-                print.c print.h
-                symbols.c symbols.h
-                actions.c actions.h)
+SET(latexila_src main.c        main.h
+                callbacks.c    callbacks.h
+                print.c        print.h
+                symbols.c      symbols.h
+                actions.c      actions.h
+                prefs.c        prefs.h)
 ADD_EXECUTABLE(latexila ${latexila_src})
 TARGET_LINK_LIBRARIES(latexila ${GTK2_LIBRARIES})
 
diff --git a/src/actions.c b/src/actions.c
index 5e91380..9c084f5 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -257,9 +257,7 @@ view_document (gchar *title, gchar *doc_extension)
        gchar *doc_path = g_regex_replace_literal (regex, latexila.active_doc->path,
                        -1, 0, doc_extension, 0, NULL);
 
-       gchar *command_view = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
-                       "command_view", NULL);
-       command = g_strdup_printf ("%s %s", command_view, doc_path);
+       command = g_strdup_printf ("%s %s", latexila.prefs->command_view, doc_path);
 
        /* the current document is a *.tex file? */
        gboolean tex_file = g_regex_match (regex, latexila.active_doc->path, 0, NULL);
diff --git a/src/callbacks.c b/src/callbacks.c
index 8843d64..4b2d0ff 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -35,6 +35,7 @@
 #include "callbacks.h"
 #include "print.h"
 #include "actions.h"
+#include "prefs.h"
 
 static void create_document_in_new_tab (const gchar *path, const gchar *text,
                const gchar *title);
@@ -52,7 +53,6 @@ static void change_font_source_view (void);
 static void create_preferences (void);
 
 static GtkWidget *pref_dialog = NULL;
-static gboolean pref_changed = FALSE;
 
 void
 cb_new (void)
@@ -180,6 +180,7 @@ cb_quit (void)
 {
        if (close_all ())
        {
+               save_preferences (latexila.prefs);
                print_info ("Bye bye");
                gtk_main_quit ();
        }
@@ -287,26 +288,25 @@ cb_preferences (void)
 void
 cb_zoom_in (void)
 {
-       latexila.font_size += PANGO_SCALE;
-       pango_font_description_set_size (latexila.font_desc, latexila.font_size);
+       latexila.prefs->font_size += PANGO_SCALE;
+       pango_font_description_set_size (latexila.prefs->font_desc,
+                       latexila.prefs->font_size);
        change_font_source_view ();
 }
 
 void
 cb_zoom_out (void)
 {
-       latexila.font_size -= PANGO_SCALE;
-       pango_font_description_set_size (latexila.font_desc, latexila.font_size);
+       latexila.prefs->font_size -= PANGO_SCALE;
+       pango_font_description_set_size (latexila.prefs->font_desc,
+                       latexila.prefs->font_size);
        change_font_source_view ();
 }
 
 void
 cb_zoom_reset (void)
 {
-       gchar *font_string = g_key_file_get_string (latexila.key_file,
-            PROGRAM_NAME, "font", NULL);
-    latexila.font_desc = pango_font_description_from_string (font_string);
-    latexila.font_size = pango_font_description_get_size (latexila.font_desc);
+       set_current_font_prefs (latexila.prefs);
        change_font_source_view ();
 }
 
@@ -731,17 +731,13 @@ void
 cb_pref_dialog_close (GtkDialog *dialog, gint response_id, gpointer user_data)
 {
        gtk_widget_hide (GTK_WIDGET (dialog));
-
-       if (pref_changed)
-               save_preferences ();
 }
 
 void
 cb_line_numbers (GtkToggleButton *toggle_button, gpointer user_data)
 {
        gboolean show_line_numbers = gtk_toggle_button_get_active (toggle_button);
-       g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
-                       "show_line_numbers", show_line_numbers);
+       latexila.prefs->show_line_numbers = show_line_numbers;
 
        //TODO optimisation?
        guint nb_docs = g_list_length (latexila.all_docs);
@@ -751,8 +747,6 @@ cb_line_numbers (GtkToggleButton *toggle_button, gpointer user_data)
                gtk_source_view_set_show_line_numbers (
                                GTK_SOURCE_VIEW (doc->source_view), show_line_numbers);
        }
-
-       pref_changed = TRUE;
 }
 
 void
@@ -760,21 +754,18 @@ cb_command_view (GtkButton *button, gpointer user_data)
 {
        GtkEntry *entry = (GtkEntry *) user_data;
        const gchar *new_command_view = gtk_entry_get_text (entry);
-       g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                       "command_view", new_command_view);
-       pref_changed = TRUE;
+       g_free (latexila.prefs->command_view);
+       latexila.prefs->command_view = g_strdup (new_command_view);
 }
 
 void
 cb_font_set (GtkFontButton *font_button, gpointer user_data)
 {
        const gchar *font_string = gtk_font_button_get_font_name (font_button);
-       g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                       "font", font_string);
-       latexila.font_desc = pango_font_description_from_string (font_string);
-       latexila.font_size = pango_font_description_get_size (latexila.font_desc);
+       g_free (latexila.prefs->font_str);
+       latexila.prefs->font_str = g_strdup (font_string);
+       set_current_font_prefs (latexila.prefs);
        change_font_source_view ();
-       pref_changed = TRUE;
 }
 
 void
@@ -887,24 +878,6 @@ open_new_document (const gchar *filename, const gchar *uri)
                print_warning ("impossible to open the file \"%s\"", filename);
 }
 
-void
-save_preferences (void)
-{
-       gchar *key_file_data = g_key_file_to_data (latexila.key_file, NULL, NULL);
-       FILE* file = fopen (latexila.pref_file, "w");
-       if (file != NULL)
-       {
-               fprintf (file, "%s", key_file_data);
-               fclose (file);
-       }
-       else
-       {
-               print_warning ("impossible to save user preferences to \"%s\"",
-                               latexila.pref_file);
-       }
-       g_free (key_file_data);
-}
-
 /******************************************************************************
  * local functions
  *****************************************************************************/
@@ -949,17 +922,16 @@ create_document_in_new_tab (const gchar *path, const gchar *text, const gchar *t
        gtk_source_view_set_auto_indent (GTK_SOURCE_VIEW (new_doc->source_view), TRUE);
 
        // set the font
-       gtk_widget_modify_font (new_doc->source_view, latexila.font_desc);
+       gtk_widget_modify_font (new_doc->source_view, latexila.prefs->font_desc);
 
        // enable text wrapping (between words only)
        gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (new_doc->source_view),
                        GTK_WRAP_WORD);
 
        // show line numbers?
-       gboolean show_line_numbers = g_key_file_get_boolean (latexila.key_file,
-                       PROGRAM_NAME, "show_line_numbers", NULL);
        gtk_source_view_set_show_line_numbers (
-                       GTK_SOURCE_VIEW (new_doc->source_view), show_line_numbers);
+                       GTK_SOURCE_VIEW (new_doc->source_view),
+                       latexila.prefs->show_line_numbers);
 
        // put the text into the buffer
        gtk_source_buffer_begin_not_undoable_action (new_doc->source_buffer);
@@ -1322,7 +1294,7 @@ change_font_source_view (void)
        for (guint i = 0 ; i < nb_docs ; i++)
        {
                document_t *doc = g_list_nth_data (latexila.all_docs, i);
-               gtk_widget_modify_font (doc->source_view, latexila.font_desc);
+               gtk_widget_modify_font (doc->source_view, latexila.prefs->font_desc);
        }
 }
 
@@ -1346,9 +1318,8 @@ create_preferences (void)
        /* show line numbers */
        GtkWidget *line_numbers = gtk_check_button_new_with_label (
                        _("Display line numbers"));
-       gboolean tmp = g_key_file_get_boolean (latexila.key_file, PROGRAM_NAME,
-                       "show_line_numbers", NULL);
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (line_numbers), tmp);
+       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (line_numbers),
+                       latexila.prefs->show_line_numbers);
        g_signal_connect (G_OBJECT (line_numbers), "toggled",
                        G_CALLBACK (cb_line_numbers), NULL);
        gtk_box_pack_start (GTK_BOX (content_area), line_numbers, FALSE, FALSE, 0);
@@ -1356,9 +1327,8 @@ create_preferences (void)
        /* font */
        GtkWidget *hbox = gtk_hbox_new (FALSE, 5);
        GtkWidget *label = gtk_label_new (_("Font:"));
-       gchar *current_font = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
-                       "font", NULL);
-       GtkWidget *font_button = gtk_font_button_new_with_font (current_font);
+       GtkWidget *font_button = gtk_font_button_new_with_font (
+                       latexila.prefs->font_str);
        g_signal_connect (G_OBJECT (font_button), "font-set",
                        G_CALLBACK (cb_font_set), NULL);
        gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
@@ -1369,9 +1339,8 @@ create_preferences (void)
        hbox = gtk_hbox_new (FALSE, 5);
        label = gtk_label_new (_("Program for viewing documents:"));
        GtkWidget *command_view_entry = gtk_entry_new ();
-       gchar *txt = g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
-                       "command_view", NULL);
-       gtk_entry_set_text (GTK_ENTRY (command_view_entry), txt);
+       gtk_entry_set_text (GTK_ENTRY (command_view_entry),
+                       latexila.prefs->command_view);
        GtkWidget *button = gtk_button_new_with_label (_("OK"));
        g_signal_connect (G_OBJECT (button), "clicked",
                        G_CALLBACK (cb_command_view), GTK_ENTRY (command_view_entry));
diff --git a/src/callbacks.h b/src/callbacks.h
index 67fd45b..c86b99e 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -68,6 +68,5 @@ void cb_symbol_selected (GtkIconView *icon_view, gpointer user_data);
 void cb_show_symbol_tables (GtkToggleAction *toggle_action, gpointer user_data);
 
 void open_new_document (const gchar *filename, const gchar *uri);
-void save_preferences (void);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index ae56998..bf8fbec 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,13 +31,14 @@
 #include "callbacks.h"
 #include "symbols.h"
 #include "print.h"
+#include "prefs.h"
 
 static void register_my_stock_icons (void);
 static gboolean option_version (const gchar *option_name, const gchar *value,
                gpointer data, GError **error);
 
 latexila_t latexila = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
-       NULL, NULL, NULL, NULL}; 
+       NULL, NULL}; 
 
 static struct {
        gchar *filename;
@@ -147,6 +148,10 @@ main (int argc, char *argv[])
        textdomain (LATEXILA_NLS_PACKAGE);
 #endif
 
+       /* preferences */
+       latexila.prefs = g_malloc (sizeof (preferences_t));
+       load_preferences (latexila.prefs);
+
        /* personal style */
        // make the close buttons in tabs smaller
        // we use gtk_widget_set_name (widget, "my-close-button") to apply this
@@ -163,13 +168,15 @@ main (int argc, char *argv[])
 
        /* main window */
        GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       latexila.main_window = GTK_WINDOW (window);
        g_signal_connect (G_OBJECT (window), "delete_event",
                        G_CALLBACK (cb_delete_event), NULL);
        gtk_window_set_title (GTK_WINDOW (window), PROGRAM_NAME);
        gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
-       gtk_window_set_default_size (GTK_WINDOW (window), 800, 600);
-
-       latexila.main_window = GTK_WINDOW (window);
+       gtk_window_set_default_size (GTK_WINDOW (window),
+                       latexila.prefs->window_width, latexila.prefs->window_height);
+       if (latexila.prefs->window_maximised)
+               gtk_window_maximize (GTK_WINDOW (window));
 
        /* boxes */
        GtkWidget *main_vbox = gtk_vbox_new (FALSE, 0);
@@ -308,14 +315,17 @@ main (int argc, char *argv[])
        latexila.redo = gtk_action_group_get_action (action_group, "EditRedo");
        GtkToggleAction *show_symbol_tables = GTK_TOGGLE_ACTION (
                        gtk_action_group_get_action (action_group, "ViewSymbols"));
-       gtk_toggle_action_set_active (show_symbol_tables, TRUE);
+       gtk_toggle_action_set_active (show_symbol_tables,
+                       latexila.prefs->show_side_pane);
 
        /* horizontal pane
         * left: symbol tables
         * right: the source view and the log zone
         */
        GtkWidget *main_hpaned = gtk_hpaned_new ();
-       gtk_paned_set_position (GTK_PANED (main_hpaned), 180);
+       latexila.main_hpaned = GTK_PANED (main_hpaned);
+       gtk_paned_set_position (GTK_PANED (main_hpaned),
+                       latexila.prefs->main_hpaned_pos);
        gtk_box_pack_start (GTK_BOX (main_vbox), main_hpaned, TRUE, TRUE, 0);
 
        /* symbol tables */
@@ -331,7 +341,8 @@ main (int argc, char *argv[])
         * bottom: log zone
         */
        GtkWidget *vpaned = gtk_vpaned_new ();
-       gtk_paned_set_position (GTK_PANED (vpaned), 380);
+       latexila.vpaned = GTK_PANED (vpaned);
+       gtk_paned_set_position (GTK_PANED (vpaned), latexila.prefs->vpaned_pos);
        gtk_paned_pack2 (GTK_PANED (main_hpaned), vpaned, TRUE, TRUE);
 
        /* source view with tabs */
@@ -349,7 +360,8 @@ main (int argc, char *argv[])
        // left: action history
        // right: details
        GtkWidget *hpaned = gtk_hpaned_new ();
-       gtk_paned_set_position (GTK_PANED (hpaned), 190);
+       latexila.log_hpaned = GTK_PANED (hpaned);
+       gtk_paned_set_position (GTK_PANED (hpaned), latexila.prefs->log_hpaned_pos);
        gtk_paned_pack2 (GTK_PANED (vpaned), hpaned, TRUE, TRUE);
 
        // action history
@@ -421,93 +433,8 @@ main (int argc, char *argv[])
        gtk_box_pack_end (GTK_BOX (statusbar), cursor_position_statusbar,
                        FALSE, TRUE, 0);
 
-       /* user preferences */
-       gboolean default_value_show_line_numbers = FALSE;
-       gchar *default_value_font = "Monospace 10";
-       gchar *default_value_command_view = "evince";
-
-       gchar *pref_file = g_strdup_printf ("%s/.latexila", g_get_home_dir ());
-       latexila.pref_file = pref_file;
-       latexila.key_file = g_key_file_new ();
-       gboolean prefs_loaded = FALSE;
-       error = NULL;
-
-       if (g_file_test (pref_file, G_FILE_TEST_IS_REGULAR))
-       {
-               g_key_file_load_from_file (latexila.key_file, pref_file,
-                               G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error);
-               if (error != NULL)
-               {
-                       print_warning ("load user preferences failed: %s", error->message);
-                       g_error_free (error);
-                       error = NULL;
-               }
-               else
-               {
-                       /* check if all keys exist
-                        * if not, set the default value for that key
-                        */
-                       gboolean prefs_saved = TRUE;
-                       g_key_file_get_boolean (latexila.key_file, PROGRAM_NAME,
-                                       "show_line_numbers", &error);
-                       if (error != NULL)
-                       {
-                               g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
-                                               "show_line_numbers", default_value_show_line_numbers);
-                               g_error_free (error);
-                               error = NULL;
-                               prefs_saved = FALSE;
-                       }
-
-                       g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
-                                       "font", &error);
-                       if (error != NULL)
-                       {
-                               g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                                               "font", default_value_font);
-                               g_error_free (error);
-                               error = NULL;
-                               prefs_saved = FALSE;
-                       }
-
-                       g_key_file_get_string (latexila.key_file, PROGRAM_NAME,
-                                       "command_view", &error);
-                       if (error != NULL)
-                       {
-                               g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                                               "command_view", default_value_command_view);
-                               g_error_free (error);
-                               error = NULL;
-                               prefs_saved = FALSE;
-                       }
-
-                       if (! prefs_saved)
-                               save_preferences ();
-
-                       print_info ("load user preferences: OK");
-                       prefs_loaded = TRUE;
-               }
-       }
-
-       if (! prefs_loaded)
-       {
-               // set default values
-               g_key_file_set_boolean (latexila.key_file, PROGRAM_NAME,
-                               "show_line_numbers", default_value_show_line_numbers);
-               g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                               "font", default_value_font);
-               g_key_file_set_string (latexila.key_file, PROGRAM_NAME,
-                               "command_view", default_value_command_view);
-
-               save_preferences ();
-       }
-
-       gchar *font_string = g_key_file_get_string (latexila.key_file,
-                       PROGRAM_NAME, "font", NULL);
-       latexila.font_desc = pango_font_description_from_string (font_string);
-       latexila.font_size = pango_font_description_get_size (latexila.font_desc);
-
 
+       /* show the window */
        gtk_widget_show_all (window);
 
        /* open documents given in arguments */
@@ -519,20 +446,26 @@ main (int argc, char *argv[])
                else
                {
                        gchar *current_dir = g_get_current_dir ();
-                       path = g_strdup_printf ("%s/%s", current_dir, argv[i]);
+                       path = g_build_filename (current_dir, argv[i], NULL);
                        g_free (current_dir);
                }
 
-               gchar *uri = g_filename_to_uri (path, NULL, NULL);
-               if (uri != NULL)
-                       open_new_document (path, uri);
+               gchar *uri = g_filename_to_uri (path, NULL, &error);
+               if (error != NULL)
+               {
+                       print_warning ("can not open the file \"%s\": %s", argv[i],
+                                       error->message);
+                       g_error_free (error);
+                       error = NULL;
+               }
                else
-                       print_warning ("can not open the file \"%s\"", argv[i]);
+                       open_new_document (path, uri);
 
                g_free (path);
                g_free (uri);
        }
 
+
        gtk_main ();
 
        return EXIT_SUCCESS;
diff --git a/src/main.h b/src/main.h
index adf6c0c..1d59b89 100644
--- a/src/main.h
+++ b/src/main.h
@@ -71,25 +71,43 @@ typedef struct
        GtkIconView                     *icon_view;
 } symbols_t;
 
+// preferences, settings
 typedef struct
 {
-       GList                           *all_docs;
-       document_t                      *active_doc;
-       action_log_t            *action_log;
-       symbols_t                       *symbols;
-       GtkWindow                       *main_window;
-       GtkNotebook                     *notebook;
-       GtkStatusbar            *statusbar;
-       GtkStatusbar            *cursor_position;
-       GtkAction                       *undo;
-       GtkAction                       *redo;
-       GKeyFile                        *key_file;
-       gchar                           *pref_file;
-       PangoFontDescription *font_desc;
-       gint                            font_size;
+       gboolean                                show_line_numbers;
+       gboolean                                show_side_pane;
+       gint                                    window_width;
+       gint                                    window_height;
+       gboolean                                window_maximised;
+       gint                                    main_hpaned_pos;
+       gint                                    vpaned_pos;
+       gint                                    log_hpaned_pos;
+       gchar                                   *command_view;
+       gchar                                   *font_str;
+       PangoFontDescription    *font_desc;
+       gint                                    font_size;
+} preferences_t;
+
+typedef struct
+{
+       GList                   *all_docs;
+       document_t              *active_doc;
+       action_log_t    *action_log;
+       symbols_t               *symbols;
+       preferences_t   *prefs;
+       GtkWindow               *main_window;
+       GtkNotebook             *notebook;
+       GtkStatusbar    *statusbar;
+       GtkStatusbar    *cursor_position;
+       GtkAction               *undo;
+       GtkAction               *redo;
+       GtkPaned                *main_hpaned;
+       GtkPaned                *vpaned;
+       GtkPaned                *log_hpaned;
 } latexila_t;
 
-// all the documents are accessible by the "latexila" variable
+// a lot of things are accessible by the "latexila" variable everywhere in the
+// sources (if this file is included)
 extern latexila_t latexila;
 
 // for the actions list in the log zone
diff --git a/src/prefs.c b/src/prefs.c
new file mode 100644
index 0000000..a60eeb5
--- /dev/null
+++ b/src/prefs.c
@@ -0,0 +1,279 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright © 2009 Sébastien Wilmet
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtk/gtk.h>
+#include <gtksourceview/gtksourceview.h>
+#include <sys/stat.h> // for S_IRWXU
+
+#include "main.h"
+#include "prefs.h"
+#include "config.h"
+#include "print.h"
+
+static void load_default_preferences (preferences_t *prefs);
+static gchar * get_rc_file (void);
+
+/* default values */
+// there is an underscore in the end of each variable name
+static gboolean        show_line_numbers_      = FALSE;
+static gboolean        show_side_pane_         = TRUE;
+static gint            window_width_           = 800;
+static gint            window_height_          = 600;
+static gboolean        window_maximised_       = FALSE;
+static gint            main_hpaned_pos_        = 180;
+static gint            vpaned_pos_                     = 380;
+static gint            log_hpaned_pos_         = 190;
+static gchar   *command_view_          = "evince";
+static gchar   *font_                          = "Monospace 10";
+
+void
+load_preferences (preferences_t *prefs)
+{
+       gchar *rc_file = get_rc_file ();
+       if (! g_file_test (rc_file, G_FILE_TEST_EXISTS))
+       {
+               load_default_preferences (prefs);
+               return;
+       }
+
+       GKeyFile *key_file = g_key_file_new ();
+       GError *error = NULL;
+       g_key_file_load_from_file (key_file, rc_file, G_KEY_FILE_NONE, &error);
+       g_free (rc_file);
+
+       if (error != NULL)
+       {
+               print_warning ("load user preferences failed: %s", error->message);
+               g_error_free (error);
+               error = NULL;
+               load_default_preferences (prefs);
+               return;
+       }
+
+       /* check if all keys exist
+        * if not, set the default value for that key
+        */
+       prefs->show_line_numbers = g_key_file_get_boolean (key_file, PROGRAM_NAME,
+                       "show_line_numbers", &error);
+       if (error != NULL)
+       {
+               prefs->show_line_numbers = show_line_numbers_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->show_side_pane = g_key_file_get_boolean (key_file, PROGRAM_NAME,
+                       "show_side_pane", &error);
+       if (error != NULL)
+       {
+               prefs->show_side_pane = show_side_pane_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->window_width = g_key_file_get_integer (key_file, PROGRAM_NAME,
+                       "window_width", &error);
+       if (error != NULL)
+       {
+               prefs->window_width = window_width_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->window_height = g_key_file_get_integer (key_file, PROGRAM_NAME,
+                       "window_height", &error);
+       if (error != NULL)
+       {
+               prefs->window_height = window_height_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->window_maximised = g_key_file_get_boolean (key_file, PROGRAM_NAME,
+                       "window_maximised", &error);
+       if (error != NULL)
+       {
+               prefs->window_maximised = window_maximised_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       // where is the end?
+       
+       prefs->main_hpaned_pos = g_key_file_get_integer (key_file, PROGRAM_NAME,
+                       "main_hpaned_pos", &error);
+       if (error != NULL)
+       {
+               prefs->main_hpaned_pos = main_hpaned_pos_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->vpaned_pos = g_key_file_get_integer (key_file, PROGRAM_NAME,
+                       "vpaned_pos", &error);
+       if (error != NULL)
+       {
+               prefs->vpaned_pos = vpaned_pos_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       prefs->log_hpaned_pos = g_key_file_get_integer (key_file, PROGRAM_NAME,
+                       "log_hpaned_pos", &error);
+       if (error != NULL)
+       {
+               prefs->log_hpaned_pos = log_hpaned_pos_;
+               g_error_free (error);
+               error = NULL;
+       }
+
+       // look, I see light, we are close to the exit!
+       
+       prefs->command_view = g_key_file_get_string (key_file, PROGRAM_NAME,
+                       "command_view", &error);
+       if (error != NULL)
+       {
+               prefs->command_view = g_strdup (command_view_);
+               g_error_free (error);
+               error = NULL;
+       }
+
+       gchar *font = g_key_file_get_string (key_file, PROGRAM_NAME, "font", &error);
+       if (error != NULL)
+       {
+               prefs->font_str = g_strdup (font_);
+               set_current_font_prefs (prefs);
+               g_error_free (error);
+               error = NULL;
+       }
+       else
+       {
+               prefs->font_str = g_strdup (font);
+               set_current_font_prefs (prefs);
+               g_free (font);
+       }
+
+       print_info ("load user preferences: OK");
+}
+
+void
+save_preferences (preferences_t *prefs)
+{
+       GKeyFile *key_file = g_key_file_new ();
+
+       /* set the keys that we can take directly from the prefs struct */
+       g_key_file_set_boolean (key_file, PROGRAM_NAME, "show_line_numbers",
+                       prefs->show_line_numbers);
+       g_key_file_set_boolean (key_file, PROGRAM_NAME, "show_side_pane",
+                       prefs->show_side_pane);
+       g_key_file_set_string (key_file, PROGRAM_NAME, "command_view",
+                       prefs->command_view);
+       g_key_file_set_string (key_file, PROGRAM_NAME, "font", prefs->font_str);
+
+       /* set the keys that must be taken from the widgets */
+       GdkWindowState flag = gdk_window_get_state (gtk_widget_get_window (
+                               GTK_WIDGET (latexila.main_window)));
+       gboolean window_maximised = flag & GDK_WINDOW_STATE_MAXIMIZED;
+       g_key_file_set_boolean (key_file, PROGRAM_NAME, "window_maximised",
+                       window_maximised);
+
+       gint window_width, window_height;
+       gtk_window_get_size (latexila.main_window, &window_width, &window_height);
+
+       /* generate bugs with the panes positions
+       // if the window is maximised, store sizes that are a bit smaller, else
+       // making window non-maximised will have no effect
+       if (window_maximised)
+       {
+               window_width -= window_width / 10;
+               window_height -= window_height / 10;
+       }
+       */
+
+       g_key_file_set_integer (key_file, PROGRAM_NAME, "window_width",
+                       window_width);
+       g_key_file_set_integer (key_file, PROGRAM_NAME, "window_height",
+                       window_height);
+
+       gint main_hpaned_pos = gtk_paned_get_position (latexila.main_hpaned);
+       gint vpaned_pos = gtk_paned_get_position (latexila.vpaned);
+       gint log_hpaned_pos = gtk_paned_get_position (latexila.log_hpaned);
+       g_key_file_set_integer (key_file, PROGRAM_NAME, "main_hpaned_pos",
+                       main_hpaned_pos);
+       g_key_file_set_integer (key_file, PROGRAM_NAME, "vpaned_pos",
+                       vpaned_pos);
+       g_key_file_set_integer (key_file, PROGRAM_NAME, "log_hpaned_pos",
+                       log_hpaned_pos);
+
+       /* save the rc file */
+       gchar *rc_file = get_rc_file ();
+       gchar *rc_path = g_path_get_dirname (rc_file);
+       g_mkdir_with_parents(rc_path, S_IRWXU);
+       gchar *key_file_data = g_key_file_to_data (key_file, NULL, NULL);
+
+       GError *error = NULL;
+       g_file_set_contents (rc_file, key_file_data, -1, &error);
+       
+       if (error != NULL)
+       {
+               print_warning ("impossible to save preferences: %s", error->message);
+               g_error_free (error);
+               error = NULL;
+       }
+
+       g_free (rc_file);
+       g_free (rc_path);
+       g_free (key_file_data);
+}
+
+void
+set_current_font_prefs (preferences_t *prefs)
+{
+       g_return_if_fail (prefs->font_str != NULL);
+
+       prefs->font_desc = pango_font_description_from_string (prefs->font_str);
+       prefs->font_size = pango_font_description_get_size (prefs->font_desc);
+}
+
+static void
+load_default_preferences (preferences_t *prefs)
+{
+       prefs->show_line_numbers = show_line_numbers_;
+       prefs->show_side_pane = show_side_pane_;
+       prefs->window_width = window_width_;
+       prefs->window_height = window_height_;
+       prefs->window_maximised = window_maximised_;
+       prefs->main_hpaned_pos = main_hpaned_pos_;
+       prefs->vpaned_pos = vpaned_pos_;
+       prefs->log_hpaned_pos = log_hpaned_pos_;
+       prefs->command_view = g_strdup (command_view_);
+       prefs->font_str = g_strdup (font_);
+
+       set_current_font_prefs (prefs);
+}
+
+static gchar *
+get_rc_file (void)
+{
+       gchar *rc_file = g_build_filename (g_get_home_dir (), ".config", "latexila",
+                       "latexilarc", NULL);
+       return rc_file;
+}
diff --git a/src/prefs.h b/src/prefs.h
new file mode 100644
index 0000000..3edfc06
--- /dev/null
+++ b/src/prefs.h
@@ -0,0 +1,8 @@
+#ifndef PREFS_H
+#define PREFS_H
+
+void load_preferences (preferences_t *prefs);
+void save_preferences (preferences_t *prefs);
+void set_current_font_prefs (preferences_t *prefs);
+
+#endif /* PREFS_H */


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