| 1 /* |
|
| 2 * @file gtkconn.c GTK+ Connection API |
|
| 3 * @ingroup gtkui |
|
| 4 * |
|
| 5 * gaim |
|
| 6 * |
|
| 7 * Gaim is the legal property of its developers, whose names are too numerous |
|
| 8 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 9 * source distribution. |
|
| 10 * |
|
| 11 * This program is free software; you can redistribute it and/or modify |
|
| 12 * it under the terms of the GNU General Public License as published by |
|
| 13 * the Free Software Foundation; either version 2 of the License, or |
|
| 14 * (at your option) any later version. |
|
| 15 * |
|
| 16 * This program is distributed in the hope that it will be useful, |
|
| 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 19 * GNU General Public License for more details. |
|
| 20 * |
|
| 21 * You should have received a copy of the GNU General Public License |
|
| 22 * along with this program; if not, write to the Free Software |
|
| 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 24 */ |
|
| 25 #include "internal.h" |
|
| 26 #include "gtkgaim.h" |
|
| 27 |
|
| 28 #include "account.h" |
|
| 29 #include "debug.h" |
|
| 30 #include "notify.h" |
|
| 31 #include "prefs.h" |
|
| 32 #include "gtkblist.h" |
|
| 33 #include "gtkconn.h" |
|
| 34 #include "gtkdialogs.h" |
|
| 35 #include "gtkstatusbox.h" |
|
| 36 #include "gtkstock.h" |
|
| 37 #include "gtkutils.h" |
|
| 38 #include "util.h" |
|
| 39 |
|
| 40 #define INITIAL_RECON_DELAY 8000 |
|
| 41 #define MAX_RECON_DELAY 600000 |
|
| 42 |
|
| 43 typedef struct { |
|
| 44 int delay; |
|
| 45 guint timeout; |
|
| 46 } GaimAutoRecon; |
|
| 47 |
|
| 48 /** |
|
| 49 * Contains accounts that are auto-reconnecting. |
|
| 50 * The key is a pointer to the GaimAccount and the |
|
| 51 * value is a pointer to a GaimAutoRecon. |
|
| 52 */ |
|
| 53 static GHashTable *hash = NULL; |
|
| 54 |
|
| 55 static void |
|
| 56 gaim_gtk_connection_connect_progress(GaimConnection *gc, |
|
| 57 const char *text, size_t step, size_t step_count) |
|
| 58 { |
|
| 59 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
|
| 60 if (!gtkblist) |
|
| 61 return; |
|
| 62 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
|
| 63 (gaim_connections_get_connecting() != NULL)); |
|
| 64 gtk_gaim_status_box_pulse_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox)); |
|
| 65 } |
|
| 66 |
|
| 67 static void |
|
| 68 gaim_gtk_connection_connected(GaimConnection *gc) |
|
| 69 { |
|
| 70 GaimAccount *account; |
|
| 71 GaimGtkBuddyList *gtkblist; |
|
| 72 |
|
| 73 account = gaim_connection_get_account(gc); |
|
| 74 gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
|
| 75 |
|
| 76 if (gtkblist != NULL) |
|
| 77 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
|
| 78 (gaim_connections_get_connecting() != NULL)); |
|
| 79 |
|
| 80 if (hash != NULL) |
|
| 81 g_hash_table_remove(hash, account); |
|
| 82 |
|
| 83 gaim_gtk_blist_update_account_error_state(account, NULL); |
|
| 84 } |
|
| 85 |
|
| 86 static void |
|
| 87 gaim_gtk_connection_disconnected(GaimConnection *gc) |
|
| 88 { |
|
| 89 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); |
|
| 90 if (!gtkblist) |
|
| 91 return; |
|
| 92 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), |
|
| 93 (gaim_connections_get_connecting() != NULL)); |
|
| 94 |
|
| 95 if (gaim_connections_get_all() != NULL) |
|
| 96 return; |
|
| 97 |
|
| 98 gaim_gtkdialogs_destroy_all(); |
|
| 99 } |
|
| 100 |
|
| 101 static void |
|
| 102 gaim_gtk_connection_notice(GaimConnection *gc, |
|
| 103 const char *text) |
|
| 104 { |
|
| 105 } |
|
| 106 |
|
| 107 |
|
| 108 static void |
|
| 109 free_auto_recon(gpointer data) |
|
| 110 { |
|
| 111 GaimAutoRecon *info = data; |
|
| 112 |
|
| 113 if (info->timeout != 0) |
|
| 114 g_source_remove(info->timeout); |
|
| 115 |
|
| 116 g_free(info); |
|
| 117 } |
|
| 118 |
|
| 119 static gboolean |
|
| 120 do_signon(gpointer data) |
|
| 121 { |
|
| 122 GaimAccount *account = data; |
|
| 123 GaimAutoRecon *info; |
|
| 124 GaimStatus *status; |
|
| 125 |
|
| 126 gaim_debug_info("autorecon", "do_signon called\n"); |
|
| 127 g_return_val_if_fail(account != NULL, FALSE); |
|
| 128 info = g_hash_table_lookup(hash, account); |
|
| 129 |
|
| 130 if (info) |
|
| 131 info->timeout = 0; |
|
| 132 |
|
| 133 status = gaim_account_get_active_status(account); |
|
| 134 if (gaim_status_is_online(status)) |
|
| 135 { |
|
| 136 gaim_debug_info("autorecon", "calling gaim_account_connect\n"); |
|
| 137 gaim_account_connect(account); |
|
| 138 gaim_debug_info("autorecon", "done calling gaim_account_connect\n"); |
|
| 139 } |
|
| 140 |
|
| 141 return FALSE; |
|
| 142 } |
|
| 143 |
|
| 144 static void |
|
| 145 gaim_gtk_connection_report_disconnect(GaimConnection *gc, const char *text) |
|
| 146 { |
|
| 147 GaimAccount *account = NULL; |
|
| 148 GaimAutoRecon *info; |
|
| 149 |
|
| 150 account = gaim_connection_get_account(gc); |
|
| 151 info = g_hash_table_lookup(hash, account); |
|
| 152 |
|
| 153 gaim_gtk_blist_update_account_error_state(account, text); |
|
| 154 if (!gc->wants_to_die) { |
|
| 155 if (info == NULL) { |
|
| 156 info = g_new0(GaimAutoRecon, 1); |
|
| 157 g_hash_table_insert(hash, account, info); |
|
| 158 info->delay = INITIAL_RECON_DELAY; |
|
| 159 } else { |
|
| 160 info->delay = MIN(2 * info->delay, MAX_RECON_DELAY); |
|
| 161 if (info->timeout != 0) |
|
| 162 g_source_remove(info->timeout); |
|
| 163 } |
|
| 164 info->timeout = g_timeout_add(info->delay, do_signon, account); |
|
| 165 } else { |
|
| 166 char *p, *s, *n=NULL ; |
|
| 167 if (info != NULL) |
|
| 168 g_hash_table_remove(hash, account); |
|
| 169 |
|
| 170 if (gaim_account_get_alias(account)) |
|
| 171 { |
|
| 172 n = g_strdup_printf("%s (%s) (%s)", |
|
| 173 gaim_account_get_username(account), |
|
| 174 gaim_account_get_alias(account), |
|
| 175 gaim_account_get_protocol_name(account)); |
|
| 176 } |
|
| 177 else |
|
| 178 { |
|
| 179 n = g_strdup_printf("%s (%s)", |
|
| 180 gaim_account_get_username(account), |
|
| 181 gaim_account_get_protocol_name(account)); |
|
| 182 } |
|
| 183 |
|
| 184 p = g_strdup_printf(_("%s disconnected"), n); |
|
| 185 s = g_strdup_printf(_("%s was disconnected due to an error: %s\n" |
|
| 186 "Gaim will not attempt to reconnect the account until you " |
|
| 187 "correct the error and re-enable the account."), n, text); |
|
| 188 gaim_notify_error(NULL, NULL, p, s); |
|
| 189 g_free(p); |
|
| 190 g_free(s); |
|
| 191 g_free(n); |
|
| 192 |
|
| 193 /* |
|
| 194 * TODO: Do we really want to disable the account when it's |
|
| 195 * disconnected by wants_to_die? This happens when you sign |
|
| 196 * on from somewhere else, or when you enter an invalid password. |
|
| 197 */ |
|
| 198 gaim_account_set_enabled(account, GAIM_GTK_UI, FALSE); |
|
| 199 } |
|
| 200 } |
|
| 201 |
|
| 202 static GaimConnectionUiOps conn_ui_ops = |
|
| 203 { |
|
| 204 gaim_gtk_connection_connect_progress, |
|
| 205 gaim_gtk_connection_connected, |
|
| 206 gaim_gtk_connection_disconnected, |
|
| 207 gaim_gtk_connection_notice, |
|
| 208 gaim_gtk_connection_report_disconnect, |
|
| 209 }; |
|
| 210 |
|
| 211 GaimConnectionUiOps * |
|
| 212 gaim_gtk_connections_get_ui_ops(void) |
|
| 213 { |
|
| 214 return &conn_ui_ops; |
|
| 215 } |
|
| 216 |
|
| 217 static void |
|
| 218 account_removed_cb(GaimAccount *account, gpointer user_data) |
|
| 219 { |
|
| 220 g_hash_table_remove(hash, account); |
|
| 221 |
|
| 222 gaim_gtk_blist_update_account_error_state(account, NULL); |
|
| 223 } |
|
| 224 |
|
| 225 |
|
| 226 /************************************************************************** |
|
| 227 * GTK+ connection glue |
|
| 228 **************************************************************************/ |
|
| 229 |
|
| 230 void * |
|
| 231 gaim_gtk_connection_get_handle(void) |
|
| 232 { |
|
| 233 static int handle; |
|
| 234 |
|
| 235 return &handle; |
|
| 236 } |
|
| 237 |
|
| 238 void |
|
| 239 gaim_gtk_connection_init(void) |
|
| 240 { |
|
| 241 hash = g_hash_table_new_full( |
|
| 242 g_direct_hash, g_direct_equal, |
|
| 243 NULL, free_auto_recon); |
|
| 244 |
|
| 245 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed", |
|
| 246 gaim_gtk_connection_get_handle(), |
|
| 247 GAIM_CALLBACK(account_removed_cb), NULL); |
|
| 248 } |
|
| 249 |
|
| 250 void |
|
| 251 gaim_gtk_connection_uninit(void) |
|
| 252 { |
|
| 253 gaim_signals_disconnect_by_handle(gaim_gtk_connection_get_handle()); |
|
| 254 |
|
| 255 g_hash_table_destroy(hash); |
|
| 256 } |
|