diff --git a/configure.ac b/configure.ac
index 7d258a8..67eb851 100644
--- a/configure.ac
+++ b/configure.ac
@@ -178,6 +178,30 @@ fi
 AM_CONDITIONAL(HAVE_ENCHANT, test "x$have_enchant" = "xyes")
 
 # -----------------------------------------------------------
+# sqlite check
+# -----------------------------------------------------------
+AC_ARG_ENABLE(sqlite3,
+              AS_HELP_STRING([--enable-sqlite3=@<:@no/yes/auto@:>@],
+                             [Enable sqlite3 logs]), ,
+                             enable_sqlite3=auto)
+
+if test "x$enable_sqlite3" != "xno"; then
+   PKG_CHECK_MODULES(SQLITE3, [sqlite3], have_sqlite3="yes", have_sqlite3="no")
+
+   if test "x$have_sqlite3" = "xyes"; then
+       AC_DEFINE(HAVE_SQLITE3, 1, [Define if you have sqlite3])
+   fi
+else
+   have_sqlite3=no
+fi
+
+if test "x$enable_sqlite3" = "xyes" -a "x$have_sqlite3" != "xyes"; then
+   AC_MSG_ERROR([Couldn't find sqlite3 dependencies.])
+fi
+
+AM_CONDITIONAL(HAVE_SQLITE3, test "x$have_sqlite3" = "xyes")
+
+# -----------------------------------------------------------
 # Megaphone
 # -----------------------------------------------------------
 AC_ARG_ENABLE(megaphone,
diff --git a/libempathy-gtk/empathy-chat.c b/libempathy-gtk/empathy-chat.c
index 453209e..3455096 100644
--- a/libempathy-gtk/empathy-chat.c
+++ b/libempathy-gtk/empathy-chat.c
@@ -465,15 +465,6 @@ chat_message_received (EmpathyChat *chat, EmpathyMessage *message)
 		empathy_contact_get_name (sender),
 		empathy_contact_get_handle (sender));
 
-	if (priv->id) {
-		gboolean is_chatroom;
-
-		is_chatroom = priv->handle_type == TP_HANDLE_TYPE_ROOM;
-		empathy_log_manager_add_message (priv->log_manager,
-						 priv->id, is_chatroom,
-						 message);
-	}
-
 	empathy_chat_view_append_message (chat->view, message);
 
 	/* We received a message so the contact is no longer composing */
diff --git a/libempathy/Makefile.am b/libempathy/Makefile.am
index 053c0c8..0561214 100644
--- a/libempathy/Makefile.am
+++ b/libempathy/Makefile.am
@@ -7,6 +7,10 @@ AM_CPPFLAGS =                                           \
 	$(LIBEMPATHY_CFLAGS)				\
 	$(WARN_CFLAGS)
 
+if HAVE_SQLITE3
+AM_CPPFLAGS += $(SQLITE3_CFLAGS)
+endif
+
 BUILT_SOURCES = 					\
 	empathy-marshal.h				\
 	empathy-marshal.c				\
@@ -36,6 +40,9 @@ libempathy_la_SOURCES =					\
 	empathy-irc-network-manager.c			\
 	empathy-irc-server.c				\
 	empathy-log-manager.c				\
+	empathy-log-store.c				\
+	empathy-log-store-empathy.c			\
+	empathy-log-store-pidgin.c			\
 	empathy-message.c				\
 	empathy-status-presets.c			\
 	empathy-time.c					\
@@ -50,6 +57,11 @@ libempathy_la_SOURCES =					\
 	empathy-tube-handler.c				\
 	empathy-utils.c
 
+if HAVE_SQLITE3
+libempathy_la_SOURCES += empathy-log-store-sqlite.c
+endif
+
+
 # do not distribute generated files
 nodist_libempathy_la_SOURCES =\
 	$(BUILT_SOURCES)
@@ -58,6 +70,10 @@ libempathy_la_LIBADD =		\
 	$(top_builddir)/extensions/libemp-extensions.la \
 	$(LIBEMPATHY_LIBS)
 
+if HAVE_SQLITE3
+libempathy_la_LIBADD += $(SQLITE3_LIBS)
+endif
+
 libempathy_la_LDFLAGS =		\
        -version-info ${LIBEMPATHY_CURRENT}:${LIBEMPATHY_REVISION}:${LIBEMPATHY_AGE} \
        -export-symbols-regex ^empathy_
diff --git a/libempathy/empathy-log-manager.c b/libempathy/empathy-log-manager.c
index 3d7acb3..2b245d2 100644
--- a/libempathy/empathy-log-manager.c
+++ b/libempathy/empathy-log-manager.c
@@ -19,6 +19,7 @@
  * Boston, MA 02111-1307, USA.
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
  */
 
 #include <config.h>
@@ -28,778 +29,432 @@
 #include <stdlib.h>
 #include <glib/gstdio.h>
 
+#include <telepathy-glib/util.h>
+
 #include "empathy-log-manager.h"
-#include "empathy-contact.h"
-#include "empathy-time.h"
+#include "empathy-log-store-empathy.h"
+#include "empathy-log-store.h"
+#include "empathy-tp-chat.h"
 #include "empathy-utils.h"
 
-#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
-#include "empathy-debug.h"
+#include "empathy-log-store-empathy.h"
+#include "empathy-log-store-pidgin.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"
+#if HAVE_SQLITE3
+#include "empathy-log-store-sqlite.h"
+#endif
 
-#define LOG_FOOTER \
-    "</log>\n"
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
 
 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogManager)
-typedef struct {
-	gchar *basedir;
+typedef struct
+{
+  GList *stores;
 } 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;
 
 static void
-empathy_log_manager_init (EmpathyLogManager *manager)
-{
-	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->stores; l; l = l->next)
+    {
+      EmpathyLogStore *store = (EmpathyLogStore *) l->data;
+      g_object_unref (store);
+    }
 
-	g_free (priv->basedir);
+  g_list_free (priv->stores);
 }
 
 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);
+
+      manager_singleton = EMPATHY_LOG_MANAGER (retval);
+      g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
+    }
+
+  return 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);
+static void
+empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-		manager_singleton = EMPATHY_LOG_MANAGER (retval);
-		g_object_add_weak_pointer (retval, (gpointer *) &manager_singleton);
-	}
+  object_class->constructor = log_manager_constructor;
+  object_class->finalize = log_manager_finalize;
 
-	return retval;
+  g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
 }
 
 static void
-empathy_log_manager_class_init (EmpathyLogManagerClass *klass)
+empathy_log_manager_init (EmpathyLogManager *manager)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  EmpathyLogManagerPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
+      EMPATHY_TYPE_LOG_MANAGER, EmpathyLogManagerPriv);
+
+  priv->stores = g_list_append (priv->stores,
+      g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL));
 
-	object_class->finalize = log_manager_finalize;
-	object_class->constructor = log_manager_constructor;
+  priv->stores = g_list_append (priv->stores,
+      g_object_new (EMPATHY_TYPE_LOG_STORE_PIDGIN, NULL));
 
-	g_type_class_add_private (object_class, sizeof (EmpathyLogManagerPriv));
+#if HAVE_SQLITE3
+  priv->stores = g_list_append (priv->stores,
+      g_object_new (EMPATHY_TYPE_LOG_STORE_SQLITE, NULL));
+#endif
+
+  manager->priv = priv;
 }
 
 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
+gboolean
 empathy_log_manager_add_message (EmpathyLogManager *manager,
-				 const gchar       *chat_id,
-				 gboolean           chatroom,
-				 EmpathyMessage     *message)
+                                 const gchar *chat_id,
+                                 gboolean chatroom,
+                                 EmpathyMessage *message,
+                                 GError **error)
 {
-	FILE          *file;
-	McAccount     *account;
-	EmpathyContact *sender;
-	const gchar   *body_str;
-	const gchar   *str;
-	EmpathyAvatar *avatar;
-	gchar         *avatar_token = NULL;
-	gchar         *filename;
-	gchar         *basedir;
-	gchar         *body;
-	gchar         *timestamp;
-	gchar         *contact_name;
-	gchar         *contact_id;
-	TpChannelTextMessageType msg_type;
-
-	g_return_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);
+  EmpathyLogManagerPriv *priv;
+  GList *l;
+  gboolean out = FALSE;
+  gboolean found = FALSE;
+
+  /* TODO: When multiple log stores appear with add_message implementations
+   * make this customisable. */
+  const gchar *add_store = "Empathy";
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), FALSE);
+  g_return_val_if_fail (chat_id != NULL, FALSE);
+  g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+
+  priv = GET_PRIV (manager);
+
+  for (l = priv->stores; l; l = l->next)
+    {
+      if (!tp_strdiff (empathy_log_store_get_name (
+              EMPATHY_LOG_STORE (l->data)), add_store))
+        {
+          out = empathy_log_store_add_message (EMPATHY_LOG_STORE (l->data),
+              chat_id, chatroom, message, error);
+          found = TRUE;
+          break;
+        }
+    }
+
+  if (!found)
+    DEBUG ("Failed to find chosen log store to write to.");
+
+  return out;
 }
 
 gboolean
 empathy_log_manager_exists (EmpathyLogManager *manager,
-			    McAccount         *account,
-			    const gchar       *chat_id,
-			    gboolean           chatroom)
+                            McAccount *account,
+                            const gchar *chat_id,
+                            gboolean chatroom)
 {
-	gchar    *dir;
-	gboolean  exists;
+  GList *l;
+  EmpathyLogManagerPriv *priv;
+
+  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);
+
+  priv = GET_PRIV (manager);
 
-	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);
+  for (l = priv->stores; l; l = l->next)
+    {
+      if (empathy_log_store_exists (EMPATHY_LOG_STORE (l->data),
+            account, chat_id, chatroom))
+        return TRUE;
+    }
 
-	return exists;
+  return FALSE;
 }
 
-GList *
-empathy_log_manager_get_dates (EmpathyLogManager *manager,
-			       McAccount         *account,
-			       const gchar       *chat_id,
-			       gboolean           chatroom)
+static void
+log_manager_get_dates_foreach (gpointer data,
+                               gpointer user_data)
 {
-	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;
+  /* g_list_first is needed in case an older date was last inserted */
+  GList *orig = g_list_first (user_data);
+
+  if (g_list_find_custom (orig, data, (GCompareFunc) strcmp))
+    orig = g_list_insert_sorted (orig, g_strdup (data), (GCompareFunc) strcmp);
 }
 
 GList *
-empathy_log_manager_get_messages_for_file (EmpathyLogManager *manager,
-					   const gchar       *filename)
+empathy_log_manager_get_dates (EmpathyLogManager *manager,
+                               McAccount *account,
+                               const gchar *chat_id,
+                               gboolean chatroom)
 {
-	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;
+  GList *l, *out = NULL;
+  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);
+
+  for (l = priv->stores; l; l = l->next)
+    {
+      EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
+
+      if (!out)
+        out = empathy_log_store_get_dates (store, account, chat_id, chatroom);
+      else
+        {
+          GList *new = empathy_log_store_get_dates (store, account, chat_id,
+              chatroom);
+          g_list_foreach (new, log_manager_get_dates_foreach, out);
+
+          g_list_foreach (new, (GFunc) g_free, NULL);
+          g_list_free (new);
+
+          /* Similar reason for using g_list_first here as before */
+          out = g_list_first (out);
+        }
+    }
+
+  return out;
 }
 
 GList *
 empathy_log_manager_get_messages_for_date (EmpathyLogManager *manager,
-					   McAccount         *account,
-					   const gchar       *chat_id,
-					   gboolean           chatroom,
-					   const gchar       *date)
+                                           McAccount *account,
+                                           const gchar *chat_id,
+                                           gboolean chatroom,
+                                           const gchar *date)
 {
-	gchar *filename;
-	GList *messages;
+  GList *l, *out = NULL;
+  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), 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 (manager, account, chat_id, chatroom, date);
-	messages = empathy_log_manager_get_messages_for_file (manager, filename);
-	g_free (filename);
+  priv = GET_PRIV (manager);
 
