2 * empathy-sanity-cleaning.c
3 * Code automatically called when starting a specific version of Empathy for
4 * the first time doing misc cleaning.
6 * Copyright (C) 2012 Collabora Ltd.
7 * @author Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "empathy-sanity-cleaning.h"
28 #include "libempathy/empathy-gsettings.h"
30 #include "libempathy-gtk/empathy-theme-manager.h"
33 #include "libempathy/empathy-pkg-kit.h"
34 #include "libempathy/empathy-uoa-utils.h"
36 #include <libaccounts-glib/ag-account-service.h>
37 #include <libaccounts-glib/ag-manager.h>
38 #include <libaccounts-glib/ag-service.h>
41 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
42 #include "libempathy/empathy-debug.h"
45 * This number has to be increased each time a new task is added or modified.
47 * If the number stored in gsettings is lower than it, all the tasks will
50 #define SANITY_CLEANING_NUMBER 4
55 GSimpleAsyncResult *result;
61 sanity_ctx_new (TpAccountManager *am,
62 GSimpleAsyncResult *result)
64 SanityCtx *ctx = g_slice_new0 (SanityCtx);
66 ctx->am = g_object_ref (am);
67 ctx->result = g_object_ref (result);
75 sanity_ctx_ref (SanityCtx *ctx)
84 sanity_ctx_unref (SanityCtx *ctx)
88 if (ctx->ref_count != 0)
91 g_simple_async_result_complete_in_idle (ctx->result);
93 g_object_unref (ctx->am);
94 g_object_unref (ctx->result);
96 g_slice_free (SanityCtx, ctx);
100 account_update_parameters_cb (GObject *source,
101 GAsyncResult *result,
104 GError *error = NULL;
105 TpAccount *account = TP_ACCOUNT (source);
107 if (!tp_account_update_parameters_finish (account, result, NULL, &error))
109 DEBUG ("Failed to update parameters of account '%s': %s",
110 tp_account_get_path_suffix (account), error->message);
112 g_error_free (error);
116 tp_account_reconnect_async (account, NULL, NULL);
119 /* Make sure XMPP accounts don't have a negative priority (bgo #671452) */
121 fix_xmpp_account_priority (TpAccountManager *am)
125 accounts = tp_account_manager_dup_valid_accounts (am);
126 for (l = accounts; l != NULL; l = g_list_next (l))
128 TpAccount *account = l->data;
132 if (tp_strdiff (tp_account_get_protocol_name (account), "jabber"))
135 params = (GHashTable *) tp_account_get_parameters (account);
139 priority = tp_asv_get_int32 (params, "priority", NULL);
143 DEBUG ("Resetting XMPP priority of account '%s' to 0",
144 tp_account_get_path_suffix (account));
146 params = tp_asv_new (
147 "priority", G_TYPE_INT, 0,
150 tp_account_update_parameters_async (account, params, NULL,
151 account_update_parameters_cb, NULL);
153 g_hash_table_unref (params);
156 g_list_free_full (accounts, g_object_unref);
160 set_facebook_account_fallback_server (TpAccountManager *am)
164 accounts = tp_account_manager_dup_valid_accounts (am);
165 for (l = accounts; l != NULL; l = g_list_next (l))
167 TpAccount *account = l->data;
169 gchar *fallback_servers[] = {
170 "chat.facebook.com:443",
173 if (tp_strdiff (tp_account_get_service (account), "facebook"))
176 params = (GHashTable *) tp_account_get_parameters (account);
180 if (tp_asv_get_strv (params, "fallback-servers") != NULL)
183 DEBUG ("Setting chat.facebook.com:443 as a fallback on account '%s'",
184 tp_account_get_path_suffix (account));
186 params = tp_asv_new (
187 "fallback-servers", G_TYPE_STRV, fallback_servers,
190 tp_account_update_parameters_async (account, params, NULL,
191 account_update_parameters_cb, NULL);
193 g_hash_table_unref (params);
196 g_list_free_full (accounts, g_object_unref);
200 upgrade_chat_theme_settings (void)
202 GSettings *gsettings_chat;
203 gchar *theme, *new_theme = NULL;
204 const char *variant = "";
206 gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
208 theme = g_settings_get_string (gsettings_chat,
209 EMPATHY_PREFS_CHAT_THEME);
211 if (!tp_strdiff (theme, "adium")) {
214 path = g_settings_get_string (gsettings_chat,
215 EMPATHY_PREFS_CHAT_ADIUM_PATH);
217 new_theme = empathy_theme_manager_dup_theme_name_from_path (path);
218 if (new_theme == NULL)
220 /* Use the Classic theme as fallback */
221 new_theme = g_strdup ("Classic");
225 } else if (!tp_strdiff (theme, "gnome")) {
226 new_theme = g_strdup ("PlanetGNOME");
227 } else if (!tp_strdiff (theme, "simple")) {
228 new_theme = g_strdup ("Boxes");
230 } else if (!tp_strdiff (theme, "clean")) {
231 new_theme = g_strdup ("Boxes");
233 } else if (!tp_strdiff (theme, "blue")) {
234 new_theme = g_strdup ("Boxes");
237 /* Assume that's an Adium theme name. The theme manager will fallback to
238 * 'Classic' if it can't find it. */
242 DEBUG ("Migrating to '%s' variant '%s'", new_theme, variant);
244 g_settings_set_string (gsettings_chat,
245 EMPATHY_PREFS_CHAT_THEME, new_theme);
246 g_settings_set_string (gsettings_chat,
247 EMPATHY_PREFS_CHAT_THEME_VARIANT, variant);
252 g_object_unref (gsettings_chat);
258 TpAccount *new_account;
259 TpAccount *old_account;
263 static UoaMigrationData *
264 uoa_migration_data_new (TpAccount *account)
266 UoaMigrationData *data;
268 data = g_slice_new0 (UoaMigrationData);
269 data->old_account = g_object_ref (account);
270 data->enabled = tp_account_is_enabled (account);
276 uoa_migration_data_free (UoaMigrationData *data)
278 g_clear_object (&data->new_account);
279 g_clear_object (&data->old_account);
280 g_slice_free (UoaMigrationData, data);
283 #define DATA_SANITY_CTX "data-sanity-ctx"
286 uoa_account_remove_cb (GObject *source,
287 GAsyncResult *result,
290 TpAccount *account = TP_ACCOUNT (source);
291 GError *error = NULL;
293 if (!tp_account_remove_finish (account, result, &error))
295 DEBUG ("Failed to remove account '%s': %s",
296 tp_account_get_path_suffix (account), error->message);
297 g_error_free (error);
300 g_object_set_data (G_OBJECT (account), DATA_SANITY_CTX, NULL);
304 uoa_migration_done (UoaMigrationData *data)
306 tp_account_remove_async (data->old_account, uoa_account_remove_cb, NULL);
308 if (data->new_account != NULL)
309 tp_account_set_enabled_async (data->new_account, data->enabled, NULL, NULL);
311 uoa_migration_data_free (data);
315 uoa_set_account_password_cb (GObject *source,
316 GAsyncResult *result,
319 UoaMigrationData *data = user_data;
320 GError *error = NULL;
322 if (!empathy_keyring_set_account_password_finish (data->new_account, result,
325 DEBUG ("Error setting old account's password on the new one: %s\n",
327 g_clear_error (&error);
330 uoa_migration_done (data);
334 uoa_get_account_password_cb (GObject *source,
335 GAsyncResult *result,
338 UoaMigrationData *data = user_data;
339 const gchar *password;
340 GError *error = NULL;
342 password = empathy_keyring_get_account_password_finish (data->old_account,
344 if (password == NULL)
346 DEBUG ("Error getting old account's password: %s\n", error->message);
347 g_clear_error (&error);
349 uoa_migration_done (data);
353 empathy_keyring_set_account_password_async (data->new_account, password,
354 TRUE, uoa_set_account_password_cb, data);
359 uoa_account_created_cb (GObject *source,
360 GAsyncResult *result,
363 TpAccountRequest *ar = (TpAccountRequest *) source;
364 UoaMigrationData *data = user_data;
365 GError *error = NULL;
367 data->new_account = tp_account_request_create_account_finish (ar, result,
369 if (data->new_account == NULL)
371 DEBUG ("Failed to migrate account '%s' to UOA: %s",
372 tp_account_get_path_suffix (data->old_account), error->message);
373 g_clear_error (&error);
375 uoa_migration_done (data);
379 DEBUG ("New account %s created to superseed %s",
380 tp_account_get_path_suffix (data->new_account),
381 tp_account_get_path_suffix (data->old_account));
383 /* Migrate password as well */
384 empathy_keyring_get_account_password_async (data->old_account,
385 uoa_get_account_password_cb, data);
390 migrate_account_to_uoa (TpAccountManager *am,
393 TpAccountRequest *ar;
397 const gchar * const *supersedes;
399 UoaMigrationData *data;
401 DEBUG ("Migrating account %s to UOA storage\n",
402 tp_account_get_path_suffix (account));
404 ar = tp_account_request_new (am,
405 tp_account_get_cm_name (account),
406 tp_account_get_protocol_name (account),
407 tp_account_get_display_name (account));
408 tp_account_request_set_storage_provider (ar, EMPATHY_UOA_PROVIDER);
409 tp_account_request_set_icon_name (ar,
410 tp_account_get_icon_name (account));
411 tp_account_request_set_nickname (ar,
412 tp_account_get_nickname (account));
413 tp_account_request_set_service (ar,
414 tp_account_get_service (account));
416 /* Do not enable the new account until we imported the password as well */
417 tp_account_request_set_enabled (ar, FALSE);
419 supersedes = tp_account_get_supersedes (account);
421 for (i = 0; supersedes[i] != NULL; i++)
422 tp_account_request_add_supersedes (ar, supersedes[i]);
424 tp_account_request_add_supersedes (ar,
425 tp_proxy_get_object_path (account));
427 params = tp_account_dup_parameters_vardict (account);
428 g_variant_iter_init (&iter, params);
429 while ((param = g_variant_iter_next_value (&iter)))
434 k = g_variant_get_child_value (param, 0);
435 key = g_variant_get_string (k, NULL);
436 v = g_variant_get_child_value (param, 1);
438 tp_account_request_set_parameter (ar, key,
439 g_variant_get_variant (v));
445 data = uoa_migration_data_new (account);
446 tp_account_set_enabled_async (account, FALSE, NULL, NULL);
447 tp_account_request_create_account_async (ar, uoa_account_created_cb,
450 g_variant_unref (params);
455 uoa_plugin_install_cb (GObject *source,
456 GAsyncResult *result,
459 TpAccount *account = user_data;
460 GError *error = NULL;
461 TpAccountManager *am;
463 if (!empathy_pkg_kit_install_packages_finish (result, &error))
465 DEBUG ("Failed to install plugin for account '%s' (%s); remove it",
466 tp_account_get_path_suffix (account), error->message);
468 g_error_free (error);
470 tp_account_remove_async (account, uoa_account_remove_cb, NULL);
474 DEBUG ("Plugin for account '%s' has been installed; migrate account",
475 tp_account_get_path_suffix (account));
477 am = tp_account_manager_dup ();
478 migrate_account_to_uoa (am, account);
482 g_object_unref (account);
486 dup_plugin_name_for_protocol (const gchar *protocol)
488 if (!tp_strdiff (protocol, "local-xmpp"))
489 return g_strdup ("account-plugin-salut");
491 return g_strdup_printf ("account-plugin-%s", protocol);
495 uoa_plugin_installed (AgManager *manager,
498 AgAccount *ag_account;
499 const gchar *protocol;
502 protocol = tp_account_get_protocol_name (account);
503 ag_account = ag_manager_create_account (manager, protocol);
505 l = ag_account_list_services_by_type (ag_account, EMPATHY_UOA_SERVICE_TYPE);
508 const gchar *packages[2];
511 pkg = dup_plugin_name_for_protocol (protocol);
513 DEBUG ("%s is not installed; try to install it", pkg);
518 empathy_pkg_kit_install_packages_async (0, packages, NULL,
519 NULL, uoa_plugin_install_cb, g_object_ref (account));
522 g_object_unref (ag_account);
526 ag_service_list_free (l);
528 g_object_unref (ag_account);
533 migrate_accounts_to_uoa (SanityCtx *ctx)
538 DEBUG ("Start migrating accounts to UOA");
540 manager = empathy_uoa_manager_dup ();
542 accounts = tp_account_manager_dup_valid_accounts (ctx->am);
543 for (l = accounts; l != NULL; l = g_list_next (l))
545 TpAccount *account = l->data;
547 /* If account is already in a specific storage (like UOA or GOA),
549 * Note that we cannot migrate GOA accounts anyway, since we can't delete
550 * them it would create duplicated accounts. */
551 if (!tp_str_empty (tp_account_get_storage_provider (account)))
554 g_object_set_data_full (G_OBJECT (account), DATA_SANITY_CTX,
555 sanity_ctx_ref (ctx), (GDestroyNotify) sanity_ctx_unref);
557 /* Try to install the plugin if it's missing */
558 if (!uoa_plugin_installed (manager, account))
561 migrate_account_to_uoa (ctx->am, account);
564 g_list_free_full (accounts, g_object_unref);
566 g_object_unref (manager);
571 run_sanity_cleaning_tasks (SanityCtx *ctx)
573 DEBUG ("Starting sanity cleaning tasks");
575 fix_xmpp_account_priority (ctx->am);
576 set_facebook_account_fallback_server (ctx->am);
577 upgrade_chat_theme_settings ();
579 migrate_accounts_to_uoa (ctx);
584 am_prepare_cb (GObject *source,
585 GAsyncResult *result,
588 GError *error = NULL;
589 TpAccountManager *am = TP_ACCOUNT_MANAGER (source);
590 SanityCtx *ctx = user_data;
592 if (!tp_proxy_prepare_finish (am, result, &error))
594 DEBUG ("Failed to prepare account manager: %s", error->message);
595 g_simple_async_result_take_error (ctx->result, error);
599 run_sanity_cleaning_tasks (ctx);
602 sanity_ctx_unref (ctx);
606 empathy_sanity_checking_run_async (GAsyncReadyCallback callback,
611 TpAccountManager *am;
612 GSimpleAsyncResult *result;
615 result = g_simple_async_result_new (NULL, callback, user_data,
616 empathy_sanity_checking_run_async);
618 settings = g_settings_new (EMPATHY_PREFS_SCHEMA);
619 number = g_settings_get_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER);
621 if (number == SANITY_CLEANING_NUMBER)
623 g_simple_async_result_complete_in_idle (result);
627 am = tp_account_manager_dup ();
629 ctx = sanity_ctx_new (am, result);
630 tp_proxy_prepare_async (am, NULL, am_prepare_cb, ctx);
632 g_settings_set_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER,
633 SANITY_CLEANING_NUMBER);
638 g_object_unref (settings);
639 g_object_unref (result);
643 empathy_sanity_checking_run_finish (GAsyncResult *result,
646 g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
647 empathy_sanity_checking_run_async), FALSE);
649 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),