8b1e9e18de74de6c0b141cdb29b07685abca8bfb
[empathy.git] / libempathy-gtk / empathy-smiley-manager.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  * 
19  * Authors: Dafydd Harrie <dafydd.harries@collabora.co.uk>
20  *          Xavier Claessens <xclaesse@gmail.com>
21  */
22
23 #include <config.h>
24
25 #include <string.h>
26
27 #include <libempathy/empathy-debug.h>
28
29 #include "empathy-smiley-manager.h"
30 #include "empathy-ui-utils.h"
31
32 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
33                        EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv))
34
35 #define DEBUG_DOMAIN "SmileyManager"
36
37 typedef struct {
38         gunichar   c;
39         GdkPixbuf *pixbuf;
40         GSList    *childrens;
41 } SmileyManagerTree;
42
43 struct _EmpathySmileyManagerPriv {
44         SmileyManagerTree *tree;
45         GSList            *smileys;
46 };
47
48 static void empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass);
49 static void empathy_smiley_manager_init       (EmpathySmileyManager      *manager);
50
51 G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);
52
53 static SmileyManagerTree *
54 smiley_manager_tree_new (gunichar c)
55 {
56         SmileyManagerTree *tree;
57
58         tree = g_slice_new0 (SmileyManagerTree);
59         tree->c = c;
60         tree->pixbuf = NULL;
61         tree->childrens = NULL;
62
63         return tree;
64 }
65
66 static void
67 smiley_manager_tree_free (SmileyManagerTree *tree)
68 {
69         GSList *l;
70
71         if (!tree) {
72                 return;
73         }
74
75         for (l = tree->childrens; l; l = l->next) {
76                 smiley_manager_tree_free (l->data);
77         }
78
79         if (tree->pixbuf) {
80                 g_object_unref (tree->pixbuf);
81         }
82         g_slist_free (tree->childrens);
83         g_slice_free (SmileyManagerTree, tree);
84 }
85
86 static EmpathySmiley *
87 smiley_new (GdkPixbuf *pixbuf, const gchar *str)
88 {
89         EmpathySmiley *smiley;
90
91         smiley = g_slice_new0 (EmpathySmiley);
92         if (pixbuf) {
93                 smiley->pixbuf = g_object_ref (pixbuf);
94         }
95         smiley->str = g_strdup (str);
96
97         return smiley;
98 }
99
100 void
101 empathy_smiley_free (EmpathySmiley *smiley)
102 {
103         if (!smiley) {
104                 return;
105         }
106
107         if (smiley->pixbuf) {
108                 g_object_unref (smiley->pixbuf);
109         }
110         g_free (smiley->str);
111         g_slice_free (EmpathySmiley, smiley);
112 }
113
114 static void
115 smiley_manager_finalize (GObject *object)
116 {
117         EmpathySmileyManagerPriv *priv = GET_PRIV (object);
118
119         smiley_manager_tree_free (priv->tree);
120         g_slist_foreach (priv->smileys, (GFunc) empathy_smiley_free, NULL);
121         g_slist_free (priv->smileys);
122 }
123
124 static void
125 empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
126 {
127         GObjectClass *object_class = G_OBJECT_CLASS (klass);
128
129         object_class->finalize = smiley_manager_finalize;
130
131         g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
132 }
133
134 static void
135 empathy_smiley_manager_init (EmpathySmileyManager *manager)
136 {
137         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
138
139         priv->tree = smiley_manager_tree_new ('\0');
140         priv->smileys = NULL;
141 }
142
143 EmpathySmileyManager *
144 empathy_smiley_manager_new (void)
145 {
146         static EmpathySmileyManager *manager = NULL;
147
148         if (!manager) {
149                 manager = g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
150                 g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
151                 empathy_smiley_manager_load (manager);
152         } else {
153                 g_object_ref (manager);
154         }
155
156         return manager;
157 }
158
159 static SmileyManagerTree *
160 smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
161 {
162         GSList *l;
163
164         for (l = tree->childrens; l; l = l->next) {
165                 SmileyManagerTree *child = l->data;
166
167                 if (child->c == c) {
168                         return child;
169                 }
170         }
171
172         return NULL;
173 }
174
175 static SmileyManagerTree *
176 smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
177 {
178         SmileyManagerTree *child;
179
180         child = smiley_manager_tree_find_child (tree, c);
181
182         if (!child) {
183                 child = smiley_manager_tree_new (c);
184                 tree->childrens = g_slist_prepend (tree->childrens, child);
185         }
186
187         return child;
188 }
189
190 static void
191 smiley_manager_tree_insert (SmileyManagerTree *tree,
192                             GdkPixbuf         *smiley,
193                             const gchar       *str)
194 {
195         SmileyManagerTree *child;
196
197         child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));
198
199         str = g_utf8_next_char (str);
200         if (*str) {
201                 smiley_manager_tree_insert (child, smiley, str);
202                 return;
203         }
204
205         child->pixbuf = g_object_ref (smiley);
206 }
207
208 static void
209 smiley_manager_add_valist (EmpathySmileyManager *manager,
210                            GdkPixbuf            *smiley,
211                            const gchar          *first_str,
212                            va_list               var_args)
213 {
214         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
215         const gchar              *str;
216
217         for (str = first_str; str; str = va_arg (var_args, gchar*)) {
218                 smiley_manager_tree_insert (priv->tree, smiley, str);
219         }
220
221         priv->smileys = g_slist_prepend (priv->smileys, smiley_new (smiley, first_str));
222 }
223
224 void
225 empathy_smiley_manager_add (EmpathySmileyManager *manager,
226                             const gchar          *icon_name,
227                             const gchar          *first_str,
228                             ...)
229 {
230         GdkPixbuf *smiley;
231         va_list    var_args;
232
233         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
234         g_return_if_fail (!G_STR_EMPTY (icon_name));
235         g_return_if_fail (!G_STR_EMPTY (first_str));
236
237         smiley = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
238         if (smiley) {
239                 va_start (var_args, first_str);
240                 smiley_manager_add_valist (manager, smiley, first_str, var_args);
241                 va_end (var_args);
242                 g_object_unref (smiley);
243         }
244 }
245
246 void
247 empathy_smiley_manager_add_from_pixbuf (EmpathySmileyManager *manager,
248                                         GdkPixbuf            *smiley,
249                                         const gchar          *first_str,
250                                         ...)
251 {
252         va_list var_args;
253
254         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
255         g_return_if_fail (GDK_IS_PIXBUF (smiley));
256         g_return_if_fail (!G_STR_EMPTY (first_str));
257
258         va_start (var_args, first_str);
259         smiley_manager_add_valist (manager, smiley, first_str, var_args);
260         va_end (var_args);
261 }
262
263 void
264 empathy_smiley_manager_load (EmpathySmileyManager *manager)
265 {
266         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
267
268         /* From fd.o icon-naming spec */
269         empathy_smiley_manager_add (manager, "face-angel",      "0:-)",  "0:)",  NULL);
270         empathy_smiley_manager_add (manager, "face-cool",       "B-)",   "B)",   NULL);
271         empathy_smiley_manager_add (manager, "face-crying",     ":'(", NULL);
272         empathy_smiley_manager_add (manager, "face-devilish",   ">:-)",  ">:)",  NULL);
273         empathy_smiley_manager_add (manager, "face-embarrassed",":-[",   ":[",   ":-$", ":$", NULL);
274         empathy_smiley_manager_add (manager, "face-kiss",       ":-*",   ":*",   NULL);
275         empathy_smiley_manager_add (manager, "face-monkey",     ":-(|)", ":(|)", NULL);
276         empathy_smiley_manager_add (manager, "face-plain",      ":-|",   ":|",   NULL);
277         empathy_smiley_manager_add (manager, "face-raspberry",  ":-P",   ":P",   ":-p", ":p", NULL);
278         empathy_smiley_manager_add (manager, "face-sad",        ":-(",   ":(",   NULL);
279         empathy_smiley_manager_add (manager, "face-smile",      ":-)",   ":)",   NULL);
280         empathy_smiley_manager_add (manager, "face-smile-big",  ":-D",   ":D",   ":-d", ":d", NULL);
281         empathy_smiley_manager_add (manager, "face-smirk",      ":-!",   ":!",   NULL);
282         empathy_smiley_manager_add (manager, "face-surprise",   ":-0",   ":0",   NULL);
283         empathy_smiley_manager_add (manager, "face-wink",       ";-)",   ";)",   NULL);
284 }
285
286 GSList *
287 empathy_smiley_manager_parse (EmpathySmileyManager *manager,
288                               const gchar          *text)
289 {
290         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
291         EmpathySmiley            *smiley;
292         SmileyManagerTree        *cur_tree = priv->tree;
293         const gchar              *t;
294         const gchar              *cur_str = text;
295         GSList                   *smileys = NULL;
296
297         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
298         g_return_val_if_fail (text != NULL, NULL);
299
300         for (t = text; *t; t = g_utf8_next_char (t)) {
301                 SmileyManagerTree *child;
302                 gunichar           c;
303                 
304                 c = g_utf8_get_char (t);
305                 child = smiley_manager_tree_find_child (cur_tree, c);
306
307                 if (cur_tree == priv->tree) {
308                         if (child) {
309                                 if (t > cur_str) {
310                                         smiley = smiley_new (NULL, g_strndup (cur_str, t - cur_str));
311                                         smileys = g_slist_prepend (smileys, smiley);
312                                 }
313                                 cur_str = t;
314                                 cur_tree = child;
315                         }
316
317                         continue;
318                 }
319
320                 if (child) {
321                         cur_tree = child;
322                         continue;
323                 }
324
325                 smiley = smiley_new (cur_tree->pixbuf, g_strndup (cur_str, t - cur_str));
326                 smileys = g_slist_prepend (smileys, smiley);
327                 if (cur_tree->pixbuf) {
328                         cur_str = t;
329                         cur_tree = smiley_manager_tree_find_child (priv->tree, c);
330
331                         if (!cur_tree) {
332                                 cur_tree = priv->tree;
333                         }
334                 } else {
335                         cur_str = t;
336                         cur_tree = priv->tree;
337                 }
338         }
339
340         smiley = smiley_new (cur_tree->pixbuf, g_strndup (cur_str, t - cur_str));
341         smileys = g_slist_prepend (smileys, smiley);
342
343         return g_slist_reverse (smileys);
344 }
345
346 GSList *
347 empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
348 {
349         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
350
351         return priv->smileys;
352 }
353