[gnome-latex: 105/205] File browser: sort the files in alphabetical order



commit a23a3b5a3896a92151fd65173c5902a138f24f52
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sat Nov 7 23:50:16 2009 +0100

    File browser: sort the files in alphabetical order
    
    The strcmp function is used. It's not perfect but it works :)
    The file names are stored in two GList's: one for files, and the other for
    directories. Then we call g_list_sort () to sort the lists in
    alphabetical order. Then we traverse the two lists for adding all the
    files in the list store.

 TODO               |  6 ++++-
 src/file_browser.c | 70 +++++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 59 insertions(+), 17 deletions(-)
---
diff --git a/TODO b/TODO
index ecd1acc..258f564 100644
--- a/TODO
+++ b/TODO
@@ -3,7 +3,11 @@ TODO LaTeXila
 [-] File browser in the side bar
        x tabs to switch between "Symbols" and "File Browser"
        x file browser integrated
-       - sort the files in alphabetical order
+       x sort the files in alphabetical order
+       - save the current folder in the prefs
+       - other pixbuf for *.pdf, *.dvi and *.ps files
+       - if clicking on a *.pdf, *.dvi or *.ps file, show the document (add a new action)
+       - add an option in the preferences: show only *.tex, *.pdf, *.dvi and *.ps files
 
 [-] Templates
        - create a few default templates
diff --git a/src/file_browser.c b/src/file_browser.c
index e1ff9fc..66e96a2 100644
--- a/src/file_browser.c
+++ b/src/file_browser.c
@@ -34,6 +34,7 @@
 static void fill_list_store_with_current_dir (void);
 static void cb_file_browser_row_activated (GtkTreeView *tree_view,
                GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data);
+gint sort_list_alphabetical_order (gconstpointer a, gconstpointer b);
 
 void
 init_file_browser (void)
@@ -82,14 +83,6 @@ fill_list_store_with_current_dir (void)
        {
                print_warning ("File browser: %s", error->message);
                g_error_free (error);
-
-               // avoid infinite loop
-               if (strcmp (latexila.file_browser.current_dir, g_get_home_dir ()) == 0)
-                       return;
-
-               g_free (latexila.file_browser.current_dir);
-               latexila.file_browser.current_dir = g_strdup (g_get_home_dir ());
-               fill_list_store_with_current_dir ();
                return;
        }
 
@@ -110,8 +103,10 @@ fill_list_store_with_current_dir (void)
                        COLUMN_FILE_BROWSER_FILE, "..",
                        -1);
 
-       // append all the files contained in the directory
+       /* append all the files contained in the directory */
        const gchar *read_name = NULL;
+       GList *directory_list = NULL;
+       GList *file_list = NULL;
        while ((read_name = g_dir_read_name (dir)) != NULL)
        {
                // not show hidden files
@@ -121,24 +116,61 @@ fill_list_store_with_current_dir (void)
                gchar *full_path = g_build_filename (latexila.file_browser.current_dir,
                                read_name, NULL);
 
-               GdkPixbuf *pixbuf;
                if (g_file_test (full_path, G_FILE_TEST_IS_DIR))
-                       pixbuf = pixbuf_dir;
+                       directory_list = g_list_prepend (directory_list, (gpointer) read_name);
                else
-                       pixbuf = pixbuf_file;
+                       file_list = g_list_prepend (file_list, (gpointer) read_name);
+
+               g_free (full_path);
+       }
 
+       g_dir_close (dir);
+
+       directory_list = g_list_sort (directory_list, sort_list_alphabetical_order);
+       file_list = g_list_sort (file_list, sort_list_alphabetical_order);
+
+       // traverse the directory list
+       GList *current = directory_list;
+       while (TRUE)
+       {
+               if (current == NULL)
+                       break;
+
+               gchar *directory = g_list_nth_data (current, 0);
+
+               // append the directory to the list store
                gtk_list_store_append (latexila.file_browser.list_store, &iter);
                gtk_list_store_set (latexila.file_browser.list_store, &iter,
-                               COLUMN_FILE_BROWSER_PIXBUF, pixbuf,
-                               COLUMN_FILE_BROWSER_FILE, read_name,
+                               COLUMN_FILE_BROWSER_PIXBUF, pixbuf_dir,
+                               COLUMN_FILE_BROWSER_FILE, directory,
                                -1);
 
-               g_free (full_path);
+               current = g_list_next (current);
+       }
+
+       // traverse the file list
+       current = file_list;
+       while (TRUE)
+       {
+               if (current == NULL)
+                       break;
+
+               gchar *file = g_list_nth_data (current, 0);
+
+               // append the file to the list store
+               gtk_list_store_append (latexila.file_browser.list_store, &iter);
+               gtk_list_store_set (latexila.file_browser.list_store, &iter,
+                               COLUMN_FILE_BROWSER_PIXBUF, pixbuf_file,
+                               COLUMN_FILE_BROWSER_FILE, file,
+                               -1);
+
+               current = g_list_next (current);
        }
 
        g_object_unref (pixbuf_dir);
        g_object_unref (pixbuf_file);
-       g_dir_close (dir);
+       g_list_free (directory_list);
+       g_list_free (file_list);
 }
 
 static void
@@ -182,3 +214,9 @@ cb_file_browser_row_activated (GtkTreeView *tree_view, GtkTreePath *path,
                g_free (uri);
        }
 }
+
+gint
+sort_list_alphabetical_order (gconstpointer a, gconstpointer b)
+{
+       return strcmp ((char *) a, (char *) b);
+}


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