]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-smiley-manager.c
Introduce a new smiley parser that can parse only a part of a string.
[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
25 #include <string.h>
26
27 #include <libempathy/empathy-utils.h>
28 #include "empathy-smiley-manager.h"
29 #include "empathy-ui-utils.h"
30
31 typedef struct _SmileyManagerTree SmileyManagerTree;
32
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySmileyManager)
34 typedef struct {
35         SmileyManagerTree *tree;
36         GSList            *smileys;
37 } EmpathySmileyManagerPriv;
38
39 struct _SmileyManagerTree {
40         gunichar     c;
41         GdkPixbuf   *pixbuf;
42         const gchar *path;
43         GSList      *childrens;
44 };
45
46 G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);
47
48 static EmpathySmileyManager *manager_singleton = NULL;
49
50 static SmileyManagerTree *
51 smiley_manager_tree_new (gunichar c)
52 {
53         SmileyManagerTree *tree;
54
55         tree = g_slice_new0 (SmileyManagerTree);
56         tree->c = c;
57         tree->pixbuf = NULL;
58         tree->childrens = NULL;
59         tree->path = NULL;
60
61         return tree;
62 }
63
64 static void
65 smiley_manager_tree_free (SmileyManagerTree *tree)
66 {
67         GSList *l;
68
69         if (!tree) {
70                 return;
71         }
72
73         for (l = tree->childrens; l; l = l->next) {
74                 smiley_manager_tree_free (l->data);
75         }
76
77         if (tree->pixbuf) {
78                 g_object_unref (tree->pixbuf);
79         }
80         g_slist_free (tree->childrens);
81         g_slice_free (SmileyManagerTree, tree);
82 }
83
84 /* Note: This function takes the ownership of str */
85 static EmpathySmiley *
86 smiley_new (GdkPixbuf *pixbuf, gchar *str, const gchar *path)
87 {
88         EmpathySmiley *smiley;
89
90         smiley = g_slice_new0 (EmpathySmiley);
91         if (pixbuf) {
92                 smiley->pixbuf = g_object_ref (pixbuf);
93         }
94         smiley->str = str;
95         smiley->path = path;
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         GSList                   *l;
119
120         smiley_manager_tree_free (priv->tree);
121         for (l = priv->smileys; l; l = l->next) {
122                 EmpathySmiley *smiley = l->data;
123
124                 /* The smiley got the ownership of the path */
125                 g_free ((gchar *) smiley->path);
126                 empathy_smiley_free (smiley);
127         }
128         g_slist_free (priv->smileys);
129 }
130
131 static GObject *
132 smiley_manager_constructor (GType type,
133                             guint n_props,
134                             GObjectConstructParam *props)
135 {
136         GObject *retval;
137
138         if (manager_singleton) {
139                 retval = g_object_ref (manager_singleton);
140         } else {
141                 retval = G_OBJECT_CLASS (empathy_smiley_manager_parent_class)->constructor
142                         (type, n_props, props);
143
144                 manager_singleton = EMPATHY_SMILEY_MANAGER (retval);
145                 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
146         }
147
148         return retval;
149 }
150
151 static void
152 empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
153 {
154         GObjectClass *object_class = G_OBJECT_CLASS (klass);
155
156         object_class->finalize = smiley_manager_finalize;
157         object_class->constructor = smiley_manager_constructor;
158
159         g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
160 }
161
162 static void
163 empathy_smiley_manager_init (EmpathySmileyManager *manager)
164 {
165         EmpathySmileyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
166                 EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv);
167
168         manager->priv = priv;
169         priv->tree = smiley_manager_tree_new ('\0');
170         priv->smileys = NULL;
171
172         empathy_smiley_manager_load (manager);
173 }
174
175 EmpathySmileyManager *
176 empathy_smiley_manager_dup_singleton (void)
177 {
178         return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
179 }
180
181 static SmileyManagerTree *
182 smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
183 {
184         GSList *l;
185
186         for (l = tree->childrens; l; l = l->next) {
187                 SmileyManagerTree *child = l->data;
188
189                 if (child->c == c) {
190                         return child;
191                 }
192         }
193
194         return NULL;
195 }
196
197 static SmileyManagerTree *
198 smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
199 {
200         SmileyManagerTree *child;
201
202         child = smiley_manager_tree_find_child (tree, c);
203
204         if (!child) {
205                 child = smiley_manager_tree_new (c);
206                 tree->childrens = g_slist_prepend (tree->childrens, child);
207         }
208
209         return child;
210 }
211
212 static void
213 smiley_manager_tree_insert (SmileyManagerTree *tree,
214                             GdkPixbuf         *pixbuf,
215                             const gchar       *str,
216                             const gchar       *path)
217 {
218         SmileyManagerTree *child;
219
220         child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));
221
222         str = g_utf8_next_char (str);
223         if (*str) {
224                 smiley_manager_tree_insert (child, pixbuf, str, path);
225                 return;
226         }
227
228         child->pixbuf = g_object_ref (pixbuf);
229         child->path = path;
230 }
231
232 static void
233 smiley_manager_add_valist (EmpathySmileyManager *manager,
234                            GdkPixbuf            *pixbuf,
235                            gchar                *path,
236                            const gchar          *first_str,
237                            va_list               var_args)
238 {
239         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
240         const gchar              *str;
241         EmpathySmiley            *smiley;
242
243         for (str = first_str; str; str = va_arg (var_args, gchar*)) {
244                 smiley_manager_tree_insert (priv->tree, pixbuf, str, path);
245         }
246
247         /* We give the ownership of path to the smiley */
248         g_object_set_data_full (G_OBJECT (pixbuf), "smiley_str",
249                                 g_strdup (first_str), g_free);
250         smiley = smiley_new (pixbuf, g_strdup (first_str), path);
251         priv->smileys = g_slist_prepend (priv->smileys, smiley);
252 }
253
254 void
255 empathy_smiley_manager_add (EmpathySmileyManager *manager,
256                             const gchar          *icon_name,
257                             const gchar          *first_str,
258                             ...)
259 {
260         GdkPixbuf *pixbuf;
261         va_list    var_args;
262
263         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
264         g_return_if_fail (!EMP_STR_EMPTY (icon_name));
265         g_return_if_fail (!EMP_STR_EMPTY (first_str));
266
267         pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
268         if (pixbuf) {
269                 gchar *path;
270
271                 va_start (var_args, first_str);
272                 path = empathy_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
273                 smiley_manager_add_valist (manager, pixbuf, path, first_str, var_args);
274                 va_end (var_args);
275                 g_object_unref (pixbuf);
276         }
277 }
278
279 void
280 empathy_smiley_manager_load (EmpathySmileyManager *manager)
281 {
282         g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
283
284         /* From fd.o icon-naming spec */
285         empathy_smiley_manager_add (manager, "face-angel",      "O:-)",  "O:)",  NULL);
286         empathy_smiley_manager_add (manager, "face-angry",      "X-(",   ":@",   NULL);
287         empathy_smiley_manager_add (manager, "face-cool",       "B-)",   "B)",   NULL);
288         empathy_smiley_manager_add (manager, "face-crying",     ":'(",           NULL);
289         empathy_smiley_manager_add (manager, "face-devilish",   ">:-)",  ">:)",  NULL);
290         empathy_smiley_manager_add (manager, "face-embarrassed",":-[",   ":[",   ":-$", ":$", NULL);
291         empathy_smiley_manager_add (manager, "face-kiss",       ":-*",   ":*",   NULL);
292         empathy_smiley_manager_add (manager, "face-laugh",      ":-))",  ":))",  NULL);
293         empathy_smiley_manager_add (manager, "face-monkey",     ":-(|)", ":(|)", NULL);
294         empathy_smiley_manager_add (manager, "face-plain",      ":-|",   ":|",   NULL);
295         empathy_smiley_manager_add (manager, "face-raspberry",  ":-P",   ":P",   ":-p", ":p", NULL);
296         empathy_smiley_manager_add (manager, "face-sad",        ":-(",   ":(",   NULL);
297         empathy_smiley_manager_add (manager, "face-sick",       ":-&",   ":&",   NULL);
298         empathy_smiley_manager_add (manager, "face-smile",      ":-)",   ":)",   NULL);
299         empathy_smiley_manager_add (manager, "face-smile-big",  ":-D",   ":D",   ":-d", ":d", NULL);
300         empathy_smiley_manager_add (manager, "face-smirk",      ":-!",   ":!",   NULL);
301         empathy_smiley_manager_add (manager, "face-surprise",   ":-O",   ":O",   ":-o", ":o", NULL);
302         empathy_smiley_manager_add (manager, "face-tired",      "|-)",   "|)",   NULL);
303         empathy_smiley_manager_add (manager, "face-uncertain",  ":-/",   ":/",   NULL);
304         empathy_smiley_manager_add (manager, "face-wink",       ";-)",   ";)",   NULL);
305         empathy_smiley_manager_add (manager, "face-worried",    ":-S",   ":S",   ":-s", ":s", NULL);
306 }
307
308 GSList *
309 empathy_smiley_manager_parse (EmpathySmileyManager *manager,
310                               const gchar          *text)
311 {
312         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
313         EmpathySmiley            *smiley;
314         SmileyManagerTree        *cur_tree = priv->tree;
315         const gchar              *t;
316         const gchar              *cur_str = text;
317         GSList                   *smileys = NULL;
318
319         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
320         g_return_val_if_fail (text != NULL, NULL);
321
322         for (t = text; *t; t = g_utf8_next_char (t)) {
323                 SmileyManagerTree *child;
324                 gunichar           c;
325
326                 c = g_utf8_get_char (t);
327                 child = smiley_manager_tree_find_child (cur_tree, c);
328
329                 if (cur_tree == priv->tree) {
330                         if (child) {
331                                 if (t > cur_str) {
332                                         smiley = smiley_new (NULL,
333                                                              g_strndup (cur_str, t - cur_str),
334                                                              NULL);
335                                         smileys = g_slist_prepend (smileys, smiley);
336                                 }
337                                 cur_str = t;
338                                 cur_tree = child;
339                         }
340
341                         continue;
342                 }
343
344                 if (child) {
345                         cur_tree = child;
346                         continue;
347                 }
348
349                 smiley = smiley_new (cur_tree->pixbuf,
350                                      g_strndup (cur_str, t - cur_str),
351                                      cur_tree->path);
352                 smileys = g_slist_prepend (smileys, smiley);
353                 if (cur_tree->pixbuf) {
354                         cur_str = t;
355                         cur_tree = smiley_manager_tree_find_child (priv->tree, c);
356
357                         if (!cur_tree) {
358                                 cur_tree = priv->tree;
359                         }
360                 } else {
361                         cur_str = t;
362                         cur_tree = priv->tree;
363                 }
364         }
365
366         smiley = smiley_new (cur_tree->pixbuf,
367                              g_strndup (cur_str, t - cur_str),
368                              cur_tree->path);
369         smileys = g_slist_prepend (smileys, smiley);
370
371         return g_slist_reverse (smileys);
372 }
373
374 static EmpathySmileyHit *
375 smiley_hit_new (SmileyManagerTree *tree,
376                 guint              start,
377                 guint              end)
378 {
379         EmpathySmileyHit *hit;
380
381         hit = g_slice_new (EmpathySmileyHit);
382         hit->pixbuf = tree->pixbuf;
383         hit->path = tree->path;
384         hit->start = start;
385         hit->end = end;
386
387         return hit;
388 }
389
390 void
391 empathy_smiley_hit_free (EmpathySmileyHit *hit)
392 {
393         g_slice_free (EmpathySmileyHit, hit);
394 }
395
396 GSList *
397 empathy_smiley_manager_parse_len (EmpathySmileyManager *manager,
398                                   const gchar          *text,
399                                   gssize                len)
400 {
401         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
402         EmpathySmileyHit         *hit;
403         GSList                   *hits = NULL;
404         SmileyManagerTree        *cur_tree = priv->tree;
405         const gchar              *cur_str;
406         const gchar              *start = NULL;
407
408         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
409         g_return_val_if_fail (text != NULL, NULL);
410
411         if (len < 0) {
412                 len = G_MAXSSIZE;
413         }
414
415         for (cur_str = text;
416              *cur_str && cur_str - text < len;
417              cur_str = g_utf8_next_char (cur_str)) {
418                 SmileyManagerTree *child;
419                 gunichar           c;
420
421                 c = g_utf8_get_char (cur_str);
422                 child = smiley_manager_tree_find_child (cur_tree, c);
423
424                 if (child) {
425                         if (cur_tree == priv->tree) {
426                                 start = cur_str;
427                         }
428                         cur_tree = child;
429                         continue;
430                 }
431
432                 if (cur_tree->pixbuf != NULL) {
433                         hit = smiley_hit_new (cur_tree, start - text,
434                                               cur_str - text);
435                         hits = g_slist_prepend (hits, hit);
436
437                         cur_tree = smiley_manager_tree_find_child (priv->tree, c);
438                         if (cur_tree) {
439                                 start = cur_str;
440                         } else {
441                                 cur_tree = priv->tree;
442                         }
443                 } else if (cur_tree != priv->tree) {
444                         cur_str = start;
445                         cur_tree = priv->tree;
446                 }
447         }
448
449         if (cur_tree->pixbuf != NULL) {
450                 hit = smiley_hit_new (cur_tree, start - text, cur_str - text);
451                 hits = g_slist_prepend (hits, hit);
452         }
453
454         return g_slist_reverse (hits);
455 }
456
457 GSList *
458 empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
459 {
460         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
461
462         return priv->smileys;
463 }
464
465 typedef struct {
466         EmpathySmileyManager *manager;
467         EmpathySmiley        *smiley;
468         EmpathySmileyMenuFunc func;
469         gpointer              user_data;
470 } ActivateData;
471
472 static void
473 smiley_menu_data_free (gpointer  user_data,
474                        GClosure *closure)
475 {
476         ActivateData *data = (ActivateData *) user_data;
477
478         g_object_unref (data->manager);
479         g_slice_free (ActivateData, data);
480 }
481
482 static void
483 smiley_menu_activate_cb (GtkMenuItem *menuitem,
484                          gpointer     user_data)
485 {
486         ActivateData *data = (ActivateData *) user_data;
487
488         data->func (data->manager, data->smiley, data->user_data);
489 }
490
491 GtkWidget *
492 empathy_smiley_menu_new (EmpathySmileyManager *manager,
493                          EmpathySmileyMenuFunc func,
494                          gpointer              user_data)
495 {
496         EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
497         GSList                   *l;
498         GtkWidget                *menu;
499         gint                      x = 0;
500         gint                      y = 0;
501
502         g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
503         g_return_val_if_fail (func != NULL, NULL);
504
505         menu = gtk_menu_new ();
506
507         for (l = priv->smileys; l; l = l->next) {
508                 EmpathySmiley *smiley;
509                 GtkWidget     *item;
510                 GtkWidget     *image;
511                 ActivateData  *data;
512
513                 smiley = l->data;
514                 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
515
516                 item = gtk_image_menu_item_new_with_label ("");
517                 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
518                 gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
519
520                 gtk_menu_attach (GTK_MENU (menu), item,
521                                  x, x + 1, y, y + 1);
522
523                 gtk_widget_set_tooltip_text (item, smiley->str);
524
525                 data = g_slice_new (ActivateData);
526                 data->manager = g_object_ref (manager);
527                 data->smiley = smiley;
528                 data->func = func;
529                 data->user_data = user_data;
530
531                 g_signal_connect_data (item, "activate",
532                                        G_CALLBACK (smiley_menu_activate_cb),
533                                        data,
534                                        smiley_menu_data_free,
535                                        0);
536
537                 if (x > 3) {
538                         y++;
539                         x = 0;
540                 } else {
541                         x++;
542                 }
543         }
544
545         gtk_widget_show_all (menu);
546
547         return menu;
548 }
549