[geary/mjog/rfc822-cleanup: 13/21] Geary.RFC822.MessageIdList: Update API to match MailboxAddresses



commit 53a6f3153d3ea48b8bfcae9978c3968a7461153a
Author: Michael Gratton <mike vee net>
Date:   Wed May 6 11:25:15 2020 +1000

    Geary.RFC822.MessageIdList: Update API to match MailboxAddresses
    
    Make immutable, provide similar properties and accessors as
    MailboxAddresses.

 src/client/composer/composer-widget.vala    |  4 +--
 src/engine/api/geary-composed-email.vala    |  4 +--
 src/engine/api/geary-email.vala             |  4 +--
 src/engine/rfc822/rfc822-message-data.vala  | 49 +++++++++++++++++++++--------
 src/engine/rfc822/rfc822-utils.vala         |  6 ++--
 test/engine/rfc822/rfc822-message-test.vala |  2 +-
 6 files changed, 46 insertions(+), 23 deletions(-)
---
diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala
index 3e95f1505..5e46c22a0 100644
--- a/src/client/composer/composer-widget.vala
+++ b/src/client/composer/composer-widget.vala
@@ -1131,7 +1131,7 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
                     this.reply_to_entry.addresses = referred.reply_to;
                 }
                 if (referred.in_reply_to != null)
-                    this.in_reply_to.add_all(referred.in_reply_to.list);
+                    this.in_reply_to.add_all(referred.in_reply_to.get_all());
                 if (referred.references != null)
                     this.references = referred.references.to_rfc822_string();
                 if (referred.subject != null)
