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 <telepathy-glib/telepathy-glib.h>
30 #include <libempathy/empathy-account-settings.h>
31 #include <libempathy/empathy-gsettings.h>
33 #include <libempathy-gtk/empathy-theme-manager.h>
36 #include <libempathy/empathy-pkg-kit.h>
37 #include <libempathy/empathy-uoa-utils.h>
39 #include <libaccounts-glib/ag-account-service.h>
40 #include <libaccounts-glib/ag-manager.h>
41 #include <libaccounts-glib/ag-service.h>
44 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
45 #include <libempathy/empathy-debug.h>
46 #include <libempathy/empathy-keyring.h>
49 * This number has to be increased each time a new task is added or modified.
51 * If the number stored in gsettings is lower than it, all the tasks will
54 #define SANITY_CLEANING_NUMBER 4
59 GSimpleAsyncResult *result;
65 sanity_ctx_new (TpAccountManager *am,
66 GSimpleAsyncResult *result)
68 SanityCtx *ctx = g_slice_new0 (SanityCtx);
70 ctx->am = g_object_ref (am);
71 ctx->result = g_object_ref (result);
79 sanity_ctx_ref (SanityCtx *ctx)
88 sanity_ctx_unref (SanityCtx *ctx)
92 if (ctx->ref_count != 0)
95 g_simple_async_result_complete_in_idle (ctx->result);
97 g_object_unref (ctx->am);
98 g_object_unref (ctx->result);
100 g_slice_free (SanityCtx, ctx);
104 account_update_parameters_cb (GObject *source,
105 GAsyncResult *result,
108 GError *error = NULL;
109 TpAccount *account = TP_ACCOUNT (source);
111 if (!tp_account_update_parameters_finish (account, result, NULL, &error))
113 DEBUG ("Failed to update parameters of account '%s': %s",
114 tp_account_get_path_suffix (account), error->message);
116 g_error_free (error);
120 tp_account_reconnect_async (account, NULL, NULL);
123 /* Make sure XMPP accounts don't have a negative priority (bgo #671452) */
125 fix_xmpp_account_priority (TpAccountManager *am)
129 accounts = tp_account_manager_dup_valid_accounts (am);
130 for (l = accounts; l != NULL; l = g_list_next (l))
132 TpAccount *account = l->data;
136 if (tp_strdiff (tp_account_get_protocol_name (account), "jabber"))
139 params = (GHashTable *) tp_account_get_parameters (account);
143 priority = tp_asv_get_int32 (params, "priority", NULL);
147 DEBUG ("Resetting XMPP priority of account '%s' to 0",
148 tp_account_get_path_suffix (account));
150 params = tp_asv_new (
151 "priority", G_TYPE_INT, 0,
154 tp_account_update_parameters_async (account, params, NULL,
155 account_update_parameters_cb, NULL);
157 g_hash_table_unref (params);
160 g_list_free_full (accounts, g_object_unref);
164 set_facebook_account_fallback_server (TpAccountManager *am)
168 accounts = tp_account_manager_dup_valid_accounts (am);
169 for (l = accounts; l != NULL; l = g_list_next (l))
171 TpAccount *account = l->data;
173 gchar *fallback_servers[] = {
174 "chat.facebook.com:443",
177 if (tp_strdiff (tp_account_get_service (account), "facebook"))
180 params = (GHashTable *) tp_account_get_parameters (account);
184 if (tp_asv_get_strv (params, "fallback-servers") != NULL)
187 DEBUG ("Setting chat.facebook.com:443 as a fallback on account '%s'",
188 tp_account_get_path_suffix (account));
190 params = tp_asv_new (
191 "fallback-servers", G_TYPE_STRV, fallback_servers,
194 tp_account_update_parameters_async (account, params, NULL,
195 account_update_parameters_cb, NULL);
197 g_hash_table_unref (params);
200 g_list_free_full (accounts, g_object_unref);
204 upgrade_chat_theme_settings (void)
206 GSettings *gsettings_chat;
207 gchar *theme, *new_theme = NULL;
208 const char *variant = "";
210 gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
212 theme = g_settings_get_string (gsettings_chat,
213 EMPATHY_PREFS_CHAT_THEME);
215 if (!tp_strdiff (theme, "adium")) {
218 path = g_settings_get_string (gsettings_chat,
219 EMPATHY_PREFS_CHAT_ADIUM_PATH);
221 new_theme = empathy_theme_manager_dup_theme_name_from_path (path);
222 if (new_theme == NULL)
224 /* Use the Classic theme as fallback */
225 new_theme = g_strdup ("Classic");
229 } else if (!tp_strdiff (theme, "gnome")) {
230 new_theme = g_strdup ("PlanetGNOME");
231 } else if (!tp_strdiff (theme, "simple")) {
232 new_theme = g_strdup ("Boxes");
234 } else if (!tp_strdiff (theme, "clean")) {
235 new_theme = g_strdup ("Boxes");
237 } else if (!tp_strdiff (theme, "blue")) {
238 new_theme = g_strdup ("Boxes");
241 /* Assume that's an Adium theme name. The theme manager will fallback to
242 * 'Classic' if it can't find it. */
246 DEBUG ("Migrating to '%s' variant '%s'", new_theme, variant);
248 g_settings_set_string (gsettings_chat,
249 EMPATHY_PREFS_CHAT_THEME, new_theme);
250 g_settings_set_string (gsettings_chat,
251 EMPATHY_PREFS_CHAT_THEME_VARIANT, variant);
256 g_object_unref (gsettings_chat);
262 TpAccount *new_account;
263 TpAccount *old_account;
267 static UoaMigrationData *
268 uoa_migration_data_new (TpAccount *account)
270 UoaMigrationData *data;
272 data = g_slice_new0 (UoaMigrationData);
273 data->old_account = g_object_ref (account);
274 data->enabled = tp_account_is_enabled (account);
280 uoa_migration_data_free (UoaMigrationData *data)
282 g_clear_object (&data->new_account);
283 g_clear_object (&data->old_account);
284 g_slice_free (UoaMigrationData, data);
287 #define DATA_SANITY_CTX "data-sanity-ctx"
290 uoa_account_remove_cb (GObject *source,
291 GAsyncResult *result,
294 TpAccount *account = TP_ACCOUNT (source);
295 GError *error = NULL;
297 if (!tp_account_remove_finish (account, result, &error))
299 DEBUG ("Failed to remove account '%s': %s",
300 tp_account_get_path_suffix (account), error->message);
301 g_error_free (error);
304 g_object_set_data (G_OBJECT (account), DATA_SANITY_CTX, NULL);
308 uoa_migration_done (UoaMigrationData *data)
310 tp_account_remove_async (data->old_account, uoa_account_remove_cb, NULL);
312 if (data->new_account != NULL)
313 tp_account_set_enabled_async (data->new_account, data->enabled, NULL, NULL);
315 uoa_migration_data_free (data);
319 uoa_set_account_password_cb (GObject *source,
320 GAsyncResult *result,
323 UoaMigrationData *data = user_data;
324 GError *error = NULL;
326 if (!empathy_keyring_set_account_password_finish (data->new_account, result,
329 DEBUG ("Error setting old account's password on the new one: %s\n",
331 g_clear_error (&error);
334 uoa_migration_done (data);
338 uoa_get_account_password_cb (GObject *source,
339 GAsyncResult *result,
342 UoaMigrationData *data = user_data;
343 const gchar *password;
344 GError *error = NULL;
346 password = empathy_keyring_get_account_password_finish (data->old_account,
348 if (password == NULL)
350 DEBUG ("Error getting old account's password: %s\n", error->message);
351 g_clear_error (&error);
353 uoa_migration_done (data);
357 empathy_keyring_set_account_password_async (data->new_account, password,
358 TRUE, uoa_set_account_password_cb, data);
363 uoa_account_created_cb (GObject *source,
364 GAsyncResult *result,
367 TpAccountRequest *ar = (TpAccountRequest *) source;
368 UoaMigrationData *data = user_data;
369 GError *error = NULL;
371 data->new_account = tp_account_request_create_account_finish (ar, result,
373 if (data->new_account == NULL)
375 DEBUG ("Failed to migrate account '%s' to UOA: %s",
376 tp_account_get_path_suffix (data->old_account), error->message);
377 g_clear_error (&error);
379 uoa_migration_done (data);
383 DEBUG ("New account %s created to superseed %s",
384 tp_account_get_path_suffix (data->new_account),
385 tp_account_get_path_suffix (data->old_account));
387 /* Migrate password as well */
388 empathy_keyring_get_account_password_async (data->old_account,
389 uoa_get_account_password_cb, data);
394 migrate_account_to_uoa (TpAccountManager *am,
397 TpAccountRequest *ar;
401 const gchar * const *supersedes;
402 UoaMigrationData *data;
404 DEBUG ("Migrating account %s to UOA storage\n",
405 tp_account_get_path_suffix (account));
407 ar = tp_account_request_new (am,
408 tp_account_get_cm_name (account),
409 tp_account_get_protocol_name (account),
410 tp_account_get_display_name (account));
411 tp_account_request_set_storage_provider (ar, EMPATHY_UOA_PROVIDER);
412 tp_account_request_set_icon_name (ar,
413 tp_account_get_icon_name (account));
414 tp_account_request_set_nickname (ar,
415 tp_account_get_nickname (account));
416 tp_account_request_set_service (ar,
417 tp_account_get_service (account));
419 /* Do not enable the new account until we imported the password as well */
420 tp_account_request_set_enabled (ar, FALSE);
422 supersedes = tp_account_get_supersedes (account);
423 while (*supersedes != NULL)
424 tp_account_request_add_supersedes (ar, *supersedes);
425 tp_account_request_add_supersedes (ar,
426 tp_proxy_get_object_path (account));
428 params = tp_account_dup_parameters_vardict (account);
429 g_variant_iter_init (&iter, params);
430 while ((param = g_variant_iter_next_value (&iter)))
435 k = g_variant_get_child_value (param, 0);
436 key = g_variant_get_string (k, NULL);
437 v = g_variant_get_child_value (param, 1);
439 tp_account_request_set_parameter (ar, key,
440 g_variant_get_variant (v));
446 data = uoa_migration_data_new (account);
447 tp_account_set_enabled_async (account, FALSE, NULL, NULL);
448 tp_account_request_create_account_async (ar, uoa_account_created_cb,
451 g_variant_unref (params);
456 uoa_plugin_install_cb (GObject *source,
457 GAsyncResult *result,
460 TpAccount *account = user_data;
461 GError *error = NULL;
462 TpAccountManager *am;
464 if (!empathy_pkg_kit_install_packages_finish (result, &error))
466 DEBUG ("Failed to install plugin for account '%s' (%s); remove it",
467 tp_account_get_path_suffix (account), error->message);
469 g_error_free (error);
471 tp_account_remove_async (account, uoa_account_remove_cb, NULL);
475 DEBUG ("Plugin for account '%s' has been installed; migrate account",
476 tp_account_get_path_suffix (account));
478 am = tp_account_manager_dup ();
479 migrate_account_to_uoa (am, account);
483 g_object_unref (account);
487 dup_plugin_name_for_protocol (const gchar *protocol)
489 if (!tp_strdiff (protocol, "local-xmpp"))
490 return g_strdup ("account-plugin-salut");
492 return g_strdup_printf ("account-plugin-%s", protocol);
496 uoa_plugin_installed (AgManager *manager,
499 AgAccount *ag_account;
500 const gchar *protocol;
503 protocol = tp_account_get_protocol_name (account);
504 ag_account = ag_manager_create_account (manager, protocol);
506 l = ag_account_list_services_by_type (ag_account, EMPATHY_UOA_SERVICE_TYPE);
509 const gchar *packages[2];
512 pkg = dup_plugin_name_for_protocol (protocol);
514 DEBUG ("%s is not installed; try to install it", pkg);
519 empathy_pkg_kit_install_packages_async (0, packages, NULL,
520 NULL, uoa_plugin_install_cb, g_object_ref (account));
523 g_object_unref (ag_account);
527 ag_service_list_free (l);
529 g_object_unref (ag_account);
534 migrate_accounts_to_uoa (SanityCtx *ctx)
539 DEBUG ("Start migrating accounts to UOA");
541 manager = empathy_uoa_manager_dup ();
543 accounts = tp_account_manager_dup_valid_accounts (ctx->am);
544 for (l = accounts; l != NULL; l = g_list_next (l))
546 TpAccount *account = l->data;
548 /* If account is already in a specific storage (like UOA or GOA),
550 * Note that we cannot migrate GOA accounts anyway, since we can't delete
551 * them it would create duplicated accounts. */
552 if (!tp_str_empty (tp_account_get_storage_provider (account)))
555 g_object_set_data_full (G_OBJECT (account), DATA_SANITY_CTX,
556 sanity_ctx_ref (ctx), (GDestroyNotify) sanity_ctx_unref);
558 /* Try to install the plugin if it's missing */
559 if (!uoa_plugin_installed (manager, account))
562 migrate_account_to_uoa (ctx->am, account);
565 g_list_free_full (accounts, g_object_unref);
567 g_object_unref (manager);
572 run_sanity_cleaning_tasks (SanityCtx *ctx)
574 DEBUG ("Starting sanity cleaning tasks");
576 fix_xmpp_account_priority (ctx->am);
577 set_facebook_account_fallback_server (ctx->am);
578 upgrade_chat_theme_settings ();
580 migrate_accounts_to_uoa (ctx);
585 am_prepare_cb (GObject *source,
586 GAsyncResult *result,
589 GError *error = NULL;
590 TpAccountManager *am = TP_ACCOUNT_MANAGER (source);
591 SanityCtx *ctx = user_data;
593 if (!tp_proxy_prepare_finish (am, result, &error))
595 DEBUG ("Failed to prepare account manager: %s", error->message);
596 g_simple_async_result_take_error (ctx->result, error);
600 run_sanity_cleaning_tasks (ctx);
603 sanity_ctx_unref (ctx);
607 empathy_sanity_checking_run_async (GAsyncReadyCallback callback,
612 TpAccountManager *am;
613 GSimpleAsyncResult *result;
616 result = g_simple_async_result_new (NULL, callback, user_data,
617 empathy_sanity_checking_run_async);
619 settings = g_settings_new (EMPATHY_PREFS_SCHEMA);
620 number = g_settings_get_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER);
622 if (number == SANITY_CLEANING_NUMBER)
624 g_simple_async_result_complete_in_idle (result);
628 am = tp_account_manager_dup ();
630 ctx = sanity_ctx_new (am, result);
631 tp_proxy_prepare_async (am, NULL, am_prepare_cb, ctx);
633 g_settings_set_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER,
634 SANITY_CLEANING_NUMBER);
639 g_object_unref (settings);
640 g_object_unref (result);
644 empathy_sanity_checking_run_finish (GAsyncResult *result,
647 g_return_val_if_fail (g_simple_async_result_is_valid (result, NULL,
648 empathy_sanity_checking_run_async), FALSE);
650 if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (result),