display the clock rate of the codec as well
[empathy-mirror.git] / libempathy-gtk / empathy-smiley-manager.c
blobf3447451f1bbdaac6697093443ee62da4f484ff7
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>
25 #include <string.h>
27 #include <libempathy/empathy-utils.h>
28 #include "empathy-smiley-manager.h"
29 #include "empathy-ui-utils.h"
31 typedef struct _SmileyManagerTree SmileyManagerTree;
33 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySmileyManager)
34 typedef struct {
35 SmileyManagerTree *tree;
36 GSList *smileys;
37 } EmpathySmileyManagerPriv;
39 struct _SmileyManagerTree {
40 gunichar c;
41 GdkPixbuf *pixbuf;
42 const gchar *path;
43 GSList *childrens;
46 G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);
48 static EmpathySmileyManager *manager_singleton = NULL;
50 static SmileyManagerTree *
51 smiley_manager_tree_new (gunichar c)
53 SmileyManagerTree *tree;
55 tree = g_slice_new0 (SmileyManagerTree);
56 tree->c = c;
57 tree->pixbuf = NULL;
58 tree->childrens = NULL;
59 tree->path = NULL;
61 return tree;
64 static void
65 smiley_manager_tree_free (SmileyManagerTree *tree)
67 GSList *l;
69 if (!tree) {
70 return;
73 for (l = tree->childrens; l; l = l->next) {
74 smiley_manager_tree_free (l->data);
77 if (tree->pixbuf) {
78 g_object_unref (tree->pixbuf);
80 g_slist_free (tree->childrens);
81 g_slice_free (SmileyManagerTree, tree);
84 static EmpathySmiley *
85 smiley_new (GdkPixbuf *pixbuf, const gchar *str)
87 EmpathySmiley *smiley;
89 smiley = g_slice_new0 (EmpathySmiley);
90 smiley->pixbuf = g_object_ref (pixbuf);
91 smiley->str = g_strdup (str);
93 return smiley;
96 static void
97 smiley_free (EmpathySmiley *smiley)
99 g_object_unref (smiley->pixbuf);
100 g_free (smiley->str);
101 g_slice_free (EmpathySmiley, smiley);
104 static void
105 smiley_manager_finalize (GObject *object)
107 EmpathySmileyManagerPriv *priv = GET_PRIV (object);
109 smiley_manager_tree_free (priv->tree);
110 g_slist_foreach (priv->smileys, (GFunc) smiley_free, NULL);
111 g_slist_free (priv->smileys);
114 static GObject *
115 smiley_manager_constructor (GType type,
116 guint n_props,
117 GObjectConstructParam *props)
119 GObject *retval;
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);
127 manager_singleton = EMPATHY_SMILEY_MANAGER (retval);
128 g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
131 return retval;
134 static void
135 empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
139 object_class->finalize = smiley_manager_finalize;
140 object_class->constructor = smiley_manager_constructor;
142 g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
145 static void
146 empathy_smiley_manager_init (EmpathySmileyManager *manager)
148 EmpathySmileyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
149 EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv);
151 manager->priv = priv;
152 priv->tree = smiley_manager_tree_new ('\0');
153 priv->smileys = NULL;
155 empathy_smiley_manager_load (manager);
158 EmpathySmileyManager *
159 empathy_smiley_manager_dup_singleton (void)
161 return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
164 static SmileyManagerTree *
165 smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
167 GSList *l;
169 for (l = tree->childrens; l; l = l->next) {
170 SmileyManagerTree *child = l->data;
172 if (child->c == c) {
173 return child;
177 return NULL;
180 static SmileyManagerTree *
181 smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
183 SmileyManagerTree *child;
185 child = smiley_manager_tree_find_child (tree, c);
187 if (!child) {
188 child = smiley_manager_tree_new (c);
189 tree->childrens = g_slist_prepend (tree->childrens, child);
192 return child;
195 static void
196 smiley_manager_tree_insert (SmileyManagerTree *tree,
197 GdkPixbuf *pixbuf,
198 const gchar *str,
199 const gchar *path)
201 SmileyManagerTree *child;
203 child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));
205 str = g_utf8_next_char (str);
206 if (*str) {
207 smiley_manager_tree_insert (child, pixbuf, str, path);
208 return;
211 child->pixbuf = g_object_ref (pixbuf);
212 child->path = path;
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)
222 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
223 const gchar *str;
224 EmpathySmiley *smiley;
226 for (str = first_str; str; str = va_arg (var_args, gchar*)) {
227 smiley_manager_tree_insert (priv->tree, pixbuf, str, path);
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);
237 void
238 empathy_smiley_manager_add (EmpathySmileyManager *manager,
239 const gchar *icon_name,
240 const gchar *first_str,
241 ...)
243 GdkPixbuf *pixbuf;
244 va_list var_args;
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));
250 pixbuf = empathy_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
251 if (pixbuf) {
252 gchar *path;
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);
262 void
263 empathy_smiley_manager_load (EmpathySmileyManager *manager)
265 g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
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-)", 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);
291 static EmpathySmileyHit *
292 smiley_hit_new (SmileyManagerTree *tree,
293 guint start,
294 guint end)
296 EmpathySmileyHit *hit;
298 hit = g_slice_new (EmpathySmileyHit);
299 hit->pixbuf = tree->pixbuf;
300 hit->path = tree->path;
301 hit->start = start;
302 hit->end = end;
304 return hit;
307 void
308 empathy_smiley_hit_free (EmpathySmileyHit *hit)
310 g_return_if_fail (hit != NULL);
312 g_slice_free (EmpathySmileyHit, hit);
315 GSList *
316 empathy_smiley_manager_parse_len (EmpathySmileyManager *manager,
317 const gchar *text,
318 gssize len)
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;
327 g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
328 g_return_val_if_fail (text != NULL, NULL);
330 /* If len is negative, parse the string until we find '\0' */
331 if (len < 0) {
332 len = G_MAXSSIZE;
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. */
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;
350 c = g_utf8_get_char (cur_str);
351 child = smiley_manager_tree_find_child (cur_tree, c);
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;
360 cur_tree = child;
361 continue;
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);
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;
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.
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;
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);
403 return g_slist_reverse (hits);
406 GSList *
407 empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
409 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
411 return priv->smileys;
414 typedef struct {
415 EmpathySmileyManager *manager;
416 EmpathySmiley *smiley;
417 EmpathySmileyMenuFunc func;
418 gpointer user_data;
419 } ActivateData;
421 static void
422 smiley_menu_data_free (gpointer user_data,
423 GClosure *closure)
425 ActivateData *data = (ActivateData *) user_data;
427 g_object_unref (data->manager);
428 g_slice_free (ActivateData, data);
431 static void
432 smiley_menu_activate_cb (GtkMenuItem *menuitem,
433 gpointer user_data)
435 ActivateData *data = (ActivateData *) user_data;
437 data->func (data->manager, data->smiley, data->user_data);
440 GtkWidget *
441 empathy_smiley_menu_new (EmpathySmileyManager *manager,
442 EmpathySmileyMenuFunc func,
443 gpointer user_data)
445 EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
446 GSList *l;
447 GtkWidget *menu;
448 gint x = 0;
449 gint y = 0;
451 g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
452 g_return_val_if_fail (func != NULL, NULL);
454 menu = gtk_menu_new ();
456 for (l = priv->smileys; l; l = l->next) {
457 EmpathySmiley *smiley;
458 GtkWidget *item;
459 GtkWidget *image;
460 ActivateData *data;
462 smiley = l->data;
463 image = gtk_image_new_from_pixbuf (smiley->pixbuf);
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);
469 gtk_menu_attach (GTK_MENU (menu), item,
470 x, x + 1, y, y + 1);
472 gtk_widget_set_tooltip_text (item, smiley->str);
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;
480 g_signal_connect_data (item, "activate",
481 G_CALLBACK (smiley_menu_activate_cb),
482 data,
483 smiley_menu_data_free,
486 if (x > 3) {
487 y++;
488 x = 0;
489 } else {
490 x++;
494 gtk_widget_show_all (menu);
496 return menu;