]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-log-store-empathy.c
add myself to AUTHORS
[empathy.git] / libempathy / empathy-log-store-empathy.c
index 7e50cb12facaba8860b110ffdc7c19848dd9b641..2088fca03ee1f1cfaa94c40a81a2cb46b0fb6b2d 100644 (file)
 #include <stdlib.h>
 #include <glib/gstdio.h>
 
+#include <telepathy-glib/account-manager.h>
+#include <telepathy-glib/util.h>
+#include <telepathy-glib/defs.h>
+
 #include "empathy-log-store.h"
 #include "empathy-log-store-empathy.h"
 #include "empathy-log-manager.h"
-#include "empathy-account-manager.h"
 #include "empathy-contact.h"
 #include "empathy-time.h"
 #include "empathy-utils.h"
@@ -60,7 +63,7 @@ typedef struct
 {
   gchar *basedir;
   gchar *name;
-  EmpathyAccountManager *account_manager;
+  TpAccountManager *account_manager;
 } EmpathyLogStoreEmpathyPriv;
 
 static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
@@ -98,33 +101,48 @@ empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
 
   self->priv = priv;
 
-  priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (),
-      ".gnome2", PACKAGE_NAME, "logs", NULL);
+  priv->basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
+    PACKAGE_NAME, "logs", NULL);
 
   priv->name = g_strdup ("Empathy");
-  priv->account_manager = empathy_account_manager_dup_singleton ();
+  priv->account_manager = tp_account_manager_dup ();
+}
+
+static gchar *
+log_store_account_to_dirname (TpAccount *account)
+{
+  const gchar *name;
+
+  name = tp_proxy_get_object_path (account);
+  if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
+    name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
+
+  return g_strdelimit (g_strdup (name), "/", '_');
 }
 
+
 static gchar *
 log_store_empathy_get_dir (EmpathyLogStore *self,
-                           EmpathyAccount *account,
+                           TpAccount *account,
                            const gchar *chat_id,
                            gboolean chatroom)
 {
-  const gchar *account_id;
   gchar *basedir;
+  gchar *escaped;
   EmpathyLogStoreEmpathyPriv *priv;
 
   priv = GET_PRIV (self);
 
-  account_id = empathy_account_get_unique_name (account);
+  escaped = log_store_account_to_dirname (account);
 
   if (chatroom)
-    basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
+    basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, escaped,
         LOG_DIR_CHATROOMS, chat_id, NULL);
   else
     basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir,
-        account_id, chat_id, NULL);
+        escaped, chat_id, NULL);
+
+  g_free (escaped);
 
   return basedir;
 }
