From 6b3b79f073995d33a2da2686a7eee70b963037b2 Mon Sep 17 00:00:00 2001 From: Jonny Lamb Date: Fri, 6 Mar 2009 11:52:08 +0000 Subject: Renamed EmpathyLogSource to EmpathyLogStore. Signed-off-by: Jonny Lamb svn path=/trunk/; revision=2593 --- libempathy/Makefile.am | 4 +- libempathy/empathy-log-manager.c | 64 +-- libempathy/empathy-log-source-empathy.c | 714 -------------------------------- libempathy/empathy-log-source.c | 156 ------- libempathy/empathy-log-source.h | 94 ----- libempathy/empathy-log-store-empathy.c | 714 ++++++++++++++++++++++++++++++++ libempathy/empathy-log-store-empathy.h | 66 +++ libempathy/empathy-log-store.c | 156 +++++++ libempathy/empathy-log-store.h | 94 +++++ 9 files changed, 1064 insertions(+), 998 deletions(-) delete mode 100644 libempathy/empathy-log-source-empathy.c delete mode 100644 libempathy/empathy-log-source.c delete mode 100644 libempathy/empathy-log-source.h create mode 100644 libempathy/empathy-log-store-empathy.c create mode 100644 libempathy/empathy-log-store-empathy.h create mode 100644 libempathy/empathy-log-store.c create mode 100644 libempathy/empathy-log-store.h (limited to 'libempathy') diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am index 085c6b999..cffb922da 100644 --- a/libempathy/Makefile.am +++ b/libempathy/Makefile.am @@ -36,8 +36,8 @@ libempathy_la_SOURCES = \ empathy-irc-network-manager.c \ empathy-irc-server.c \ empathy-log-manager.c \ - empathy-log-source.c \ - empathy-log-source-empathy.c \ + empathy-log-store.c \ + empathy-log-store-empathy.c \ empathy-message.c \ empathy-status-presets.c \ empathy-time.c \ diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c index 2b001ac0a..4125add7c 100644 --- a/libempathy/empathy-log-manager.c +++ b/libempathy/empathy-log-manager.c @@ -31,8 +31,8 @@ #include #include "empathy-log-manager.h" -#include "empathy-log-source-empathy.h" -#include "empathy-log-source.h" +#include "empathy-log-store-empathy.h" +#include "empathy-log-store.h" #include "empathy-tp-chat.h" #include "empathy-utils.h" @@ -42,7 +42,7 @@ #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager) typedef struct { - GList *sources; + GList *stores; } EmpathyLogManagerPriv; G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT); @@ -57,13 +57,13 @@ log_manager_finalize (GObject *object) priv = GET_PRIV (object); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - EmpathyLogSource *source = (EmpathyLogSource *) l->data; - g_object_unref (source); + EmpathyLogStore *store = (EmpathyLogStore *) l->data; + g_object_unref (store); } - g_list_free (priv->sources); + g_list_free (priv->stores); } static GObject * @@ -111,8 +111,8 @@ empathy_log_manager_init (EmpathyLogManager *manager) EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager, EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv); - priv->sources = g_list_append (priv->sources, - g_object_new (EMPATHY_TYPE_LOG_SOURCE_EMPATHY, NULL)); + priv->stores = g_list_append (priv->stores, + g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL)); manager->priv = priv; } @@ -135,9 +135,9 @@ empathy_log_manager_add_message (EmpathyLogManager *manager, gboolean out = FALSE; gboolean found = FALSE; - /* TODO: When multiple log sources appear with add_message implementations + /* TODO: When multiple log stores appear with add_message implementations * make this customisable. */ - const gchar *add_source = "Empathy"; + const gchar *add_store = "Empathy"; g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE); g_return_val_if_fail (chat_id != NULL, FALSE); @@ -145,12 +145,12 @@ empathy_log_manager_add_message (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - if (!tp_strdiff (empathy_log_source_get_name ( - EMPATHY_LOG_SOURCE (l->data)), add_source)) + if (!tp_strdiff (empathy_log_store_get_name ( + EMPATHY_LOG_STORE (l->data)), add_store)) { - out = empathy_log_source_add_message (EMPATHY_LOG_SOURCE (l->data), + out = empathy_log_store_add_message (EMPATHY_LOG_STORE (l->data), chat_id, chatroom, message, error); found = TRUE; break; @@ -158,7 +158,7 @@ empathy_log_manager_add_message (EmpathyLogManager *manager, } if (!found) - DEBUG ("Failed to find chosen log source to write to."); + DEBUG ("Failed to find chosen log store to write to."); return out; } @@ -178,9 +178,9 @@ empathy_log_manager_exists (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - if (empathy_log_source_exists (EMPATHY_LOG_SOURCE (l->data), + if (empathy_log_store_exists (EMPATHY_LOG_STORE (l->data), account, chat_id, chatroom)) return TRUE; } @@ -214,15 +214,15 @@ empathy_log_manager_get_dates (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - EmpathyLogSource *source = EMPATHY_LOG_SOURCE (l->data); + EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data); if (!out) - out = empathy_log_source_get_dates (source, account, chat_id, chatroom); + out = empathy_log_store_get_dates (store, account, chat_id, chatroom); else { - GList *new = empathy_log_source_get_dates (source, account, chat_id, + GList *new = empathy_log_store_get_dates (store, account, chat_id, chatroom); g_list_foreach (new, log_manager_get_dates_foreach, out); @@ -253,12 +253,12 @@ empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - EmpathyLogSource *source = EMPATHY_LOG_SOURCE (l->data); + EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data); - out = g_list_concat (out, empathy_log_source_get_messages_for_date ( - source, account, chat_id, chatroom, date)); + out = g_list_concat (out, empathy_log_store_get_messages_for_date ( + store, account, chat_id, chatroom, date)); } return out; @@ -303,12 +303,12 @@ empathy_log_manager_get_chats (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - EmpathyLogSource *source = EMPATHY_LOG_SOURCE (l->data); + EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data); out = g_list_concat (out, - empathy_log_source_get_chats (source, account)); + empathy_log_store_get_chats (store, account)); } return out; @@ -326,12 +326,12 @@ empathy_log_manager_search_new (EmpathyLogManager *manager, priv = GET_PRIV (manager); - for (l = priv->sources; l; l = l->next) + for (l = priv->stores; l; l = l->next) { - EmpathyLogSource *source = EMPATHY_LOG_SOURCE (l->data); + EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data); out = g_list_concat (out, - empathy_log_source_search_new (source, text)); + empathy_log_store_search_new (store, text)); } return out; diff --git a/libempathy/empathy-log-source-empathy.c b/libempathy/empathy-log-source-empathy.c deleted file mode 100644 index baad6c965..000000000 --- a/libempathy/empathy-log-source-empathy.c +++ /dev/null @@ -1,714 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2003-2007 Imendio AB - * Copyright (C) 2007-2008 Collabora Ltd. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * Authors: Xavier Claessens - * Jonny Lamb - */ - -#include - -#include -#include -#include -#include - -#include "empathy-log-source.h" -#include "empathy-log-source-empathy.h" -#include "empathy-log-manager.h" -#include "empathy-contact.h" -#include "empathy-time.h" -#include "empathy-utils.h" - -#define DEBUG_FLAG EMPATHY_DEBUG_OTHER -#include "empathy-debug.h" - -#define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR) -#define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR) -#define LOG_DIR_CHATROOMS "chatrooms" -#define LOG_FILENAME_SUFFIX ".log" -#define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S" -#define LOG_TIME_FORMAT "%Y%m%d" -#define LOG_HEADER \ - "\n" \ - "\n" \ - "\n" - -#define LOG_FOOTER \ - "\n" - - -#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogSourceEmpathy) -typedef struct -{ - gchar *basedir; - gchar *name; -} EmpathyLogSourceEmpathyPriv; - -static void log_source_iface_init (gpointer g_iface,gpointer iface_data); - -G_DEFINE_TYPE_WITH_CODE (EmpathyLogSourceEmpathy, empathy_log_source_empathy, - G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_SOURCE, - log_source_iface_init)); - -static void -log_source_empathy_finalize (GObject *object) -{ - EmpathyLogSourceEmpathy *self = EMPATHY_LOG_SOURCE_EMPATHY (object); - EmpathyLogSourceEmpathyPriv *priv = GET_PRIV (self); - - g_free (priv->basedir); - g_free (priv->name); -} - -static void -empathy_log_source_empathy_class_init (EmpathyLogSourceEmpathyClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->finalize = log_source_empathy_finalize; - - g_type_class_add_private (object_class, sizeof (EmpathyLogSourceEmpathyPriv)); -} - -static void -empathy_log_source_empathy_init (EmpathyLogSourceEmpathy *self) -{ - EmpathyLogSourceEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, - EMPATHY_TYPE_LOG_SOURCE_EMPATHY, EmpathyLogSourceEmpathyPriv); - - self->priv = priv; - - priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), - ".gnome2", PACKAGE_NAME, "logs", NULL); - - priv->name = g_strdup ("Empathy"); -} - -static gchar * -log_source_empathy_get_dir (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - const gchar *account_id; - gchar *basedir; - EmpathyLogSourceEmpathyPriv *priv; - - priv = GET_PRIV (self); - - account_id = mc_account_get_unique_name (account); - - if (chatroom) - basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id, - LOG_DIR_CHATROOMS, chat_id, NULL); - else - basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, - account_id, chat_id, NULL); - - return basedir; -} - -static gchar * -log_source_empathy_get_timestamp_filename (void) -{ - time_t t; - gchar *time_str; - gchar *filename; - - t = empathy_time_get_current (); - time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT); - filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL); - - g_free (time_str); - - return filename; -} - -static gchar * -log_source_empathy_get_timestamp_from_message (EmpathyMessage *message) -{ - time_t t; - - t = empathy_message_get_timestamp (message); - - /* We keep the timestamps in the messages as UTC. */ - return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL); -} - -static gchar * -log_source_empathy_get_filename (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - gchar *basedir; - gchar *timestamp; - gchar *filename; - - basedir = log_source_empathy_get_dir (self, account, chat_id, chatroom); - timestamp = log_source_empathy_get_timestamp_filename (); - filename = g_build_filename (basedir, timestamp, NULL); - - g_free (basedir); - g_free (timestamp); - - return filename; -} - -static gboolean -log_source_empathy_add_message (EmpathyLogSource *self, - const gchar *chat_id, - gboolean chatroom, - EmpathyMessage *message, - GError **error) -{ - FILE *file; - McAccount *account; - EmpathyContact *sender; - const gchar *body_str; - const gchar *str; - EmpathyAvatar *avatar; - gchar *avatar_token = NULL; - gchar *filename; - gchar *basedir; - gchar *body; - gchar *timestamp; - gchar *contact_name; - gchar *contact_id; - TpChannelTextMessageType msg_type; - - g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), FALSE); - g_return_val_if_fail (chat_id != NULL, FALSE); - g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE); - - sender = empathy_message_get_sender (message); - account = empathy_contact_get_account (sender); - body_str = empathy_message_get_body (message); - msg_type = empathy_message_get_tptype (message); - - if (G_STR_EMPTY (body_str)) - return FALSE; - - filename = log_source_empathy_get_filename (self, account, chat_id, chatroom); - basedir = g_path_get_dirname (filename); - if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) - { - DEBUG ("Creating directory:'%s'", basedir); - g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE); - } - g_free (basedir); - - DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename); - - if (!g_file_test (filename, G_FILE_TEST_EXISTS)) - { - file = g_fopen (filename, "w+"); - if (file) - g_fprintf (file, LOG_HEADER); - - g_chmod (filename, LOG_FILE_CREATE_MODE); - } - else - { - file = g_fopen (filename, "r+"); - if (file) - fseek (file, - strlen (LOG_FOOTER), SEEK_END); - } - - body = g_markup_escape_text (body_str, -1); - timestamp = log_source_empathy_get_timestamp_from_message (message); - - str = empathy_contact_get_name (sender); - contact_name = g_markup_escape_text (str, -1); - - str = empathy_contact_get_id (sender); - contact_id = g_markup_escape_text (str, -1); - - avatar = empathy_contact_get_avatar (sender); - if (avatar) - avatar_token = g_markup_escape_text (avatar->token, -1); - - g_fprintf (file, - "" - "%s\n" LOG_FOOTER, timestamp, contact_id, contact_name, - avatar_token ? avatar_token : "", - empathy_contact_is_user (sender) ? "true" : "false", - empathy_message_type_to_str (msg_type), body); - - fclose (file); - g_free (filename); - g_free (contact_id); - g_free (contact_name); - g_free (timestamp); - g_free (body); - g_free (avatar_token); - - return TRUE; -} - -static gboolean -log_source_empathy_exists (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - gchar *dir; - gboolean exists; - - dir = log_source_empathy_get_dir (self, account, chat_id, chatroom); - exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR); - g_free (dir); - - return exists; -} - -static GList * -log_source_empathy_get_dates (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - GList *dates = NULL; - gchar *date; - gchar *directory; - GDir *dir; - const gchar *filename; - const gchar *p; - - g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL); - g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL); - g_return_val_if_fail (chat_id != NULL, NULL); - - directory = log_source_empathy_get_dir (self, account, chat_id, chatroom); - dir = g_dir_open (directory, 0, NULL); - if (!dir) - { - DEBUG ("Could not open directory:'%s'", directory); - g_free (directory); - return NULL; - } - - DEBUG ("Collating a list of dates in:'%s'", directory); - - while ((filename = g_dir_read_name (dir)) != NULL) - { - if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) - continue; - - p = strstr (filename, LOG_FILENAME_SUFFIX); - date = g_strndup (filename, p - filename); - - if (!date) - continue; - - if (!g_regex_match_simple ("\\d{8}", date, 0, 0)) - continue; - - dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp); - } - - g_free (directory); - g_dir_close (dir); - - DEBUG ("Parsed %d dates", g_list_length (dates)); - - return dates; -} - -static gchar * -log_source_empathy_get_filename_for_date (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom, - const gchar *date) -{ - gchar *basedir; - gchar *timestamp; - gchar *filename; - - basedir = log_source_empathy_get_dir (self, account, chat_id, chatroom); - timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL); - filename = g_build_filename (basedir, timestamp, NULL); - - g_free (basedir); - g_free (timestamp); - - return filename; -} - -static EmpathyLogSearchHit * -log_source_empathy_search_hit_new (EmpathyLogSource *self, - const gchar *filename) -{ - EmpathyLogSearchHit *hit; - const gchar *account_name; - const gchar *end; - gchar **strv; - guint len; - - if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) - return NULL; - - strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1); - len = g_strv_length (strv); - - hit = g_slice_new0 (EmpathyLogSearchHit); - - end = strstr (strv[len-1], LOG_FILENAME_SUFFIX); - hit->date = g_strndup (strv[len-1], end - strv[len-1]); - hit->chat_id = g_strdup (strv[len-2]); - hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0); - - if (hit->is_chatroom) - account_name = strv[len-4]; - else - account_name = strv[len-3]; - - hit->account = mc_account_lookup (account_name); - hit->filename = g_strdup (filename); - - g_strfreev (strv); - - return hit; -} - -static GList * -log_source_empathy_get_messages_for_file (EmpathyLogSource *self, - const gchar *filename) -{ - GList *messages = NULL; - xmlParserCtxtPtr ctxt; - xmlDocPtr doc; - xmlNodePtr log_node; - xmlNodePtr node; - EmpathyLogSearchHit *hit; - McAccount *account; - - g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL); - g_return_val_if_fail (filename != NULL, NULL); - - DEBUG ("Attempting to parse filename:'%s'...", filename); - - if (!g_file_test (filename, G_FILE_TEST_EXISTS)) - { - DEBUG ("Filename:'%s' does not exist", filename); - return NULL; - } - - /* Get the account from the filename */ - hit = log_source_empathy_search_hit_new (self, filename); - account = g_object_ref (hit->account); - empathy_log_manager_search_hit_free (hit); - - /* Create parser. */ - ctxt = xmlNewParserCtxt (); - - /* Parse and validate the file. */ - doc = xmlCtxtReadFile (ctxt, filename, NULL, 0); - if (!doc) - { - g_warning ("Failed to parse file:'%s'", filename); - xmlFreeParserCtxt (ctxt); - return NULL; - } - - /* The root node, presets. */ - log_node = xmlDocGetRootElement (doc); - if (!log_node) - { - xmlFreeDoc (doc); - xmlFreeParserCtxt (ctxt); - return NULL; - } - - /* Now get the messages. */ - for (node = log_node->children; node; node = node->next) - { - EmpathyMessage *message; - EmpathyContact *sender; - gchar *time; - time_t t; - gchar *sender_id; - gchar *sender_name; - gchar *sender_avatar_token; - gchar *body; - gchar *is_user_str; - gboolean is_user = FALSE; - gchar *msg_type_str; - TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL; - - if (strcmp (node->name, "message") != 0) - continue; - - body = xmlNodeGetContent (node); - time = xmlGetProp (node, "time"); - sender_id = xmlGetProp (node, "id"); - sender_name = xmlGetProp (node, "name"); - sender_avatar_token = xmlGetProp (node, "token"); - is_user_str = xmlGetProp (node, "isuser"); - msg_type_str = xmlGetProp (node, "type"); - - if (is_user_str) - is_user = strcmp (is_user_str, "true") == 0; - - if (msg_type_str) - msg_type = empathy_message_type_from_str (msg_type_str); - - t = empathy_time_parse (time); - - sender = empathy_contact_new_full (account, sender_id, sender_name); - empathy_contact_set_is_user (sender, is_user); - if (!G_STR_EMPTY (sender_avatar_token)) - empathy_contact_load_avatar_cache (sender, - sender_avatar_token); - - message = empathy_message_new (body); - empathy_message_set_sender (message, sender); - empathy_message_set_timestamp (message, t); - empathy_message_set_tptype (message, msg_type); - - messages = g_list_append (messages, message); - - g_object_unref (sender); - xmlFree (time); - xmlFree (sender_id); - xmlFree (sender_name); - xmlFree (body); - xmlFree (is_user_str); - xmlFree (msg_type_str); - } - - DEBUG ("Parsed %d messages", g_list_length (messages)); - - xmlFreeDoc (doc); - xmlFreeParserCtxt (ctxt); - - return messages; -} - -static GList * -log_source_empathy_get_all_files (EmpathyLogSource *self, - const gchar *dir) -{ - GDir *gdir; - GList *files = NULL; - const gchar *name; - const gchar *basedir; - EmpathyLogSourceEmpathyPriv *priv; - - priv = GET_PRIV (self); - - basedir = dir ? dir : priv->basedir; - - gdir = g_dir_open (basedir, 0, NULL); - if (!gdir) - return NULL; - - while ((name = g_dir_read_name (gdir)) != NULL) - { - gchar *filename; - - filename = g_build_filename (basedir, name, NULL); - if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) - { - files = g_list_prepend (files, filename); - continue; - } - - if (g_file_test (filename, G_FILE_TEST_IS_DIR)) - { - /* Recursively get all log files */ - files = g_list_concat (files, - log_source_empathy_get_all_files (self, filename)); - } - - g_free (filename); - } - - g_dir_close (gdir); - - return files; -} - -static GList * -log_source_empathy_search_new (EmpathyLogSource *self, - const gchar *text) -{ - GList *files, *l; - GList *hits = NULL; - gchar *text_casefold; - - g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL); - g_return_val_if_fail (!G_STR_EMPTY (text), NULL); - - text_casefold = g_utf8_casefold (text, -1); - - files = log_source_empathy_get_all_files (self, NULL); - DEBUG ("Found %d log files in total", g_list_length (files)); - - for (l = files; l; l = l->next) - { - gchar *filename; - GMappedFile *file; - gsize length; - gchar *contents; - gchar *contents_casefold; - - filename = l->data; - - file = g_mapped_file_new (filename, FALSE, NULL); - if (!file) - continue; - - length = g_mapped_file_get_length (file); - contents = g_mapped_file_get_contents (file); - contents_casefold = g_utf8_casefold (contents, length); - - g_mapped_file_free (file); - - if (strstr (contents_casefold, text_casefold)) - { - EmpathyLogSearchHit *hit; - - hit = log_source_empathy_search_hit_new (self, filename); - - if (hit) - { - hits = g_list_prepend (hits, hit); - DEBUG ("Found text:'%s' in file:'%s' on date:'%s'", - text, hit->filename, hit->date); - } - } - - g_free (contents_casefold); - g_free (filename); - } - - g_list_free (files); - g_free (text_casefold); - - return hits; -} - -static GList * -log_source_empathy_get_chats_for_dir (EmpathyLogSource *self, - const gchar *dir, - gboolean is_chatroom) -{ - GDir *gdir; - GList *hits = NULL; - const gchar *name; - - gdir = g_dir_open (dir, 0, NULL); - if (!gdir) - return NULL; - - while ((name = g_dir_read_name (gdir)) != NULL) - { - EmpathyLogSearchHit *hit; - gchar *filename; - - filename = g_build_filename (dir, name, NULL); - if (strcmp (name, LOG_DIR_CHATROOMS) == 0) - { - hits = g_list_concat (hits, log_source_empathy_get_chats_for_dir ( - self, dir, TRUE)); - g_free (filename); - continue; - } - - hit = g_slice_new0 (EmpathyLogSearchHit); - hit->chat_id = g_strdup (name); - hit->is_chatroom = is_chatroom; - - hits = g_list_prepend (hits, hit); - } - - g_dir_close (gdir); - - return hits; -} - - -static GList * -log_source_empathy_get_messages_for_date (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom, - const gchar *date) -{ - gchar *filename; - GList *messages; - - g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL); - g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL); - g_return_val_if_fail (chat_id != NULL, NULL); - - filename = log_source_empathy_get_filename_for_date (self, account, - chat_id, chatroom, date); - messages = log_source_empathy_get_messages_for_file (self, filename); - g_free (filename); - - return messages; -} - -static GList * -log_source_empathy_get_chats (EmpathyLogSource *self, - McAccount *account) -{ - gchar *dir; - GList *hits; - EmpathyLogSourceEmpathyPriv *priv; - - priv = GET_PRIV (self); - - dir = g_build_filename (priv->basedir, - mc_account_get_unique_name (account), NULL); - - hits = log_source_empathy_get_chats_for_dir (self, dir, FALSE); - - g_free (dir); - - return hits; -} - -static const gchar * -log_source_empathy_get_name (EmpathyLogSource *self) -{ - EmpathyLogSourceEmpathyPriv *priv = GET_PRIV (self); - - return priv->name; -} - -static void -log_source_iface_init (gpointer g_iface, - gpointer iface_data) -{ - EmpathyLogSourceInterface *iface = (EmpathyLogSourceInterface *) g_iface; - - iface->get_name = log_source_empathy_get_name; - iface->exists = log_source_empathy_exists; - iface->add_message = log_source_empathy_add_message; - iface->get_dates = log_source_empathy_get_dates; - iface->get_messages_for_date = log_source_empathy_get_messages_for_date; - iface->get_chats = log_source_empathy_get_chats; - iface->search_new = log_source_empathy_search_new; - iface->ack_message = NULL; -} diff --git a/libempathy/empathy-log-source.c b/libempathy/empathy-log-source.c deleted file mode 100644 index f1e37fa79..000000000 --- a/libempathy/empathy-log-source.c +++ /dev/null @@ -1,156 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2008 Collabora Ltd. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * Authors: Jonny Lamb - */ - -#include "empathy-log-source.h" - -GType -empathy_log_source_get_type (void) -{ - static GType type = 0; - if (type == 0) { - static const GTypeInfo info = { - sizeof (EmpathyLogSourceInterface), - NULL, /* base_init */ - NULL, /* base_finalize */ - NULL, /* class_init */ - NULL, /* class_finalize */ - NULL, /* class_data */ - 0, - 0, /* n_preallocs */ - NULL /* instance_init */ - }; - type = g_type_register_static (G_TYPE_INTERFACE, "EmpathyLogSource", - &info, 0); - } - return type; -} - -const gchar * -empathy_log_source_get_name (EmpathyLogSource *self) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_name) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_name (self); -} - -gboolean -empathy_log_source_exists (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->exists) - return FALSE; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->exists ( - self, account, chat_id, chatroom); -} - - - -gboolean -empathy_log_source_add_message (EmpathyLogSource *self, - const gchar *chat_id, - gboolean chatroom, - EmpathyMessage *message, - GError **error) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->add_message) - return FALSE; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->add_message ( - self, chat_id, chatroom, message, error); -} - -GList * -empathy_log_source_get_dates (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_dates) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_dates ( - self, account, chat_id, chatroom); -} - -GList * -empathy_log_source_get_messages_for_date (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom, - const gchar *date) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_messages_for_date) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_messages_for_date ( - self, account, chat_id, chatroom, date); -} - -GList * -empathy_log_source_get_last_messages (EmpathyLogSource *self, - McAccount *account, - const gchar *chat_id, - gboolean chatroom) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_last_messages) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_last_messages ( - self, account, chat_id, chatroom); -} - -GList * -empathy_log_source_get_chats (EmpathyLogSource *self, - McAccount *account) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_chats) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->get_chats (self, account); -} - -GList * -empathy_log_source_search_new (EmpathyLogSource *self, - const gchar *text) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->search_new) - return NULL; - - return EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->search_new (self, text); -} - -void -empathy_log_source_ack_message (EmpathyLogSource *self, - const gchar *chat_id, - gboolean chatroom, - EmpathyMessage *message) -{ - if (!EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->ack_message) - return; - - EMPATHY_LOG_SOURCE_GET_INTERFACE (self)->ack_message ( - self, chat_id, chatroom, message); -} diff --git a/libempathy/empathy-log-source.h b/libempathy/empathy-log-source.h deleted file mode 100644 index 03d920c28..000000000 --- a/libempathy/empathy-log-source.h +++ /dev/null @@ -1,94 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Copyright (C) 2008 Collabora Ltd. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program; if not, write to the - * Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * Authors: Jonny Lamb - */ - -#ifndef __EMPATHY_LOG_SOURCE_H__ -#define __EMPATHY_LOG_SOURCE_H__ - -#include - -#include - -#include "empathy-message.h" - -G_BEGIN_DECLS - -#define EMPATHY_TYPE_LOG_SOURCE (empathy_log_source_get_type ()) -#define EMPATHY_LOG_SOURCE(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_SOURCE, \ - EmpathyLogSource)) -#define EMPATHY_IS_LOG_SOURCE(obj) \ - (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_SOURCE)) -#define EMPATHY_LOG_SOURCE_GET_INTERFACE(inst) \ - (G_TYPE_INSTANCE_GET_INTERFACE ((inst), EMPATHY_TYPE_LOG_SOURCE, \ - EmpathyLogSourceInterface)) - -typedef struct _EmpathyLogSource EmpathyLogSource; /* dummy object */ -typedef struct _EmpathyLogSourceInterface EmpathyLogSourceInterface; - -struct _EmpathyLogSourceInterface -{ - GTypeInterface parent; - - const gchar * (*get_name) (EmpathyLogSource *self); - gboolean (*exists) (EmpathyLogSource *self, McAccount *account, - const gchar *chat_id, gboolean chatroom); - gboolean (*add_message) (EmpathyLogSource *self, const gchar *chat_id, - gboolean chatroom, EmpathyMessage *message, GError **error); - GList * (*get_dates) (EmpathyLogSource *self, McAccount *account, - const gchar *chat_id, gboolean chatroom); - GList * (*get_messages_for_date) (EmpathyLogSource *self, - McAccount *account, const gchar *chat_id, gboolean chatroom, - const gchar *date); - GList * (*get_last_messages) (EmpathyLogSource *self, McAccount *account, - const gchar *chat_id, gboolean chatroom); - GList * (*get_chats) (EmpathyLogSource *self, - McAccount *account); - GList * (*search_new) (EmpathyLogSource *self, const gchar *text); - void (*ack_message) (EmpathyLogSource *self, const gchar *chat_id, - gboolean chatroom, EmpathyMessage *message); -}; - -GType empathy_log_source_get_type (void) G_GNUC_CONST; - -const gchar *empathy_log_source_get_name (EmpathyLogSource *self); -gboolean empathy_log_source_exists (EmpathyLogSource *self, - McAccount *account, const gchar *chat_id, gboolean chatroom); -gboolean empathy_log_source_add_message (EmpathyLogSource *self, - const gchar *chat_id, gboolean chatroom, EmpathyMessage *message, - GError **error); -GList *empathy_log_source_get_dates (EmpathyLogSource *self, - McAccount *account, const gchar *chat_id, gboolean chatroom); -GList *empathy_log_source_get_messages_for_date (EmpathyLogSource *self, - McAccount *account, const gchar *chat_id, gboolean chatroom, - const gchar *date); -GList *empathy_log_source_get_last_messages (EmpathyLogSource *self, - McAccount *account, const gchar *chat_id, gboolean chatroom); -GList *empathy_log_source_get_chats (EmpathyLogSource *self, - McAccount *account); -GList *empathy_log_source_search_new (EmpathyLogSource *self, - const gchar *text); -void empathy_log_source_ack_message (EmpathyLogSource *self, - const gchar *chat_id, gboolean chatroom, EmpathyMessage *message); - -G_END_DECLS - -#endif /* __EMPATHY_LOG_SOURCE_H__ */ diff --git a/libempathy/empathy-log-store-empathy.c b/libempathy/empathy-log-store-empathy.c new file mode 100644 index 000000000..e9cf887b2 --- /dev/null +++ b/libempathy/empathy-log-store-empathy.c @@ -0,0 +1,714 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2003-2007 Imendio AB + * Copyright (C) 2007-2008 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Xavier Claessens + * Jonny Lamb + */ + +#include + +#include +#include +#include +#include + +#include "empathy-log-store.h" +#include "empathy-log-store-empathy.h" +#include "empathy-log-manager.h" +#include "empathy-contact.h" +#include "empathy-time.h" +#include "empathy-utils.h" + +#define DEBUG_FLAG EMPATHY_DEBUG_OTHER +#include "empathy-debug.h" + +#define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR) +#define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR) +#define LOG_DIR_CHATROOMS "chatrooms" +#define LOG_FILENAME_SUFFIX ".log" +#define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S" +#define LOG_TIME_FORMAT "%Y%m%d" +#define LOG_HEADER \ + "\n" \ + "\n" \ + "\n" + +#define LOG_FOOTER \ + "\n" + + +#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreEmpathy) +typedef struct +{ + gchar *basedir; + gchar *name; +} EmpathyLogStoreEmpathyPriv; + +static void log_store_iface_init (gpointer g_iface,gpointer iface_data); + +G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy, + G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE, + log_store_iface_init)); + +static void +log_store_empathy_finalize (GObject *object) +{ + EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object); + EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self); + + g_free (priv->basedir); + g_free (priv->name); +} + +static void +empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = log_store_empathy_finalize; + + g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv)); +} + +static void +empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self) +{ + EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv); + + self->priv = priv; + + priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), + ".gnome2", PACKAGE_NAME, "logs", NULL); + + priv->name = g_strdup ("Empathy"); +} + +static gchar * +log_store_empathy_get_dir (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + const gchar *account_id; + gchar *basedir; + EmpathyLogStoreEmpathyPriv *priv; + + priv = GET_PRIV (self); + + account_id = mc_account_get_unique_name (account); + + if (chatroom) + basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id, + LOG_DIR_CHATROOMS, chat_id, NULL); + else + basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, + account_id, chat_id, NULL); + + return basedir; +} + +static gchar * +log_store_empathy_get_timestamp_filename (void) +{ + time_t t; + gchar *time_str; + gchar *filename; + + t = empathy_time_get_current (); + time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT); + filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL); + + g_free (time_str); + + return filename; +} + +static gchar * +log_store_empathy_get_timestamp_from_message (EmpathyMessage *message) +{ + time_t t; + + t = empathy_message_get_timestamp (message); + + /* We keep the timestamps in the messages as UTC. */ + return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL); +} + +static gchar * +log_store_empathy_get_filename (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + gchar *basedir; + gchar *timestamp; + gchar *filename; + + basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom); + timestamp = log_store_empathy_get_timestamp_filename (); + filename = g_build_filename (basedir, timestamp, NULL); + + g_free (basedir); + g_free (timestamp); + + return filename; +} + +static gboolean +log_store_empathy_add_message (EmpathyLogStore *self, + const gchar *chat_id, + gboolean chatroom, + EmpathyMessage *message, + GError **error) +{ + FILE *file; + McAccount *account; + EmpathyContact *sender; + const gchar *body_str; + const gchar *str; + EmpathyAvatar *avatar; + gchar *avatar_token = NULL; + gchar *filename; + gchar *basedir; + gchar *body; + gchar *timestamp; + gchar *contact_name; + gchar *contact_id; + TpChannelTextMessageType msg_type; + + g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE); + g_return_val_if_fail (chat_id != NULL, FALSE); + g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE); + + sender = empathy_message_get_sender (message); + account = empathy_contact_get_account (sender); + body_str = empathy_message_get_body (message); + msg_type = empathy_message_get_tptype (message); + + if (G_STR_EMPTY (body_str)) + return FALSE; + + filename = log_store_empathy_get_filename (self, account, chat_id, chatroom); + basedir = g_path_get_dirname (filename); + if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) + { + DEBUG ("Creating directory:'%s'", basedir); + g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE); + } + g_free (basedir); + + DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename); + + if (!g_file_test (filename, G_FILE_TEST_EXISTS)) + { + file = g_fopen (filename, "w+"); + if (file) + g_fprintf (file, LOG_HEADER); + + g_chmod (filename, LOG_FILE_CREATE_MODE); + } + else + { + file = g_fopen (filename, "r+"); + if (file) + fseek (file, - strlen (LOG_FOOTER), SEEK_END); + } + + body = g_markup_escape_text (body_str, -1); + timestamp = log_store_empathy_get_timestamp_from_message (message); + + str = empathy_contact_get_name (sender); + contact_name = g_markup_escape_text (str, -1); + + str = empathy_contact_get_id (sender); + contact_id = g_markup_escape_text (str, -1); + + avatar = empathy_contact_get_avatar (sender); + if (avatar) + avatar_token = g_markup_escape_text (avatar->token, -1); + + g_fprintf (file, + "" + "%s\n" LOG_FOOTER, timestamp, contact_id, contact_name, + avatar_token ? avatar_token : "", + empathy_contact_is_user (sender) ? "true" : "false", + empathy_message_type_to_str (msg_type), body); + + fclose (file); + g_free (filename); + g_free (contact_id); + g_free (contact_name); + g_free (timestamp); + g_free (body); + g_free (avatar_token); + + return TRUE; +} + +static gboolean +log_store_empathy_exists (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + gchar *dir; + gboolean exists; + + dir = log_store_empathy_get_dir (self, account, chat_id, chatroom); + exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR); + g_free (dir); + + return exists; +} + +static GList * +log_store_empathy_get_dates (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + GList *dates = NULL; + gchar *date; + gchar *directory; + GDir *dir; + const gchar *filename; + const gchar *p; + + g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL); + g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL); + g_return_val_if_fail (chat_id != NULL, NULL); + + directory = log_store_empathy_get_dir (self, account, chat_id, chatroom); + dir = g_dir_open (directory, 0, NULL); + if (!dir) + { + DEBUG ("Could not open directory:'%s'", directory); + g_free (directory); + return NULL; + } + + DEBUG ("Collating a list of dates in:'%s'", directory); + + while ((filename = g_dir_read_name (dir)) != NULL) + { + if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) + continue; + + p = strstr (filename, LOG_FILENAME_SUFFIX); + date = g_strndup (filename, p - filename); + + if (!date) + continue; + + if (!g_regex_match_simple ("\\d{8}", date, 0, 0)) + continue; + + dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp); + } + + g_free (directory); + g_dir_close (dir); + + DEBUG ("Parsed %d dates", g_list_length (dates)); + + return dates; +} + +static gchar * +log_store_empathy_get_filename_for_date (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom, + const gchar *date) +{ + gchar *basedir; + gchar *timestamp; + gchar *filename; + + basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom); + timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL); + filename = g_build_filename (basedir, timestamp, NULL); + + g_free (basedir); + g_free (timestamp); + + return filename; +} + +static EmpathyLogSearchHit * +log_store_empathy_search_hit_new (EmpathyLogStore *self, + const gchar *filename) +{ + EmpathyLogSearchHit *hit; + const gchar *account_name; + const gchar *end; + gchar **strv; + guint len; + + if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) + return NULL; + + strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1); + len = g_strv_length (strv); + + hit = g_slice_new0 (EmpathyLogSearchHit); + + end = strstr (strv[len-1], LOG_FILENAME_SUFFIX); + hit->date = g_strndup (strv[len-1], end - strv[len-1]); + hit->chat_id = g_strdup (strv[len-2]); + hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0); + + if (hit->is_chatroom) + account_name = strv[len-4]; + else + account_name = strv[len-3]; + + hit->account = mc_account_lookup (account_name); + hit->filename = g_strdup (filename); + + g_strfreev (strv); + + return hit; +} + +static GList * +log_store_empathy_get_messages_for_file (EmpathyLogStore *self, + const gchar *filename) +{ + GList *messages = NULL; + xmlParserCtxtPtr ctxt; + xmlDocPtr doc; + xmlNodePtr log_node; + xmlNodePtr node; + EmpathyLogSearchHit *hit; + McAccount *account; + + g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL); + g_return_val_if_fail (filename != NULL, NULL); + + DEBUG ("Attempting to parse filename:'%s'...", filename); + + if (!g_file_test (filename, G_FILE_TEST_EXISTS)) + { + DEBUG ("Filename:'%s' does not exist", filename); + return NULL; + } + + /* Get the account from the filename */ + hit = log_store_empathy_search_hit_new (self, filename); + account = g_object_ref (hit->account); + empathy_log_manager_search_hit_free (hit); + + /* Create parser. */ + ctxt = xmlNewParserCtxt (); + + /* Parse and validate the file. */ + doc = xmlCtxtReadFile (ctxt, filename, NULL, 0); + if (!doc) + { + g_warning ("Failed to parse file:'%s'", filename); + xmlFreeParserCtxt (ctxt); + return NULL; + } + + /* The root node, presets. */ + log_node = xmlDocGetRootElement (doc); + if (!log_node) + { + xmlFreeDoc (doc); + xmlFreeParserCtxt (ctxt); + return NULL; + } + + /* Now get the messages. */ + for (node = log_node->children; node; node = node->next) + { + EmpathyMessage *message; + EmpathyContact *sender; + gchar *time; + time_t t; + gchar *sender_id; + gchar *sender_name; + gchar *sender_avatar_token; + gchar *body; + gchar *is_user_str; + gboolean is_user = FALSE; + gchar *msg_type_str; + TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL; + + if (strcmp (node->name, "message") != 0) + continue; + + body = xmlNodeGetContent (node); + time = xmlGetProp (node, "time"); + sender_id = xmlGetProp (node, "id"); + sender_name = xmlGetProp (node, "name"); + sender_avatar_token = xmlGetProp (node, "token"); + is_user_str = xmlGetProp (node, "isuser"); + msg_type_str = xmlGetProp (node, "type"); + + if (is_user_str) + is_user = strcmp (is_user_str, "true") == 0; + + if (msg_type_str) + msg_type = empathy_message_type_from_str (msg_type_str); + + t = empathy_time_parse (time); + + sender = empathy_contact_new_full (account, sender_id, sender_name); + empathy_contact_set_is_user (sender, is_user); + if (!G_STR_EMPTY (sender_avatar_token)) + empathy_contact_load_avatar_cache (sender, + sender_avatar_token); + + message = empathy_message_new (body); + empathy_message_set_sender (message, sender); + empathy_message_set_timestamp (message, t); + empathy_message_set_tptype (message, msg_type); + + messages = g_list_append (messages, message); + + g_object_unref (sender); + xmlFree (time); + xmlFree (sender_id); + xmlFree (sender_name); + xmlFree (body); + xmlFree (is_user_str); + xmlFree (msg_type_str); + } + + DEBUG ("Parsed %d messages", g_list_length (messages)); + + xmlFreeDoc (doc); + xmlFreeParserCtxt (ctxt); + + return messages; +} + +static GList * +log_store_empathy_get_all_files (EmpathyLogStore *self, + const gchar *dir) +{ + GDir *gdir; + GList *files = NULL; + const gchar *name; + const gchar *basedir; + EmpathyLogStoreEmpathyPriv *priv; + + priv = GET_PRIV (self); + + basedir = dir ? dir : priv->basedir; + + gdir = g_dir_open (basedir, 0, NULL); + if (!gdir) + return NULL; + + while ((name = g_dir_read_name (gdir)) != NULL) + { + gchar *filename; + + filename = g_build_filename (basedir, name, NULL); + if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX)) + { + files = g_list_prepend (files, filename); + continue; + } + + if (g_file_test (filename, G_FILE_TEST_IS_DIR)) + { + /* Recursively get all log files */ + files = g_list_concat (files, + log_store_empathy_get_all_files (self, filename)); + } + + g_free (filename); + } + + g_dir_close (gdir); + + return files; +} + +static GList * +log_store_empathy_search_new (EmpathyLogStore *self, + const gchar *text) +{ + GList *files, *l; + GList *hits = NULL; + gchar *text_casefold; + + g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL); + g_return_val_if_fail (!G_STR_EMPTY (text), NULL); + + text_casefold = g_utf8_casefold (text, -1); + + files = log_store_empathy_get_all_files (self, NULL); + DEBUG ("Found %d log files in total", g_list_length (files)); + + for (l = files; l; l = l->next) + { + gchar *filename; + GMappedFile *file; + gsize length; + gchar *contents; + gchar *contents_casefold; + + filename = l->data; + + file = g_mapped_file_new (filename, FALSE, NULL); + if (!file) + continue; + + length = g_mapped_file_get_length (file); + contents = g_mapped_file_get_contents (file); + contents_casefold = g_utf8_casefold (contents, length); + + g_mapped_file_free (file); + + if (strstr (contents_casefold, text_casefold)) + { + EmpathyLogSearchHit *hit; + + hit = log_store_empathy_search_hit_new (self, filename); + + if (hit) + { + hits = g_list_prepend (hits, hit); + DEBUG ("Found text:'%s' in file:'%s' on date:'%s'", + text, hit->filename, hit->date); + } + } + + g_free (contents_casefold); + g_free (filename); + } + + g_list_free (files); + g_free (text_casefold); + + return hits; +} + +static GList * +log_store_empathy_get_chats_for_dir (EmpathyLogStore *self, + const gchar *dir, + gboolean is_chatroom) +{ + GDir *gdir; + GList *hits = NULL; + const gchar *name; + + gdir = g_dir_open (dir, 0, NULL); + if (!gdir) + return NULL; + + while ((name = g_dir_read_name (gdir)) != NULL) + { + EmpathyLogSearchHit *hit; + gchar *filename; + + filename = g_build_filename (dir, name, NULL); + if (strcmp (name, LOG_DIR_CHATROOMS) == 0) + { + hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir ( + self, dir, TRUE)); + g_free (filename); + continue; + } + + hit = g_slice_new0 (EmpathyLogSearchHit); + hit->chat_id = g_strdup (name); + hit->is_chatroom = is_chatroom; + + hits = g_list_prepend (hits, hit); + } + + g_dir_close (gdir); + + return hits; +} + + +static GList * +log_store_empathy_get_messages_for_date (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom, + const gchar *date) +{ + gchar *filename; + GList *messages; + + g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL); + g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL); + g_return_val_if_fail (chat_id != NULL, NULL); + + filename = log_store_empathy_get_filename_for_date (self, account, + chat_id, chatroom, date); + messages = log_store_empathy_get_messages_for_file (self, filename); + g_free (filename); + + return messages; +} + +static GList * +log_store_empathy_get_chats (EmpathyLogStore *self, + McAccount *account) +{ + gchar *dir; + GList *hits; + EmpathyLogStoreEmpathyPriv *priv; + + priv = GET_PRIV (self); + + dir = g_build_filename (priv->basedir, + mc_account_get_unique_name (account), NULL); + + hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE); + + g_free (dir); + + return hits; +} + +static const gchar * +log_store_empathy_get_name (EmpathyLogStore *self) +{ + EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self); + + return priv->name; +} + +static void +log_store_iface_init (gpointer g_iface, + gpointer iface_data) +{ + EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface; + + iface->get_name = log_store_empathy_get_name; + iface->exists = log_store_empathy_exists; + iface->add_message = log_store_empathy_add_message; + iface->get_dates = log_store_empathy_get_dates; + iface->get_messages_for_date = log_store_empathy_get_messages_for_date; + iface->get_chats = log_store_empathy_get_chats; + iface->search_new = log_store_empathy_search_new; + iface->ack_message = NULL; +} diff --git a/libempathy/empathy-log-store-empathy.h b/libempathy/empathy-log-store-empathy.h new file mode 100644 index 000000000..3bc9997c0 --- /dev/null +++ b/libempathy/empathy-log-store-empathy.h @@ -0,0 +1,66 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2003-2007 Imendio AB + * Copyright (C) 2007-2008 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Xavier Claessens + * Jonny Lamb + */ + +#ifndef __EMPATHY_LOG_STORE_EMPATHY_H__ +#define __EMPATHY_LOG_STORE_EMPATHY_H__ + +#include + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_LOG_STORE_EMPATHY \ + (empathy_log_store_empathy_get_type ()) +#define EMPATHY_LOG_STORE_EMPATHY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE_EMPATHY, \ + EmpathyLogStoreEmpathy)) +#define EMPATHY_LOG_STORE_EMPATHY_CLASS(vtable) \ + (G_TYPE_CHECK_CLASS_CAST ((vtable), EMPATHY_TYPE_LOG_STORE_EMPATHY, \ + EmpathyLogStoreEmpathyClass)) +#define EMPATHY_IS_LOG_STORE_EMPATHY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE_EMPATHY)) +#define EMPATHY_IS_LOG_STORE_EMPATHY_CLASS(vtable) \ + (G_TYPE_CHECK_CLASS_TYPE ((vtable), EMPATHY_TYPE_LOG_STORE_EMPATHY)) +#define EMPATHY_LOG_STORE_EMPATHY_GET_CLASS(inst) \ + (G_TYPE_INSTANCE_GET_CLASS ((inst), EMPATHY_TYPE_LOG_STORE_EMPATHY, \ + EmpathyLogStoreEmpathyClass)) + +typedef struct _EmpathyLogStoreEmpathy EmpathyLogStoreEmpathy; +typedef struct _EmpathyLogStoreEmpathyClass EmpathyLogStoreEmpathyClass; + +struct _EmpathyLogStoreEmpathy +{ + GObject parent; + gpointer priv; +}; + +struct _EmpathyLogStoreEmpathyClass +{ + GObjectClass parent; +}; + +GType empathy_log_store_empathy_get_type (void); + +G_END_DECLS + +#endif /* __EMPATHY_LOG_STORE_EMPATHY_H__ */ diff --git a/libempathy/empathy-log-store.c b/libempathy/empathy-log-store.c new file mode 100644 index 000000000..54681fed9 --- /dev/null +++ b/libempathy/empathy-log-store.c @@ -0,0 +1,156 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2008 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Jonny Lamb + */ + +#include "empathy-log-store.h" + +GType +empathy_log_store_get_type (void) +{ + static GType type = 0; + if (type == 0) { + static const GTypeInfo info = { + sizeof (EmpathyLogStoreInterface), + NULL, /* base_init */ + NULL, /* base_finalize */ + NULL, /* class_init */ + NULL, /* class_finalize */ + NULL, /* class_data */ + 0, + 0, /* n_preallocs */ + NULL /* instance_init */ + }; + type = g_type_register_static (G_TYPE_INTERFACE, "EmpathyLogStore", + &info, 0); + } + return type; +} + +const gchar * +empathy_log_store_get_name (EmpathyLogStore *self) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_name) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_name (self); +} + +gboolean +empathy_log_store_exists (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->exists) + return FALSE; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->exists ( + self, account, chat_id, chatroom); +} + + + +gboolean +empathy_log_store_add_message (EmpathyLogStore *self, + const gchar *chat_id, + gboolean chatroom, + EmpathyMessage *message, + GError **error) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->add_message) + return FALSE; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->add_message ( + self, chat_id, chatroom, message, error); +} + +GList * +empathy_log_store_get_dates (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_dates) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_dates ( + self, account, chat_id, chatroom); +} + +GList * +empathy_log_store_get_messages_for_date (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom, + const gchar *date) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_messages_for_date) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_messages_for_date ( + self, account, chat_id, chatroom, date); +} + +GList * +empathy_log_store_get_last_messages (EmpathyLogStore *self, + McAccount *account, + const gchar *chat_id, + gboolean chatroom) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_last_messages) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_last_messages ( + self, account, chat_id, chatroom); +} + +GList * +empathy_log_store_get_chats (EmpathyLogStore *self, + McAccount *account) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_chats) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_chats (self, account); +} + +GList * +empathy_log_store_search_new (EmpathyLogStore *self, + const gchar *text) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->search_new) + return NULL; + + return EMPATHY_LOG_STORE_GET_INTERFACE (self)->search_new (self, text); +} + +void +empathy_log_store_ack_message (EmpathyLogStore *self, + const gchar *chat_id, + gboolean chatroom, + EmpathyMessage *message) +{ + if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->ack_message) + return; + + EMPATHY_LOG_STORE_GET_INTERFACE (self)->ack_message ( + self, chat_id, chatroom, message); +} diff --git a/libempathy/empathy-log-store.h b/libempathy/empathy-log-store.h new file mode 100644 index 000000000..c14f7f1c4 --- /dev/null +++ b/libempathy/empathy-log-store.h @@ -0,0 +1,94 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Copyright (C) 2008 Collabora Ltd. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * Authors: Jonny Lamb + */ + +#ifndef __EMPATHY_LOG_STORE_H__ +#define __EMPATHY_LOG_STORE_H__ + +#include + +#include + +#include "empathy-message.h" + +G_BEGIN_DECLS + +#define EMPATHY_TYPE_LOG_STORE (empathy_log_store_get_type ()) +#define EMPATHY_LOG_STORE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE, \ + EmpathyLogStore)) +#define EMPATHY_IS_LOG_STORE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE)) +#define EMPATHY_LOG_STORE_GET_INTERFACE(inst) \ + (G_TYPE_INSTANCE_GET_INTERFACE ((inst), EMPATHY_TYPE_LOG_STORE, \ + EmpathyLogStoreInterface)) + +typedef struct _EmpathyLogStore EmpathyLogStore; /* dummy object */ +typedef struct _EmpathyLogStoreInterface EmpathyLogStoreInterface; + +struct _EmpathyLogStoreInterface +{ + GTypeInterface parent; + + const gchar * (*get_name) (EmpathyLogStore *self); + gboolean (*exists) (EmpathyLogStore *self, McAccount *account, + const gchar *chat_id, gboolean chatroom); + gboolean (*add_message) (EmpathyLogStore *self, const gchar *chat_id, + gboolean chatroom, EmpathyMessage *message, GError **error); + GList * (*get_dates) (EmpathyLogStore *self, McAccount *account, + const gchar *chat_id, gboolean chatroom); + GList * (*get_messages_for_date) (EmpathyLogStore *self, + McAccount *account, const gchar *chat_id, gboolean chatroom, + const gchar *date); + GList * (*get_last_messages) (EmpathyLogStore *self, McAccount *account, + const gchar *chat_id, gboolean chatroom); + GList * (*get_chats) (EmpathyLogStore *self, + McAccount *account); + GList * (*search_new) (EmpathyLogStore *self, const gchar *text); + void (*ack_message) (EmpathyLogStore *self, const gchar *chat_id, + gboolean chatroom, EmpathyMessage *message); +}; + +GType empathy_log_store_get_type (void) G_GNUC_CONST; + +const gchar *empathy_log_store_get_name (EmpathyLogStore *self); +gboolean empathy_log_store_exists (EmpathyLogStore *self, + McAccount *account, const gchar *chat_id, gboolean chatroom); +gboolean empathy_log_store_add_message (EmpathyLogStore *self, + const gchar *chat_id, gboolean chatroom, EmpathyMessage *message, + GError **error); +GList *empathy_log_store_get_dates (EmpathyLogStore *self, + McAccount *account, const gchar *chat_id, gboolean chatroom); +GList *empathy_log_store_get_messages_for_date (EmpathyLogStore *self, + McAccount *account, const gchar *chat_id, gboolean chatroom, + const gchar *date); +GList *empathy_log_store_get_last_messages (EmpathyLogStore *self, + McAccount *account, const gchar *chat_id, gboolean chatroom); +GList *empathy_log_store_get_chats (EmpathyLogStore *self, + McAccount *account); +GList *empathy_log_store_search_new (EmpathyLogStore *self, + const gchar *text); +void empathy_log_store_ack_message (EmpathyLogStore *self, + const gchar *chat_id, gboolean chatroom, EmpathyMessage *message); + +G_END_DECLS + +#endif /* __EMPATHY_LOG_STORE_H__ */ -- cgit v1.2.3