Be more compatible with Facebook emoticon codes
[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-2008 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 #include "empathy-smiley-manager.h"
25
26 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
27 #include <tp-account-widgets/tpaw-utils.h>
28
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
31
32 typedef struct _SmileyManagerTree SmileyManagerTree;
33
34 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySmileyManager)
35 typedef struct {
36         SmileyManagerTree *tree;
37         GSList            *smileys;
38 } EmpathySmileyManagerPriv;
39
40 struct _SmileyManagerTree {
41         gunichar     c;
42         GdkPixbuf   *pixbuf;
43         gchar       *path;
44         GSList      *childrens;
45 };
46
47 G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);
48
49 static EmpathySmileyManager *manager_singleton = NULL;
50
51 static SmileyManagerTree *
52 smiley_manager_tree_new (gunichar c)
53 {
54         SmileyManagerTree *tree;
55
56         tree = g_slice_new0 (SmileyManagerTree);
57         tree->c = c;
58         tree->pixbuf = NULL;
59         tree->childrens = NULL;
60         tree->path = NULL;
61
62         return tree;
63 }
64
65 static void
66 smiley_manager_tree_free (SmileyManagerTree *tree)
67 {
68         GSList *l;
69
70         if (!tree) {
71                 return;
72         }
73
74         for (l = tree->childrens; l; l = l->next) {
75                 smiley_manager_tree_free (l->data);
76         }
77
78         if (tree->pixbuf) {
79                 g_object_unref (tree->pixbuf);
80         }
81         g_slist_free (tree->childrens);
82         g_free (tree->path);
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         smiley->pixbuf = g_object_ref (pixbuf);
93         smiley->str = g_strdup (str);
94
95         return smiley;
96 }
97
98 static void
99 smiley_free (EmpathySmiley *smiley)
100 {
101         g_object_unref (smiley->pixbuf);
102         g_free (smiley->str);
103         g_slice_free (EmpathySmiley, smiley);
104 }
105
106 static void
107 smiley_manager_finalize (GObject *object)
108 {
109         EmpathySmileyManagerPriv *priv = GET_PRIV (object);
110
111         smiley_manager_tree_free (priv->tree);
112         g_slist_foreach (priv->smileys, (GFunc) smiley_free, NULL);
113         g_slist_free (priv->smileys);
114 }
115
116 static GObject *
117 smiley_manager_constructor (GType type,
118                             guint n_props,
119                             GObjectConstructParam *props)
120 {
121         GObject *retval;
122
123         if (manager_singleton) {
124                 retval = g_object_ref (manager_singleton);
125         } else {
126                 retval = G_OBJECT_CLASS (empathy_smiley_manager_parent_class)->constructor
127                         (type, n_props, props);
128
129                 manager_singleton = EMPATHY_SMILEY_MANAGER (retval);
130                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
131         }
132
133         return retval;
134 }
135
136 static void
137 empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
138 {
139         GObjectClass *object_class = G_OBJECT_CLASS (klass);
140
141         object_class->finalize = smiley_manager_finalize;
142         object_class->constructor = smiley_manager_constructor;
143
144         g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
145 }
146
147 static void
148 empathy_smiley_manager_init (EmpathySmileyManager *manager)
149 {
150         EmpathySmileyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
151                 EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv);
152
153         manager->priv = priv;
154         priv->tree = smiley_manager_tree_new ('\0');
155         priv->smileys = NULL;
156
157         empathy_smiley_manager_load (manager);
158 }
159
160 EmpathySmileyManager *
161 empathy_smiley_manager_dup_singleton (void)
162 {
163         return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
164 }
165
166 static SmileyManagerTree *
167 smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
168 {
169         GSList *l;
170
171         for (l = tree->childrens; l; l = l->next) {
172                 SmileyManagerTree *child = l->data;
173
174                 if (child->c == c) {
175                         return child;
176                 }
177         }
178
179         return NULL;
180 }
181
182 static SmileyManagerTree *
183 smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
184 {
185         SmileyManagerTree *child;
186
187         child = smiley_manager_tree_find_child (tree, c);
188
189         if (!child) {
190                 child = smiley_manager_tree_new (c);
191                 tree->childrens = g_slist_prepend (tree->childrens, child);
192         }
193
194         return child;
195 }
196
197 static void
198 smiley_manager_tree_insert (SmileyManagerTree *tree,
199                             GdkPixbuf         *pixbuf,
200                             const gchar       *str,
201                             const gchar       *path)
202 {
203         SmileyManagerTree *child;
204
205         child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));
206
207         str = g_utf8_next_char (str);
208         if (*str) {
209                 smiley_manager_tree_insert (child, pixbuf, str, path);
210                 return;
211         }
212
213         child->pixbuf = g_object_ref (pixbuf);
214         child->path = g_strdup (path);
215 }
216
217 static void
218 smiley_manager_add_valist (EmpathySmileyManager *manager,
219                            GdkPixbuf            *pixbuf,
220                            const gchar          *path,
221                            const gchar          *first_str,
222                            va_list               var_args)
223 {
224         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
225         const gchar              *str;
226         EmpathySmiley            *smiley;
227
228         for (str = first_str; str; str = va_arg (var_args, gchar*)) {
229                 smiley_manager_tree_insert (priv->tree, pixbuf, str, path);
230         }
231
232         g_object_set_data_full (G_OBJECT (pixbuf), "smiley_str",
233                                 g_strdup (first_str), g_free);
234         smiley = smiley_new (pixbuf, first_str);
235         priv->smileys = g_slist_prepend (priv->smileys, smiley);
236 }
237
238 void
239 empathy_smiley_manager_add (EmpathySmileyManager *manager,
240                             const gchar          *icon_name,
241                             const gchar          *first_str,
242                             ...)
243 {
244         GdkPixbuf *pixbuf;
245         va_list    var_args;
246
247         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
248         g_return_if_fail (!TPAW_STR_EMPTY (icon_name));
249         g_return_if_fail (!TPAW_STR_EMPTY (first_str));
250
251         pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
252         if (pixbuf) {
253                 gchar *path;
254
255                 va_start (var_args, first_str);
256                 path = tpaw_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
257                 smiley_manager_add_valist (manager, pixbuf, path, first_str, var_args);
258                 va_end (var_args);
259                 g_object_unref (pixbuf);
260                 g_free (path);
261         }
262 }
263
264 void
265 empathy_smiley_manager_load (EmpathySmileyManager *manager)
266 {
267         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
268
269         /* From fd.o icon-naming spec */
270         empathy_smiley_manager_add (manager, "face-angel",      "O:-)",  "O:)",  NULL);
271         empathy_smiley_manager_add (manager, "face-angry",      "X-(",   ":@",   NULL);
272         empathy_smiley_manager_add (manager, "face-cool",       "B-)",   "B-|",  NULL);
273         empathy_smiley_manager_add (manager, "face-crying",     ":'(",           NULL);
274         empathy_smiley_manager_add (manager, "face-devilish",   ">:-)",  ">:)",  NULL);
275         empathy_smiley_manager_add (manager, "face-embarrassed",":-[",   ":[",   ":-$", ":$", NULL);
276         empathy_smiley_manager_add (manager, "face-glasses",    "8-)",   NULL);
277         empathy_smiley_manager_add (manager, "face-kiss",       ":-*",   ":*",   NULL);
278         empathy_smiley_manager_add (manager, "face-laugh",      ":-))",  ":))",  NULL);
279         empathy_smiley_manager_add (manager, "face-monkey",     ":-(|)", ":(|)", NULL);
280         empathy_smiley_manager_add (manager, "face-plain",      ":-|",   ":|",   NULL);
281         empathy_smiley_manager_add (manager, "face-raspberry",  ":-P",   ":P",   ":-p", ":p", NULL);
282         empathy_smiley_manager_add (manager, "face-sad",        ":-(",   ":(",   NULL);
283         empathy_smiley_manager_add (manager, "face-sick",       ":-&",   ":&",   NULL);
284         empathy_smiley_manager_add (manager, "face-smile",      ":-)",   ":)",   ":]",  "=)", NULL);
285         empathy_smiley_manager_add (manager, "face-smile-big",  ":-D",   ":D",   ":-d", ":d", NULL);
286         empathy_smiley_manager_add (manager, "face-smirk",      ":-!",   ":!",   NULL);
287         empathy_smiley_manager_add (manager, "face-surprise",   ":-O",   ":O",   ":-o", ":o", NULL);
288         empathy_smiley_manager_add (manager, "face-tired",      "|-)",   "|)",   NULL);
289         empathy_smiley_manager_add (manager, "face-uncertain",  ":-/",   ":/",   ":-\\", ":\\", NULL);
290         empathy_smiley_manager_add (manager, "face-wink",       ";-)",   ";)",   NULL);
291         empathy_smiley_manager_add (manager, "face-worried",    ":-S",   ":S",   ":-s", ":s", NULL);
292
293         empathy_smiley_manager_add (manager, "emblem-favorite", "<3", NULL);
294 }
295
296 static EmpathySmileyHit *
297 smiley_hit_new (SmileyManagerTree *tree,
298                 guint              start,
299                 guint              end)
300 {
301         EmpathySmileyHit *hit;
302
303         hit = g_slice_new (EmpathySmileyHit);
304         hit->pixbuf = tree->pixbuf;
305         hit->path = tree->path;
306         hit->start = start;
307         hit->end = end;
308
309         return hit;
310 }
311
312 void
313 empathy_smiley_hit_free (EmpathySmileyHit *hit)
314 {
315         g_return_if_fail (hit != NULL);
316
317         g_slice_free (EmpathySmileyHit, hit);
318 }
319
320 GSList *
321 empathy_smiley_manager_parse_len (EmpathySmileyManager *manager,
322                                   const gchar          *text,
323                                   gssize                len)
324 {
325         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
326         EmpathySmileyHit         *hit;
327         GSList                   *hits = NULL;
328         SmileyManagerTree        *cur_tree = priv->tree;
329         const gchar              *cur_str;
330         const gchar              *start = NULL;
331
332         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
333         g_return_val_if_fail (text != NULL, NULL);
334
335         /* If len is negative, parse the string until we find '\0' */
336         if (len < 0) {
337                 len = G_MAXSSIZE;
338         }
339
340         /* Parse the len first bytes of text to find smileys. Each time a smiley
341          * is detected, append a EmpathySmileyHit struct to the returned list,
342          * containing the smiley pixbuf and the position of the text to be
343          * replaced by it.
344          * cur_str is a pointer in the text showing the current position
345          * of the parsing. It is always at the begining of an UTF-8 character,
346          * because we support unicode smileys! For example we could want to
347          * replace ™ by an image. */
348
349         for (cur_str = text;
350              *cur_str != '\0' && cur_str - text < len;
351              cur_str = g_utf8_next_char (cur_str)) {
352                 SmileyManagerTree *child;
353                 gunichar           c;
354
355                 c = g_utf8_get_char (cur_str);
356                 child = smiley_manager_tree_find_child (cur_tree, c);
357
358                 /* If we have a child it means c is part of a smiley */
359                 if (child) {
360                         if (cur_tree == priv->tree) {
361                                 /* c is the first char of some smileys, keep
362                                  * the begining position */
363                                 start = cur_str;
364                         }
365                         cur_tree = child;
366                         continue;
367                 }
368
369                 /* c is not part of a smiley. let's check if we found a smiley
370                  * before it. */
371                 if (cur_tree->pixbuf != NULL) {
372                         /* found! */
373                         hit = smiley_hit_new (cur_tree, start - text,
374                                               cur_str - text);
375                         hits = g_slist_prepend (hits, hit);
376
377                         /* c was not part of this smiley, check if a new smiley
378                          * start with it. */
379                         cur_tree = smiley_manager_tree_find_child (priv->tree, c);
380                         if (cur_tree) {
381                                 start = cur_str;
382                         } else {
383                                 cur_tree = priv->tree;
384                         }
385                 } else if (cur_tree != priv->tree) {
386                         /* We searched a smiley starting at 'start' but we ended
387                          * with no smiley. Look again starting from next char.
388                          *
389                          * For example ">:)" and ":(" are both valid smileys,
390                          * when parsing text ">:(" we first see '>' which could
391                          * be the start of a smiley. 'start' variable is set to
392                          * that position and we parse next char which is ':' and
393                          * is still potential smiley. Then we see '(' which is
394                          * NOT part of the smiley, ">:(" does not exist, so we
395                          * have to start again from ':' to find ":(" which is
396                          * correct smiley. */
397                         cur_str = start;
398                         cur_tree = priv->tree;
399                 }
400         }
401
402         /* Check if last char of the text was the end of a smiley */
403         if (cur_tree->pixbuf != NULL) {
404                 hit = smiley_hit_new (cur_tree, start - text, cur_str - text);
405                 hits = g_slist_prepend (hits, hit);
406         }
407
408         return g_slist_reverse (hits);
409 }
410
411 GSList *
412 empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
413 {
414         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
415
416         return priv->smileys;
417 }
418
419 typedef struct {
420         EmpathySmileyManager *manager;
421         EmpathySmiley        *smiley;
422         EmpathySmileyMenuFunc func;
423         gpointer              user_data;
424 } ActivateData;
425
426 static void
427 smiley_menu_data_free (gpointer  user_data,
428                        GClosure *closure)
429 {
430         ActivateData *data = (ActivateData *) user_data;
431
432         g_object_unref (data->manager);
433         g_slice_free (ActivateData, data);
434 }
435
436 static void
437 smiley_menu_activate_cb (GtkMenuItem *menuitem,
438                          gpointer     user_data)
439 {
440         ActivateData *data = (ActivateData *) user_data;
441
442         data->func (data->manager, data->smiley, data->user_data);
443 }
444
445 GtkWidget *
446 empathy_smiley_menu_new (EmpathySmileyManager *manager,
447                          EmpathySmileyMenuFunc func,
448                          gpointer              user_data)
449 {
450         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
451         GSList                   *l;
452         GtkWidget                *menu;
453         gint                      x = 0;
454         gint                      y = 0;
455
456         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
457         g_return_val_if_fail (func != NULL, NULL);
458
459         menu = gtk_menu_new ();
460
461         for (l = priv->smileys; l; l = l->next) {
462                 EmpathySmiley *smiley;
463                 GtkWidget     *item;
464                 GtkWidget     *image;
465                 ActivateData  *data;
466
467                 smiley = l->data;
468                 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
469
470                 item = gtk_image_menu_item_new_with_label ("");
471                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
472                 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
473
474                 gtk_menu_attach (GTK_MENU (menu), item,
475                                  x, x + 1, y, y + 1);
476
477                 gtk_widget_set_tooltip_text (item, smiley->str);
478
479                 data = g_slice_new (ActivateData);
480                 data->manager = g_object_ref (manager);
481                 data->smiley = smiley;
482                 data->func = func;
483                 data->user_data = user_data;
484
485                 g_signal_connect_data (item, "activate",
486                                        G_CALLBACK (smiley_menu_activate_cb),
487                                        data,
488                                        smiley_menu_data_free,
489                                        0);
490
491                 if (x > 3) {
492                         y++;
493                         x = 0;
494                 } else {
495                         x++;
496                 }
497         }
498
499         gtk_widget_show_all (menu);
500
501         return menu;
502 }
503