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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Xavier Claessens <xclaesse@gmail.com>
22 * Jonny Lamb <jonny.lamb@collabora.co.uk>
31 /* FIXME: g_mapped_file_free has been deprecated in GLib 2.22, but the
32 * replacement symbol, g_mapped_file_unref is not available in older Glib
33 * and we're not ready to bump our version requirement just for this. When
34 * we're ready to bump our version requirement, just revert this patch. */
35 #undef G_DISABLE_DEPRECATED
36 #include <glib/gstdio.h>
37 #define G_DISABLE_DEPRECATED
39 #include "empathy-log-store.h"
40 #include "empathy-log-store-empathy.h"
41 #include "empathy-log-manager.h"
42 #include "empathy-account-manager.h"
43 #include "empathy-contact.h"
44 #include "empathy-time.h"
45 #include "empathy-utils.h"
47 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
48 #include "empathy-debug.h"
50 #define LOG_DIR_CREATE_MODE (S_IRUSR | S_IWUSR | S_IXUSR)
51 #define LOG_FILE_CREATE_MODE (S_IRUSR | S_IWUSR)
52 #define LOG_DIR_CHATROOMS "chatrooms"
53 #define LOG_FILENAME_SUFFIX ".log"
54 #define LOG_TIME_FORMAT_FULL "%Y%m%dT%H:%M:%S"
55 #define LOG_TIME_FORMAT "%Y%m%d"
57 "<?xml version='1.0' encoding='utf-8'?>\n" \
58 "<?xml-stylesheet type=\"text/xsl\" href=\"empathy-log.xsl\"?>\n" \
65 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreEmpathy)
70 EmpathyAccountManager *account_manager;
71 } EmpathyLogStoreEmpathyPriv;
73 static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
75 G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy,
76 G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
77 log_store_iface_init));
80 log_store_empathy_finalize (GObject *object)
82 EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
83 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
85 g_object_unref (priv->account_manager);
86 g_free (priv->basedir);
91 empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass)
93 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95 object_class->finalize = log_store_empathy_finalize;
97 g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv));
101 empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
103 EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
104 EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv);
108 priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
109 ".gnome2", PACKAGE_NAME, "logs", NULL);
111 priv->name = g_strdup ("Empathy");
112 priv->account_manager = empathy_account_manager_dup_singleton ();
116 log_store_empathy_get_dir (EmpathyLogStore *self,
117 EmpathyAccount *account,
118 const gchar *chat_id,
121 const gchar *account_id;
123 EmpathyLogStoreEmpathyPriv *priv;
125 priv = GET_PRIV (self);
127 account_id = empathy_account_get_unique_name (account);
130 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
131 LOG_DIR_CHATROOMS, chat_id, NULL);
133 basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
134 account_id, chat_id, NULL);
140 log_store_empathy_get_timestamp_filename (void)
146 t = empathy_time_get_current ();
147 time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
148 filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
156 log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
160 t = empathy_message_get_timestamp (message);
162 /* We keep the timestamps in the messages as UTC. */
163 return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
167 log_store_empathy_get_filename (EmpathyLogStore *self,
168 EmpathyAccount *account,
169 const gchar *chat_id,
176 basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
177 timestamp = log_store_empathy_get_timestamp_filename ();
178 filename = g_build_filename (basedir, timestamp, NULL);
187 log_store_empathy_add_message (EmpathyLogStore *self,
188 const gchar *chat_id,
190 EmpathyMessage *message,
194 EmpathyAccount *account;
195 EmpathyContact *sender;
196 const gchar *body_str;
198 EmpathyAvatar *avatar;
199 gchar *avatar_token = NULL;
206 TpChannelTextMessageType msg_type;
208 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
209 g_return_val_if_fail (chat_id != NULL, FALSE);
210 g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
212 sender = empathy_message_get_sender (message);
213 account = empathy_contact_get_account (sender);
214 body_str = empathy_message_get_body (message);
215 msg_type = empathy_message_get_tptype (message);
217 if (EMP_STR_EMPTY (body_str))
220 filename = log_store_empathy_get_filename (self, account, chat_id, chatroom);
221 basedir = g_path_get_dirname (filename);
222 if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
224 DEBUG ("Creating directory:'%s'", basedir);
225 g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
229 DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
231 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
233 file = g_fopen (filename, "w+");
235 g_fprintf (file, LOG_HEADER);
237 g_chmod (filename, LOG_FILE_CREATE_MODE);
241 file = g_fopen (filename, "r+");
243 fseek (file, - strlen (LOG_FOOTER), SEEK_END);
246 body = g_markup_escape_text (body_str, -1);
247 timestamp = log_store_empathy_get_timestamp_from_message (message);
249 str = empathy_contact_get_name (sender);
250 contact_name = g_markup_escape_text (str, -1);
252 str = empathy_contact_get_id (sender);
253 contact_id = g_markup_escape_text (str, -1);
255 avatar = empathy_contact_get_avatar (sender);
257 avatar_token = g_markup_escape_text (avatar->token, -1);
260 "<message time='%s' cm_id='%d' id='%s' name='%s' token='%s' isuser='%s' type='%s'>"
261 "%s</message>\n" LOG_FOOTER, timestamp,
262 empathy_message_get_id (message),
263 contact_id, contact_name,
264 avatar_token ? avatar_token : "",
265 empathy_contact_is_user (sender) ? "true" : "false",
266 empathy_message_type_to_str (msg_type), body);
271 g_free (contact_name);
274 g_free (avatar_token);
280 log_store_empathy_exists (EmpathyLogStore *self,
281 EmpathyAccount *account,
282 const gchar *chat_id,
288 dir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
289 exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
296 log_store_empathy_get_dates (EmpathyLogStore *self,
297 EmpathyAccount *account,
298 const gchar *chat_id,
305 const gchar *filename;
308 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
309 g_return_val_if_fail (chat_id != NULL, NULL);
311 directory = log_store_empathy_get_dir (self, account, chat_id, chatroom);
312 dir = g_dir_open (directory, 0, NULL);
315 DEBUG ("Could not open directory:'%s'", directory);
320 DEBUG ("Collating a list of dates in:'%s'", directory);
322 while ((filename = g_dir_read_name (dir)) != NULL)
324 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
327 p = strstr (filename, LOG_FILENAME_SUFFIX);
328 date = g_strndup (filename, p - filename);
333 if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
336 dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
342 DEBUG ("Parsed %d dates", g_list_length (dates));
348 log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
349 EmpathyAccount *account,
350 const gchar *chat_id,
358 basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
359 timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
360 filename = g_build_filename (basedir, timestamp, NULL);
368 static EmpathyLogSearchHit *
369 log_store_empathy_search_hit_new (EmpathyLogStore *self,
370 const gchar *filename)
372 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
373 EmpathyLogSearchHit *hit;
374 const gchar *account_name;
379 if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
382 strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
383 len = g_strv_length (strv);
385 hit = g_slice_new0 (EmpathyLogSearchHit);
387 end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
388 hit->date = g_strndup (strv[len-1], end - strv[len-1]);
389 hit->chat_id = g_strdup (strv[len-2]);
390 hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
392 if (hit->is_chatroom)
393 account_name = strv[len-4];
395 account_name = strv[len-3];
397 hit->account = empathy_account_manager_lookup (priv->account_manager,
399 hit->filename = g_strdup (filename);
407 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
408 const gchar *filename)
410 GList *messages = NULL;
411 xmlParserCtxtPtr ctxt;
415 EmpathyLogSearchHit *hit;
416 EmpathyAccount *account;
418 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
419 g_return_val_if_fail (filename != NULL, NULL);
421 DEBUG ("Attempting to parse filename:'%s'...", filename);
423 if (!g_file_test (filename, G_FILE_TEST_EXISTS))
425 DEBUG ("Filename:'%s' does not exist", filename);
429 /* Get the account from the filename */
430 hit = log_store_empathy_search_hit_new (self, filename);
431 account = g_object_ref (hit->account);
432 empathy_log_manager_search_hit_free (hit);
435 ctxt = xmlNewParserCtxt ();
437 /* Parse and validate the file. */
438 doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
441 g_warning ("Failed to parse file:'%s'", filename);
442 xmlFreeParserCtxt (ctxt);
446 /* The root node, presets. */
447 log_node = xmlDocGetRootElement (doc);
451 xmlFreeParserCtxt (ctxt);
455 /* Now get the messages. */
456 for (node = log_node->children; node; node = node->next)
458 EmpathyMessage *message;
459 EmpathyContact *sender;
464 gchar *sender_avatar_token;
467 gboolean is_user = FALSE;
471 TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
473 if (strcmp (node->name, "message") != 0)
476 body = xmlNodeGetContent (node);
477 time = xmlGetProp (node, "time");
478 sender_id = xmlGetProp (node, "id");
479 sender_name = xmlGetProp (node, "name");
480 sender_avatar_token = xmlGetProp (node, "token");
481 is_user_str = xmlGetProp (node, "isuser");
482 msg_type_str = xmlGetProp (node, "type");
483 cm_id_str = xmlGetProp (node, "cm_id");
486 is_user = strcmp (is_user_str, "true") == 0;
489 msg_type = empathy_message_type_from_str (msg_type_str);
492 cm_id = atoi (cm_id_str);
494 t = empathy_time_parse (time);
496 sender = empathy_contact_new_for_log (account, sender_id, sender_name,
499 if (!EMP_STR_EMPTY (sender_avatar_token))
500 empathy_contact_load_avatar_cache (sender,
501 sender_avatar_token);
503 message = empathy_message_new (body);
504 empathy_message_set_sender (message, sender);
505 empathy_message_set_timestamp (message, t);
506 empathy_message_set_tptype (message, msg_type);
507 empathy_message_set_is_backlog (message, TRUE);
510 empathy_message_set_id (message, cm_id);
512 messages = g_list_append (messages, message);
514 g_object_unref (sender);
517 xmlFree (sender_name);
519 xmlFree (is_user_str);
520 xmlFree (msg_type_str);
522 xmlFree (sender_avatar_token);
525 DEBUG ("Parsed %d messages", g_list_length (messages));
528 xmlFreeParserCtxt (ctxt);
534 log_store_empathy_get_all_files (EmpathyLogStore *self,
540 const gchar *basedir;
541 EmpathyLogStoreEmpathyPriv *priv;
543 priv = GET_PRIV (self);
545 basedir = dir ? dir : priv->basedir;
547 gdir = g_dir_open (basedir, 0, NULL);
551 while ((name = g_dir_read_name (gdir)) != NULL)
555 filename = g_build_filename (basedir, name, NULL);
556 if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
558 files = g_list_prepend (files, filename);
562 if (g_file_test (filename, G_FILE_TEST_IS_DIR))
564 /* Recursively get all log files */
565 files = g_list_concat (files,
566 log_store_empathy_get_all_files (self, filename));
578 log_store_empathy_search_new (EmpathyLogStore *self,
583 gchar *text_casefold;
585 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
586 g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
588 text_casefold = g_utf8_casefold (text, -1);
590 files = log_store_empathy_get_all_files (self, NULL);
591 DEBUG ("Found %d log files in total", g_list_length (files));
593 for (l = files; l; l = g_list_next (l))
599 gchar *contents_casefold;
603 file = g_mapped_file_new (filename, FALSE, NULL);
607 length = g_mapped_file_get_length (file);
608 contents = g_mapped_file_get_contents (file);
609 contents_casefold = g_utf8_casefold (contents, length);
611 g_mapped_file_free (file);
613 if (strstr (contents_casefold, text_casefold))
615 EmpathyLogSearchHit *hit;
617 hit = log_store_empathy_search_hit_new (self, filename);
621 hits = g_list_prepend (hits, hit);
622 DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
623 text, hit->filename, hit->date);
627 g_free (contents_casefold);
632 g_free (text_casefold);
638 log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
640 gboolean is_chatroom)
645 GError *error = NULL;
647 gdir = g_dir_open (dir, 0, &error);
650 DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
651 g_error_free (error);
655 while ((name = g_dir_read_name (gdir)) != NULL)
657 EmpathyLogSearchHit *hit;
659 if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
661 gchar *filename = g_build_filename (dir, name, NULL);
662 hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
663 self, filename, TRUE));
667 hit = g_slice_new0 (EmpathyLogSearchHit);
668 hit->chat_id = g_strdup (name);
669 hit->is_chatroom = is_chatroom;
671 hits = g_list_prepend (hits, hit);
681 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
682 EmpathyAccount *account,
683 const gchar *chat_id,
690 g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
691 g_return_val_if_fail (chat_id != NULL, NULL);
693 filename = log_store_empathy_get_filename_for_date (self, account,
694 chat_id, chatroom, date);
695 messages = log_store_empathy_get_messages_for_file (self, filename);
702 log_store_empathy_get_chats (EmpathyLogStore *self,
703 EmpathyAccount *account)
707 EmpathyLogStoreEmpathyPriv *priv;
709 priv = GET_PRIV (self);
711 dir = g_build_filename (priv->basedir,
712 empathy_account_get_unique_name (account), NULL);
714 hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
722 log_store_empathy_get_name (EmpathyLogStore *self)
724 EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
730 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
731 EmpathyAccount *account,
732 const gchar *chat_id,
735 EmpathyLogMessageFilter filter,
738 GList *dates, *l, *messages = NULL;
741 dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
743 for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
745 GList *new_messages, *n, *next;
747 /* FIXME: We should really restrict the message parsing to get only
748 * the newest num_messages. */
749 new_messages = log_store_empathy_get_messages_for_date (self, account,
750 chat_id, chatroom, l->data);
755 next = g_list_next (n);
756 if (!filter (n->data, user_data))
758 g_object_unref (n->data);
759 new_messages = g_list_delete_link (new_messages, n);
767 messages = g_list_concat (messages, new_messages);
770 g_list_foreach (dates, (GFunc) g_free, NULL);
777 log_store_iface_init (gpointer g_iface,
780 EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
782 iface->get_name = log_store_empathy_get_name;
783 iface->exists = log_store_empathy_exists;
784 iface->add_message = log_store_empathy_add_message;
785 iface->get_dates = log_store_empathy_get_dates;
786 iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
787 iface->get_chats = log_store_empathy_get_chats;
788 iface->search_new = log_store_empathy_search_new;
789 iface->ack_message = NULL;
790 iface->get_filtered_messages = log_store_empathy_get_filtered_messages;