]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat-text-view.c
Updated Polish translation
[empathy.git] / libempathy-gtk / empathy-chat-text-view.c
index 5b0a3568055c31a165418de0e86b79f35a123c98..7f16ab9a325a9f4e5693d9e96bf4a0daf5ca0fd0 100644 (file)
 #include <telepathy-glib/util.h>
 
 #include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-account.h>
 
 #include "empathy-chat-text-view.h"
 #include "empathy-chat.h"
 #include "empathy-conf.h"
 #include "empathy-ui-utils.h"
 #include "empathy-smiley-manager.h"
+#include "empathy-string-parser.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_CHAT
 #include <libempathy/empathy-debug.h>
@@ -74,6 +74,8 @@ typedef struct {
 
 static void chat_text_view_iface_init (EmpathyChatViewIface *iface);
 
+static void chat_text_view_copy_clipboard (EmpathyChatView *view);
+
 G_DEFINE_TYPE_WITH_CODE (EmpathyChatTextView, empathy_chat_text_view,
                         GTK_TYPE_TEXT_VIEW,
                         G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_CHAT_VIEW,
@@ -359,7 +361,7 @@ chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
        EmpathyChatTextViewPriv *priv;
        GtkTextIter         top, bottom;
        gint                line;
-       gint                remove;
+       gint                remove_;
        GtkTextTagTable    *table;
        GtkTextTag         *tag;
 
@@ -371,11 +373,11 @@ chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
                return;
        }
 
-       remove = line - MAX_LINES;
+       remove_ = line - MAX_LINES;
        gtk_text_buffer_get_start_iter (priv->buffer, &top);
 
        bottom = top;
-       if (!gtk_text_iter_forward_lines (&bottom, remove)) {
+       if (!gtk_text_iter_forward_lines (&bottom, remove_)) {
                return;
        }
 
@@ -506,7 +508,7 @@ chat_text_view_drag_motion (GtkWidget      *widget,
                            GdkDragContext *context,
                            gint            x,
                            gint            y,
-                           guint           time)
+                           guint           time_)
 {
        /* Don't handle drag motion, since we don't want the view to scroll as
         * the result of dragging something across it. */
@@ -580,11 +582,18 @@ chat_text_view_finalize (GObject *object)
        G_OBJECT_CLASS (empathy_chat_text_view_parent_class)->finalize (object);
 }
 
+static void
+text_view_copy_clipboard (GtkTextView *text_view)
+{
+       chat_text_view_copy_clipboard (EMPATHY_CHAT_VIEW (text_view));
+}
+
 static void
 empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
 {
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+       GtkTextViewClass *text_view_class = GTK_TEXT_VIEW_CLASS (klass);
 
        object_class->finalize = chat_text_view_finalize;
        object_class->get_property = chat_text_view_get_property;
@@ -593,6 +602,8 @@ empathy_chat_text_view_class_init (EmpathyChatTextViewClass *klass)
        widget_class->size_allocate = chat_text_view_size_allocate;
        widget_class->drag_motion = chat_text_view_drag_motion;
 
+       text_view_class->copy_clipboard = text_view_copy_clipboard;
+
        g_object_class_install_property (object_class,
                                         PROP_LAST_CONTACT,
                                         g_param_spec_object ("last-contact",
@@ -828,7 +839,8 @@ chat_text_view_clear (EmpathyChatView *view)
 static gboolean
 chat_text_view_find_previous (EmpathyChatView *view,
                                const gchar     *search_criteria,
-                               gboolean         new_search)
+                               gboolean         new_search,
+                               gboolean         match_case)
 {
        EmpathyChatTextViewPriv *priv;
        GtkTextBuffer      *buffer;
@@ -870,7 +882,7 @@ chat_text_view_find_previous (EmpathyChatView *view,
                from_start = TRUE;
        }
 
-       if (priv->find_mark_previous) {
+       if (!new_search && priv->find_mark_previous) {
                gtk_text_buffer_get_iter_at_mark (buffer,
                                                  &iter_at_mark,
                                                  priv->find_mark_previous);
@@ -881,11 +893,23 @@ chat_text_view_find_previous (EmpathyChatView *view,
 
        priv->find_last_direction = FALSE;
 
-       found = empathy_text_iter_backward_search (&iter_at_mark,
-                                                  search_criteria,
-                                                  &iter_match_start,
-                                                  &iter_match_end,
-                                                  NULL);
+       /* Use the standard GTK+ method for case sensitive searches. It can't do
+        * case insensitive searches (see bug #61852), so keep the custom method
+        * around for case insensitive searches. */
+       if (match_case) {
+               found = gtk_text_iter_backward_search (&iter_at_mark,
+                                                      search_criteria,
+                                                      0, /* no text search flags, we want exact matches */
+                                                      &iter_match_start,
+                                                      &iter_match_end,
+                                                      NULL);
+       } else {
+               found = empathy_text_iter_backward_search (&iter_at_mark,
+                                                          search_criteria,
+                                                          &iter_match_start,
+                                                          &iter_match_end,
+                                                          NULL);
+       }
 
        if (!found) {
                gboolean result = FALSE;
@@ -899,7 +923,8 @@ chat_text_view_find_previous (EmpathyChatView *view,
                        priv->find_wrapped = TRUE;
                        result = chat_text_view_find_previous (view,
                                                                 search_criteria,
-                                                                FALSE);
+                                                                FALSE,
+                                                                match_case);
                        priv->find_wrapped = FALSE;
                }
 
@@ -943,7 +968,8 @@ chat_text_view_find_previous (EmpathyChatView *view,
 static gboolean
 chat_text_view_find_next (EmpathyChatView *view,
                            const gchar     *search_criteria,
-                           gboolean         new_search)
+                           gboolean         new_search,
+                           gboolean         match_case)
 {
        EmpathyChatTextViewPriv *priv;
        GtkTextBuffer      *buffer;
@@ -985,7 +1011,7 @@ chat_text_view_find_next (EmpathyChatView *view,
                from_start = TRUE;
        }
 
-       if (priv->find_mark_next) {
+       if (!new_search && priv->find_mark_next) {
                gtk_text_buffer_get_iter_at_mark (buffer,
                                                  &iter_at_mark,
                                                  priv->find_mark_next);
@@ -996,11 +1022,23 @@ chat_text_view_find_next (EmpathyChatView *view,
 
        priv->find_last_direction = TRUE;
 
-       found = empathy_text_iter_forward_search (&iter_at_mark,
-                                                 search_criteria,
-                                                 &iter_match_start,
-                                                 &iter_match_end,
-                                                 NULL);
+       /* Use the standard GTK+ method for case sensitive searches. It can't do
+        * case insensitive searches (see bug #61852), so keep the custom method
+        * around for case insensitive searches. */
+       if (match_case) {
+               found = gtk_text_iter_forward_search (&iter_at_mark,
+                                                     search_criteria,
+                                                     0,
+                                                     &iter_match_start,
+                                                     &iter_match_end,
+                                                     NULL);
+       } else {
+               found = empathy_text_iter_forward_search (&iter_at_mark,
+                                                         search_criteria,
+                                                         &iter_match_start,
+                                                         &iter_match_end,
+                                                         NULL);
+       }
 
        if (!found) {
                gboolean result = FALSE;
@@ -1014,7 +1052,8 @@ chat_text_view_find_next (EmpathyChatView *view,
                        priv->find_wrapped = TRUE;
                        result = chat_text_view_find_next (view,
                                                             search_criteria,
-                                                            FALSE);
+                                                            FALSE,
+                                                            match_case);
                        priv->find_wrapped = FALSE;
                }
 
@@ -1058,6 +1097,7 @@ chat_text_view_find_next (EmpathyChatView *view,
 static void
 chat_text_view_find_abilities (EmpathyChatView *view,
                                 const gchar    *search_criteria,
+                                gboolean        match_case,
                                 gboolean       *can_do_previous,
                                 gboolean       *can_do_next)
 {
@@ -1084,11 +1124,20 @@ chat_text_view_find_abilities (EmpathyChatView *view,
                        gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
                }
 
-               *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
-                                                                     search_criteria,
-                                                                     &iter_match_start,
-                                                                     &iter_match_end,
-                                                                     NULL);
+               if (match_case) {
+                       *can_do_previous = gtk_text_iter_backward_search (&iter_at_mark,
+                                                                         search_criteria,
+                                                                         0,
+                                                                         &iter_match_start,
+                                                                         &iter_match_end,
+                                                                         NULL);
+               } else {
+                       *can_do_previous = empathy_text_iter_backward_search (&iter_at_mark,
+                                                                             search_criteria,
+                                                                             &iter_match_start,
+                                                                             &iter_match_end,
+                                                                             NULL);
+               }
        }
 
        if (can_do_next) {
@@ -1100,17 +1149,27 @@ chat_text_view_find_abilities (EmpathyChatView *view,
                        gtk_text_buffer_get_start_iter (buffer, &iter_at_mark);
                }
 
-               *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
-                                                                search_criteria,
-                                                                &iter_match_start,
-                                                                &iter_match_end,
-                                                                NULL);
+               if (match_case) {
+                       *can_do_next = gtk_text_iter_forward_search (&iter_at_mark,
+                                                                    search_criteria,
+                                                                    0,
+                                                                    &iter_match_start,
+                                                                    &iter_match_end,
+                                                                    NULL);
+               } else {
+                       *can_do_next = empathy_text_iter_forward_search (&iter_at_mark,
+                                                                        search_criteria,
+                                                                        &iter_match_start,
+                                                                        &iter_match_end,
+                                                                        NULL);
+               }
        }
 }
 
 static void
 chat_text_view_highlight (EmpathyChatView *view,
-                           const gchar     *text)
+                           const gchar     *text,
+                           gboolean         match_case)
 {
        GtkTextBuffer *buffer;
        GtkTextIter    iter;
@@ -1136,12 +1195,20 @@ chat_text_view_highlight (EmpathyChatView *view,
        }
 
        while (1) {
-               found = empathy_text_iter_forward_search (&iter,
-                                                         text,
-                                                         &iter_match_start,
-                                                         &iter_match_end,
-                                                         NULL);
-
+               if (match_case) {
+                       found = gtk_text_iter_forward_search (&iter,
+                                                             text,
+                                                             0,
+                                                             &iter_match_start,
+                                                             &iter_match_end,
+                                                             NULL);
+               } else {
+                       found = empathy_text_iter_forward_search (&iter,
+                                                                 text,
+                                                                 &iter_match_start,
+                                                                 &iter_match_end,
+                                                                 NULL);
+               }
                if (!found) {
                        break;
                }
@@ -1151,7 +1218,6 @@ chat_text_view_highlight (EmpathyChatView *view,
                                                   &iter_match_end);
 
                iter = iter_match_end;
-               gtk_text_iter_forward_char (&iter);
        }
 }
 
@@ -1180,6 +1246,8 @@ chat_text_view_copy_clipboard (EmpathyChatView *view)
 
        for (iter = start; !gtk_text_iter_equal (&iter, &end); gtk_text_iter_forward_char (&iter)) {
                c = gtk_text_iter_get_char (&iter);
+               /* 0xFFFC is the 'object replacement' unicode character,
+                * it indicates the presence of a pixbuf or a widget. */
                if (c == 0xFFFC) {
                        ignore_newlines = FALSE;
                        if ((pixbuf = gtk_text_iter_get_pixbuf (&iter))) {
@@ -1210,7 +1278,8 @@ chat_text_view_copy_clipboard (EmpathyChatView *view)
                }
        }
 
-       gtk_clipboard_set_text (clipboard, g_string_free (str, FALSE), str->len);
+       gtk_clipboard_set_text (clipboard, str->str, str->len);
+       g_string_free (str, TRUE);
 }
 
 static void
@@ -1254,109 +1323,99 @@ empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
 }
 
 static void
-chat_text_view_insert_text_with_emoticons (EmpathyChatTextView *view,
-                                          GtkTextIter         *iter,
-                                          const gchar         *str)
+chat_text_view_replace_link (const gchar *text,
+                            gssize len,
+                            gpointer match_data,
+                            gpointer user_data)
 {
-       EmpathyChatTextViewPriv *priv = GET_PRIV (view);
-       gboolean                 use_smileys = FALSE;
-       GSList                  *smileys, *l;
+       GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
+       GtkTextIter iter;
 
-       empathy_conf_get_bool (empathy_conf_get (),
-                              EMPATHY_PREFS_CHAT_SHOW_SMILEYS,
-                              &use_smileys);
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
+                                                 text, len,
+                                                 EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
+                                                 NULL);
+}
 
-       if (!use_smileys) {
-               gtk_text_buffer_insert (priv->buffer, iter, str, -1);
-               return;
-       }
+static void
+chat_text_view_replace_smiley (const gchar *text,
+                              gssize len,
+                              gpointer match_data,
+                              gpointer user_data)
+{
+       EmpathySmileyHit *hit = match_data;
+       GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
+       GtkTextIter iter;
 
-       smileys = empathy_smiley_manager_parse (priv->smiley_manager, str);
-       for (l = smileys; l; l = l->next) {
-               EmpathySmiley *smiley;
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert_pixbuf (buffer, &iter, hit->pixbuf);
+}
 
-               smiley = l->data;
-               if (smiley->pixbuf) {
-                       gtk_text_buffer_insert_pixbuf (priv->buffer, iter, smiley->pixbuf);
-               } else {
-                       gtk_text_buffer_insert (priv->buffer, iter, smiley->str, -1);
-               }
-               empathy_smiley_free (smiley);
-       }
-       g_slist_free (smileys);
+static void
+chat_text_view_replace_verbatim (const gchar *text,
+                                gssize len,
+                                gpointer match_data,
+                                gpointer user_data)
+{
+       GtkTextBuffer *buffer = GTK_TEXT_BUFFER (user_data);
+       GtkTextIter iter;
+
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert (buffer, &iter, text, len);
 }
 
+static EmpathyStringParser string_parsers[] = {
+       {empathy_string_match_link, chat_text_view_replace_link},
+       {empathy_string_match_all, chat_text_view_replace_verbatim},
+       {NULL, NULL}
+};
+
+static EmpathyStringParser string_parsers_with_smiley[] = {
+       {empathy_string_match_link, chat_text_view_replace_link},
+       {empathy_string_match_smiley, chat_text_view_replace_smiley},
+       {empathy_string_match_all, chat_text_view_replace_verbatim},
+       {NULL, NULL}
+};
+
 void
 empathy_chat_text_view_append_body (EmpathyChatTextView *view,
                                    const gchar         *body,
                                    const gchar         *tag)
 {
        EmpathyChatTextViewPriv *priv = GET_PRIV (view);
-       GtkTextIter              start_iter, end_iter;
-       GtkTextMark             *mark;
+       EmpathyStringParser     *parsers;
+       gboolean                 use_smileys;
+       GtkTextIter              start_iter;
        GtkTextIter              iter;
-       GRegex                  *uri_regex;
-       GMatchInfo              *match_info;
-       gboolean                 match;
-       gint                     last = 0;
-       gint                     s = 0, e = 0;
-       gchar                   *tmp;
+       GtkTextMark             *mark;
 
-       priv = GET_PRIV (view);
+       /* 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;
 
+       /* Create a mark at the place we'll start inserting */
        gtk_text_buffer_get_end_iter (priv->buffer, &start_iter);
        mark = gtk_text_buffer_create_mark (priv->buffer, NULL, &start_iter, TRUE);
 
-       uri_regex = empathy_uri_regex_dup_singleton ();
-       for (match = g_regex_match (uri_regex, body, 0, &match_info); match;
-            match = g_match_info_next (match_info, NULL)) {
-               if (!g_match_info_fetch_pos (match_info, 0, &s, &e))
-                       continue;
-
-               if (s > last) {
-                       tmp = empathy_substring (body, last, s);
-
-                       gtk_text_buffer_get_end_iter (priv->buffer, &iter);
-                       chat_text_view_insert_text_with_emoticons (view,
-                                                                  &iter,
-                                                                  tmp);
-                       g_free (tmp);
-               }
-
-               tmp = empathy_substring (body, s, e);
-
-               gtk_text_buffer_get_end_iter (priv->buffer, &iter);
-               gtk_text_buffer_insert_with_tags_by_name (priv->buffer,
-                                                         &iter,
-                                                         tmp,
-                                                         -1,
-                                                         EMPATHY_CHAT_TEXT_VIEW_TAG_LINK,
-                                                         NULL);
-
-               g_free (tmp);
-               last = e;
-       }
-       g_match_info_free (match_info);
-       g_regex_unref (uri_regex);
-
-       if (last < strlen (body)) {
-               gtk_text_buffer_get_end_iter (priv->buffer, &iter);
-               chat_text_view_insert_text_with_emoticons (view,
-                                                          &iter,
-                                                          body + last);
-       }
+       /* Parse text for links/smileys and insert in the buffer */
+       empathy_string_parser_substr (body, -1, parsers, priv->buffer);
 
+       /* Insert a newline after the text inserted */
        gtk_text_buffer_get_end_iter (priv->buffer, &iter);
        gtk_text_buffer_insert (priv->buffer, &iter, "\n", 1);
 
        /* Apply the style to the inserted text. */
        gtk_text_buffer_get_iter_at_mark (priv->buffer, &start_iter, mark);
-       gtk_text_buffer_get_end_iter (priv->buffer, &end_iter);
-
-       gtk_text_buffer_apply_tag_by_name (priv->buffer,
-                                          tag,
+       gtk_text_buffer_get_end_iter (priv->buffer, &iter);
+       gtk_text_buffer_apply_tag_by_name (priv->buffer, tag,
                                           &start_iter,
-                                          &end_iter);
+                                          &iter);
 
        gtk_text_buffer_delete_mark (priv->buffer, mark);
 }