Updated Portuguese translation
[empathy-mirror.git] / src / empathy-migrate-butterfly-logs.c
blob4a8bf973ce2597094cb38162c1ad8678c3e96b37
1 /*
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
19 #include <string.h>
21 #include <gio/gio.h>
23 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
24 #include <libempathy/empathy-debug.h>
25 #include <libempathy/empathy-gsettings.h>
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/defs.h>
31 #include "empathy-migrate-butterfly-logs.h"
33 static guint butterfly_log_migration_id = 0;
35 static void
36 migrate_log_files_in_dir (const gchar *dirname)
38 GDir *dir;
39 const gchar *subdir;
40 gchar *new_name;
41 gchar *full_path;
42 GError *error = NULL;
44 dir = g_dir_open (dirname, 0, &error);
46 if (dir == NULL)
48 DEBUG ("Failed to open dir: %s", error->message);
49 g_error_free (error);
50 return;
53 while ((subdir = g_dir_read_name (dir)) != NULL)
55 GFile *old_gfile, *new_gfile;
57 if (!tp_strdiff (subdir, "chatrooms"))
58 continue;
60 if (g_str_has_suffix (subdir, "#1"))
62 new_name = g_strndup (subdir, (strlen (subdir) - 2));
64 else if (g_str_has_suffix (subdir, "#32"))
66 gchar *tmp;
67 tmp = g_strndup (subdir, (strlen (subdir) - 3));
68 new_name = g_strdup_printf ("%s#yahoo", tmp);
69 g_free (tmp);
71 else
73 continue;
76 full_path = g_build_filename (dirname, subdir, NULL);
77 old_gfile = g_file_new_for_path (full_path);
78 g_free (full_path);
80 full_path = g_build_filename (dirname, new_name, NULL);
81 new_gfile = g_file_new_for_path (full_path);
82 g_free (full_path);
84 if (!g_file_move (old_gfile, new_gfile, G_FILE_COPY_NONE,
85 NULL, NULL, NULL, &error))
87 DEBUG ("Failed to move file: %s", error->message);
88 g_clear_error (&error);
90 else
92 DEBUG ("Successfully migrated logs for %s", new_name);
95 g_free (new_name);
96 g_object_unref (old_gfile);
97 g_object_unref (new_gfile);
100 g_dir_close (dir);
103 /* This is copied from empathy-log-store-empathy.c (see #613437) */
104 static gchar *
105 log_store_account_to_dirname (TpAccount *account)
107 const gchar *name;
109 name = tp_proxy_get_object_path (account);
110 if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
111 name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
113 return g_strdelimit (g_strdup (name), "/", '_');
116 static gchar *
117 get_log_dir_for_account (TpAccount *account)
119 gchar *basedir;
120 gchar *escaped;
122 escaped = log_store_account_to_dirname (account);
124 basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
125 PACKAGE_NAME, "logs", escaped, NULL);
127 g_free (escaped);
129 return basedir;
133 static void
134 migration_account_manager_prepared_cb (GObject *source_object,
135 GAsyncResult *result,
136 gpointer user_data)
138 TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
139 GError *error = NULL;
140 GList *accounts, *l;
141 GSettings *gsettings;
143 if (!tp_account_manager_prepare_finish (am, result, &error))
145 DEBUG ("Failed to prepare the account manager: %s", error->message);
146 g_error_free (error);
147 return;
150 accounts = tp_account_manager_get_valid_accounts (am);
152 for (l = accounts; l != NULL; l = l->next)
154 TpAccount *account = TP_ACCOUNT (l->data);
155 gchar *dir, *cm;
157 tp_account_parse_object_path (tp_proxy_get_object_path (account),
158 &cm, NULL, NULL, NULL);
160 if (tp_strdiff (cm, "butterfly"))
162 g_free (cm);
163 continue;
166 dir = get_log_dir_for_account (account);
167 DEBUG ("Migrating all logs from dir: %s", dir);
169 migrate_log_files_in_dir (dir);
171 g_free (cm);
172 g_free (dir);
175 DEBUG ("Finished all migrating");
177 gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
178 g_settings_set_boolean (gsettings, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
179 TRUE);
180 g_object_unref (gsettings);
182 g_list_free (accounts);
185 static gboolean
186 migrate_logs (gpointer data)
188 TpAccountManager *account_manager;
190 account_manager = tp_account_manager_dup ();
192 tp_account_manager_prepare_async (account_manager, NULL,
193 migration_account_manager_prepared_cb, NULL);
195 g_object_unref (account_manager);
197 return FALSE;
200 gboolean
201 empathy_migrate_butterfly_logs (EmpathyContact *contact)
203 GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
204 gchar *cm;
206 /* Already in progress. */
207 if (butterfly_log_migration_id != 0)
208 return FALSE;
210 /* Already done. */
211 if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED))
212 return FALSE;
214 tp_account_parse_object_path (
215 tp_proxy_get_object_path (empathy_contact_get_account (contact)),
216 &cm, NULL, NULL, NULL);
218 if (tp_strdiff (cm, "butterfly"))
220 g_free (cm);
221 return TRUE;
223 g_free (cm);
225 if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
226 || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
227 return TRUE;
229 /* Okay, we know a new butterfly is being used, so we should migrate its logs */
230 butterfly_log_migration_id = g_idle_add_full (G_PRIORITY_LOW,
231 migrate_logs, NULL, NULL);
233 return FALSE;