File: empathy-smiley-manager.c

package info (click to toggle)
empathy 3.25.90%2Breally3.12.14-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 39,616 kB
  • sloc: ansic: 88,757; sh: 4,473; python: 3,185; makefile: 1,809; xml: 1,249
file content (527 lines) | stat: -rw-r--r-- 15,222 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007-2008 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Dafydd Harrie <dafydd.harries@collabora.co.uk>
 *          Xavier Claessens <xclaesse@gmail.com>
 */

#include "config.h"
#include "empathy-smiley-manager.h"

#include <tp-account-widgets/tpaw-pixbuf-utils.h>
#include <tp-account-widgets/tpaw-utils.h>

#include "empathy-ui-utils.h"
#include "empathy-utils.h"

typedef struct _SmileyManagerTree SmileyManagerTree;

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathySmileyManager)
typedef struct {
	SmileyManagerTree *tree;
	GSList            *smileys;
} EmpathySmileyManagerPriv;

struct _SmileyManagerTree {
	gunichar     c;
	GdkPixbuf   *pixbuf;
	gchar       *path;
	GSList      *childrens;
};

G_DEFINE_TYPE (EmpathySmileyManager, empathy_smiley_manager, G_TYPE_OBJECT);

static EmpathySmileyManager *manager_singleton = NULL;

static SmileyManagerTree *
smiley_manager_tree_new (gunichar c)
{
	SmileyManagerTree *tree;

	tree = g_slice_new0 (SmileyManagerTree);
	tree->c = c;
	tree->pixbuf = NULL;
	tree->childrens = NULL;
	tree->path = NULL;

	return tree;
}

static void
smiley_manager_tree_free (SmileyManagerTree *tree)
{
	GSList *l;

	if (!tree) {
		return;
	}

	for (l = tree->childrens; l; l = l->next) {
		smiley_manager_tree_free (l->data);
	}

	if (tree->pixbuf) {
		g_object_unref (tree->pixbuf);
	}
	g_slist_free (tree->childrens);
	g_free (tree->path);
	g_slice_free (SmileyManagerTree, tree);
}

static EmpathySmiley *
smiley_new (GdkPixbuf *pixbuf, const gchar *str)
{
	EmpathySmiley *smiley;

	smiley = g_slice_new0 (EmpathySmiley);
	smiley->pixbuf = g_object_ref (pixbuf);
	smiley->str = g_strdup (str);

	return smiley;
}

static void
smiley_free (EmpathySmiley *smiley)
{
	g_object_unref (smiley->pixbuf);
	g_free (smiley->str);
	g_slice_free (EmpathySmiley, smiley);
}

static void
smiley_manager_finalize (GObject *object)
{
	EmpathySmileyManagerPriv *priv = GET_PRIV (object);

	smiley_manager_tree_free (priv->tree);
	g_slist_foreach (priv->smileys, (GFunc) smiley_free, NULL);
	g_slist_free (priv->smileys);
}

static GObject *
smiley_manager_constructor (GType type,
			    guint n_props,
			    GObjectConstructParam *props)
{
	GObject *retval;

	if (manager_singleton) {
		retval = g_object_ref (manager_singleton);
	} else {
		retval = G_OBJECT_CLASS (empathy_smiley_manager_parent_class)->constructor
			(type, n_props, props);

		manager_singleton = EMPATHY_SMILEY_MANAGER (retval);
		g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
	}

	return retval;
}

static void
empathy_smiley_manager_class_init (EmpathySmileyManagerClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = smiley_manager_finalize;
	object_class->constructor = smiley_manager_constructor;

	g_type_class_add_private (object_class, sizeof (EmpathySmileyManagerPriv));
}

static void
empathy_smiley_manager_init (EmpathySmileyManager *manager)
{
	EmpathySmileyManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
		EMPATHY_TYPE_SMILEY_MANAGER, EmpathySmileyManagerPriv);

	manager->priv = priv;
	priv->tree = smiley_manager_tree_new ('\0');
	priv->smileys = NULL;

	empathy_smiley_manager_load (manager);
}

EmpathySmileyManager *
empathy_smiley_manager_dup_singleton (void)
{
	return g_object_new (EMPATHY_TYPE_SMILEY_MANAGER, NULL);
}

