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