]> git.0d.be Git - empathy.git/blob - src/empathy-sanity-cleaning.c
Updated Persian Translations
[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 2
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 run_sanity_cleaning_tasks (TpAccountManager *am)
145 {
146   DEBUG ("Starting sanity cleaning tasks");
147
148   fix_xmpp_account_priority (am);
149   set_facebook_account_fallback_server (am);
150 }
151
152 static void
153 am_prepare_cb (GObject *source,
154     GAsyncResult *result,
155     gpointer user_data)
156 {
157   GError *error = NULL;
158   TpAccountManager *am = TP_ACCOUNT_MANAGER (source);
159
160   if (!tp_proxy_prepare_finish (am, result, &error))
161     {
162       DEBUG ("Failed to prepare account manager: %s", error->message);
163       g_error_free (error);
164       return;
165     }
166
167   run_sanity_cleaning_tasks (am);
168 }
169
170 void empathy_sanity_checking_run_if_needed (void)
171 {
172   GSettings *settings;
173   guint number;
174   TpAccountManager *am;
175
176   settings = g_settings_new (EMPATHY_PREFS_SCHEMA);
177   number = g_settings_get_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER);
178
179   if (number == SANITY_CLEANING_NUMBER)
180     goto out;
181
182   am = tp_account_manager_dup ();
183
184   tp_proxy_prepare_async (am, NULL, am_prepare_cb, NULL);
185
186   g_settings_set_uint (settings, EMPATHY_PREFS_SANITY_CLEANING_NUMBER,
187       SANITY_CLEANING_NUMBER);
188
189   g_object_unref (am);
190 out:
191   g_object_unref (settings);
192 }