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