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.
This commit is contained in:
Michael Gratton 2020-08-06 14:50:18 +10:00 committed by Michael James Gratton
parent 6c944703e5
commit 0d283dfc72
3 changed files with 70 additions and 4 deletions

View file

@ -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
)

View file

@ -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<MailboxAddress>? 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;
}

View file

@ -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<int?>(list.merge_mailbox(b).size, 2);
assert_equal<int?>(list.merge_mailbox(a2).size, 1);
assert_equal<int?>(list.merge_list(new MailboxAddresses.single(b)).size, 2);
assert_equal<int?>(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" });