From 14b8da134abffcf0272c06c72d5f6df6036e06e7 Mon Sep 17 00:00:00 2001 From: Eitan Isaacson Date: Sun, 16 Jan 2011 11:48:24 +0200 Subject: [PATCH] Modified backends to use LinkedHashSet in IMable.im_addresses. --- backends/key-file/kf-persona-store.vala | 4 +- backends/key-file/kf-persona.vala | 75 +++++++++++++-------------------- backends/telepathy/lib/tpf-persona.vala | 12 +++--- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/backends/key-file/kf-persona-store.vala b/backends/key-file/kf-persona-store.vala index a5ba6fd..4a3d1a4 100644 --- a/backends/key-file/kf-persona-store.vala +++ b/backends/key-file/kf-persona-store.vala @@ -296,8 +296,8 @@ public class Folks.Backends.Kf.PersonaStore : Folks.PersonaStore HashTable details) throws Folks.PersonaStoreError { unowned Value val = details.lookup ("im-addresses"); - unowned HashTable> im_addresses = - (HashTable>) val.get_boxed (); + unowned HashTable> im_addresses = + (HashTable>) val.get_boxed (); if (im_addresses == null || im_addresses.size () == 0) { diff --git a/backends/key-file/kf-persona.vala b/backends/key-file/kf-persona.vala index c83a595..1c338f8 100644 --- a/backends/key-file/kf-persona.vala +++ b/backends/key-file/kf-persona.vala @@ -36,7 +36,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, /* FIXME: As described in the IMable interface, we have to use * GenericArray here rather than just string[], as null-terminated * arrays aren't supported as generic types. */ - private HashTable> _im_addresses; + private HashTable> _im_addresses; private string _alias; private const string[] _linkable_properties = { "im-addresses" }; @@ -74,7 +74,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, /** * {@inheritDoc} */ - public HashTable> im_addresses + public HashTable> im_addresses { get { return this._im_addresses; } @@ -97,48 +97,42 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, /* Add the new IM addresses to the key file and build a normalised * table of them to set as the new property value */ - HashTable> im_addresses = - new HashTable> (str_hash, str_equal); + HashTable> im_addresses = + new HashTable> (str_hash, str_equal); value.foreach ((k, v) => { unowned string protocol = (string) k; - unowned GenericArray addresses = - (GenericArray) v; - var offset = 0; + unowned LinkedHashSet addresses = + (LinkedHashSet) v; + LinkedHashSet normalized_addresses = + new LinkedHashSet (); - for (var i = 0; i < addresses.length; i++) + foreach (string address in addresses) { + string normalized_address; try { - addresses[i - offset] = - IMable.normalise_im_address (addresses[i], - protocol); + normalized_address = IMable.normalise_im_address ( + address, protocol); } catch (IMableError e) { /* Somehow an error has crept into the user's * relationships.ini. Warn of it and ignore the IM - * address. We achieve this by decrementing the offset - * between the index of the address being set and the - * index of the address being read. */ + * address. */ warning (e.message); - addresses[i - offset] = null; - offset++; + continue; } - } - /* Nullify the last few addresses if we have a non-zero offset, - * as they're the gaps left by invalid addresses, which we want - * set_string_list() to ignore. */ - for (; offset > 0; offset--) - addresses[addresses.length - offset] = null; + normalized_addresses.add (normalized_address); + } - unowned string[] addrs = (string[]) ((PtrArray) addresses).pdata; - addrs.length = (int) addresses.length; + string[] addrs = (string[]) normalized_addresses.to_array (); + addrs.length = normalized_addresses.size; this._key_file.set_string_list (this.display_id, protocol, addrs); - im_addresses.insert (protocol, addresses); + im_addresses.insert (protocol, normalized_addresses); }); this._im_addresses = im_addresses; @@ -169,7 +163,7 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, id); this._key_file = key_file; - this._im_addresses = new HashTable> ( + this._im_addresses = new HashTable> ( str_hash, str_equal); /* Load the IM addresses from the key file */ @@ -192,34 +186,26 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, var im_addresses = this._key_file.get_string_list ( this.display_id, protocol); - /* FIXME: We have to convert our nice efficient string[] to a - * GenericArray because Vala doesn't like null-terminated - * arrays as generic types. - * We can take this opportunity to remove duplicates. */ - var address_set = new HashSet (); - var im_address_array = new GenericArray (); + var address_set = new LinkedHashSet (); foreach (var im_address in im_addresses) { + string address; try { - var address = IMable.normalise_im_address (im_address, + address = IMable.normalise_im_address (im_address, protocol); - - if (!address_set.contains (address)) - { - im_address_array.add (address); - address_set.add (address); - } } catch (IMableError e) { /* Warn of and ignore any invalid IM addresses */ warning (e.message); + continue; } + address_set.add (address); } - this._im_addresses.insert (protocol, im_address_array); + this._im_addresses.insert (protocol, address_set); } } catch (KeyFileError e) @@ -247,14 +233,11 @@ public class Folks.Backends.Kf.Persona : Folks.Persona, this.im_addresses.foreach ((k, v) => { unowned string protocol = (string) k; - unowned GenericArray im_addresses = - (GenericArray) v; + unowned LinkedHashSet im_addresses = + (LinkedHashSet) v; - im_addresses.foreach ((v) => - { - unowned string address = (string) v; + foreach (string address in im_addresses) callback (protocol + ":" + address); - }); }); } else diff --git a/backends/telepathy/lib/tpf-persona.vala b/backends/telepathy/lib/tpf-persona.vala index d98a129..f168d6c 100644 --- a/backends/telepathy/lib/tpf-persona.vala +++ b/backends/telepathy/lib/tpf-persona.vala @@ -38,7 +38,7 @@ public class Tpf.Persona : Folks.Persona, private HashTable _groups; private bool _is_favourite; private string _alias; - private HashTable> _im_addresses; + private HashTable> _im_addresses; private const string[] _linkable_properties = { "im-addresses" }; /* Whether we've finished being constructed; this is used to prevent @@ -123,7 +123,7 @@ public class Tpf.Persona : Folks.Persona, * * See {@link Folks.IMable.im_addresses}. */ - public HashTable> im_addresses + public HashTable> im_addresses { get { return this._im_addresses; } private set {} @@ -243,10 +243,10 @@ public class Tpf.Persona : Folks.Persona, this._is_constructed = true; /* Set our single IM address */ - GenericArray im_address_array = new GenericArray (); + LinkedHashSet im_address_set = new LinkedHashSet (); try { - im_address_array.add (IMable.normalise_im_address (id, + im_address_set.add (IMable.normalise_im_address (id, account.get_protocol ())); } catch (IMableError e) @@ -256,8 +256,8 @@ public class Tpf.Persona : Folks.Persona, } this._im_addresses = - new HashTable> (str_hash, str_equal); - this._im_addresses.insert (account.get_protocol (), im_address_array); + new HashTable> (str_hash, str_equal); + this._im_addresses.insert (account.get_protocol (), im_address_set); /* Groups */ this._groups = new HashTable (str_hash, str_equal); -- 2.7.4