1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2003-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Jonny Lamb <jonny.lamb@collabora.co.uk>
30 #include <glib/gstdio.h>
32 #include "empathy-log-source.h"
33 #include "empathy-log-source-empathy.h"
34 #include "empathy-log-manager.h"
35 #include "empathy-contact.h"
36 #include "empathy-time.h"
37 #include "empathy-utils.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
40 #include "empathy-debug.h"
42 #define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
43 #define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
44 #define LOG_DIR_CHATROOMS "chatrooms"
45 #define LOG_FILENAME_SUFFIX ".log"
46 #define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S"
47 #define LOG_TIME_FORMAT "%Y%m%d"
49 "<?xml version='1.0' encoding='utf-8'?>\n" \
50 "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
57 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogSourceEmpathy)
62 } EmpathyLogSourceEmpathyPriv;
64 static void log_source_iface_init (gpointer g_iface,gpointer iface_data);
66 G_DEFINE_TYPE_WITH_CODE (EmpathyLogSourceEmpathy, empathy_log_source_empathy,
67 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_SOURCE,
68 log_source_iface_init));
71 log_source_empathy_finalize (GObject *object)
73 EmpathyLogSourceEmpathy *self = EMPATHY_LOG_SOURCE_EMPATHY (object);
74 EmpathyLogSourceEmpathyPriv *priv = GET_PRIV (self);
76 g_free (priv->basedir);
81 empathy_log_source_empathy_class_init (EmpathyLogSourceEmpathyClass *klass)
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
85 object_class->finalize = log_source_empathy_finalize;
87 g_type_class_add_private (object_class, sizeof (EmpathyLogSourceEmpathyPriv));
91 empathy_log_source_empathy_init (EmpathyLogSourceEmpathy *self)
93 EmpathyLogSourceEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
94 EMPATHY_TYPE_LOG_SOURCE_EMPATHY, EmpathyLogSourceEmpathyPriv);
98 priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
99 ".gnome2", PACKAGE_NAME, "logs", NULL);
101 priv->name = g_strdup ("Empathy");
105 log_source_empathy_get_dir (EmpathyLogSource *self,
107 const gchar *chat_id,
110 const gchar *account_id;
112 EmpathyLogSourceEmpathyPriv *priv;
114 priv = GET_PRIV (self);
116 account_id = mc_account_get_unique_name (account);
119 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
120 LOG_DIR_CHATROOMS, chat_id, NULL);
122 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
123 account_id, chat_id, NULL);
129 log_source_empathy_get_timestamp_filename (void)
135 t = empathy_time_get_current ();
136 time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
137 filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
145 log_source_empathy_get_timestamp_from_message (EmpathyMessage *message)
149 t = empathy_message_get_timestamp (message);
151 /* We keep the timestamps in the messages as UTC. */
152 return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
156 log_source_empathy_get_filename (EmpathyLogSource *self,
158 const gchar *chat_id,
165 basedir = log_source_empathy_get_dir (self, account, chat_id, chatroom);
166 timestamp = log_source_empathy_get_timestamp_filename ();
167 filename = g_build_filename (basedir, timestamp, NULL);
176 log_source_empathy_add_message (EmpathyLogSource *self,
177 const gchar *chat_id,
179 EmpathyMessage *message)
183 EmpathyContact *sender;
184 const gchar *body_str;
186 EmpathyAvatar *avatar;
187 gchar *avatar_token = NULL;
194 TpChannelTextMessageType msg_type;
196 g_return_if_fail (EMPATHY_IS_LOG_SOURCE (self));
197 g_return_if_fail (chat_id != NULL);
198 g_return_if_fail (EMPATHY_IS_MESSAGE (message));
200 sender = empathy_message_get_sender (message);
201 account = empathy_contact_get_account (sender);
202 body_str = empathy_message_get_body (message);
203 msg_type = empathy_message_get_tptype (message);
205 if (G_STR_EMPTY (body_str))
208 filename = log_source_empathy_get_filename (self, account, chat_id, chatroom);
209 basedir = g_path_get_dirname (filename);
210 if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
212 DEBUG ("Creating directory:'%s'", basedir);
213 g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
217 DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
219 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
221 file = g_fopen (filename, "w+");
223 g_fprintf (file, LOG_HEADER);
225 g_chmod (filename, LOG_FILE_CREATE_MODE);
229 file = g_fopen (filename, "r+");
231 fseek (file, - strlen (LOG_FOOTER), SEEK_END);
234 body = g_markup_escape_text (body_str, -1);
235 timestamp = log_source_empathy_get_timestamp_from_message (message);
237 str = empathy_contact_get_name (sender);
238 contact_name = g_markup_escape_text (str, -1);
240 str = empathy_contact_get_id (sender);
241 contact_id = g_markup_escape_text (str, -1);
243 avatar = empathy_contact_get_avatar (sender);
245 avatar_token = g_markup_escape_text (avatar->token, -1);
248 "<message time='%s' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
249 "%s</message>\n" LOG_FOOTER, timestamp, contact_id, contact_name,
250 avatar_token ? avatar_token : "",
251 empathy_contact_is_user (sender) ? "true" : "false",
252 empathy_message_type_to_str (msg_type), body);
257 g_free (contact_name);
260 g_free (avatar_token);
264 log_source_empathy_exists (EmpathyLogSource *self,
266 const gchar *chat_id,
272 dir = log_source_empathy_get_dir (self, account, chat_id, chatroom);
273 exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
280 log_source_empathy_get_dates (EmpathyLogSource *self,
282 const gchar *chat_id,
289 const gchar *filename;
292 g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL);
293 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
294 g_return_val_if_fail (chat_id != NULL, NULL);
296 directory = log_source_empathy_get_dir (self, account, chat_id, chatroom);
297 dir = g_dir_open (directory, 0, NULL);
300 DEBUG ("Could not open directory:'%s'", directory);
305 DEBUG ("Collating a list of dates in:'%s'", directory);
307 while ((filename = g_dir_read_name (dir)) != NULL)
309 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
312 p = strstr (filename, LOG_FILENAME_SUFFIX);
313 date = g_strndup (filename, p - filename);
318 if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
321 dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
327 DEBUG ("Parsed %d dates", g_list_length (dates));
333 log_source_empathy_get_filename_for_date (EmpathyLogSource *self,
335 const gchar *chat_id,
343 basedir = log_source_empathy_get_dir (self, account, chat_id, chatroom);
344 timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
345 filename = g_build_filename (basedir, timestamp, NULL);
353 static EmpathyLogSearchHit *
354 log_source_empathy_search_hit_new (EmpathyLogSource *self,
355 const gchar *filename)
357 EmpathyLogSearchHit *hit;
358 const gchar *account_name;
363 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
366 strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
367 len = g_strv_length (strv);
369 hit = g_slice_new0 (EmpathyLogSearchHit);
371 end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
372 hit->date = g_strndup (strv[len-1], end - strv[len-1]);
373 hit->chat_id = g_strdup (strv[len-2]);
374 hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
376 if (hit->is_chatroom)
377 account_name = strv[len-4];
379 account_name = strv[len-3];
381 hit->account = mc_account_lookup (account_name);
382 hit->filename = g_strdup (filename);
390 log_source_empathy_get_messages_for_file (EmpathyLogSource *self,
391 const gchar *filename)
393 GList *messages = NULL;
394 xmlParserCtxtPtr ctxt;
398 EmpathyLogSearchHit *hit;
401 g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL);
402 g_return_val_if_fail (filename != NULL, NULL);
404 DEBUG ("Attempting to parse filename:'%s'...", filename);
406 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
408 DEBUG ("Filename:'%s' does not exist", filename);
412 /* Get the account from the filename */
413 hit = log_source_empathy_search_hit_new (self, filename);
414 account = g_object_ref (hit->account);
415 empathy_log_manager_search_hit_free (hit);
418 ctxt = xmlNewParserCtxt ();
420 /* Parse and validate the file. */
421 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
424 g_warning ("Failed to parse file:'%s'", filename);
425 xmlFreeParserCtxt (ctxt);
429 /* The root node, presets. */
430 log_node = xmlDocGetRootElement (doc);
434 xmlFreeParserCtxt (ctxt);
438 /* Now get the messages. */
439 for (node = log_node->children; node; node = node->next)
441 EmpathyMessage *message;
442 EmpathyContact *sender;
447 gchar *sender_avatar_token;
450 gboolean is_user = FALSE;
452 TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
454 if (strcmp (node->name, "message") != 0)
457 body = xmlNodeGetContent (node);
458 time = xmlGetProp (node, "time");
459 sender_id = xmlGetProp (node, "id");
460 sender_name = xmlGetProp (node, "name");
461 sender_avatar_token = xmlGetProp (node, "token");
462 is_user_str = xmlGetProp (node, "isuser");
463 msg_type_str = xmlGetProp (node, "type");
466 is_user = strcmp (is_user_str, "true") == 0;
469 msg_type = empathy_message_type_from_str (msg_type_str);
471 t = empathy_time_parse (time);
473 sender = empathy_contact_new_full (account, sender_id, sender_name);
474 empathy_contact_set_is_user (sender, is_user);
475 if (!G_STR_EMPTY (sender_avatar_token))
476 empathy_contact_load_avatar_cache (sender,
477 sender_avatar_token);
479 message = empathy_message_new (body);
480 empathy_message_set_sender (message, sender);
481 empathy_message_set_timestamp (message, t);
482 empathy_message_set_tptype (message, msg_type);
484 messages = g_list_append (messages, message);
486 g_object_unref (sender);
489 xmlFree (sender_name);
491 xmlFree (is_user_str);
492 xmlFree (msg_type_str);
495 DEBUG ("Parsed %d messages", g_list_length (messages));
498 xmlFreeParserCtxt (ctxt);
504 log_source_empathy_get_all_files (EmpathyLogSource *self,
510 const gchar *basedir;
511 EmpathyLogSourceEmpathyPriv *priv;
513 priv = GET_PRIV (self);
515 basedir = dir ? dir : priv->basedir;
517 gdir = g_dir_open (basedir, 0, NULL);
521 while ((name = g_dir_read_name (gdir)) != NULL)
525 filename = g_build_filename (basedir, name, NULL);
526 if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
528 files = g_list_prepend (files, filename);
532 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
534 /* Recursively get all log files */
535 files = g_list_concat (files,
536 log_source_empathy_get_all_files (self, filename));
548 log_source_empathy_search_new (EmpathyLogSource *self,
553 gchar *text_casefold;
555 g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL);
556 g_return_val_if_fail (!G_STR_EMPTY (text), NULL);
558 text_casefold = g_utf8_casefold (text, -1);
560 files = log_source_empathy_get_all_files (self, NULL);
561 DEBUG ("Found %d log files in total", g_list_length (files));
563 for (l = files; l; l = l->next)
569 gchar *contents_casefold;
573 file = g_mapped_file_new (filename, FALSE, NULL);
577 length = g_mapped_file_get_length (file);
578 contents = g_mapped_file_get_contents (file);
579 contents_casefold = g_utf8_casefold (contents, length);
581 g_mapped_file_free (file);
583 if (strstr (contents_casefold, text_casefold))
585 EmpathyLogSearchHit *hit;
587 hit = log_source_empathy_search_hit_new (self, filename);
591 hits = g_list_prepend (hits, hit);
592 DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
593 text, hit->filename, hit->date);
597 g_free (contents_casefold);
602 g_free (text_casefold);
608 log_source_empathy_get_chats_for_dir (EmpathyLogSource *self,
610 gboolean is_chatroom)
616 gdir = g_dir_open (dir, 0, NULL);
620 while ((name = g_dir_read_name (gdir)) != NULL)
622 EmpathyLogSearchHit *hit;
625 filename = g_build_filename (dir, name, NULL);
626 if (strcmp (name, LOG_DIR_CHATROOMS) == 0)
628 hits = g_list_concat (hits, log_source_empathy_get_chats_for_dir (
634 hit = g_slice_new0 (EmpathyLogSearchHit);
635 hit->chat_id = g_strdup (name);
636 hit->is_chatroom = is_chatroom;
638 hits = g_list_prepend (hits, hit);
648 log_source_empathy_get_messages_for_date (EmpathyLogSource *self,
650 const gchar *chat_id,
657 g_return_val_if_fail (EMPATHY_IS_LOG_SOURCE (self), NULL);
658 g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
659 g_return_val_if_fail (chat_id != NULL, NULL);
661 filename = log_source_empathy_get_filename_for_date (self, account,
662 chat_id, chatroom, date);
663 messages = log_source_empathy_get_messages_for_file (self, filename);
670 log_source_empathy_get_chats (EmpathyLogSource *self,
675 EmpathyLogSourceEmpathyPriv *priv;
677 priv = GET_PRIV (self);
679 dir = g_build_filename (priv->basedir,
680 mc_account_get_unique_name (account), NULL);
682 hits = log_source_empathy_get_chats_for_dir (self, dir, FALSE);
690 log_source_empathy_get_name (EmpathyLogSource *self)
692 EmpathyLogSourceEmpathyPriv *priv = GET_PRIV (self);
698 log_source_iface_init (gpointer g_iface,
701 EmpathyLogSourceInterface *iface = (EmpathyLogSourceInterface *) g_iface;
703 iface->get_name = log_source_empathy_get_name;
704 iface->exists = log_source_empathy_exists;
705 iface->add_message = log_source_empathy_add_message;
706 iface->get_dates = log_source_empathy_get_dates;
707 iface->get_messages_for_date = log_source_empathy_get_messages_for_date;
708 iface->get_chats = log_source_empathy_get_chats;
709 iface->search_new = log_source_empathy_search_new;
710 iface->ack_message = NULL;