2 * Copyright (C) 2011 Collabora Ltd.
4 * This library is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or
7 * (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 * Marco Barisione <marco.barisione@collabora.co.uk>
19 * Travis Reitter <travis.reitter@collabora.co.uk>
26 * Object representing any type of value that can have some vCard-like
27 * parameters associated with it.
29 * Some contact details, like phone numbers or URLs, can have some
30 * extra details associated with them.
31 * For instance, a phone number expressed in vcard notation as
32 * `tel;type=work,voice:(111) 555-1234` would be represented as
33 * a AbstractFieldDetails with value "(111) 555-1234" and with parameters
34 * `['type': ('work', 'voice')]`.
36 * The parameter name "type" with values "work", "home", or "other" are common
37 * amongst most vCard attributes (and thus most AbstractFieldDetails-derived
38 * classes). A "type" of "pref" may be used to indicate a preferred
39 * {@link AbstractFieldDetails.value} amongst many. See specific classes for
40 * information on additional parameters and values specific to that class.
42 * See [[http://www.ietf.org/rfc/rfc2426.txt|RFC2426]] for more details on
43 * pre-defined parameter names and values.
47 public abstract class Folks.AbstractFieldDetails<T> : Object
50 * Parameter name for classifying the type of value this field contains.
52 * For example, the value could be relevant to the contact's home life, or to
53 * their work life; values of {@link AbstractFieldDetails.PARAM_TYPE_HOME}
54 * and {@link AbstractFieldDetails.PARAM_TYPE_WORK} would be used for the
55 * {@link AbstractFieldDetails.PARAM_TYPE} parameter, respectively, in those
60 public static const string PARAM_TYPE = "type";
63 * Parameter value for home-related field values.
65 * Value for a parameter with name {@link AbstractFieldDetails.PARAM_TYPE}.
69 public static const string PARAM_TYPE_HOME = "home";
72 * Parameter value for work-related field values.
74 * Value for a parameter with name {@link AbstractFieldDetails.PARAM_TYPE}.
78 public static const string PARAM_TYPE_WORK = "work";
81 * Parameter value for miscellaneous field values.
83 * Value for a parameter with name {@link AbstractFieldDetails.PARAM_TYPE}.
87 public static const string PARAM_TYPE_OTHER = "other";
91 * The value of the field.
93 * The value of the field, the exact type and content of which depends on what
94 * the structure is used for.
98 public virtual T @value
100 get { return this._value; }
101 set { this._value = value; }
104 private MultiMap<string, string> _parameters =
105 new HashMultiMap<string, string> ();
107 * The parameters associated with the value.
109 * A multi-map of the parameters associated with
110 * {@link Folks.AbstractFieldDetails.value}. The keys are the names of
111 * the parameters, while the values are a list of strings.
115 public virtual MultiMap<string, string> parameters
117 get { return this._parameters; }
121 this._parameters.clear ();
123 this._parameters = value;
128 * Get the values for a parameter
130 * @param parameter_name the parameter name
131 * @return a collection of values for `parameter_name` or `null` (i.e. no
132 * collection) if there are no such parameters.
136 public Collection<string>? get_parameter_values (string parameter_name)
138 if (this.parameters.contains (parameter_name) == false)
143 return this.parameters.get (parameter_name).read_only_view;
147 * Add a new value for a parameter.
149 * If there is already a parameter called `parameter_name` then
150 * `parameter_value` is added to the existing ones.
152 * @param parameter_name the name of the parameter
153 * @param parameter_value the value to add
157 public void add_parameter (string parameter_name, string parameter_value)
159 this.parameters.set (parameter_name, parameter_value);
163 * Set the value of a parameter.
165 * Sets the parameter called `parameter_name` to be `parameter_value`.
166 * If there were already parameters with the same name they are replaced.
168 * @param parameter_name the name of the parameter
169 * @param parameter_value the value to add
173 public void set_parameter (string parameter_name, string parameter_value)
175 this.parameters.remove_all (parameter_name);
176 this.parameters.set (parameter_name, parameter_value);
180 * Extend the existing parameters.
182 * Merge the parameters from `additional` into the existing ones.
184 * @param additional the parameters to add
188 public void extend_parameters (MultiMap<string, string> additional)
190 foreach (var name in additional.get_keys ())
192 var values = additional.get (name);
193 foreach (var val in values)
195 this.add_parameter (name, val);
201 * Remove all instances of a parameter.
203 * @param parameter_name the name of the parameter
207 public void remove_parameter_all (string parameter_name)
209 this.parameters.remove_all (parameter_name);
213 * An equality function for {@link AbstractFieldDetails}.
215 * This defaults to string comparison of the
216 * {@link AbstractFieldDetails.value}s if the generic type is string;
217 * otherwise, direct pointer comparison of the
218 * {@link AbstractFieldDetails.value}s.
220 * @param that another {@link AbstractFieldDetails}
222 * @return whether the elements are equal
226 public virtual bool equal (AbstractFieldDetails<T> that)
228 EqualFunc equal_func = direct_equal;
230 if (typeof (T) == typeof (string))
231 equal_func = str_equal;
233 if ((this.get_type () != that.get_type ()) ||
234 !equal_func (this.value, that.value))
239 /* Check that the parameter names and their values match exactly in both
240 * AbstractFieldDetails objects. */
241 if (this.parameters.size != that.parameters.size)
244 foreach (var param in this.parameters.get_keys ())
246 /* Since these parameters are meant to model vCard parameters, we
247 * should compare on a case-insensitive basis. However, this leads to
248 * ambiguity in the case:
250 * this.parameters = {"foo": {"bar"}}
251 * that.parameters = {"foo": {"bar"}, "FOO": {"qux"}}
253 * So parameter names should normalised elsewhere (either in the
254 * insertion functions (with a big warning for the clients) or in the
255 * clients themselves).
257 * Note that parameter values can't be normalised in general, since
258 * they can be user-set labels.
260 if (!that.parameters.contains (param))
263 var this_param_values = this.parameters.get_values ();
264 var that_param_values = that.parameters.get_values ();
266 if (this_param_values.size != that_param_values.size)
269 foreach (var param_val in this.parameters.get_values ())
271 if (!that_param_values.contains (param_val))
280 * A hash function for the {@link AbstractFieldDetails}.
282 * This defaults to a string hash of the
283 * {@link AbstractFieldDetails.value} if the generic type is string;
284 * otherwise, direct hash of the {@link AbstractFieldDetails.value}.
286 * @return the hash value
290 public virtual uint hash ()
292 HashFunc hash_func = direct_hash;
294 if (typeof (T) == typeof (string))
295 hash_func = str_hash;
297 return hash_func (this.value);