comparison pidgin/plugins/xmppconsole.c @ 32813:b31f7945d9b6

Use 'div's for indent which allows WebKit to do better word-wrapping.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Thu, 31 May 2012 06:09:59 +0000
parents 43e89b279eb4
children
comparison
equal deleted inserted replaced
32812:43e89b279eb4 32813:b31f7945d9b6
44 XmppConsole *console = NULL; 44 XmppConsole *console = NULL;
45 static void *xmpp_console_handle = NULL; 45 static void *xmpp_console_handle = NULL;
46 46
47 #define EMPTY_HTML \ 47 #define EMPTY_HTML \
48 "<html><head><style type='text/css'>" \ 48 "<html><head><style type='text/css'>" \
49 "body { white-space: pre-wrap; margin: 0; }" \ 49 "body { word-wrap: break-word; margin: 0; }" \
50 "div.tab { padding-left: 1em; }" \
50 "div.info { color: #777777; }" \ 51 "div.info { color: #777777; }" \
51 "div.incoming { background-color: #ffcece; }" \ 52 "div.incoming { background-color: #ffcece; }" \
52 "div.outgoing { background-color: #dcecc4; }" \ 53 "div.outgoing { background-color: #dcecc4; }" \
53 "span.bracket { color: #940f8c; }" \ 54 "span.bracket { color: #940f8c; }" \
54 "span.tag { color: #8b1dab; font-weight: bold; }" \ 55 "span.tag { color: #8b1dab; font-weight: bold; }" \
56 "span.value { color: #324aa4; }" \ 57 "span.value { color: #324aa4; }" \
57 "span.xmlns { color: #2cb12f; font-weight: bold;}" \ 58 "span.xmlns { color: #2cb12f; font-weight: bold;}" \
58 "</style></head></html>" 59 "</style></head></html>"
59 60
60 static char * 61 static char *
61 xmlnode_to_pretty_str(xmlnode *node, int *len, int depth) 62 xmlnode_to_pretty_str(xmlnode *node, int *len)
62 { 63 {
63 GString *text = g_string_new(""); 64 GString *text = g_string_new("");
64 xmlnode *c; 65 xmlnode *c;
65 char *node_name, *esc, *esc2, *tab = NULL; 66 char *node_name, *esc, *esc2;
66 gboolean need_end = FALSE, pretty = TRUE; 67 gboolean need_end = FALSE, pretty = TRUE;
67 68
68 g_return_val_if_fail(node != NULL, NULL); 69 g_return_val_if_fail(node != NULL, NULL);
69
70 if (pretty && depth) {
71 tab = g_strnfill(depth, '\t');
72 text = g_string_append(text, tab);
73 }
74 70
75 node_name = g_markup_escape_text(node->name, -1); 71 node_name = g_markup_escape_text(node->name, -1);
76 g_string_append_printf(text, 72 g_string_append_printf(text,
77 "<span class=bracket>&lt;</span>" 73 "<span class=bracket>&lt;</span>"
78 "<span class=tag>%s</span>", 74 "<span class=tag>%s</span>",
113 if (need_end) { 109 if (need_end) {
114 g_string_append_printf(text, 110 g_string_append_printf(text,
115 "<span class=bracket>&gt;</span>%s", 111 "<span class=bracket>&gt;</span>%s",
116 pretty ? "<br>" : ""); 112 pretty ? "<br>" : "");
117 113
114 need_end = FALSE;
118 for (c = node->child; c; c = c->next) 115 for (c = node->child; c; c = c->next)
119 { 116 {
120 if (c->type == XMLNODE_TYPE_TAG) { 117 if (c->type == XMLNODE_TYPE_TAG) {
121 int esc_len; 118 int esc_len;
122 esc = xmlnode_to_pretty_str(c, &esc_len, depth+1); 119 esc = xmlnode_to_pretty_str(c, &esc_len);
120 if (!need_end) {
121 g_string_append(text, "<div class=tab>");
122 need_end = TRUE;
123 }
123 text = g_string_append_len(text, esc, esc_len); 124 text = g_string_append_len(text, esc, esc_len);
124 g_free(esc); 125 g_free(esc);
125 } else if (c->type == XMLNODE_TYPE_DATA && c->data_sz > 0) { 126 } else if (c->type == XMLNODE_TYPE_DATA && c->data_sz > 0) {
126 esc = g_markup_escape_text(c->data, c->data_sz); 127 esc = g_markup_escape_text(c->data, c->data_sz);
127 text = g_string_append(text, esc); 128 text = g_string_append(text, esc);
128 g_free(esc); 129 g_free(esc);
129 } 130 }
130 } 131 }
131 132
132 if(tab && pretty) 133 if (need_end)
133 text = g_string_append(text, tab); 134 g_string_append(text, "</div>");
135
134 g_string_append_printf(text, 136 g_string_append_printf(text,
135 "<span class=bracket>&lt;</span>/" 137 "<span class=bracket>&lt;</span>/"
136 "<span class=tag>%s</span>" 138 "<span class=tag>%s</span>"
137 "<span class=bracket>&gt;</span><br>", 139 "<span class=bracket>&gt;</span><br>",
138 node_name); 140 node_name);
141 "/<span class=bracket>&gt;</span><br>"); 143 "/<span class=bracket>&gt;</span><br>");
142 } 144 }
143 145
144 g_free(node_name); 146 g_free(node_name);
145 147
146 g_free(tab);
147
148 if (len) 148 if (len)
149 *len = text->len; 149 *len = text->len;
150 150
151 return g_string_free(text, FALSE); 151 return g_string_free(text, FALSE);
152 } 152 }
156 { 156 {
157 char *str, *formatted; 157 char *str, *formatted;
158 158
159 if (!console || console->gc != gc) 159 if (!console || console->gc != gc)
160 return; 160 return;
161 str = xmlnode_to_pretty_str(*packet, NULL, 0); 161 str = xmlnode_to_pretty_str(*packet, NULL);
162 formatted = g_strdup_printf("<div class=incoming>%s</div>", str); 162 formatted = g_strdup_printf("<div class=incoming>%s</div>", str);
163 gtk_webview_append_html(GTK_WEBVIEW(console->webview), formatted); 163 gtk_webview_append_html(GTK_WEBVIEW(console->webview), formatted);
164 g_free(formatted); 164 g_free(formatted);
165 g_free(str); 165 g_free(str);
166 } 166 }
177 node = xmlnode_from_str(*packet, -1); 177 node = xmlnode_from_str(*packet, -1);
178 178
179 if (!node) 179 if (!node)
180 return; 180 return;
181 181
182 str = xmlnode_to_pretty_str(node, NULL, 0); 182 str = xmlnode_to_pretty_str(node, NULL);
183 formatted = g_strdup_printf("<div class=outgoing>%s</div>", str); 183 formatted = g_strdup_printf("<div class=outgoing>%s</div>", str);
184 gtk_webview_append_html(GTK_WEBVIEW(console->webview), formatted); 184 gtk_webview_append_html(GTK_WEBVIEW(console->webview), formatted);
185 g_free(formatted); 185 g_free(formatted);
186 g_free(str); 186 g_free(str);
187 xmlnode_free(node); 187 xmlnode_free(node);
748 } 748 }
749 749
750 static void 750 static void
751 dropdown_changed_cb(GtkComboBox *widget, gpointer nul) 751 dropdown_changed_cb(GtkComboBox *widget, gpointer nul)
752 { 752 {
753 PurpleAccount *account;
754
755 if (!console) 753 if (!console)
756 return; 754 return;
757 755
758 console->gc = g_list_nth_data(console->accounts, gtk_combo_box_get_active(GTK_COMBO_BOX(console->dropdown))); 756 console->gc = g_list_nth_data(console->accounts, gtk_combo_box_get_active(GTK_COMBO_BOX(console->dropdown)));
759 gtk_webview_load_html_string(GTK_WEBVIEW(console->webview), EMPTY_HTML); 757 gtk_webview_load_html_string(GTK_WEBVIEW(console->webview), EMPTY_HTML);