-	return messages;
+  for (l = priv->stores; l; l = l->next)
+    {
+      EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
+
+      out = g_list_concat (out, empathy_log_store_get_messages_for_date (
+          store, account, chat_id, chatroom, date));
+    }
+
+  return out;
 }
 
 GList *
 empathy_log_manager_get_last_messages (EmpathyLogManager *manager,
-				       McAccount         *account,
-				       const gchar       *chat_id,
-				       gboolean           chatroom)
+                                       McAccount *account,
+                                       const gchar *chat_id,
+                                       gboolean chatroom)
 {
-	GList *messages = NULL;
-	GList *dates;
-	GList *l;
+  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);
+  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, chatroom);
+  dates = empathy_log_manager_get_dates (manager, account, chat_id, chatroom);
 
-	l = g_list_last (dates);
-	if (l) {
-		messages = empathy_log_manager_get_messages_for_date (manager,
-								      account,
-								      chat_id,
-								      chatroom,
-								      l->data);
-	}
+  l = g_list_last (dates);
+  if (l)
+    messages = empathy_log_manager_get_messages_for_date (manager, account,
+        chat_id, chatroom, l->data);
 
-	g_list_foreach (dates, (GFunc) g_free, NULL);
-	g_list_free (dates);
+  g_list_foreach (dates, (GFunc) g_free, NULL);
+  g_list_free (dates);
 
-	return messages;
+  return messages;
 }
 
 GList *
 empathy_log_manager_get_chats (EmpathyLogManager *manager,
-			       McAccount         *account)
+                               McAccount *account)
 {
-	const gchar *basedir;
-	gchar       *dir;
+  GList *l, *out = NULL;
+  EmpathyLogManagerPriv *priv;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_MANAGER (manager), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
 
-	basedir = log_manager_get_basedir (manager);
-	dir = g_build_filename (basedir,
-				mc_account_get_unique_name (account),
-				NULL);
+  priv = GET_PRIV (manager);
 
-	return log_manager_get_chats (manager, dir, FALSE);
+  for (l = priv->stores; l; l = l->next)
+    {
+      EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
+
+      out = g_list_concat (out,
+          empathy_log_store_get_chats (store, account));
+    }
+
+  return out;
 }
 
 GList *
 empathy_log_manager_search_new (EmpathyLogManager *manager,
-				const gchar       *text)
+                                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 (!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 (!EMP_STR_EMPTY (text), NULL);
 
-		filename = l->data;
+  priv = GET_PRIV (manager);
 
-		file = g_mapped_file_new (filename, FALSE, NULL);
-		if (!file) {
-			continue;
-		}
+  for (l = priv->stores; l; l = l->next)
+    {
+      EmpathyLogStore *store = EMPATHY_LOG_STORE (l->data);
 
-		length = g_mapped_file_get_length (file);
-		contents = g_mapped_file_get_contents (file);
-		contents_casefold = g_utf8_casefold (contents, length);
+      out = g_list_concat (out,
+          empathy_log_store_search_new (store, text));
+    }
 
-		g_mapped_file_free (file);
-
-		if (strstr (contents_casefold, text_casefold)) {
-			EmpathyLogSearchHit *hit;
-
-			hit = log_manager_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);
-			}
-		}
+  return out;
+}
 
-		g_free (contents_casefold);
-		g_free (filename);
-	}
-	g_list_free (files);
+void
+empathy_log_manager_search_hit_free (EmpathyLogSearchHit *hit)
+{
+  if (hit->account)
+    g_object_unref (hit->account);
 
-	g_free (text_casefold);
+  g_free (hit->date);
+  g_free (hit->filename);
+  g_free (hit->chat_id);
 
-	return hits;
+  g_slice_free (EmpathyLogSearchHit, hit);
 }
 
 void
 empathy_log_manager_search_free (GList *hits)
 {
-	GList *l;
+  GList *l;
 
-	for (l = hits; l; l = l->next) {
-		log_manager_search_hit_free (l->data);
-	}
-	
-	g_list_free (hits);
+  for (l = hits; l; l = l->next)
+    {
+      empathy_log_manager_search_hit_free (l->data);
+    }
+
+  g_list_free (hits);
 }
 
 /* Format is just date, 20061201. */
 gchar *
 empathy_log_manager_get_date_readable (const gchar *date)
 {
-	time_t t;
+  time_t t;
 
-	t = empathy_time_parse (date);
+  t = empathy_time_parse (date);
 
-	return empathy_time_to_string_local (t, "%a %d %b %Y");
+  return empathy_time_to_string_local (t, "%a %d %b %Y");
 }
 
-static const gchar *
-log_manager_get_basedir (EmpathyLogManager *manager)
+typedef struct
 {
-	EmpathyLogManagerPriv *priv;	
-
-	priv = GET_PRIV (manager);
-
-	if (priv->basedir) {
-		return priv->basedir;
-	}
-
-	priv->basedir = g_build_path (G_DIR_SEPARATOR_S,
-				      g_get_home_dir (),
-				      ".gnome2",
-				      PACKAGE_NAME,
-				      "logs",
-				      NULL);
-
-	return priv->basedir;
-}
-
-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;
-}
-
-static GList *
-log_manager_get_chats (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_manager_get_chats (manager, filename, 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);
+  EmpathyLogManager *log_manager;
+  TpChannel *channel;
+} MessageObserveData;
 
-	return hits;
-}
-
-static gchar *
-log_manager_get_dir (EmpathyLogManager *manager,
-		     McAccount         *account,
-		     const gchar       *chat_id,
-		     gboolean           chatroom)
+static void
+message_observe_data_free (MessageObserveData *data)
 {
-	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;
+  g_slice_free (MessageObserveData, data);
 }
 
-static gchar *
-log_manager_get_filename (EmpathyLogManager *manager,
-			  McAccount         *account,
-			  const gchar       *chat_id,
-			  gboolean           chatroom)
-{
-	gchar *basedir;
-	gchar *timestamp;
-	gchar *filename;
-
-	basedir = log_manager_get_dir (manager, account, chat_id, chatroom);
-	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 (EmpathyLogManager *manager,
-				   McAccount         *account,
-				   const gchar       *chat_id,
-				   gboolean           chatroom,
-				   const gchar       *date)
+static void
+log_manager_chat_received_message_cb (EmpathyTpChat *tp_chat,
+                                      EmpathyMessage *message,
+                                      MessageObserveData *data)
 {
-	gchar *basedir;
-	gchar *timestamp;
-	gchar *filename;
-
-	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);
-
-	g_free (basedir);
-	g_free (timestamp);
-
-	return filename;
+  GError *error = NULL;
+  TpHandleType handle_type;
+
+  tp_channel_get_handle (data->channel, &handle_type);
+
+  if (!empathy_log_manager_add_message (data->log_manager,
+        tp_channel_get_identifier (data->channel),
+        handle_type == TP_HANDLE_TYPE_ROOM,
+        message, &error))
+    {
+      DEBUG ("Failed to write message: %s",
+          error ? error->message : "No error message");
+
+      if (error)
+        g_error_free (error);
+    }
 }
 
-static gchar *
-log_manager_get_timestamp_filename (void)
+static void
+log_manager_dispatcher_observe_cb (EmpathyDispatcher *dispatcher,
+                                   EmpathyDispatchOperation *operation,
+                                   EmpathyLogManager *log_manager)
 {
-	time_t      t;
-	gchar      *time_str;
-	gchar      *filename;
+  GQuark channel_type;
 
-	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);
+  channel_type = empathy_dispatch_operation_get_channel_type_id (operation);
 
-	g_free (time_str);
+  if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_TEXT)
+    {
+      EmpathyTpChat *tp_chat;
+      TpChannel *channel;
+      MessageObserveData *data;
 
-	return filename;
-}
+      tp_chat = EMPATHY_TP_CHAT (
+          empathy_dispatch_operation_get_channel_wrapper (operation));
 
-static gchar *
-log_manager_get_timestamp_from_message (EmpathyMessage *message)
-{
-	time_t t;
+      channel = empathy_dispatch_operation_get_channel (operation);
 
-	t = empathy_message_get_timestamp (message);
+      data = g_slice_new0 (MessageObserveData);
+      data->log_manager = log_manager;
+      data->channel = channel;
 
-	/* We keep the timestamps in the messages as UTC. */
-	return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
+      g_signal_connect_data (tp_chat, "message-received",
+          G_CALLBACK (log_manager_chat_received_message_cb), data,
+          (GClosureNotify) message_observe_data_free, 0);
+    }
 }
 
-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;
-}
 
-static void
-log_manager_search_hit_free (EmpathyLogSearchHit *hit)
+void
+empathy_log_manager_observe (EmpathyLogManager *log_manager,
+                             EmpathyDispatcher *dispatcher)
 {
-	if (hit->account) {
-		g_object_unref (hit->account);
-	}
-
-	g_free (hit->date);
-	g_free (hit->filename);
-	g_free (hit->chat_id);
-
-	g_slice_free (EmpathyLogSearchHit, hit);
+  g_signal_connect (dispatcher, "observe",
+      G_CALLBACK (log_manager_dispatcher_observe_cb), log_manager);
 }
diff --git a/libempathy/empathy-log-manager.h b/libempathy/empathy-log-manager.h
index bad0140..b177df0 100644
--- a/libempathy/empathy-log-manager.h
+++ b/libempathy/empathy-log-manager.h
@@ -19,6 +19,7 @@
  * Boston, MA 02111-1307, USA.
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
  */
 
 #ifndef __EMPATHY_LOG_MANAGER_H__
@@ -29,70 +30,72 @@
 #include <libmissioncontrol/mc-account.h>
 
 #include "empathy-message.h"
+#include "empathy-dispatcher.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))
+#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;
 
-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);
+GType empathy_log_manager_get_type (void) G_GNUC_CONST;
+EmpathyLogManager *empathy_log_manager_dup_singleton (void);
+gboolean empathy_log_manager_add_message (EmpathyLogManager *manager,
+    const gchar *chat_id, gboolean chatroom, EmpathyMessage *message,
+    GError **error);
+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);
+void empathy_log_manager_observe (EmpathyLogManager *log_manager,
+    EmpathyDispatcher *dispatcher);
 
 G_END_DECLS
 
