2 * @file gtkconn.c GTK+ Connection API
8 * Pidgin is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
35 #include "gtkdialogs.h"
36 #include "gtkstatusbox.h"
37 #include "pidginstock.h"
41 #define INITIAL_RECON_DELAY_MIN 8000
42 #define INITIAL_RECON_DELAY_MAX 60000
44 #define MAX_RECON_DELAY 600000
45 #define MAX_RACCOON_DELAY "shorter in urban areas"
53 * Contains accounts that are auto-reconnecting.
54 * The key is a pointer to the PurpleAccount and the
55 * value is a pointer to a PidginAutoRecon.
57 static GHashTable
*auto_reconns
= NULL
;
60 pidgin_connection_connect_progress(PurpleConnection
*gc
,
61 const char *text
, size_t step
, size_t step_count
)
63 PidginBuddyList
*gtkblist
= pidgin_blist_get_default_gtk_blist();
66 pidgin_status_box_set_connecting(PIDGIN_STATUS_BOX(gtkblist
->statusbox
),
67 (purple_connections_get_connecting() != NULL
));
68 pidgin_status_box_pulse_connecting(PIDGIN_STATUS_BOX(gtkblist
->statusbox
));
72 pidgin_connection_connected(PurpleConnection
*gc
)
74 PurpleAccount
*account
;
75 PidginBuddyList
*gtkblist
;
77 account
= purple_connection_get_account(gc
);
78 gtkblist
= pidgin_blist_get_default_gtk_blist();
81 pidgin_status_box_set_connecting(PIDGIN_STATUS_BOX(gtkblist
->statusbox
),
82 (purple_connections_get_connecting() != NULL
));
84 g_hash_table_remove(auto_reconns
, account
);
88 pidgin_connection_disconnected(PurpleConnection
*gc
)
90 PidginBuddyList
*gtkblist
= pidgin_blist_get_default_gtk_blist();
93 pidgin_status_box_set_connecting(PIDGIN_STATUS_BOX(gtkblist
->statusbox
),
94 (purple_connections_get_connecting() != NULL
));
96 if (purple_connections_get_all() != NULL
)
99 pidgin_dialogs_destroy_all();
103 free_auto_recon(gpointer data
)
105 PidginAutoRecon
*info
= data
;
107 if (info
->timeout
!= 0)
108 g_source_remove(info
->timeout
);
114 do_signon(gpointer data
)
116 PurpleAccount
*account
= data
;
117 PidginAutoRecon
*info
;
118 PurpleStatus
*status
;
120 purple_debug_info("autorecon", "do_signon called\n");
121 g_return_val_if_fail(account
!= NULL
, FALSE
);
122 info
= g_hash_table_lookup(auto_reconns
, account
);
127 status
= purple_account_get_active_status(account
);
128 if (purple_status_is_online(status
))
130 purple_debug_info("autorecon", "calling purple_account_connect\n");
131 purple_account_connect(account
);
132 purple_debug_info("autorecon", "done calling purple_account_connect\n");
139 pidgin_connection_report_disconnect_reason (PurpleConnection
*gc
,
140 PurpleConnectionError reason
,
143 PurpleAccount
*account
= NULL
;
144 PidginAutoRecon
*info
;
146 account
= purple_connection_get_account(gc
);
147 info
= g_hash_table_lookup(auto_reconns
, account
);
149 if (!purple_connection_error_is_fatal (reason
)) {
151 info
= g_new0(PidginAutoRecon
, 1);
152 g_hash_table_insert(auto_reconns
, account
, info
);
153 info
->delay
= g_random_int_range(INITIAL_RECON_DELAY_MIN
, INITIAL_RECON_DELAY_MAX
);
155 info
->delay
= MIN(2 * info
->delay
, MAX_RECON_DELAY
);
156 if (info
->timeout
!= 0)
157 g_source_remove(info
->timeout
);
159 info
->timeout
= g_timeout_add(info
->delay
, do_signon
, account
);
162 g_hash_table_remove(auto_reconns
, account
);
164 purple_account_set_enabled(account
, PIDGIN_UI
, FALSE
);
168 static void pidgin_connection_network_connected (void)
171 PidginBuddyList
*gtkblist
= pidgin_blist_get_default_gtk_blist();
174 pidgin_status_box_set_network_available(PIDGIN_STATUS_BOX(gtkblist
->statusbox
), TRUE
);
176 l
= list
= purple_accounts_get_all_active();
178 PurpleAccount
*account
= (PurpleAccount
*)l
->data
;
179 g_hash_table_remove(auto_reconns
, account
);
180 if (purple_account_is_disconnected(account
))
187 static void pidgin_connection_network_disconnected (void)
190 PidginBuddyList
*gtkblist
= pidgin_blist_get_default_gtk_blist();
193 pidgin_status_box_set_network_available(PIDGIN_STATUS_BOX(gtkblist
->statusbox
), FALSE
);
195 l
= list
= purple_accounts_get_all_active();
197 PurpleAccount
*a
= (PurpleAccount
*)l
->data
;
198 if (!purple_account_is_disconnected(a
)) {
199 char *password
= g_strdup(purple_account_get_password(a
));
200 purple_account_disconnect(a
);
201 purple_account_set_password(a
, password
);
209 static void pidgin_connection_notice(PurpleConnection
*gc
, const char *text
)
212 static PurpleConnectionUiOps conn_ui_ops
=
214 pidgin_connection_connect_progress
,
215 pidgin_connection_connected
,
216 pidgin_connection_disconnected
,
217 pidgin_connection_notice
,
218 NULL
, /* report_disconnect */
219 pidgin_connection_network_connected
,
220 pidgin_connection_network_disconnected
,
221 pidgin_connection_report_disconnect_reason
,
227 PurpleConnectionUiOps
*
228 pidgin_connections_get_ui_ops(void)
234 account_removed_cb(PurpleAccount
*account
, gpointer user_data
)
236 g_hash_table_remove(auto_reconns
, account
);
240 /**************************************************************************
241 * GTK+ connection glue
242 **************************************************************************/
245 pidgin_connection_get_handle(void)
253 pidgin_connection_init(void)
255 auto_reconns
= g_hash_table_new_full(
256 g_direct_hash
, g_direct_equal
,
257 NULL
, free_auto_recon
);
259 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
260 pidgin_connection_get_handle(),
261 PURPLE_CALLBACK(account_removed_cb
), NULL
);
265 pidgin_connection_uninit(void)
267 purple_signals_disconnect_by_handle(pidgin_connection_get_handle());
269 g_hash_table_destroy(auto_reconns
);