4 * Finch is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
28 #include "connection.h"
32 #include "gntaccount.h"
35 #define INITIAL_RECON_DELAY_MIN 8000
36 #define INITIAL_RECON_DELAY_MAX 60000
38 #define MAX_RECON_DELAY 600000
46 * Contains accounts that are auto-reconnecting.
47 * The key is a pointer to the PurpleAccount and the
48 * value is a pointer to a FinchAutoRecon.
50 static GHashTable
*hash
= NULL
;
53 free_auto_recon(gpointer data
)
55 FinchAutoRecon
*info
= data
;
57 if (info
->timeout
!= 0)
58 g_source_remove(info
->timeout
);
65 do_signon(gpointer data
)
67 PurpleAccount
*account
= data
;
71 purple_debug_info("autorecon", "do_signon called\n");
72 g_return_val_if_fail(account
!= NULL
, FALSE
);
73 info
= g_hash_table_lookup(hash
, account
);
78 status
= purple_account_get_active_status(account
);
79 if (purple_status_is_online(status
))
81 purple_debug_info("autorecon", "calling purple_account_connect\n");
82 purple_account_connect(account
);
83 purple_debug_info("autorecon", "done calling purple_account_connect\n");
90 ce_modify_account_cb(PurpleAccount
*account
)
92 finch_account_dialog_show(account
);
96 ce_enable_account_cb(PurpleAccount
*account
)
98 purple_account_set_enabled(account
, FINCH_UI
, TRUE
);
102 finch_connection_report_disconnect(PurpleConnection
*gc
, PurpleConnectionError reason
,
105 FinchAutoRecon
*info
;
106 PurpleAccount
*account
= purple_connection_get_account(gc
);
108 if (!purple_connection_error_is_fatal(reason
)) {
109 info
= g_hash_table_lookup(hash
, account
);
112 info
= g_new0(FinchAutoRecon
, 1);
113 g_hash_table_insert(hash
, account
, info
);
114 info
->delay
= g_random_int_range(INITIAL_RECON_DELAY_MIN
, INITIAL_RECON_DELAY_MAX
);
116 info
->delay
= MIN(2 * info
->delay
, MAX_RECON_DELAY
);
117 if (info
->timeout
!= 0)
118 g_source_remove(info
->timeout
);
120 info
->timeout
= g_timeout_add(info
->delay
, do_signon
, account
);
122 char *act
, *primary
, *secondary
;
123 act
= g_strdup_printf(_("%s (%s)"), purple_account_get_username(account
),
124 purple_account_get_protocol_name(account
));
126 primary
= g_strdup_printf(_("%s disconnected."), act
);
127 secondary
= g_strdup_printf(_("%s\n\n"
128 "Finch will not attempt to reconnect the account until you "
129 "correct the error and re-enable the account."), text
);
131 purple_request_action(account
, NULL
, primary
, secondary
, 2,
132 purple_request_cpar_from_account(account
), account
, 3,
134 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb
),
135 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb
));
140 purple_account_set_enabled(account
, FINCH_UI
, FALSE
);
145 account_removed_cb(PurpleAccount
*account
, gpointer user_data
)
147 g_hash_table_remove(hash
, account
);
151 finch_connection_get_handle(void)
158 static PurpleConnectionUiOps ops
=
160 NULL
, /* connect_progress */
161 NULL
, /* connected */
162 NULL
, /* disconnected */
164 NULL
, /* network_connected */
165 NULL
, /* network_disconnected */
166 finch_connection_report_disconnect
,
173 PurpleConnectionUiOps
*finch_connections_get_ui_ops()
178 void finch_connections_init()
180 hash
= g_hash_table_new_full(
181 g_direct_hash
, g_direct_equal
,
182 NULL
, free_auto_recon
);
184 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
185 finch_connection_get_handle(),
186 PURPLE_CALLBACK(account_removed_cb
), NULL
);
189 void finch_connections_uninit()
191 purple_signals_disconnect_by_handle(finch_connection_get_handle());
192 g_hash_table_destroy(hash
);