comparison src/gtkconn.c @ 13014:5efbb0162f86

[gaim-migrate @ 15367] I don't expect any of this to be controversial. * Some comment and other small changes. * Changed gtkconn.c to store the list of errored accounts in a hash table instead of a glist * Added init and uninit functions to gtkconn.c. They're used to create and destroy the two hash tables used within that file. * Also use the new init and uninit functions to connect to the account deleted signal so that we clear out the old error message if we delete an account that had an error while connecting. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Mon, 23 Jan 2006 06:54:29 +0000
parents 8fe78402c010
children 2d71d4081e06
comparison
equal deleted inserted replaced
13013:8fe78402c010 13014:5efbb0162f86
43 typedef struct { 43 typedef struct {
44 int delay; 44 int delay;
45 guint timeout; 45 guint timeout;
46 } GaimAutoRecon; 46 } GaimAutoRecon;
47 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 */
48 static GHashTable *hash = NULL; 53 static GHashTable *hash = NULL;
49 /* 54 static GHashTable *errored_accounts = NULL;
50 * TODO: Remove an account from this list if the account is deleted. 55
51 */ 56 static void
52 static GSList *errored_accounts = NULL; 57 gaim_gtk_connection_connect_progress(GaimConnection *gc,
53
54 static void gaim_gtk_connection_connect_progress(GaimConnection *gc,
55 const char *text, size_t step, size_t step_count) 58 const char *text, size_t step, size_t step_count)
56 { 59 {
57 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); 60 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
58 if (!gtkblist) 61 if (!gtkblist)
59 return; 62 return;
60 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), 63 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
61 (gaim_connections_get_connecting() != NULL)); 64 (gaim_connections_get_connecting() != NULL));
62 gtk_gaim_status_box_pulse_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox)); 65 gtk_gaim_status_box_pulse_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox));
63 } 66 }
64 67
65 static void gaim_gtk_connection_connected(GaimConnection *gc) 68 static void
66 { 69 gaim_gtk_connection_connected(GaimConnection *gc)
67 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); 70 {
68 GaimAccount *account = NULL; 71 GaimAccount *account;
69 if (!gtkblist) 72 GaimGtkBuddyList *gtkblist;
70 return; 73
71 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), 74 account = gaim_connection_get_account(gc);
75 gtkblist = gaim_gtk_blist_get_default_gtk_blist();
76
77 if (gtkblist != NULL)
78 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
72 (gaim_connections_get_connecting() != NULL)); 79 (gaim_connections_get_connecting() != NULL));
73 account = gaim_connection_get_account(gc);
74 80
75 if (hash != NULL) 81 if (hash != NULL)
76 g_hash_table_remove(hash, account); 82 g_hash_table_remove(hash, account);
77 if (errored_accounts == NULL) 83
78 return; 84 if (g_hash_table_size(errored_accounts) > 0)
79 errored_accounts = g_slist_remove(errored_accounts, account); 85 {
80 if (errored_accounts == NULL) 86 g_hash_table_remove(errored_accounts, account);
81 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), NULL); 87 if (g_hash_table_size(errored_accounts) == 0)
82 } 88 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), NULL);
83 89 }
84 static void gaim_gtk_connection_disconnected(GaimConnection *gc) 90 }
91
92 static void
93 gaim_gtk_connection_disconnected(GaimConnection *gc)
85 { 94 {
86 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); 95 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
87 if (!gtkblist) 96 if (!gtkblist)
88 return; 97 return;
89 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), 98 gtk_gaim_status_box_set_connecting(GTK_GAIM_STATUS_BOX(gtkblist->statusbox),
93 return; 102 return;
94 103
95 gaim_gtkdialogs_destroy_all(); 104 gaim_gtkdialogs_destroy_all();
96 } 105 }
97 106
98 static void gaim_gtk_connection_notice(GaimConnection *gc, 107 static void
108 gaim_gtk_connection_notice(GaimConnection *gc,
99 const char *text) 109 const char *text)
100 { 110 {
101 } 111 }
102 112
103 113
116 do_signon(gpointer data) 126 do_signon(gpointer data)
117 { 127 {
118 GaimAccount *account = data; 128 GaimAccount *account = data;
119 GaimAutoRecon *info; 129 GaimAutoRecon *info;
120 130
121 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "do_signon called\n"); 131 gaim_debug_info("autorecon", "do_signon called\n");
122 g_return_val_if_fail(account != NULL, FALSE); 132 g_return_val_if_fail(account != NULL, FALSE);
123 info = g_hash_table_lookup(hash, account); 133 info = g_hash_table_lookup(hash, account);
124 134
125 if (g_list_index(gaim_accounts_get_all(), account) < 0)
126 return FALSE;
127
128 if (info) 135 if (info)
129 info->timeout = 0; 136 info->timeout = 0;
130 137
131 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "calling gaim_account_connect\n"); 138 gaim_debug_info("autorecon", "calling gaim_account_connect\n");
132 gaim_account_connect(account); 139 gaim_account_connect(account);
133 gaim_debug(GAIM_DEBUG_INFO, "autorecon", "done calling gaim_account_connect\n"); 140 gaim_debug_info("autorecon", "done calling gaim_account_connect\n");
134 141
135 return FALSE; 142 return FALSE;
136 } 143 }
137 144
138 static void gaim_gtk_connection_report_disconnect(GaimConnection *gc, const char *text) 145 static void
146 gaim_gtk_connection_report_disconnect(GaimConnection *gc, const char *text)
139 { 147 {
140 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist(); 148 GaimGtkBuddyList *gtkblist = gaim_gtk_blist_get_default_gtk_blist();
141 GaimAccount *account = NULL; 149 GaimAccount *account = NULL;
142 GaimAutoRecon *info; 150 GaimAutoRecon *info;
143 GSList* errored_account; 151 GSList* errored_account;
144 152
145 if (hash == NULL) {
146 hash = g_hash_table_new_full(g_int_hash, g_int_equal, NULL,
147 free_auto_recon);
148 }
149 account = gaim_connection_get_account(gc); 153 account = gaim_connection_get_account(gc);
150 info = g_hash_table_lookup(hash, account); 154 info = g_hash_table_lookup(hash, account);
151 if (errored_accounts) 155 errored_account = g_hash_table_lookup(errored_accounts, account);
152 errored_account = g_slist_find(errored_accounts, account);
153 else
154 errored_account = NULL;
155 156
156 if (!gc->wants_to_die) { 157 if (!gc->wants_to_die) {
157 if (gtkblist != NULL) 158 if (gtkblist != NULL)
158 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), text); 159 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), text);
159 160
166 if (info->timeout != 0) 167 if (info->timeout != 0)
167 g_source_remove(info->timeout); 168 g_source_remove(info->timeout);
168 } 169 }
169 info->timeout = g_timeout_add(info->delay, do_signon, account); 170 info->timeout = g_timeout_add(info->delay, do_signon, account);
170 171
171 if (!errored_account) 172 g_hash_table_insert(errored_accounts, account, NULL);
172 errored_accounts = g_slist_prepend(errored_accounts, account);
173 } else { 173 } else {
174 char *p, *s, *n=NULL ; 174 char *p, *s, *n=NULL ;
175 if (info != NULL) 175 if (info != NULL)
176 g_hash_table_remove(hash, account); 176 g_hash_table_remove(hash, account);
177 177
178 if (errored_account) 178 if (errored_account != NULL)
179 errored_accounts = g_slist_delete_link(errored_accounts, errored_account); 179 g_hash_table_remove(errored_accounts, errored_account);
180 180
181 if (gaim_account_get_alias(account)) { 181 if (gaim_account_get_alias(account))
182 n = g_strdup_printf("%s (%s) (%s)", 182 {
183 gaim_account_get_username(account), 183 n = g_strdup_printf("%s (%s) (%s)",
184 gaim_account_get_alias(account), 184 gaim_account_get_username(account),
185 gaim_account_get_protocol_name(account)); 185 gaim_account_get_alias(account),
186 } else { 186 gaim_account_get_protocol_name(account));
187 n = g_strdup_printf("%s (%s)", 187 }
188 gaim_account_get_username(account), 188 else
189 gaim_account_get_protocol_name(account)); 189 {
190 } 190 n = g_strdup_printf("%s (%s)",
191 191 gaim_account_get_username(account),
192 p = g_strdup_printf(_("%s disconnected"), n); 192 gaim_account_get_protocol_name(account));
193 s = g_strdup_printf(_("%s was disconnected due to an error. %s The account has been disabled. " 193 }
194 "Correct the error and reenable the account to connect."), n, text); 194
195 gaim_notify_error(NULL, NULL, p, s); 195 p = g_strdup_printf(_("%s disconnected"), n);
196 g_free(p); 196 s = g_strdup_printf(_("%s was disconnected due to an error. %s The account has been disabled. "
197 g_free(s); 197 "Correct the error and reenable the account to connect."), n, text);
198 g_free(n); 198 gaim_notify_error(NULL, NULL, p, s);
199 /* XXX: do we really want to disable the account when it's disconnected by wants_to_die? 199 g_free(p);
200 * This normally happens when you sign on from somewhere else. */ 200 g_free(s);
201 gaim_account_set_enabled(account, GAIM_GTK_UI, FALSE); 201 g_free(n);
202
203 /*
204 * TODO: Do we really want to disable the account when it's
205 * disconnected by wants_to_die? This happens when you sign
206 * on from somewhere else, or when you enter an invalid password.
207 */
208 gaim_account_set_enabled(account, GAIM_GTK_UI, FALSE);
202 } 209 }
203 } 210 }
204 211
205 static GaimConnectionUiOps conn_ui_ops = 212 static GaimConnectionUiOps conn_ui_ops =
206 { 213 {
214 GaimConnectionUiOps * 221 GaimConnectionUiOps *
215 gaim_gtk_connections_get_ui_ops(void) 222 gaim_gtk_connections_get_ui_ops(void)
216 { 223 {
217 return &conn_ui_ops; 224 return &conn_ui_ops;
218 } 225 }
226
227 static void
228 account_removed_cb(GaimAccount *account, gpointer user_data)
229 {
230 g_hash_table_remove(hash, account);
231
232 if (g_hash_table_size(errored_accounts) > 0)
233 {
234 g_hash_table_remove(errored_accounts, account);
235 if (g_hash_table_size(errored_accounts) == 0)
236 {
237 GaimGtkBuddyList *gtkblist;
238
239 gtkblist = gaim_gtk_blist_get_default_gtk_blist();
240 if (gtkblist != NULL)
241 gtk_gaim_status_box_set_error(GTK_GAIM_STATUS_BOX(gtkblist->statusbox), NULL);
242 }
243 }
244 }
245
246
247 /**************************************************************************
248 * GTK+ connection glue
249 **************************************************************************/
250
251 void *
252 gaim_gtk_connection_get_handle(void)
253 {
254 static int handle;
255
256 return &handle;
257 }
258
259 void
260 gaim_gtk_connection_init(void)
261 {
262 hash = g_hash_table_new_full(
263 g_direct_hash, g_direct_equal,
264 NULL, free_auto_recon);
265 errored_accounts = g_hash_table_new_full(
266 g_direct_hash, g_direct_equal,
267 NULL, NULL);
268
269 gaim_signal_connect(gaim_accounts_get_handle(), "account-removed",
270 gaim_gtk_connection_get_handle(),
271 GAIM_CALLBACK(account_removed_cb), NULL);
272 }
273
274 void
275 gaim_gtk_connection_uninit(void)
276 {
277 gaim_signals_disconnect_by_handle(gaim_gtk_connection_get_handle());
278
279 g_hash_table_destroy(hash);
280 g_hash_table_destroy(errored_accounts);
281 }