]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-theme-adium.c
Updated Polish translation
[empathy.git] / libempathy-gtk / empathy-theme-adium.c
index fcf95ca026a01f898a49459cea3987fc7dd2c32c..7736be27070bb2e783fc545502aafaff94d0c0a4 100644 (file)
@@ -40,6 +40,7 @@
 #include "empathy-conf.h"
 #include "empathy-ui-utils.h"
 #include "empathy-plist.h"
+#include "empathy-string-parser.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
 #include <libempathy/empathy-debug.h>
@@ -191,122 +192,88 @@ 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);
+       GString *string = user_data;
+       gint i;
+       gint prev = 0;
 
-       /* 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;
-
-                               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_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, (int)len, text, (int)len, text);
+}
+
+static EmpathyStringParser string_parsers[] = {
+       {empathy_string_match_link, empathy_string_replace_link},
+       {theme_adium_match_newline, NULL},
+       {empathy_string_match_all, empathy_string_replace_escaped},
+       {NULL, NULL}
+};
+
+static EmpathyStringParser string_parsers_with_smiley[] = {
+       {empathy_string_match_link, empathy_string_replace_link},
+       {empathy_string_match_smiley, theme_adium_replace_smiley},
+       {theme_adium_match_newline, NULL},
+       {empathy_string_match_all, empathy_string_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
@@ -434,6 +401,28 @@ theme_adium_append_html (EmpathyThemeAdium *theme,
        g_free (script);
 }
 
+static void
+theme_adium_append_event_escaped (EmpathyChatView *view,
+                                 const gchar     *escaped)
+{
+       EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
+       EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
+
+       if (priv->data->status_html) {
+               theme_adium_append_html (theme, "appendMessage",
+                                        priv->data->status_html,
+                                        priv->data->status_len,
+                                        escaped, NULL, NULL, NULL, NULL,
+                                        "event", empathy_time_get_current ());
+       }
+
+       /* There is no last contact */
+       if (priv->last_contact) {
+               g_object_unref (priv->last_contact);
+               priv->last_contact = NULL;
+       }
+}
+
 static void
 theme_adium_append_message (EmpathyChatView *view,
                            EmpathyMessage  *msg)
@@ -441,8 +430,8 @@ theme_adium_append_message (EmpathyChatView *view,
        EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
        EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
        EmpathyContact        *sender;
-       EmpathyAccount        *account;
-       gchar                 *dup_body = NULL;
+       TpAccount             *account;
+       gchar                 *body_escaped;
        const gchar           *body;
        const gchar           *name;
        const gchar           *contact_id;
@@ -467,15 +456,12 @@ theme_adium_append_message (EmpathyChatView *view,
        sender = empathy_message_get_sender (msg);
        account = empathy_contact_get_account (sender);
        service_name = empathy_protocol_name_to_display_name
-               (empathy_account_get_protocol (account));
+               (tp_account_get_protocol (account));
        if (service_name == NULL)
-               service_name = empathy_account_get_protocol (account);
+               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);
 
@@ -483,10 +469,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);
-               empathy_chat_view_append_event (view, str);
+               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;
        }
 
@@ -607,7 +594,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);
@@ -623,7 +610,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);
 }
 
@@ -631,26 +618,11 @@ static void
 theme_adium_append_event (EmpathyChatView *view,
                          const gchar     *str)
 {
-       EmpathyThemeAdium     *theme = EMPATHY_THEME_ADIUM (view);
-       EmpathyThemeAdiumPriv *priv = GET_PRIV (theme);
-
-       if (priv->data->status_html) {
-               gchar *str_escaped;
-
-               str_escaped = g_markup_escape_text (str, -1);
-               theme_adium_append_html (theme, "appendMessage",
-                                        priv->data->status_html,
-                                        priv->data->status_len,
-                                        str_escaped, NULL, NULL, NULL, NULL,
-                                        "event", empathy_time_get_current ());
-               g_free (str_escaped);
-       }
+       gchar *str_escaped;
 
-       /* There is no last contact */
-       if (priv->last_contact) {
-               g_object_unref (priv->last_contact);
-               priv->last_contact = NULL;
-       }
+       str_escaped = g_markup_escape_text (str, -1);
+       theme_adium_append_event_escaped (view, str_escaped);
+       g_free (str_escaped);
 }
 
 static void
@@ -697,26 +669,31 @@ theme_adium_clear (EmpathyChatView *view)
 static gboolean
 theme_adium_find_previous (EmpathyChatView *view,
                           const gchar     *search_criteria,
-                          gboolean         new_search)
+                          gboolean         new_search,
+                          gboolean         match_case)
 {
+       /* FIXME: Doesn't respect new_search */
        return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
-                                           search_criteria, FALSE,
+                                           search_criteria, match_case,
                                            FALSE, TRUE);
 }
 
 static gboolean
 theme_adium_find_next (EmpathyChatView *view,
                       const gchar     *search_criteria,
-                      gboolean         new_search)
+                      gboolean         new_search,
+                      gboolean         match_case)
 {
+       /* FIXME: Doesn't respect new_search */
        return webkit_web_view_search_text (WEBKIT_WEB_VIEW (view),
-                                           search_criteria, FALSE,
+                                           search_criteria, match_case,
                                            TRUE, TRUE);
 }
 
 static void
 theme_adium_find_abilities (EmpathyChatView *view,
                            const gchar    *search_criteria,
+                            gboolean        match_case,
                            gboolean       *can_do_previous,
                            gboolean       *can_do_next)
 {
@@ -730,11 +707,12 @@ theme_adium_find_abilities (EmpathyChatView *view,
 
 static void
 theme_adium_highlight (EmpathyChatView *view,
-                      const gchar     *text)
+                      const gchar     *text,
+                      gboolean         match_case)
 {
        webkit_web_view_unmark_text_matches (WEBKIT_WEB_VIEW (view));
        webkit_web_view_mark_text_matches (WEBKIT_WEB_VIEW (view),
-                                          text, FALSE, 0);
+                                          text, match_case, 0);
        webkit_web_view_set_highlight_text_matches (WEBKIT_WEB_VIEW (view),
                                                    TRUE);
 }
@@ -827,6 +805,8 @@ theme_adium_context_menu_for_event (EmpathyThemeAdium *theme, GdkEventButton *ev
        gtk_widget_show_all (menu);
        gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
                        event->button, event->time);
+       g_object_ref_sink (menu);
+       g_object_unref (menu);
 }
 
 static gboolean