aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonny Lamb <jonny.lamb@collabora.co.uk>2009-03-06 19:51:17 +0800
committerXavier Claessens <xclaesse@src.gnome.org>2009-03-06 19:51:17 +0800
commit51b836484f5eef122ffb1b364caec38cb3973fde (patch)
treef04b5aee325ed2c6ec83303d792252bffad1323c
parent4ee8934f3b36bc72b0c1ba257c6082b56c6d1702 (diff)
downloadgsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar.gz
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar.bz2
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar.lz
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar.xz
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.tar.zst
gsoc2013-empathy-51b836484f5eef122ffb1b364caec38cb3973fde.zip
Converted log code to Collabora coding style. (Jonny Lamb)
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk> svn path=/trunk/; revision=2578
-rw-r--r--libempathy/empathy-log-manager.c862
-rw-r--r--libempathy/empathy-log-manager.h121
-rw-r--r--libempathy/empathy-log-source-empathy.c688
3 files changed, 968 insertions, 703 deletions
diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c
index 2dccbc9fd..371100e13 100644
--- a/libempathy/empathy-log-manager.c
+++ b/libempathy/empathy-log-manager.c
@@ -36,50 +36,12 @@
#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 \
- "<?xml version='1.0' encoding='utf-8'?>\n" \
- "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
- "<log>\n"
-
-#define LOG_FOOTER \
- "</log>\n"
-
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager)
-typedef struct {
- gchar *basedir;
+typedef struct
+{
+ GList *sources;
} EmpathyLogManagerPriv;
-static const gchar * log_manager_get_basedir (EmpathyLogManager *manager);
-static GList * log_manager_get_all_files (EmpathyLogManager *manager,
- const gchar *dir);
-static GList * log_manager_get_chats (EmpathyLogManager *manager,
- const gchar *dir,
- gboolean is_chatroom);
-static gchar * log_manager_get_dir (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom);
-static gchar * log_manager_get_filename (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom);
-static gchar * log_manager_get_filename_for_date (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom,
- const gchar *date);
-static gchar * log_manager_get_timestamp_filename (void);
-static gchar * log_manager_get_timestamp_from_message (EmpathyMessage *message);
-static EmpathyLogSearchHit *log_manager_search_hit_new (EmpathyLogManager *manager,
- const gchar *filename);
-static void log_manager_search_hit_free (EmpathyLogSearchHit *hit);
-
G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT);
static EmpathyLogManager * manager_singleton = NULL;
@@ -87,719 +49,317 @@ static EmpathyLogManager * manager_singleton = NULL;
static void
empathy_log_manager_init (EmpathyLogManager *manager)
{
- EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
- EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
-
- manager->priv = priv;
+ EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
+ EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
+ manager->priv = priv;
}
static void
log_manager_finalize (GObject *object)
{
- EmpathyLogManagerPriv *priv;
+ EmpathyLogManagerPriv *priv;
+ GList *l;
+
+ priv = GET_PRIV (object);
- priv = GET_PRIV (object);
+ for (l = priv->sources; l; l = l->next)
+ {
+ g_slice_free (EmpathyLogSource, l->data);
+ }
- g_free (priv->basedir);
+ g_list_free (priv->sources);
}
static GObject *
log_manager_constructor (GType type,
- guint n_props,
- GObjectConstructParam *props)
+ guint n_props,
+ GObjectConstructParam *props)
{
- GObject *retval;
+ GObject *retval;
+
+ if (manager_singleton)
+ {
+ retval = g_object_ref (manager_singleton);
+ }
+ else
+ {
+ retval = G_OBJECT_CLASS (empathy_log_manager_parent_class)->constructor
+ (type, n_props, props);
- if (manager_singleton) {
- retval = g_object_ref (manager_singleton);
- } else {
- retval = G_OBJECT_CLASS (empathy_log_manager_parent_class)->constructor
- (type, n_props, props);
+ manager_singleton = EMPATHY_LOG_MANAGER (retval);
+ g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
- manager_singleton = EMPATHY_LOG_MANAGER (retval);
- g_object_add_weak_pointer (retval, (gpointer) &manager_singleton);
- }
+ priv = GET_PRIV (manager_singleton);
- return retval;
+ manager_singleton = EMPATHY_LOG_MANAGER (retval);
+ g_object_add_weak_pointer (retval, (gpointer), &manager_singleton);
+
+ priv->sources = g_list_append (priv->sources,
+ empathy_log_source_empathy_get_source ());
+ }
+
+ return retval;
}
static void
empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
{
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
- object_class->finalize = log_manager_finalize;
- object_class->constructor = log_manager_constructor;
+ object_class->finalize = log_manager_finalize;
+ object_class->constructor = log_manager_constructor;
- g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
+ g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
}
EmpathyLogManager *
empathy_log_manager_dup_singleton (void)
{
- return g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
+ return g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
}
void
empathy_log_manager_add_message (EmpathyLogManager *manager,
- const gchar *chat_id,
- gboolean chatroom,
- EmpathyMessage *message)
+ const gchar *chat_id,
+ gboolean chatroom,
+ EmpathyMessage *message)
{
- 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_if_fail (EMPATHY_IS_LOG_MANAGER (manager));
- g_return_if_fail (chat_id != NULL);
- g_return_if_fail (EMPATHY_IS_MESSAGE (message));
-
- 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 (EMP_STR_EMPTY (body_str)) {
- return;
- }
-
- filename = log_manager_get_filename (manager, 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_manager_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,
- "<message time='%s' id='%s' name='%s' token='%s' isuser='%s' type='%s'>%s</message>\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);
-}
-
-gboolean
-empathy_log_manager_exists (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom)
-{
- gchar *dir;
- gboolean exists;
-
- dir = log_manager_get_dir (manager, account, chat_id, chatroom);
- exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
- g_free (dir);
-
- return exists;
-}
-
-GList *
-empathy_log_manager_get_dates (EmpathyLogManager *manager,
- 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_MANAGER (manager), NULL);
- g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
- g_return_val_if_fail (chat_id != NULL, NULL);
-
- directory = log_manager_get_dir (manager, 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;
-}
+ EmpathyLogManagerPriv *priv;
+ GList *l;
-GList *
-empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager,
- 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_MANAGER (manager), 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_manager_search_hit_new (manager, filename);
- account = g_object_ref (hit->account);
- 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 (!EMP_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;
-}
+ g_return_if_fail (EMPATHY_IS_LOG_MANAGER (manager));
+ g_return_if_fail (chat_id != NULL);
+ g_return_if_fail (EMPATHY_IS_MESSAGE (message));
-GList *
-empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom,
- const gchar *date)
-{
- gchar *filename;
- GList *messages;
+ priv = GET_PRIV (manager);
- 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);
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- filename = log_manager_get_filename_for_date (manager, account, chat_id, chatroom, date);
- messages = empathy_log_manager_get_messages_for_file (manager, filename);
- g_free (filename);
+ if (!source->add_message)
+ continue;
- return messages;
+ source->add_message (manager, chat_id, chatroom, message);
+ }
}
-GList *
-empathy_log_manager_get_last_messages (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom)
+gboolean
+empathy_log_manager_exists (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
{
- GList *messages = NULL;
- GList *dates;
- GList *l;
+ GList *l;
+ EmpathyLogManagerPriv *priv;
- 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);
+ g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
+ g_return_val_if_fail (MC_IS_ACCOUNT (account), FALSE);
+ g_return_val_if_fail (chat_id != NULL, FALSE);
- dates = empathy_log_manager_get_dates (manager, account, chat_id, chatroom);
+ priv = GET_PRIV (manager);
- l = g_list_last (dates);
- if (l) {
- messages = empathy_log_manager_get_messages_for_date (manager,
- account,
- chat_id,
- chatroom,
- l->data);
- }
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- g_list_foreach (dates, (GFunc) g_free, NULL);
- g_list_free (dates);
+ if (!source->exists)
+ continue;
- return messages;
-}
+ if (source->exists (manager, account, chat_id, chatroom))
+ return TRUE;
+ }
-GList *
-empathy_log_manager_get_chats (EmpathyLogManager *manager,
- McAccount *account)
-{
- const gchar *basedir;
- gchar *dir;
-
- basedir = log_manager_get_basedir (manager);
- dir = g_build_filename (basedir,
- mc_account_get_unique_name (account),
- NULL);
-
- return log_manager_get_chats (manager, dir, FALSE);
+ return FALSE;
}
GList *
-empathy_log_manager_search_new (EmpathyLogManager *manager,
- const gchar *text)
+empathy_log_manager_get_dates (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
{
- GList *files, *l;
- GList *hits = NULL;
- gchar *text_casefold;
-
- g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
- g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
-
- text_casefold = g_utf8_casefold (text, -1);
-
- files = log_manager_get_all_files (manager, NULL);
- DEBUG ("Found %d log files in total", g_list_length (files));
+ GList *l, *out = NULL;
+ EmpathyLogManagerPriv *priv;
- for (l = files; l; l = l->next) {
- gchar *filename;
- GMappedFile *file;
- gsize length;
- gchar *contents;
- gchar *contents_casefold;
+ 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);
- filename = l->data;
+ priv = GET_PRIV (manager);
- file = g_mapped_file_new (filename, FALSE, NULL);
- if (!file) {
- continue;
- }
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- length = g_mapped_file_get_length (file);
- contents = g_mapped_file_get_contents (file);
- contents_casefold = g_utf8_casefold (contents, length);
+ if (!source->get_dates)
+ continue;
- g_mapped_file_free (file);
+ if (!out)
+ out = source->get_dates (manager, account, chat_id, chatroom);
+ else
+ /* TODO fix this */
+ out = g_list_concat (out, source->get_dates (manager, account,
+ chat_id, chatroom));
+ }
- if (strstr (contents_casefold, text_casefold)) {
- EmpathyLogSearchHit *hit;
+ return out;
+}
- hit = log_manager_search_hit_new (manager, filename);
+GList *
+empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom,
+ const gchar *date)
+{
+ GList *l, *out = NULL;
+ EmpathyLogManagerPriv *priv;
- if (hit) {
- hits = g_list_prepend (hits, hit);
- DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
- text, hit->filename, hit->date);
- }
- }
+ 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);
- g_free (contents_casefold);
- g_free (filename);
- }
- g_list_free (files);
+ priv = GET_PRIV (manager);
- g_free (text_casefold);
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- return hits;
-}
+ if (!source->get_messages_for_date)
+ continue;
-void
-empathy_log_manager_search_free (GList *hits)
-{
- GList *l;
+ if (!out)
+ out = source->get_messages_for_date (manager, account, chat_id,
+ chatroom, date);
+ else
+ out = g_list_concat (out, source->get_messages_for_date (manager,
+ account, chat_id, chatroom, date));
+ }
- for (l = hits; l; l = l->next) {
- log_manager_search_hit_free (l->data);
- }
-
- g_list_free (hits);
+ return out;
}
-/* Format is just date, 20061201. */
-gchar *
-empathy_log_manager_get_date_readable (const gchar *date)
+GList *
+empathy_log_manager_get_last_messages (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
{
- time_t t;
-
- t = empathy_time_parse (date);
-
- return empathy_time_to_string_local (t, "%a %d %b %Y");
-}
+ GList *l, *out = NULL;
+ EmpathyLogManagerPriv *priv;
-static const gchar *
-log_manager_get_basedir (EmpathyLogManager *manager)
-{
- EmpathyLogManagerPriv *priv;
+ 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);
- priv = GET_PRIV (manager);
+ priv = GET_PRIV (manager);
- if (priv->basedir) {
- return priv->basedir;
- }
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- priv->basedir = g_build_path (G_DIR_SEPARATOR_S,
- g_get_home_dir (),
- ".gnome2",
- PACKAGE_NAME,
- "logs",
- NULL);
+ if (!source->get_last_messages)
+ continue;
- return priv->basedir;
-}
+ if (!out)
+ out = source->get_last_messages (manager, account, chat_id, chatroom);
+ else
+ out = g_list_concat (out, source->get_last_messages (manager, account, chat_id, chatroom));
+ }
-static GList *
-log_manager_get_all_files (EmpathyLogManager *manager,
- const gchar *dir)
-{
- GDir *gdir;
- GList *files = NULL;
- const gchar *name;
-
- if (!dir) {
- dir = log_manager_get_basedir (manager);
- }
-
- gdir = g_dir_open (dir, 0, NULL);
- if (!gdir) {
- return NULL;
- }
-
- while ((name = g_dir_read_name (gdir)) != NULL) {
- gchar *filename;
-
- filename = g_build_filename (dir, 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_manager_get_all_files (manager, filename));
- }
- g_free (filename);
- }
-
- g_dir_close (gdir);
-
- return files;
+ return out;
}
-static GList *
-log_manager_get_chats (EmpathyLogManager *manager,
- const gchar *dir,
- gboolean is_chatroom)
+GList *
+empathy_log_manager_get_chats (EmpathyLogManager *manager,
+ McAccount *account)
{
- GDir *gdir;
- GList *hits = NULL;
- const gchar *name;
+ GList *l, *out = NULL;
+ EmpathyLogManagerPriv *priv;
- gdir = g_dir_open (dir, 0, NULL);
- if (!gdir) {
- return NULL;
- }
+ g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
+ g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
- while ((name = g_dir_read_name (gdir)) != NULL) {
- EmpathyLogSearchHit *hit;
- gchar *filename;
+ priv = GET_PRIV (manager);
- filename = g_build_filename (dir, name, NULL);
- if (strcmp (name, LOG_DIR_CHATROOMS) == 0) {
- hits = g_list_concat (hits, log_manager_get_chats (manager, filename, TRUE));
- g_free (filename);
- continue;
- }
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
- hit = g_slice_new0 (EmpathyLogSearchHit);
- hit->chat_id = g_strdup (name);
- hit->is_chatroom = is_chatroom;
+ if (!source->get_chats)
+ continue;
- hits = g_list_prepend (hits, hit);
- }
+ if (!out)
+ out = source->get_chats (manager, account);
+ else
+ out = g_list_concat (out, source->get_chats (manager, account));
+ }
- g_dir_close (gdir);
-
- return hits;
+ return out;
}
-static gchar *
-log_manager_get_dir (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom)
-{
- const gchar *account_id;
- gchar *basedir;
-
- account_id = mc_account_get_unique_name (account);
-
- if (chatroom) {
- basedir = g_build_path (G_DIR_SEPARATOR_S,
- log_manager_get_basedir (manager),
- account_id,
- LOG_DIR_CHATROOMS,
- chat_id,
- NULL);
- } else {
- basedir = g_build_path (G_DIR_SEPARATOR_S,
- log_manager_get_basedir (manager),
- account_id,
- chat_id,
- NULL);
- }
-
- return basedir;
-}
-
-static gchar *
-log_manager_get_filename (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom)
+GList *
+empathy_log_manager_search_new (EmpathyLogManager *manager,
+ const gchar *text)
{
- gchar *basedir;
- gchar *timestamp;
- gchar *filename;
+ GList *l, *out = NULL;
+ EmpathyLogManagerPriv *priv;
- basedir = log_manager_get_dir (manager, account, chat_id, chatroom);
- timestamp = log_manager_get_timestamp_filename ();
- filename = g_build_filename (basedir, timestamp, NULL);
+ g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
+ g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
- g_free (basedir);
- g_free (timestamp);
+ priv = GET_PRIV (manager);
- return filename;
-}
+ for (l = priv->sources; l; l = l->next)
+ {
+ EmpathyLogSource *source = (EmpathyLogSource *) l->data;
-static gchar *
-log_manager_get_filename_for_date (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom,
- const gchar *date)
-{
- gchar *basedir;
- gchar *timestamp;
- gchar *filename;
+ if (!source->search_new)
+ continue;
- basedir = log_manager_get_dir (manager, account, chat_id, chatroom);
- timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
- filename = g_build_filename (basedir, timestamp, NULL);
+ if (!out)
+ out = source->search_new (manager, text);
+ else
+ out = g_list_concat (out, source->search_new (manager, text));
+ }
- g_free (basedir);
- g_free (timestamp);
-
- return filename;
+ return out;
}
-static gchar *
-log_manager_get_timestamp_filename (void)
+void
+empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
{
- time_t t;
- gchar *time_str;
- gchar *filename;
+ if (hit->account)
+ g_object_unref (hit->account);
- 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 (hit->date);
+ g_free (hit->filename);
+ g_free (hit->chat_id);
- g_free (time_str);
-
- return filename;
+ g_slice_free (EmpathyLogSearchHit, hit);
}
-static gchar *
-log_manager_get_timestamp_from_message (EmpathyMessage *message)
+void
+empathy_log_manager_search_free (GList *hits)
{
- time_t t;
+ GList *l;
- 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);
-}
+ for (l = hits; l; l = l->next)
+ {
+ empathy_log_manager_search_hit_free (l->data);
+ }
-static EmpathyLogSearchHit *
-log_manager_search_hit_new (EmpathyLogManager *manager,
- 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;
+ g_list_free (hits);
}
-static void
-log_manager_search_hit_free (EmpathyLogSearchHit *hit)
+/* Format is just date, 20061201. */
+gchar *
+empathy_log_manager_get_date_readable (const gchar *date)
{
- if (hit->account) {
- g_object_unref (hit->account);
- }
+ time_t t;
- g_free (hit->date);
- g_free (hit->filename);
- g_free (hit->chat_id);
+ t = empathy_time_parse (date);
- g_slice_free (EmpathyLogSearchHit, hit);
+ return empathy_time_to_string_local (t, "%a %d %b %Y");
}
diff --git a/libempathy/empathy-log-manager.h b/libempathy/empathy-log-manager.h
index bad014000..b8f86efd7 100644
--- a/libempathy/empathy-log-manager.h
+++ b/libempathy/empathy-log-manager.h
@@ -32,67 +32,84 @@
G_BEGIN_DECLS
-#define EMPATHY_TYPE_LOG_MANAGER (empathy_log_manager_get_type ())
-#define EMPATHY_LOG_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManager))
-#define EMPATHY_LOG_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerClass))
-#define EMPATHY_IS_LOG_MANAGER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_LOG_MANAGER))
-#define EMPATHY_IS_LOG_MANAGER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_LOG_MANAGER))
-#define EMPATHY_LOG_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerClass))
+#define EMPATHY_TYPE_LOG_MANAGER (empathy_log_manager_get_type ())
+#define EMPATHY_LOG_MANAGER(o) \
+ (G_TYPE_CHECK_INSTANCE_CAST ((o), EMPATHY_TYPE_LOG_MANAGER, \
+ EmpathyLogManager))
+#define EMPATHY_LOG_MANAGER_CLASS(k) \
+ (G_TYPE_CHECK_CLASS_CAST ((k), EMPATHY_TYPE_LOG_MANAGER, \
+ EmpathyLogManagerClass))
+#define EMPATHY_IS_LOG_MANAGER(o) \
+ (G_TYPE_CHECK_INSTANCE_TYPE ((o), EMPATHY_TYPE_LOG_MANAGER))
+#define EMPATHY_IS_LOG_MANAGER_CLASS(k) \
+ (G_TYPE_CHECK_CLASS_TYPE ((k), EMPATHY_TYPE_LOG_MANAGER))
+#define EMPATHY_LOG_MANAGER_GET_CLASS(o) \
+ (G_TYPE_INSTANCE_GET_CLASS ((o), EMPATHY_TYPE_LOG_MANAGER, \
+ EmpathyLogManagerClass))
-typedef struct _EmpathyLogManager EmpathyLogManager;
+typedef struct _EmpathyLogManager EmpathyLogManager;
typedef struct _EmpathyLogManagerClass EmpathyLogManagerClass;
-typedef struct _EmpathyLogSearchHit EmpathyLogSearchHit;
+typedef struct _EmpathyLogSearchHit EmpathyLogSearchHit;
+typedef struct _EmpathyLogSource EmpathyLogSource;
-struct _EmpathyLogManager {
- GObject parent;
- gpointer priv;
+struct _EmpathyLogManager
+{
+ GObject parent;
+ gpointer priv;
};
-struct _EmpathyLogManagerClass {
- GObjectClass parent_class;
+struct _EmpathyLogManagerClass
+{
+ GObjectClass parent_class;
};
-struct _EmpathyLogSearchHit {
- McAccount *account;
- gchar *chat_id;
- gboolean is_chatroom;
- gchar *filename;
- gchar *date;
+struct _EmpathyLogSearchHit
+{
+ McAccount *account;
+ gchar *chat_id;
+ gboolean is_chatroom;
+ gchar *filename;
+ gchar *date;
};
-GType empathy_log_manager_get_type (void) G_GNUC_CONST;
-EmpathyLogManager *empathy_log_manager_dup_singleton (void);
-void empathy_log_manager_add_message (EmpathyLogManager *manager,
- const gchar *chat_id,
- gboolean chatroom,
- EmpathyMessage *message);
-gboolean empathy_log_manager_exists (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom);
-GList * empathy_log_manager_get_dates (EmpathyLogManager *manager,
- McAccount *account,
- const gchar *chat_id,
- gboolean chatroom);
-GList * empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager,
- const gchar *filename);
-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_messages_for_file (EmpathyLogManager *manager,
- const gchar *filename);
-GList * empathy_log_manager_get_chats (EmpathyLogManager *manager,
- McAccount *account);
-GList * empathy_log_manager_search_new (EmpathyLogManager *manager,
- const gchar *text);
-void empathy_log_manager_search_free (GList *hits);
-gchar * empathy_log_manager_get_date_readable (const gchar *date);
+struct _EmpathyLogSource
+{
+ gboolean (*exists) (EmpathyLogManager *manager, McAccount *account,
+ const gchar *chat_id, gboolean chatroom);
+ void (*add_message) (EmpathyLogManager *manager, const gchar *chat_id,
+ gboolean chatroom, EmpathyMessage *message);
+ GList * (*get_dates) (EmpathyLogManager *manager, McAccount *account,
+ const gchar *chat_id, gboolean chatroom);
+ GList * (*get_messages_for_date) (EmpathyLogManager *manager,
+ McAccount *account, const gchar *chat_id, gboolean chatroom,
+ const gchar *date);
+ GList * (*get_last_messages) (EmpathyLogManager *manager, McAccount *account,
+ const gchar *chat_id, gboolean chatroom);
+ GList * (*get_chats) (EmpathyLogManager *manager,
+ McAccount *account);
+ GList * (*search_new) (EmpathyLogManager *manager, const gchar *text);
+};
+
+GType empathy_log_manager_get_type (void) G_GNUC_CONST;
+EmpathyLogManager *empathy_log_manager_dup_singleton (void);
+void empathy_log_manager_add_message (EmpathyLogManager *manager,
+ const gchar *chat_id, gboolean chatroom, EmpathyMessage *message);
+gboolean empathy_log_manager_exists (EmpathyLogManager *manager,
+ McAccount *account, const gchar *chat_id, gboolean chatroom);
+GList *empathy_log_manager_get_dates (EmpathyLogManager *manager,
+ McAccount *account, const gchar *chat_id, gboolean chatroom);
+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_chats (EmpathyLogManager *manager,
+ McAccount *account);
+GList *empathy_log_manager_search_new (EmpathyLogManager *manager,
+ const gchar *text);
+void empathy_log_manager_search_free (GList *hits);
+gchar *empathy_log_manager_get_date_readable (const gchar *date);
+void empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit);
G_END_DECLS
diff --git a/libempathy/empathy-log-source-empathy.c b/libempathy/empathy-log-source-empathy.c
new file mode 100644
index 000000000..678ac423a
--- /dev/null
+++ b/libempathy/empathy-log-source-empathy.c
@@ -0,0 +1,688 @@
+/* -*- 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 <xclaesse@gmail.com>
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <glib/gstdio.h>
+
+#include "empathy-log-manager.h"
+#include "empathy-log-source-empathy.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 \
+ "<?xml version='1.0' encoding='utf-8'?>\n" \
+ "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
+ "<log>\n"
+
+#define LOG_FOOTER \
+ "</log>\n"
+
+static gchar *
+log_source_empathy_get_basedir (void)
+{
+ return g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
+ ".gnome2", PACKAGE_NAME, "logs", NULL);
+}
+
+static gchar *
+log_source_empathy_get_dir (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
+{
+ const gchar *account_id;
+ gchar *basedir, *log_dir;
+
+ account_id = mc_account_get_unique_name (account);
+
+ log_dir = log_source_empathy_get_basedir ();
+
+ if (chatroom)
+ basedir = g_build_path (G_DIR_SEPARATOR_S, log_dir, account_id,
+ LOG_DIR_CHATROOMS, chat_id, NULL);
+ else
+ basedir = g_build_path (G_DIR_SEPARATOR_S, log_dir,
+ account_id, chat_id, NULL);
+
+ g_free (log_dir);
+
+ 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 (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
+{
+ gchar *basedir;
+ gchar *timestamp;
+ gchar *filename;
+
+ basedir = log_source_empathy_get_dir (manager, 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 void
+log_source_empathy_add_message (EmpathyLogManager *manager,
+ const gchar *chat_id,
+ gboolean chatroom,
+ EmpathyMessage *message)
+{
+ 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_if_fail (EMPATHY_IS_LOG_MANAGER (manager));
+ g_return_if_fail (chat_id != NULL);
+ g_return_if_fail (EMPATHY_IS_MESSAGE (message));
+
+ 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;
+
+ filename = log_source_empathy_get_filename (manager, 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,
+ "<message time='%s' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
+ "%s</message>\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);
+}
+
+static gboolean
+log_source_empathy_exists (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
+{
+ gchar *dir;
+ gboolean exists;
+
+ dir = log_source_empathy_get_dir (manager, 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 (EmpathyLogManager *manager,
+ 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_MANAGER (manager), 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 (manager, 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 (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom,
+ const gchar *date)
+{
+ gchar *basedir;
+ gchar *timestamp;
+ gchar *filename;
+
+ basedir = log_source_empathy_get_dir (manager, 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 (EmpathyLogManager *manager,
+ 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 (EmpathyLogManager *manager,
+ 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_MANAGER (manager), 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 (manager, 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 (EmpathyLogManager *manager,
+ const gchar *dir)
+{
+ GDir *gdir;
+ GList *files = NULL;
+ const gchar *name;
+ gchar *basedir;
+
+ basedir = dir ? g_strdup (dir) : log_source_empathy_get_basedir ();
+
+ gdir = g_dir_open (basedir, 0, NULL);
+ if (!gdir)
+ {
+ g_free (basedir);
+ 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 (manager, filename));
+ }
+
+ g_free (filename);
+ }
+
+ g_dir_close (gdir);
+ g_free (basedir);
+
+ return files;
+}
+
+static GList *
+log_source_empathy_search_new (EmpathyLogManager *manager,
+ const gchar *text)
+{
+ GList *files, *l;
+ GList *hits = NULL;
+ gchar *text_casefold;
+
+ g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), 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 (manager, 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 (manager, 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 (EmpathyLogManager *manager,
+ 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 (manager, 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 (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom,
+ const gchar *date)
+{
+ gchar *filename;
+ GList *messages;
+
+ 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);
+
+ filename = log_source_empathy_get_filename_for_date (manager, account,
+ chat_id, chatroom, date);
+ messages = log_source_empathy_get_messages_for_file (manager, filename);
+ g_free (filename);
+
+ return messages;
+}
+
+static GList *
+log_source_empathy_get_last_messages (EmpathyLogManager *manager,
+ McAccount *account,
+ const gchar *chat_id,
+ gboolean chatroom)
+{
+ GList *messages = NULL;
+ GList *dates;
+ GList *l;
+
+ 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 = log_source_empathy_get_dates (manager, account, chat_id, chatroom);
+
+ l = g_list_last (dates);
+ if (l)
+ messages = log_source_empathy_get_messages_for_date (manager, account,
+ chat_id, chatroom, l->data);
+
+ g_list_foreach (dates, (GFunc) g_free, NULL);
+ g_list_free (dates);
+
+ return messages;
+}
+
+static GList *
+log_source_empathy_get_chats (EmpathyLogManager *manager,
+ McAccount *account)
+{
+ gchar *basedir;
+ gchar *dir;
+ GList *hits;
+
+ basedir = log_source_empathy_get_basedir ();
+ dir = g_build_filename (basedir, mc_account_get_unique_name (account),
+ NULL);
+ g_free (basedir);
+
+ hits = log_source_empathy_get_chats_for_dir (manager, dir, FALSE);
+
+ g_free (dir);
+
+ return hits;
+}
+
+EmpathyLogSource *
+empathy_log_source_empathy_get_source (void)
+{
+ EmpathyLogSource *source;
+ source = g_slice_new0 (EmpathyLogSource);
+
+ source->exists = log_source_empathy_exists;
+ source->add_message = log_source_empathy_add_message;
+ source->get_dates = log_source_empathy_get_dates;
+ source->get_last_messages = log_source_empathy_get_last_messages;
+ source->get_messages_for_date = log_source_empathy_get_messages_for_date;
+ source->get_chats = log_source_empathy_get_chats;
+ source->search_new = log_source_empathy_search_new;
+
+ return source;
+}