]> git.0d.be Git - empathy.git/blobdiff - libempathy-gtk/empathy-chat-text-view.c
individual_view_drag_end: remove the auto scroll
[empathy.git] / libempathy-gtk / empathy-chat-text-view.c
index bdf66333ee6f1c2d7967042f5b4b9bbde136466e..60d642ccb7bb84c9e7c1f0175980023f4eb3f978 100644 (file)
 #include <gtk/gtk.h>
 
 #include <telepathy-glib/util.h>
-#include <libmissioncontrol/mc-account.h>
 
+#include <libempathy/empathy-gsettings.h>
 #include <libempathy/empathy-utils.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>
 
 /* Number of seconds between timestamps when using normal mode, 5 minutes. */
-#define TIMESTAMP_INTERVAL 300
+#define TIMESTAMP_INTERVAL (5 * G_TIME_SPAN_MINUTE)
 
 #define MAX_LINES 800
 #define MAX_SCROLL_TIME 0.4 /* seconds */
@@ -65,15 +65,19 @@ typedef struct {
        gboolean              find_wrapped;
        gboolean              find_last_direction;
        EmpathyContact       *last_contact;
-       time_t                last_timestamp;
+       gint64                last_timestamp;
        gboolean              allow_scrolling;
        guint                 notify_system_fonts_id;
+       GSettings            *gsettings_desktop;
+       GSettings            *gsettings_chat;
        EmpathySmileyManager *smiley_manager;
        gboolean              only_if_date;
 } EmpathyChatTextViewPriv;
 
 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,
@@ -147,7 +151,7 @@ chat_text_view_event_cb (EmpathyChatTextView *view,
                return FALSE;
        }
 
-       gdk_window_get_pointer (win, &x, &y, NULL);
+       gdk_window_get_device_position (win, event->device, &x, &y, NULL);
 
        /* Get the iter where the cursor is at */
        gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view), type,
