3 * Finch is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "connection.h"
30 #include "gntaccount.h"
33 #define INITIAL_RECON_DELAY_MIN 8000
34 #define INITIAL_RECON_DELAY_MAX 60000
36 #define MAX_RECON_DELAY 600000
44 * Contains accounts that are auto-reconnecting.
45 * The key is a pointer to the PurpleAccount and the
46 * value is a pointer to a FinchAutoRecon.
48 static GHashTable
*hash
= NULL
;
51 free_auto_recon(gpointer data
)
53 FinchAutoRecon
*info
= data
;
55 if (info
->timeout
!= 0)
56 g_source_remove(info
->timeout
);
63 do_signon(gpointer data
)
65 PurpleAccount
*account
= data
;
69 purple_debug_info("autorecon", "do_signon called\n");
70 g_return_val_if_fail(account
!= NULL
, FALSE
);
71 info
= g_hash_table_lookup(hash
, account
);
76 status
= purple_account_get_active_status(account
);
77 if (purple_status_is_online(status
))
79 purple_debug_info("autorecon", "calling purple_account_connect\n");
80 purple_account_connect(account
);
81 purple_debug_info("autorecon", "done calling purple_account_connect\n");
88 ce_modify_account_cb(PurpleAccount
*account
)
90 finch_account_dialog_show(account
);
94 ce_enable_account_cb(PurpleAccount
*account
)
96 purple_account_set_enabled(account
, FINCH_UI
, TRUE
);
100 finch_connection_report_disconnect(PurpleConnection
*gc
, PurpleConnectionError reason
,
103 FinchAutoRecon
*info
;
104 PurpleAccount
*account
= purple_connection_get_account(gc
);
106 if (!purple_connection_error_is_fatal(reason
)) {
107 info
= g_hash_table_lookup(hash
, account
);
110 info
= g_new0(FinchAutoRecon
, 1);
111 g_hash_table_insert(hash
, account
, info
);
112 info
->delay
= g_random_int_range(INITIAL_RECON_DELAY_MIN
, INITIAL_RECON_DELAY_MAX
);
114 info
->delay
= MIN(2 * info
->delay
, MAX_RECON_DELAY
);
115 if (info
->timeout
!= 0)
116 g_source_remove(info
->timeout
);
118 info
->timeout
= g_timeout_add(info
->delay
, do_signon
, account
);
120 char *act
, *primary
, *secondary
;
121 act
= g_strdup_printf(_("%s (%s)"), purple_account_get_username(account
),
122 purple_account_get_protocol_name(account
));
124 primary
= g_strdup_printf(_("%s disconnected."), act
);
125 secondary
= g_strdup_printf(_("%s\n\n"
126 "Finch will not attempt to reconnect the account until you "
127 "correct the error and re-enable the account."), text
);
129 purple_request_action(account
, NULL
, primary
, secondary
, 2,
130 purple_request_cpar_from_account(account
), account
, 3,
132 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb
),
133 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb
));
138 purple_account_set_enabled(account
, FINCH_UI
, FALSE
);
143 account_removed_cb(PurpleAccount
*account
, gpointer user_data
)
145 g_hash_table_remove(hash
, account
);
149 finch_connection_get_handle(void)
156 static PurpleConnectionUiOps ops
=
158 NULL
, /* connect_progress */
159 NULL
, /* connected */
160 NULL
, /* disconnected */
162 NULL
, /* network_connected */
163 NULL
, /* network_disconnected */
164 finch_connection_report_disconnect
,
171 PurpleConnectionUiOps
*finch_connections_get_ui_ops()
176 void finch_connections_init()
178 hash
= g_hash_table_new_full(
179 g_direct_hash
, g_direct_equal
,
180 NULL
, free_auto_recon
);
182 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
183 finch_connection_get_handle(),
184 PURPLE_CALLBACK(account_removed_cb
), NULL
);
187 void finch_connections_uninit()
189 purple_signals_disconnect_by_handle(finch_connection_get_handle());
190 g_hash_table_destroy(hash
);