]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-contactinfo-utils.c
string-parser: move everything except for _match_smiley(), to tp-aw
[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 #include <tp-account-widgets/tpaw-time.h>
28 #include <tp-account-widgets/tpaw-string-parser.h>
29
30 #include "empathy-ui-utils.h"
31
32 static gchar *
33 linkify_first_value (GStrv values)
34 {
35   return tpaw_add_link_markup (values[0]);
36 }
37
38 static gchar *
39 format_idle_time (GStrv values)
40 {
41   const gchar *value = values[0];
42   int duration = strtol (value, NULL, 10);
43
44   if (duration <= 0)
45     return NULL;
46
47   return tpaw_duration_to_string (duration);
48 }
49
50 static gchar *
51 format_server (GStrv values)
52 {
53   g_assert (values[0] != NULL);
54
55   if (values[1] == NULL)
56     return g_markup_escape_text (values[0], -1);
57   else
58     return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
59 }
60
61 static gchar *
62 presence_hack (GStrv values)
63 {
64   if (tp_str_empty (values[0]))
65     return NULL;
66
67   return g_markup_escape_text (values[0], -1);
68 }
69
70 typedef struct
71 {
72   const gchar *field_name;
73   const gchar *title;
74   EmpathyContactInfoFormatFunc format;
75 } InfoFieldData;
76
77 /* keep this syncronised with info_field_data below */
78 static const char *info_field_names[] =
79 {
80   "fn",
81   "tel",
82   "email",
83   "url",
84   "bday",
85
86   "x-idle-time",
87   "x-irc-server",
88   "x-host",
89
90   "x-presence-status-message",
91
92   NULL
93 };
94
95 static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
96 {
97   { "fn",    N_("Full name"),      NULL },
98   { "tel",   N_("Phone number"),   NULL },
99   { "email", N_("E-mail address"), linkify_first_value },
100   { "url",   N_("Website"),        linkify_first_value },
101   { "bday",  N_("Birthday"),       NULL },
102
103   /* Note to translators: this is the caption for a string of the form "5
104    * minutes ago", and refers to the time since the contact last interacted
105    * with their IM client. */
106   { "x-idle-time",  N_("Last seen:"),      format_idle_time },
107   { "x-irc-server", N_("Server:"),         format_server },
108   { "x-host",       N_("Connected from:"), format_server },
109
110   /* FIXME: once Idle implements SimplePresence using this information, we can
111    * and should bin this. */
112   { "x-presence-status-message", N_("Away message:"), presence_hack },
113
114   { NULL, NULL }
115 };
116
117 typedef struct
118 {
119   const gchar *type;
120   const gchar *title;
121 } InfoParameterData;
122
123 static InfoParameterData info_parameter_data[] =
124 {
125   { "work", N_("work") },
126   { "home", N_("home") },
127   { "cell", N_("mobile") },
128   { "voice", N_("voice") },
129   { "pref", N_("preferred") },
130   { "postal", N_("postal") },
131   { "parcel", N_("parcel") },
132   { NULL, NULL }
133 };
134
135 const char **
136 empathy_contact_info_get_field_names (guint *nnames)
137 {
138   if (nnames != NULL)
139     *nnames = G_N_ELEMENTS (info_field_names) - 1;
140
141   return info_field_names;
142 }
143
144 gboolean
145 empathy_contact_info_lookup_field (const gchar *field_name,
146     const gchar **title,
147     EmpathyContactInfoFormatFunc *format)
148 {
149   guint i;
150
151   for (i = 0; info_field_data[i].field_name != NULL; i++)
152     {
153       if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
154         {
155           if (title != NULL)
156             *title = gettext (info_field_data[i].title);
157
158           if (format != NULL)
159             *format = info_field_data[i].format;
160
161           return TRUE;
162         }
163     }
164
165   return FALSE;
166 }
167
168 static char *
169 build_parameters_string (GStrv parameters)
170 {
171   GPtrArray *output = g_ptr_array_new ();
172   char *join;
173   GStrv iter;
174
175   for (iter = parameters; iter != NULL && *iter != NULL; iter++)
176     {
177       static const char *prefix = "type=";
178       const char *param = *iter;
179       InfoParameterData *iter2;
180
181       if (!g_str_has_prefix (param, prefix))
182         continue;
183
184       param += strlen (prefix);
185
186       for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
187         {
188           if (!tp_strdiff (iter2->type, param))
189             {
190               g_ptr_array_add (output, gettext (iter2->title));
191               break;
192             }
193         }
194     }
195
196   if (output->len == 0)
197     return NULL;
198
199   g_ptr_array_add (output, NULL); /* NULL-terminate */
200
201   join = g_strjoinv (", ", (char **) output->pdata);
202   g_ptr_array_unref (output);
203
204   return join;
205 }
206
207 char *
208 empathy_contact_info_field_label (const char *field_name,
209     GStrv parameters,
210     gboolean show_parameters)
211 {
212   char *ret;
213   const char *title;
214   char *join = NULL;
215
216   if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
217     return NULL;
218
219   if (show_parameters)
220     join = build_parameters_string (parameters);
221
222   if (join != NULL)
223     ret = g_strdup_printf ("%s (%s)", title, join);
224   else
225     ret = g_strdup_printf ("%s", title);
226
227   g_free (join);
228
229   return ret;
230 }
231
232 static gint
233 contact_info_field_name_cmp (const gchar *name1,
234     const gchar *name2)
235 {
236   guint i;
237
238   if (tp_strdiff (name1, name2) == FALSE)
239     return 0;
240
241   /* We use the order of info_field_data */
242   for (i = 0; info_field_data[i].field_name != NULL; i++)
243     {
244       if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
245         return -1;
246       if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
247         return +1;
248     }
249
250   return g_strcmp0 (name1, name2);
251 }
252
253 gint
254 empathy_contact_info_field_cmp (TpContactInfoField *field1,
255     TpContactInfoField *field2)
256 {
257   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
258 }
259
260 gint
261 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
262     TpContactInfoFieldSpec *spec2)
263 {
264     return contact_info_field_name_cmp (spec1->name, spec2->name);
265 }
266