]> git.0d.be Git - empathy.git/commitdiff
Log chats and display 10 last messages when opening a new chat.
authorXavier Claessens <xclaesse@gmail.com>
Sat, 2 Jun 2007 15:56:53 +0000 (15:56 +0000)
committerXavier Claessens <xclaesse@src.gnome.org>
Sat, 2 Jun 2007 15:56:53 +0000 (15:56 +0000)
2007-06-02  Xavier Claessens  <xclaesse@gmail.com>

* libempathy-gtk/gossip-group-chat.c:
* libempathy-gtk/gossip-private-chat.c:
* libempathy-gtk/gossip-chat.c:
* libempathy/empathy-log-manager.c:
* libempathy/empathy-log-manager.h:
* libempathy/empathy-log-manager.xsl:
* libempathy/Makefile.am: Log chats and display 10 last messages when
opening a new chat.

svn path=/trunk/; revision=114

ChangeLog
libempathy-gtk/gossip-chat.c
libempathy-gtk/gossip-group-chat.c
libempathy-gtk/gossip-private-chat.c
libempathy/Makefile.am
libempathy/empathy-log-manager.c [new file with mode: 0644]
libempathy/empathy-log-manager.h [new file with mode: 0644]
libempathy/empathy-log-manager.xsl [new file with mode: 0644]

index 5ec871fc8e1f5eccb8e866ee22c865ba7913e632..24f684f83ad9ceada2a0d8a83408be24a6497485 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2007-06-02  Xavier Claessens  <xclaesse@gmail.com>
+
+       * libempathy-gtk/gossip-group-chat.c:
+       * libempathy-gtk/gossip-private-chat.c:
+       * libempathy-gtk/gossip-chat.c:
+       * libempathy/empathy-log-manager.c:
+       * libempathy/empathy-log-manager.h:
+       * libempathy/empathy-log-manager.xsl:
+       * libempathy/Makefile.am: Log chats and display 10 last messages when
+       opening a new chat.
+
 2007-06-02  Xavier Claessens  <xclaesse@gmail.com>
 
        * libempathy-gtk/gossip-group-chat.c:
index a29c318185737c68ca986a6ca20be651fd640062..677cdd258d99381948790546a74c9c0615158792 100644 (file)
@@ -36,6 +36,7 @@
 #include <glade/glade.h>
 
 #include <libempathy/empathy-contact-manager.h>
+#include <libempathy/empathy-log-manager.h>
 #include <libempathy/gossip-debug.h>
 #include <libempathy/gossip-utils.h>
 #include <libempathy/gossip-conf.h>
