]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contactinfo-utils.c
Merge branch 'gnome-3-8'
[empathy.git] / libempathy-gtk / empathy-contactinfo-utils.c
1 /*
2  * Copyright (C) 2007-2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Xavier Claessens <xclaesse@gmail.com>
19  *          Philip Withnall <philip.withnall@collabora.co.uk>
20  *          Danielle Madeley <danielle.madeley@collabora.co.uk>
21  */
22
23 #include "config.h"
24 #include "empathy-contactinfo-utils.h"
25
26 #include <glib/gi18n-lib.h>
27
28 #include "empathy-request-util.h"
29 #include "empathy-string-parser.h"
30 #include "empathy-time.h"
31 #include "empathy-ui-utils.h"
32
33 static gchar *
34 linkify_first_value (GStrv values)
35 {
36   return empathy_add_link_markup (values[0]);
37 }
38
39 static gchar *
40 format_idle_time (GStrv values)
41 {
42   const gchar *value = values[0];
43   int duration = strtol (value, NULL, 10);
44
45   if (duration <= 0)
46     return NULL;
47
48   return empathy_duration_to_string (duration);
49 }
50
51 static gchar *
52 format_server (GStrv values)
53 {
54   g_assert (values[0] != NULL);
55
56   if (values[1] == NULL)
57     return g_markup_escape_text (values[0], -1);
58   else
59     return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
60 }
61
62 static gchar *
63 presence_hack (GStrv values)
64 {
65   if (tp_str_empty (values[0]))
66     return NULL;
67
68   return g_markup_escape_text (values[0], -1);
69 }
70
71 typedef struct
72 {
73   const gchar *field_name;
74   const gchar *title;
75   EmpathyContactInfoFormatFunc format;
76 } InfoFieldData;
77
78 /* keep this syncronised with info_field_data below */
79 static const char *info_field_names[] =
80 {
81   "fn",
82   "tel",
83   "email",
84   "url",
85   "bday",
86
87   "x-idle-time",
88   "x-irc-server",
89   "x-host",
90
91   "x-presence-status-message",
92
93   NULL
94 };
95
96 static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
97 {
98   { "fn",    N_("Full name"),      NULL },
99   { "tel",   N_("Phone number"),   NULL },
100   { "email", N_("E-mail address"), linkify_first_value },
101   { "url",   N_("Website"),        linkify_first_value },
102   { "bday",  N_("Birthday"),       NULL },
103
104   /* Note to translators: this is the caption for a string of the form "5
105    * minutes ago", and refers to the time since the contact last interacted
106    * with their IM client. */
107   { "x-idle-time",  N_("Last seen:"),      format_idle_time },
108   { "x-irc-server", N_("Server:"),         format_server },
109   { "x-host",       N_("Connected from:"), format_server },
110
111   /* FIXME: once Idle implements SimplePresence using this information, we can
112    * and should bin this. */
113   { "x-presence-status-message", N_("Away message:"), presence_hack },
114
115   { NULL, NULL }
116 };
117
118 typedef struct
119 {
120   const gchar *type;
121   const gchar *title;
122 } InfoParameterData;
123
124 static InfoParameterData info_parameter_data[] =
125 {
126   { "work", N_("work") },
127   { "home", N_("home") },
128   { "cell", N_("mobile") },
129   { "voice", N_("voice") },
130   { "pref", N_("preferred") },
131   { "postal", N_("postal") },
132   { "parcel", N_("parcel") },
133   { NULL, NULL }
134 };
135
136 const char **
137 empathy_contact_info_get_field_names (guint *nnames)
138 {
139   if (nnames != NULL)
140     *nnames = G_N_ELEMENTS (info_field_names) - 1;
141
142   return info_field_names;
143 }
144
145 gboolean
146 empathy_contact_info_lookup_field (const gchar *field_name,
147     const gchar **title,
148     EmpathyContactInfoFormatFunc *format)
149 {
150   guint i;
151
152   for (i = 0; info_field_data[i].field_name != NULL; i++)
153     {
154       if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
155         {
156           if (title != NULL)
157             *title = gettext (info_field_data[i].title);
158
159           if (format != NULL)
160             *format = info_field_data[i].format;
161
162           return TRUE;
163         }
164     }
165
166   return FALSE;
167 }
168
169 static char *
170 build_parameters_string (GStrv parameters)
171 {
172   GPtrArray *output = g_ptr_array_new ();
173   char *join;
174   GStrv iter;
175
176   for (iter = parameters; iter != NULL && *iter != NULL; iter++)
177     {
178       static const char *prefix = "type=";
179       const char *param = *iter;
180       InfoParameterData *iter2;
181
182       if (!g_str_has_prefix (param, prefix))
183         continue;
184
185       param += strlen (prefix);
186
187       for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
188         {
189           if (!tp_strdiff (iter2->type, param))
190             {
191               g_ptr_array_add (output, gettext (iter2->title));
192               break;
193             }
194         }
195     }
196
197   if (output->len == 0)
198     return NULL;
199
200   g_ptr_array_add (output, NULL); /* NULL-terminate */
201
202   join = g_strjoinv (", ", (char **) output->pdata);
203   g_ptr_array_unref (output);
204
205   return join;
206 }
207
208 char *
209 empathy_contact_info_field_label (const char *field_name,
210     GStrv parameters,
211     gboolean show_parameters)
212 {
213   char *ret;
214   const char *title;
215   char *join = NULL;
216
217   if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
218     return NULL;
219
220   if (show_parameters)
221     join = build_parameters_string (parameters);
222
223   if (join != NULL)
224     ret = g_strdup_printf ("%s (%s)", title, join);
225   else
226     ret = g_strdup_printf ("%s", title);
227
228   g_free (join);
229
230   return ret;
231 }
232
233 static gint
234 contact_info_field_name_cmp (const gchar *name1,
235     const gchar *name2)
236 {
237   guint i;
238
239   if (tp_strdiff (name1, name2) == FALSE)
240     return 0;
241
242   /* We use the order of info_field_data */
243   for (i = 0; info_field_data[i].field_name != NULL; i++)
244     {
245       if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
246         return -1;
247       if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
248         return +1;
249     }
250
251   return g_strcmp0 (name1, name2);
252 }
253
254 gint
255 empathy_contact_info_field_cmp (TpContactInfoField *field1,
256     TpContactInfoField *field2)
257 {
258   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
259 }
260
261 gint
262 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
263     TpContactInfoFieldSpec *spec2)
264 {
265     return contact_info_field_name_cmp (spec1->name, spec2->name);
266 }
267
268 static gboolean
269 channel_name_activated_cb (
270     GtkLabel *label,
271     gchar *uri,
272     TpAccount *account)
273 {
274   empathy_join_muc (account, uri, empathy_get_current_action_time ());
275   return TRUE;
276 }
277
278 GtkWidget *
279 empathy_contact_info_create_channel_list_label (TpAccount *account,
280     GList *info,
281     guint row)
282 {
283   GtkWidget *label = NULL;
284   GString *label_markup = g_string_new ("");
285   guint i;
286   GPtrArray *channels;
287   GList *l;
288
289   /* Is there channels? */
290   channels = g_ptr_array_new ();
291
292   for (l = info; l != NULL; l = l->next)
293     {
294       TpContactInfoField *field = l->data;
295
296       if (!tp_strdiff (field->field_name, "x-irc-channel"))
297         g_ptr_array_add (channels, (gpointer) field->field_value[0]);
298     }
299
300   if (channels->len == 0)
301     goto out;
302
303   for (i = 0; i < channels->len; i++)
304     {
305       const gchar *channel_name = g_ptr_array_index (channels, i);
306       /* We abuse the URI of the link to hold the channel name. It seems to
307        * be okay to just use it essentially verbatim, rather than trying to
308        * ensure it's actually a valid URI. */
309       gchar *escaped = g_markup_escape_text (channel_name, -1);
310
311       if (i > 0)
312         g_string_append (label_markup, ", ");
313
314       g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
315           escaped, escaped);
316       g_free (escaped);
317     }
318
319   label = gtk_label_new (NULL);
320   gtk_label_set_markup (GTK_LABEL (label), label_markup->str);
321   gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
322
323   g_signal_connect (label, "activate-link",
324       (GCallback) channel_name_activated_cb, account);
325
326 out:
327   g_ptr_array_unref (channels);
328   g_string_free (label_markup, TRUE);
329
330   return label;
331 }