]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contactinfo-utils.c
fcf06194330a536ecf1ce8929f2aed27fe2df844
[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
25 #include <glib/gi18n-lib.h>
26
27 #include "libempathy/empathy-time.h"
28 #include "libempathy/empathy-request-util.h"
29
30 #include "empathy-contactinfo-utils.h"
31 #include "empathy-string-parser.h"
32 #include "empathy-ui-utils.h"
33
34 static gchar *
35 linkify_first_value (GStrv values)
36 {
37   return empathy_add_link_markup (values[0]);
38 }
39
40 static gchar *
41 format_idle_time (GStrv values)
42 {
43   const gchar *value = values[0];
44   int duration = strtol (value, NULL, 10);
45
46   if (duration <= 0)
47     return NULL;
48
49   return empathy_duration_to_string (duration);
50 }
51
52 static gchar *
53 format_server (GStrv values)
54 {
55   g_assert (values[0] != NULL);
56
57   if (values[1] == NULL)
58     return g_markup_escape_text (values[0], -1);
59   else
60     return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
61 }
62
63 static gchar *
64 presence_hack (GStrv values)
65 {
66   if (tp_str_empty (values[0]))
67     return NULL;
68
69   return g_markup_escape_text (values[0], -1);
70 }
71
72 typedef struct
73 {
74   const gchar *field_name;
75   const gchar *title;
76   EmpathyContactInfoFormatFunc format;
77 } InfoFieldData;
78
79 /* keep this syncronised with info_field_data below */
80 static const char *info_field_names[] =
81 {
82   "fn",
83   "tel",
84   "email",
85   "url",
86   "bday",
87
88   "x-idle-time",
89   "x-irc-server",
90   "x-host",
91
92   "x-presence-status-message",
93
94   NULL
95 };
96
97 static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
98 {
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 },
104
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 },
111
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 },
115
116   { NULL, NULL }
117 };
118
119 typedef struct
120 {
121   const gchar *type;
122   const gchar *title;
123 } InfoParameterData;
124
125 static InfoParameterData info_parameter_data[] =
126 {
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") },
134   { NULL, NULL }
135 };
136
137 const char **
138 empathy_contact_info_get_field_names (guint *nnames)
139 {
140   if (nnames != NULL)
141     *nnames = G_N_ELEMENTS (info_field_names) - 1;
142
143   return info_field_names;
144 }
145
146 gboolean
147 empathy_contact_info_lookup_field (const gchar *field_name,
148     const gchar **title,
149     EmpathyContactInfoFormatFunc *format)
150 {
151   guint i;
152
153   for (i = 0; info_field_data[i].field_name != NULL; i++)
154     {
155       if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
156         {
157           if (title != NULL)
158             *title = gettext (info_field_data[i].title);
159
160           if (format != NULL)
161             *format = info_field_data[i].format;
162
163           return TRUE;
164         }
165     }
166
167   return FALSE;
168 }
169
170 static char *
171 build_parameters_string (GStrv parameters)
172 {
173   GPtrArray *output = g_ptr_array_new ();
174   char *join;
175   GStrv iter;
176
177   for (iter = parameters; iter != NULL && *iter != NULL; iter++)
178     {
179       static const char *prefix = "type=";
180       const char *param = *iter;
181       InfoParameterData *iter2;
182
183       if (!g_str_has_prefix (param, prefix))
184         continue;
185
186       param += strlen (prefix);
187
188       for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
189         {
190           if (!tp_strdiff (iter2->type, param))
191             {
192               g_ptr_array_add (output, gettext (iter2->title));
193               break;
194             }
195         }
196     }
197
198   if (output->len == 0)
199     return NULL;
200
201   g_ptr_array_add (output, NULL); /* NULL-terminate */
202
203   join = g_strjoinv (", ", (char **) output->pdata);
204   g_ptr_array_unref (output);
205
206   return join;
207 }
208
209 char *
210 empathy_contact_info_field_label (const char *field_name,
211     GStrv parameters,
212     gboolean show_parameters)
213 {
214   char *ret;
215   const char *title;
216   char *join = NULL;
217
218   if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
219     return NULL;
220
221   if (show_parameters)
222     join = build_parameters_string (parameters);
223
224   if (join != NULL)
225     ret = g_strdup_printf ("%s (%s)", title, join);
226   else
227     ret = g_strdup_printf ("%s", title);
228
229   g_free (join);
230
231   return ret;
232 }
233
234 static gint
235 contact_info_field_name_cmp (const gchar *name1,
236     const gchar *name2)
237 {
238   guint i;
239
240   if (tp_strdiff (name1, name2) == FALSE)
241     return 0;
242
243   /* We use the order of info_field_data */
244   for (i = 0; info_field_data[i].field_name != NULL; i++)
245     {
246       if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
247         return -1;
248       if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
249         return +1;
250     }
251
252   return g_strcmp0 (name1, name2);
253 }
254
255 gint
256 empathy_contact_info_field_cmp (TpContactInfoField *field1,
257     TpContactInfoField *field2)
258 {
259   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
260 }
261
262 gint
263 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
264     TpContactInfoFieldSpec *spec2)
265 {
266     return contact_info_field_name_cmp (spec1->name, spec2->name);
267 }
268
269 static gboolean
270 channel_name_activated_cb (
271     GtkLabel *label,
272     gchar *uri,
273     TpAccount *account)
274 {
275   empathy_join_muc (account, uri, empathy_get_current_action_time ());
276   return TRUE;
277 }
278
279 GtkWidget *
280 empathy_contact_info_create_channel_list_label (TpAccount *account,
281     GList *info,
282     guint row)
283 {
284   GtkWidget *label = NULL;
285   GString *label_markup = g_string_new ("");
286   guint i;
287   GPtrArray *channels;
288   GList *l;
289
290   /* Is there channels? */
291   channels = g_ptr_array_new ();
292
293   for (l = info; l != NULL; l = l->next)
294     {
295       TpContactInfoField *field = l->data;
296
297       if (!tp_strdiff (field->field_name, "x-irc-channel"))
298         g_ptr_array_add (channels, (gpointer) field->field_value[0]);
299     }
300
301   if (channels->len == 0)
302     goto out;
303
304   for (i = 0; i < channels->len; i++)
305     {
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);
311
312       if (i > 0)
313         g_string_append (label_markup, ", ");
314
315       g_string_append_printf (label_markup, "<a href='%s'>%s</a>",
316           escaped, escaped);
317       g_free (escaped);
318     }
319
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);
323
324   g_signal_connect (label, "activate-link",
325       (GCallback) channel_name_activated_cb, account);
326
327 out:
328   g_ptr_array_unref (channels);
329   g_string_free (label_markup, TRUE);
330
331   return label;
332 }