[Xfce4-commits] <postler:master> Introduce Postler.Contact for address parsing

Christian Dywan noreply at xfce.org
Thu Jun 23 21:00:02 CEST 2011


Updating branch refs/heads/master
         to 7eff2eee4a705c25b271b1abeaf6ee97af73a794 (commit)
       from 522773e6876c419e4c229672503c2bf9ad764ab9 (commit)

commit 7eff2eee4a705c25b271b1abeaf6ee97af73a794
Author: Christian Dywan <christian at twotoasts.de>
Date:   Fri Jun 17 22:19:13 2011 +0200

    Introduce Postler.Contact for address parsing

 postler/postler-composer.vala       |    6 ++--
 postler/postler-contact.vala        |   66 +++++++++++++++++++++++++++++++++++
 postler/postler-content.vala        |   14 ++++----
 postler/postler-message.vala        |    9 ++---
 postler/postler-messages.vala       |   41 +++------------------
 postler/postler-recipiententry.vala |    3 +-
 tests/parsing.vala                  |    2 +-
 7 files changed, 88 insertions(+), 53 deletions(-)

diff --git a/postler/postler-composer.vala b/postler/postler-composer.vala
index a0037d9..24162d5 100644
--- a/postler/postler-composer.vala
+++ b/postler/postler-composer.vala
@@ -74,9 +74,9 @@ public class Postler.Composer : Gtk.Window {
 
     AccountInfo? selected_account_for_address (string address)
     {
-        string[] from = Postler.Messages.parse_address (address);
+        string from = Postler.Contact.address_from_string (address);
         foreach (var info in accounts.get_infos ())
-            if (info.address != null && from[1] in info.address)
+            if (info.address != null && from in info.address)
                 return info;
         return null;
     }
@@ -763,7 +763,7 @@ public class Postler.Composer : Gtk.Window {
         if (name == "from") {
             var model = combo_from.model;
             Gtk.TreeIter iter;
-            string from = Postler.Messages.parse_address (data)[1];
+            string from = Postler.Contact.address_from_string (data);
             if (model.iter_children (out iter, null)) {
                 do {
                     string address;
diff --git a/postler/postler-contact.vala b/postler/postler-contact.vala
new file mode 100644
index 0000000..67c34f0
--- /dev/null
+++ b/postler/postler-contact.vala
@@ -0,0 +1,66 @@
+/*
+ Copyright (C) 2011 Christian Dywan <christian at twotoasts.de>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ See the file COPYING for the full license text.
+*/
+
+namespace Postler {
+    public class Contact {
+        internal static string[] parse (string address)
+            ensures (result[0] != null && result[1] != null) {
+            if (address.length < 1) {
+                GLib.critical ("parse: assertion '!address.length < 1' failed");
+                return { address, address };
+            }
+
+            if (!(">" in address && "<" in address))
+                return { address, address };
+
+            long greater = address.length - 1;
+            while (address[greater] != '>')
+                greater--;
+            long lower = greater;
+            while (address[lower] != '<')
+                lower--;
+
+            string recipient = address.slice (lower + 1, greater);
+            if (recipient.has_prefix ("mailto:"))
+                recipient = recipient.substring (7, -1);
+            if (lower == 0)
+                return { recipient, recipient };
+            if (">" in recipient)
+                return { recipient, recipient };
+
+            /* Remove double or single quotes around the name */
+            long first = address.has_prefix ("'") || address.has_prefix ("\"") ? 1 : 0;
+            return { address.substring (first, lower - 1)
+                .replace ("\\\"", "`")
+                .replace ("' ", "").replace ("\"", "").chomp (), recipient };
+        }
+
+        public static string address_from_string (string contact) {
+            return parse (contact)[1];
+        }
+
+        public static string name_from_string (string contact) {
+            return parse (contact)[0];
+        }
+
+        public static bool equal (string contact, string? other)
+            requires (contact.chr (-1, '<') == null) {
+            if (other == null)
+                return false;
+            if (other.chr (-1, '<') == null && contact == other)
+                return true;
+            if (contact == address_from_string (other))
+                return true;
+            return false;
+        }
+    }
+}
+
diff --git a/postler/postler-content.vala b/postler/postler-content.vala
index c2776ae..d2d8e8d 100644
--- a/postler/postler-content.vala
+++ b/postler/postler-content.vala
@@ -251,7 +251,7 @@ public class Postler.Content : WebKit.WebView {
                 linkified.append (html_escape (address));
                 continue;
             }
-            string[] parsed = Postler.Messages.parse_address (address);
+            string[] parsed = Postler.Contact.parse (address);
             string name = html_escape (parsed[0]);
             string quoted = html_escape (address);
             if (linkified.len > 0)
@@ -490,7 +490,7 @@ public class Postler.Content : WebKit.WebView {
         if (location != null && !is_bug_tracker) {
             ulong now = new Soup.Date.from_now (0).to_time_t ();
             string date = GLib.Time.local ((time_t)now).format ("%x %X");
-            string sender = Postler.Messages.parse_address (recipient)[0];
+            string sender = Postler.Contact.name_from_string (recipient);
 
             body.append_printf (_("On %s, %s wrote:"), date, sender);
             body.append_c ('\n');
@@ -696,7 +696,7 @@ public class Postler.Content : WebKit.WebView {
         /* See if recipient is among the accounts, otherwise pick a fallback */
         string? recipient = message.get_field ("to");
         if (recipient != null) {
-            string from = Postler.Messages.parse_address (recipient)[1];
+            string from = Postler.Contact.address_from_string (recipient);
             var accounts = new Accounts ();
             foreach (var info in accounts.get_infos ())
                 if (info.address != null && from in info.address)
@@ -731,12 +731,12 @@ public class Postler.Content : WebKit.WebView {
         reply_to_all = message.recipients;
         list_post = message.get_field ("list-post");
         if (list_post != null)
-            list_post = Postler.Messages.parse_address (list_post)[1];
+            list_post = Postler.Contact.address_from_string (list_post);
         list_unsubscribe = message.get_field ("list-unsubscribe");
-        if (list_unsubscribe != null)
-            list_unsubscribe = Postler.Messages.parse_address (list_unsubscribe)[1];
-        if (list_unsubscribe != null)
+        if (list_unsubscribe != null) {
+            list_unsubscribe = Postler.Contact.address_from_string (list_unsubscribe);
             list_unsubscribe = linkify_address (list_unsubscribe, null);
+        }
 
         message_parts = new List<MessagePart> ();
         content_encoding = message.get_field ("content-transfer-encoding");
diff --git a/postler/postler-message.vala b/postler/postler-message.vala
index e2e9aab..766687c 100644
--- a/postler/postler-message.vala
+++ b/postler/postler-message.vala
@@ -257,13 +257,12 @@ namespace Postler {
                point to the author but the list.
                Also Reply-To may equal From, which is at best confusing. */
             if (reply_to != null) {
-                string canonical = Postler.Messages.parse_address (reply_to)[1];
-                if (get_field ("to") != null
-                 && canonical == Postler.Messages.parse_address (get_field ("to"))[1])
+                string canonical = Postler.Contact.address_from_string (reply_to);
+                if (Postler.Contact.equal (canonical, get_field ("to")))
                     reply_to = null;
-                else if (get_field ("list-post") != null && canonical == get_field ("list-post"))
+                else if (Postler.Contact.equal (canonical, get_field ("list-post")))
                     reply_to = null;
-                else if (canonical == Postler.Messages.parse_address (sender)[1])
+                else if (Postler.Contact.equal (canonical, sender))
                     reply_to = null;
             }
         }
diff --git a/postler/postler-messages.vala b/postler/postler-messages.vala
index aee94e1..dcd81f1 100644
--- a/postler/postler-messages.vala
+++ b/postler/postler-messages.vala
@@ -150,8 +150,12 @@ public class Postler.Messages : Gtk.TreeView {
         for (int j = 0; j < 20 - size; j++)
             date = date + " ";
 
-        string from = escape_text (parse_address (message.sender ?? _("Unknown"))[0]);
-        from = dexter.get_name (from) ?? from;
+        string from;
+        if (message.sender != null)
+            from = escape_text (dexter.get_name (message.sender)
+                ?? Postler.Contact.name_from_string (message.sender));
+        else
+            from = _("Unknown");
         int weight = message.unread ? Pango.Weight.BOLD : Pango.Weight.NORMAL;
 
         renderer.ellipsize = Pango.EllipsizeMode.MIDDLE;
@@ -371,39 +375,6 @@ public class Postler.Messages : Gtk.TreeView {
         return decoded.str;
     }
 
-    internal static string[] parse_address (string address)
-        ensures (result[0] != null && result[1] != null) {
-        if (address.length < 1) {
-            GLib.critical ("parse_address: assertion '!address.length < 1' failed");
-            return { address, address };
-        }
-
-        if (!(">" in address && "<" in address))
-            return { address, address };
-
-        long greater = address.length - 1;
-        while (address[greater] != '>')
-            greater--;
-        long lower = greater;
-        while (address[lower] != '<')
-            lower--;
-
-        string recipient = address.slice (lower + 1, greater);
-        if (recipient.has_prefix ("mailto:"))
-            recipient = recipient.substring (7, -1);
-        if (lower == 0)
-            return { recipient, recipient };
-        /* TODO: Parse multiple addresses */
-        if (">" in recipient)
-            return { recipient, recipient };
-
-        /* Remove double or single quotes around the name */
-        long first = address.has_prefix ("'") || address.has_prefix ("\"") ? 1 : 0;
-        return { address.substring (first, lower - 1)
-            .replace ("\\\"", "`")
-            .replace ("' ", "").replace ("\"", "").chomp (), recipient };
-    }
-
     public void clear () {
         folder_monitors = {};
         store.clear ();
diff --git a/postler/postler-recipiententry.vala b/postler/postler-recipiententry.vala
index 33d0191..a9ec771 100644
--- a/postler/postler-recipiententry.vala
+++ b/postler/postler-recipiententry.vala
@@ -93,9 +93,8 @@ namespace Postler {
                 return;
             }
 
-            string[] parsed = Postler.Messages.parse_address (address);
             var box = new Gtk.HBox (false, 0);
-            var label = new Gtk.Label (parsed[0]);
+            var label = new Gtk.Label (Postler.Contact.name_from_string (address));
             box.pack_start (label, true, false, 0);
             var icon = new Gtk.Image.from_stock (Gtk.STOCK_CLOSE, Gtk.IconSize.MENU);
             box.pack_end (icon, false, false, 0);
diff --git a/tests/parsing.vala b/tests/parsing.vala
index c1a66b4..5ba8ea2 100644
--- a/tests/parsing.vala
+++ b/tests/parsing.vala
@@ -75,7 +75,7 @@ const TestCase[] addresses = {
 
 void parsing_headers_address () {
     foreach (var address in addresses) {
-        string[] formatted = Postler.Messages.parse_address (address.data);
+        string[] formatted = Postler.Contact.parse (address.data);
         string expected = address.expected;
         if (expected == null)
             expected = address.data + " " + address.data;



More information about the Xfce4-commits mailing list