]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-log-store-empathy.c
Merge commit 'jtellier/video-call-button-sensitivity'
[empathy.git] / libempathy / empathy-log-store-empathy.c
index 3a924aa53ec04fda4544f9684422eb880e5639ca..b814defe723e2b1605706fdaa00c345735625f8e 100644 (file)
@@ -15,8 +15,8 @@
  *
  * 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA  02110-1301  USA
  *
  * Authors: Xavier Claessens <xclaesse@gmail.com>
  *          Jonny Lamb <jonny.lamb@collabora.co.uk>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
+
+/* FIXME: g_mapped_file_free has been deprecated in GLib 2.22, but the
+ * replacement symbol, g_mapped_file_unref is not available in older Glib
+ * and we're not ready to bump our version requirement just for this. When
+ * we're ready to bump our version requirement, just revert this patch. */
+#undef G_DISABLE_DEPRECATED
 #include <glib/gstdio.h>
+#define G_DISABLE_DEPRECATED
 
 #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"
@@ -59,6 +67,7 @@ typedef struct
 {
   gchar *basedir;
   gchar *name;
+  EmpathyAccountManager *account_manager;
 } EmpathyLogStoreEmpathyPriv;
 
 static void log_store_iface_init (gpointer g_iface,gpointer iface_data);
@@ -73,6 +82,7 @@ log_store_empathy_finalize (GObject *object)
   EmpathyLogStoreEmpathy *self = EMPATHY_LOG_STORE_EMPATHY (object);
   EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
 
+  g_object_unref (priv->account_manager);
   g_free (priv->basedir);
   g_free (priv->name);
 }
@@ -99,11 +109,12 @@ empathy_log_store_empathy_init (EmpathyLogStoreEmpathy *self)
       ".gnome2", PACKAGE_NAME, "logs", NULL);
 
   priv->name = g_strdup ("Empathy");
