[Xfce4-commits] <postler:master> Use FolderType instead of hardcoding folder names

Christian Dywan noreply at xfce.org
Mon Nov 22 22:58:03 CET 2010


Updating branch refs/heads/master
         to 411cb0c636c71afdd290087f38645f81fbd6c138 (commit)
       from 37b6f2c1a1562436ac2cd2378f8aa23295bd96fa (commit)

commit 411cb0c636c71afdd290087f38645f81fbd6c138
Author: Christian Dywan <christian at twotoasts.de>
Date:   Mon Nov 22 22:56:52 2010 +0100

    Use FolderType instead of hardcoding folder names

 postler/postler-accounts.vala |    7 ++---
 postler/postler-bureau.vala   |   13 +++++----
 postler/postler-composer.vala |    9 ++++--
 postler/postler-folders.vala  |    6 ++--
 postler/postler-messages.vala |   55 ++++++++++++++++++----------------------
 5 files changed, 44 insertions(+), 46 deletions(-)

diff --git a/postler/postler-accounts.vala b/postler/postler-accounts.vala
index 8c2ff04..35d1f49 100644
--- a/postler/postler-accounts.vala
+++ b/postler/postler-accounts.vala
@@ -16,7 +16,7 @@ namespace Postler {
         SEARCH
     }
 
