Merged pidgin/main into default
[pidgin-git.git] / finch / gntconn.c
blobcd0026a5313dea87be9ea4474860977cdcd0167d
1 /* finch
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
5 * source distribution.
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
21 #include <internal.h>
22 #include "finch.h"
24 #include "account.h"
25 #include "core.h"
26 #include "connection.h"
27 #include "debug.h"
28 #include "request.h"
30 #include "gntaccount.h"
31 #include "gntconn.h"
33 #define INITIAL_RECON_DELAY_MIN 8000
34 #define INITIAL_RECON_DELAY_MAX 60000
36 #define MAX_RECON_DELAY 600000
38 typedef struct {
39 int delay;
40 guint timeout;
41 } FinchAutoRecon;
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;
50 static void
51 free_auto_recon(gpointer data)
53 FinchAutoRecon *info = data;
55 if (info->timeout != 0)
56 g_source_remove(info->timeout);
58 g_free(info);
62 static gboolean
63 do_signon(gpointer data)
65 PurpleAccount *account = data;
66 FinchAutoRecon *info;
67 PurpleStatus *status;
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);
73 if (info)
74 info->timeout = 0;
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");
84 return FALSE;
87 static void
88 ce_modify_account_cb(PurpleAccount *account)
90 finch_account_dialog_show(account);
93 static void
94 ce_enable_account_cb(PurpleAccount *account)
96 purple_account_set_enabled(account, FINCH_UI, TRUE);
99 static void
100 finch_connection_report_disconnect(PurpleConnection *gc, PurpleConnectionError reason,
101 const char *text)
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);
109 if (info == NULL) {
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);
113 } else {
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);
119 } else {
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,
131 _("OK"), NULL,
132 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb),
133 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb));
135 g_free(act);
136 g_free(primary);
137 g_free(secondary);
138 purple_account_set_enabled(account, FINCH_UI, FALSE);
142 static void
143 account_removed_cb(PurpleAccount *account, gpointer user_data)
145 g_hash_table_remove(hash, account);
148 static void *
149 finch_connection_get_handle(void)
151 static int handle;
153 return &handle;
156 static PurpleConnectionUiOps ops =
158 NULL, /* connect_progress */
159 NULL, /* connected */
160 NULL, /* disconnected */
161 NULL, /* notice */
162 NULL, /* network_connected */
163 NULL, /* network_disconnected */
164 finch_connection_report_disconnect,
165 NULL,
166 NULL,
167 NULL,
168 NULL
171 PurpleConnectionUiOps *finch_connections_get_ui_ops()
173 return &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);