]> git.0d.be Git - empathy.git/blob - src/empathy-sanity-cleaning.c
5b366ddf0fea631c9487d537e7661e93346e2258
[empathy.git] / src / empathy-sanity-cleaning.c
1 /*
2  * empathy-sanity-cleaning.c
3  * Code automatically called when starting a specific version of Empathy for
4  * the first time doing misc cleaning.
5  *
6  * Copyright (C) 2012 Collabora Ltd.
7  * @author Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25
26 #include "empathy-sanity-cleaning.h"
27
28 #include <telepathy-glib/telepathy-glib.h>
29
30 #include <libempathy/empathy-gsettings.h>
31
32 #include <libempathy-gtk/empathy-theme-manager.h>
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
35 #include <libempathy/empathy-debug.h>
36
37 /*
38  * This number has to be increased each time a new task is added or modified.
39  *
40  * If the number stored in gsettings is lower than it, all the tasks will
41  * be executed.
42  */
43 #define SANITY_CLEANING_NUMBER 3
44
45 static void
46 account_update_parameters_cb (GObject *source,
47     GAsyncResult *result,
48     gpointer user_data)
49 {
50   GError *error = NULL;
51   TpAccount *account = TP_ACCOUNT (source);
52
53   if (!tp_account_update_parameters_finish (account, result, NULL, &error))
54     {
55       DEBUG ("Failed to update parameters of account '%s': %s",
56           tp_account_get_path_suffix (account), error->message);
57
58       g_error_free (error);
59       return;
60     }
61
62   tp_account_reconnect_async (account, NULL, NULL);
63 }
64
65 /* Make sure XMPP accounts don't have a negative priority (bgo #671452) */
66 static void
67 fix_xmpp_account_priority (TpAccountManager *am)
68 {
69   GList *accounts, *l;
70
71   accounts = tp_account_manager_get_valid_accounts (am);
72   for (l = accounts; l != NULL; l = g_list_next (l))
73     {
74       TpAccount *account = l->data;
75       GHashTable *params;
76       gint priority;
77
78       if (tp_strdiff (tp_account_get_protocol (account), "jabber"))
79         continue;
80
81       params = (GHashTable *) tp_account_get_parameters (account);
82       if (params == NULL)
83         continue;
84
85       priority = tp_asv_get_int32 (params, "priority", NULL);
86       if (priority >= 0)
87         continue;
88
89       DEBUG ("Resetting XMPP priority of account '%s' to 0",
90           tp_account_get_path_suffix (account));
91
92       params = tp_asv_new (
93           "priority", G_TYPE_INT, 0,
94           NULL);
95
96       tp_account_update_parameters_async (account, params, NULL,
97           account_update_parameters_cb, NULL);
98
99       g_hash_table_unref (params);
100     }
101
102   g_list_free (accounts);
103 }
104
105 static void
106 set_facebook_account_fallback_server (TpAccountManager *am)
107 {
108   GList *accounts, *l;
109
110   accounts = tp_account_manager_get_valid_accounts (am);
111   for (l = accounts; l != NULL; l = g_list_next (l))
112     {
113       TpAccount *account = l->data;
114       GHashTable *params;
115       gchar *fallback_servers[] = {
116           "chat.facebook.com:443",
117           NULL };
118
119       if (tp_strdiff (tp_account_get_service (account), "facebook"))
120         continue;
121
122       params = (GHashTable *) tp_account_get_parameters (account);
123       if (params == NULL)
124         continue;
125
126       if (tp_asv_get_strv (params, "fallback-servers") != NULL)
127         continue;
128
129       DEBUG ("Setting chat.facebook.com:443 as a fallback on account '%s'",
130           tp_account_get_path_suffix (account));
131
132       params = tp_asv_new (
133           "fallback-servers", G_TYPE_STRV, fallback_servers,
134           NULL);
135
136       tp_account_update_parameters_async (account, params, NULL,
137           account_update_parameters_cb, NULL);
138
139       g_hash_table_unref (params);
140     }
141
142   g_list_free (accounts);
143 }
144
145 static void
146 upgrade_chat_theme_settings (void)
147 {
148   GSettings *gsettings_chat;
149   gchar *theme, *new_theme = NULL;
150   const char *variant = "";
151
152   gsettings_chat = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
153
154   theme = g_settings_get_string (gsettings_chat,
155       EMPATHY_PREFS_CHAT_THEME);
156
157   if (!tp_strdiff (theme, "adium")) {
158     gchar *path;
159
160     path = g_settings_get_string (gsettings_chat,
161         EMPATHY_PREFS_CHAT_ADIUM_PATH);
162
163     new_theme = empathy_theme_manager_dup_theme_name_from_path (path);
164     if (new_theme == NULL)
165       {
166         /* Use the Classic theme as fallback */
167         new_theme = g_strdup ("Classic");
168       }
169
170     g_free (path);
171   } else if (!tp_strdiff (theme, "gnome")) {
172     new_theme = g_strdup ("PlanetGNOME");
173   } else if (!tp_strdiff (theme, "simple")) {
174     new_theme = g_strdup ("Boxes");
175     variant = "Simple";
176   } else if (!tp_strdiff (theme, "clean")) {
177     new_theme = g_strdup ("Boxes");
178     variant = "Clean";
179   } else if (!tp_strdiff (theme, "blue")) {
180     new_theme = g_strdup ("Boxes");
181     variant = "Blue";
182   } else {
183     /* Assume that's an Adium theme name. The theme manager will fallback to
184      * 'Classic' if it can't find it. */
185     goto finally;
186   }
187
188   DEBUG ("Migrating to '%s' variant '%s'", new_theme, variant);
189
190   g_settings_set_string (gsettings_chat,
191     EMPATHY_PREFS_CHAT_THEME, new_theme);
192   g_settings_set_string (gsettings_chat,
193     EMPATHY_PREFS_CHAT_THEME_VARIANT, variant);
194
195 finally:
196   g_free (theme);
197   g_free (new_theme);
198   g_object_unref (gsettings_chat);
199 }
200
201 static void
202 run_sanity_cleaning_tasks (TpAccountManager *am)
203 {
204   DEBUG ("Starting sanity cleaning tasks");
205
206   fix_xmpp_account_priority (am);
207   set_facebook_account_fallback_server (am);
208   upgrade_chat_theme_settings ();
209 }
210
211 static void
212 am_prepare_cb (GObject *source,
213     GAsyncResult *result,
214     gpointer user_data)
215 {
216   GError *error = NULL;
217   TpAccountManager *am = TP_ACCOUNT_MANAGER (source);
218
219   if (!tp_proxy_prepare_finish (am, result, &error))
220     {
221       DEBUG ("Failed to prepare account manager: %s", error->message);
222       g_error_free (error);
223       return;
224     }
225
226   run_sanity_cleaning_tasks (am);
227 }
228
229 void empathy_sanity_checking_run_if_needed (void)
230 {
231   GSettings *settings;
232   guint number;
233   TpAccountManager *am;
234
235   settings = g_settings_new (EMPATHY_PREFS_SCHEMA);
236   number = g_settings_get_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER);
237
238   if (number == SANITY_CLEANING_NUMBER)
239     goto out;
240
241   am = tp_account_manager_dup ();
242
243   tp_proxy_prepare_async (am, NULL, am_prepare_cb, NULL);
244
245   g_settings_set_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER,
246       SANITY_CLEANING_NUMBER);
247
248   g_object_unref (am);
249 out:
250   g_object_unref (settings);
251 }