@@ -201,19 +205,21 @@ chat_text_view_create_tags (EmpathyChatTextView *view)
 static void
 chat_text_view_system_font_update (EmpathyChatTextView *view)
 {
+       EmpathyChatTextViewPriv *priv = GET_PRIV (view);
        PangoFontDescription *font_description = NULL;
        gchar                *font_name;
 
-       if (empathy_conf_get_string (empathy_conf_get (),
-                                    "/desktop/gnome/interface/document_font_name",
-                                    &font_name) && font_name) {
-                                            font_description = pango_font_description_from_string (font_name);
-                                            g_free (font_name);
-                                    } else {
-                                            font_description = NULL;
-                                    }
+       font_name = g_settings_get_string (priv->gsettings_desktop,
+                       EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME);
+
+       if (font_name != NULL) {
+               font_description = pango_font_description_from_string (font_name);
+               g_free (font_name);
+       } else {
+               font_description = NULL;
+       }
 
-       gtk_widget_modify_font (GTK_WIDGET (view), font_description);
+       gtk_widget_override_font (GTK_WIDGET (view), font_description);
 
        if (font_description) {
                pango_font_description_free (font_description);
@@ -221,13 +227,11 @@ chat_text_view_system_font_update (EmpathyChatTextView *view)
 }
 
 static void
-chat_text_view_notify_system_font_cb (EmpathyConf *conf,
+chat_text_view_notify_system_font_cb (GSettings *gsettings,
                                      const gchar *key,
-                                     gpointer     user_data)
+                                     EmpathyChatTextView *self)
 {
-       EmpathyChatTextView *view = user_data;
-
-       chat_text_view_system_font_update (view);
+       chat_text_view_system_font_update (self);
 }
 
 static void
@@ -266,11 +270,11 @@ chat_text_view_populate_popup (EmpathyChatTextView *view,
        /* Clear menu item */
        if (gtk_text_buffer_get_char_count (priv->buffer) > 0) {
                item = gtk_separator_menu_item_new ();
-               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
+               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
                gtk_widget_show (item);
 
                item = gtk_image_menu_item_new_from_stock (GTK_STOCK_CLEAR, NULL);
-               gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), item);
+               gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
                gtk_widget_show (item);
 
                g_signal_connect_swapped (item, "activate",
@@ -282,7 +286,9 @@ chat_text_view_populate_popup (EmpathyChatTextView *view,
        table = gtk_text_buffer_get_tag_table (priv->buffer);
        tag = gtk_text_tag_table_lookup (table, EMPATHY_CHAT_TEXT_VIEW_TAG_LINK);
 
-       gtk_widget_get_pointer (GTK_WIDGET (view), &x, &y);
+       gdk_window_get_device_position (gtk_widget_get_window (GTK_WIDGET (view)),
+               gdk_device_manager_get_client_pointer (gdk_display_get_device_manager (
+                       gtk_widget_get_display (GTK_WIDGET (view)))), &x, &y, NULL);
 
        gtk_text_view_window_to_buffer_coords (GTK_TEXT_VIEW (view),
                                               GTK_TEXT_WINDOW_WIDGET,
@@ -331,23 +337,18 @@ chat_text_view_populate_popup (EmpathyChatTextView *view,
 static gboolean
 chat_text_view_is_scrolled_down (EmpathyChatTextView *view)
 {
-       GtkWidget *sw;
-
-       sw = gtk_widget_get_parent (GTK_WIDGET (view));
-       if (GTK_IS_SCROLLED_WINDOW (sw)) {
-               GtkAdjustment *vadj;
-               gdouble value;
-               gdouble upper;
-               gdouble page_size;
-
-               vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (sw));
-               value = gtk_adjustment_get_value (vadj);
-               upper = gtk_adjustment_get_upper (vadj);
-               page_size = gtk_adjustment_get_page_size (vadj);
-
-               if (value < upper - page_size) {
-                       return FALSE;
-               }
+       GtkAdjustment *vadj;
+       gdouble value;
+       gdouble upper;
+       gdouble page_size;
+
+       vadj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
+       value = gtk_adjustment_get_value (vadj);
+       upper = gtk_adjustment_get_upper (vadj);
+       page_size = gtk_adjustment_get_page_size (vadj);
+
+       if (value < upper - page_size) {
+               return FALSE;
        }
 
        return TRUE;
@@ -359,7 +360,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 +372,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;
        }
 
@@ -399,7 +400,7 @@ chat_text_view_maybe_trim_buffer (EmpathyChatTextView *view)
 
 static void
 chat_text_view_append_timestamp (EmpathyChatTextView *view,
-                                time_t               timestamp,
+                                gint64               timestamp,
                                 gboolean             show_date)
 {
        EmpathyChatTextViewPriv *priv = GET_PRIV (view);
@@ -411,17 +412,12 @@ chat_text_view_append_timestamp (EmpathyChatTextView *view,
 
        /* Append date if needed */
        if (show_date) {
-               GDate *date;
-               gchar  buf[256];
-
-               date = g_date_new ();
-               g_date_set_time_t (date, timestamp);
                /* Translators: timestamp displayed between conversations in
                 * chat windows (strftime format string) */
-               g_date_strftime (buf, 256, _("%A %B %d %Y"), date);
-               g_string_append (str, buf);
+               tmp = empathy_time_to_string_utc (timestamp, _("%A %B %d %Y"));
+               g_string_append (str, tmp);
                g_string_append (str, ", ");
-               g_date_free (date);
+               g_free (tmp);
        }
 
        /* Append time */
@@ -440,39 +436,37 @@ chat_text_view_append_timestamp (EmpathyChatTextView *view,
                                                  EMPATHY_CHAT_TEXT_VIEW_TAG_TIME,
                                                  NULL);
 
-       priv->last_timestamp = timestamp;
-
        g_string_free (str, TRUE);
 }
 
 static void
 chat_text_maybe_append_date_and_time (EmpathyChatTextView *view,
-                                     time_t               timestamp)
+                                     gint64               timestamp)
 {
        EmpathyChatTextViewPriv *priv = GET_PRIV (view);
-       GDate                   *date, *last_date;
+       GDateTime               *date, *last_date;
        gboolean                 append_date = FALSE;
        gboolean                 append_time = FALSE;
+       GTimeSpan                delta;
 
        /* Get the date from last message */
-       last_date = g_date_new ();
-       g_date_set_time_t (last_date, priv->last_timestamp);
+       last_date = g_date_time_new_from_unix_utc (priv->last_timestamp);
 
        /* Get the date of the message we are appending */
-       date = g_date_new ();
-       g_date_set_time_t (date, timestamp);
+       date = g_date_time_new_from_unix_utc (timestamp);
 
+       delta = g_date_time_difference (date, last_date);
        /* If last message was from another day we append date and time */
-       if (g_date_compare (date, last_date) > 0) {
+       if (delta >= G_TIME_SPAN_DAY) {
                append_date = TRUE;
                append_time = TRUE;
        }
 
-       g_date_free (last_date);
-       g_date_free (date);
+       g_date_time_unref (last_date);
+       g_date_time_unref (date);
 
        /* If last message is 'old' append the time */
-       if (timestamp - priv->last_timestamp >= TIMESTAMP_INTERVAL) {
+       if (delta >= TIMESTAMP_INTERVAL) {
                append_time = TRUE;
        }
 
@@ -494,8 +488,10 @@ chat_text_view_size_allocate (GtkWidget     *widget,
        if (down) {
                GtkAdjustment *adj;
 
-               adj = GTK_TEXT_VIEW (widget)->vadjustment;
-               gtk_adjustment_set_value (adj, adj->upper - adj->page_size);
+               adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (widget));
+               gtk_adjustment_set_value (adj,
+                                         gtk_adjustment_get_upper (adj) -
+                                         gtk_adjustment_get_page_size (adj));
        }
 }
 
@@ -504,7 +500,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. */
@@ -562,7 +558,8 @@ chat_text_view_finalize (GObject *object)
 
        DEBUG ("%p", object);
 
-       empathy_conf_notify_remove (empathy_conf_get (), priv->notify_system_fonts_id);
+       g_object_unref (priv->gsettings_chat);
+       g_object_unref (priv->gsettings_desktop);
 
        if (priv->last_contact) {
                g_object_unref (priv->last_contact);
@@ -578,11 +575,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;
@@ -591,6 +595,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",
@@ -628,11 +634,14 @@ empathy_chat_text_view_init (EmpathyChatTextView *view)
                      "cursor-visible", FALSE,
                      NULL);
 
-       priv->notify_system_fonts_id =
-               empathy_conf_notify_add (empathy_conf_get (),
-                                        "/desktop/gnome/interface/document_font_name",
-                                        chat_text_view_notify_system_font_cb,
-                                        view);
+       priv->gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
+
+       priv->gsettings_desktop = g_settings_new (
+                         EMPATHY_PREFS_DESKTOP_INTERFACE_SCHEMA);
+       g_signal_connect (priv->gsettings_desktop,
+                         "changed::" EMPATHY_PREFS_DESKTOP_INTERFACE_DOCUMENT_FONT_NAME,
+                         G_CALLBACK (chat_text_view_notify_system_font_cb),
+                         view);
        chat_text_view_system_font_update (view);
        chat_text_view_create_tags (view);
 
@@ -642,6 +651,18 @@ empathy_chat_text_view_init (EmpathyChatTextView *view)
                          NULL);
 }
 
+static void
+chat_text_view_scroll_stop (EmpathyChatTextView *view)
+{
+       EmpathyChatTextViewPriv *priv = GET_PRIV (view);
+
+       g_timer_destroy (priv->scroll_time);
+       priv->scroll_time = NULL;
+
+       g_source_remove (priv->scroll_timeout);
+       priv->scroll_timeout = 0;
+}
+
 /* Code stolen from pidgin/gtkimhtml.c */
 static gboolean
 chat_text_view_scroll_cb (EmpathyChatTextView *view)
@@ -651,17 +672,16 @@ chat_text_view_scroll_cb (EmpathyChatTextView *view)
        gdouble             max_val;
 
        priv = GET_PRIV (view);
-       adj = GTK_TEXT_VIEW (view)->vadjustment;
-       max_val = adj->upper - adj->page_size;
+
+       adj = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (view));
+       max_val = gtk_adjustment_get_upper (adj) - gtk_adjustment_get_page_size (adj);
 
        g_return_val_if_fail (priv->scroll_time != NULL, FALSE);
 
        if (g_timer_elapsed (priv->scroll_time, NULL) > MAX_SCROLL_TIME) {
                /* time's up. jump to the end and kill the timer */
                gtk_adjustment_set_value (adj, max_val);
-               g_timer_destroy (priv->scroll_time);
-               priv->scroll_time = NULL;
-               priv->scroll_timeout = 0;
+               chat_text_view_scroll_stop (view);
                return FALSE;
        }
 
@@ -697,12 +717,13 @@ chat_text_view_scroll_down (EmpathyChatView *view)
 
 static void
 chat_text_view_append_message (EmpathyChatView *view,
-                              EmpathyMessage  *msg)
+                              EmpathyMessage  *msg,
+                              gboolean         should_highlight)
 {
        EmpathyChatTextView     *text_view = EMPATHY_CHAT_TEXT_VIEW (view);
        EmpathyChatTextViewPriv *priv = GET_PRIV (text_view);
        gboolean                 bottom;
-       time_t                   timestamp;
+       gint64                   timestamp;
 
        g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
        g_return_if_fail (EMPATHY_IS_MESSAGE (msg));
@@ -719,7 +740,8 @@ chat_text_view_append_message (EmpathyChatView *view,
        chat_text_maybe_append_date_and_time (text_view, timestamp);
        if (EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message) {
                EMPATHY_CHAT_TEXT_VIEW_GET_CLASS (view)->append_message (text_view,
-                                                                        msg);
+                                                                        msg,
+                                                                        should_highlight);
        }
 
        if (bottom) {
@@ -731,6 +753,8 @@ chat_text_view_append_message (EmpathyChatView *view,
        }
        priv->last_contact = g_object_ref (empathy_message_get_sender (msg));
        g_object_notify (G_OBJECT (view), "last-contact");
+
+       priv->last_timestamp = timestamp;
 }
 
 static void
@@ -826,7 +850,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;
@@ -868,7 +893,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);
@@ -879,11 +904,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;
@@ -897,7 +934,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;
                }
 
@@ -941,7 +979,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;
@@ -983,7 +1022,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);
@@ -994,11 +1033,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;
@@ -1012,7 +1063,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;
                }
 
