2 * @file gntconn.c GNT Connection API
8 * Finch 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
31 #include "connection.h"
35 #include "gntaccount.h"
38 #define INITIAL_RECON_DELAY_MIN 8000
39 #define INITIAL_RECON_DELAY_MAX 60000
41 #define MAX_RECON_DELAY 600000
49 * Contains accounts that are auto-reconnecting.
50 * The key is a pointer to the PurpleAccount and the
51 * value is a pointer to a FinchAutoRecon.
53 static GHashTable
*hash
= NULL
;
56 free_auto_recon(gpointer data
)
58 FinchAutoRecon
*info
= data
;
60 if (info
->timeout
!= 0)
61 g_source_remove(info
->timeout
);
68 do_signon(gpointer data
)
70 PurpleAccount
*account
= data
;
74 purple_debug_info("autorecon", "do_signon called\n");
75 g_return_val_if_fail(account
!= NULL
, FALSE
);
76 info
= g_hash_table_lookup(hash
, account
);
81 status
= purple_account_get_active_status(account
);
82 if (purple_status_is_online(status
))
84 purple_debug_info("autorecon", "calling purple_account_connect\n");
85 purple_account_connect(account
);
86 purple_debug_info("autorecon", "done calling purple_account_connect\n");
93 ce_modify_account_cb(PurpleAccount
*account
)
95 finch_account_dialog_show(account
);
99 ce_enable_account_cb(PurpleAccount
*account
)
101 purple_account_set_enabled(account
, FINCH_UI
, TRUE
);
105 finch_connection_report_disconnect(PurpleConnection
*gc
, PurpleConnectionError reason
,
108 FinchAutoRecon
*info
;
109 PurpleAccount
*account
= purple_connection_get_account(gc
);
111 if (!purple_connection_error_is_fatal(reason
)) {
112 info
= g_hash_table_lookup(hash
, account
);
115 info
= g_new0(FinchAutoRecon
, 1);
116 g_hash_table_insert(hash
, account
, info
);
117 info
->delay
= g_random_int_range(INITIAL_RECON_DELAY_MIN
, INITIAL_RECON_DELAY_MAX
);
119 info
->delay
= MIN(2 * info
->delay
, MAX_RECON_DELAY
);
120 if (info
->timeout
!= 0)
121 g_source_remove(info
->timeout
);
123 info
->timeout
= g_timeout_add(info
->delay
, do_signon
, account
);
125 char *act
, *primary
, *secondary
;
126 act
= g_strdup_printf(_("%s (%s)"), purple_account_get_username(account
),
127 purple_account_get_protocol_name(account
));
129 primary
= g_strdup_printf(_("%s disconnected."), act
);
130 secondary
= g_strdup_printf(_("%s\n\n"
131 "Finch will not attempt to reconnect the account until you "
132 "correct the error and re-enable the account."), text
);
134 purple_request_action(account
, NULL
, primary
, secondary
, 2,
138 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb
),
139 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb
));
144 purple_account_set_enabled(account
, FINCH_UI
, FALSE
);
149 account_removed_cb(PurpleAccount
*account
, gpointer user_data
)
151 g_hash_table_remove(hash
, account
);
155 finch_connection_get_handle(void)
162 static PurpleConnectionUiOps ops
=
164 NULL
, /* connect_progress */
165 NULL
, /* connected */
166 NULL
, /* disconnected */
169 NULL
, /* network_connected */
170 NULL
, /* network_disconnected */
171 finch_connection_report_disconnect
,
177 PurpleConnectionUiOps
*finch_connections_get_ui_ops()
182 void finch_connections_init()
184 hash
= g_hash_table_new_full(
185 g_direct_hash
, g_direct_equal
,
186 NULL
, free_auto_recon
);
188 purple_signal_connect(purple_accounts_get_handle(), "account-removed",
189 finch_connection_get_handle(),
190 PURPLE_CALLBACK(account_removed_cb
), NULL
);
193 void finch_connections_uninit()
195 purple_signals_disconnect_by_handle(finch_connection_get_handle());
196 g_hash_table_destroy(hash
);