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>
26 #include <libempathy-gtk/empathy-conf.h>
28 #include <telepathy-glib/account-manager.h>
29 #include <telepathy-glib/util.h>
30 #include <telepathy-glib/defs.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);
104 /* This is copied from empathy-log-store-empathy.c (see #613437) */
106 log_store_account_to_dirname (TpAccount *account)
110 name = tp_proxy_get_object_path (account);
111 if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
112 name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
114 return g_strdelimit (g_strdup (name), "/", '_');
118 get_log_dir_for_account (TpAccount *account)
123 escaped = log_store_account_to_dirname (account);
125 basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
126 PACKAGE_NAME, "logs", escaped, NULL);
135 migration_account_manager_prepared_cb (GObject *source_object,
136 GAsyncResult *result,
139 TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
140 GError *error = NULL;
144 if (!tp_account_manager_prepare_finish (am, result, &error))
146 DEBUG ("Failed to prepare the account manager: %s", error->message);
147 g_error_free (error);
151 accounts = tp_account_manager_get_valid_accounts (am);
153 for (l = accounts; l != NULL; l = l->next)
155 TpAccount *account = TP_ACCOUNT (l->data);
158 tp_account_parse_object_path (tp_proxy_get_object_path (account),
159 &cm, NULL, NULL, NULL);
161 if (tp_strdiff (cm, "butterfly"))
167 dir = get_log_dir_for_account (account);
168 DEBUG ("Migrating all logs from dir: %s", dir);
170 migrate_log_files_in_dir (dir);
176 DEBUG ("Finished all migrating");
178 conf = empathy_conf_get ();
179 empathy_conf_set_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED, TRUE);
181 g_list_free (accounts);
185 migrate_logs (gpointer data)
187 TpAccountManager *account_manager;
189 account_manager = tp_account_manager_dup ();
191 tp_account_manager_prepare_async (account_manager, NULL,
192 migration_account_manager_prepared_cb, NULL);
194 g_object_unref (account_manager);
200 empathy_migrate_butterfly_logs (EmpathyContact *contact)
203 gboolean logs_migrated;
206 conf = empathy_conf_get ();
208 /* Already in progress. */
209 if (butterfly_log_migration_id != 0)
213 if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
220 tp_account_parse_object_path (
221 tp_proxy_get_object_path (empathy_contact_get_account (contact)),
222 &cm, NULL, NULL, NULL);
224 if (tp_strdiff (cm, "butterfly"))
231 if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
232 || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
235 /* Okay, we know a new butterfly is being used, so we should migrate its logs */
236 butterfly_log_migration_id = g_idle_add_full (G_PRIORITY_LOW,
237 migrate_logs, NULL, NULL);