]> git.0d.be Git - empathy.git/blob - libempathy/empathy-contactinfo-utils.c
Abstract common contactinfo code into empathy-contactinfo-utils
[empathy.git] / libempathy / 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
27 #include <glib/gi18n-lib.h>
28
29 #include <telepathy-glib/util.h>
30
31 #include "empathy-contactinfo-utils.h"
32
33 typedef struct
34 {
35   const gchar *field_name;
36   const gchar *title;
37   gboolean linkify;
38 } InfoFieldData;
39
40 static InfoFieldData info_field_data[] =
41 {
42   { "fn",    N_("Full name"),      FALSE },
43   { "tel",   N_("Phone number"),   FALSE },
44   { "email", N_("E-mail address"), TRUE },
45   { "url",   N_("Website"),        TRUE },
46   { "bday",  N_("Birthday"),       FALSE },
47   { NULL, NULL }
48 };
49
50 typedef struct
51 {
52   const gchar *type;
53   const gchar *title;
54 } InfoParameterData;
55
56 static InfoParameterData info_parameter_data[] =
57 {
58   { "work", N_("work") },
59   { "home", N_("home") },
60   { "cell", N_("mobile") },
61   { "voice", N_("voice") },
62   { "pref", N_("preferred") },
63   { "postal", N_("postal") },
64   { "parcel", N_("parcel") },
65   { NULL, NULL }
66 };
67
68 gboolean
69 empathy_contact_info_lookup_field (const gchar *field_name,
70     const gchar **title,
71     gboolean *linkify)
72 {
73   guint i;
74
75   for (i = 0; info_field_data[i].field_name != NULL; i++)
76     {
77       if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
78         {
79           if (title != NULL)
80             *title = gettext (info_field_data[i].title);
81
82           if (linkify != NULL)
83             *linkify = info_field_data[i].linkify;
84
85           return TRUE;
86         }
87     }
88
89   return FALSE;
90 }
91
92 static char *
93 build_parameters_string (GStrv parameters)
94 {
95   GPtrArray *output = g_ptr_array_new ();
96   char *join;
97   GStrv iter;
98
99   for (iter = parameters; iter != NULL && *iter != NULL; iter++)
100     {
101       static const char *prefix = "type=";
102       const char *param = *iter;
103       InfoParameterData *iter2;
104
105       if (!g_str_has_prefix (param, prefix))
106         continue;
107
108       param += strlen (prefix);
109
110       for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
111         {
112           if (!tp_strdiff (iter2->type, param))
113             {
114               g_ptr_array_add (output, gettext (iter2->title));
115               break;
116             }
117         }
118     }
119
120   if (output->len == 0)
121     return NULL;
122
123   g_ptr_array_add (output, NULL); /* NULL-terminate */
124
125   join = g_strjoinv (", ", (char **) output->pdata);
126   g_ptr_array_free (output, TRUE);
127
128   return join;
129 }
130
131 char *
132 empathy_contact_info_field_label (const char *field_name,
133     GStrv parameters)
134 {
135   char *ret;
136   const char *title;
137   char *join = build_parameters_string (parameters);
138
139   if (!empathy_contact_info_lookup_field (field_name, &title, NULL))
140     return NULL;
141
142   if (join != NULL)
143     ret = g_strdup_printf ("%s (%s):", title, join);
144   else
145     ret = g_strdup_printf ("%s:", title);
146
147   g_free (join);
148
149   return ret;
150 }
151
152 static gint
153 contact_info_field_name_cmp (const gchar *name1,
154     const gchar *name2)
155 {
156   guint i;
157
158   if (tp_strdiff (name1, name2) == FALSE)
159     return 0;
160
161   /* We use the order of info_field_data */
162   for (i = 0; info_field_data[i].field_name != NULL; i++)
163     {
164       if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
165         return -1;
166       if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
167         return +1;
168     }
169
170   return g_strcmp0 (name1, name2);
171 }
172
173 gint
174 empathy_contact_info_field_cmp (TpContactInfoField *field1,
175     TpContactInfoField *field2)
176 {
177   return contact_info_field_name_cmp (field1->field_name, field2->field_name);
178 }
179
180 gint
181 empathy_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
182     TpContactInfoFieldSpec *spec2)
183 {
184     return contact_info_field_name_cmp (spec1->name, spec2->name);
185 }