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>
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;
36 migrate_log_files_in_dir (const gchar
*dirname
)
44 dir
= g_dir_open (dirname
, 0, &error
);
48 DEBUG ("Failed to open dir: %s", error
->message
);
53 while ((subdir
= g_dir_read_name (dir
)) != NULL
)
55 GFile
*old_gfile
, *new_gfile
;
57 if (!tp_strdiff (subdir
, "chatrooms"))
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"))
67 tmp
= g_strndup (subdir
, (strlen (subdir
) - 3));
68 new_name
= g_strdup_printf ("%s#yahoo", tmp
);
76 full_path
= g_build_filename (dirname
, subdir
, NULL
);
77 old_gfile
= g_file_new_for_path (full_path
);
80 full_path
= g_build_filename (dirname
, new_name
, NULL
);
81 new_gfile
= g_file_new_for_path (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
);
92 DEBUG ("Successfully migrated logs for %s", new_name
);
96 g_object_unref (old_gfile
);
97 g_object_unref (new_gfile
);
103 /* This is copied from empathy-log-store-empathy.c (see #613437) */
105 log_store_account_to_dirname (TpAccount
*account
)
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
), "/", '_');
117 get_log_dir_for_account (TpAccount
*account
)
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
);
134 migration_account_manager_prepared_cb (GObject
*source_object
,
135 GAsyncResult
*result
,
138 TpAccountManager
*am
= TP_ACCOUNT_MANAGER (source_object
);
139 GError
*error
= NULL
;
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
);
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
);
157 tp_account_parse_object_path (tp_proxy_get_object_path (account
),
158 &cm
, NULL
, NULL
, NULL
);
160 if (tp_strdiff (cm
, "butterfly"))
166 dir
= get_log_dir_for_account (account
);
167 DEBUG ("Migrating all logs from dir: %s", dir
);
169 migrate_log_files_in_dir (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
,
180 g_object_unref (gsettings
);
182 g_list_free (accounts
);
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
);
201 empathy_migrate_butterfly_logs (EmpathyContact
*contact
)
203 GSettings
*gsettings
= g_settings_new (EMPATHY_PREFS_SCHEMA
);
206 /* Already in progress. */
207 if (butterfly_log_migration_id
!= 0)
211 if (g_settings_get_boolean (gsettings
, EMPATHY_PREFS_BUTTERFLY_LOGS_MIGRATED
))
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"))
225 if (g_str_has_suffix (empathy_contact_get_id (contact
), "#32")
226 || g_str_has_suffix (empathy_contact_get_id (contact
), "#1"))
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
);