]> git.0d.be Git - empathy.git/commitdiff
Migrate butterfly logs to remove the network ID suffix.
authorJonny Lamb <jonnylamb@gnome.org>
Fri, 12 Mar 2010 02:22:47 +0000 (02:22 +0000)
committerJonny Lamb <jonnylamb@gnome.org>
Fri, 12 Mar 2010 16:47:09 +0000 (16:47 +0000)
Bug #612519 contains much more information about this change.

Signed-off-by: Jonny Lamb <jonnylamb@gnome.org>
data/empathy.schemas.in
libempathy-gtk/empathy-conf.h
libempathy/empathy-log-store-empathy.c
libempathy/empathy-log-store-empathy.h
src/Makefile.am
src/empathy-main-window.c
src/empathy-migrate-butterfly-logs.c [new file with mode: 0644]
src/empathy-migrate-butterfly-logs.h [new file with mode: 0644]

index 2d9031bc284a98cf4103472f3af1b217be77f8eb..e151fb5f01cae6bc1fb366abf5bde2e72a6a549e 100644 (file)
       </locale>
     </schema>
 
+    <schema>
+      <key>/schemas/apps/empathy/butterfly_logs_migrated</key>
+      <applyto>/apps/empathy/butterfly_logs_migrated</applyto>
+      <owner>empathy</owner>
+      <type>bool</type>
+      <default>false</default>
+      <locale name="C">
+        <short>Empathy has migrated butterfly logs</short>
+        <long>
+        Whether Empathy has migrated butterfly logs.
+        </long>
+      </locale>
+    </schema>
+
     <schema>
       <key>/schemas/apps/empathy/file_transfer/default_folder</key>
       <applyto>/apps/empathy/file_transfer_default_folder</applyto>