+  priv->account_manager = empathy_account_manager_dup_singleton ();
 }
 
 static gchar *
 log_store_empathy_get_dir (EmpathyLogStore *self,
-                           McAccount *account,
+                           EmpathyAccount *account,
                            const gchar *chat_id,
                            gboolean chatroom)
 {
@@ -113,7 +124,7 @@ log_store_empathy_get_dir (EmpathyLogStore *self,
 
   priv = GET_PRIV (self);
 
-  account_id = mc_account_get_unique_name (account);
+  account_id = empathy_account_get_unique_name (account);
 
   if (chatroom)
     basedir = g_build_path (G_DIR_SEPARATOR_S, priv->basedir, account_id,
@@ -154,7 +165,7 @@ log_store_empathy_get_timestamp_from_message (EmpathyMessage *message)
 
 static gchar *
 log_store_empathy_get_filename (EmpathyLogStore *self,
-                                McAccount *account,
+                                EmpathyAccount *account,
                                 const gchar *chat_id,
                                 gboolean chatroom)
 {
@@ -180,7 +191,7 @@ log_store_empathy_add_message (EmpathyLogStore *self,
                                GError **error)
 {
   FILE *file;
-  McAccount *account;
+  EmpathyAccount *account;
   EmpathyContact *sender;
   const gchar *body_str;
   const gchar *str;
@@ -267,7 +278,7 @@ log_store_empathy_add_message (EmpathyLogStore *self,
 
 static gboolean
 log_store_empathy_exists (EmpathyLogStore *self,
-                          McAccount *account,
+                          EmpathyAccount *account,
                           const gchar *chat_id,
                           gboolean chatroom)
 {
@@ -283,7 +294,7 @@ log_store_empathy_exists (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_dates (EmpathyLogStore *self,
-                             McAccount *account,
+                             EmpathyAccount *account,
                              const gchar *chat_id,
                              gboolean chatroom)
 {
@@ -295,7 +306,6 @@ log_store_empathy_get_dates (EmpathyLogStore *self,
   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);
@@ -336,7 +346,7 @@ log_store_empathy_get_dates (EmpathyLogStore *self,
 
 static gchar *
 log_store_empathy_get_filename_for_date (EmpathyLogStore *self,
-                                         McAccount *account,
+                                         EmpathyAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          const gchar *date)
@@ -359,6 +369,7 @@ static EmpathyLogSearchHit *
 log_store_empathy_search_hit_new (EmpathyLogStore *self,
                                   const gchar *filename)
 {
+  EmpathyLogStoreEmpathyPriv *priv = GET_PRIV (self);
   EmpathyLogSearchHit *hit;
   const gchar *account_name;
   const gchar *end;
@@ -383,7 +394,8 @@ log_store_empathy_search_hit_new (EmpathyLogStore *self,
   else
     account_name = strv[len-3];
 
-  hit->account = mc_account_lookup (account_name);
+  hit->account = empathy_account_manager_lookup (priv->account_manager,
+    account_name);
   hit->filename = g_strdup (filename);
 
   g_strfreev (strv);
@@ -401,7 +413,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
   xmlNodePtr log_node;
   xmlNodePtr node;
   EmpathyLogSearchHit *hit;
-  McAccount *account;
+  EmpathyAccount *account;
 
   g_return_val_if_fail (EMPATHY_IS_LOG_STORE (self), NULL);
   g_return_val_if_fail (filename != NULL, NULL);
@@ -481,8 +493,9 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
 
       t = empathy_time_parse (time);
 
-      sender = empathy_contact_new_full (account, sender_id, sender_name);
-      empathy_contact_set_is_user (sender, is_user);
+      sender = empathy_contact_new_for_log (account, sender_id, sender_name,
+                                           is_user);
+
       if (!EMP_STR_EMPTY (sender_avatar_token))
         empathy_contact_load_avatar_cache (sender,
             sender_avatar_token);
@@ -491,6 +504,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);
@@ -505,6 +519,7 @@ log_store_empathy_get_messages_for_file (EmpathyLogStore *self,
       xmlFree (is_user_str);
       xmlFree (msg_type_str);
       xmlFree (cm_id_str);
+      xmlFree (sender_avatar_token);
     }
 
   DEBUG ("Parsed %d messages", g_list_length (messages));
@@ -627,25 +642,28 @@ log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
   GDir *gdir;
   GList *hits = NULL;
   const gchar *name;
+  GError *error = NULL;
 
-  gdir = g_dir_open (dir, 0, NULL);
+  gdir = g_dir_open (dir, 0, &error);
   if (!gdir)
-    return NULL;
+    {
+      DEBUG ("Failed to open directory: %s, error: %s", dir, error->message);
+      g_error_free (error);
+      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)
+      if (!is_chatroom && strcmp (name, LOG_DIR_CHATROOMS) == 0)
         {
+          gchar *filename = g_build_filename (dir, name, NULL);
           hits = g_list_concat (hits, log_store_empathy_get_chats_for_dir (
-                self, dir, TRUE));
+                self, filename, TRUE));
           g_free (filename);
           continue;
         }
-
       hit = g_slice_new0 (EmpathyLogSearchHit);
       hit->chat_id = g_strdup (name);
       hit->is_chatroom = is_chatroom;
@@ -661,7 +679,7 @@ log_store_empathy_get_chats_for_dir (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
-                                         McAccount *account,
+                                         EmpathyAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          const gchar *date)
@@ -670,7 +688,6 @@ log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
   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,
@@ -683,7 +700,7 @@ log_store_empathy_get_messages_for_date (EmpathyLogStore *self,
 
 static GList *
 log_store_empathy_get_chats (EmpathyLogStore *self,
-                              McAccount *account)
+                              EmpathyAccount *account)
 {
   gchar *dir;
   GList *hits;
@@ -692,7 +709,7 @@ log_store_empathy_get_chats (EmpathyLogStore *self,
   priv = GET_PRIV (self);
 
   dir = g_build_filename (priv->basedir,
-      mc_account_get_unique_name (account), NULL);
+      empathy_account_get_unique_name (account), NULL);
 
   hits = log_store_empathy_get_chats_for_dir (self, dir, FALSE);
 
@@ -711,7 +728,7 @@ log_store_empathy_get_name (EmpathyLogStore *self)
 
 static GList *
 log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
-                                         McAccount *account,
+                                         EmpathyAccount *account,
                                          const gchar *chat_id,
                                          gboolean chatroom,
                                          guint num_messages,
@@ -719,13 +736,16 @@ log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
                                          gpointer user_data)
 {
   GList *dates, *l, *messages = NULL;
+  guint i = 0;
 
   dates = log_store_empathy_get_dates (self, account, chat_id, chatroom);
 
-  for (l = g_list_last (dates); l && g_list_length (messages) < num_messages; l = g_list_previous (l))
+  for (l = g_list_last (dates); l && i < num_messages; l = g_list_previous (l))
     {
       GList *new_messages, *n, *next;
 
+      /* FIXME: We should really restrict the message parsing to get only
+       * the newest num_messages. */
       new_messages = log_store_empathy_get_messages_for_date (self, account,
           chat_id, chatroom, l->data);
 
@@ -738,6 +758,10 @@ log_store_empathy_get_filtered_messages (EmpathyLogStore *self,
               g_object_unref (n->data);
               new_messages = g_list_delete_link (new_messages, n);
             }
+          else
+            {
+              i++;
+            }
           n = next;
         }
       messages = g_list_concat (messages, new_messages);