@@ -158,7 +176,7 @@ log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
 
 static gchar *
 log_store_empathy_get_filename (EmpathyLogStore *self,
-                                EmpathyAccount *account,
+                                TpAccount *account,
                                 const gchar *chat_id,
                                 gboolean chatroom)
 {
@@ -184,7 +202,7 @@ log_store_empathy_add_message (EmpathyLogStore *self,
                                GError **error)
 {
   FILE *file;
-  EmpathyAccount *account;
+  TpAccount *account;
   EmpathyContact *sender;
   const gchar *body_str;
   const gchar *str;
@@ -271,7 +289,7 @@ log_store_empathy_add_message (EmpathyLogStore *self,
 
 static gboolean
 log_store_empathy_exists (EmpathyLogStore *self,
-                          EmpathyAccount *account,
+                          TpAccount *account,
                           const gchar *chat_id,
                           gboolean chatroom)
 {
@@ -287,7 +305,7 @@ log_store_empathy_exists (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_dates (EmpathyLogStore *self,
-                             EmpathyAccount *account,
+                             TpAccount *account,
                              const gchar *chat_id,
                              gboolean chatroom)
 {
@@ -339,7 +357,7 @@ log_store_empathy_get_dates (EmpathyLogStore *self,
 
 static gchar *
 log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
-                                         EmpathyAccount *account,
+                                         TpAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          const gchar *date)
@@ -364,10 +382,11 @@ log_store_empathy_search_hit_new (EmpathyLogStore *self,
 {
   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
   EmpathyLogSearchHit *hit;
-  const gchar *account_name;
+  gchar *account_name;
   const gchar *end;
   gchar **strv;
   guint len;
+  GList *accounts, *l;
 
   if (!g_str_has_suffix (filename, LOG_FILENAME_SUFFIX))
     return NULL;
@@ -387,8 +406,26 @@ log_store_empathy_search_hit_new (EmpathyLogStore *self,
   else
     account_name = strv[len-3];
 
-  hit->account = empathy_account_manager_lookup (priv->account_manager,
-    account_name);
+  /* FIXME: This assumes the account manager is prepared, but the
+   * synchronous API forces this. See bug #599189. */
+  accounts = tp_account_manager_get_valid_accounts (priv->account_manager);
+
+  for (l = accounts; l != NULL; l = g_list_next (l))
+    {
+      TpAccount *account = TP_ACCOUNT (l->data);
+      gchar *name;
+
+      name = log_store_account_to_dirname (account);
+      if (!tp_strdiff (name, account_name))
+        {
+          g_assert (hit->account == NULL);
+          hit->account = account;
+          g_object_ref (account);
+        }
+      g_free (name);
+    }
+  g_list_free (accounts);
+
   hit->filename = g_strdup (filename);
 
   g_strfreev (strv);
@@ -398,6 +435,7 @@ log_store_empathy_search_hit_new (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
+                                         TpAccount *account,
                                          const gchar *filename)
 {
   GList *messages = NULL;
@@ -405,8 +443,6 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
   xmlDocPtr doc;
   xmlNodePtr log_node;
   xmlNodePtr node;
-  EmpathyLogSearchHit *hit;
-  EmpathyAccount *account;
 
   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
   g_return_val_if_fail (filename != NULL, NULL);
@@ -419,11 +455,6 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       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 ();
 
@@ -450,7 +481,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
     {
       EmpathyMessage *message;
       EmpathyContact *sender;
-      gchar *time;
+      gchar *time_;
       time_t t;
       gchar *sender_id;
       gchar *sender_name;
@@ -463,17 +494,18 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       guint cm_id;
       TpChannelTextMessageType msg_type = TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL;
 
-      if (strcmp (node->name, "message") != 0)
+      if (strcmp ((const gchar *) 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");
-      cm_id_str = xmlGetProp (node, "cm_id");
+      body = (gchar *) xmlNodeGetContent (node);
+      time_ = (gchar *) xmlGetProp (node, (const xmlChar *) "time");
+      sender_id = (gchar *) xmlGetProp (node, (const xmlChar *) "id");
+      sender_name = (gchar *) xmlGetProp (node, (const xmlChar *) "name");
+      sender_avatar_token = (gchar *) xmlGetProp (node,
+          (const xmlChar *) "token");
+      is_user_str = (gchar *) xmlGetProp (node, (const xmlChar *) "isuser");
+      msg_type_str = (gchar *) xmlGetProp (node, (const xmlChar *) "type");
+      cm_id_str = (gchar *) xmlGetProp (node, (const xmlChar *) "cm_id");
 
       if (is_user_str)
         is_user = strcmp (is_user_str, "true") == 0;
@@ -484,7 +516,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       if (cm_id_str)
         cm_id = atoi (cm_id_str);
 
-      t = empathy_time_parse (time);
+      t = empathy_time_parse (time_);
 
       sender = empathy_contact_new_for_log (account, sender_id, sender_name,
                                            is_user);
@@ -497,6 +529,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       empathy_message_set_sender (message, sender);
       empathy_message_set_timestamp (message, t);
       empathy_message_set_tptype (message, msg_type);
+      empathy_message_set_is_backlog (message, TRUE);
 
       if (cm_id_str)
         empathy_message_set_id (message, cm_id);
@@ -504,7 +537,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       messages = g_list_append (messages, message);
 
       g_object_unref (sender);
-      xmlFree (time);
+      xmlFree (time_);
       xmlFree (sender_id);
       xmlFree (sender_name);
       xmlFree (body);
@@ -600,7 +633,7 @@ log_store_empathy_search_new (EmpathyLogStore *self,
       contents = g_mapped_file_get_contents (file);
       contents_casefold = g_utf8_casefold (contents, length);
 
-      g_mapped_file_free (file);
+      g_mapped_file_unref (file);
 
       if (strstr (contents_casefold, text_casefold))
         {
@@ -671,7 +704,7 @@ log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
-                                         EmpathyAccount *account,
+                                         TpAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          const gchar *date)
@@ -681,10 +714,12 @@ log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
 
   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
   g_return_val_if_fail (chat_id != NULL, NULL);
+  g_return_val_if_fail (account != 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);
+  messages = log_store_empathy_get_messages_for_file (self, account,
+    filename);
   g_free (filename);
 
   return messages;
@@ -692,7 +727,7 @@ log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_chats (EmpathyLogStore *self,
-                              EmpathyAccount *account)
+                              TpAccount *account)
 {
   gchar *dir;
   GList *hits;
@@ -700,8 +735,7 @@ log_store_empathy_get_chats (EmpathyLogStore *self,
 
   priv = GET_PRIV (self);
 
-  dir = g_build_filename (priv->basedir,
-      empathy_account_get_unique_name (account), NULL);
+  dir = log_store_empathy_get_dir (self, account, NULL, FALSE);
 
   hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
 
@@ -720,7 +754,7 @@ log_store_empathy_get_name (EmpathyLogStore *self)
 
 static GList *
 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
-                                         EmpathyAccount *account,
+                                         TpAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          guint num_messages,