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