diff --git a/libempathy/empathy-log-store-empathy.c b/libempathy/empathy-log-store-empathy.c
new file mode 100644
index 0000000..2930ace
--- /dev/null
+++ b/libempathy/empathy-log-store-empathy.c
@@ -0,0 +1,714 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2003-2007 Imendio AB
+ * Copyright (C) 2007-2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#include <config.h>
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <glib/gstdio.h>
+
+#include "empathy-log-store.h"
+#include "empathy-log-store-empathy.h"
+#include "empathy-log-manager.h"
+#include "empathy-contact.h"
+#include "empathy-time.h"
+#include "empathy-utils.h"
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
+
+#define LOG_DIR_CREATE_MODE       (S_IRUSR | S_IWUSR | S_IXUSR)
+#define LOG_FILE_CREATE_MODE      (S_IRUSR | S_IWUSR)
+#define LOG_DIR_CHATROOMS         "chatrooms"
+#define LOG_FILENAME_SUFFIX       ".log"
+#define LOG_TIME_FORMAT_FULL      "%Y%m%dT%H:%M:%S"
+#define LOG_TIME_FORMAT           "%Y%m%d"
+#define LOG_HEADER \
+    "<?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, EmpathyLogStoreEmpathy)
+typedef struct
+{
+  gchar *basedir;
+  gchar *name;
+} EmpathyLogStoreEmpathyPriv;
+
+static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
+
+G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreEmpathy, empathy_log_store_empathy,
+    G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
+      log_store_iface_init));
+
+static void
+log_store_empathy_finalize (GObject *object)
+{
+  EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
+  EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
+
+  g_free (priv->basedir);
+  g_free (priv->name);
+}
+
+static void
+empathy_log_store_empathy_class_init (EmpathyLogStoreEmpathyClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = log_store_empathy_finalize;
+
+  g_type_class_add_private (object_class, sizeof (EmpathyLogStoreEmpathyPriv));
+}
+
+static void
+empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
+{
+  EmpathyLogStoreEmpathyPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_LOG_STORE_EMPATHY, EmpathyLogStoreEmpathyPriv);
+
+  self->priv = priv;
+
+  priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
+      ".gnome2", PACKAGE_NAME, "logs", NULL);
+
+  priv->name = g_strdup ("Empathy");
+}
+
+static gchar *
+log_store_empathy_get_dir (EmpathyLogStore *self,
+                           McAccount *account,
+                           const gchar *chat_id,
+                           gboolean chatroom)
+{
+  const gchar *account_id;
+  gchar *basedir;
+  EmpathyLogStoreEmpathyPriv *priv;
+
+  priv = GET_PRIV (self);
+
+  account_id = mc_account_get_unique_name (account);
+
+  if (chatroom)
+    basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
+        LOG_DIR_CHATROOMS, chat_id, NULL);
+  else
+    basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
+        account_id, chat_id, NULL);
+
+  return basedir;
+}
+
+static gchar *
+log_store_empathy_get_timestamp_filename (void)
+{
+  time_t t;
+  gchar *time_str;
+  gchar *filename;
+
+  t = empathy_time_get_current ();
+  time_str = empathy_time_to_string_local (t, LOG_TIME_FORMAT);
+  filename = g_strconcat (time_str, LOG_FILENAME_SUFFIX, NULL);
+
+  g_free (time_str);
+
+  return filename;
+}
+
+static gchar *
+log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
+{
+  time_t t;
+
+  t = empathy_message_get_timestamp (message);
+
+  /* We keep the timestamps in the messages as UTC. */
+  return empathy_time_to_string_utc (t, LOG_TIME_FORMAT_FULL);
+}
+
+static gchar *
+log_store_empathy_get_filename (EmpathyLogStore *self,
+                                McAccount *account,
+                                const gchar *chat_id,
+                                gboolean chatroom)
+{
+  gchar *basedir;
+  gchar *timestamp;
+  gchar *filename;
+
+  basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
+  timestamp = log_store_empathy_get_timestamp_filename ();
+  filename = g_build_filename (basedir, timestamp, NULL);
+
+  g_free (basedir);
+  g_free (timestamp);
+
+  return filename;
+}
+
+static gboolean
+log_store_empathy_add_message (EmpathyLogStore *self,
+                               const gchar *chat_id,
+                               gboolean chatroom,
+                               EmpathyMessage *message,
+                               GError **error)
+{
+  FILE *file;
+  McAccount *account;
+  EmpathyContact *sender;
+  const gchar *body_str;
+  const gchar *str;
+  EmpathyAvatar *avatar;
+  gchar *avatar_token = NULL;
+  gchar *filename;
+  gchar *basedir;
+  gchar *body;
+  gchar *timestamp;
+  gchar *contact_name;
+  gchar *contact_id;
+  TpChannelTextMessageType msg_type;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
+  g_return_val_if_fail (chat_id != NULL, FALSE);
+  g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+
+  sender = empathy_message_get_sender (message);
+  account = empathy_contact_get_account (sender);
+  body_str = empathy_message_get_body (message);
+  msg_type = empathy_message_get_tptype (message);
+
+  if (EMP_STR_EMPTY (body_str))
+    return FALSE;
+
+  filename = log_store_empathy_get_filename (self, account, chat_id, chatroom);
+  basedir = g_path_get_dirname (filename);
+  if (!g_file_test (basedir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
+    {
+      DEBUG ("Creating directory:'%s'", basedir);
+      g_mkdir_with_parents (basedir, LOG_DIR_CREATE_MODE);
+    }
+  g_free (basedir);
+
+  DEBUG ("Adding message: '%s' to file: '%s'", body_str, filename);
+
+  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+    {
+      file = g_fopen (filename, "w+");
+      if (file)
+        g_fprintf (file, LOG_HEADER);
+
+      g_chmod (filename, LOG_FILE_CREATE_MODE);
+    }
+  else
+    {
+      file = g_fopen (filename, "r+");
+      if (file)
+        fseek (file, - strlen (LOG_FOOTER), SEEK_END);
+    }
+
+  body = g_markup_escape_text (body_str, -1);
+  timestamp = log_store_empathy_get_timestamp_from_message (message);
+
+  str = empathy_contact_get_name (sender);
+  contact_name = g_markup_escape_text (str, -1);
+
+  str = empathy_contact_get_id (sender);
+  contact_id = g_markup_escape_text (str, -1);
+
+  avatar = empathy_contact_get_avatar (sender);
+  if (avatar)
+    avatar_token = g_markup_escape_text (avatar->token, -1);
+
+  g_fprintf (file,
+       "<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);
+
+  return TRUE;
+}
+
+static gboolean
+log_store_empathy_exists (EmpathyLogStore *self,
+                          McAccount *account,
+                          const gchar *chat_id,
+                          gboolean chatroom)
+{
+  gchar *dir;
+  gboolean exists;
+
+  dir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
+  exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
+  g_free (dir);
+
+  return exists;
+}
+
+static GList *
+log_store_empathy_get_dates (EmpathyLogStore *self,
+                             McAccount *account,
+                             const gchar *chat_id,
+                             gboolean chatroom)
+{
+  GList *dates = NULL;
+  gchar *date;
+  gchar *directory;
+  GDir *dir;
+  const gchar *filename;
+  const gchar *p;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  directory = log_store_empathy_get_dir (self, account, chat_id, chatroom);
+  dir = g_dir_open (directory, 0, NULL);
+  if (!dir)
+    {
+      DEBUG ("Could not open directory:'%s'", directory);
+      g_free (directory);
+      return NULL;
+    }
+
+  DEBUG ("Collating a list of dates in:'%s'", directory);
+
+  while ((filename = g_dir_read_name (dir)) != NULL)
+    {
+      if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
+        continue;
+
+      p = strstr (filename, LOG_FILENAME_SUFFIX);
+      date = g_strndup (filename, p - filename);
+
+      if (!date)
+        continue;
+
+      if (!g_regex_match_simple ("\\d{8}", date, 0, 0))
+        continue;
+
+      dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
+    }
+
+  g_free (directory);
+  g_dir_close (dir);
+
+  DEBUG ("Parsed %d dates", g_list_length (dates));
+
+  return dates;
+}
+
+static gchar *
+log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
+                                         McAccount *account,
+                                         const gchar *chat_id,
+                                         gboolean chatroom,
+                                         const gchar *date)
+{
+  gchar *basedir;
+  gchar *timestamp;
+  gchar *filename;
+
+  basedir = log_store_empathy_get_dir (self, account, chat_id, chatroom);
+  timestamp = g_strconcat (date, LOG_FILENAME_SUFFIX, NULL);
+  filename = g_build_filename (basedir, timestamp, NULL);
+
+  g_free (basedir);
+  g_free (timestamp);
+
+  return filename;
+}
+
+static EmpathyLogSearchHit *
+log_store_empathy_search_hit_new (EmpathyLogStore *self,
+                                  const gchar *filename)
+{
+  EmpathyLogSearchHit *hit;
+  const gchar *account_name;
+  const gchar *end;
+  gchar **strv;
+  guint len;
+
+  if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
+    return NULL;
+
+  strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
+  len = g_strv_length (strv);
+
+  hit = g_slice_new0 (EmpathyLogSearchHit);
+
+  end = strstr (strv[len-1], LOG_FILENAME_SUFFIX);
+  hit->date = g_strndup (strv[len-1], end - strv[len-1]);
+  hit->chat_id = g_strdup (strv[len-2]);
+  hit->is_chatroom = (strcmp (strv[len-3], LOG_DIR_CHATROOMS) == 0);
+
+  if (hit->is_chatroom)
+    account_name = strv[len-4];
+  else
+    account_name = strv[len-3];
+
+  hit->account = mc_account_lookup (account_name);
+  hit->filename = g_strdup (filename);
+
+  g_strfreev (strv);
+
+  return hit;
+}
+
+static GList *
+log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
+                                         const gchar *filename)
+{
+  GList *messages = NULL;
+  xmlParserCtxtPtr ctxt;
+  xmlDocPtr doc;
+  xmlNodePtr log_node;
+  xmlNodePtr node;
+  EmpathyLogSearchHit *hit;
+  McAccount *account;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (filename != NULL, NULL);
+
+  DEBUG ("Attempting to parse filename:'%s'...", filename);
+
+  if (!g_file_test (filename, G_FILE_TEST_EXISTS))
+    {
+      DEBUG ("Filename:'%s' does not exist", filename);
+      return NULL;
+    }
+
+  /* Get the account from the filename */
+  hit = log_store_empathy_search_hit_new (self, filename);
+  account = g_object_ref (hit->account);
+  empathy_log_manager_search_hit_free (hit);
+
+  /* Create parser. */
+  ctxt = xmlNewParserCtxt ();
+
+  /* Parse and validate the file. */
+  doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
+  if (!doc)
+    {
+      g_warning ("Failed to parse file:'%s'", filename);
+      xmlFreeParserCtxt (ctxt);
+      return NULL;
+    }
+
+  /* The root node, presets. */
+  log_node = xmlDocGetRootElement (doc);
+  if (!log_node)
+    {
+      xmlFreeDoc (doc);
+      xmlFreeParserCtxt (ctxt);
+      return NULL;
+    }
+
+  /* Now get the messages. */
+  for (node = log_node->children; node; node = node->next)
+    {
+      EmpathyMessage *message;
+      EmpathyContact *sender;
+      gchar *time;
+      time_t t;
+      gchar *sender_id;
+      gchar *sender_name;
+      gchar *sender_avatar_token;
+      gchar *body;
+      gchar *is_user_str;
+      gboolean is_user = FALSE;
+      gchar *msg_type_str;
+      TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
+
+      if (strcmp (node->name, "message") != 0)
+        continue;
+
+      body = xmlNodeGetContent (node);
+      time = xmlGetProp (node, "time");
+      sender_id = xmlGetProp (node, "id");
+      sender_name = xmlGetProp (node, "name");
+      sender_avatar_token = xmlGetProp (node, "token");
+      is_user_str = xmlGetProp (node, "isuser");
+      msg_type_str = xmlGetProp (node, "type");
+
+      if (is_user_str)
+        is_user = strcmp (is_user_str, "true") == 0;
+
+      if (msg_type_str)
+        msg_type = empathy_message_type_from_str (msg_type_str);
+
+      t = empathy_time_parse (time);
+
+      sender = empathy_contact_new_full (account, sender_id, sender_name);
+      empathy_contact_set_is_user (sender, is_user);
+      if (!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;
+}
+
+static GList *
+log_store_empathy_get_all_files (EmpathyLogStore *self,
+                                 const gchar *dir)
+{
+  GDir *gdir;
+  GList *files = NULL;
+  const gchar *name;
+  const gchar *basedir;
+  EmpathyLogStoreEmpathyPriv *priv;
+
+  priv = GET_PRIV (self);
+
+  basedir = dir ? dir : priv->basedir;
+
+  gdir = g_dir_open (basedir, 0, NULL);
+  if (!gdir)
+    return NULL;
+
+  while ((name = g_dir_read_name (gdir)) != NULL)
+    {
+      gchar *filename;
+
+      filename = g_build_filename (basedir, name, NULL);
+      if (g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
+        {
+          files = g_list_prepend (files, filename);
+          continue;
+        }
+
+      if (g_file_test (filename, G_FILE_TEST_IS_DIR))
+        {
+          /* Recursively get all log files */
+          files = g_list_concat (files,
+              log_store_empathy_get_all_files (self, filename));
+        }
+
+      g_free (filename);
+    }
+
+  g_dir_close (gdir);
+
+  return files;
+}
+
+static GList *
+log_store_empathy_search_new (EmpathyLogStore *self,
+                              const gchar *text)
+{
+  GList *files, *l;
+  GList *hits = NULL;
+  gchar *text_casefold;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
+
+  text_casefold = g_utf8_casefold (text, -1);
+
+  files = log_store_empathy_get_all_files (self, NULL);
+  DEBUG ("Found %d log files in total", g_list_length (files));
+
+  for (l = files; l; l = l->next)
+    {
+      gchar *filename;
+      GMappedFile *file;
+      gsize length;
+      gchar *contents;
+      gchar *contents_casefold;
+
+      filename = l->data;
+
+      file = g_mapped_file_new (filename, FALSE, NULL);
+      if (!file)
+        continue;
+
+      length = g_mapped_file_get_length (file);
+      contents = g_mapped_file_get_contents (file);
+      contents_casefold = g_utf8_casefold (contents, length);
+
+      g_mapped_file_free (file);
+
+      if (strstr (contents_casefold, text_casefold))
+        {
+          EmpathyLogSearchHit *hit;
+
+          hit = log_store_empathy_search_hit_new (self, filename);
+
+          if (hit)
+            {
+              hits = g_list_prepend (hits, hit);
+              DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
+                  text, hit->filename, hit->date);
+            }
+        }
+
+      g_free (contents_casefold);
+      g_free (filename);
+    }
+
+  g_list_free (files);
+  g_free (text_casefold);
+
+  return hits;
+}
+
+static GList *
+log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
+                                     const gchar *dir,
+                                     gboolean is_chatroom)
+{
+  GDir *gdir;
+  GList *hits = NULL;
+  const gchar *name;
+
+  gdir = g_dir_open (dir, 0, NULL);
+  if (!gdir)
+    return NULL;
+
+  while ((name = g_dir_read_name (gdir)) != NULL)
+    {
+      EmpathyLogSearchHit *hit;
+      gchar *filename;
+
+      filename = g_build_filename (dir, name, NULL);
+      if (strcmp (name, LOG_DIR_CHATROOMS) == 0)
+        {
+          hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
+                self, dir, TRUE));
+          g_free (filename);
+          continue;
+        }
+
+      hit = g_slice_new0 (EmpathyLogSearchHit);
+      hit->chat_id = g_strdup (name);
+      hit->is_chatroom = is_chatroom;
+
+      hits = g_list_prepend (hits, hit);
+    }
+
+  g_dir_close (gdir);
+
+  return hits;
+}
+
+
+static GList *
+log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
+                                         McAccount *account,
+                                         const gchar *chat_id,
+                                         gboolean chatroom,
+                                         const gchar *date)
+{
+  gchar *filename;
+  GList *messages;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  filename = log_store_empathy_get_filename_for_date (self, account,
+      chat_id, chatroom, date);
+  messages = log_store_empathy_get_messages_for_file (self, filename);
+  g_free (filename);
+
+  return messages;
+}
+
+static GList *
+log_store_empathy_get_chats (EmpathyLogStore *self,
+                              McAccount *account)
+{
+  gchar *dir;
+  GList *hits;
+  EmpathyLogStoreEmpathyPriv *priv;
+
+  priv = GET_PRIV (self);
+
+  dir = g_build_filename (priv->basedir,
+      mc_account_get_unique_name (account), NULL);
+
+  hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
+
+  g_free (dir);
+
+  return hits;
+}
+
+static const gchar *
+log_store_empathy_get_name (EmpathyLogStore *self)
+{
+  EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
+
+  return priv->name;
+}
+
+static void
+log_store_iface_init (gpointer g_iface,
+                      gpointer iface_data)
+{
+  EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
+
+  iface->get_name = log_store_empathy_get_name;
+  iface->exists = log_store_empathy_exists;
+  iface->add_message = log_store_empathy_add_message;
+  iface->get_dates = log_store_empathy_get_dates;
+  iface->get_messages_for_date = log_store_empathy_get_messages_for_date;
+  iface->get_chats = log_store_empathy_get_chats;
+  iface->search_new = log_store_empathy_search_new;
+  iface->ack_message = NULL;
+}
diff --git a/libempathy/empathy-log-store-empathy.h b/libempathy/empathy-log-store-empathy.h
new file mode 100644
index 0000000..3bc9997
--- /dev/null
+++ b/libempathy/empathy-log-store-empathy.h
@@ -0,0 +1,66 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2003-2007 Imendio AB
+ * Copyright (C) 2007-2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Xavier Claessens <xclaesse@gmail.com>
+ *          Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#ifndef __EMPATHY_LOG_STORE_EMPATHY_H__
+#define __EMPATHY_LOG_STORE_EMPATHY_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define EMPATHY_TYPE_LOG_STORE_EMPATHY \
+  (empathy_log_store_empathy_get_type ())
+#define EMPATHY_LOG_STORE_EMPATHY(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE_EMPATHY, \
+                               EmpathyLogStoreEmpathy))
+#define EMPATHY_LOG_STORE_EMPATHY_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_CAST ((vtable), EMPATHY_TYPE_LOG_STORE_EMPATHY, \
+                            EmpathyLogStoreEmpathyClass))
+#define EMPATHY_IS_LOG_STORE_EMPATHY(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE_EMPATHY))
+#define EMPATHY_IS_LOG_STORE_EMPATHY_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_TYPE ((vtable), EMPATHY_TYPE_LOG_STORE_EMPATHY))
+#define EMPATHY_LOG_STORE_EMPATHY_GET_CLASS(inst) \
+  (G_TYPE_INSTANCE_GET_CLASS ((inst), EMPATHY_TYPE_LOG_STORE_EMPATHY, \
+                              EmpathyLogStoreEmpathyClass))
+
+typedef struct _EmpathyLogStoreEmpathy EmpathyLogStoreEmpathy;
+typedef struct _EmpathyLogStoreEmpathyClass EmpathyLogStoreEmpathyClass;
+
+struct _EmpathyLogStoreEmpathy
+{
+  GObject parent;
+  gpointer priv;
+};
+
+struct _EmpathyLogStoreEmpathyClass
+{
+  GObjectClass parent;
+};
+
+GType empathy_log_store_empathy_get_type (void);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_LOG_STORE_EMPATHY_H__ */
diff --git a/libempathy/empathy-log-store-pidgin.c b/libempathy/empathy-log-store-pidgin.c
new file mode 100644
index 0000000..70b1975
--- /dev/null
+++ b/libempathy/empathy-log-store-pidgin.c
@@ -0,0 +1,713 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#include <config.h>
+
+#define _XOPEN_SOURCE
+#include <time.h>
+#include <string.h>
+
+#include <libmissioncontrol/mc-profile.h>
+
+#include <telepathy-glib/util.h>
+
+#include "empathy-log-store.h"
+#include "empathy-log-store-pidgin.h"
+#include "empathy-log-manager.h"
+#include "empathy-utils.h"
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
+
+#define TXT_LOG_FILENAME_SUFFIX ".txt"
+
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStorePidgin)
+typedef struct
+{
+  gchar *basedir;
+} EmpathyLogStorePidginPriv;
+
+static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
+
+G_DEFINE_TYPE_WITH_CODE (EmpathyLogStorePidgin, empathy_log_store_pidgin,
+    G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
+      log_store_iface_init));
+
+static void
+log_store_pidgin_finalize (GObject *object)
+{
+  EmpathyLogStorePidginPriv *priv = GET_PRIV (object);
+
+  g_free (priv->basedir);
+}
+
+static void
+empathy_log_store_pidgin_class_init (EmpathyLogStorePidginClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = log_store_pidgin_finalize;
+
+  g_type_class_add_private (object_class, sizeof (EmpathyLogStorePidginPriv));
+}
+
+static void
+empathy_log_store_pidgin_init (EmpathyLogStorePidgin *self)
+{
+  EmpathyLogStorePidginPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_LOG_STORE_PIDGIN, EmpathyLogStorePidginPriv);
+
+  self->priv = priv;
+
+  priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
+      ".purple", "logs", NULL);
+}
+
+static gchar *
+log_store_pidgin_get_dir (EmpathyLogStore *self,
+                          McAccount *account,
+                          const gchar *chat_id,
+                          gboolean chatroom)
+{
+  const gchar *protocol;
+  gchar *basedir;
+  McProfile *profile;
+  EmpathyLogStorePidginPriv *priv;
+  gchar *username;
+  gchar *id;
+
+  priv = GET_PRIV (self);
+
+  profile = mc_account_get_profile (account);
+  protocol = mc_profile_get_protocol_name (profile);
+
+  g_object_unref (profile);
+
+
+  if (mc_account_get_param_string (account, "account", &username) == 0)
+    {
+      DEBUG ("Failed to get account");
+      return NULL;
+    }
+
+  /* TODO: Why does butterfly do this? */
+  if (chat_id != NULL && g_str_has_suffix (chat_id, "#1"))
+    id = g_strndup (chat_id, (strlen (chat_id) - 2));
+  else if (!chat_id)
+    id = NULL;
+  else
+    id = g_strdup (chat_id);
+
+  basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, protocol,
+      username, id, NULL);
+
+  g_free (id);
+  g_free (username);
+
+  return basedir;
+}
+
+static gboolean
+log_store_pidgin_exists (EmpathyLogStore *self,
+                         McAccount *account,
+                         const gchar *chat_id,
+                         gboolean chatroom)
+{
+  gchar *dir;
+  gboolean exists;
+
+  dir = log_store_pidgin_get_dir (self, account, chat_id, chatroom);
+
+  if (dir)
+    exists = g_file_test (dir, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
+  else
+    exists = FALSE;
+
+  g_free (dir);
+
+  return exists;
+}
+
+static gchar *
+log_store_pidgin_get_time (const gchar *filename)
+{
+  gchar *date;
+  const gchar *p;
+
+  gint year;
+  gint month;
+  gint day;
+  guint hour;
+  gint minute;
+  gint second;
+
+  if (!filename)
+    return NULL;
+
+  if (g_str_has_suffix (filename, TXT_LOG_FILENAME_SUFFIX))
+    {
+      p = strstr (filename, TXT_LOG_FILENAME_SUFFIX);
+      date = g_strndup (filename, p - filename);
+    }
+  else
+    {
+      date = g_strdup (filename);
+    }
+
+  sscanf (date, "%4d-%2d-%2d.%2d%2d%2d%*s",
+      &year, &month, &day, &hour, &minute, &second);
+  g_free (date);
+
+  date = g_strdup_printf ("%d%02d%02i", year, month, day);
+
+  return date;
+}
+
+static GList *
+log_store_pidgin_get_dates (EmpathyLogStore *self,
+                            McAccount *account,
+                            const gchar *chat_id,
+                            gboolean chatroom)
+{
+  GList *dates = NULL;
+  gchar *date;
+  gchar *directory;
+  GDir *dir;
+  const gchar *filename;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  directory = log_store_pidgin_get_dir (self, account, chat_id, chatroom);
+
+  if (!directory)
+    {
+      g_free (directory);
+      return NULL;
+    }
+
+  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, TXT_LOG_FILENAME_SUFFIX))
+        continue;
+
+      date = log_store_pidgin_get_time (filename);
+      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 GList *
+log_store_pidgin_get_filenames_for_date (EmpathyLogStore *self,
+                                        McAccount *account,
+                                        const gchar *chat_id,
+                                        gboolean chatroom,
+                                        const gchar *date)
+{
+  gchar *basedir;
+  gchar *timestamp;
+  GList *filenames = NULL;
+  time_t time;
+  GDir *dir;
+  const gchar *dirfile;
+
+  basedir = log_store_pidgin_get_dir (self, account, chat_id, chatroom);
+
+  if (!basedir)
+    return NULL;
+
+  dir = g_dir_open (basedir, 0, NULL);
+  if (!dir)
+    {
+      g_free (basedir);
+      return NULL;
+    }
+
+  time = empathy_time_parse (date);
+
+  if (g_regex_match_simple ("\\d{8}", date, 0, 0))
+    timestamp = empathy_time_to_string_utc (time, "%F");
+  else
+    timestamp = empathy_time_to_string_utc (time, "%F.%H%M%S");
+
+  while ((dirfile = g_dir_read_name (dir)) != NULL)
+    {
+      if (g_str_has_prefix (dirfile, timestamp))
+        filenames = g_list_insert_sorted (filenames,
+            g_build_filename (basedir, dirfile, NULL),
+            (GCompareFunc) g_strcmp0);
+    }
+
+  g_dir_close (dir);
+
+  g_free (basedir);
+  g_free (timestamp);
+
+  return filenames;
+}
+
+static EmpathyLogSearchHit *
+log_store_pidgin_search_hit_new (EmpathyLogStore *self,
+                                 const gchar *filename)
+{
+  EmpathyLogSearchHit *hit;
+  const gchar *username;
+  const gchar *protocol;
+  gchar **strv;
+  guint len;
+  GList *accounts, *l;
+  McAccount *account = NULL;
+
+  if (!g_str_has_suffix (filename, TXT_LOG_FILENAME_SUFFIX))
+    return NULL;
+
+  strv = g_strsplit (filename, G_DIR_SEPARATOR_S, -1);
+  len = g_strv_length (strv);
+
+  hit = g_slice_new0 (EmpathyLogSearchHit);
+  hit->date = log_store_pidgin_get_time (strv[len-1]);
+  hit->chat_id = g_strdup (strv[len-2]);
+  hit->is_chatroom = FALSE; /* TODO: fix this */
+  hit->filename = g_strdup (filename);
+
+  username = strv[len-3];
+  protocol = strv[len-4];
+  accounts = mc_accounts_list ();
+
+  /* You can have multiple accounts with the same username so we have to
+   * look at all the accounts to find the right one going on the username and
+   * protocol. */
+  for (l = accounts; l; l = l->next)
+    {
+      gchar *user;
+      McAccount *acc = (McAccount *) l->data;
+      McProfile *profile;
+      gboolean found = FALSE;
+
+      profile = mc_account_get_profile (acc);
+
+      if (!tp_strdiff (mc_profile_get_protocol_name (profile), protocol))
+        /* More like half way there */
+        found = TRUE;
+
+      g_object_unref (profile);
+
+      if (!found)
+        continue;
+
+      mc_account_get_param_string (acc, "account", &user);
+      if (tp_strdiff (username, user))
+        found = FALSE;
+
+      g_free (user);
+
+      if (found)
+        {
+          account = g_object_ref (acc);
+          break;
+        }
+    }
+
+  hit->account = account;
+
+  mc_accounts_list_free (accounts);
+  g_strfreev (strv);
+
+  return hit;
+}
+
+static GList *
+log_store_pidgin_get_messages_for_files (EmpathyLogStore *self,
+                                         const GList *filenames)
+{
+  GList *messages = NULL;
+  const GList *l;
+
+  gchar *sender_id = NULL;
+  gchar *date = NULL;
+  gchar *own_user = NULL;
+  gchar *protocol = NULL;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (filenames != NULL, NULL);
+
+  for (l = filenames; l; l = l->next)
+    {
+      const gchar *filename;
+
+      EmpathyLogSearchHit *hit;
+      McAccount *account;
+
+      gchar *buffer;
+      GError *error = NULL;
+      gssize length;
+      gchar **lines;
+      int i;
+      struct tm tm;
+      time_t t;
+
+      GRegex *regex;
+      GMatchInfo *match_info;
+      gchar **hits;
+
+      filename = (gchar *) l->data;
+
+      hit = log_store_pidgin_search_hit_new (self, filename);
+      account = g_object_ref (hit->account);
+      empathy_log_manager_search_hit_free (hit);
+
+      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;
+        }
+
+      if (!g_file_get_contents (filename, &buffer, &length, &error))
+        {
+          DEBUG ("Failed to read file: %s",
+              (error->message) ? error->message : "no message");
+          g_error_free (error);
+          return NULL;
+        }
+
+      lines = g_strsplit (buffer, "\n", -1);
+
+      g_free (buffer);
+
+      /* Perhaps this regex could be a little more specific */
+      regex = g_regex_new ("Conversation with (.+) at (.+) on (.+) \\((.+)\\)",
+          0, 0, NULL);
+      g_regex_match (regex, lines[0], 0, &match_info);
+      hits = g_match_info_fetch_all (match_info);
+
+      g_match_info_free (match_info);
+      g_regex_unref (regex);
+
+      if (!hits)
+        {
+          g_strfreev (lines);
+          goto CONTINUE;
+        }
+
+      sender_id = g_strdup (hits[1]);
+      own_user = g_strdup (hits[3]);
+      protocol = g_strdup (hits[4]);
+
+      strptime (hits[2], "%a %d %b %Y %H:%M:%S %Z", &tm);
+      t = empathy_time_get_local_time (&tm);
+
+      g_strfreev (hits);
+
+      for (i = 1; lines[i] != NULL; i++)
+        {
+          EmpathyMessage *message;
+          EmpathyContact *sender;
+          gchar *sender_name = NULL;
+          gchar *time = NULL;
+          gchar *body = NULL;
+          gchar *tmp = NULL;
+          int j = i + 1;
+
+          regex = g_regex_new ("\\((.+)\\) (.+):", 0, 0, NULL);
+          g_regex_match (regex, lines[i], 0, &match_info);
+          hits = g_match_info_fetch_all (match_info);
+
+          g_match_info_free (match_info);
+          g_regex_unref (regex);
+
+          if (!hits)
+            {
+              g_strfreev (hits);
+              goto CONTINUE;
+            }
+
+          time = g_strdup (hits[1]);
+          sender_name = g_strdup (hits[2]);
+
+          while (lines[j] != NULL
+              && !g_regex_match_simple ("\\(\\d{2}:\\d{2}:\\d{2}\\) .+:",
+                lines[j], 0, 0))
+            {
+              if (!body)
+                tmp = g_strdup (lines[j]);
+              else
+                tmp = g_strjoin ("\n", body, lines[j], NULL);
+
+              g_free (body);
+              body = tmp;
+              j++;
+            }
+
+          sender = empathy_contact_new_full (account, sender_id, sender_name);
+
+          /* TODO: There's no way to tell which user is you, so this can't really
+           * be used:
+          empathy_contact_set_is_user (sender, is_user); */
+
+          /* Body needs to be something */
+          body = body ? body : g_strdup ("");
+
+          message = empathy_message_new (body);
+          empathy_message_set_sender (message, sender);
+          empathy_message_set_timestamp (message, t);
+          empathy_message_set_tptype (message,
+              TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL);
+
+          messages = g_list_append (messages, message);
+
+          g_free (sender_name);
+          g_free (time);
+          g_object_unref (sender);
+
+          i = j - 1;
+        }
+
+      g_free (sender_id);
+      g_free (own_user);
+      g_free (date);
+      g_free (protocol);
+
+      g_strfreev (lines);
+
+CONTINUE:
+      g_object_unref (account);
+    }
+
+  DEBUG ("Parsed %d messages", g_list_length (messages));
+
+  return messages;
+}
+
+static GList *
+log_store_pidgin_get_all_files (EmpathyLogStore *self,
+                                const gchar *dir)
+{
+  GDir *gdir;
+  GList *files = NULL;
+  const gchar *name;
+  const gchar *basedir;
+  EmpathyLogStorePidginPriv *priv;
+
+  priv = GET_PRIV (self);
+
+  basedir = dir ? dir : priv->basedir;
+
+  gdir = g_dir_open (basedir, 0, NULL);
+  if (!gdir)
+    return NULL;
+
+  while ((name = g_dir_read_name (gdir)) != NULL)
+    {
+      gchar *filename;
+
+      filename = g_build_filename (basedir, name, NULL);
+      if (g_str_has_suffix (filename, TXT_LOG_FILENAME_SUFFIX))
+        {
+          files = g_list_prepend (files, filename);
+          continue;
+        }
+
+      if (g_file_test (filename, G_FILE_TEST_IS_DIR))
+        {
+          files = g_list_concat (files,
+              log_store_pidgin_get_all_files (self, filename));
+        }
+
+      g_free (filename);
+    }
+
+  g_dir_close (gdir);
+
+  return files;
+}
+
+static GList *
+log_store_pidgin_search_new (EmpathyLogStore *self,
+                              const gchar *text)
+{
+  GList *files, *l;
+  GList *hits = NULL;
+  gchar *text_casefold;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
+
+  text_casefold = g_utf8_casefold (text, -1);
+
+  files = log_store_pidgin_get_all_files (self, NULL);
+  DEBUG ("Found %d log files in total", g_list_length (files));
+
+  for (l = files; l; l = l->next)
+    {
+      gchar *filename;
+      GMappedFile *file;
+      gsize length;
+      gchar *contents;
+      gchar *contents_casefold;
+
+      filename = l->data;
+
+      file = g_mapped_file_new (filename, FALSE, NULL);
+      if (!file)
+        continue;
+
+      length = g_mapped_file_get_length (file);
+      contents = g_mapped_file_get_contents (file);
+      contents_casefold = g_utf8_casefold (contents, length);
+
+      g_mapped_file_free (file);
+
+      if (strstr (contents_casefold, text_casefold))
+        {
+          EmpathyLogSearchHit *hit;
+
+          hit = log_store_pidgin_search_hit_new (self, filename);
+
+          if (hit)
+            {
+              hits = g_list_prepend (hits, hit);
+              DEBUG ("Found text:'%s' in file:'%s' on date:'%s'",
+                  text, hit->filename, hit->date);
+            }
+        }
+
+      g_free (contents_casefold);
+      g_free (filename);
+    }
+
+  g_list_free (files);
+  g_free (text_casefold);
+
+  return hits;
+}
+
+static GList *
+log_store_pidgin_get_chats_for_dir (EmpathyLogStore *self,
+                                    const gchar *dir,
+                                    gboolean is_chatroom)
+{
+  GDir *gdir;
+  GList *hits = NULL;
+  const gchar *name;
+
+  gdir = g_dir_open (dir, 0, NULL);
+  if (!gdir)
+    return NULL;
+
+  while ((name = g_dir_read_name (gdir)) != NULL)
+    {
+      EmpathyLogSearchHit *hit;
+
+      hit = g_slice_new0 (EmpathyLogSearchHit);
+      hit->chat_id = g_strdup (name);
+      hit->is_chatroom = is_chatroom;
+
+      hits = g_list_prepend (hits, hit);
+    }
+
+  g_dir_close (gdir);
+
+  return hits;
+}
+
+static GList *
+log_store_pidgin_get_messages_for_date (EmpathyLogStore *self,
+                                        McAccount *account,
+                                        const gchar *chat_id,
+                                        gboolean chatroom,
+                                        const gchar *date)
+{
+  GList *messages, *filenames;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  filenames = log_store_pidgin_get_filenames_for_date (self, account,
+      chat_id, chatroom, date);
+
+  if (filenames == NULL)
+    return NULL;
+
+  messages = log_store_pidgin_get_messages_for_files (self, filenames);
+
+  g_list_foreach (filenames, (GFunc) g_free, NULL);
+  g_list_free (filenames);
+
+  return messages;
+}
+
+static GList *
+log_store_pidgin_get_chats (EmpathyLogStore *self,
+                             McAccount *account)
+{
+  gchar *dir;
+  GList *hits;
+  EmpathyLogStorePidginPriv *priv;
+
+  priv = GET_PRIV (self);
+
+  dir = log_store_pidgin_get_dir (self, account, NULL, FALSE);
+
+  if (dir)
+    hits = log_store_pidgin_get_chats_for_dir (self, dir, FALSE);
+  else
+    hits = NULL;
+
+  g_free (dir);
+
+  return hits;
+}
+
+static void
+log_store_iface_init (gpointer g_iface,
+                      gpointer iface_data)
+{
+  EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
+
+  iface->exists = log_store_pidgin_exists;
+  iface->add_message = NULL;
+  iface->get_dates = log_store_pidgin_get_dates;
+  iface->get_messages_for_date = log_store_pidgin_get_messages_for_date;
+  iface->get_chats = log_store_pidgin_get_chats;
+  iface->search_new = log_store_pidgin_search_new;
+  iface->ack_message = NULL;
+}
diff --git a/libempathy/empathy-log-store-pidgin.h b/libempathy/empathy-log-store-pidgin.h
new file mode 100644
index 0000000..b947a04
--- /dev/null
+++ b/libempathy/empathy-log-store-pidgin.h
@@ -0,0 +1,64 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#ifndef __EMPATHY_LOG_STORE_PIDGIN_H__
+#define __EMPATHY_LOG_STORE_PIDGIN_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define EMPATHY_TYPE_LOG_STORE_PIDGIN \
+  (empathy_log_store_pidgin_get_type ())
+#define EMPATHY_LOG_STORE_PIDGIN(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE_PIDGIN, \
+                               EmpathyLogStorePidgin))
+#define EMPATHY_LOG_STORE_PIDGIN_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_CAST ((vtable), EMPATHY_TYPE_LOG_STORE_PIDGIN, \
+                            EmpathyLogStorePidginClass))
+#define EMPATHY_IS_LOG_STORE_PIDGIN(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE_PIDGIN))
+#define EMPATHY_IS_LOG_STORE_PIDGIN_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_TYPE ((vtable), EMPATHY_TYPE_LOG_STORE_PIDGIN))
+#define EMPATHY_LOG_STORE_PIDGIN_GET_CLASS(inst) \
+  (G_TYPE_INSTANCE_GET_CLASS ((inst), EMPATHY_TYPE_LOG_STORE_PIDGIN, \
+                              EmpathyLogStorePidginClass))
+
+typedef struct _EmpathyLogStorePidgin EmpathyLogStorePidgin;
+typedef struct _EmpathyLogStorePidginClass EmpathyLogStorePidginClass;
+
+struct _EmpathyLogStorePidgin
+{
+  GObject parent;
+  gpointer priv;
+};
+
+struct _EmpathyLogStorePidginClass
+{
+  GObjectClass parent;
+};
+
+GType empathy_log_store_pidgin_get_type (void);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_LOG_STORE_PIDGIN_H__ */
diff --git a/libempathy/empathy-log-store-sqlite.c b/libempathy/empathy-log-store-sqlite.c
new file mode 100644
index 0000000..bb4a8de
--- /dev/null
+++ b/libempathy/empathy-log-store-sqlite.c
@@ -0,0 +1,625 @@
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <libmissioncontrol/mc-profile.h>
+#include <telepathy-glib/util.h>
+#include <sqlite3.h>
+
+#include "empathy-log-store.h"
+#include "empathy-log-store-sqlite.h"
+#include "empathy-log-manager.h"
+#include "empathy-utils.h"
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include "empathy-debug.h"
+
+#define SQLITE_TABLE_NAME "messages"
+
+#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyLogStoreSqlite)
+typedef struct
+{
+  sqlite3 *db;
+} EmpathyLogStoreSqlitePriv;
+
+static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
+
+G_DEFINE_TYPE_WITH_CODE (EmpathyLogStoreSqlite, empathy_log_store_sqlite,
+    G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (EMPATHY_TYPE_LOG_STORE,
+      log_store_iface_init));
+
+static void
+log_store_sqlite_finalize (GObject *object)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (object);
+
+  sqlite3_close (priv->db);
+}
+
+static void
+empathy_log_store_sqlite_class_init (EmpathyLogStoreSqliteClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = log_store_sqlite_finalize;
+
+  g_type_class_add_private (object_class, sizeof (EmpathyLogStoreSqlitePriv));
+}
+
+static gboolean
+log_store_sqlite_exec (EmpathyLogStoreSqlite *self,
+                       const gchar *sql,
+                       sqlite3_stmt **statement)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  gint rc;
+
+  rc = sqlite3_prepare_v2 (priv->db, sql, -1, statement, NULL);
+
+  if (rc != SQLITE_OK || *statement == NULL)
+    {
+      g_warning ("Error: %s", sqlite3_errmsg (priv->db));
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
+static void
+log_store_sqlite_prepare_db (EmpathyLogStoreSqlite *self)
+{
+  sqlite3_stmt *statement;
+
+  if (log_store_sqlite_exec (self,
+      "CREATE TABLE IF NOT EXISTS " SQLITE_TABLE_NAME " ("
+      "id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+      "account VARCHAR(100) NOT NULL, "
+      "time DATETIME NOT NULL, "
+      "chat_id VARCHAR(255) NOT NULL, "
+      "name VARCHAR(255) NOT NULL, "
+      "token VARCHAR (32), "
+      "is_user BOOLEAN NOT NULL, "
+      "type VARCHAR(255), "
+      "message TEXT"
+      ");",
+      &statement))
+    {
+      while (sqlite3_step (statement) != SQLITE_DONE)
+        ;
+    }
+
+  sqlite3_finalize (statement);
+}
+
+static void
+empathy_log_store_sqlite_init (EmpathyLogStoreSqlite *self)
+{
+  EmpathyLogStoreSqlitePriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+      EMPATHY_TYPE_LOG_STORE_SQLITE, EmpathyLogStoreSqlitePriv);
+  gint rc;
+  gchar *filename;
+
+  self->priv = priv;
+
+  filename = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), ".gnome2",
+      PACKAGE_NAME, "logs.db", NULL);
+
+  rc = sqlite3_open (filename, &priv->db);
+
+  if (rc)
+    {
+      g_warning ("Couldn't open database: %s", sqlite3_errmsg (priv->db));
+      sqlite3_close (priv->db);
+    }
+
+  g_free (filename);
+
+  log_store_sqlite_prepare_db (self);
+}
+
+static gboolean
+log_store_sqlite_exists (EmpathyLogStore *self,
+                         McAccount *account,
+                         const gchar *chat_id,
+                         gboolean chatroom)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  sqlite3_stmt *statement;
+  gint rc;
+  gboolean found = FALSE;
+
+  gchar *sql = g_strdup_printf (
+      "SELECT COUNT(chat_id) "
+      "FROM %s "
+      "WHERE chat_id = \"%s\" "
+      "AND account = \"%s\";",
+      SQLITE_TABLE_NAME, chat_id,
+      mc_account_get_unique_name (account));
+
+  if (!log_store_sqlite_exec (
+        EMPATHY_LOG_STORE_SQLITE (self), sql, &statement))
+    {
+      goto OUT;
+    }
+
+  while ((rc = sqlite3_step (statement)) != SQLITE_DONE)
+    {
+      if (rc == SQLITE_ERROR || rc == SQLITE_MISUSE)
+        break;
+
+      if (rc != SQLITE_ROW)
+        continue;
+
+      if (sqlite3_column_int (statement, 0) > 0)
+        found = TRUE;
+    }
+
+  if (rc == SQLITE_ERROR)
+    g_warning ("SQL Error: %s", sqlite3_errmsg (priv->db));
+  else if (rc == SQLITE_MISUSE)
+    g_warning ("SQL misuse");
+
+  sqlite3_finalize (statement);
+
+OUT:
+  g_free (sql);
+  return found;
+}
+
+static gchar *
+log_store_sqlite_escape_quotes (const gchar *text)
+{
+  gchar *newtext;
+  guint i, j;
+  guint no_quotes = 0;
+  guint new_size;
+
+  for (i = 0; text[i] != '\0'; i++)
+    {
+      if (text[i] == '\'')
+        no_quotes++;
+    }
+
+  new_size = i + no_quotes;
+
+  newtext = g_malloc0 (new_size);
+
+  for (i = 0, j = 0; text[i] != '\0'; i++, j++)
+    {
+      if (text[i] == '\'')
+        newtext[j++] = '\'';
+      newtext[j] = text[i];
+    }
+  newtext[j] = '\0';
+
+  return newtext;
+}
+
+static gboolean
+log_store_sqlite_add_message (EmpathyLogStore *self,
+                              const gchar *chat_id,
+                              gboolean chatroom,
+                              EmpathyMessage *message,
+                              GError **error)
+{
+  sqlite3_stmt *statement;
+  McAccount *account;
+  EmpathyAvatar *avatar;
+  TpChannelTextMessageType msg_type;
+  EmpathyContact *sender;
+  const gchar *body_str;
+  gchar *sql;
+  gchar *token = NULL;
+  gchar *sender_id;
+  gchar *sender_name;
+  gchar *body;
+  gboolean ret = TRUE;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), FALSE);
+  g_return_val_if_fail (chat_id != NULL, FALSE);
+  g_return_val_if_fail (EMPATHY_IS_MESSAGE (message), FALSE);
+
+  sender = empathy_message_get_sender (message);
+  account = empathy_contact_get_account (sender);
+  body_str = empathy_message_get_body (message);
+  msg_type = empathy_message_get_tptype (message);
+
+  if (EMP_STR_EMPTY (body_str))
+    return FALSE;
+
+  avatar = empathy_contact_get_avatar (sender);
+  if (avatar)
+    token = g_strdup (avatar->token);
+
+  sender_id = log_store_sqlite_escape_quotes (chat_id);
+  sender_name = log_store_sqlite_escape_quotes (
+      empathy_contact_get_name (sender));
+  body = log_store_sqlite_escape_quotes (body_str);
+
+  sql = g_strconcat ("INSERT INTO ", SQLITE_TABLE_NAME,
+      " (account, time, chat_id, name, token, is_user, type, message) ",
+      "VALUES (",
+      "'", mc_account_get_unique_name (account), "', ",
+      "DATETIME('now'), ",
+      "'", sender_id, "', ",
+      "'", sender_name, "', ",
+      "'", token ? token : "", "', ",
+      "'", empathy_contact_is_user (sender) ? "true" : "false", "', ",
+      "'", empathy_message_type_to_str (msg_type), "', ",
+      "'", body, "');", NULL);
+
+  if (token)
+    g_free (token);
+
+  g_free (sender_id);
+  g_free (sender_name);
+  g_free (body);
+
+  if (!log_store_sqlite_exec (EMPATHY_LOG_STORE_SQLITE (self),
+        sql, &statement))
+    {
+      ret = FALSE;
+      goto OUT;
+    }
+
+  while (sqlite3_step (statement) != SQLITE_DONE)
+    ;
+
+OUT:
+  g_free (sql);
+  sqlite3_finalize (statement);
+  return ret;
+}
+
+static GList *
+log_store_sqlite_get_dates (EmpathyLogStore *self,
+                            McAccount *account,
+                            const gchar *chat_id,
+                            gboolean chatroom)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  GList *dates = NULL;
+  sqlite3_stmt *statement;
+  gchar *sql;
+  gint rc;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  sql = g_strdup_printf (
+      "SELECT strftime(\"%%Y%%m%%d\", time) "
+      "FROM %s "
+      "WHERE chat_id = \"%s\" "
+      "AND account = \"%s\";",
+      SQLITE_TABLE_NAME, chat_id,
+      mc_account_get_unique_name (account));
+
+  if (!log_store_sqlite_exec (
+        EMPATHY_LOG_STORE_SQLITE (self), sql, &statement))
+    {
+      goto OUT;
+    }
+
+  while ((rc = sqlite3_step (statement)) != SQLITE_DONE)
+    {
+      gchar *date = NULL;
+
+      if (rc == SQLITE_ERROR || rc == SQLITE_MISUSE)
+        break;
+
+      if (rc != SQLITE_ROW)
+        continue;
+
+      date = g_strdup (sqlite3_column_text (statement, 0));
+      dates = g_list_insert_sorted (dates, date, (GCompareFunc) strcmp);
+    }
+
+  if (rc == SQLITE_ERROR)
+    g_warning ("SQL Error: %s", sqlite3_errmsg (priv->db));
+  else if (rc == SQLITE_MISUSE)
+    g_warning ("SQL misuse");
+
+  sqlite3_finalize (statement);
+
+  DEBUG ("Parsed %d dates", g_list_length (dates));
+
+OUT:
+  g_free (sql);
+  return dates;
+}
+
+static GList *
+log_store_sqlite_search_new (EmpathyLogStore *self,
+                             const gchar *text)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  GList *hits = NULL;
+  gchar *sql;
+  sqlite3_stmt *statement;
+  gint rc;
+  gchar *query;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (!EMP_STR_EMPTY (text), NULL);
+
+  query = log_store_sqlite_escape_quotes (text);
+
+  sql = g_strdup_printf (
+      "SELECT strftime (\"%%Y%%m%%dT%%H:%%M:%%S\", time), chat_id, account "
+      "FROM %s "
+      "WHERE message LIKE '%%%s%%' "
+      "ORDER BY time;",
+      SQLITE_TABLE_NAME, query);
+
+  g_free (query);
+
+  if (!log_store_sqlite_exec (
+        EMPATHY_LOG_STORE_SQLITE (self), sql, &statement))
+    {
+      goto OUT;
+    }
+
+  while ((rc = sqlite3_step (statement)) != SQLITE_DONE)
+    {
+      EmpathyLogSearchHit *hit;
+      const gchar *time;
+      const gchar *chat_id;
+      McAccount *account = NULL;
+
+      if (rc == SQLITE_ERROR || rc == SQLITE_MISUSE)
+        break;
+
+      if (rc != SQLITE_ROW)
+        continue;
+
+      time = sqlite3_column_text (statement, 0);
+      chat_id = sqlite3_column_text (statement, 1);
+      account = mc_account_lookup (sqlite3_column_text (statement, 2));
+
+      if (!account)
+        continue;
+
+      hit = g_slice_new0 (EmpathyLogSearchHit);
+
+      hit->account = g_object_ref (account);
+      hit->chat_id = g_strdup (chat_id);
+      hit->is_chatroom = FALSE; /* TODO */
+      hit->date = g_strdup (time);
+
+      hits = g_list_prepend (hits, hit);
+
+      g_object_unref (account);
+    }
+
+  if (rc == SQLITE_ERROR)
+    g_warning ("SQL Error: %s", sqlite3_errmsg (priv->db));
+  else if (rc == SQLITE_MISUSE)
+    g_warning ("SQL misuse");
+
+  sqlite3_finalize (statement);
+
+  DEBUG ("Found %u hits", g_list_length (hits));
+
+OUT:
+  g_free (sql);
+  return hits;
+}
+
+static EmpathyMessage *
+log_store_sqlite_message_new (EmpathyLogStoreSqlite *self,
+                              McAccount *account,
+                              const gchar *time,
+                              const gchar *chat_id,
+                              const gchar *name,
+                              const gchar *token,
+                              gboolean is_user,
+                              const gchar *type,
+                              const gchar *body)
+{
+  EmpathyMessage *message;
+  EmpathyContact *sender;
+  time_t t;
+  TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+
+  g_return_val_if_fail (time != NULL, NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+  g_return_val_if_fail (name != NULL, NULL);
+
+  sender = empathy_contact_new_full (account, chat_id, name);
+  empathy_contact_set_is_user (sender, is_user);
+
+  t = empathy_time_parse (time);
+
+  if (!EMP_STR_EMPTY (token))
+    empathy_contact_load_avatar_cache (sender, token);
+
+  if (type)
+    msg_type = empathy_message_type_from_str (type);
+
+  if (body == NULL)
+    body = "";
+
+  message = empathy_message_new (body);
+  empathy_message_set_sender (message, sender);
+  empathy_message_set_timestamp (message, t);
+  empathy_message_set_tptype (message, msg_type);
+
+  g_object_unref (sender);
+
+  return message;
+}
+
+
+static GList *
+log_store_sqlite_get_messages_for_date (EmpathyLogStore *self,
+                                        McAccount *account,
+                                        const gchar *chat_id,
+                                        gboolean chatroom,
+                                        const gchar *date)
+{
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  GList *messages = NULL;
+  guint year, month, day;
+  gchar *time;
+  gchar *sql;
+  gint rc;
+  sqlite3_stmt *statement;
+
+  g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
+  g_return_val_if_fail (MC_IS_ACCOUNT (account), NULL);
+  g_return_val_if_fail (chat_id != NULL, NULL);
+
+  if (sscanf (date, "%4d%2d%2d", &year, &month, &day) != 3)
+    return NULL;
+
+  time = g_strdup_printf ("%4u-%02u-%02u", year, month, day);
+
+  sql = g_strdup_printf (
+      "SELECT strftime (\"%%Y%%m%%dT%%H:%%M:%%S\", time), name, token, is_user, type, message "
+      "FROM %s "
+      "WHERE chat_id = \"%s\" "
+      "AND date(time) = \"%s\" "
+      "AND account = \"%s\" "
+      "ORDER BY time;",
+      SQLITE_TABLE_NAME, chat_id, time,
+      mc_account_get_unique_name (account));
+
+  g_free (time);
+
+  if (!log_store_sqlite_exec (
+        EMPATHY_LOG_STORE_SQLITE (self), sql, &statement))
+    {
+      goto OUT;
+    }
+
+  while ((rc = sqlite3_step (statement)) != SQLITE_DONE)
+    {
+      const gchar *time;
+      const gchar *name;
+      const gchar *token;
+      gboolean is_user;
+      const gchar *type;
+      const gchar *message;
+
+      if (rc == SQLITE_ERROR || rc == SQLITE_MISUSE)
+        break;
+
+      if (rc != SQLITE_ROW)
+        continue;
+
+      time = sqlite3_column_text (statement, 0);
+      name = sqlite3_column_text (statement, 1);
+      token = sqlite3_column_text (statement, 2);
+      is_user = sqlite3_column_int (statement, 3);
+      type = sqlite3_column_text (statement, 4);
+      message = sqlite3_column_text (statement, 5);
+
+      messages = g_list_append (
+          messages, log_store_sqlite_message_new (
+            EMPATHY_LOG_STORE_SQLITE (self), account, time, chat_id, name,
+            token, is_user, type, message));
+    }
+
+  if (rc == SQLITE_ERROR)
+    g_warning ("SQL Error: %s", sqlite3_errmsg (priv->db));
+  else if (rc == SQLITE_MISUSE)
+    g_warning ("SQL misuse");
+
+  sqlite3_finalize (statement);
+
+  DEBUG ("Parsed %u messages", g_list_length (messages));
+
+OUT:
+  g_free (sql);
+  return messages;
+}
+
+static GList *
+log_store_sqlite_get_chats (EmpathyLogStore *self,
+                            McAccount *account)
+{
+  GList *hits = NULL;
+  EmpathyLogStoreSqlitePriv *priv = GET_PRIV (self);
+  sqlite3_stmt *statement;
+  gint rc;
+
+  gchar *sql = g_strdup_printf (
+      "SELECT chat_id "
+      "FROM %s "
+      "WHERE account = \"%s\";",
+      SQLITE_TABLE_NAME,
+      mc_account_get_unique_name (account));
+
+  if (!log_store_sqlite_exec (
+        EMPATHY_LOG_STORE_SQLITE (self), sql, &statement))
+    {
+      goto OUT;
+    }
+
+  while ((rc = sqlite3_step (statement)) != SQLITE_DONE)
+    {
+      EmpathyLogSearchHit *hit;
+
+      if (rc == SQLITE_ERROR || rc == SQLITE_MISUSE)
+        break;
+
+      if (rc != SQLITE_ROW)
+        continue;
+
+      hit = g_slice_new0 (EmpathyLogSearchHit);
+      hit->chat_id = g_strdup (sqlite3_column_text (statement, 0));
+
+      hits = g_list_prepend (hits, hit);
+    }
+
+  if (rc == SQLITE_ERROR)
+    g_warning ("SQL Error: %s", sqlite3_errmsg (priv->db));
+  else if (rc == SQLITE_MISUSE)
+    g_warning ("SQL misuse");
+
+  sqlite3_finalize (statement);
+
+OUT:
+  g_free (sql);
+  return hits;
+}
+
+static void
+log_store_iface_init (gpointer g_iface,
+                      gpointer iface_data)
+{
+  EmpathyLogStoreInterface *iface = (EmpathyLogStoreInterface *) g_iface;
+
+  iface->exists = log_store_sqlite_exists;
+  iface->add_message = log_store_sqlite_add_message;
+  iface->get_dates = log_store_sqlite_get_dates;
+  iface->get_messages_for_date = log_store_sqlite_get_messages_for_date;
+  iface->get_chats = log_store_sqlite_get_chats;
+  iface->search_new = log_store_sqlite_search_new;
+  iface->ack_message = NULL;
+}
diff --git a/libempathy/empathy-log-store-sqlite.h b/libempathy/empathy-log-store-sqlite.h
new file mode 100644
index 0000000..35c5d60
--- /dev/null
+++ b/libempathy/empathy-log-store-sqlite.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#ifndef __EMPATHY_LOG_STORE_SQLITE_H__
+#define __EMPATHY_LOG_STORE_SQLITE_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#define EMPATHY_TYPE_LOG_STORE_SQLITE \
+  (empathy_log_store_sqlite_get_type ())
+#define EMPATHY_LOG_STORE_SQLITE(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE_SQLITE, \
+                               EmpathyLogStoreSqlite))
+#define EMPATHY_LOG_STORE_SQLITE_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_CAST ((vtable), EMPATHY_TYPE_LOG_STORE_SQLITE, \
+                            EmpathyLogStoreSqliteClass))
+#define EMPATHY_IS_LOG_STORE_SQLITE(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE_SQLITE))
+#define EMPATHY_IS_LOG_STORE_SQLITE_CLASS(vtable) \
+  (G_TYPE_CHECK_CLASS_TYPE ((vtable), EMPATHY_TYPE_LOG_STORE_SQLITE))
+#define EMPATHY_LOG_STORE_SQLITE_GET_CLASS(inst) \
+  (G_TYPE_INSTANCE_GET_CLASS ((inst), EMPATHY_TYPE_LOG_STORE_SQLITE, \
+                              EmpathyLogStoreSqliteClass))
+
+typedef struct _EmpathyLogStoreSqlite EmpathyLogStoreSqlite;
+typedef struct _EmpathyLogStoreSqliteClass EmpathyLogStoreSqliteClass;
+
+struct _EmpathyLogStoreSqlite
+{
+  GObject parent;
+  gpointer priv;
+};
+
+struct _EmpathyLogStoreSqliteClass
+{
+  GObjectClass parent;
+};
+
+GType empathy_log_store_sqlite_get_type (void);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_LOG_STORE_SQLITE_H__ */
diff --git a/libempathy/empathy-log-store.c b/libempathy/empathy-log-store.c
new file mode 100644
index 0000000..54681fe
--- /dev/null
+++ b/libempathy/empathy-log-store.c
@@ -0,0 +1,156 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#include "empathy-log-store.h"
+
+GType
+empathy_log_store_get_type (void)
+{
+  static GType type = 0;
+  if (type == 0) {
+    static const GTypeInfo info = {
+      sizeof (EmpathyLogStoreInterface),
+      NULL,   /* base_init */
+      NULL,   /* base_finalize */
+      NULL,   /* class_init */
+      NULL,   /* class_finalize */
+      NULL,   /* class_data */
+      0,
+      0,      /* n_preallocs */
+      NULL    /* instance_init */
+    };
+    type = g_type_register_static (G_TYPE_INTERFACE, "EmpathyLogStore",
+        &info, 0);
+  }
+  return type;
+}
+
+const gchar *
+empathy_log_store_get_name (EmpathyLogStore *self)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_name)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_name (self);
+}
+
+gboolean
+empathy_log_store_exists (EmpathyLogStore *self,
+                          McAccount *account,
+                          const gchar *chat_id,
+                          gboolean chatroom)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->exists)
+    return FALSE;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->exists (
+      self, account, chat_id, chatroom);
+}
+
+
+
+gboolean
+empathy_log_store_add_message (EmpathyLogStore *self,
+                               const gchar *chat_id,
+                               gboolean chatroom,
+                               EmpathyMessage *message,
+                               GError **error)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->add_message)
+    return FALSE;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->add_message (
+      self, chat_id, chatroom, message, error);
+}
+
+GList *
+empathy_log_store_get_dates (EmpathyLogStore *self,
+                             McAccount *account,
+                             const gchar *chat_id,
+                             gboolean chatroom)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_dates)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_dates (
+      self, account, chat_id, chatroom);
+}
+
+GList *
+empathy_log_store_get_messages_for_date (EmpathyLogStore *self,
+                                         McAccount *account,
+                                         const gchar *chat_id,
+                                         gboolean chatroom,
+                                         const gchar *date)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_messages_for_date)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_messages_for_date (
+      self, account, chat_id, chatroom, date);
+}
+
+GList *
+empathy_log_store_get_last_messages (EmpathyLogStore *self,
+                                     McAccount *account,
+                                     const gchar *chat_id,
+                                     gboolean chatroom)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_last_messages)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_last_messages (
+      self, account, chat_id, chatroom);
+}
+
+GList *
+empathy_log_store_get_chats (EmpathyLogStore *self,
+                             McAccount *account)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_chats)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->get_chats (self, account);
+}
+
+GList *
+empathy_log_store_search_new (EmpathyLogStore *self,
+                              const gchar *text)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->search_new)
+    return NULL;
+
+  return EMPATHY_LOG_STORE_GET_INTERFACE (self)->search_new (self, text);
+}
+
+void
+empathy_log_store_ack_message (EmpathyLogStore *self,
+                               const gchar *chat_id,
+                               gboolean chatroom,
+                               EmpathyMessage *message)
+{
+  if (!EMPATHY_LOG_STORE_GET_INTERFACE (self)->ack_message)
+    return;
+
+  EMPATHY_LOG_STORE_GET_INTERFACE (self)->ack_message (
+      self, chat_id, chatroom, message);
+}
diff --git a/libempathy/empathy-log-store.h b/libempathy/empathy-log-store.h
new file mode 100644
index 0000000..c14f7f1
--- /dev/null
+++ b/libempathy/empathy-log-store.h
@@ -0,0 +1,94 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2008 Collabora Ltd.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Jonny Lamb <jonny.lamb@collabora.co.uk>
+ */
+
+#ifndef __EMPATHY_LOG_STORE_H__
+#define __EMPATHY_LOG_STORE_H__
+
+#include <glib-object.h>
+
+#include <libmissioncontrol/mc-account.h>
+
+#include "empathy-message.h"
+
+G_BEGIN_DECLS
+
+#define EMPATHY_TYPE_LOG_STORE (empathy_log_store_get_type ())
+#define EMPATHY_LOG_STORE(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMPATHY_TYPE_LOG_STORE, \
+                               EmpathyLogStore))
+#define EMPATHY_IS_LOG_STORE(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMPATHY_TYPE_LOG_STORE))
+#define EMPATHY_LOG_STORE_GET_INTERFACE(inst) \
+  (G_TYPE_INSTANCE_GET_INTERFACE ((inst), EMPATHY_TYPE_LOG_STORE, \
+                                  EmpathyLogStoreInterface))
+
+typedef struct _EmpathyLogStore EmpathyLogStore; /* dummy object */
+typedef struct _EmpathyLogStoreInterface EmpathyLogStoreInterface;
+
+struct _EmpathyLogStoreInterface
+{
+  GTypeInterface parent;
+
+  const gchar * (*get_name) (EmpathyLogStore *self);
+  gboolean (*exists) (EmpathyLogStore *self, McAccount *account,
+      const gchar *chat_id, gboolean chatroom);
+  gboolean (*add_message) (EmpathyLogStore *self, const gchar *chat_id,
+      gboolean chatroom, EmpathyMessage *message, GError **error);
+  GList * (*get_dates) (EmpathyLogStore *self, McAccount *account,
+      const gchar *chat_id, gboolean chatroom);
+  GList * (*get_messages_for_date) (EmpathyLogStore *self,
+      McAccount *account, const gchar *chat_id, gboolean chatroom,
+      const gchar *date);
+  GList * (*get_last_messages) (EmpathyLogStore *self, McAccount *account,
+      const gchar *chat_id, gboolean chatroom);
+  GList * (*get_chats) (EmpathyLogStore *self,
+            McAccount         *account);
+  GList * (*search_new) (EmpathyLogStore *self, const gchar *text);
+  void (*ack_message) (EmpathyLogStore *self, const gchar *chat_id,
+      gboolean chatroom, EmpathyMessage *message);
+};
+
+GType empathy_log_store_get_type (void) G_GNUC_CONST;
+
+const gchar *empathy_log_store_get_name (EmpathyLogStore *self);
+gboolean empathy_log_store_exists (EmpathyLogStore *self,
+    McAccount *account, const gchar *chat_id, gboolean chatroom);
+gboolean empathy_log_store_add_message (EmpathyLogStore *self,
+    const gchar *chat_id, gboolean chatroom, EmpathyMessage *message,
+    GError **error);
+GList *empathy_log_store_get_dates (EmpathyLogStore *self,
+    McAccount *account, const gchar *chat_id, gboolean chatroom);
+GList *empathy_log_store_get_messages_for_date (EmpathyLogStore *self,
+    McAccount *account, const gchar *chat_id, gboolean chatroom,
+    const gchar *date);
+GList *empathy_log_store_get_last_messages (EmpathyLogStore *self,
+    McAccount *account, const gchar *chat_id, gboolean chatroom);
+GList *empathy_log_store_get_chats (EmpathyLogStore *self,
+    McAccount *account);
+GList *empathy_log_store_search_new (EmpathyLogStore *self,
+    const gchar *text);
+void empathy_log_store_ack_message (EmpathyLogStore *self,
+    const gchar *chat_id, gboolean chatroom, EmpathyMessage *message);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_LOG_STORE_H__ */
diff --git a/src/empathy.c b/src/empathy.c
index a7311eb..067d7c1 100644
--- a/src/empathy.c
+++ b/src/empathy.c
@@ -42,8 +42,10 @@
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-call-factory.h>
 #include <libempathy/empathy-chatroom-manager.h>
