Commit 40b460dc authored by Michael Gratton's avatar Michael Gratton 🤞

Tidy up MailboxAddress{es} source and API a bit.

parent 178ce351
......@@ -16,8 +16,10 @@
*
* See [[https://tools.ietf.org/html/rfc5322#section-3.4]]
*/
public class Geary.RFC822.MailboxAddress : Geary.MessageData.SearchableMessageData,
Gee.Hashable<MailboxAddress>, BaseObject {
public class Geary.RFC822.MailboxAddress :
Geary.MessageData.SearchableMessageData,
Gee.Hashable<MailboxAddress>,
BaseObject {
/** Determines if a string contains a valid RFC822 mailbox address. */
public static bool is_valid_address(string address) {
......@@ -76,9 +78,6 @@ public class Geary.RFC822.MailboxAddress : Geary.MessageData.SearchableMessageDa
}
internal delegate string ListToStringDelegate(MailboxAddress address);
/**
* The optional human-readable part of the mailbox address.
*
......@@ -414,26 +413,4 @@ public class Geary.RFC822.MailboxAddress : Geary.MessageData.SearchableMessageDa
return to_rfc822_string();
}
internal static string list_to_string(Gee.List<MailboxAddress> addrs,
string empty, ListToStringDelegate to_s) {
switch (addrs.size) {
case 0:
return empty;
case 1:
return to_s(addrs[0]);
default:
StringBuilder builder = new StringBuilder();
foreach (MailboxAddress addr in addrs) {
if (!String.is_empty(builder.str))
builder.append(", ");
builder.append(to_s(addr));
}
return builder.str;
}
}
}
/* Copyright 2016 Software Freedom Conservancy Inc.
/*
* Copyright 2016 Software Freedom Conservancy Inc.
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
......@@ -12,21 +13,62 @@
*
* See [[https://tools.ietf.org/html/rfc5322#section-3.4]]
*/
public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageData,
Geary.MessageData.SearchableMessageData, Geary.RFC822.MessageData, Gee.Hashable<MailboxAddresses> {
public int size { get { return addrs.size; } }
public class Geary.RFC822.MailboxAddresses :
Geary.MessageData.AbstractMessageData,
Geary.MessageData.SearchableMessageData,
Geary.RFC822.MessageData, Gee.Hashable<MailboxAddresses> {
/**
* Converts a list of mailbox addresses to a string.
*
* The delegate //to_s// is used for converting addresses in the
* given list. If the list is empty, the given empty string is
* returned.
*/
private static string list_to_string(Gee.List<MailboxAddress> addrs,
string empty,
ListToStringDelegate to_s) {
switch (addrs.size) {
case 0:
return empty;
case 1:
return to_s(addrs[0]);
default:
StringBuilder builder = new StringBuilder();
foreach (MailboxAddress addr in addrs) {
if (!String.is_empty(builder.str))
builder.append(", ");
builder.append(to_s(addr));
}
return builder.str;
}
}
/** Signature for "to_string" implementation for {@link list_to_string}. */
private delegate string ListToStringDelegate(MailboxAddress address);
/** Returns the number of addresses in this list. */
public int size {
get { return this.addrs.size; }
}
private Gee.List<MailboxAddress> addrs = new Gee.ArrayList<MailboxAddress>();
public MailboxAddresses(Gee.Collection<MailboxAddress> addrs) {
this.addrs.add_all(addrs);
}
public MailboxAddresses.single(MailboxAddress addr) {
addrs.add(addr);
this.addrs.add(addr);
}
public MailboxAddresses.from_rfc822_string(string rfc822) {
InternetAddressList addrlist = InternetAddressList.parse_string(rfc822);
if (addrlist == null)
......@@ -57,41 +99,41 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
}
}
}
public new MailboxAddress? get(int index) {
return addrs.get(index);
}
public Gee.Iterator<MailboxAddress> iterator() {
return addrs.iterator();
}
public Gee.List<MailboxAddress> get_all() {
return addrs.read_only_view;
}
public bool contains_normalized(string address) {
if (addrs.size < 1)
return false;
string normalized_address = address.normalize().casefold();
foreach (MailboxAddress mailbox_address in addrs) {
if (mailbox_address.address.normalize().casefold() == normalized_address)
return true;
}
return false;
}
public bool contains(string address) {
if (addrs.size < 1)
return false;
foreach (MailboxAddress a in addrs)
if (a.address == address)
return true;
return false;
}
......@@ -112,7 +154,7 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
* @see MailboxAddress.to_rfc822_string
*/
public string to_rfc822_string() {
return MailboxAddress.list_to_string(addrs, ", ", (a) => a.to_rfc822_string());
return list_to_string(addrs, ", ", (a) => a.to_rfc822_string());
}
public uint hash() {
......@@ -120,28 +162,28 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
Gee.TreeSet<string> sorted_addresses = traverse<RFC822.MailboxAddress>(addrs)
.map<string>(m => m.address)
.to_tree_set(String.stri_cmp);
// xor all strings in sorted order
uint xor = 0;
foreach (string address in sorted_addresses)
xor ^= address.hash();
return xor;
}
public bool equal_to(MailboxAddresses other) {
if (this == other)
return true;
if (addrs.size != other.addrs.size)
return false;
Gee.HashSet<RFC822.MailboxAddress> first = new Gee.HashSet<RFC822.MailboxAddress>();
first.add_all(addrs);
Gee.HashSet<RFC822.MailboxAddress> second = new Gee.HashSet<RFC822.MailboxAddress>();
second.add_all(other.addrs);
return Collection.are_sets_equal<RFC822.MailboxAddress>(first, second);
}
......@@ -149,11 +191,11 @@ public class Geary.RFC822.MailboxAddresses : Geary.MessageData.AbstractMessageDa
* See Geary.MessageData.SearchableMessageData.
*/
public string to_searchable_string() {
return MailboxAddress.list_to_string(addrs, " ", (a) => a.to_searchable_string());
return list_to_string(addrs, " ", (a) => a.to_searchable_string());
}
public override string to_string() {
return MailboxAddress.list_to_string(addrs, "(no addresses)", (a) => a.to_string());
return list_to_string(addrs, "(no addresses)", (a) => a.to_string());
}
}
}
/* Copyright 2016 Software Freedom Conservancy Inc.
/*
* Copyright 2016 Software Freedom Conservancy Inc.
* Copyright 2018 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
......@@ -716,19 +718,21 @@ public class Geary.RFC822.Message : BaseObject {
return body;
}
/**
* Return the full list of recipients (to, cc, and bcc) as a searchable
* string. Note that values that come out of this function are persisted.
*/
public string? get_searchable_recipients() {
Gee.List<RFC822.MailboxAddress>? recipients = get_recipients();
if (recipients == null)
return null;
return RFC822.MailboxAddress.list_to_string(recipients, "", (a) => a.to_searchable_string());
string searchable = null;
Gee.List<RFC822.MailboxAddress>? recipient_list = get_recipients();
if (recipient_list != null) {
MailboxAddresses recipients = new MailboxAddresses(recipient_list);
searchable = recipients.to_searchable_string();
}
return searchable;
}
public Memory.Buffer get_content_by_mime_id(string mime_id) throws RFC822Error {
GMime.Part? part = find_mime_part_by_mime_id(message.get_mime_part(), mime_id);
if (part == null)
......
/*
* Copyright 2016 Michael Gratton <mike@vee.net>
* Copyright 2016-2018 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment