[Xfce4-commits] <postler:master> Move flag handling into Postler.Message

Christian Dywan noreply at xfce.org
Fri May 27 20:32:08 CEST 2011


Updating branch refs/heads/master
         to 81001743bcef4a5781fc1d2d115fe64b068efd63 (commit)
       from 8ed3c62f8b01d5fe75b4afc443ea3a43894a347e (commit)

commit 81001743bcef4a5781fc1d2d115fe64b068efd63
Author: Christian Dywan <christian at twotoasts.de>
Date:   Thu May 26 23:18:00 2011 +0200

    Move flag handling into Postler.Message
    
    And stop exposing bare filenames in the message treeview.

 postler/postler-bureau.vala   |   16 ++--
 postler/postler-message.vala  |   13 +++-
 postler/postler-messages.vala |  152 +++++++++++++----------------------------
 3 files changed, 66 insertions(+), 115 deletions(-)

diff --git a/postler/postler-bureau.vala b/postler/postler-bureau.vala
index 9890ac3..8d623b2 100644
--- a/postler/postler-bureau.vala
+++ b/postler/postler-bureau.vala
@@ -547,7 +547,7 @@ public class Postler.Bureau : Gtk.Window {
                     assert_not_reached ();
                 }
             });
-            messages.notify["selected-location"].connect ((object, pspec) => {
+            messages.notify["selected-message"].connect ((object, pspec) => {
                 infobar.set_response_sensitive (1,
                     messages.get_selection ().count_selected_rows () > 0);
             });
@@ -580,7 +580,7 @@ public class Postler.Bureau : Gtk.Window {
         infobar.response.connect_after ((response) => {
             infobar.destroy ();
         });
-        messages.notify["selected-location"].connect ((object, pspec) => {
+        messages.notify["selected-message"].connect ((object, pspec) => {
             infobar.destroy ();
         });
         client.progress.connect ((account_name, text, fraction) => {
@@ -836,9 +836,9 @@ public class Postler.Bureau : Gtk.Window {
         actions.get_action ("MessageArchive").sensitive = false;
         actions.get_action ("MessageJunk").sensitive = false;
         actions.get_action ("MessageDelete").sensitive = false;
-        messages.notify["selected-location"].connect ((object, pspec) => {
+        messages.notify["selected-message"].connect ((object, pspec) => {
             Postler.Messages messages = object as Postler.Messages;
-            bool state = messages.selected_location != null;
+            bool state = messages.selected_message != null;
             actions.get_action ("MessageFlag").sensitive = state;
             actions.get_action ("MessageArchive").sensitive = state
                 && messages.account_info.get_folder (FolderType.ARCHIVE) != null;
@@ -912,9 +912,9 @@ public class Postler.Bureau : Gtk.Window {
         actions.get_action ("MessageReplyAll").sensitive = false;
         actions.get_action ("MessageForward").sensitive = false;
         actions.get_action ("ViewSource").sensitive = false;
-        messages.notify["selected-location"].connect ((object, pspec) => {
+        messages.notify["selected-message"].connect ((object, pspec) => {
             Postler.Messages messages = object as Postler.Messages;
-            bool state = messages.selected_location != null;
+            bool state = messages.selected_message != null;
             actions.get_action ("MessageReply").sensitive = state;
             actions.get_action ("MessageForward").sensitive = state;
             state = content.reply_to_all != null;
@@ -938,8 +938,8 @@ public class Postler.Bureau : Gtk.Window {
         messages.notify["location"].connect ((object, pspec) => {
             viewer.hide ();
         });
-        messages.notify["selected-location"].connect ((object, pspec) => {
-            if (messages.selected_location != null && !viewer.visible) {
+        messages.notify["selected-message"].connect ((object, pspec) => {
+            if (messages.selected_message != null && !viewer.visible) {
                 content.clear ();
                 viewer.show ();
                 /* Ensure that the selection is visible, in case of resizing */
diff --git a/postler/postler-message.vala b/postler/postler-message.vala
index 9d0985c..cb709d6 100644
--- a/postler/postler-message.vala
+++ b/postler/postler-message.vala
@@ -22,8 +22,8 @@ namespace Postler {
         public string? sender { public get; set; }
         public string? recipients { public get; set; }
         public string? reply_to { public get; set; }
-        public bool unread { public get; set; } /* TODO: should be writable */
-        public bool flagged { public get; set; } /* TODO: should be writable */
+        public bool unread { public get; set; }
+        public bool flagged { public get; set; }
         public bool priority { public get; set; }
         public string organization { public get; set; }
         public string application { public get; set; }
@@ -90,6 +90,15 @@ namespace Postler {
             }
         }
 
+        public void toggle_flag (char flag) {
+            string old_path = get_path ();
+            return_if_fail (old_path != null);
+            string new_path = Postler.Messages.toggle_flag (old_path, flag);
+            if (FileUtils.rename (old_path, new_path) == 0) {
+                read_flags (new_path);
+            }
+        }
+
         public Message.from_file (GLib.File file,
             GLib.Cancellable? cancellable = null) throws GLib.Error {
 
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index cc330bf..6dfe791 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -20,7 +20,7 @@ public class Postler.Messages : Gtk.TreeView {
 
     public string? location { get; private set; }
     public AccountInfo account_info { get; private set; }
-    public string? selected_location { get; private set; }
+    public Message? selected_message { get; private set; }
 
     string to_or_from;
     string[] headers = {};
@@ -29,8 +29,7 @@ public class Postler.Messages : Gtk.TreeView {
     FileMonitor[] folder_monitors = {};
 
     enum Columns {
-        MESSAGE,
-        LOCATION
+        MESSAGE
     }
 
     bool search_inline (Gtk.TreeModel model, int column, string key,
@@ -43,11 +42,11 @@ public class Postler.Messages : Gtk.TreeView {
     void selection_changed () {
         Gtk.TreeIter iter;
         if (get_selected_iter (out iter)) {
-            string location;
-            model.get (iter, Columns.LOCATION, out location);
-            selected_location = location;
+            Message message;
+            model.get (iter, Columns.MESSAGE, out message);
+            selected_message = message;
         } else
-            selected_location = null;
+            selected_message = null;
     }
 
     static string escape_text (string text) {
@@ -163,9 +162,9 @@ public class Postler.Messages : Gtk.TreeView {
         if (!model.get_iter_from_string (out iter, path))
             return;
 
-        string location;
-        model.get (iter, Columns.LOCATION, out location);
-        toggle_message_flag (iter, ref location, 'F');
+        Message message;
+        model.get (iter, Columns.MESSAGE, out message);
+        message.toggle_flag ('F');
     }
 
     void renderer_status_toggled (Gtk.CellRendererToggle renderer,
@@ -174,9 +173,9 @@ public class Postler.Messages : Gtk.TreeView {
         if (!model.get_iter_from_string (out iter, path))
             return;
 
-        string location;
-        model.get (iter, Columns.LOCATION, out location);
-        toggle_message_flag (iter, ref location, 'S');
+        Message message;
+        model.get (iter, Columns.MESSAGE, out message);
+        message.toggle_flag ('S');
     }
 
     void on_drag_data_get (Gdk.DragContext context,
@@ -188,9 +187,9 @@ public class Postler.Messages : Gtk.TreeView {
         string[] uris = null;
         foreach (Gtk.TreePath path in rows) {
             model.get_iter_from_string (out iter, path.to_string ());
-            string? location;
-            model.get (iter, Columns.LOCATION, out location);
-            uris += ("file://" + location);
+            Message message;
+            model.get (iter, Columns.MESSAGE, out message);
+            uris += message.uri;
         }
         if (uris != null)
             selection_data.set_uris (uris);
@@ -204,7 +203,7 @@ public class Postler.Messages : Gtk.TreeView {
 
         this.accounts = accounts;
         set_fixed_height_mode (true);
-        store = new Gtk.TreeStore (2, typeof (Message), typeof (string));
+        store = new Gtk.TreeStore (1, typeof (Message));
         set_search_equal_func (search_inline);
         get_selection ().set_mode (Gtk.SelectionMode.MULTIPLE);
         get_selection ().changed.connect (selection_changed);
@@ -287,10 +286,11 @@ public class Postler.Messages : Gtk.TreeView {
     void content_new_window () {
         Gtk.TreeIter iter;
         if (get_selected_iter (out iter)) {
-            string location;
-            model.get (iter, Columns.LOCATION, out location);
-            mark_message_read (iter, ref location);
-            Postler.App.spawn_module ("content", location);
+            Message message;
+            model.get (iter, Columns.MESSAGE, out message);
+            if (message.unread)
+                message.toggle_flag ('S');
+            Postler.App.spawn_module ("content", message.get_path ());
         }
     }
     [Signal (action=true)]
@@ -421,41 +421,6 @@ public class Postler.Messages : Gtk.TreeView {
         return parts[1];
     }
 
-    static string parse_flags (string name, out string flagged, out int weight) {
-        /* format "unique:2,DFPRST", ordered alphabetically */
-        unowned string status = STOCK_MAIL_UNREAD;
-        string bare_filename;
-        string flags = flags_from_filename (name, out bare_filename);
-        if (flags == "")
-            return status;
-
-        foreach (var character in flags.to_utf8 ()) {
-            switch (character) {
-            case 'D':
-                status = STOCK_EMBLEM_DRAFT;
-                break;
-            case 'F':
-                flagged = STOCK_EMBLEM_IMPORTANT;
-                break;
-            case 'P':
-                status = STOCK_MAIL_FORWARDED;
-                break;
-            case 'R':
-                status = STOCK_MAIL_REPLIED;
-                break;
-            case 'S':
-                weight = Pango.Weight.NORMAL;
-                if (status == STOCK_MAIL_UNREAD)
-                    status = null;
-                break;
-            case 'T':
-                flagged = "T";
-                break;
-            }
-        }
-        return status;
-    }
-
     public void search (string filter, string header="subject") {
         headers = {};
         filters = {};
@@ -513,8 +478,7 @@ public class Postler.Messages : Gtk.TreeView {
 
             foreach (var message in messages) {
                 store.insert_with_values (null, null, 0,
-                    Columns.MESSAGE, message,
-                    Columns.LOCATION, message.get_path ());
+                    Columns.MESSAGE, message);
             }
 
             /* Show error for failed search, ie. no results */
@@ -580,23 +544,6 @@ public class Postler.Messages : Gtk.TreeView {
         return folder + "/" + Path.get_basename (bare_filename) + ":" + new_flags.str;
     }
 
-    void toggle_message_flag (Gtk.TreeIter iter, ref string location, char flag) {
-        return_if_fail (location != null);
-        string new_location = toggle_flag (location, flag);
-        /* TODO update message file */
-        if (FileUtils.rename (location, new_location) == 0) {
-            location = new_location;
-            int font_weight = Pango.Weight.BOLD;
-            string? flagged = null;
-            string status = parse_flags (location, out flagged, out font_weight);
-
-            store.set (iter,
-                       Columns.LOCATION, new_location);
-            if (location == selected_location)
-                selected_location = new_location;
-        }
-    }
-
     bool get_selected_iter (out Gtk.TreeIter iter) {
         GLib.List<Gtk.TreePath> paths = get_selection ().get_selected_rows (null);
         iter = Gtk.TreeIter ();
@@ -611,9 +558,9 @@ public class Postler.Messages : Gtk.TreeView {
         if (!store.iter_children (out message_iter, null))
             return false;
         do {
-            string existing_location;
-            store.get (message_iter, Columns.LOCATION, out existing_location);
-            if (existing_location == location) {
+            Message existing_message;
+            store.get (message_iter, Columns.MESSAGE, out existing_message);
+            if (existing_message.get_path () == location) {
                 if (&iter != null)
                     iter = message_iter;
                 return true;
@@ -658,8 +605,7 @@ public class Postler.Messages : Gtk.TreeView {
                 var message = new Message.from_file (file);
                 bool scroll = vadjustment.value == 0;
                 store.insert_with_values (null, null, 0,
-                    Columns.MESSAGE, message,
-                    Columns.LOCATION, message.get_path ());
+                    Columns.MESSAGE, message);
                 if (scroll)
                     vadjustment.value = 0;
             }
@@ -682,13 +628,6 @@ public class Postler.Messages : Gtk.TreeView {
         }
     }
 
-    void mark_message_read (Gtk.TreeIter iter, ref string location) {
-        return_if_fail (location != null);
-        string bare_filename;
-        if (!flags_from_filename (location, out bare_filename).contains ("S"))
-            toggle_message_flag (iter, ref location, 'S');
-    }
-
     public void toggle_selected_flag (char flag) {
         GLib.List<Gtk.TreePath> paths;
         var references = new GLib.List<Gtk.TreeRowReference> ();
@@ -701,18 +640,19 @@ public class Postler.Messages : Gtk.TreeView {
             var path = reference.get_path ();
             Gtk.TreeIter iter;
             if (model.get_iter (out iter, path)) {
-                string location;
-                model.get (iter, Columns.LOCATION, out location);
-                toggle_message_flag (iter, ref location, flag);
+                Message message;
+                model.get (iter, Columns.MESSAGE, out message);
+                message.toggle_flag (flag);
             }
         }
     }
 
     void display_message (Gtk.TreeIter iter) {
-        string location;
-        model.get (iter, Columns.LOCATION, out location);
-        mark_message_read (iter, ref location);
-        content.display (location, account_info);
+        Message message;
+        model.get (iter, Columns.MESSAGE, out message);
+        if (message.unread)
+            message.toggle_flag ('S');
+        content.display (message.get_path (), account_info);
     }
 
     public static bool ensure_folder (string folder) {
@@ -813,15 +753,16 @@ public class Postler.Messages : Gtk.TreeView {
             var path = reference.get_path ();
             Gtk.TreeIter iter;
             if (model.get_iter (out iter, path)) {
-                string location;
-                model.get (iter, Columns.LOCATION, out location);
-                var file = File.new_for_path (location);
+                Message message;
+                model.get (iter, Columns.MESSAGE, out message);
+                var file = File.new_for_path (message.get_path ());
                 try {
                     if (destination_path == null)
                         file.delete (null);
                     else {
                         if (account_info.type == AccountType.SEARCH) {
                             /* Look for the account belonging to the message */
+                            string location = file.get_path ();
                             string inbox_folder = location.substring (0,
                                 location.pointer_to_offset (location.rstr ("/")));
                             return_if_fail (inbox_folder.has_suffix ("/INBOX/new")
@@ -856,12 +797,12 @@ public class Postler.Messages : Gtk.TreeView {
                         }
                     }
                 } catch (GLib.Error error) {
-                    unowned string message;
+                    unowned string warning;
                     if (destination_path == null)
-                        message = _("Failed to delete message \"%s\": %s");
+                        warning = _("Failed to delete message \"%s\": %s");
                     else
-                        message = _("Failed to move message \"%s\": %s");
-                    GLib.critical (message, location, error.message);
+                        warning = _("Failed to move message \"%s\": %s");
+                    GLib.critical (warning, location, error.message);
                 }
             }
         }
@@ -875,10 +816,11 @@ public class Postler.Messages : Gtk.TreeView {
         if (event.type == Gdk.EventType.2BUTTON_PRESS) {
             Gtk.TreeIter iter;
             if (get_selected_iter (out iter)) {
-                string location;
-                model.get (iter, Columns.LOCATION, out location);
-                mark_message_read (iter, ref location);
-                Postler.App.spawn_module ("content", location);
+                Message message;
+                model.get (iter, Columns.MESSAGE, out message);
+                if (message.unread)
+                    message.toggle_flag ('S');
+                Postler.App.spawn_module ("content", message.get_path ());
             }
         } else if (event.type == Gdk.EventType.BUTTON_PRESS
                 && get_selection ().count_selected_rows () > 1



More information about the Xfce4-commits mailing list