comparison libpurple/protocols/jabber/google/google.c @ 31449:1c660ba17ba1

propagate from branch 'im.pidgin.pidgin' (head 6f879669a6513a5c40335bbaefe842389a92b39e) to branch 'im.pidgin.cpw.qulogic.cairo' (head 4ab0199887f74442673fd46dcbb662bc7f070bc4)
author Marcus Lundblad <ml@update.uu.se>
date Sun, 21 Nov 2010 20:30:12 +0000
parents 34f586bffe4e
children a8cc50c2279f
comparison
equal deleted inserted replaced
31448:9a705087d64e 31449:1c660ba17ba1
1 /**
2 * Purple is the legal property of its developers, whose names are too numerous
3 * to list here. Please refer to the COPYRIGHT file distributed with this
4 * source distribution.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
19 */
20
21 #include "internal.h"
22 #include "debug.h"
23
24 #include "google.h"
25 #include "jabber.h"
26 #include "chat.h"
27
28 /* This does two passes on the string. The first pass goes through
29 * and determine if all the structured text is properly balanced, and
30 * how many instances of each there is. The second pass goes and converts
31 * everything to HTML, depending on what's figured out by the first pass.
32 * It will short circuit once it knows it has no more replacements to make
33 */
34 char *jabber_google_format_to_html(const char *text)
35 {
36 const char *p;
37
38 /* The start of the screen may be consdiered a space for this purpose */
39 gboolean preceding_space = TRUE;
40
41 gboolean in_bold = FALSE, in_italic = FALSE;
42 gboolean in_tag = FALSE;
43
44 gint bold_count = 0, italic_count = 0;
45
46 GString *str;
47
48 for (p = text; *p != '\0'; p = g_utf8_next_char(p)) {
49 gunichar c = g_utf8_get_char(p);
50 if (c == '*' && !in_tag) {
51 if (in_bold && (g_unichar_isspace(*(p+1)) ||
52 *(p+1) == '\0' ||
53 *(p+1) == '<')) {
54 bold_count++;
55 in_bold = FALSE;
56 } else if (preceding_space && !in_bold && !g_unichar_isspace(*(p+1))) {
57 bold_count++;
58 in_bold = TRUE;
59 }
60 preceding_space = TRUE;
61 } else if (c == '_' && !in_tag) {
62 if (in_italic && (g_unichar_isspace(*(p+1)) ||
63 *(p+1) == '\0' ||
64 *(p+1) == '<')) {
65 italic_count++;
66 in_italic = FALSE;
67 } else if (preceding_space && !in_italic && !g_unichar_isspace(*(p+1))) {
68 italic_count++;
69 in_italic = TRUE;
70 }
71 preceding_space = TRUE;
72 } else if (c == '<' && !in_tag) {
73 in_tag = TRUE;
74 } else if (c == '>' && in_tag) {
75 in_tag = FALSE;
76 } else if (!in_tag) {
77 if (g_unichar_isspace(c))
78 preceding_space = TRUE;
79 else
80 preceding_space = FALSE;
81 }
82 }
83
84 str = g_string_new(NULL);
85 in_bold = in_italic = in_tag = FALSE;
86 preceding_space = TRUE;
87
88 for (p = text; *p != '\0'; p = g_utf8_next_char(p)) {
89 gunichar c = g_utf8_get_char(p);
90
91 if (bold_count < 2 && italic_count < 2 && !in_bold && !in_italic) {
92 g_string_append(str, p);
93 return g_string_free(str, FALSE);
94 }
95
96
97 if (c == '*' && !in_tag) {
98 if (in_bold &&
99 (g_unichar_isspace(*(p+1))||*(p+1)=='<')) { /* This is safe in UTF-8 */
100 str = g_string_append(str, "</b>");
101 in_bold = FALSE;
102 bold_count--;
103 } else if (preceding_space && bold_count > 1 && !g_unichar_isspace(*(p+1))) {
104 str = g_string_append(str, "<b>");
105 bold_count--;
106 in_bold = TRUE;
107 } else {
108 str = g_string_append_unichar(str, c);
109 }
110 preceding_space = TRUE;
111 } else if (c == '_' && !in_tag) {
112 if (in_italic &&
113 (g_unichar_isspace(*(p+1))||*(p+1)=='<')) {
114 str = g_string_append(str, "</i>");
115 italic_count--;
116 in_italic = FALSE;
117 } else if (preceding_space && italic_count > 1 && !g_unichar_isspace(*(p+1))) {
118 str = g_string_append(str, "<i>");
119 italic_count--;
120 in_italic = TRUE;
121 } else {
122 str = g_string_append_unichar(str, c);
123 }
124 preceding_space = TRUE;
125 } else if (c == '<' && !in_tag) {
126 str = g_string_append_unichar(str, c);
127 in_tag = TRUE;
128 } else if (c == '>' && in_tag) {
129 str = g_string_append_unichar(str, c);
130 in_tag = FALSE;
131 } else if (!in_tag) {
132 str = g_string_append_unichar(str, c);
133 if (g_unichar_isspace(c))
134 preceding_space = TRUE;
135 else
136 preceding_space = FALSE;
137 } else {
138 str = g_string_append_unichar(str, c);
139 }
140 }
141 return g_string_free(str, FALSE);
142 }
143
144
145
146 void google_buddy_node_chat(PurpleBlistNode *node, gpointer data)
147 {
148 PurpleBuddy *buddy;
149 PurpleConnection *gc;
150 JabberStream *js;
151 JabberChat *chat;
152 gchar *room;
153 gchar *uuid = purple_uuid_random();
154
155 g_return_if_fail(PURPLE_BLIST_NODE_IS_BUDDY(node));
156
157 buddy = PURPLE_BUDDY(node);
158 gc = purple_account_get_connection(purple_buddy_get_account(buddy));
159 g_return_if_fail(gc != NULL);
160 js = purple_connection_get_protocol_data(gc);
161
162 room = g_strdup_printf("private-chat-%s", uuid);
163 chat = jabber_join_chat(js, room, GOOGLE_GROUPCHAT_SERVER, js->user->node,
164 NULL, NULL);
165 if (chat) {
166 chat->muc = TRUE;
167 jabber_chat_invite(gc, chat->id, "", purple_buddy_get_name(buddy));
168 }
169
170 g_free(room);
171 g_free(uuid);
172 }