-    enum FolderType {
+    public enum FolderType {
         SENT,
         DRAFTS,
         QUEUE,
@@ -86,9 +86,8 @@ namespace Postler {
         public string hide = "Apple Mail To Do";
         public string[]? folders = { null, null, null, null, null };
 
-        public unowned string get_trash ()
-            requires (folders[FolderType.TRASH] != null) {
-            return folders[FolderType.TRASH];
+        public unowned string? get_folder (FolderType type) {
+            return folders[type];
         }
 
         public unowned MailFolder get_localized_folder (string folder_name) {
diff --git a/postler/postler-bureau.vala b/postler/postler-bureau.vala
index 31ee89f..1969202 100644
--- a/postler/postler-bureau.vala
+++ b/postler/postler-bureau.vala
@@ -186,11 +186,11 @@ public class Postler.Bureau : Gtk.Window {
     }
 
     void action_archive () {
-        messages.move_selected ("Archive");
+        messages.move_selected (FolderType.ARCHIVE);
     }
 
     void action_junk () {
-        messages.move_selected ("Junk");
+        messages.move_selected (FolderType.JUNK);
     }
 
     void action_delete () {
@@ -343,7 +343,7 @@ public class Postler.Bureau : Gtk.Window {
 
     void action_hide_read () {
         messages.hide_read = !messages.hide_read;
-        messages.populate (messages.selected_location, messages.trash_folder);
+        messages.populate (messages.selected_location, messages.account_info);
     }
 
     /* TODO: Send outstanding outgoing mail */
@@ -508,11 +508,12 @@ public class Postler.Bureau : Gtk.Window {
             Postler.Messages messages = object as Postler.Messages;
             bool state = messages.selected_location != null;
             actions.get_action ("MessageFlag").sensitive = state;
+            string folder = Path.get_basename (folders.selected_location);
             actions.get_action ("MessageArchive").sensitive = state
-                && !folders.selected_location.has_suffix ("/Archive");
+                && folder != messages.account_info.get_folder (FolderType.ARCHIVE);
             actions.get_action ("MessageJunk").sensitive = state
-                && !folders.selected_location.has_suffix ("/Junk")
-                && true; /* FIXME: only show button if Junk exists */
+                && messages.account_info.get_folder (FolderType.JUNK) != null
+                && folder != messages.account_info.get_folder (FolderType.JUNK);
             actions.get_action ("MessageDelete").sensitive = state;
         });
         scrolled = new Postler.ScrolledWindow (messages);
diff --git a/postler/postler-composer.vala b/postler/postler-composer.vala
index b33d441..1041160 100644
--- a/postler/postler-composer.vala
+++ b/postler/postler-composer.vala
@@ -86,8 +86,10 @@ public class Postler.Composer : Gtk.Window {
         string[] from = Postler.Messages.parse_address (sender);
         foreach (var info in accounts.get_infos ()) {
             if (info.address != null && from[1] in info.address) {
-                if (!Postler.Messages.ensure_folder (info.path + "/Queue")
-                 || !Postler.Messages.ensure_folder (info.path + "/Sent")) {
+                string queue = info.path + "/" + info.get_folder (FolderType.QUEUE);
+                string sent = info.path + "/" + info.get_folder (FolderType.SENT);
+                if (!Postler.Messages.ensure_folder (queue)
+                 || !Postler.Messages.ensure_folder (sent)) {
                     GLib.critical (_("Folders for sent messages couldn't be created."));
                     return;
                 }
@@ -114,7 +116,7 @@ public class Postler.Composer : Gtk.Window {
                 string body = clipboard.wait_for_text ();
                 try {
                     /* TODO: Put in /Queue/new/ first and move on success */
-                    string filename = info.path + "/Sent/cur/"
+                    string filename = sent + "/cur/"
                         + Postler.Messages.generate_maildir_filename ("S");
                     FileUtils.set_contents (filename, header + body, -1);
                     FileUtils.chmod (filename, 0700);
@@ -124,6 +126,7 @@ public class Postler.Composer : Gtk.Window {
                 } catch (GLib.Error error) {
                     GLib.critical (_("Failed to send message: %s"), error.message);
                 }
+                break;
             }
         }
     }
diff --git a/postler/postler-folders.vala b/postler/postler-folders.vala
index f6e569e..86efef6 100644
--- a/postler/postler-folders.vala
+++ b/postler/postler-folders.vala
@@ -284,7 +284,7 @@ public class Postler.Folders : Gtk.TreeView {
         notify_property ("selected-location");
 
         if (location != null) {
-            messages.populate (location, account_info.get_trash ());
+            messages.populate (location, account_info);
             messages.grab_focus ();
         }
         else
@@ -331,7 +331,7 @@ public class Postler.Folders : Gtk.TreeView {
                 FileUtils.set_contents (filename, body.str, -1);
                 FileUtils.chmod (filename, 0700);
             }
-            messages.populate (location, account_info.get_trash ());
+            messages.populate (location, account_info);
         } catch (GLib.Error error) {
             GLib.critical (_("Failed to empty folder \"%s\": %s"),
                 location, error.message);
@@ -377,7 +377,7 @@ public class Postler.Folders : Gtk.TreeView {
                  store.get (iter, Columns.LOCATION, out location,
                                   Columns.INFO, out account_info);
                  var bureau = new Bureau ();
-                 bureau.messages.populate (location, account_info.get_trash ());
+                 bureau.messages.populate (location, account_info);
                  bureau.messages.grab_focus ();
                  bureau.show ();
              }
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index 42e5a24..b30b393 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -20,9 +20,9 @@ public class Postler.Messages : Gtk.TreeView {
     public bool show_attachments { get; set; default = false; }
     public bool newest_at_the_bottom { get; set; default = false; }
     
-    public string? location { get; set; }
-    public string trash_folder = "";
-    public string? selected_location { get; set; }
+    public string? location { get; private set; }
+    public AccountInfo account_info { get; private set; }
+    public string? selected_location { get; private set; }
 
     string to_or_from;
     string last_filter = "";
@@ -468,7 +468,7 @@ public class Postler.Messages : Gtk.TreeView {
 
     public void search (string filter, string header="subject") {
         last_filter = filter;
-        populate (location, trash_folder, filter.down (), header);
+        populate (location, account_info, filter.down (), header);
     }
 
     private struct Message {
@@ -592,7 +592,7 @@ public class Postler.Messages : Gtk.TreeView {
         return message;
     }
 
-    public bool populate (string? location, string trash_folder,
+    public bool populate (string? location, AccountInfo account_info,
         string filter="", string header="subject") {
         clear ();
         if (location == null)
@@ -608,7 +608,9 @@ public class Postler.Messages : Gtk.TreeView {
         now.set_time_val (GLib.TimeVal ());
 
         string basename = Path.get_basename (location);
-        if (basename == "Sent" || basename == "Queue" || basename == "Drafts")
+        if (basename == account_info.get_folder (FolderType.SENT)
+         || basename == account_info.get_folder (FolderType.QUEUE)
+         || basename == account_info.get_folder (FolderType.DRAFTS))
             to_or_from = "to";
         else
             to_or_from = "from";
@@ -697,7 +699,7 @@ public class Postler.Messages : Gtk.TreeView {
         /* Avoid changing 'location' to not cause property notification */
         if (this.location != location)
             this.location = location;
-        this.trash_folder = trash_folder;
+        this.account_info = account_info;
         return false;
     }
 
@@ -834,10 +836,10 @@ public class Postler.Messages : Gtk.TreeView {
     }
 
     public void delete_selected () {
-        if (location.has_suffix ("/" + trash_folder))
+        if (location.has_suffix ("/" + account_info.get_folder (FolderType.TRASH)))
             move_selected (null);
         else
-            move_selected (trash_folder);
+            move_selected (FolderType.TRASH);
     }
 
     static uint maildir_sequence_number = 0;
@@ -859,11 +861,20 @@ public class Postler.Messages : Gtk.TreeView {
             flags);
     }
 
-    public void move_selected (string? destination_folder) {
-        string? destination_path = null;
+    public void move_selected (FolderType? type) {
+        string? destination_path;
+        if (type == null)
+            destination_path = null;
+        else {
+            destination_path = account_info.path + "/" + account_info.get_folder (type);
+            if (!ensure_folder (destination_path)) {
+                GLib.critical (_("Folders for moving messages couldn't be created."));
+                return;
+            }
+        }
+
         GLib.List<Gtk.TreePath> paths;
         var references = new GLib.List<Gtk.TreeRowReference> ();
-
         paths = get_selection ().get_selected_rows (null);
         foreach (var path in paths)
             references.prepend (new Gtk.TreeRowReference (sort, path));
@@ -876,25 +887,9 @@ public class Postler.Messages : Gtk.TreeView {
                 sort.get (sort_iter, Columns.LOCATION, out location);
                 var file = File.new_for_path (location);
                 try {
-                    if (destination_folder == null)
+                    if (destination_path == null)
                         file.delete (null);
                     else {
-                        if (destination_path == null) {
-                            File? parent = file.get_parent ();
-                            if (parent != null)
-                                parent = parent.get_parent ();
-                            if (parent != null)
-                                parent = parent.get_parent ();
-                            if (parent != null)
-                                destination_path = parent.get_path ()
-                                    + "/" + destination_folder;
-                            if (parent == null || !ensure_folder (destination_path)) {
-                                GLib.critical (_("Folders for moving messages "
-                                    + "couldn't be created."));
-                                return;
-                            }
-                        }
-
                         string bare_filename;
                         string flags = flags_from_filename (location, out bare_filename);
                         string filename;
@@ -917,7 +912,7 @@ public class Postler.Messages : Gtk.TreeView {
                     content.clear ();
                 } catch (GLib.Error error) {
                     unowned string message;
-                    if (destination_folder == null)
+                    if (destination_path == null)
                         message = _("Failed to delete message \"%s\": %s");
                     else
                         message = _("Failed to move message \"%s\": %s");



More information about the Xfce4-commits mailing list