Simplify Facebook API soup session setup.
[pidgin-git.git] / finch / gntconn.c
blob6f52e4aea895c5a26d02e19b1ee58dedb099e3e5
1 /*
2 * finch
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
6 * source distribution.
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
23 #include <internal.h>
24 #include "finch.h"
26 #include "account.h"
27 #include "core.h"
28 #include "connection.h"
29 #include "debug.h"
30 #include "request.h"
32 #include "gntaccount.h"
33 #include "gntconn.h"
35 #define INITIAL_RECON_DELAY_MIN 8000
36 #define INITIAL_RECON_DELAY_MAX 60000
38 #define MAX_RECON_DELAY 600000
40 typedef struct {
41 int delay;
42 guint timeout;
43 } FinchAutoRecon;
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;
52 static void
53 free_auto_recon(gpointer data)
55 FinchAutoRecon *info = data;
57 if (info->timeout != 0)
58 g_source_remove(info->timeout);
60 g_free(info);
64 static gboolean
65 do_signon(gpointer data)
67 PurpleAccount *account = data;
68 FinchAutoRecon *info;
69 PurpleStatus *status;
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);
75 if (info)
76 info->timeout = 0;
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");
86 return FALSE;
89 static void
90 ce_modify_account_cb(PurpleAccount *account)
92 finch_account_dialog_show(account);
95 static void
96 ce_enable_account_cb(PurpleAccount *account)
98 purple_account_set_enabled(account, FINCH_UI, TRUE);
101 static void
102 finch_connection_report_disconnect(PurpleConnection *gc, PurpleConnectionError reason,
103 const char *text)
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);
111 if (info == NULL) {
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);
115 } else {
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);
121 } else {
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,
133 _("OK"), NULL,
134 _("Modify Account"), PURPLE_CALLBACK(ce_modify_account_cb),
135 _("Re-enable Account"), PURPLE_CALLBACK(ce_enable_account_cb));
137 g_free(act);
138 g_free(primary);
139 g_free(secondary);
140 purple_account_set_enabled(account, FINCH_UI, FALSE);
144 static void
145 account_removed_cb(PurpleAccount *account, gpointer user_data)
147 g_hash_table_remove(hash, account);
150 static void *
151 finch_connection_get_handle(void)
153 static int handle;
155 return &handle;
158 static PurpleConnectionUiOps ops =
160 NULL, /* connect_progress */
161 NULL, /* connected */
162 NULL, /* disconnected */
163 NULL, /* notice */
164 NULL, /* network_connected */
165 NULL, /* network_disconnected */
166 finch_connection_report_disconnect,
167 NULL,
168 NULL,
169 NULL,
170 NULL
173 PurpleConnectionUiOps *finch_connections_get_ui_ops()
175 return &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);