src/gtkconn.c

branch
gaim
changeset 20470
77693555855f
parent 13071
b98e72d4089a
parent 20469
b2836a24d81e
child 20471
1966704b3e42
equal deleted inserted replaced
13071:b98e72d4089a 20470:77693555855f
1 /*
2 * @file gtkconn.c GTK+ Connection API
3 * @ingroup gtkui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25 #include "internal.h"
26 #include "gtkgaim.h"
27
28 #include "account.h"
29 #include "debug.h"
30 #include "notify.h"
31 #include "prefs.h"
32 #include "gtkblist.h"
33 #include "gtkconn.h"
34 #include "gtkdialogs.h"
35 #include "gtkstatusbox.h"
36 #include "gtkstock.h"
37 #include "gtkutils.h"
38 #include "util.h"
39
40 #define INITIAL_RECON_DELAY 8000
41 #define MAX_RECON_DELAY 600000
42
43 typedef struct {
44 int delay;
45 guint timeout;
46 } GaimAutoRecon;
47
48 /**
49 * Contains accounts that are auto-reconnecting.
50 * The key is a pointer to the GaimAccount and the
51 * value is a pointer to a GaimAutoRecon.
52 */
53 static GHashTable *hash = NULL;
54 static GHashTable *errored_accounts = NULL;
55
56 static void
57 gaim_gtk_connection_connect_progress(GaimConnection *gc,
58 const char *text, size_t step, size_t step_count)
59 {
60 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
61 if (!gtkblist)
62 return;
63 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
64 (gaim_connections_get_connecting() != NULL));
65 gtk_gaim_status_box_pulse_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox));
66 }
67
68 static void
69 gaim_gtk_connection_connected(GaimConnection *gc)
70 {
71 GaimAccount *account;
72 GaimGtkBuddyList *gtkblist;
73
74 account = gaim_connection_get_account(gc);
75 gtkblist = gaim_gtk_blist_get_default_gtk_blist();
76
77 if (gtkblist != NULL)
78 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
79 (gaim_connections_get_connecting() != NULL));
80
81 if (hash != NULL)
82 g_hash_table_remove(hash, account);
83
84 if (g_hash_table_size(errored_accounts) > 0)
85 {
86 g_hash_table_remove(errored_accounts, account);
87 gaim_gtk_blist_update_account_error_state(account, NULL);
88 if (g_hash_table_size(errored_accounts) == 0)
89 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), NULL);
90 }
91 }
92
93 static void
94 gaim_gtk_connection_disconnected(GaimConnection *gc)
95 {
96 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
97 if (!gtkblist)
98 return;
99 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
100 (gaim_connections_get_connecting() != NULL));
101
102 if (gaim_connections_get_all() != NULL)
103 return;
104
105 gaim_gtkdialogs_destroy_all();
106 }
107
108 static void
109 gaim_gtk_connection_notice(GaimConnection *gc,
110 const char *text)
111 {
112 }
113
114
115 static void
116 free_auto_recon(gpointer data)
117 {
118 GaimAutoRecon *info = data;
119
120 if (info->timeout != 0)
121 g_source_remove(info->timeout);
122
123 g_free(info);
124 }
125
126 static gboolean
127 do_signon(gpointer data)
128 {
129 GaimAccount *account = data;
130 GaimAutoRecon *info;
131
132 gaim_debug_info("autorecon", "do_signon called\n");
133 g_return_val_if_fail(account != NULL, FALSE);
134 info = g_hash_table_lookup(hash, account);
135
136 if (info)
137 info->timeout = 0;
138
139 gaim_debug_info("autorecon", "calling gaim_account_connect\n");
140 gaim_account_connect(account);
141 gaim_debug_info("autorecon", "done calling gaim_account_connect\n");
142
143 return FALSE;
144 }
145
146 static void
147 gaim_gtk_connection_report_disconnect(GaimConnection *gc, const char *text)
148 {
149 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
150 GaimAccount *account = NULL;
151 GaimAutoRecon *info;
152 GSList* errored_account;
153
154 account = gaim_connection_get_account(gc);
155 info = g_hash_table_lookup(hash, account);
156 errored_account = g_hash_table_lookup(errored_accounts, account);
157
158 gaim_gtk_blist_update_account_error_state(account, text);
159 if (!gc->wants_to_die) {
160 if (gtkblist != NULL)
161 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), text);
162
163 if (info == NULL) {
164 info = g_new0(GaimAutoRecon, 1);
165 g_hash_table_insert(hash, account, info);
166 info->delay = INITIAL_RECON_DELAY;
167 } else {
168 info->delay = MIN(2 * info->delay, MAX_RECON_DELAY);
169 if (info->timeout != 0)
170 g_source_remove(info->timeout);
171 }
172 info->timeout = g_timeout_add(info->delay, do_signon, account);
173
174 g_hash_table_insert(errored_accounts, account, NULL);
175 } else {
176 char *p, *s, *n=NULL ;
177 if (info != NULL)
178 g_hash_table_remove(hash, account);
179
180 if (errored_account != NULL)
181 g_hash_table_remove(errored_accounts, errored_account);
182
183 if (gaim_account_get_alias(account))
184 {
185 n = g_strdup_printf("%s (%s) (%s)",
186 gaim_account_get_username(account),
187 gaim_account_get_alias(account),
188 gaim_account_get_protocol_name(account));
189 }
190 else
191 {
192 n = g_strdup_printf("%s (%s)",
193 gaim_account_get_username(account),
194 gaim_account_get_protocol_name(account));
195 }
196
197 p = g_strdup_printf(_("%s disconnected"), n);
198 s = g_strdup_printf(_("%s was disconnected due to an error: %s\nThe account has been disabled. "
199 "Correct the error and reenable the account to connect."), n, text);
200 gaim_notify_error(NULL, NULL, p, s);
201 g_free(p);
202 g_free(s);
203 g_free(n);
204
205 /*
206 * TODO: Do we really want to disable the account when it's
207 * disconnected by wants_to_die? This happens when you sign
208 * on from somewhere else, or when you enter an invalid password.
209 */
210 gaim_account_set_enabled(account, GAIM_GTK_UI, FALSE);
211 }
212 }
213
214 static GaimConnectionUiOps conn_ui_ops =
215 {
216 gaim_gtk_connection_connect_progress,
217 gaim_gtk_connection_connected,
218 gaim_gtk_connection_disconnected,
219 gaim_gtk_connection_notice,
220 gaim_gtk_connection_report_disconnect,
221 };
222
223 GaimConnectionUiOps *
224 gaim_gtk_connections_get_ui_ops(void)
225 {
226 return &conn_ui_ops;
227 }
228
229 static void
230 account_removed_cb(GaimAccount *account, gpointer user_data)
231 {
232 g_hash_table_remove(hash, account);
233
234 if (g_hash_table_size(errored_accounts) > 0)
235 {
236 g_hash_table_remove(errored_accounts, account);
237 gaim_gtk_blist_update_account_error_state(account, NULL);
238 if (g_hash_table_size(errored_accounts) == 0)
239 {
240 GaimGtkBuddyList *gtkblist;
241
242 gtkblist = gaim_gtk_blist_get_default_gtk_blist();
243 if (gtkblist != NULL)
244 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), NULL);
245 }
246 }
247 }
248
249
250 /**************************************************************************
251 * GTK+ connection glue
252 **************************************************************************/
253
254 void *
255 gaim_gtk_connection_get_handle(void)
256 {
257 static int handle;
258
259 return &handle;
260 }
261
262 void
263 gaim_gtk_connection_init(void)
264 {
265 hash = g_hash_table_new_full(
266 g_direct_hash, g_direct_equal,
267 NULL, free_auto_recon);
268 errored_accounts = g_hash_table_new_full(
269 g_direct_hash, g_direct_equal,
270 NULL, NULL);
271
272 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed",
273 gaim_gtk_connection_get_handle(),
274 GAIM_CALLBACK(account_removed_cb), NULL);
275 }
276
277 void
278 gaim_gtk_connection_uninit(void)
279 {
280 gaim_signals_disconnect_by_handle(gaim_gtk_connection_get_handle());
281
282 g_hash_table_destroy(hash);
283 g_hash_table_destroy(errored_accounts);
284 }

mercurial