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>
25 #include <glib/gi18n-lib.h>
27 #include <libempathy/empathy-time.h>
28 #include <libempathy/empathy-request-util.h>
30 #include "empathy-contactinfo-utils.h"
31 #include "empathy-string-parser.h"
32 #include "empathy-ui-utils.h"
35 linkify_first_value (GStrv values)
37 return empathy_add_link_markup (values[0]);
41 format_idle_time (GStrv values)
43 const gchar *value = values[0];
44 int duration = strtol (value, NULL, 10);
49 return empathy_duration_to_string (duration);
53 format_server (GStrv values)
55 g_assert (values[0] != NULL);
57 if (values[1] == NULL)
58 return g_markup_escape_text (values[0], -1);
60 return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
64 presence_hack (GStrv values)
66 if (tp_str_empty (values[0]))
69 return g_markup_escape_text (values[0], -1);
74 const gchar *field_name;
76 EmpathyContactInfoFormatFunc format;
79 /* keep this syncronised with info_field_data below */
80 static const char *info_field_names[] =
92 "x-presence-status-message",
97 static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
99 { "fn", N_("Full name"), NULL },
100 { "tel", N_("Phone number"), NULL },
101 { "email", N_("E-mail address"), linkify_first_value },
102 { "url", N_("Website"), linkify_first_value },
103 { "bday", N_("Birthday"), NULL },
105 /* Note to translators: this is the caption for a string of the form "5
106 * minutes ago", and refers to the time since the contact last interacted
107 * with their IM client. */
108 { "x-idle-time", N_("Last seen:"), format_idle_time },
109 { "x-irc-server", N_("Server:"), format_server },
110 { "x-host", N_("Connected from:"), format_server },
112 /* FIXME: once Idle implements SimplePresence using this information, we can
113 * and should bin this. */
114 { "x-presence-status-message", N_("Away message:"), presence_hack },
125 static InfoParameterData info_parameter_data[] =
127 { "work", N_("work") },
128 { "home", N_("home") },
129 { "cell", N_("mobile") },
130 { "voice", N_("voice") },
131 { "pref", N_("preferred") },
132 { "postal", N_("postal") },
133 { "parcel", N_("parcel") },
138 empathy_contact_info_get_field_names (guint *nnames)
141 *nnames = G_N_ELEMENTS (info_field_names) - 1;
143 return info_field_names;
147 empathy_contact_info_lookup_field (const gchar *field_name,
149 EmpathyContactInfoFormatFunc *format)
153 for (i = 0; info_field_data[i].field_name != NULL; i++)
155 if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
158 *title = gettext (info_field_data[i].title);
161 *format = info_field_data[i].format;
171 build_parameters_string (GStrv parameters)
173 GPtrArray *output = g_ptr_array_new ();
177 for (iter = parameters; iter != NULL && *iter != NULL; iter++)
179 static const char *prefix = "type=";
180 const char *param = *iter;
181 InfoParameterData *iter2;
183 if (!g_str_has_prefix (param, prefix))
186 param += strlen (prefix);
188 for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
190 if (!tp_strdiff (iter2->type, param))
192 g_ptr_array_add (output, gettext (iter2->title));
198 if (output->len == 0)
201 g_ptr_array_add (output, NULL); /* NULL-terminate */
203 join = g_strjoinv (", ", (char **) output->pdata);
204 g_ptr_array_unref (output);
210 empathy_contact_info_field_label (const char *field_name,
212 gboolean show_parameters)
218 if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
222 join = build_parameters_string (parameters);
225 ret = g_strdup_printf ("%s (%s)", title, join);
227 ret = g_strdup_printf ("%s", title);
235 contact_info_field_name_cmp (const gchar *name1,
240 if (tp_strdiff (name1, name2) == FALSE)
243 /* We use the order of info_field_data */
244 for (i = 0; info_field_data[i].field_name != NULL; i++)
246 if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
248 if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
252 return g_strcmp0 (name1, name2);
256 empathy_contact_info_field_cmp (TpContactInfoField *field1,
257 TpContactInfoField *field2)
259 return contact_info_field_name_cmp (field1->field_name, field2->field_name);
263 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
264 TpContactInfoFieldSpec *spec2)
266 return contact_info_field_name_cmp (spec1->name, spec2->name);
270 channel_name_activated_cb (
275 empathy_join_muc (account, uri, empathy_get_current_action_time ());
280 empathy_contact_info_create_channel_list_label (TpAccount *account,
284 GtkWidget *label = NULL;
285 GString *label_markup = g_string_new ("");
290 /* Is there channels? */
291 channels = g_ptr_array_new ();
293 for (l = info; l != NULL; l = l->next)
295 TpContactInfoField *field = l->data;
297 if (!tp_strdiff (field->field_name, "x-irc-channel"))
298 g_ptr_array_add (channels, (gpointer) field->field_value[0]);
301 if (channels->len == 0)
304 for (i = 0; i < channels->len; i++)
306 const gchar *channel_name = g_ptr_array_index (channels, i);
307 /* We abuse the URI of the link to hold the channel name. It seems to
308 * be okay to just use it essentially verbatim, rather than trying to
309 * ensure it's actually a valid URI. */
310 gchar *escaped = g_markup_escape_text (channel_name, -1);
313 g_string_append (label_markup, ", ");
315 g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
320 label = gtk_label_new (NULL);
321 gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
322 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
324 g_signal_connect (label, "activate-link",
325 (GCallback) channel_name_activated_cb, account);
328 g_ptr_array_unref (channels);
329 g_string_free (label_markup, TRUE);