index 2a74c1da55f281d7eaea0de9fd85779212f8a511..21e3b38107b80b09e8c4a33b0b5ce6383a1dc78f 100644 (file)
@@ -82,6 +82,7 @@ struct _EmpathyConfClass {
 #define EMPATHY_PREFS_USE_CONN                     EMPATHY_PREFS_PATH "/use_conn"
 #define EMPATHY_PREFS_AUTOCONNECT                  EMPATHY_PREFS_PATH "/autoconnect"
 #define EMPATHY_PREFS_IMPORT_ASKED                 EMPATHY_PREFS_PATH "/import_asked"
+#define EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED      EMPATHY_PREFS_PATH "/butterfly_logs_migrated"
 #define EMPATHY_PREFS_FILE_TRANSFER_DEFAULT_FOLDER EMPATHY_PREFS_PATH "/file_transfer/default_folder"
 #define EMPATHY_PREFS_LOCATION_PUBLISH             EMPATHY_PREFS_PATH "/location/publish"
 #define EMPATHY_PREFS_LOCATION_RESOURCE_NETWORK    EMPATHY_PREFS_PATH "/location/resource_network"
index 2088fca03ee1f1cfaa94c40a81a2cb46b0fb6b2d..06eb935ca5c6f6efc8ea219c7c7afa47094a6857 100644 (file)
@@ -815,3 +815,10 @@ log_store_iface_init (gpointer g_iface,
   iface->ack_message = NULL;
   iface->get_filtered_messages = log_store_empathy_get_filtered_messages;
 }
+
+gchar *
+empathy_log_store_empathy_get_dir (EmpathyLogStoreEmpathy *store,
+                                   TpAccount *account)
+{
+  return log_store_empathy_get_dir (EMPATHY_LOG_STORE (store), account, NULL, FALSE);
+}
index 3b26d2ccfb82e698df4ab36eec30a755f3d9c1a2..5fa6c22868df8699a76f7901aef3d0cb7b400d68 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <glib.h>
 
+#include <telepathy-glib/account.h>
+
 G_BEGIN_DECLS
 
 #define EMPATHY_TYPE_LOG_STORE_EMPATHY \
@@ -64,6 +66,9 @@ struct _EmpathyLogStoreEmpathyClass
 
 GType empathy_log_store_empathy_get_type (void);
 
+gchar * empathy_log_store_empathy_get_dir (EmpathyLogStoreEmpathy *store,
+    TpAccount *account);
+
 G_END_DECLS
 
 #endif /* ENABLE_TPL */
index 3f8d15733ffd19149074693fca00f9ed87526613..f63f7d65cc071230a50fd79ab34e8c34950d6017 100644 (file)
@@ -105,6 +105,7 @@ empathy_handwritten_source = \
        empathy-ft-manager.c empathy-ft-manager.h                       \
        empathy-invite-participant-dialog.c empathy-invite-participant-dialog.h \
        empathy-main-window.c empathy-main-window.h                     \
+       empathy-migrate-butterfly-logs.c empathy-migrate-butterfly-logs.h \
        empathy-new-chatroom-dialog.c empathy-new-chatroom-dialog.h     \
        empathy-preferences.c empathy-preferences.h                     \
        empathy-sidebar.c empathy-sidebar.h                             \
index 240f4b18be8ae8a1ae0526f18df8852fe321afb4..84331e7bd92f37e22d89f0423df0982290e905cf 100644 (file)
@@ -63,6 +63,7 @@
 #include "empathy-chatrooms-window.h"
 #include "empathy-event-manager.h"
 #include "empathy-ft-manager.h"
+#include "empathy-migrate-butterfly-logs.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include <libempathy/empathy-debug.h>
@@ -119,6 +120,9 @@ typedef struct {
 
        /* Actions that are enabled when there are connected accounts */
        GList                  *actions_connected;
+
+       /* The idle event source to migrate butterfly's logs */
+       guint butterfly_log_migration_contact_added_id;
 } EmpathyMainWindow;
 
 static EmpathyMainWindow *main_window = NULL;
@@ -1243,6 +1247,18 @@ account_manager_prepared_cb (GObject *source_object,
        g_list_free (accounts);
 }
 
+static void
+main_window_contact_added_cb (EmpathyContactMonitor    *monitor,
+                             EmpathyContact            *contact,
+                             EmpathyMainWindow         *window)
+{
+       if (!empathy_migrate_butterfly_logs (contact)) {
+               g_signal_handler_disconnect (monitor,
+                                            window->butterfly_log_migration_contact_added_id);
+               window->butterfly_log_migration_contact_added_id = 0;
+       }
+}
+
 GtkWidget *
 empathy_main_window_show (void)
 {
@@ -1391,6 +1407,8 @@ empathy_main_window_show (void)
                                                           EMPATHY_CONTACT_FEATURE_ALL);
        g_signal_connect (monitor, "contact-presence-changed",
                          G_CALLBACK (main_window_contact_presence_changed_cb), window);
+       window->butterfly_log_migration_contact_added_id =  g_signal_connect (monitor, "contact-added",
+                         G_CALLBACK (main_window_contact_added_cb), window);
        g_object_unref (list_iface);
 
        gtk_widget_show (GTK_WIDGET (window->list_view));
diff --git a/src/empathy-migrate-butterfly-logs.c b/src/empathy-migrate-butterfly-logs.c
new file mode 100644 (file)
index 0000000..50d7b8e
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+*  Copyright (C) 2010 Collabora Ltd.
+*
+*  This library is free software; you can redistribute it and/or
+*  modify it under the terms of the GNU Lesser General Public
+*  License as published by the Free Software Foundation; either
+*  version 2.1 of the License, or (at your option) any later version.
+*
+*  This library 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
+*  Lesser General Public License for more details.
+*
+*  You should have received a copy of the GNU Lesser General Public
+*  License along with this library; if not, write to the Free Software
+*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include <string.h>
+
+#include <gio/gio.h>
+
+#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
+#include <libempathy/empathy-debug.h>
+#include <libempathy/empathy-log-store-empathy.h>
+
+#include <libempathy-gtk/empathy-conf.h>
+
+#include <telepathy-glib/account-manager.h>
+#include <telepathy-glib/util.h>
+
+#include "empathy-migrate-butterfly-logs.h"
+
+static guint butterfly_log_migration_id = 0;
+
+static void
+migrate_log_files_in_dir (const gchar *dirname)
+{
+  GDir *dir;
+  const gchar *subdir;
+  gchar *new_name;
+  gchar *full_path;
+  GError *error = NULL;
+
+  dir = g_dir_open (dirname, 0, &error);
+
+  if (dir == NULL)
+    {
+      DEBUG ("Failed to open dir: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  while ((subdir = g_dir_read_name (dir)) != NULL)
+    {
+      GFile *old_gfile, *new_gfile;
+
+      if (!tp_strdiff (subdir, "chatrooms"))
+        continue;
+
+      if (g_str_has_suffix (subdir, "#1"))
+        {
+          new_name = g_strndup (subdir, (strlen (subdir) - 2));
+        }
+      else if (g_str_has_suffix (subdir, "#32"))
+        {
+          gchar *tmp;
+          tmp = g_strndup (subdir, (strlen (subdir) - 3));
+          new_name = g_strdup_printf ("%s#yahoo", tmp);
+          g_free (tmp);
+        }
+      else
+        {
+          continue;
+        }
+
+      full_path = g_build_filename (dirname, subdir, NULL);
+      old_gfile = g_file_new_for_path (full_path);
+      g_free (full_path);
+
+      full_path = g_build_filename (dirname, new_name, NULL);
+      new_gfile = g_file_new_for_path (full_path);
+      g_free (full_path);
+
+      if (!g_file_move (old_gfile, new_gfile, G_FILE_COPY_NONE,
+              NULL, NULL, NULL, &error))
+        {
+          DEBUG ("Failed to move file: %s", error->message);
+          g_clear_error (&error);
+        }
+      else
+        {
+          DEBUG ("Successfully migrated logs for %s", new_name);
+        }
+
+      g_free (new_name);
+      g_object_unref (old_gfile);
+      g_object_unref (new_gfile);
+    }
+
+  g_dir_close (dir);
+}
+
+static void
+migration_account_manager_prepared_cb (GObject *source_object,
+    GAsyncResult *result,
+    gpointer user_data)
+{
+  TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
+  GError *error = NULL;
+  GList *accounts, *l;
+  EmpathyLogStoreEmpathy *log_store;
+  EmpathyConf *conf;
+
+  if (!tp_account_manager_prepare_finish (am, result, &error))
+    {
+      DEBUG ("Failed to prepare the account manager: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  log_store = g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL);
+  accounts = tp_account_manager_get_valid_accounts (am);
+
+  for (l = accounts; l != NULL; l = l->next)
+    {
+      TpAccount *account = TP_ACCOUNT (l->data);
+      gchar *dir, *cm;
+
+      tp_account_parse_object_path (tp_proxy_get_object_path (account),
+          &cm, NULL, NULL, NULL);
+
+      if (tp_strdiff (cm, "butterfly"))
+        {
+          g_free (cm);
+          continue;
+        }
+
+      dir = empathy_log_store_empathy_get_dir (log_store, account);
+      DEBUG ("Migrating all logs from dir: %s", dir);
+
+      migrate_log_files_in_dir (dir);
+
+      g_free (cm);
+      g_free (dir);
+    }
+
+  DEBUG ("Finished all migrating");
+
+  conf = empathy_conf_get ();
+  empathy_conf_set_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED, TRUE);
+
+  g_list_free (accounts);
+  g_object_unref (log_store);
+}
+
+static gboolean
+migrate_logs (gpointer data)
+{
+  TpAccountManager *account_manager;
+
+  account_manager = tp_account_manager_dup ();
+
+  tp_account_manager_prepare_async (account_manager, NULL,
+      migration_account_manager_prepared_cb, NULL);
+
+  g_object_unref (account_manager);
+
+  return FALSE;
+}
+
+gboolean
+empathy_migrate_butterfly_logs (EmpathyContact *contact)
+{
+  EmpathyConf *conf;
+  gboolean logs_migrated;
+  gchar *cm;
+
+  conf = empathy_conf_get ();
+
+  /* Already in progress. */
+  if (butterfly_log_migration_id != 0)
+    return FALSE;
+
+  /* Already done. */
+  if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
+          &logs_migrated))
+    return FALSE;
+
+  if (logs_migrated)
+    return FALSE;
+
+  tp_account_parse_object_path (
+      tp_proxy_get_object_path (empathy_contact_get_account (contact)),
+      &cm, NULL, NULL, NULL);
+
+  if (tp_strdiff (cm, "butterfly"))
+    {
+      g_free (cm);
+      return TRUE;
+    }
+  g_free (cm);
+
+  if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
+      || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
+    return TRUE;
+
+  /* Okay, we know a new butterfly is being used, so we should migrate its logs */
+  butterfly_log_migration_id = g_idle_add_full (G_PRIORITY_LOW,
+      migrate_logs, NULL, NULL);
+
+  return FALSE;
+}
diff --git a/src/empathy-migrate-butterfly-logs.h b/src/empathy-migrate-butterfly-logs.h
new file mode 100644 (file)
index 0000000..a06c1b4
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+*  Copyright (C) 2010 Collabora Ltd.
+*
+*  This library is free software; you can redistribute it and/or
+*  modify it under the terms of the GNU Lesser General Public
+*  License as published by the Free Software Foundation; either
+*  version 2.1 of the License, or (at your option) any later version.
+*
+*  This library 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
+*  Lesser General Public License for more details.
+*
+*  You should have received a copy of the GNU Lesser General Public
+*  License along with this library; if not, write to the Free Software
+*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+#include <glib.h>
+
+#include <libempathy/empathy-contact.h>
+
+#ifndef __EMPATHY_MIGRATE_BUTTERFLY_LOGS_H__
+#define __EMPATHY_MIGRATE_BUTTERFLY_LOGS_H__
+
+G_BEGIN_DECLS
+
+gboolean empathy_migrate_butterfly_logs (EmpathyContact *contact);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_MIGRATE_BUTTERFLY_LOGS_H__ */