aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy-gtk/empathy-theme-adium.c
diff options
context:
space:
mode:
Diffstat (limited to 'libempathy-gtk/empathy-theme-adium.c')
-rw-r--r--libempathy-gtk/empathy-theme-adium.c230
1 files changed, 116 insertions, 114 deletions
diff --git a/libempathy-gtk/empathy-theme-adium.c b/libempathy-gtk/empathy-theme-adium.c
index c1365579b..4e70fedb2 100644
--- a/libempathy-gtk/empathy-theme-adium.c
+++ b/libempathy-gtk/empathy-theme-adium.c
@@ -191,122 +191,126 @@ theme_adium_open_address_cb (GtkMenuItem *menuitem,
g_free (uri);
}
-static gchar *
-theme_adium_parse_body (EmpathyThemeAdium *theme,
- const gchar *text)
+static void
+theme_adium_match_newline (const gchar *text,
+ gssize len,
+ EmpathyStringReplace replace_func,
+ EmpathyStringParser *sub_parsers,
+ gpointer user_data)
{
- EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
- gboolean use_smileys = FALSE;
- GSList *smileys, *l;
- GString *string;
- gint i;
- GRegex *uri_regex;
- GMatchInfo *match_info;
- gboolean match;
- gchar *ret = NULL;
- gint prev;
-
- empathy_conf_get_bool (empathy_conf_get (),
- EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
- &use_smileys);
-
- /* Add <a href></a> arround links */
- uri_regex = empathy_uri_regex_dup_singleton ();
- match = g_regex_match (uri_regex, text, 0, &match_info);
- if (match) {
- gint last = 0;
- gint s = 0, e = 0;
-
- string = g_string_sized_new (strlen (text));
- do {
- gchar *real_url;
-
- g_match_info_fetch_pos (match_info, 0, &s, &e);
-
- if (s > last) {
- /* Append the text between last link (or the
- * start of the message) and this link */
- gchar *str;
- str = g_markup_escape_text (text + last, s - last);
- g_string_append (string, str);
- g_free (str);
- }
-
- /* Append the link inside <a href=""></a> tag */
- real_url = empathy_make_absolute_url_len (text + s, e - s);
-
- g_string_append (string, "<a href=\"");
- g_string_append (string, real_url);
- g_string_append (string, "\">");
- g_string_append_len (string, text + s, e - s);
- g_string_append (string, "</a>");
-
- g_free (real_url);
- last = e;
- } while (g_match_info_next (match_info, NULL));
-
- if (e < (gint) strlen (text)) {
- /* Append the text after the last link */
- gchar *str;
- str = g_markup_escape_text (text + e, strlen (text) - e);
- g_string_append (string, str);
- g_free (str);
- }
-
- g_free (ret);
- text = ret = g_string_free (string, FALSE);
- } else if (use_smileys) {
- /* Replace smileys by a <img/> tag */
- string = g_string_sized_new (strlen (text));
- smileys = empathy_smiley_manager_parse (priv->smiley_manager, text);
- for (l = smileys; l; l = l->next) {
- EmpathySmiley *smiley;
-
- smiley = l->data;
- if (smiley->path) {
- g_string_append_printf (string,
- "<abbr title='%s'><img src=\"%s\"/ alt=\"%s\"/></abbr>",
- smiley->str, smiley->path, smiley->str);
- } else {
- gchar *str;
+ GString *string = user_data;
+ gint i;
+ gint prev = 0;
- str = g_markup_escape_text (smiley->str, -1);
- g_string_append (string, str);
- g_free (str);
- }
- empathy_smiley_free (smiley);
- }
- g_slist_free (smileys);
-
- g_free (ret);
- text = ret = g_string_free (string, FALSE);
- } else {
- text = ret = g_markup_escape_text (text, -1);
+ if (len < 0) {
+ len = G_MAXSSIZE;
}
- g_match_info_free (match_info);
- g_regex_unref (uri_regex);
-
/* Replace \n by <br/> */
- string = NULL;
- prev = 0;
- for (i = 0; text[i] != '\0'; i++) {
+ for (i = 0; i < len && text[i] != '\0'; i++) {
if (text[i] == '\n') {
- if (!string ) {
- string = g_string_sized_new (strlen (text));
- }
- g_string_append_len (string, text + prev, i - prev);
+ empathy_string_parser_substr (text + prev,
+ i - prev, sub_parsers,
+ user_data);
g_string_append (string, "<br/>");
prev = i + 1;
}
}
- if (string) {
- g_string_append (string, text + prev);
- g_free (ret);
- text = ret = g_string_free (string, FALSE);
- }
+ empathy_string_parser_substr (text + prev, i - prev,
+ sub_parsers, user_data);
+}
- return ret;
+static void
+theme_adium_replace_link (const gchar *text,
+ gssize len,
+ gpointer match_data,
+ gpointer user_data)
+{
+ GString *string = user_data;
+ gchar *real_url;
+ gchar *escaped;
+
+ real_url = empathy_make_absolute_url_len (text, len);
+
+ /* The thing we are making a link of may contain
+ * characters which need escaping */
+ escaped = g_markup_escape_text (text, len);
+
+ /* Append the link inside <a href=""></a> tag */
+ g_string_append_printf (string, "<a href=\"%s\">%s</a>",
+ real_url, escaped);
+
+ g_free (real_url);
+ g_free (escaped);
+}
+
+static void
+theme_adium_replace_smiley (const gchar *text,
+ gssize len,
+ gpointer match_data,
+ gpointer user_data)
+{
+ EmpathySmileyHit *hit = match_data;
+ GString *string = user_data;
+
+ /* Replace smiley by a <img/> tag */
+ g_string_append_printf (string,
+ "<img src=\"%s\" alt=\"%.*s\" title=\"%.*s\"/>",
+ hit->path, len, text, len, text);
+}
+
+static void
+theme_adium_replace_escaped (const gchar *text,
+ gssize len,
+ gpointer match_data,
+ gpointer user_data)
+{
+ GString *string = user_data;
+ gchar *escaped;
+
+ escaped = g_markup_escape_text (text, len);
+ g_string_append (string, escaped);
+ g_free (escaped);
+}
+
+static EmpathyStringParser string_parsers[] = {
+ {empathy_string_match_link, theme_adium_replace_link},
+ {theme_adium_match_newline, NULL},
+ {empathy_string_match_all, theme_adium_replace_escaped},
+ {NULL, NULL}
+};
+
+static EmpathyStringParser string_parsers_with_smiley[] = {
+ {empathy_string_match_link, theme_adium_replace_link},
+ {empathy_string_match_smiley, theme_adium_replace_smiley},
+ {theme_adium_match_newline, NULL},
+ {empathy_string_match_all, theme_adium_replace_escaped},
+ {NULL, NULL}
+};
+
+static gchar *
+theme_adium_parse_body (const gchar *text)
+{
+ EmpathyStringParser *parsers;
+ GString *string;
+ gboolean use_smileys;
+
+ /* Check if we have to parse smileys */
+ empathy_conf_get_bool (empathy_conf_get (),
+ EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
+ &use_smileys);
+ if (use_smileys)
+ parsers = string_parsers_with_smiley;
+ else
+ parsers = string_parsers;
+
+ /* Parse text and construct string with links and smileys replaced
+ * by html tags. Also escape text to make sure html code is
+ * displayed verbatim. */
+ string = g_string_sized_new (strlen (text));
+ empathy_string_parser_substr (text, -1, parsers, string);
+
+ return g_string_free (string, FALSE);
}
static void
@@ -464,7 +468,7 @@ theme_adium_append_message (EmpathyChatView *view,
EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
EmpathyContact *sender;
TpAccount *account;
- gchar *dup_body = NULL;
+ gchar *body_escaped;
const gchar *body;
const gchar *name;
const gchar *contact_id;
@@ -494,10 +498,7 @@ theme_adium_append_message (EmpathyChatView *view,
service_name = tp_account_get_protocol (account);
timestamp = empathy_message_get_timestamp (msg);
body = empathy_message_get_body (msg);
- dup_body = theme_adium_parse_body (theme, body);
- if (dup_body) {
- body = dup_body;
- }
+ body_escaped = theme_adium_parse_body (body);
name = empathy_contact_get_name (sender);
contact_id = empathy_contact_get_id (sender);
@@ -505,10 +506,11 @@ theme_adium_append_message (EmpathyChatView *view,
if (empathy_message_get_tptype (msg) == TP_CHANNEL_TEXT_MESSAGE_TYPE_ACTION) {
gchar *str;
- str = g_strdup_printf ("%s %s", name, body);
+ str = g_strdup_printf ("%s %s", name, body_escaped);
theme_adium_append_event_escaped (view, str);
+
g_free (str);
- g_free (dup_body);
+ g_free (body_escaped);
return;
}
@@ -629,7 +631,7 @@ theme_adium_append_message (EmpathyChatView *view,
}
if (html != NULL) {
- theme_adium_append_html (theme, func, html, len, body,
+ theme_adium_append_html (theme, func, html, len, body_escaped,
avatar_filename, name, contact_id,
service_name, message_classes->str,
timestamp);
@@ -645,7 +647,7 @@ theme_adium_append_message (EmpathyChatView *view,
priv->last_timestamp = timestamp;
priv->last_is_backlog = is_backlog;
- g_free (dup_body);
+ g_free (body_escaped);
g_string_free (message_classes, TRUE);
}