Update Greek translation
[empathy-mirror.git] / libempathy-gtk / empathy-smiley-manager.c
blobd63763d8933da270ef0fe846a95c1baaacd609ea
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Copyright (C) 2007-2008 Collabora Ltd.
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.
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.
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
19 * Authors: Dafydd Harrie <dafydd.harries@collabora.co.uk>
20 * Xavier Claessens <xclaesse@gmail.com>
23 #include "config.h"
24 #include "empathy-smiley-manager.h"
26 #include <tp-account-widgets/tpaw-pixbuf-utils.h>
27 #include <tp-account-widgets/tpaw-utils.h>
29 #include "empathy-ui-utils.h"
30 #include "empathy-utils.h"
32 typedef struct _SmileyManagerTree SmileyManagerTree;
34 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySmileyManager)
35 typedef struct {
36 SmileyManagerTree *tree;
37 GSList *smileys;
38 } EmpathySmileyManagerPriv;
40 struct _SmileyManagerTree {
41 gunichar c;
42 GdkPixbuf *pixbuf;
43 gchar *path;
44 GSList *childrens;
47 G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);
49 static EmpathySmileyManager *manager_singleton = NULL;
51 static SmileyManagerTree *
52 smiley_manager_tree_new (gunichar c)
54 SmileyManagerTree *tree;
56 tree = g_slice_new0 (SmileyManagerTree);
57 tree->c = c;
58 tree->pixbuf = NULL;
59 tree->childrens = NULL;
60 tree->path = NULL;
62 return tree;
65 static void
66 smiley_manager_tree_free (SmileyManagerTree *tree)
68 GSList *l;
70 if (!tree) {
71 return;
74 for (l = tree->childrens; l; l = l->next) {
75 smiley_manager_tree_free (l->data);
78 if (tree->pixbuf) {
79 g_object_unref (tree->pixbuf);
81 g_slist_free (tree->childrens);
82 g_free (tree->path);
83 g_slice_free (SmileyManagerTree, tree);
86 static EmpathySmiley *
87 smiley_new (GdkPixbuf *pixbuf, const gchar *str)
89 EmpathySmiley *smiley;
91 smiley = g_slice_new0 (EmpathySmiley);
92 smiley->pixbuf = g_object_ref (pixbuf);
93 smiley->str = g_strdup (str);
95 return smiley;
98 static void
99 smiley_free (EmpathySmiley *smiley)
101 g_object_unref (smiley->pixbuf);
102 g_free (smiley->str);
103 g_slice_free (EmpathySmiley, smiley);
106 static void
107 smiley_manager_finalize (GObject *object)
109 EmpathySmileyManagerPriv *priv = GET_PRIV (object);
111 smiley_manager_tree_free (priv->tree);
112 g_slist_foreach (priv->smileys, (GFunc) smiley_free, NULL);
113 g_slist_free (priv->smileys);
116 static GObject *
117 smiley_manager_constructor (GType type,
118 guint n_props,
119 GObjectConstructParam *props)
121 GObject *retval;
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);
129 manager_singleton = EMPATHY_SMILEY_MANAGER (retval);
130 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
133 return retval;
136 static void
137 empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
139 GObjectClass *object_class = G_OBJECT_CLASS (klass);
141 object_class->finalize = smiley_manager_finalize;
142 object_class->constructor = smiley_manager_constructor;
144 g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
147 static void
148 empathy_smiley_manager_init (EmpathySmileyManager *manager)
150 EmpathySmileyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
151 EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv);
153 manager->priv = priv;
154 priv->tree = smiley_manager_tree_new ('\0');
155 priv->smileys = NULL;
157 empathy_smiley_manager_load (manager);
160 EmpathySmileyManager *
161 empathy_smiley_manager_dup_singleton (void)
163 return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
166 static SmileyManagerTree *
167 smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
169 GSList *l;
171 for (l = tree->childrens; l; l = l->next) {
172 SmileyManagerTree *child = l->data;
174 if (child->c == c) {
175 return child;
179 return NULL;
182 static SmileyManagerTree *
183 smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
185 SmileyManagerTree *child;
187 child = smiley_manager_tree_find_child (tree, c);
189 if (!child) {
190 child = smiley_manager_tree_new (c);
191 tree->childrens = g_slist_prepend (tree->childrens, child);
194 return child;
197 static void
198 smiley_manager_tree_insert (SmileyManagerTree *tree,
199 GdkPixbuf *pixbuf,
200 const gchar *str,
201 const gchar *path)
203 SmileyManagerTree *child;
205 child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));
207 str = g_utf8_next_char (str);
208 if (*str) {
209 smiley_manager_tree_insert (child, pixbuf, str, path);
210 return;
213 child->pixbuf = g_object_ref (pixbuf);
214 child->path = g_strdup (path);
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)
224 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
225 const gchar *str;
226 EmpathySmiley *smiley;
228 for (str = first_str; str; str = va_arg (var_args, gchar*)) {
229 smiley_manager_tree_insert (priv->tree, pixbuf, str, path);
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);
238 void
239 empathy_smiley_manager_add (EmpathySmileyManager *manager,
240 const gchar *icon_name,
241 const gchar *first_str,
242 ...)
244 GdkPixbuf *pixbuf;
245 va_list var_args;
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));
251 pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
252 if (pixbuf) {
253 gchar *path;
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);
264 void
265 empathy_smiley_manager_load (EmpathySmileyManager *manager)
267 g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
269 /* From fd.o icon-naming spec */
271 /* U+1F47C BABY ANGEL */
272 empathy_smiley_manager_add (manager, "face-angel", "👼", "O:-)", "O:)", NULL);
273 /* U+1F620 ANGRY FACE */
274 empathy_smiley_manager_add (manager, "face-angry", "😠", "X-(", ":@", NULL);
275 /* U+1F60E SMILING FACE WITH SUNGLASSES */
276 empathy_smiley_manager_add (manager, "face-cool", "😎", "B-)", "B-|", NULL);
277 /* U+1F62D LOUDLY CRYING FACE */
278 empathy_smiley_manager_add (manager, "face-crying", "😭", ":'(", NULL);
279 /* U+1F608 SMILING FACE WITH HORNS */
280 empathy_smiley_manager_add (manager, "face-devilish", "😈", ">:-)", ">:)", NULL);
281 /* U+1F633 FLUSHED FACE */
282 empathy_smiley_manager_add (manager, "face-embarrassed","😳", ":-[", ":[", ":-$", ":$", NULL);
283 /* no suitable character in unicode */
284 empathy_smiley_manager_add (manager, "face-glasses", "8-)", NULL);
285 /* U+1F618 FACE THROWING A KISS */
286 empathy_smiley_manager_add (manager, "face-kiss", "😘", ":-*", ":*", NULL);
287 /* U+1F604 SMILING FACE WITH OPEN MOUTH AND SMILING EYES" */
288 empathy_smiley_manager_add (manager, "face-laugh", "😄", ":-))", ":))", NULL);
289 /* U+1F435 MONKEY */
290 empathy_smiley_manager_add (manager, "face-monkey", "🐵", ":-(|)", ":(|)", NULL);
291 /* U+1F610 NEUTRAL FACE */
292 empathy_smiley_manager_add (manager, "face-plain", "😐", ":-|", ":|", NULL);
293 /* U+1F61B FACE WITH STUCK-OUT TONGUE */
294 empathy_smiley_manager_add (manager, "face-raspberry", "😛", ":-P", ":P", ":-p", ":p", NULL);
295 /* U+1F626 FROWING FACE WITH OPEN MOUTH */
296 empathy_smiley_manager_add (manager, "face-sad", "😦", ":-(", ":(", NULL);
297 /* U+1F635 DIZZY FACE */
298 empathy_smiley_manager_add (manager, "face-sick", "😵", ":-&", ":&", NULL);
299 /* U+1F603 SMILING FACE WITH OPEN MOUTH */
300 empathy_smiley_manager_add (manager, "face-smile", "😃", ":-)", ":)", ":]", "=)", NULL);
301 /* U+1F601 GRINNING FACE WITH SMILING EYES */
302 empathy_smiley_manager_add (manager, "face-smile-big", "😁", ":-D", ":D", ":-d", ":d", NULL);
303 /* U+1F60F SMIRKING FACE */
304 empathy_smiley_manager_add (manager, "face-smirk", "😏", ":-!", ":!", NULL);
305 /* U+1F632 ASTONISHED FACE */
306 empathy_smiley_manager_add (manager, "face-surprise", "😲", ":-O", ":O", ":-o", ":o", NULL);
307 /* U+1F62A SLEEPY FACE */
308 empathy_smiley_manager_add (manager, "face-tired", "😪", "|-)", "|)", NULL);
309 /* U+1F615 CONFUSED FACE */
310 empathy_smiley_manager_add (manager, "face-uncertain", "😕", ":-/", ":/", ":-\\", ":\\", NULL);
311 /* U+1F609 WINKING FACE */
312 empathy_smiley_manager_add (manager, "face-wink", "😉", ";-)", ";)", NULL);
313 /* U+1F61F WORRIED FACE */
314 empathy_smiley_manager_add (manager, "face-worried", "😟", ":-S", ":S", ":-s", ":s", NULL);
315 /* U+2764 HEAVY BLACK HEART */
316 empathy_smiley_manager_add (manager, "emblem-favorite", "❤", "<3", NULL);
319 static EmpathySmileyHit *
320 smiley_hit_new (SmileyManagerTree *tree,
321 guint start,
322 guint end)
324 EmpathySmileyHit *hit;
326 hit = g_slice_new (EmpathySmileyHit);
327 hit->pixbuf = tree->pixbuf;
328 hit->path = tree->path;
329 hit->start = start;
330 hit->end = end;
332 return hit;
335 void
336 empathy_smiley_hit_free (EmpathySmileyHit *hit)
338 g_return_if_fail (hit != NULL);
340 g_slice_free (EmpathySmileyHit, hit);
343 GSList *
344 empathy_smiley_manager_parse_len (EmpathySmileyManager *manager,
345 const gchar *text,
346 gssize len)
348 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
349 EmpathySmileyHit *hit;
350 GSList *hits = NULL;
351 SmileyManagerTree *cur_tree = priv->tree;
352 const gchar *cur_str;
353 const gchar *start = NULL;
355 g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
356 g_return_val_if_fail (text != NULL, NULL);
358 /* If len is negative, parse the string until we find '\0' */
359 if (len < 0) {
360 len = G_MAXSSIZE;
363 /* Parse the len first bytes of text to find smileys. Each time a smiley
364 * is detected, append a EmpathySmileyHit struct to the returned list,
365 * containing the smiley pixbuf and the position of the text to be
366 * replaced by it.
367 * cur_str is a pointer in the text showing the current position
368 * of the parsing. It is always at the begining of an UTF-8 character,
369 * because we support unicode smileys! For example we could want to
370 * replace ™ by an image. */
372 for (cur_str = text;
373 *cur_str != '\0' && cur_str - text < len;
374 cur_str = g_utf8_next_char (cur_str)) {
375 SmileyManagerTree *child;
376 gunichar c;
378 c = g_utf8_get_char (cur_str);
379 child = smiley_manager_tree_find_child (cur_tree, c);
381 /* If we have a child it means c is part of a smiley */
382 if (child) {
383 if (cur_tree == priv->tree) {
384 /* c is the first char of some smileys, keep
385 * the begining position */
386 start = cur_str;
388 cur_tree = child;
389 continue;
392 /* c is not part of a smiley. let's check if we found a smiley
393 * before it. */
394 if (cur_tree->pixbuf != NULL) {
395 /* found! */
396 hit = smiley_hit_new (cur_tree, start - text,
397 cur_str - text);
398 hits = g_slist_prepend (hits, hit);
400 /* c was not part of this smiley, check if a new smiley
401 * start with it. */
402 cur_tree = smiley_manager_tree_find_child (priv->tree, c);
403 if (cur_tree) {
404 start = cur_str;
405 } else {
406 cur_tree = priv->tree;
408 } else if (cur_tree != priv->tree) {
409 /* We searched a smiley starting at 'start' but we ended
410 * with no smiley. Look again starting from next char.
412 * For example ">:)" and ":(" are both valid smileys,
413 * when parsing text ">:(" we first see '>' which could
414 * be the start of a smiley. 'start' variable is set to
415 * that position and we parse next char which is ':' and
416 * is still potential smiley. Then we see '(' which is
417 * NOT part of the smiley, ">:(" does not exist, so we
418 * have to start again from ':' to find ":(" which is
419 * correct smiley. */
420 cur_str = start;
421 cur_tree = priv->tree;
425 /* Check if last char of the text was the end of a smiley */
426 if (cur_tree->pixbuf != NULL) {
427 hit = smiley_hit_new (cur_tree, start - text, cur_str - text);
428 hits = g_slist_prepend (hits, hit);
431 return g_slist_reverse (hits);
434 GSList *
435 empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
437 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
439 return priv->smileys;
442 typedef struct {
443 EmpathySmileyManager *manager;
444 EmpathySmiley *smiley;
445 EmpathySmileyMenuFunc func;
446 gpointer user_data;
447 } ActivateData;
449 static void
450 smiley_menu_data_free (gpointer user_data,
451 GClosure *closure)
453 ActivateData *data = (ActivateData *) user_data;
455 g_object_unref (data->manager);
456 g_slice_free (ActivateData, data);
459 static void
460 smiley_menu_activate_cb (GtkMenuItem *menuitem,
461 gpointer user_data)
463 ActivateData *data = (ActivateData *) user_data;
465 data->func (data->manager, data->smiley, data->user_data);
468 GtkWidget *
469 empathy_smiley_menu_new (EmpathySmileyManager *manager,
470 EmpathySmileyMenuFunc func,
471 gpointer user_data)
473 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
474 GSList *l;
475 GtkWidget *menu;
476 gint x = 0;
477 gint y = 0;
479 g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
480 g_return_val_if_fail (func != NULL, NULL);
482 menu = gtk_menu_new ();
484 for (l = priv->smileys; l; l = l->next) {
485 EmpathySmiley *smiley;
486 GtkWidget *item;
487 GtkWidget *image;
488 ActivateData *data;
490 smiley = l->data;
491 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
493 item = gtk_image_menu_item_new ();
494 gtk_style_context_add_class (gtk_widget_get_style_context (item),
495 "empathy-smiley-menu-item");
496 gtk_container_add (GTK_CONTAINER (item), image);
498 gtk_menu_attach (GTK_MENU (menu), item,
499 x, x + 1, y, y + 1);
501 gtk_widget_set_tooltip_text (item, smiley->str);
503 data = g_slice_new (ActivateData);
504 data->manager = g_object_ref (manager);
505 data->smiley = smiley;
506 data->func = func;
507 data->user_data = user_data;
509 g_signal_connect_data (item, "activate",
510 G_CALLBACK (smiley_menu_activate_cb),
511 data,
512 smiley_menu_data_free,
515 if (x > 3) {
516 y++;
517 x = 0;
518 } else {
519 x++;
523 gtk_widget_show_all (menu);
525 return menu;