From 0d283dfc72b2a34dc662f6a70cdb41976d730f30 Mon Sep 17 00:00:00 2001 From: Michael Gratton Date: Thu, 6 Aug 2020 14:50:18 +1000 Subject: [PATCH] Geary.RFC822.MailboxAddresses: Update append methods Rename append method to concatenate_list since that's what it actually does. Add new method for cat'ing a single mailbox, add methods for merging both a single mailbox and mailbox list into a new list. --- src/client/composer/composer-widget.vala | 4 +- .../rfc822/rfc822-mailbox-addresses.vala | 55 ++++++++++++++++++- .../rfc822/rfc822-mailbox-addresses-test.vala | 15 +++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/src/client/composer/composer-widget.vala b/src/client/composer/composer-widget.vala index c62fb804..a420e5d0 100644 --- a/src/client/composer/composer-widget.vala +++ b/src/client/composer/composer-widget.vala @@ -1182,12 +1182,12 @@ public class Composer.Widget : Gtk.EventBox, Geary.BaseInterface { foreach (var candidate in email_map.get_keys()) { if (candidate.message_id != null && mid.equal_to(candidate.message_id)) { - to_addresses = to_addresses.append( + to_addresses = to_addresses.merge_list( Geary.RFC822.Utils.create_to_addresses_for_reply( candidate, sender_addresses ) ); - cc_addresses = cc_addresses.append( + cc_addresses = cc_addresses.merge_list( Geary.RFC822.Utils.create_cc_addresses_for_reply_all( candidate, sender_addresses ) diff --git a/src/engine/rfc822/rfc822-mailbox-addresses.vala b/src/engine/rfc822/rfc822-mailbox-addresses.vala index 7a40f822..81b95b5e 100644 --- a/src/engine/rfc822/rfc822-mailbox-addresses.vala +++ b/src/engine/rfc822/rfc822-mailbox-addresses.vala @@ -71,16 +71,25 @@ public class Geary.RFC822.MailboxAddresses : private uint hash_value = 0; + /** + * Constructs a new mailbox list. + * + * If the optional collection of addresses is not given, the list + * is created empty. Otherwise the collection's addresses are + * added to the list by iterating over it in natural order. + */ public MailboxAddresses(Gee.Collection? addrs = null) { if (addrs != null) { this.addrs.add_all(addrs); } } + /** Constructs a new mailbox list with a single address. */ public MailboxAddresses.single(MailboxAddress addr) { this.addrs.add(addr); } + /** Constructs a new mailbox list by parsing a RFC822 string. */ public MailboxAddresses.from_rfc822_string(string rfc822) throws Error { var list = GMime.InternetAddressList.parse( @@ -93,6 +102,7 @@ public class Geary.RFC822.MailboxAddresses : this.from_gmime(list); } + /** Constructs a new mailbox from a GMime list. */ public MailboxAddresses.from_gmime(GMime.InternetAddressList list) throws Error { int length = list.length(); @@ -165,11 +175,52 @@ public class Geary.RFC822.MailboxAddresses : return false; } + /** + * Returns a list with the given mailbox appended if not already present. + * + * This list is returned if the given mailbox is already present, + * otherwise the result of a call to {@link concatenate_mailbox} is + * returned. + */ + public MailboxAddresses merge_mailbox(MailboxAddress other) { + return ( + this.addrs.contains(other) + ? this + : this.concatenate_mailbox(other) + ); + } + + /** + * Returns a list with the given mailboxes appended if not already present. + * + * This list is returned if all given mailboxes are already + * present, otherwise the result of a call to {@link + * concatenate_mailbox} for each not present is returned. + */ + public MailboxAddresses merge_list(MailboxAddresses other) { + var list = this; + foreach (var addr in other) { + if (!this.addrs.contains(addr)) { + list = list.concatenate_mailbox(addr); + } + } + return list; + } + + /** + * Returns a new list with the given address appended to this list's. + */ + public MailboxAddresses concatenate_mailbox(MailboxAddress other) { + var new_addrs = new MailboxAddresses(this.addrs); + new_addrs.addrs.add(other); + return new_addrs; + } + /** * Returns a new list with the given addresses appended to this list's. */ - public MailboxAddresses append(MailboxAddresses others) { - MailboxAddresses new_addrs = new MailboxAddresses(this.addrs); + public MailboxAddresses concatenate_list(MailboxAddresses others) { + var new_addrs = new MailboxAddresses(this.addrs); new_addrs.addrs.add_all(others.addrs); return new_addrs; } diff --git a/test/engine/rfc822/rfc822-mailbox-addresses-test.vala b/test/engine/rfc822/rfc822-mailbox-addresses-test.vala index 2c969b92..d4c3ea66 100644 --- a/test/engine/rfc822/rfc822-mailbox-addresses-test.vala +++ b/test/engine/rfc822/rfc822-mailbox-addresses-test.vala @@ -13,6 +13,7 @@ class Geary.RFC822.MailboxAddressesTest : TestCase { add_test("from_rfc822_string_quoted", from_rfc822_string_quoted); add_test("to_rfc822_string", to_rfc822_string); add_test("contains_all", contains_all); + add_test("merge", merge); add_test("equal_to", equal_to); add_test("hash", hash); } @@ -85,6 +86,20 @@ class Geary.RFC822.MailboxAddressesTest : TestCase { ); } + public void merge() throws GLib.Error { + var a1 = new MailboxAddress(null, "a@example.com"); + var b = new MailboxAddress(null, "b@example.com"); + var a2 = new MailboxAddress(null, "a@example.com"); + var list = new MailboxAddresses.single(a1); + + assert_equal(list.merge_mailbox(b).size, 2); + assert_equal(list.merge_mailbox(a2).size, 1); + + assert_equal(list.merge_list(new MailboxAddresses.single(b)).size, 2); + assert_equal(list.merge_list(new MailboxAddresses.single(a2)).size, 1); + } + + public void equal_to() throws GLib.Error { var mailboxes_a = new_addreses({ "test1@example.com" }); var mailboxes_b = new_addreses({ "test1@example.com" });