static SmileyManagerTree *
smiley_manager_tree_find_child (SmileyManagerTree *tree, gunichar c)
{
	GSList *l;

	for (l = tree->childrens; l; l = l->next) {
		SmileyManagerTree *child = l->data;

		if (child->c == c) {
			return child;
		}
	}

	return NULL;
}

static SmileyManagerTree *
smiley_manager_tree_find_or_insert_child (SmileyManagerTree *tree, gunichar c)
{
	SmileyManagerTree *child;

	child = smiley_manager_tree_find_child (tree, c);

	if (!child) {
		child = smiley_manager_tree_new (c);
		tree->childrens = g_slist_prepend (tree->childrens, child);
	}

	return child;
}

static void
smiley_manager_tree_insert (SmileyManagerTree *tree,
			    GdkPixbuf         *pixbuf,
			    const gchar       *str,
			    const gchar       *path)
{
	SmileyManagerTree *child;

	child = smiley_manager_tree_find_or_insert_child (tree, g_utf8_get_char (str));

	str = g_utf8_next_char (str);
	if (*str) {
		smiley_manager_tree_insert (child, pixbuf, str, path);
		return;
	}

	child->pixbuf = g_object_ref (pixbuf);
	child->path = g_strdup (path);
}

static void
smiley_manager_add_valist (EmpathySmileyManager *manager,
			   GdkPixbuf            *pixbuf,
			   const gchar          *path,
			   const gchar          *first_str,
			   va_list               var_args)
{
	EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
	const gchar              *str;
	EmpathySmiley            *smiley;

	for (str = first_str; str; str = va_arg (var_args, gchar*)) {
		smiley_manager_tree_insert (priv->tree, pixbuf, str, path);
	}

	g_object_set_data_full (G_OBJECT (pixbuf), "smiley_str",
				g_strdup (first_str), g_free);
	smiley = smiley_new (pixbuf, first_str);
	priv->smileys = g_slist_prepend (priv->smileys, smiley);
}

void
empathy_smiley_manager_add (EmpathySmileyManager *manager,
			    const gchar          *icon_name,
			    const gchar          *first_str,
			    ...)
{
	GdkPixbuf *pixbuf;
	va_list    var_args;

	g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));
	g_return_if_fail (!TPAW_STR_EMPTY (icon_name));
	g_return_if_fail (!TPAW_STR_EMPTY (first_str));

	pixbuf = tpaw_pixbuf_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
	if (pixbuf) {
		gchar *path;

		va_start (var_args, first_str);
		path = tpaw_filename_from_icon_name (icon_name, GTK_ICON_SIZE_MENU);
		smiley_manager_add_valist (manager, pixbuf, path, first_str, var_args);
		va_end (var_args);
		g_object_unref (pixbuf);
		g_free (path);
	}
}

