]> git.0d.be Git - empathy.git/blob - src/empathy-migrate-butterfly-logs.c
Updated Polish translation
[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-log-store-empathy.h>
26
27 #include <libempathy-gtk/empathy-conf.h>
28
29 #include <telepathy-glib/account-manager.h>
30 #include <telepathy-glib/util.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 static void
105 migration_account_manager_prepared_cb (GObject *source_object,
106     GAsyncResult *result,
107     gpointer user_data)
108 {
109   TpAccountManager *am = TP_ACCOUNT_MANAGER (source_object);
110   GError *error = NULL;
111   GList *accounts, *l;
112   EmpathyLogStoreEmpathy *log_store;
113   EmpathyConf *conf;
114
115   if (!tp_account_manager_prepare_finish (am, result, &error))
116     {
117       DEBUG ("Failed to prepare the account manager: %s", error->message);
118       g_error_free (error);
119       return;
120     }
121
122   log_store = g_object_new (EMPATHY_TYPE_LOG_STORE_EMPATHY, NULL);
123   accounts = tp_account_manager_get_valid_accounts (am);
124
125   for (l = accounts; l != NULL; l = l->next)
126     {
127       TpAccount *account = TP_ACCOUNT (l->data);
128       gchar *dir, *cm;
129
130       tp_account_parse_object_path (tp_proxy_get_object_path (account),
131           &cm, NULL, NULL, NULL);
132
133       if (tp_strdiff (cm, "butterfly"))
134         {
135           g_free (cm);
136           continue;
137         }
138
139       dir = empathy_log_store_empathy_get_dir (log_store, account);
140       DEBUG ("Migrating all logs from dir: %s", dir);
141
142       migrate_log_files_in_dir (dir);
143
144       g_free (cm);
145       g_free (dir);
146     }
147
148   DEBUG ("Finished all migrating");
149
150   conf = empathy_conf_get ();
151   empathy_conf_set_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED, TRUE);
152
153   g_list_free (accounts);
154   g_object_unref (log_store);
155 }
156
157 static gboolean
158 migrate_logs (gpointer data)
159 {
160   TpAccountManager *account_manager;
161
162   account_manager = tp_account_manager_dup ();
163
164   tp_account_manager_prepare_async (account_manager, NULL,
165       migration_account_manager_prepared_cb, NULL);
166
167   g_object_unref (account_manager);
168
169   return FALSE;
170 }
171
172 gboolean
173 empathy_migrate_butterfly_logs (EmpathyContact *contact)
174 {
175   EmpathyConf *conf;
176   gboolean logs_migrated;
177   gchar *cm;
178
179   conf = empathy_conf_get ();
180
181   /* Already in progress. */
182   if (butterfly_log_migration_id != 0)
183     return FALSE;
184
185   /* Already done. */
186   if (!empathy_conf_get_bool (conf, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED,
187           &logs_migrated))
188     return FALSE;
189
190   if (logs_migrated)
191     return FALSE;
192
193   tp_account_parse_object_path (
194       tp_proxy_get_object_path (empathy_contact_get_account (contact)),
195       &cm, NULL, NULL, NULL);
196
197   if (tp_strdiff (cm, "butterfly"))
198     {
199       g_free (cm);
200       return TRUE;
201     }
202   g_free (cm);
203
204   if (g_str_has_suffix (empathy_contact_get_id (contact), "#32")
205       || g_str_has_suffix (empathy_contact_get_id (contact), "#1"))
206     return TRUE;
207
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);
211
212   return FALSE;
213 }