@@ -64,6 +65,7 @@
 
 struct _GossipChatPriv {
        EmpathyContactManager *manager;
+       EmpathyLogManager     *log_manager;
        EmpathyTpChat         *tp_chat;
        GossipChatWindow      *window;
        GtkTooltips           *tooltips;
@@ -74,6 +76,7 @@ struct _GossipChatPriv {
        gint                   sent_messages_index;
        GList                 *compositors;
        guint                  scroll_idle_id;
+       gboolean               first_tp_chat;
        /* Used to automatically shrink a window that has temporarily
         * grown due to long input. 
         */
@@ -139,6 +142,7 @@ static void             chat_state_changed_cb             (EmpathyTpChat   *tp_c
                                                           GossipContact   *contact,
                                                           TelepathyChannelChatState  state,
                                                           GossipChat      *chat);
+static void             chat_add_logs                     (GossipChat      *chat);
 static gboolean         chat_scroll_down_idle_func        (GossipChat      *chat);
 
 enum {
@@ -228,12 +232,14 @@ gossip_chat_init (GossipChat *chat)
        priv = GET_PRIV (chat);
 
        priv->manager = empathy_contact_manager_new ();
+       priv->log_manager = empathy_log_manager_new ();
        priv->tooltips = g_object_ref_sink (gtk_tooltips_new ());
        priv->default_window_height = -1;
        priv->vscroll_visible = FALSE;
        priv->sensitive = TRUE;
        priv->sent_messages = NULL;
        priv->sent_messages_index = -1;
+       priv->first_tp_chat = TRUE;
 
        g_signal_connect (chat->input_text_view,
                          "key_press_event",
@@ -270,51 +276,6 @@ gossip_chat_init (GossipChat *chat)
                                    "misspelled",
                                    "underline", PANGO_UNDERLINE_ERROR,
                                    NULL);
-
-
-
-       /* Turn off scrolling temporarily */
-       gossip_chat_view_scroll (chat->view, FALSE);
-#if 0
-FIXME:
-       /* Add messages from last conversation */
-       log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
-       messages = gossip_log_get_last_for_contact (log_manager, priv->contact);
-       num_messages  = g_list_length (messages);
-
-       for (l = messages, i = 0; l; l = l->next, i++) {
-               message = l->data;
-
-               if (num_messages - i > 10) {
-                       continue;
-               }
-
-               sender = gossip_message_get_sender (message);
-               if (gossip_contact_equal (priv->own_contact, sender)) {
-                       gossip_chat_view_append_message_from_self (view,
-                                                                  message,
-                                                                  priv->own_contact,
-                                                                  priv->own_avatar);
-               } else {
-                       gossip_chat_view_append_message_from_other (view,
-                                                                   message,
-                                                                   sender,
-                                                                   priv->other_avatar);
-               }
-       }
-
-       g_list_foreach (messages, (GFunc) g_object_unref, NULL);
-       g_list_free (messages);
-#endif
-       /* Turn back on scrolling */
-       gossip_chat_view_scroll (chat->view, TRUE);
-
-       /* Scroll to the most recent messages, we reference the chat
-        * for the duration of the scroll func.
-        */
-       priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func, 
-                                          g_object_ref (chat));
-
 }
 
 static void
@@ -337,6 +298,7 @@ chat_finalize (GObject *object)
        chat_composing_remove_timeout (chat);
        g_object_unref (chat->account);
        g_object_unref (priv->manager);
+       g_object_unref (priv->log_manager);
        g_object_unref (priv->tooltips);
 
        if (priv->tp_chat) {
@@ -377,10 +339,9 @@ static void
 chat_send (GossipChat  *chat,
           const gchar *msg)
 {
-       GossipChatPriv   *priv;
-       //GossipLogManager *log_manager;
-       GossipMessage    *message;
-       GossipContact    *own_contact;
+       GossipChatPriv    *priv;
+       GossipMessage     *message;
+       GossipContact     *own_contact;
 
        priv = GET_PRIV (chat);
 
@@ -398,15 +359,10 @@ chat_send (GossipChat  *chat,
        /* FIXME: add here something to let group/privrate chat handle
         *        some special messages */
 
-       /* FIXME: gossip_app_set_not_away ();*/
-
        own_contact = empathy_contact_manager_get_user (priv->manager, chat->account);
        message = gossip_message_new (msg);
        gossip_message_set_sender (message, own_contact);
 
-       //FIXME: log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
-       //gossip_log_message_for_contact (log_manager, message, FALSE);
-
        empathy_tp_chat_send (priv->tp_chat, message);
 
        g_object_unref (message);
@@ -443,8 +399,7 @@ chat_message_received_cb (EmpathyTpChat *tp_chat,
                          GossipChat    *chat)
 {
        GossipChatPriv *priv;
-       //GossipLogManager      *log_manager;
-       GossipContact         *sender;
+       GossipContact  *sender;
 
        priv = GET_PRIV (chat);
 
@@ -452,10 +407,10 @@ chat_message_received_cb (EmpathyTpChat *tp_chat,
        gossip_debug (DEBUG_DOMAIN, "Appending message ('%s')",
                      gossip_contact_get_name (sender));
 
-/*FIXME:
-       log_manager = gossip_session_get_log_manager (gossip_app_get_session ());
-       gossip_log_message_for_contact (log_manager, message, TRUE);
-*/
+       empathy_log_manager_add_message (priv->log_manager,
+                                        gossip_chat_get_id (chat),
+                                        message);
+
        gossip_chat_view_append_message (chat->view, message);
 
        if (gossip_chat_should_play_sound (chat)) {
@@ -1129,6 +1084,52 @@ chat_state_changed_cb (EmpathyTpChat             *tp_chat,
        }
 }
 
+static void
+chat_add_logs (GossipChat *chat)
+{
+       GossipChatPriv *priv;
+       GList          *messages, *l;
+       guint           num_messages;
+       guint           i;
+
+       priv = GET_PRIV (chat);
+
+       /* Turn off scrolling temporarily */
+       gossip_chat_view_scroll (chat->view, FALSE);
+
+       /* Add messages from last conversation */
+       messages = empathy_log_manager_get_last_messages (priv->log_manager,
+                                                         chat->account,
+                                                         gossip_chat_get_id (chat));
+       num_messages  = g_list_length (messages);
+
+       for (l = messages, i = 0; l; l = l->next, i++) {
+               GossipMessage *message;
+
+               message = l->data;
+
+               /* Only add 10 last messages */
+               if (num_messages - i > 10) {
+                       g_object_unref (message);
+                       continue;
+               }
+
+
+               gossip_chat_view_append_message (chat->view, message);
+               g_object_unref (message);
+       }
+       g_list_free (messages);
+
+       /* Turn back on scrolling */
+       gossip_chat_view_scroll (chat->view, TRUE);
+
+       /* Scroll to the most recent messages, we reference the chat
+        * for the duration of the scroll func.
+        */
+       priv->scroll_idle_id = g_idle_add ((GSourceFunc) chat_scroll_down_idle_func, 
+                                          g_object_ref (chat));
+}
+
 /* Scroll down after the back-log has been received. */
 static gboolean
 chat_scroll_down_idle_func (GossipChat *chat)
@@ -1308,6 +1309,12 @@ gossip_chat_set_tp_chat (GossipChat    *chat,
        priv->tp_chat = g_object_ref (tp_chat);
        priv->id = g_strdup (empathy_tp_chat_get_id (tp_chat));
 
+       if (priv->first_tp_chat) {
+               chat_add_logs (chat);
+               priv->first_tp_chat = FALSE;
+       }
+
+
        g_signal_connect (tp_chat, "message-received",
                          G_CALLBACK (chat_message_received_cb),
                          chat);
index 493e07461308f07bb74f93dc2ac6271b426d86be..8e8aa1967f15b1bf1468530a8cff0746caf93f9b 100644 (file)
@@ -176,9 +176,9 @@ gossip_group_chat_new (McAccount *account,
 
        priv = GET_PRIV (chat);
 
+       GOSSIP_CHAT (chat)->account = g_object_ref (account);
        priv->tp_chat = empathy_tp_chatroom_new (account, tp_chan);
        gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), EMPATHY_TP_CHAT (priv->tp_chat));
-       GOSSIP_CHAT (chat)->account = g_object_ref (account);
 
        /* FIXME: Ask the user before accepting */
        empathy_tp_chatroom_accept_invitation (priv->tp_chat);
index 12e83dcccfb56eebe04fb42c25ca22191cf53447..f047f12e290b9a81e0eb960faa0e6df1b7ceb18e 100644 (file)
@@ -307,13 +307,12 @@ private_chat_setup (GossipPrivateChat *chat,
 
        priv = GET_PRIV (chat);
 
-       gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), tp_chat);
-
-       priv->contact = g_object_ref (contact);
        GOSSIP_CHAT (chat)->account = g_object_ref (gossip_contact_get_account (contact));
-
+       priv->contact = g_object_ref (contact);
        priv->name = g_strdup (gossip_contact_get_name (contact));
 
+       gossip_chat_set_tp_chat (GOSSIP_CHAT (chat), tp_chat);
+
        g_signal_connect (priv->contact, 
                          "notify::name",
                          G_CALLBACK (private_chat_contact_updated_cb),
index 2b1d75ecf4e394c89892768c04e820b6bc750be7..72f740e5d16e247c130f076b6f258cbbc1e8bf77 100644 (file)
@@ -33,6 +33,7 @@ libempathy_la_SOURCES =                                                       \
        empathy-tp-chatroom.c           empathy-tp-chatroom.h                   \
        empathy-chandler.c              empathy-chandler.h                      \
        empathy-idle.c                  empathy-idle.h                          \
+       empathy-log-manager.c           empathy-log-manager.h                   \
        empathy-marshal-main.c
 
 libempathy_la_LIBADD =         \
@@ -55,9 +56,14 @@ dtddir = $(datadir)/empathy
 dtd_DATA =                                     \
        gossip-chatroom-manager.dtd
 
+stylesheetdir = $(datadir)/empathy
+stylesheet_DATA =              \
+       empathy-log-manager.xsl
+
 EXTRA_DIST =                   \
        empathy-marshal.list    \
        empathy-chandler.xml    \
+       $(stylesheet_DATA)      \
        $(dtd_DATA)
 
 CLEANFILES = $(BUILT_SOURCES)
diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c
new file mode 100644 (file)
index 0000000..68e20c8
--- /dev/null
@@ -0,0 +1,445 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2007 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 <glib/gstdio.h>
+
+#include "empathy-log-manager.h"
+#include "gossip-contact.h"
+#include "gossip-time.h"
+#include "gossip-debug.h"
+#include "gossip-utils.h"
+
+#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
+                      EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv))
+
+#define DEBUG_DOMAIN "LogManager"
+
+#define LOG_DIR_CREATE_MODE       (S_IRUSR | S_IWUSR | S_IXUSR)
+#define LOG_FILE_CREATE_MODE      (S_IRUSR | S_IWUSR)
+#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=\"gossip-log.xsl\"?>\n" \
+    "<log>\n"
+
+#define LOG_FOOTER \
+    "</log>\n"
+
+struct _EmpathyLogManagerPriv {
+       gboolean dummy;
+};
+
+static void    empathy_log_manager_class_init         (EmpathyLogManagerClass *klass);
+static void    empathy_log_manager_init               (EmpathyLogManager      *manager);
+static void    log_manager_finalize                   (GObject                *object);
+static gchar * log_manager_get_dir                    (McAccount              *account,
+                                                      const gchar            *chat_id);
+static gchar * log_manager_get_filename               (McAccount              *account,
+                                                      const gchar            *chat_id);
+static gchar * log_manager_get_filename_for_date      (McAccount              *account,
+                                                      const gchar            *chat_id,
+                                                      const gchar            *date);
+static gchar * log_manager_get_timestamp_filename     (void);
+static gchar * log_manager_get_timestamp_from_message (GossipMessage          *message);
+
+G_DEFINE_TYPE (EmpathyLogManager, empathy_log_manager, G_TYPE_OBJECT);
+
+static void
+empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->finalize = log_manager_finalize;
+
+       g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
+}
+
+static void
+empathy_log_manager_init (EmpathyLogManager *manager)
+{
+}
+
+static void
+log_manager_finalize (GObject *object)
+{
+}
+
+EmpathyLogManager *
+empathy_log_manager_new (void)
+{
+       static EmpathyLogManager *manager = NULL;
+
+       if (!manager) {
+               manager = g_object_new (EMPATHY_TYPE_LOG_MANAGER, NULL);
+               g_object_add_weak_pointer (G_OBJECT (manager), (gpointer) &manager);
+       } else {
+               g_object_ref (manager);
+       }
+
+       return manager;
+}
+
+void
+empathy_log_manager_add_message (EmpathyLogManager *manager,
+                                const gchar       *chat_id,
+                                GossipMessage     *message)
+{
+       FILE          *file;
+       McAccount     *account;
+       GossipContact *sender;
+       const gchar   *body_str;
+       const gchar   *str;
+       gchar         *filename;
+       gchar         *body;
+       gchar         *timestamp;
+       gchar         *contact_name;
+       gchar         *contact_id;
+
+       g_return_if_fail (EMPATHY_IS_LOG_MANAGER (manager));
+       g_return_if_fail (chat_id != NULL);
+       g_return_if_fail (GOSSIP_IS_MESSAGE (message));
+
+       sender = gossip_message_get_sender (message);
+       account = gossip_contact_get_account (sender);
+       body_str = gossip_message_get_body (message);
+
+       if (G_STR_EMPTY (body_str)) {
+               return;
+       }
+
+       filename = log_manager_get_filename (account, chat_id);
+
+       gossip_debug (DEBUG_DOMAIN, "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 = gossip_contact_get_name (sender);
+       if (!str) {
+               contact_name = g_strdup ("");
+       } else {
+               contact_name = g_markup_escape_text (str, -1);
+       }
+
+       str = gossip_contact_get_id (sender);
+       if (!str) {
+               contact_id = g_strdup ("");
+       } else {
+               contact_id = g_markup_escape_text (str, -1);
+       }
+
+       g_fprintf (file,
+                  "<message time='%s' id='%s' name='%s'>%s</message>\n" LOG_FOOTER,
+                  timestamp,
+                  contact_id,
+                  contact_name,
+                  body);
+
+       fclose (file);
+       g_free (filename);
+       g_free (contact_id);
+       g_free (contact_name);
+       g_free (timestamp);
+       g_free (body);
+}
+
+GList *
+empathy_log_manager_get_dates (EmpathyLogManager *manager,
+                              McAccount         *account,
+                              const gchar       *chat_id)
+{
+       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 (account, chat_id);
+       if (!directory) {
+               return NULL;
+       }
+
+       dir = g_dir_open (directory, 0, NULL);
+       if (!dir) {
+               gossip_debug (DEBUG_DOMAIN, "Could not open directory:'%s'", directory);
+               g_free (directory);
+               return NULL;
+       }
+
+       gossip_debug (DEBUG_DOMAIN, "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;
+               }
+
+               dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
+       }
+
+       g_free (directory);
+       g_dir_close (dir);
+
+       gossip_debug (DEBUG_DOMAIN, "Parsed %d dates", g_list_length (dates));
+
+       return dates;
+}
+
+GList *
+empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
+                                          McAccount         *account,
+                                          const gchar       *chat_id,
+                                          const gchar       *date)
+{
+       gchar            *filename;
+       GList            *messages = NULL;
+       xmlParserCtxtPtr  ctxt;
+       xmlDocPtr         doc;
+       xmlNodePtr        log_node;
+       xmlNodePtr        node;
+
+       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_manager_get_filename_for_date (account, chat_id, date);
+
+       gossip_debug (DEBUG_DOMAIN, "Attempting to parse filename:'%s'...", filename);
+
+       if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
+               gossip_debug (DEBUG_DOMAIN, "Filename:'%s' does not exist", filename);
+               g_free (filename);
+               return NULL;
+       }
+
+       /* 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);
+               g_free (filename);
+               xmlFreeParserCtxt (ctxt);
+               return NULL;
+       }
+
+       /* The root node, presets. */
+       log_node = xmlDocGetRootElement (doc);
+       if (!log_node) {
+               g_free (filename);
+               xmlFreeDoc (doc);
+               xmlFreeParserCtxt (ctxt);
+               return NULL;
+       }
+
+       /* Now get the messages. */
+       for (node = log_node->children; node; node = node->next) {
+               GossipMessage *message;
+               GossipContact *sender;
+               gchar         *time;
+               GossipTime     t;
+               gchar         *sender_id;
+               gchar         *sender_name;
+               gchar         *body;
+
+               if (strcmp (node->name, "message") != 0) {
+                       continue;
+               }
+
+               body = xmlNodeGetContent (node);
+               time = xmlGetProp (node, "time");
+               sender_id = xmlGetProp (node, "id");
+               sender_name = xmlGetProp (node, "name");
+
+               t = gossip_time_parse (time);
+
+               sender = gossip_contact_new_full (account, sender_id, sender_name);
+               message = gossip_message_new (body);
+               gossip_message_set_sender (message, sender);
+               gossip_message_set_timestamp (message, t);
+
+               messages = g_list_append (messages, message);
+
+               g_object_unref (sender);
+               xmlFree (time);
+               xmlFree (sender_id);
+               xmlFree (sender_name);
+               xmlFree (body);
+       }
+
+       gossip_debug (DEBUG_DOMAIN, "Parsed %d messages", g_list_length (messages));
+
+       g_free (filename);
+       xmlFreeDoc (doc);
+       xmlFreeParserCtxt (ctxt);
+
+       return messages;
+}
+
+GList *
+empathy_log_manager_get_last_messages (EmpathyLogManager *manager,
+                                      McAccount         *account,
+                                      const gchar       *chat_id)
+{
+       GList *messages;
+       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 = empathy_log_manager_get_dates (manager, account, chat_id);
+
+       l = g_list_last (dates);
+       messages = empathy_log_manager_get_messages_for_date (manager,
+                                                             account,
+                                                             chat_id,
+                                                             l->data);
+
+       g_list_foreach (dates, (GFunc) g_free, NULL);
+       g_list_free (dates);
+
+       return messages;
+}
+
+static gchar *
+log_manager_get_dir (McAccount   *account,
+                    const gchar *chat_id)
+{
+       const gchar *account_id;
+       gchar       *basedir;
+
+       account_id = mc_account_get_unique_name (account);
+       basedir = g_build_path (G_DIR_SEPARATOR_S,
+                               g_get_home_dir (),
+                               ".gnome2",
+                               PACKAGE_NAME,
+                               "logs",
+                               account_id,
+                               chat_id,
+                               NULL);
+
+       if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
+               gossip_debug (DEBUG_DOMAIN, "Creating directory:'%s'", basedir);
+
+               g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
+       }
+
+       return basedir;
+}
+
+static gchar *
+log_manager_get_filename (McAccount   *account,
+                         const gchar *chat_id)
+{
+       gchar *basedir;
+       gchar *timestamp;
+       gchar *filename;
+
+       basedir = log_manager_get_dir (account, chat_id);
+       timestamp = log_manager_get_timestamp_filename ();
+       filename = g_build_filename (basedir, timestamp, NULL);
+
+       g_free (basedir);
+       g_free (timestamp);
+
+       return filename;
+}
+
+static gchar *
+log_manager_get_filename_for_date (McAccount   *account,
+                                  const gchar *chat_id,
+                                  const gchar *date)
+{
+       gchar *basedir;
+       gchar *timestamp;
+       gchar *filename;
+
+       basedir = log_manager_get_dir (account, chat_id);
+       timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
+       filename = g_build_filename (basedir, timestamp, NULL);
+
+       g_free (basedir);
+       g_free (timestamp);
+
+       return filename;
+}
+
+static gchar *
+log_manager_get_timestamp_filename (void)
+{
+       GossipTime  t;
+       gchar      *time_str;
+       gchar      *filename;
+
+       t = gossip_time_get_current ();
+       time_str = gossip_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_manager_get_timestamp_from_message (GossipMessage *message)
+{
+       GossipTime t;
+
+       t = gossip_message_get_timestamp (message);
+
+       /* We keep the timestamps in the messages as UTC. */
+       return gossip_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
+}
+
diff --git a/libempathy/empathy-log-manager.h b/libempathy/empathy-log-manager.h
new file mode 100644 (file)
index 0000000..8df68d5
--- /dev/null
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2007 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>
+ */
+
+#ifndef __EMPATHY_LOG_MANAGER_H__
+#define __EMPATHY_LOG_MANAGER_H__
+
+#include <glib-object.h>
+
+#include <libmissioncontrol/mc-account.h>
+
+#include "gossip-message.h"
+
+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))
+
+typedef struct _EmpathyLogManager      EmpathyLogManager;
+typedef struct _EmpathyLogManagerClass EmpathyLogManagerClass;
+typedef struct _EmpathyLogManagerPriv  EmpathyLogManagerPriv;
+
+struct _EmpathyLogManager {
+       GObject parent;
+};
+
+struct _EmpathyLogManagerClass {
+       GObjectClass parent_class;
+};
+
+GType              empathy_log_manager_get_type              (void) G_GNUC_CONST;
+EmpathyLogManager *empathy_log_manager_new                   (void);
+void               empathy_log_manager_add_message           (EmpathyLogManager *manager,
+                                                             const gchar       *chat_id,
+                                                             GossipMessage     *message);
+GList *            empathy_log_manager_get_dates             (EmpathyLogManager *manager,
+                                                             McAccount         *account,
+                                                             const gchar       *chat_id);
+GList *            empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
+                                                             McAccount         *account,
+                                                             const gchar       *chat_id,
+                                                             const gchar       *date);
+GList *            empathy_log_manager_get_last_messages     (EmpathyLogManager *manager,
+                                                             McAccount         *account,
+                                                             const gchar       *chat_id);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_LOG_MANAGER_H__ */
diff --git a/libempathy/empathy-log-manager.xsl b/libempathy/empathy-log-manager.xsl
new file mode 100644 (file)
index 0000000..a934f3a
--- /dev/null
@@ -0,0 +1,148 @@
+<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
+
+  <xsl:output method="html" encoding="utf-8" indent="yes"/>
+
+  <xsl:template match="/">
+    <html>
+      <head>
+        <style type="text/css">
+          <xsl:text>
+            body {
+              background: #fff;
+             font-family: Verdana, "Bitstream Vera Sans", Sans-Serif; 
+             font-size: 10pt;
+            }
+            .stamp {
+              color: #999;
+            }
+            .top-day-stamp {
+              color: #999;
+              text-align: center;
+              margin-bottom: 1em;
+            }
+            .new-day-stamp {
+              color: #999;
+              text-align: center;
+              margin-bottom: 1em;
+              margin-top: 1em;
+            }
+            .nick {
+              color: rgb(54,100, 139);
+            }
+            .nick-self {
+              color: rgb(46,139,87);
+            }
+           .nick-highlight {
+              color: rgb(205,92,92);
+            }
+          </xsl:text>
+        </style>
+        <title><xsl:value-of select="$title"/></title>
+      </head>
+      <body>
+        <xsl:apply-templates/>
+      </body>
+    </html>
+  </xsl:template>
+
+  <xsl:template name="get-day">
+    <xsl:param name="stamp"/>
+    <xsl:value-of select="substring ($stamp, 1, 8)"/>
+  </xsl:template>
+
+  <xsl:template name="format-stamp">
+    <xsl:param name="stamp"/>
+    <xsl:variable name="hour" select="substring ($stamp, 10, 2)"/>
+    <xsl:variable name="min" select="substring ($stamp, 13, 2)"/>
+
+    <xsl:value-of select="$hour"/>:<xsl:value-of select="$min"/>
+  </xsl:template>
+
+  <xsl:template name="format-day-stamp">
+    <xsl:param name="stamp"/>
+    <xsl:variable name="year" select="substring ($stamp, 1, 4)"/>
+    <xsl:variable name="month" select="substring ($stamp, 5, 2)"/>
+    <xsl:variable name="day" select="substring ($stamp, 7, 2)"/>
+
+    <xsl:value-of select="$year"/>-<xsl:value-of select="$month"/>-<xsl:value-of select="$day"/>
+  </xsl:template>
+
+  <xsl:template name="header">
+    <xsl:param name="stamp"/>
+    <div class="top-day-stamp">
+      <xsl:call-template name="format-day-stamp">
+        <xsl:with-param name="stamp" select="@time"/>
+      </xsl:call-template>
+    </div>
+  </xsl:template>  
+
+  <xsl:template match="a">
+    <xsl:text disable-output-escaping="yes">&lt;a href="</xsl:text>
+
+    <xsl:value-of disable-output-escaping="yes" select="@href"/>
+
+    <xsl:text disable-output-escaping="yes">"&gt;</xsl:text>
+
+    <xsl:value-of select="@href"/>
+    <xsl:text disable-output-escaping="yes">&lt;/a&gt;</xsl:text>
+  </xsl:template>
+
+  <xsl:template match="log">
+
+    <div class="top-day-stamp">
+      <xsl:call-template name="format-day-stamp">
+        <xsl:with-param name="stamp" select="//message[1]/@time"/>
+      </xsl:call-template>
+    </div>
+
+    <xsl:for-each select="*">
+
+      <xsl:variable name="prev-time">
+        <xsl:call-template name="get-day">
+          <xsl:with-param name="stamp" select="preceding-sibling::*[1]/@time"/>
+        </xsl:call-template>
+      </xsl:variable>
+
+      <xsl:variable name="this-time">
+        <xsl:call-template name="get-day">
+          <xsl:with-param name="stamp" select="@time"/>
+        </xsl:call-template>
+      </xsl:variable>
+
+      <xsl:if test="$prev-time &lt; $this-time">
+        <div class="new-day-stamp">
+        <xsl:call-template name="format-day-stamp">
+          <xsl:with-param name="stamp" select="@time"/>
+        </xsl:call-template>
+        </div>
+      </xsl:if>
+
+      <xsl:variable name="stamp">
+        <xsl:call-template name="format-stamp">
+          <xsl:with-param name="stamp" select="@time"/>
+        </xsl:call-template>
+      </xsl:variable>
+
+      <span class="stamp">
+       <xsl:value-of select="$stamp"/>
+      </span>
+
+      <xsl:variable name="nick-class">
+        <xsl:choose>
+          <xsl:when test="not(string(@id))">nick-self</xsl:when>
+          <xsl:otherwise>nick</xsl:otherwise>
+        </xsl:choose>
+      </xsl:variable>
+
+      <span class="{$nick-class}">
+        &lt;<xsl:value-of select="@name"/>&gt;
+      </span>
+      <xsl:apply-templates/>
+      <br/>
+
+    </xsl:for-each>
+
+  </xsl:template>
+
+</xsl:stylesheet>