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