[gnome-latex: 122/205] Reopen files on startup: use GPtrArray



commit 18fb717ab88249ed70ef82391e0c26ea6d131a48
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Tue Nov 17 21:34:51 2009 +0100

    Reopen files on startup: use GPtrArray

 src/callbacks.c | 44 +++++++-------------------------------------
 src/main.c      |  8 +++++---
 src/main.h      |  3 +--
 src/prefs.c     | 27 ++++++++++++++++++---------
 4 files changed, 31 insertions(+), 51 deletions(-)
---
diff --git a/src/callbacks.c b/src/callbacks.c
index 14fa81c..ce19de7 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -52,7 +52,6 @@ static gboolean find_next_match (const gchar *what, GtkSourceSearchFlags flags,
 static void free_latexila (void);
 
 static gboolean save_list_opened_docs = FALSE;
-static GList *list_opened_docs = NULL;
 
 void
 cb_new (void)
@@ -188,40 +187,13 @@ cb_close_tab (GtkWidget *widget, GtkWidget *child)
 void
 cb_quit (void)
 {
-       // remember the files opened at exit for reopening them at next starting
-       // list_opened_docs (a GList) is filled in the close_document () function
+       // remember the files opened at exit for reopening them on next startup
+       // latexila.prefs.list_opened_docs is filled in close_document ()
        // (wich is called indirectly by calling close_all ())
        save_list_opened_docs = TRUE;
-       list_opened_docs = NULL;
 
        if (close_all ())
        {
-               g_strfreev (latexila.prefs.list_opened_docs);
-
-               // convert the GList into an array
-               guint nb_opened_docs = g_list_length (list_opened_docs);
-               latexila.prefs.nb_opened_docs = nb_opened_docs;
-               latexila.prefs.list_opened_docs = g_malloc (
-                               (nb_opened_docs + 1) * sizeof (gchar *));
-
-               gint i = 0;
-               GList *current = list_opened_docs;
-               while (TRUE)
-               {
-                       if (current == NULL)
-                       {
-                               // NULL-terminate the array so we can call g_strfreev () to free the
-                               // list
-                               latexila.prefs.list_opened_docs[i] = NULL;
-                               break;
-                       }
-
-                       latexila.prefs.list_opened_docs[i] = g_list_nth_data (current, 0);
-                       
-                       current = g_list_next (current);
-                       i++;
-               }
-
                save_preferences (&latexila.prefs);
                free_latexila ();
                print_info ("Bye bye");
@@ -231,10 +203,9 @@ cb_quit (void)
        // free the list of opened documents
        else
        {
+               g_ptr_array_free (latexila.prefs.list_opened_docs, TRUE);
+               latexila.prefs.list_opened_docs = g_ptr_array_new ();
                save_list_opened_docs = FALSE;
-               g_list_foreach (list_opened_docs, (GFunc) g_free, NULL);
-               g_list_free (list_opened_docs);
-               list_opened_docs = NULL;
        }
 }
 
@@ -1083,8 +1054,8 @@ close_document (gint index)
        if (latexila.active_doc->path != NULL)
        {
                if (save_list_opened_docs)
-                       list_opened_docs = g_list_append (list_opened_docs,
-                                       g_strdup (latexila.active_doc->path));
+                       g_ptr_array_add (latexila.prefs.list_opened_docs,
+                                       (gpointer) g_strdup (latexila.active_doc->path));
 
                print_info ("close the file \"%s\"", latexila.active_doc->path);
                g_free (latexila.active_doc->path);
@@ -1402,8 +1373,7 @@ free_latexila (void)
        g_free (latexila.prefs.command_dvips);
        g_free (latexila.prefs.file_chooser_dir);
        g_free (latexila.prefs.file_browser_dir);
-       g_strfreev (latexila.prefs.list_opened_docs);
-       g_list_free (list_opened_docs);
+       g_ptr_array_free (latexila.prefs.list_opened_docs, TRUE);
 
        for (int i = 0 ; i < 7 ; i++)
                g_object_unref (latexila.symbols.list_stores[i]);
diff --git a/src/main.c b/src/main.c
index 0f8312c..a66e697 100644
--- a/src/main.c
+++ b/src/main.c
@@ -313,12 +313,14 @@ main (int argc, char *argv[])
                gtk_widget_hide (latexila.edit_toolbar);
 
        /* reopen files on startup */
-       for (int i = 0 ; i < latexila.prefs.nb_opened_docs
+       gchar ** list_opened_docs = (gchar **) latexila.prefs.list_opened_docs->pdata;
+       for (int i = 0 ; i < latexila.prefs.list_opened_docs->len
                        && latexila.prefs.reopen_files_on_startup ; i++)
        {
-               gchar *path = latexila.prefs.list_opened_docs[i];
-               open_new_document_without_uri (path);
+               open_new_document_without_uri (list_opened_docs[i]);
        }
+       g_ptr_array_free (latexila.prefs.list_opened_docs, TRUE);
+       latexila.prefs.list_opened_docs = g_ptr_array_new ();
 
        /* if --new-document option is used */
        if (option_new_document)
diff --git a/src/main.h b/src/main.h
index 015c5d5..0898576 100644
--- a/src/main.h
+++ b/src/main.h
@@ -91,8 +91,7 @@ typedef struct
        gchar                                   *command_dvips;
        gchar                                   *file_chooser_dir;
        gchar                                   *file_browser_dir;
-       gchar                                   **list_opened_docs;
-       guint                                   nb_opened_docs;
+       GPtrArray                               *list_opened_docs;
        gboolean                                reopen_files_on_startup;
 } preferences_t;
 
diff --git a/src/prefs.c b/src/prefs.c
index b581d3e..c9bcf46 100644
--- a/src/prefs.c
+++ b/src/prefs.c
@@ -271,19 +271,27 @@ load_preferences (preferences_t *prefs)
                error = NULL;
        }
 
-       gsize nb_opened_docs;
-       prefs->list_opened_docs = g_key_file_get_string_list (key_file, PROGRAM_NAME,
-                       "list_opened_documents", &nb_opened_docs, &error);
+       gchar **list_opened_docs = g_key_file_get_string_list (key_file, PROGRAM_NAME,
+                       "list_opened_documents", NULL, &error);
+       prefs->list_opened_docs = g_ptr_array_new ();
        if (error != NULL)
        {
                print_warning ("%s", error->message);
-               prefs->nb_opened_docs = 0;
-               prefs->list_opened_docs = NULL;
                g_error_free (error);
                error = NULL;
        }
        else
-               prefs->nb_opened_docs = (guint) nb_opened_docs;
+       {
+               gchar **current = list_opened_docs;
+               while (*current != NULL)
+               {
+                       g_ptr_array_add (prefs->list_opened_docs,
+                                       (gpointer) g_strdup (*current));
+                       current++;
+               }
+
+               g_strfreev (list_opened_docs);
+       }
 
        prefs->reopen_files_on_startup = g_key_file_get_boolean (key_file,
                        PROGRAM_NAME, "reopen_files_on_startup", &error);
@@ -328,7 +336,9 @@ save_preferences (preferences_t *prefs)
        g_key_file_set_string (key_file, PROGRAM_NAME, "file_browser_directory",
                        prefs->file_browser_dir);
        g_key_file_set_string_list (key_file, PROGRAM_NAME, "list_opened_documents",
-                       (const gchar **) prefs->list_opened_docs, prefs->nb_opened_docs);
+                       (const gchar **) prefs->list_opened_docs->pdata,
+                       prefs->list_opened_docs->len);
+
        g_key_file_set_boolean (key_file, PROGRAM_NAME, "reopen_files_on_startup",
                        prefs->reopen_files_on_startup);
 
@@ -420,8 +430,7 @@ load_default_preferences (preferences_t *prefs)
        prefs->command_dvips = g_strdup (command_dvips_);
        prefs->file_chooser_dir = NULL;
        prefs->file_browser_dir = g_strdup (g_get_home_dir ());
-       prefs->list_opened_docs = NULL;
-       prefs->nb_opened_docs = 0;
+       prefs->list_opened_docs = g_ptr_array_new ();
        prefs->reopen_files_on_startup = reopen_files_on_startup_;
 
        set_current_font_prefs (prefs);


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