+#include <libempathy/empathy-contact-factory.h>
 #include <libempathy/empathy-dispatcher.h>
 #include <libempathy/empathy-dispatch-operation.h>
+#include <libempathy/empathy-log-manager.h>
 #include <libempathy/empathy-tp-chat.h>
 #include <libempathy/empathy-tp-call.h>
 
@@ -410,6 +412,7 @@ main (int argc, char *argv[])
 	guint32            startup_timestamp;
 	EmpathyStatusIcon *icon;
 	EmpathyDispatcher *dispatcher;
+	EmpathyLogManager *log_manager;
 	EmpathyChatroomManager *chatroom_manager;
 	EmpathyCallFactory *call_factory;
 	GtkWidget         *window;
@@ -547,6 +550,10 @@ main (int argc, char *argv[])
 	dispatcher = empathy_dispatcher_dup_singleton ();
 	g_signal_connect (dispatcher, "dispatch", G_CALLBACK (dispatch_cb), NULL);
 
+	/* Logging */
+	log_manager = empathy_log_manager_dup_singleton ();
+	empathy_log_manager_observe (log_manager, dispatcher);
+
 	chatroom_manager = empathy_chatroom_manager_dup_singleton (NULL);
 	empathy_chatroom_manager_observe (chatroom_manager, dispatcher);
 
@@ -563,6 +570,7 @@ main (int argc, char *argv[])
 	g_object_unref (mc);
 	g_object_unref (idle);
 	g_object_unref (icon);
+	g_object_unref (log_manager);
 	g_object_unref (dispatcher);
 	g_object_unref (chatroom_manager);
 
