[Xfce4-commits] <postler:master> Sort localised folders by their type

Christian Dywan noreply at xfce.org
Sat Jan 22 00:28:01 CET 2011


Updating branch refs/heads/master
         to 5c8896064ac8d19a28e941ec84db00de12631e06 (commit)
       from cd782645626678f40be384861d73e077f0e33871 (commit)

commit 5c8896064ac8d19a28e941ec84db00de12631e06
Author: Sergio Spinatelli <spinatelli.sergio at gmail.com>
Date:   Sat Jan 22 00:11:54 2011 +0100

    Sort localised folders by their type

 postler/postler-accounts.vala |   30 +++++++++++++++++++-----------
 postler/postler-folders.vala  |   40 +++++++++++++++++++++++++++++++++-------
 2 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/postler/postler-accounts.vala b/postler/postler-accounts.vala
index 509b073..acd0705 100644
--- a/postler/postler-accounts.vala
+++ b/postler/postler-accounts.vala
@@ -24,6 +24,7 @@ namespace Postler {
         TRASH,
         ARCHIVE,
         JUNK,
+        GENERIC,
         MAX;
         public string get_stock_id () {
             return localized_folders [this].stock_id;
@@ -38,6 +39,8 @@ namespace Postler {
         public string label;
         string[]? localized;
         int n_localized;
+        public FolderType type;
+        public int i_type;
     }
 
     const string[] sent_folders = {
@@ -61,16 +64,19 @@ namespace Postler {
 
     /* The length values must be in the array to work around Vala mistakenly
        optimizing the value away when using arrays indirectly with MailFolder */
+    /* The index at the end is required for FolderType to work, somehow the
+       index is taken in place of the enum value */
     const MailFolder[] localized_folders = {
-        { "INBOX", STOCK_INBOX, N_("Inbox"), null, 0 },
-        { "Sent", STOCK_SENT_MAIL, N_("Sent"), sent_folders, sent_folders.length },
-        { "Drafts", null, N_("Drafts"), drafts_folders, drafts_folders.length },
-        { "Queue", STOCK_OUTBOX, N_("Outbox"), queue_folders, queue_folders.length },
-        { "Trash", STOCK_USER_TRASH, N_("Trash"), trash_folders, trash_folders.length },
-        { "Archive", STOCK_ARCHIVE, N_("Archive"), archive_folders, archive_folders.length },
-        { null, null, N_("Junk"), junk_folders, junk_folders.length }
+        { "INBOX", STOCK_INBOX, N_("Inbox"), null, 0, FolderType.INBOX, 0 },
+        { "Sent", STOCK_SENT_MAIL, N_("Sent"), sent_folders, sent_folders.length, FolderType.SENT, 1 },
+        { "Drafts", null, N_("Drafts"), drafts_folders, drafts_folders.length, FolderType.DRAFTS, 2 },
+        { "Queue", STOCK_OUTBOX, N_("Outbox"), queue_folders, queue_folders.length, FolderType.QUEUE, 3 },
+        { "Trash", STOCK_USER_TRASH, N_("Trash"), trash_folders, trash_folders.length, FolderType.TRASH, 4 },
+        { "Archive", STOCK_ARCHIVE, N_("Archive"), archive_folders, archive_folders.length, FolderType.ARCHIVE, 5 },
+        { null, null, N_("Junk"), junk_folders, junk_folders.length, FolderType.JUNK, 6 },
+        { null, Gtk.STOCK_DIRECTORY, null, null, 0, FolderType.GENERIC, 7 }
     };
-    const MailFolder generic_folder = { null, Gtk.STOCK_DIRECTORY, null, null };
+
     const string hide_folder_default = "Apple Mail To Do";
 
     public class AccountInfo : GLib.Object {
@@ -90,7 +96,7 @@ namespace Postler {
         public string organization;
         public string? signature;
         public string hide = hide_folder_default;
-        public string[]? folders = { null, null, null, null, null, null, null };
+        public string[]? folders = { null, null, null, null, null, null, null, null };
 
         public unowned string? get_folder (FolderType type) {
             return folders[type];
@@ -107,6 +113,7 @@ namespace Postler {
             string sane_name = folder_name.replace ("[Google Mail]", "[Gmail]");
             for (int type = 0; type < FolderType.MAX; type++) {
                 if (this.folders[type] == null
+                 && localized_folders[type].localized != null
                  && sane_name in localized_folders[type].localized) {
                     this.folders[type] = folder_name;
                     unowned string? role = localized_folders[type].role;
@@ -115,14 +122,15 @@ namespace Postler {
                     return localized_folders[type];
                 }
             }
-            return generic_folder;
+            return localized_folders[FolderType.GENERIC];
         }
 
         public delegate void MailFolderCallback (int type, MailFolder folder);
         public void verify_folders (MailFolderCallback callback) {
             for (int type = 0; type < FolderType.MAX; type++)
                 if (this.folders[type] == null && localized_folders[type].role != null)
-                    callback (type, localized_folders[type]);
+                    if (type != FolderType.GENERIC)
+                        callback (type, localized_folders[type]);
         }
     }
 }
diff --git a/postler/postler-folders.vala b/postler/postler-folders.vala
index a78e3d8..0f0d989 100644
--- a/postler/postler-folders.vala
+++ b/postler/postler-folders.vala
@@ -37,6 +37,7 @@ public class Postler.Folders : Gtk.TreeView {
         TOPLEVEL_MONITOR,
         FOLDER_MONITOR,
         UNREAD_MONITOR,
+        FOLDER_TYPE
     }
 
     public Folders (Accounts accounts) {
@@ -61,10 +62,10 @@ public class Postler.Folders : Gtk.TreeView {
             }
         }
 
-        store = new Gtk.TreeStore (8,
+        store = new Gtk.TreeStore (9,
             typeof (string), typeof (string), typeof (int), typeof (string),
-            typeof (AccountInfo),
-            typeof (GLib.FileMonitor), typeof (GLib.FileMonitor), typeof (GLib.FileMonitor));
+            typeof (AccountInfo), typeof (GLib.FileMonitor),
+            typeof (GLib.FileMonitor), typeof (GLib.FileMonitor), typeof (FolderType));
         set_model (store);
         insert_column_with_attributes (-1, "Icon",
             new Gtk.CellRendererPixbuf (), "stock-id", Columns.ICON);
@@ -72,6 +73,27 @@ public class Postler.Folders : Gtk.TreeView {
             new Gtk.CellRendererText (), "text", Columns.NAME,
                                          "ellipsize", Columns.ELLIPSIZE);
         GLib.Idle.add (populate);
+
+        store.set_sort_func (Columns.FOLDER_TYPE, folder_compare);
+        store.set_sort_column_id (Columns.FOLDER_TYPE, Gtk.SortType.ASCENDING);
+        store.sort_column_changed ();
+    }
+
+    public int folder_compare (Gtk.TreeModel model, Gtk.TreeIter a, Gtk.TreeIter b) {
+        FolderType type_a;
+        FolderType type_b;
+
+        model.get (a, Columns.FOLDER_TYPE, out type_a);
+        model.get (b, Columns.FOLDER_TYPE, out type_b);
+
+        if (type_a < type_b)
+            return -1;
+        if (type_a > type_b)
+            return 1;
+        if (type_a == type_b)
+            return 0;
+
+        assert_not_reached ();
     }
 
     void unread_monitor_changed (File msg_dir, string path, string label) {
@@ -237,7 +259,8 @@ public class Postler.Folders : Gtk.TreeView {
                                 Columns.NAME, parent,
                                 Columns.ELLIPSIZE, Pango.EllipsizeMode.MIDDLE,
                                 Columns.LOCATION, folder_dir_path + "/" + parent_path,
-                                Columns.INFO, account_info);
+                                Columns.INFO, account_info,
+                                Columns.FOLDER_TYPE, FolderType.GENERIC);
                         }
                     } else if (get_folder_iter (account_info.path + "/" + name,
                                                 account_iter, null))
@@ -258,7 +281,8 @@ public class Postler.Folders : Gtk.TreeView {
                             Columns.ICON, STOCK_INBOX,
                             Columns.NAME, label,
                             Columns.LOCATION, account_info.path + "/" + name,
-                            Columns.UNREAD_MONITOR, monitor);
+                            Columns.UNREAD_MONITOR, monitor,
+                            Columns.FOLDER_TYPE, FolderType.INBOX);
                         unread_monitor_changed (msg_dir, path, label);
                         continue;
                     }
@@ -287,7 +311,8 @@ public class Postler.Folders : Gtk.TreeView {
                         Columns.ELLIPSIZE, Pango.EllipsizeMode.MIDDLE,
                         Columns.LOCATION, account_info.path + "/" + name,
                         Columns.INFO, account_info,
-                        Columns.FOLDER_MONITOR, monitor);
+                        Columns.FOLDER_MONITOR, monitor,
+                        Columns.FOLDER_TYPE, folder.type);
                     var msg_dir = folder_dir.resolve_relative_path (
                         account_info.path + "/" + name + "/new");
                     monitor = msg_dir.monitor_directory (0, null);
@@ -307,7 +332,8 @@ public class Postler.Folders : Gtk.TreeView {
                         account_info.folders[type] = folder.role;
                         store.set (folder_iter,
                                    Columns.ICON, folder.stock_id ?? Gtk.STOCK_DIRECTORY,
-                                   Columns.NAME, folder.label);
+                                   Columns.NAME, folder.label,
+                                   Columns.FOLDER_TYPE, folder.type);
                         need_update = true;
                     } else {
                         string missing = account_info.path + "/" + folder.role;



More information about the Xfce4-commits mailing list