annotate pidgin/smileyparser.c @ 32575:dcbdee6148e4

save the style path across sessions.
author tdrhq@soc.pidgin.im
date Wed, 14 Oct 2009 02:52:43 +0000
parents fe5b5fc21274
children 8e1ec44ede75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32528
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
1
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
2 #include <gtk/gtk.h>
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
3 #include "smileyparser.h"
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
4 #include <smiley.h>
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
5 #include <string.h>
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
6 #include "gtkthemes.h"
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
7
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
8 static char* get_fullpath (const char* filename)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
9 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
10 if (g_path_is_absolute (filename)) return g_strdup (filename);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
11 else return g_build_path (g_get_current_dir (), filename, NULL);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
12 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
13
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
14 static void
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
15 parse_for_shortcut_plaintext (const char* text, const char* shortcut, const char* file, GString* ret)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
16 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
17 const char *tmp = text;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
18
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
19 for(;*tmp;) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
20 const char *end = strstr (tmp, shortcut);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
21 char *path;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
22 char *escaped_path;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
23
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
24 if (end == NULL) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
25 g_string_append (ret, tmp);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
26 break;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
27 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
28 path = get_fullpath (file);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
29 escaped_path = g_markup_escape_text (path, -1);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
30
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
31 g_string_append_len (ret, tmp, end-tmp);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
32 g_string_append_printf (ret,"<img alt='%s' src='%s' />",
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
33 shortcut, escaped_path);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
34 g_free (path);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
35 g_free (escaped_path);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
36 g_assert (strlen (tmp) >= strlen (shortcut));
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
37 tmp = end + strlen (shortcut);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
38 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
39 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
40
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
41 static char*
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
42 parse_for_shortcut (const char* markup, const char* shortcut, const char* file)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
43 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
44 GString* ret = g_string_new ("");
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
45 char *local_markup = g_strdup (markup);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
46 char *escaped_shortcut = g_markup_escape_text (shortcut, -1);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
47
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
48 char *temp = local_markup;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
49
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
50 for (;*temp;) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
51 char *end = strchr (temp, '<');
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
52 char *end_of_tag;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
53
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
54 if (!end) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
55 parse_for_shortcut_plaintext (temp, escaped_shortcut, file, ret);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
56 break;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
57 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
58
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
59 *end = 0;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
60 parse_for_shortcut_plaintext (temp, escaped_shortcut, file, ret);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
61 *end = '<';
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
62
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
63 /* if this is well-formed, then there should be no '>' within
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
64 * the tag. TODO: handle a comment tag better :( */
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
65 end_of_tag = strchr (end, '>');
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
66 if (!end_of_tag) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
67 g_string_append (ret, end);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
68 break;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
69 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
70
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
71 g_string_append_len (ret, end, end_of_tag-end+1);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
72
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
73 temp = end_of_tag + 1;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
74 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
75 g_free (local_markup);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
76 g_free (escaped_shortcut);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
77 return g_string_free (ret, FALSE);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
78 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
79
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
80 static char*
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
81 parse_for_purple_smiley (const char* markup, PurpleSmiley *smiley)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
82 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
83 char *file = purple_smiley_get_full_path (smiley);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
84 char *ret = parse_for_shortcut (markup, purple_smiley_get_shortcut (smiley), file);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
85 g_free (file);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
86 return ret;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
87 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
88
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
89 static char*
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
90 parse_for_smiley_list (const char* markup, GHashTable* smileys)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
91 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
92 GHashTableIter iter;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
93 char *key, *value;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
94 char *ret = g_strdup (markup);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
95
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
96 g_hash_table_iter_init (&iter, smileys);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
97 while (g_hash_table_iter_next (&iter, (gpointer*)&key, (gpointer*)&value))
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
98 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
99 char* temp = parse_for_shortcut (ret, key, value);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
100 g_free (ret);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
101 ret = temp;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
102 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
103 return ret;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
104 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
105
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
106 char*
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
107 smiley_parse_markup (const char* markup, const char *proto_id)
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
108 {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
109 GList *smileys = purple_smileys_get_all ();
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
110 char *temp = g_strdup (markup), *temp2;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
111 struct smiley_list *list;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
112 const char *proto_name = "default";
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
113
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
114 if (proto_id != NULL) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
115 PurplePlugin *proto;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
116 proto = purple_find_prpl (proto_id);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
117 proto_name = proto->info->name;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
118 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
119
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
120 /* unnecessarily slow, but lets manage for now. */
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
121 for (; smileys; smileys = g_list_next (smileys)) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
122 temp2 = parse_for_purple_smiley (temp, PURPLE_SMILEY (smileys->data));
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
123 g_free (temp);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
124 temp = temp2;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
125 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
126
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
127 /* now for each theme smiley, observe that this does look nasty */
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
128
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
129 if (!current_smiley_theme || !(current_smiley_theme->list)) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
130 printf ("theme does not exist\n");
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
131 return temp;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
132 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
133
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
134 for (list = current_smiley_theme->list; list; list = list->next) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
135 if (g_str_equal (list->sml, proto_name)) {
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
136 temp2 = parse_for_smiley_list (temp, list->files);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
137 g_free (temp);
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
138 temp = temp2;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
139 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
140 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
141
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
142 return temp;
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
143 }
fe5b5fc21274 added smileyparser code.
tdrhq@soc.pidgin.im
parents:
diff changeset
144