void
empathy_smiley_manager_load (EmpathySmileyManager *manager)
{
	g_return_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager));

	/* From fd.o icon-naming spec */

	/* U+1F47C BABY ANGEL */
	empathy_smiley_manager_add (manager, "face-angel",      "👼",    "O:-)",  "O:)",  NULL);
	/* U+1F620 ANGRY FACE */
	empathy_smiley_manager_add (manager, "face-angry",      "😠",    "X-(",   ":@",   NULL);
	/* U+1F60E SMILING FACE WITH SUNGLASSES */
	empathy_smiley_manager_add (manager, "face-cool",       "😎",    "B-)",   "B-|",  NULL);
	/* U+1F62D LOUDLY CRYING FACE */
	empathy_smiley_manager_add (manager, "face-crying",     "😭",    ":'(",           NULL);
	/* U+1F608 SMILING FACE WITH HORNS  */
	empathy_smiley_manager_add (manager, "face-devilish",   "😈",    ">:-)",  ">:)",  NULL);
	/* U+1F633 FLUSHED FACE */
	empathy_smiley_manager_add (manager, "face-embarrassed","😳",    ":-[",   ":[",   ":-$", ":$", NULL);
	/* no suitable character in unicode */
	empathy_smiley_manager_add (manager, "face-glasses",    "8-)",   NULL);
	/* U+1F618 FACE THROWING A KISS */
	empathy_smiley_manager_add (manager, "face-kiss",       "😘",    ":-*",   ":*",   NULL);
	/* U+1F604 SMILING FACE WITH OPEN MOUTH AND SMILING EYES" */
	empathy_smiley_manager_add (manager, "face-laugh",      "😄",    ":-))",  ":))",  NULL);
	/* U+1F435 MONKEY */
	empathy_smiley_manager_add (manager, "face-monkey",     "🐵",    ":-(|)", ":(|)", NULL);
	/* U+1F610 NEUTRAL FACE */
	empathy_smiley_manager_add (manager, "face-plain",      "😐",    ":-|",   ":|",   NULL);
	/* U+1F61B FACE WITH STUCK-OUT TONGUE */
	empathy_smiley_manager_add (manager, "face-raspberry",  "😛",    ":-P",   ":P",	 ":-p", ":p", NULL);
	/* U+1F626 FROWING FACE WITH OPEN MOUTH */
	empathy_smiley_manager_add (manager, "face-sad",        "😦",    ":-(",   ":(",   NULL);
	/* U+1F635 DIZZY FACE */
	empathy_smiley_manager_add (manager, "face-sick",       "😵",    ":-&",   ":&",   NULL);
	/* U+1F603 SMILING FACE WITH OPEN MOUTH */
	empathy_smiley_manager_add (manager, "face-smile",      "😃",    ":-)",   ":)",   ":]",  "=)", NULL);
	/* U+1F601 GRINNING FACE WITH SMILING EYES */
	empathy_smiley_manager_add (manager, "face-smile-big",  "😁",    ":-D",   ":D",   ":-d", ":d", NULL);
	/* U+1F60F SMIRKING FACE */
	empathy_smiley_manager_add (manager, "face-smirk",      "😏",    ":-!",   ":!",   NULL);
	/* U+1F632 ASTONISHED FACE */
	empathy_smiley_manager_add (manager, "face-surprise",   "😲",    ":-O",   ":O",   ":-o", ":o", NULL);
	/* U+1F62A SLEEPY FACE */
	empathy_smiley_manager_add (manager, "face-tired",      "😪",    "|-)",   "|)",   NULL);
	/* U+1F615 CONFUSED FACE */
	empathy_smiley_manager_add (manager, "face-uncertain",  "😕",    ":-/",   ":/",   ":-\\", ":\\", NULL);
	/* U+1F609 WINKING FACE */
	empathy_smiley_manager_add (manager, "face-wink",       "😉",    ";-)",   ";)",   NULL);
	/* U+1F61F WORRIED FACE */
	empathy_smiley_manager_add (manager, "face-worried",    "😟",    ":-S",   ":S",   ":-s", ":s", NULL);
	/* U+2764 HEAVY BLACK HEART */
	empathy_smiley_manager_add (manager, "emblem-favorite", "❤",     "<3", NULL);
}

static EmpathySmileyHit *
smiley_hit_new (SmileyManagerTree *tree,
		guint              start,
		guint              end)
{
	EmpathySmileyHit *hit;

	hit = g_slice_new (EmpathySmileyHit);
	hit->pixbuf = tree->pixbuf;
	hit->path = tree->path;
	hit->start = start;
	hit->end = end;

	return hit;
}

void
empathy_smiley_hit_free (EmpathySmileyHit *hit)
{
	g_return_if_fail (hit != NULL);

	g_slice_free (EmpathySmileyHit, hit);
}

