| |
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 "gaimstock.h" |
| |
37 #include "gtkutils.h" |
| |
38 #include "util.h" |
| |
39 |
| |
40 #define INITIAL_RECON_DELAY_MIN 8000 |
| |
41 #define INITIAL_RECON_DELAY_MAX 60000 |
| |
42 |
| |
43 #define MAX_RECON_DELAY 600000 |
| |
44 |
| |
45 typedef struct { |
| |
46 int delay; |
| |
47 guint timeout; |
| |
48 } GaimAutoRecon; |
| |
49 |
| |
50 /** |
| |
51 * Contains accounts that are auto-reconnecting. |
| |
52 * The key is a pointer to the GaimAccount and the |
| |
53 * value is a pointer to a GaimAutoRecon. |
| |
54 */ |
| |
55 static GHashTable *hash = NULL; |
| |
56 |
| |
57 static void |
| |
58 gaim_gtk_connection_connect_progress(GaimConnection *gc, |
| |
59 const char *text, size_t step, size_t step_count) |
| |
60 { |
| |
61 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
| |
62 if (!gtkblist) |
| |
63 return; |
| |
64 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
| |
65 (gaim_connections_get_connecting() != NULL)); |
| |
66 gtk_gaim_status_box_pulse_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox)); |
| |
67 } |
| |
68 |
| |
69 static void |
| |
70 gaim_gtk_connection_connected(GaimConnection *gc) |
| |
71 { |
| |
72 GaimAccount *account; |
| |
73 GaimGtkBuddyList *gtkblist; |
| |
74 |
| |
75 account = gaim_connection_get_account(gc); |
| |
76 gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
| |
77 |
| |
78 if (gtkblist != NULL) |
| |
79 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
| |
80 (gaim_connections_get_connecting() != NULL)); |
| |
81 |
| |
82 if (hash != NULL) |
| |
83 g_hash_table_remove(hash, account); |
| |
84 |
| |
85 gaim_gtk_blist_update_account_error_state(account, NULL); |
| |
86 } |
| |
87 |
| |
88 static void |
| |
89 gaim_gtk_connection_disconnected(GaimConnection *gc) |
| |
90 { |
| |
91 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
| |
92 if (!gtkblist) |
| |
93 return; |
| |
94 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
| |
95 (gaim_connections_get_connecting() != NULL)); |
| |
96 |
| |
97 if (gaim_connections_get_all() != NULL) |
| |
98 return; |
| |
99 |
| |
100 gaim_gtkdialogs_destroy_all(); |
| |
101 } |
| |
102 |
| |
103 static void |
| |
104 gaim_gtk_connection_notice(GaimConnection *gc, |
| |
105 const char *text) |
| |
106 { |
| |
107 } |
| |
108 |
| |
109 |
| |
110 static void |
| |
111 free_auto_recon(gpointer data) |
| |
112 { |
| |
113 GaimAutoRecon *info = data; |
| |
114 |
| |
115 if (info->timeout != 0) |
| |
116 g_source_remove(info->timeout); |
| |
117 |
| |
118 g_free(info); |
| |
119 } |
| |
120 |
| |
121 static gboolean |
| |
122 do_signon(gpointer data) |
| |
123 { |
| |
124 GaimAccount *account = data; |
| |
125 GaimAutoRecon *info; |
| |
126 GaimStatus *status; |
| |
127 |
| |
128 gaim_debug_info("autorecon", "do_signon called\n"); |
| |
129 g_return_val_if_fail(account != NULL, FALSE); |
| |
130 info = g_hash_table_lookup(hash, account); |
| |
131 |
| |
132 if (info) |
| |
133 info->timeout = 0; |
| |
134 |
| |
135 status = gaim_account_get_active_status(account); |
| |
136 if (gaim_status_is_online(status)) |
| |
137 { |
| |
138 gaim_debug_info("autorecon", "calling gaim_account_connect\n"); |
| |
139 gaim_account_connect(account); |
| |
140 gaim_debug_info("autorecon", "done calling gaim_account_connect\n"); |
| |
141 } |
| |
142 |
| |
143 return FALSE; |
| |
144 } |
| |
145 |
| |
146 static void |
| |
147 gaim_gtk_connection_report_disconnect(GaimConnection *gc, const char *text) |
| |
148 { |
| |
149 GaimAccount *account = NULL; |
| |
150 GaimAutoRecon *info; |
| |
151 |
| |
152 account = gaim_connection_get_account(gc); |
| |
153 info = g_hash_table_lookup(hash, account); |
| |
154 |
| |
155 gaim_gtk_blist_update_account_error_state(account, text); |
| |
156 if (!gc->wants_to_die) { |
| |
157 if (info == NULL) { |
| |
158 info = g_new0(GaimAutoRecon, 1); |
| |
159 g_hash_table_insert(hash, account, info); |
| |
160 info->delay = g_random_int_range(INITIAL_RECON_DELAY_MIN, INITIAL_RECON_DELAY_MAX); |
| |
161 } else { |
| |
162 info->delay = MIN(2 * info->delay, MAX_RECON_DELAY); |
| |
163 if (info->timeout != 0) |
| |
164 g_source_remove(info->timeout); |
| |
165 } |
| |
166 info->timeout = g_timeout_add(info->delay, do_signon, account); |
| |
167 } else { |
| |
168 char *p, *s, *n=NULL ; |
| |
169 if (info != NULL) |
| |
170 g_hash_table_remove(hash, account); |
| |
171 |
| |
172 if (gaim_account_get_alias(account)) |
| |
173 { |
| |
174 n = g_strdup_printf("%s (%s) (%s)", |
| |
175 gaim_account_get_username(account), |
| |
176 gaim_account_get_alias(account), |
| |
177 gaim_account_get_protocol_name(account)); |
| |
178 } |
| |
179 else |
| |
180 { |
| |
181 n = g_strdup_printf("%s (%s)", |
| |
182 gaim_account_get_username(account), |
| |
183 gaim_account_get_protocol_name(account)); |
| |
184 } |
| |
185 |
| |
186 p = g_strdup_printf(_("%s disconnected"), n); |
| |
187 s = g_strdup_printf(_("%s was disconnected due to an error: %s\n" |
| |
188 "Gaim will not attempt to reconnect the account until you " |
| |
189 "correct the error and re-enable the account."), n, text); |
| |
190 gaim_notify_error(NULL, NULL, p, s); |
| |
191 g_free(p); |
| |
192 g_free(s); |
| |
193 g_free(n); |
| |
194 |
| |
195 /* |
| |
196 * TODO: Do we really want to disable the account when it's |
| |
197 * disconnected by wants_to_die? This happens when you sign |
| |
198 * on from somewhere else, or when you enter an invalid password. |
| |
199 */ |
| |
200 gaim_account_set_enabled(account, GAIM_GTK_UI, FALSE); |
| |
201 } |
| |
202 } |
| |
203 |
| |
204 static GaimConnectionUiOps conn_ui_ops = |
| |
205 { |
| |
206 gaim_gtk_connection_connect_progress, |
| |
207 gaim_gtk_connection_connected, |
| |
208 gaim_gtk_connection_disconnected, |
| |
209 gaim_gtk_connection_notice, |
| |
210 gaim_gtk_connection_report_disconnect, |
| |
211 }; |
| |
212 |
| |
213 GaimConnectionUiOps * |
| |
214 gaim_gtk_connections_get_ui_ops(void) |
| |
215 { |
| |
216 return &conn_ui_ops; |
| |
217 } |
| |
218 |
| |
219 static void |
| |
220 account_removed_cb(GaimAccount *account, gpointer user_data) |
| |
221 { |
| |
222 g_hash_table_remove(hash, account); |
| |
223 |
| |
224 gaim_gtk_blist_update_account_error_state(account, NULL); |
| |
225 } |
| |
226 |
| |
227 |
| |
228 /************************************************************************** |
| |
229 * GTK+ connection glue |
| |
230 **************************************************************************/ |
| |
231 |
| |
232 void * |
| |
233 gaim_gtk_connection_get_handle(void) |
| |
234 { |
| |
235 static int handle; |
| |
236 |
| |
237 return &handle; |
| |
238 } |
| |
239 |
| |
240 void |
| |
241 gaim_gtk_connection_init(void) |
| |
242 { |
| |
243 hash = g_hash_table_new_full( |
| |
244 g_direct_hash, g_direct_equal, |
| |
245 NULL, free_auto_recon); |
| |
246 |
| |
247 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed", |
| |
248 gaim_gtk_connection_get_handle(), |
| |
249 GAIM_CALLBACK(account_removed_cb), NULL); |
| |
250 } |
| |
251 |
| |
252 void |
| |
253 gaim_gtk_connection_uninit(void) |
| |
254 { |
| |
255 gaim_signals_disconnect_by_handle(gaim_gtk_connection_get_handle()); |
| |
256 |
| |
257 g_hash_table_destroy(hash); |
| |
258 } |