2 * Copyright (C) 2007-2011 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Xavier Claessens <xclaesse@gmail.com>
19 * Philip Withnall <philip.withnall@collabora.co.uk>
20 * Danielle Madeley <danielle.madeley@collabora.co.uk>
28 #include <glib/gi18n-lib.h>
30 #include <telepathy-glib/util.h>
32 #include <libempathy/empathy-time.h>
34 #include "empathy-contactinfo-utils.h"
35 #include "empathy-string-parser.h"
38 linkify_first_value (GStrv values)
40 return empathy_add_link_markup (values[0]);
44 format_idle_time (GStrv values)
46 const gchar *value = values[0];
47 int duration = strtol (value, NULL, 10);
52 return empathy_duration_to_string (duration);
56 format_server (GStrv values)
58 g_assert (values[0] != NULL);
60 if (values[1] == NULL)
61 return g_markup_escape_text (values[0], -1);
63 return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
67 presence_hack (GStrv values)
69 if (tp_str_empty (values[0]))
72 return g_markup_escape_text (values[0], -1);
77 const gchar *field_name;
79 EmpathyContactInfoFormatFunc format;
82 /* keep this syncronised with info_field_data below */
83 static const char *info_field_names[] =
95 "x-presence-status-message",
100 static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
102 { "fn", N_("Full name"), NULL },
103 { "tel", N_("Phone number"), NULL },
104 { "email", N_("E-mail address"), linkify_first_value },
105 { "url", N_("Website"), linkify_first_value },
106 { "bday", N_("Birthday"), NULL },
108 /* Note to translators: this is the caption for a string of the form "5
109 * minutes ago", and refers to the time since the contact last interacted
110 * with their IM client. */
111 { "x-idle-time", N_("Last seen:"), format_idle_time },
112 { "x-irc-server", N_("Server:"), format_server },
113 { "x-host", N_("Connected from:"), format_server },
115 /* FIXME: once Idle implements SimplePresence using this information, we can
116 * and should bin this. */
117 { "x-presence-status-message", N_("Away message:"), presence_hack },
128 static InfoParameterData info_parameter_data[] =
130 { "work", N_("work") },
131 { "home", N_("home") },
132 { "cell", N_("mobile") },
133 { "voice", N_("voice") },
134 { "pref", N_("preferred") },
135 { "postal", N_("postal") },
136 { "parcel", N_("parcel") },
141 empathy_contact_info_get_field_names (guint *nnames)
144 *nnames = G_N_ELEMENTS (info_field_names) - 1;
146 return info_field_names;
150 empathy_contact_info_lookup_field (const gchar *field_name,
152 EmpathyContactInfoFormatFunc *format)
156 for (i = 0; info_field_data[i].field_name != NULL; i++)
158 if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
161 *title = gettext (info_field_data[i].title);
164 *format = info_field_data[i].format;
174 build_parameters_string (GStrv parameters)
176 GPtrArray *output = g_ptr_array_new ();
180 for (iter = parameters; iter != NULL && *iter != NULL; iter++)
182 static const char *prefix = "type=";
183 const char *param = *iter;
184 InfoParameterData *iter2;
186 if (!g_str_has_prefix (param, prefix))
189 param += strlen (prefix);
191 for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
193 if (!tp_strdiff (iter2->type, param))
195 g_ptr_array_add (output, gettext (iter2->title));
201 if (output->len == 0)
204 g_ptr_array_add (output, NULL); /* NULL-terminate */
206 join = g_strjoinv (", ", (char **) output->pdata);
207 g_ptr_array_free (output, TRUE);
213 empathy_contact_info_field_label (const char *field_name,
218 char *join = build_parameters_string (parameters);
220 if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
224 ret = g_strdup_printf ("%s (%s):", title, join);
226 ret = g_strdup_printf ("%s:", title);
234 contact_info_field_name_cmp (const gchar *name1,
239 if (tp_strdiff (name1, name2) == FALSE)
242 /* We use the order of info_field_data */
243 for (i = 0; info_field_data[i].field_name != NULL; i++)
245 if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
247 if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
251 return g_strcmp0 (name1, name2);
255 empathy_contact_info_field_cmp (TpContactInfoField *field1,
256 TpContactInfoField *field2)
258 return contact_info_field_name_cmp (field1->field_name, field2->field_name);
262 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
263 TpContactInfoFieldSpec *spec2)
265 return contact_info_field_name_cmp (spec1->name, spec2->name);