GSList *
empathy_smiley_manager_parse_len (EmpathySmileyManager *manager,
				  const gchar          *text,
				  gssize                len)
{
	EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
	EmpathySmileyHit         *hit;
	GSList                   *hits = NULL;
	SmileyManagerTree        *cur_tree = priv->tree;
	const gchar              *cur_str;
	const gchar              *start = NULL;

	g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
	g_return_val_if_fail (text != NULL, NULL);

	/* If len is negative, parse the string until we find '\0' */
	if (len < 0) {
		len = G_MAXSSIZE;
	}

	/* Parse the len first bytes of text to find smileys. Each time a smiley
	 * is detected, append a EmpathySmileyHit struct to the returned list,
	 * containing the smiley pixbuf and the position of the text to be
	 * replaced by it.
	 * cur_str is a pointer in the text showing the current position
	 * of the parsing. It is always at the begining of an UTF-8 character,
	 * because we support unicode smileys! For example we could want to
	 * replace ™ by an image. */

	for (cur_str = text;
	     *cur_str != '\0' && cur_str - text < len;
	     cur_str = g_utf8_next_char (cur_str)) {
		SmileyManagerTree *child;
		gunichar           c;

		c = g_utf8_get_char (cur_str);
		child = smiley_manager_tree_find_child (cur_tree, c);

		/* If we have a child it means c is part of a smiley */
		if (child) {
			if (cur_tree == priv->tree) {
				/* c is the first char of some smileys, keep
				 * the begining position */
				start = cur_str;
			}
			cur_tree = child;
			continue;
		}

		/* c is not part of a smiley. let's check if we found a smiley
		 * before it. */
		if (cur_tree->pixbuf != NULL) {
			/* found! */
			hit = smiley_hit_new (cur_tree, start - text,
					      cur_str - text);
			hits = g_slist_prepend (hits, hit);

			/* c was not part of this smiley, check if a new smiley
			 * start with it. */
			cur_tree = smiley_manager_tree_find_child (priv->tree, c);
			if (cur_tree) {
				start = cur_str;
			} else {
				cur_tree = priv->tree;
			}
		} else if (cur_tree != priv->tree) {
			/* We searched a smiley starting at 'start' but we ended
			 * with no smiley. Look again starting from next char.
			 *
			 * For example ">:)" and ":(" are both valid smileys,
			 * when parsing text ">:(" we first see '>' which could
			 * be the start of a smiley. 'start' variable is set to
			 * that position and we parse next char which is ':' and
			 * is still potential smiley. Then we see '(' which is
			 * NOT part of the smiley, ">:(" does not exist, so we
			 * have to start again from ':' to find ":(" which is
			 * correct smiley. */
			cur_str = start;
			cur_tree = priv->tree;
		}
	}

	/* Check if last char of the text was the end of a smiley */
	if (cur_tree->pixbuf != NULL) {
		hit = smiley_hit_new (cur_tree, start - text, cur_str - text);
		hits = g_slist_prepend (hits, hit);
	}

	return g_slist_reverse (hits);
}

GSList *
empathy_smiley_manager_get_all (EmpathySmileyManager *manager)
{
	EmpathySmileyManagerPriv *priv = GET_PRIV (manager);

	return priv->smileys;
}

typedef struct {
	EmpathySmileyManager *manager;
	EmpathySmiley        *smiley;
	EmpathySmileyMenuFunc func;
	gpointer              user_data;
} ActivateData;

static void
smiley_menu_data_free (gpointer  user_data,
		       GClosure *closure)
{
	ActivateData *data = (ActivateData *) user_data;

	g_object_unref (data->manager);
	g_slice_free (ActivateData, data);
}

static void
smiley_menu_activate_cb (GtkMenuItem *menuitem,
			 gpointer     user_data)
{
	ActivateData *data = (ActivateData *) user_data;

	data->func (data->manager, data->smiley, data->user_data);
}

GtkWidget *
empathy_smiley_menu_new (EmpathySmileyManager *manager,
			 EmpathySmileyMenuFunc func,
			 gpointer              user_data)
{
	EmpathySmileyManagerPriv *priv = GET_PRIV (manager);
	GSList                   *l;
	GtkWidget                *menu;
	gint                      x = 0;
	gint                      y = 0;

	g_return_val_if_fail (EMPATHY_IS_SMILEY_MANAGER (manager), NULL);
	g_return_val_if_fail (func != NULL, NULL);

	menu = gtk_menu_new ();

	for (l = priv->smileys; l; l = l->next) {
		EmpathySmiley *smiley;
		GtkWidget     *item;
		GtkWidget     *image;
		ActivateData  *data;

		smiley = l->data;
		image = gtk_image_new_from_pixbuf (smiley->pixbuf);

		item = gtk_image_menu_item_new ();
		gtk_style_context_add_class (gtk_widget_get_style_context (item),
			"empathy-smiley-menu-item");
		gtk_container_add (GTK_CONTAINER (item), image);

		gtk_menu_attach (GTK_MENU (menu), item,
				 x, x + 1, y, y + 1);

		gtk_widget_set_tooltip_text (item, smiley->str);

		data = g_slice_new (ActivateData);
		data->manager = g_object_ref (manager);
		data->smiley = smiley;
		data->func = func;
		data->user_data = user_data;

		g_signal_connect_data (item, "activate",
				       G_CALLBACK (smiley_menu_activate_cb),
				       data,
				       smiley_menu_data_free,
				       0);

		if (x > 3) {
			y++;
			x = 0;
		} else {
			x++;
		}
	}

	gtk_widget_show_all (menu);

	return menu;
}