libpurple/smiley-parser.c

Wed, 02 Apr 2014 02:37:34 +0200

author
Tomasz Wasilczyk <twasilczyk@pidgin.im>
date
Wed, 02 Apr 2014 02:37:34 +0200
changeset 35717
45bde03f86a6
parent 35716
80bedd712883
child 35739
cb9acfd6fa1e
permissions
-rw-r--r--

Custom smileys: simplify storage implementation

35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
1 /* purple
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
2 *
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
3 * Purple is the legal property of its developers, whose names are too numerous
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
4 * to list here. Please refer to the COPYRIGHT file distributed with this
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
5 * source distribution.
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
6 *
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
10 * (at your option) any later version.
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
11 *
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
15 * GNU General Public License for more details.
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
16 *
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
20 */
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
21
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
22 #include "smiley-parser.h"
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
23
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
24 #include "smiley-custom.h"
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
25 #include "smiley-theme.h"
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
26
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
27 static gboolean escape_checked = FALSE;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
28 static gboolean escape_value;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
29
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
30 gboolean
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
31 purple_smiley_parse_escape(void)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
32 {
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
33 GHashTable *ui_info;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
34
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
35 if (escape_checked)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
36 return escape_value;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
37
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
38 ui_info = purple_core_get_ui_info();
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
39 if (!ui_info)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
40 escape_value = FALSE;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
41 else {
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
42 escape_value = GPOINTER_TO_INT(g_hash_table_lookup(ui_info,
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
43 "smiley-parser-escape"));
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
44 }
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
45
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
46 escape_checked = TRUE;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
47 return escape_value;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
48 }
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
49
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
50 static gboolean purple_smiley_parse_cb(GString *out, const gchar *word,
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
51 gpointer _smiley, gpointer _unused)
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
52 {
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
53 PurpleSmiley *smiley = _smiley;
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
54
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
55 g_string_append_printf(out, "<img alt=\"%s\" src=\"%s\" />",
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
56 word, purple_smiley_get_path(smiley));
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
57
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
58 return TRUE;
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
59 }
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
60
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
61 gchar *
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
62 purple_smiley_parse(const gchar *message, gpointer ui_data)
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
63 {
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
64 PurpleSmileyTheme *theme;
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
65 PurpleSmileyList *theme_smileys = NULL;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
66 PurpleTrie *theme_trie = NULL, *custom_trie;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
67 GSList *tries = NULL, tries_theme, tries_custom;
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
68
35713
4423f463a782 Smileys in PidginWebviewToolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35709
diff changeset
69 if (message == NULL || message[0] == '\0')
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
70 return g_strdup(message);
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
71
35717
45bde03f86a6 Custom smileys: simplify storage implementation
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35716
diff changeset
72 custom_trie = purple_smiley_list_get_trie(
45bde03f86a6 Custom smileys: simplify storage implementation
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35716
diff changeset
73 purple_smiley_custom_get_list());
45bde03f86a6 Custom smileys: simplify storage implementation
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35716
diff changeset
74 if (purple_trie_get_size(custom_trie) == 0)
45bde03f86a6 Custom smileys: simplify storage implementation
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35716
diff changeset
75 custom_trie = NULL;
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
76
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
77 theme = purple_smiley_theme_get_current();
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
78 if (theme != NULL)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
79 theme_smileys = purple_smiley_theme_get_smileys(theme, ui_data);
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
80
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
81 if (theme_smileys != NULL)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
82 theme_trie = purple_smiley_list_get_trie(theme_smileys);
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
83
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
84 if (theme_trie == NULL && custom_trie == NULL)
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
85 return g_strdup(message);
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
86
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
87 /* Create a tries list on stack. */
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
88 tries_theme.data = theme_trie;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
89 tries_custom.data = custom_trie;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
90 tries_theme.next = tries_custom.next = NULL;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
91 if (custom_trie != NULL)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
92 tries = &tries_custom;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
93 if (theme_trie != NULL) {
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
94 if (tries)
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
95 tries->next = &tries_theme;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
96 else
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
97 tries = &tries_theme;
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
98 }
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
99
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
100 /* XXX: should we parse custom smileys,
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
101 * if protocol doesn't support it? */
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
102
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
103 /* TODO: don't replace text within tags, ie. <span style=":)"> */
35716
80bedd712883 Custom smileys: parse and display in toolbar
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents: 35713
diff changeset
104 return purple_trie_multi_replace(tries, message,
35709
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
105 purple_smiley_parse_cb, NULL);
0d7a84931572 Smileys: implement and use new parser, only themes for now
Tomasz Wasilczyk <twasilczyk@pidgin.im>
parents:
diff changeset
106 }

mercurial