From: Will Thompson Date: Wed, 18 Jan 2012 16:31:28 +0000 (+0000) Subject: Move empathy_message_should_highlight to EmpathyChat X-Git-Url: https://git.0d.be/?p=empathy.git;a=commitdiff_plain;h=b12ce233dce1dcd581b34ea9944e36e59f511497 Move empathy_message_should_highlight to EmpathyChat 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!) --- diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c index 267cdca2..ea46e6b7 100644 --- a/libempathy-gtk/empathy-chat.c +++ b/libempathy-gtk/empathy-chat.c @@ -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); diff --git a/libempathy/empathy-message.c b/libempathy/empathy-message.c index 9841cbb6..6111bcd8 100644 --- a/libempathy/empathy-message.c +++ b/libempathy/empathy-message.c @@ -39,9 +39,6 @@ # include #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) { diff --git a/libempathy/empathy-message.h b/libempathy/empathy-message.h index 0c27c09e..81da7bf4 100644 --- a/libempathy/empathy-message.h +++ b/libempathy/empathy-message.h @@ -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);