@@ -1360,7 +1360,7 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface {
              this.context_type == REPLY_ALL) &&
             !this.in_reply_to.is_empty)
             email.set_in_reply_to(
-                new Geary.RFC822.MessageIDList.from_collection(this.in_reply_to)
+                new Geary.RFC822.MessageIDList(this.in_reply_to)
             );
 
         if (!Geary.String.is_empty(this.references)) {
diff --git a/src/engine/api/geary-composed-email.vala b/src/engine/api/geary-composed-email.vala
index 5c3e27d88..a1409cba9 100644
--- a/src/engine/api/geary-composed-email.vala
+++ b/src/engine/api/geary-composed-email.vala
@@ -70,7 +70,7 @@ public class Geary.ComposedEmail : EmailHeaderSet, BaseObject {
     public string img_src_prefix { get; set; default = ""; }
 
     public ComposedEmail(DateTime date, RFC822.MailboxAddresses from) {
-        this.date = new RFC822.Date.from_date_time(date);
+        this.date = new RFC822.Date(date);
         this.from = from;
     }
 
@@ -165,7 +165,7 @@ public class Geary.ComposedEmail : EmailHeaderSet, BaseObject {
             ret = null;
         } else {
             RFC822.MessageIDList? ids = list as RFC822.MessageIDList;
-            if (ids != null && ids.list.size == 0) {
+            if (ids != null && ids.size == 0) {
                 ret = null;
             }
         }
diff --git a/src/engine/api/geary-email.vala b/src/engine/api/geary-email.vala
index 04a2cdb4c..b2b77c867 100644
--- a/src/engine/api/geary-email.vala
+++ b/src/engine/api/geary-email.vala
@@ -523,12 +523,12 @@ public class Geary.Email : BaseObject, EmailHeaderSet {
 
         // References list the email trail back to its source
         if (references != null)
-            ancestors.add_all(references.list);
+            ancestors.add_all(references.get_all());
 
         // RFC822 requires the In-Reply-To Message-ID be prepended to the References list, but
         // this ensures that's the case
         if (in_reply_to != null)
-           ancestors.add_all(in_reply_to.list);
+           ancestors.add_all(in_reply_to.get_all());
 
        return (ancestors.size > 0) ? ancestors : null;
     }
diff --git a/src/engine/rfc822/rfc822-message-data.vala b/src/engine/rfc822/rfc822-message-data.vala
index 14523cfc7..d3a6441f1 100644
--- a/src/engine/rfc822/rfc822-message-data.vala
+++ b/src/engine/rfc822/rfc822-message-data.vala
@@ -37,30 +37,38 @@ public class Geary.RFC822.MessageID : Geary.MessageData.StringMessageData, Geary
 }
 
 /**
- * A Message-ID list stores its IDs from earliest to latest.
+ * A immutable list of RFC822 Message-ID values.
  */
 public class Geary.RFC822.MessageIDList : Geary.MessageData.AbstractMessageData, Geary.RFC822.MessageData {
     public Gee.List<MessageID> list { get; private set; }
 
-    public MessageIDList() {
-        list = new Gee.ArrayList<MessageID>();
+
+    /** Returns the number of ids in this list. */
+    public int size {
+        get { return this.list.size; }
+    }
+
+    /** Determines if there are no ids in the list. */
+    public bool is_empty {
+        get { return this.list.is_empty; }
     }
 
-    public MessageIDList.from_collection(Gee.Collection<MessageID> collection) {
-        this ();
+    private Gee.List<MessageID> list = new Gee.ArrayList<MessageID>();
+
 
-        foreach(MessageID msg_id in collection)
-            this.list.add(msg_id);
+    public MessageIDList(Gee.Collection<MessageID>? collection = null) {
+        if (collection != null) {
+            this.list.add_all(collection);
+        }
     }
 
     public MessageIDList.single(MessageID msg_id) {
-        this ();
-
+        this();
         list.add(msg_id);
     }
 
     public MessageIDList.from_rfc822_string(string value) {
-        this ();
+        this();
 
         // Have seen some mailers use commas between Message-IDs and whitespace inside Message-IDs,
         // meaning that the standard whitespace tokenizer is not sufficient.  The only guarantee
@@ -143,12 +151,26 @@ public class Geary.RFC822.MessageIDList : Geary.MessageData.AbstractMessageData,
         // from any non-empty string, an empty Message-ID (i.e. "<>") won't.
     }
 
+    /** Returns the id at the given index, if it exists. */
+    public new MessageID? get(int index) {
+        return this.list.get(index);
+    }
+
+    /** Returns a read-only iterator of the ids in this list. */
+    public Gee.Iterator<MessageID> iterator() {
+        return this.list.read_only_view.iterator();
+    }
+
+    /** Returns a read-only collection of the ids in this list. */
+    public Gee.List<MessageID> get_all() {
+        return this.list.read_only_view;
+    }
+
     /**
      * Returns a new list with the given messages ids appended to this list's.
      */
     public MessageIDList append(MessageIDList others) {
-        MessageIDList new_ids = new MessageIDList();
-        new_ids.list.add_all(this.list);
+        MessageIDList new_ids = new MessageIDList(this.list);
         new_ids.list.add_all(others.list);
         return new_ids;
     }
@@ -157,13 +179,14 @@ public class Geary.RFC822.MessageIDList : Geary.MessageData.AbstractMessageData,
         return "MessageIDList (%d)".printf(list.size);
     }
 
-    public virtual string to_rfc822_string() {
+    public string to_rfc822_string() {
         string[] strings = new string[list.size];
         for(int i = 0; i < list.size; ++i)
             strings[i] = list[i].value;
 
         return string.joinv(" ", strings);
     }
+
 }
 
 public class Geary.RFC822.Date : Geary.RFC822.MessageData, Geary.MessageData.AbstractMessageData,
diff --git a/src/engine/rfc822/rfc822-utils.vala b/src/engine/rfc822/rfc822-utils.vala
index 866e8098d..51fae6a37 100644
--- a/src/engine/rfc822/rfc822-utils.vala
+++ b/src/engine/rfc822/rfc822-utils.vala
@@ -101,13 +101,13 @@ namespace Geary.RFC822.Utils {
         var list = new Gee.ArrayList<MessageID>();
 
         // 1. Start with the source's References list
-        if (source.references != null && source.references.list.size > 0) {
-            list.add_all(source.references.list);
+        if (source.references != null) {
+            list.add_all(source.references.get_all());
         }
 
         // 2. If there are In-Reply-To Message-IDs and they're not in the References list, append them
         if (source.in_reply_to != null) {
-            foreach (var reply_id in source.in_reply_to.list) {
+            foreach (var reply_id in source.in_reply_to.get_all()) {
                 if (!list.contains(reply_id)) {
                     list.add(reply_id);
                 }
diff --git a/test/engine/rfc822/rfc822-message-test.vala b/test/engine/rfc822/rfc822-message-test.vala
index 979fcccb1..1d263cc09 100644
--- a/test/engine/rfc822/rfc822-message-test.vala
+++ b/test/engine/rfc822/rfc822-message-test.vala
@@ -100,7 +100,7 @@ This is the second line.
     public void duplicate_message_id() throws Error {
         Message dup = string_to_message(DUPLICATE_REFERENCES);
 
-        assert(dup.references.list.size == 2);
+        assert(dup.references.size == 2);
         assert_message_id_list(
             dup.references, "<1234@local.machine.example> <5678@local.machine.example>"
         );


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