2 * Copyright (C) 2010 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
24 #include <libempathy/empathy-debug.h>
25 #include <libempathy/empathy-log-store-empathy.h>
27 #include <libempathy-gtk/empathy-conf.h>
29 #include <telepathy-glib/account-manager.h>
30 #include <telepathy-glib/util.h>
32 #include "empathy-migrate-butterfly-logs.h"
34 static guint butterfly_log_migration_id = 0;
37 migrate_log_files_in_dir (const gchar *dirname)
45 dir = g_dir_open (dirname, 0, &error);
49 DEBUG ("Failed to open dir: %s", error->message);
54 while ((subdir = g_dir_read_name (dir)) != NULL)
56 GFile *old_gfile, *new_gfile;
58 if (!tp_strdiff (subdir, "chatrooms"))
61 if (g_str_has_suffix (subdir, "#1"))
63 new_name = g_strndup (subdir, (strlen (subdir) - 2));
65 else if (g_str_has_suffix (subdir, "#32"))
68 tmp = g_strndup (subdir, (strlen (subdir) - 3));
69 new_name = g_strdup_printf ("%s#yahoo", tmp);
77 full_path = g_build_filename (dirname, subdir, NULL);
78 old_gfile = g_file_new_for_path (full_path);
81 full_path = g_build_filename (dirname, new_name, NULL);
82 new_gfile = g_file_new_for_path (full_path);
85 if (!g_file_move (old_gfile, new_gfile, G_FILE_COPY_NONE,
86 NULL, NULL, NULL, &error))
88 DEBUG ("Failed to move file: %s", error->message);
89 g_clear_error (&error);
93 DEBUG ("Successfully migrated logs for %s", new_name);
97 g_object_unref (old_gfile);
98 g_object_unref (new_gfile);
105 migration_account_manager_prepared_cb (GObject *source_object,
106 GAsyncResult *result,
109 TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
110 GError *error = NULL;
112 EmpathyLogStoreEmpathy *log_store;
115 if (!tp_account_manager_prepare_finish (am, result, &error))
117 DEBUG ("Failed to prepare the account manager: %s", error->message);
118 g_error_free (error);
122 log_store = g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL);
123 accounts = tp_account_manager_get_valid_accounts (am);
125 for (l = accounts; l != NULL; l = l->next)
127 TpAccount *account = TP_ACCOUNT (l->data);
130 tp_account_parse_object_path (tp_proxy_get_object_path (account),
131 &cm, NULL, NULL, NULL);
133 if (tp_strdiff (cm, "butterfly"))
139 dir = empathy_log_store_empathy_get_dir (log_store, account);
140 DEBUG ("Migrating all logs from dir: %s", dir);
142 migrate_log_files_in_dir (dir);
148 DEBUG ("Finished all migrating");
150 conf = empathy_conf_get ();
151 empathy_conf_set_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED, TRUE);
153 g_list_free (accounts);
154 g_object_unref (log_store);
158 migrate_logs (gpointer data)
160 TpAccountManager *account_manager;
162 account_manager = tp_account_manager_dup ();
164 tp_account_manager_prepare_async (account_manager, NULL,
165 migration_account_manager_prepared_cb, NULL);
167 g_object_unref (account_manager);
173 empathy_migrate_butterfly_logs (EmpathyContact *contact)
176 gboolean logs_migrated;
179 conf = empathy_conf_get ();
181 /* Already in progress. */
182 if (butterfly_log_migration_id != 0)
186 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
193 tp_account_parse_object_path (
194 tp_proxy_get_object_path (empathy_contact_get_account (contact)),
195 &cm, NULL, NULL, NULL);
197 if (tp_strdiff (cm, "butterfly"))
204 if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
205 || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
208 /* Okay, we know a new butterfly is being used, so we should migrate its logs */
209 butterfly_log_migration_id = g_idle_add_full (G_PRIORITY_LOW,
210 migrate_logs, NULL, NULL);