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