From 6d3c3b4e7ae6c3d9fc7328607e903938d5679395 Mon Sep 17 00:00:00 2001 From: Jonny Lamb Date: Fri, 6 Mar 2009 11:52:48 +0000 Subject: Replaced get_last_messages with get_filtered_messages. Signed-off-by: Jonny Lamb svn path=/trunk/; revision=2605 --- libempathy/empathy-log-manager.c | 75 +++++++++++++++++++++++++++++----------- libempathy/empathy-log-manager.h | 8 +++-- 2 files changed, 60 insertions(+), 23 deletions(-) (limited to 'libempathy') diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c index 30c31f92a..b8a346268 100644 --- a/libempathy/empathy-log-manager.c +++ b/libempathy/empathy-log-manager.c @@ -262,41 +262,74 @@ empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager, return out; } +static gint +log_manager_sort_message_by_date (gconstpointer a, + gconstpointer b) +{ + EmpathyMessage *one = (EmpathyMessage *) a; + EmpathyMessage *two = (EmpathyMessage *) b; + time_t one_time, two_time; + gint ret = 0; + + one_time = empathy_message_get_timestamp (one); + two_time = empathy_message_get_timestamp (two); + + if (one_time < two_time) { + ret = -1; + } else if (one_time == two_time) { + ret = 0; + } else if (one_time > two_time) { + ret = 1; + } + + return ret; +} + GList * -empathy_log_manager_get_last_messages (EmpathyLogManager *manager, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) +empathy_log_manager_get_filtered_messages (EmpathyLogManager *manager, + McAccount *account, + const gchar *chat_id, + gboolean chatroom, + guint num_messages, + EmpathyLogMessageFilter filter, + gpointer user_data) { - GList *messages = NULL; - GList *dates; + EmpathyLogManagerPriv *priv; + GList *out = NULL; GList *l; + guint out_size, i; g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL); g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL); g_return_val_if_fail (chat_id != NULL, NULL); - dates = empathy_log_manager_get_dates (manager, account, chat_id, chatroom); + priv = GET_PRIV (manager); - l = g_list_last (dates); - if (l) + /* Get num_messages from each log store */ + for (l = priv->stores; l; l = l->next) { - messages = empathy_log_manager_get_messages_for_date (manager, account, - chat_id, chatroom, l->data); + EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data); - /* The latest date will always be today as messages are logged immediately. */ - l = g_list_previous (l); - if (l) - { - messages = g_list_concat (messages, empathy_log_manager_get_messages_for_date ( - manager, account, chat_id, chatroom, l->data)); - } + out = g_list_concat (out, empathy_log_store_get_filtered_messages (store, + account, chat_id, chatroom, num_messages, filter, user_data)); } - g_list_foreach (dates, (GFunc) g_free, NULL); - g_list_free (dates); + /* Sort the list by time */ + out = g_list_sort (out, log_manager_sort_message_by_date); + + /* Cut list down to num_messages length */ + out_size = g_list_length (out); + + for (i = 0; out_size - i > num_messages; i++) + { + EmpathyMessage *message; - return messages; + message = out->data; + out = g_list_remove (out, message); + g_object_unref (message); + } + + return out; } GList * diff --git a/libempathy/empathy-log-manager.h b/libempathy/empathy-log-manager.h index 4ec2c9019..f6a9290bd 100644 --- a/libempathy/empathy-log-manager.h +++ b/libempathy/empathy-log-manager.h @@ -72,6 +72,9 @@ struct _EmpathyLogSearchHit gchar *date; }; +typedef gboolean (*EmpathyLogMessageFilter) (EmpathyMessage *message, + gpointer user_data); + GType empathy_log_manager_get_type (void) G_GNUC_CONST; EmpathyLogManager *empathy_log_manager_dup_singleton (void); gboolean empathy_log_manager_add_message (EmpathyLogManager *manager, @@ -84,8 +87,9 @@ GList *empathy_log_manager_get_dates (EmpathyLogManager *manager, GList *empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager, McAccount *account, const gchar *chat_id, gboolean chatroom, const gchar *date); -GList *empathy_log_manager_get_last_messages (EmpathyLogManager *manager, - McAccount *account, const gchar *chat_id, gboolean chatroom); +GList *empathy_log_manager_get_filtered_messages (EmpathyLogManager *manager, + McAccount *account, const gchar *chat_id, gboolean chatroom, + guint num_messages, EmpathyLogMessageFilter filter, gpointer user_data); GList *empathy_log_manager_get_chats (EmpathyLogManager *manager, McAccount *account); GList *empathy_log_manager_search_new (EmpathyLogManager *manager, -- cgit v1.2.3