]> git.0d.be Git - empathy.git/blob - src/empathy-migrate-butterfly-logs.c
Merge branch 'sasl'
[empathy.git] / src / empathy-migrate-butterfly-logs.c
1 /*
2 *  Copyright (C) 2010 Collabora Ltd.
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include <string.h>
20
21 #include <gio/gio.h>
22
23 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
24 #include <libempathy/empathy-debug.h>
25 #include <libempathy/empathy-gsettings.h>
26
27 #include <telepathy-glib/account-manager.h>
28 #include <telepathy-glib/util.h>
29 #include <telepathy-glib/defs.h>
30
31 #include "empathy-migrate-butterfly-logs.h"
32
33 static guint butterfly_log_migration_id = 0;
34
35 static void
36 migrate_log_files_in_dir (const gchar *dirname)
37 {
38   GDir *dir;
39   const gchar *subdir;
40   gchar *new_name;
41   gchar *full_path;
42   GError *error = NULL;
43
44   dir = g_dir_open (dirname, 0, &error);
45
46   if (dir == NULL)
47     {
48       DEBUG ("Failed to open dir: %s", error->message);
49       g_error_free (error);
50       return;
51     }
52
53   while ((subdir = g_dir_read_name (dir)) != NULL)
54     {
55       GFile *old_gfile, *new_gfile;
56
57       if (!tp_strdiff (subdir, "chatrooms"))
58         continue;
59
60       if (g_str_has_suffix (subdir, "#1"))
61         {
62           new_name = g_strndup (subdir, (strlen (subdir) - 2));
63         }
64       else if (g_str_has_suffix (subdir, "#32"))
65         {
66           gchar *tmp;
67           tmp = g_strndup (subdir, (strlen (subdir) - 3));
68           new_name = g_strdup_printf ("%s#yahoo", tmp);
69           g_free (tmp);
70         }
71       else
72         {
73           continue;
74         }
75
76       full_path = g_build_filename (dirname, subdir, NULL);
77       old_gfile = g_file_new_for_path (full_path);
78       g_free (full_path);
79
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);
83
84       if (!g_file_move (old_gfile, new_gfile, G_FILE_COPY_NONE,
85               NULL, NULL, NULL, &error))
86         {
87           DEBUG ("Failed to move file: %s", error->message);
88           g_clear_error (&error);
89         }
90       else
91         {
92           DEBUG ("Successfully migrated logs for %s", new_name);
93         }
94
95       g_free (new_name);
96       g_object_unref (old_gfile);
97       g_object_unref (new_gfile);
98     }
99
100   g_dir_close (dir);
101 }
102
103 /* This is copied from empathy-log-store-empathy.c (see #613437) */
104 static gchar *
105 log_store_account_to_dirname (TpAccount *account)
106 {
107   const gchar *name;
108
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);
112
113   return g_strdelimit (g_strdup (name), "/", '_');
114 }
115
116 static gchar *
117 get_log_dir_for_account (TpAccount *account)
118 {
119   gchar *basedir;
120   gchar *escaped;
121
122   escaped = log_store_account_to_dirname (account);
123
124   basedir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
125     PACKAGE_NAME, "logs", escaped, NULL);
126
127   g_free (escaped);
128
129   return basedir;
130 }
131
132
133 static void
134 migration_account_manager_prepared_cb (GObject *source_object,
135     GAsyncResult *result,
136     gpointer user_data)
137 {
138   TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
139   GError *error = NULL;
140   GList *accounts, *l;
141   GSettings *gsettings;
142
143   if (!tp_account_manager_prepare_finish (am, result, &error))
144     {
145       DEBUG ("Failed to prepare the account manager: %s", error->message);
146       g_error_free (error);
147       return;
148     }
149
150   accounts = tp_account_manager_get_valid_accounts (am);
151
152   for (l = accounts; l != NULL; l = l->next)
153     {
154       TpAccount *account = TP_ACCOUNT (l->data);
155       gchar *dir, *cm;
156
157       tp_account_parse_object_path (tp_proxy_get_object_path (account),
158           &cm, NULL, NULL, NULL);
159
160       if (tp_strdiff (cm, "butterfly"))
161         {
162           g_free (cm);
163           continue;
164         }
165
166       dir = get_log_dir_for_account (account);
167       DEBUG ("Migrating all logs from dir: %s", dir);
168
169       migrate_log_files_in_dir (dir);
170
171       g_free (cm);
172       g_free (dir);
173     }
174
175   DEBUG ("Finished all migrating");
176
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);
181
182   g_list_free (accounts);
183 }
184
185 static gboolean
186 migrate_logs (gpointer data)
187 {
188   TpAccountManager *account_manager;
189
190   account_manager = tp_account_manager_dup ();
191
192   tp_account_manager_prepare_async (account_manager, NULL,
193       migration_account_manager_prepared_cb, NULL);
194
195   g_object_unref (account_manager);
196
197   return FALSE;
198 }
199
200 gboolean
201 empathy_migrate_butterfly_logs (EmpathyContact *contact)
202 {
203   GSettings *gsettings = g_settings_new (EMPATHY_PREFS_SCHEMA);
204   gchar *cm;
205
206   /* Already in progress. */
207   if (butterfly_log_migration_id != 0)
208     return FALSE;
209
210   /* Already done. */
211   if (g_settings_get_boolean (gsettings, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED))
212     return FALSE;
213
214   tp_account_parse_object_path (
215       tp_proxy_get_object_path (empathy_contact_get_account (contact)),
216       &cm, NULL, NULL, NULL);
217
218   if (tp_strdiff (cm, "butterfly"))
219     {
220       g_free (cm);
221       return TRUE;
222     }
223   g_free (cm);
224
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;
228
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);
232
233   return FALSE;
234 }