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