Merge Windows-specific changes for 2.7.0 and 2.7.1 into the main ChangeLog and
[pidgin-git.git] / finch / gntconn.c
blob28e0b9f30c5991f38ec950d215d25bc2d9142d9b
1 /**
2 * @file gntconn.c GNT Connection API
3 * @ingroup finch
4 */
6 /* finch
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
26 #include <internal.h>
27 #include "finch.h"
29 #include "account.h"
30 #include "core.h"
31 #include "connection.h"
32 #include "debug.h"
33 #include "request.h"
35 #include "gntaccount.h"
36 #include "gntconn.h"
38 #define INITIAL_RECON_DELAY_MIN 8000
39 #define INITIAL_RECON_DELAY_MAX 60000
41 #define MAX_RECON_DELAY 600000
43 typedef struct {
44 int delay;
45 guint timeout;
46 } FinchAutoRecon;
48 /**
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;
55 static void
56 free_auto_recon(gpointer data)
58 FinchAutoRecon *info = data;
60 if (info->timeout != 0)
61 g_source_remove(info->timeout);
63 g_free(info);
67 static gboolean
68 do_signon(gpointer data)
70 PurpleAccount *account = data;
71 FinchAutoRecon *info;
72 PurpleStatus *status;
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);
78 if (info)
79 info->timeout = 0;
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");
89 return FALSE;
92 static void
93 ce_modify_account_cb(PurpleAccount *account)
95 finch_account_dialog_show(account);
98 static void
99 ce_enable_account_cb(PurpleAccount *account)
101 purple_account_set_enabled(account, FINCH_UI, TRUE);
104 static void
105 finch_connection_report_disconnect(PurpleConnection *gc, PurpleConnectionError reason,
106 const char *text)
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);
114 if (info == NULL) {
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);
118 } else {
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);
124 } else {
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,
135 account, NULL, NULL,
136 account, 3,
137 _("OK"), NULL,
138 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb),
139 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb));
141 g_free(act);
142 g_free(primary);
143 g_free(secondary);
144 purple_account_set_enabled(account, FINCH_UI, FALSE);
148 static void
149 account_removed_cb(PurpleAccount *account, gpointer user_data)
151 g_hash_table_remove(hash, account);
154 static void *
155 finch_connection_get_handle(void)
157 static int handle;
159 return &handle;
162 static PurpleConnectionUiOps ops =
164 NULL, /* connect_progress */
165 NULL, /* connected */
166 NULL, /* disconnected */
167 NULL, /* notice */
168 NULL,
169 NULL, /* network_connected */
170 NULL, /* network_disconnected */
171 finch_connection_report_disconnect,
172 NULL,
173 NULL,
174 NULL
177 PurpleConnectionUiOps *finch_connections_get_ui_ops()
179 return &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);