@@ -1056,6 +1108,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)
 {
@@ -1082,11 +1135,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) {
@@ -1098,17 +1160,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;
@@ -1134,12 +1206,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;
                }
@@ -1149,7 +1229,6 @@ chat_text_view_highlight (EmpathyChatView *view,
                                                   &iter_match_end);
 
                iter = iter_match_end;
-               gtk_text_iter_forward_char (&iter);
        }
 }
 
@@ -1157,14 +1236,61 @@ static void
 chat_text_view_copy_clipboard (EmpathyChatView *view)
 {
        GtkTextBuffer *buffer;
+       GtkTextIter start, iter, end;
        GtkClipboard  *clipboard;
+       GdkPixbuf *pixbuf;
+       gunichar c;
+       GtkTextChildAnchor *anchor = NULL;
+       GString *str;
+       GList *list;
+       gboolean ignore_newlines = FALSE;
 
        g_return_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view));
 
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
        clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
 
-       gtk_text_buffer_copy_clipboard (buffer, clipboard);
+       if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
+               return;
+
+       str = g_string_new ("");
+
+       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))) {
+                               gchar *text;
+                               text = g_object_get_data (G_OBJECT(pixbuf),
+                                                         "smiley_str");
+                               if (text)
+                                       str = g_string_append (str, text);
+                       } else if ((anchor = gtk_text_iter_get_child_anchor (&iter))) {
+                               gchar *text;
+                               list = gtk_text_child_anchor_get_widgets (anchor);
+                               if (list) {
+                                       text = g_object_get_data (G_OBJECT(list->data),
+                                                                 "str_obj");
+                                       if (text)
+                                               str = g_string_append (str, text);
+                               }
+                               g_list_free (list);
+                       }
+               } else if (c == '\n') {
+                       if (!ignore_newlines) {
+                               ignore_newlines = TRUE;
+                               str = g_string_append_unichar (str, c);
+                       }
+               } else {
+                       ignore_newlines = FALSE;
+                       str = g_string_append_unichar (str, c);
+               }
+       }
+
+       gtk_clipboard_set_text (clipboard, str->str, str->len);
+       g_string_free (str, TRUE);
 }
 
 static void
@@ -1193,6 +1319,16 @@ empathy_chat_text_view_get_last_contact (EmpathyChatTextView *view)
        return priv->last_contact;
 }
 
+gint64
+empathy_chat_text_view_get_last_timestamp (EmpathyChatTextView *view)
+{
+       EmpathyChatTextViewPriv *priv = GET_PRIV (view);
+
+       g_return_val_if_fail (EMPATHY_IS_CHAT_TEXT_VIEW (view), 0);
+
+       return priv->last_timestamp;
+}
+
 void
 empathy_chat_text_view_set_only_if_date (EmpathyChatTextView *view,
                                         gboolean             only_if_date)
@@ -1208,109 +1344,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 */
+       use_smileys = g_settings_get_boolean (priv->gsettings_chat,
+                       EMPATHY_PREFS_CHAT_SHOW_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);
 }