]> git.0d.be Git - empathy.git/commitdiff
Move empathy_message_should_highlight to EmpathyChat
authorWill Thompson <will@willthompson.co.uk>
Wed, 18 Jan 2012 16:31:28 +0000 (16:31 +0000)
committerWill Thompson <will@willthompson.co.uk>
Wed, 18 Jan 2012 17:55:08 +0000 (17:55 +0000)
This will give us a place to cache the GRegex object. (Of course, this
also depends on monitoring changes to the TpChat's self contact's alias,
and changes to the TpChat's self contact!)

libempathy-gtk/empathy-chat.c
libempathy/empathy-message.c
libempathy/empathy-message.h

index 267cdca2ae10a3ac860ceac7de8bddfaf1b3ba54..ea46e6b768e2c215088dc3087269a9d623bfa533 100644 (file)
@@ -1383,6 +1383,74 @@ chat_state_changed_cb (EmpathyTpChat      *tp_chat,
        }
 }
 
+static GRegex *
+get_highlight_regex_for (const gchar *name)
+{
+       GRegex *regex;
+       gchar *name_esc, *pattern;
+       GError *error = NULL;
+
+       name_esc = g_regex_escape_string (name, -1);
+       pattern = g_strdup_printf ("\\b%s\\b", name_esc);
+       regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
+               &error);
+
+       if (regex == NULL) {
+               DEBUG ("couldn't compile regex /%s/: %s", pattern,
+                       error->message);
+
+               g_error_free (error);
+       }
+
+       g_free (pattern);
+       g_free (name_esc);
+
+       return regex;
+}
+
+static gboolean
+chat_should_highlight (EmpathyChat *chat,
+       EmpathyMessage *message)
+{
+       EmpathyContact *contact;
+       const gchar   *msg, *to;
+       gboolean       ret_val = FALSE;
+       TpChannelTextMessageFlags flags;
+       GRegex *regex;
+
+       g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+
+       msg = empathy_message_get_body (message);
+       if (!msg) {
+               return FALSE;
+       }
+
+       contact = empathy_tp_chat_get_self_contact (chat->priv->tp_chat);
+       if (!contact) {
+               return FALSE;
+       }
+
+       to = empathy_contact_get_alias (contact);
+       if (!to) {
+               return FALSE;
+       }
+
+       flags = empathy_message_get_flags (message);
+       if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
+               /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
+                * have already been received by the user before (and so are in the logs) */
+               return FALSE;
+       }
+
+       regex = get_highlight_regex_for (to);
+       if (regex != NULL) {
+               ret_val = g_regex_match (regex, msg, 0, NULL);
+               g_regex_unref (regex);
+       }
+
+       return ret_val;
+}
+
 static void
 chat_message_received (EmpathyChat *chat,
        EmpathyMessage *message,
@@ -1400,7 +1468,7 @@ chat_message_received (EmpathyChat *chat,
 
                empathy_chat_view_edit_message (chat->view, message);
        } else {
-               gboolean should_highlight = empathy_message_should_highlight (message);
+               gboolean should_highlight = chat_should_highlight (chat, message);
                DEBUG ("Appending new message '%s' from %s (%d)",
                        empathy_message_get_token (message),
                        empathy_contact_get_alias (sender),
@@ -2494,14 +2562,14 @@ got_filtered_messages_cb (GObject *manager,
                                NULL);
 
                        empathy_chat_view_append_message (chat->view, syn_msg,
-                                                         empathy_message_should_highlight (syn_msg));
+                                                         chat_should_highlight (chat, syn_msg));
                        empathy_chat_view_edit_message (chat->view, message);
 
                        g_object_unref (syn_msg);
                } else {
                        /* append the latest message */
                        empathy_chat_view_append_message (chat->view, message,
-                                                         empathy_message_should_highlight (message));
+                                                         chat_should_highlight (chat, message));
                }
 
                g_object_unref (message);
index 9841cbb618f24e3e7246be3eddf7d101aca0895c..6111bcd8c25ace3f783f840325353b111c62a964 100644 (file)
@@ -39,9 +39,6 @@
 # include <telepathy-logger/call-event.h>
 #endif
 
-#define DEBUG_FLAG EMPATHY_DEBUG_CHAT
-#include "empathy-debug.h"
-
 #include "empathy-client-factory.h"
 #include "empathy-message.h"
 #include "empathy-utils.h"
@@ -636,73 +633,6 @@ empathy_message_is_backlog (EmpathyMessage *message)
        return priv->is_backlog;
 }
 
-static GRegex *
-get_highlight_regex_for (const gchar *name)
-{
-       GRegex *regex;
-       gchar *name_esc, *pattern;
-       GError *error = NULL;
-
-       name_esc = g_regex_escape_string (name, -1);
-       pattern = g_strdup_printf ("\\b%s\\b", name_esc);
-       regex = g_regex_new (pattern, G_REGEX_CASELESS | G_REGEX_OPTIMIZE, 0,
-               &error);
-
-       if (regex == NULL) {
-               DEBUG ("couldn't compile regex /%s/: %s", pattern,
-                       error->message);
-
-               g_error_free (error);
-       }
-
-       g_free (pattern);
-       g_free (name_esc);
-
-       return regex;
-}
-
-gboolean
-empathy_message_should_highlight (EmpathyMessage *message)
-{
-       EmpathyContact *contact;
-       const gchar   *msg, *to;
-       gboolean       ret_val = FALSE;
-       TpChannelTextMessageFlags flags;
-       GRegex *regex;
-
-       g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
-
-       msg = empathy_message_get_body (message);
-       if (!msg) {
-               return FALSE;
-       }
-
-       contact = empathy_message_get_receiver (message);
-       if (!contact || !empathy_contact_is_user (contact)) {
-               return FALSE;
-       }
-
-       to = empathy_contact_get_alias (contact);
-       if (!to) {
-               return FALSE;
-       }
-
-       flags = empathy_message_get_flags (message);
-       if (flags & TP_CHANNEL_TEXT_MESSAGE_FLAG_SCROLLBACK) {
-               /* FIXME: Ideally we shouldn't highlight scrollback messages only if they
-                * have already been received by the user before (and so are in the logs) */
-               return FALSE;
-       }
-
-       regex = get_highlight_regex_for (to);
-       if (regex != NULL) {
-               ret_val = g_regex_match (regex, msg, 0, NULL);
-               g_regex_unref (regex);
-       }
-
-       return ret_val;
-}
-
 TpChannelTextMessageType
 empathy_message_type_from_str (const gchar *type_str)
 {
index 0c27c09e99f6c0a88073fb24932a190608d43967..81da7bf43ba257d02486a9602022f704727ece62 100644 (file)
@@ -77,7 +77,6 @@ gint64                   empathy_message_get_original_timestamp (EmpathyMessage
 gboolean                 empathy_message_is_backlog        (EmpathyMessage           *message);
 gboolean                 empathy_message_is_incoming       (EmpathyMessage           *message);
 
-gboolean                 empathy_message_should_highlight  (EmpathyMessage           *message);
 TpChannelTextMessageType empathy_message_type_from_str     (const gchar              *type_str);
 const gchar *            empathy_message_type_to_str       (TpChannelTextMessageType  type);