Mercurial > pidgin
annotate libgaim/connection.c @ 14683:a8c2af13b96d
[gaim-migrate @ 17435]
Fix the docklet on wingaim.
What is the plan for the docklet prefs?
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Fri, 06 Oct 2006 05:03:28 +0000 |
parents | f23506e8f812 |
children | 4124030c3f3a |
rev | line source |
---|---|
14192 | 1 /** |
2 * @file connection.c Connection API | |
3 * @ingroup core | |
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 "account.h" | |
27 #include "blist.h" | |
28 #include "connection.h" | |
29 #include "dbus-maybe.h" | |
30 #include "debug.h" | |
31 #include "log.h" | |
32 #include "notify.h" | |
33 #include "prefs.h" | |
34 #include "request.h" | |
35 #include "server.h" | |
36 #include "signals.h" | |
37 #include "util.h" | |
38 | |
39 static GList *connections = NULL; | |
40 static GList *connections_connecting = NULL; | |
41 static GaimConnectionUiOps *connection_ui_ops = NULL; | |
42 | |
43 static int connections_handle; | |
44 | |
45 static gboolean | |
46 send_keepalive(gpointer data) | |
47 { | |
48 GaimConnection *gc = data; | |
49 GaimPluginProtocolInfo *prpl_info = NULL; | |
50 | |
51 if (gc != NULL && gc->prpl != NULL) | |
52 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
53 | |
54 if (prpl_info && prpl_info->keepalive) | |
55 prpl_info->keepalive(gc); | |
56 | |
57 return TRUE; | |
58 } | |
59 | |
60 static void | |
61 update_keepalive(GaimConnection *gc, gboolean on) | |
62 { | |
14483
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
63 GaimPluginProtocolInfo *prpl_info = NULL; |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
64 |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
65 if (gc != NULL && gc->prpl != NULL) |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
66 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
67 |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
68 if (!prpl_info || !prpl_info->keepalive) |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
69 return; |
af856551902b
[gaim-migrate @ 17202]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
14192
diff
changeset
|
70 |
14192 | 71 if (on && !gc->keepalive) |
72 { | |
73 gaim_debug_info("connection", "Activating keepalive.\n"); | |
74 gc->keepalive = gaim_timeout_add(30000, send_keepalive, gc); | |
75 } | |
76 else if (!on && gc->keepalive > 0) | |
77 { | |
78 gaim_debug_info("connection", "Deactivating keepalive.\n"); | |
79 gaim_timeout_remove(gc->keepalive); | |
80 gc->keepalive = 0; | |
81 } | |
82 } | |
83 | |
84 void | |
85 gaim_connection_new(GaimAccount *account, gboolean regist, const char *password) | |
86 { | |
87 GaimConnection *gc; | |
88 GaimPlugin *prpl; | |
89 GaimPluginProtocolInfo *prpl_info; | |
90 | |
91 g_return_if_fail(account != NULL); | |
92 | |
93 if (!gaim_account_is_disconnected(account)) | |
94 return; | |
95 | |
96 prpl = gaim_find_prpl(gaim_account_get_protocol_id(account)); | |
97 | |
98 if (prpl != NULL) | |
99 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(prpl); | |
100 else { | |
101 gchar *message; | |
102 | |
103 message = g_strdup_printf(_("Missing protocol plugin for %s"), | |
104 gaim_account_get_username(account)); | |
105 gaim_notify_error(NULL, regist ? _("Registration Error") : | |
106 _("Connection Error"), message, NULL); | |
107 g_free(message); | |
108 return; | |
109 } | |
110 | |
111 if (regist) | |
112 { | |
113 if (prpl_info->register_user == NULL) | |
114 return; | |
115 } | |
116 else | |
117 { | |
118 if (((password == NULL) || (*password == '\0')) && | |
119 !(prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
120 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL)) | |
121 { | |
122 gaim_debug_error("connection", "Can not connect to account %s without " | |
123 "a password.\n", gaim_account_get_username(account)); | |
124 return; | |
125 } | |
126 } | |
127 | |
128 gc = g_new0(GaimConnection, 1); | |
129 GAIM_DBUS_REGISTER_POINTER(gc, GaimConnection); | |
130 | |
131 gc->prpl = prpl; | |
132 if ((password != NULL) && (*password != '\0')) | |
133 gc->password = g_strdup(password); | |
134 gaim_connection_set_account(gc, account); | |
135 gaim_connection_set_state(gc, GAIM_CONNECTING); | |
136 connections = g_list_append(connections, gc); | |
137 gaim_account_set_connection(account, gc); | |
138 | |
139 gaim_signal_emit(gaim_connections_get_handle(), "signing-on", gc); | |
140 | |
141 if (regist) | |
142 { | |
143 gaim_debug_info("connection", "Registering. gc = %p\n", gc); | |
144 | |
145 /* set this so we don't auto-reconnect after registering */ | |
146 gc->wants_to_die = TRUE; | |
147 | |
148 prpl_info->register_user(account); | |
149 } | |
150 else | |
151 { | |
152 gaim_debug_info("connection", "Connecting. gc = %p\n", gc); | |
153 | |
154 gaim_signal_emit(gaim_accounts_get_handle(), "account-connecting", account); | |
155 prpl_info->login(account); | |
156 } | |
157 } | |
158 | |
159 void | |
160 gaim_connection_destroy(GaimConnection *gc) | |
161 { | |
162 GaimAccount *account; | |
163 #if 0 | |
164 GList *wins; | |
165 #endif | |
166 GaimPluginProtocolInfo *prpl_info = NULL; | |
167 gboolean remove = FALSE; | |
168 | |
169 g_return_if_fail(gc != NULL); | |
170 | |
171 account = gaim_connection_get_account(gc); | |
172 | |
173 gaim_debug_info("connection", "Disconnecting connection %p\n", gc); | |
174 | |
175 if (gaim_connection_get_state(gc) != GAIM_CONNECTING) | |
176 remove = TRUE; | |
177 | |
178 gaim_signal_emit(gaim_connections_get_handle(), "signing-off", gc); | |
179 | |
180 while (gc->buddy_chats) | |
181 { | |
182 GaimConversation *b = gc->buddy_chats->data; | |
183 | |
184 gc->buddy_chats = g_slist_remove(gc->buddy_chats, b); | |
185 gaim_conv_chat_left(GAIM_CONV_CHAT(b)); | |
186 } | |
187 | |
188 update_keepalive(gc, FALSE); | |
189 | |
190 if (gc->prpl != NULL) | |
191 { | |
192 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
193 | |
194 if (prpl_info->close) | |
195 (prpl_info->close)(gc); | |
196 } | |
197 | |
198 connections = g_list_remove(connections, gc); | |
199 | |
200 gaim_connection_set_state(gc, GAIM_DISCONNECTED); | |
201 | |
202 if (remove) | |
203 gaim_blist_remove_account(account); | |
204 | |
205 gaim_signal_emit(gaim_connections_get_handle(), "signed-off", gc); | |
206 | |
207 #if 0 | |
208 /* see comment later in file on if 0'd same code */ | |
209 /* | |
210 * XXX This is a hack! Remove this and replace it with a better event | |
211 * notification system. | |
212 */ | |
213 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
214 GaimConvWindow *win = (GaimConvWindow *)wins->data; | |
215 gaim_conversation_update(gaim_conv_window_get_conversation_at(win, 0), | |
216 GAIM_CONV_ACCOUNT_OFFLINE); | |
217 } | |
218 #endif | |
219 | |
220 gaim_request_close_with_handle(gc); | |
221 gaim_notify_close_with_handle(gc); | |
222 | |
223 gaim_debug_info("connection", "Destroying connection %p\n", gc); | |
224 | |
225 gaim_account_set_connection(account, NULL); | |
226 | |
227 g_free(gc->password); | |
228 g_free(gc->display_name); | |
229 | |
230 if (gc->disconnect_timeout) | |
231 gaim_timeout_remove(gc->disconnect_timeout); | |
232 | |
233 GAIM_DBUS_UNREGISTER_POINTER(gc); | |
234 g_free(gc); | |
235 } | |
236 | |
237 /* | |
238 * d:)->-< | |
239 * | |
240 * d:O-\-< | |
241 * | |
242 * d:D-/-< | |
243 * | |
244 * d8D->-< DANCE! | |
245 */ | |
246 | |
247 void | |
248 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state) | |
249 { | |
250 GaimConnectionUiOps *ops; | |
251 | |
252 g_return_if_fail(gc != NULL); | |
253 | |
254 if (gc->state == state) | |
255 return; | |
256 | |
257 gc->state = state; | |
258 | |
259 ops = gaim_connections_get_ui_ops(); | |
260 | |
261 if (gc->state == GAIM_CONNECTING) { | |
262 connections_connecting = g_list_append(connections_connecting, gc); | |
263 } | |
264 else { | |
265 connections_connecting = g_list_remove(connections_connecting, gc); | |
266 } | |
267 | |
268 if (gc->state == GAIM_CONNECTED) { | |
269 GaimAccount *account; | |
270 GaimPresence *presence; | |
271 | |
272 account = gaim_connection_get_account(gc); | |
273 presence = gaim_account_get_presence(account); | |
274 | |
275 /* Set the time the account came online */ | |
276 gaim_presence_set_login_time(presence, time(NULL)); | |
277 | |
278 if (gaim_prefs_get_bool("/core/logging/log_system")) | |
279 { | |
280 GaimLog *log = gaim_account_get_log(account, TRUE); | |
281 | |
282 if (log != NULL) | |
283 { | |
284 char *msg = g_strdup_printf(_("+++ %s signed on"), | |
285 gaim_account_get_username(account)); | |
286 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
287 gaim_account_get_username(account), | |
288 gaim_presence_get_login_time(presence), | |
289 msg); | |
290 g_free(msg); | |
291 } | |
292 } | |
293 | |
294 if (ops != NULL && ops->connected != NULL) | |
295 ops->connected(gc); | |
296 | |
297 gaim_blist_add_account(account); | |
298 | |
299 gaim_signal_emit(gaim_connections_get_handle(), "signed-on", gc); | |
300 | |
301 serv_set_permit_deny(gc); | |
302 | |
303 update_keepalive(gc, TRUE); | |
304 | |
305 if (gaim_account_get_user_info(account) != NULL) | |
306 serv_set_info(gc, gaim_account_get_user_info(account)); | |
307 } | |
308 else if (gc->state == GAIM_DISCONNECTED) { | |
309 GaimAccount *account = gaim_connection_get_account(gc); | |
310 | |
311 if (gaim_prefs_get_bool("/core/logging/log_system")) | |
312 { | |
313 GaimLog *log = gaim_account_get_log(account, FALSE); | |
314 | |
315 if (log != NULL) | |
316 { | |
317 char *msg = g_strdup_printf(_("+++ %s signed off"), | |
318 gaim_account_get_username(account)); | |
319 gaim_log_write(log, GAIM_MESSAGE_SYSTEM, | |
320 gaim_account_get_username(account), time(NULL), | |
321 msg); | |
322 g_free(msg); | |
323 } | |
324 } | |
325 | |
326 gaim_account_destroy_log(account); | |
327 | |
328 if (ops != NULL && ops->disconnected != NULL) | |
329 ops->disconnected(gc); | |
330 } | |
331 } | |
332 | |
333 void | |
334 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account) | |
335 { | |
336 g_return_if_fail(gc != NULL); | |
337 g_return_if_fail(account != NULL); | |
338 | |
339 gc->account = account; | |
340 } | |
341 | |
342 void | |
343 gaim_connection_set_display_name(GaimConnection *gc, const char *name) | |
344 { | |
345 g_return_if_fail(gc != NULL); | |
346 | |
347 g_free(gc->display_name); | |
348 gc->display_name = g_strdup(name); | |
349 } | |
350 | |
351 GaimConnectionState | |
352 gaim_connection_get_state(const GaimConnection *gc) | |
353 { | |
354 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED); | |
355 | |
356 return gc->state; | |
357 } | |
358 | |
359 GaimAccount * | |
360 gaim_connection_get_account(const GaimConnection *gc) | |
361 { | |
362 g_return_val_if_fail(gc != NULL, NULL); | |
363 | |
364 return gc->account; | |
365 } | |
366 | |
367 const char * | |
368 gaim_connection_get_password(const GaimConnection *gc) | |
369 { | |
370 g_return_val_if_fail(gc != NULL, NULL); | |
371 | |
372 return gc->password; | |
373 } | |
374 | |
375 const char * | |
376 gaim_connection_get_display_name(const GaimConnection *gc) | |
377 { | |
378 g_return_val_if_fail(gc != NULL, NULL); | |
379 | |
380 return gc->display_name; | |
381 } | |
382 | |
383 void | |
384 gaim_connection_update_progress(GaimConnection *gc, const char *text, | |
385 size_t step, size_t count) | |
386 { | |
387 GaimConnectionUiOps *ops; | |
388 | |
389 g_return_if_fail(gc != NULL); | |
390 g_return_if_fail(text != NULL); | |
391 g_return_if_fail(step < count); | |
392 g_return_if_fail(count > 1); | |
393 | |
394 ops = gaim_connections_get_ui_ops(); | |
395 | |
396 if (ops != NULL && ops->connect_progress != NULL) | |
397 ops->connect_progress(gc, text, step, count); | |
398 } | |
399 | |
400 void | |
401 gaim_connection_notice(GaimConnection *gc, const char *text) | |
402 { | |
403 GaimConnectionUiOps *ops; | |
404 | |
405 g_return_if_fail(gc != NULL); | |
406 g_return_if_fail(text != NULL); | |
407 | |
408 ops = gaim_connections_get_ui_ops(); | |
409 | |
410 if (ops != NULL && ops->notice != NULL) | |
411 ops->notice(gc, text); | |
412 } | |
413 | |
414 static gboolean | |
415 gaim_connection_disconnect_cb(gpointer data) | |
416 { | |
417 GaimAccount *account = data; | |
418 char *password = g_strdup(gaim_account_get_password(account)); | |
419 gaim_account_disconnect(account); | |
420 gaim_account_set_password(account, password); | |
421 g_free(password); | |
422 return FALSE; | |
423 } | |
424 | |
425 void | |
426 gaim_connection_error(GaimConnection *gc, const char *text) | |
427 { | |
428 GaimConnectionUiOps *ops; | |
429 | |
430 g_return_if_fail(gc != NULL); | |
431 g_return_if_fail(text != NULL); | |
432 | |
433 /* If we've already got one error, we don't need any more */ | |
434 if (gc->disconnect_timeout) | |
435 return; | |
436 | |
437 ops = gaim_connections_get_ui_ops(); | |
438 | |
439 if (ops != NULL) { | |
440 if (ops->report_disconnect != NULL) | |
441 ops->report_disconnect(gc, text); | |
442 } | |
443 | |
444 gc->disconnect_timeout = gaim_timeout_add(0, gaim_connection_disconnect_cb, | |
445 gaim_connection_get_account(gc)); | |
446 } | |
447 | |
448 void | |
449 gaim_connections_disconnect_all(void) | |
450 { | |
451 GList *l; | |
452 GaimConnection *gc; | |
453 | |
454 while ((l = gaim_connections_get_all()) != NULL) { | |
455 gc = l->data; | |
456 gc->wants_to_die = TRUE; | |
457 gaim_account_disconnect(gc->account); | |
458 } | |
459 } | |
460 | |
461 GList * | |
462 gaim_connections_get_all(void) | |
463 { | |
464 return connections; | |
465 } | |
466 | |
467 GList * | |
468 gaim_connections_get_connecting(void) | |
469 { | |
470 return connections_connecting; | |
471 } | |
472 | |
473 void | |
474 gaim_connections_set_ui_ops(GaimConnectionUiOps *ops) | |
475 { | |
476 connection_ui_ops = ops; | |
477 } | |
478 | |
479 GaimConnectionUiOps * | |
480 gaim_connections_get_ui_ops(void) | |
481 { | |
482 return connection_ui_ops; | |
483 } | |
484 | |
485 void | |
486 gaim_connections_init(void) | |
487 { | |
488 void *handle = gaim_connections_get_handle(); | |
489 | |
490 gaim_signal_register(handle, "signing-on", | |
491 gaim_marshal_VOID__POINTER, NULL, 1, | |
492 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
493 GAIM_SUBTYPE_CONNECTION)); | |
494 | |
495 gaim_signal_register(handle, "signed-on", | |
496 gaim_marshal_VOID__POINTER, NULL, 1, | |
497 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
498 GAIM_SUBTYPE_CONNECTION)); | |
499 | |
500 gaim_signal_register(handle, "signing-off", | |
501 gaim_marshal_VOID__POINTER, NULL, 1, | |
502 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
503 GAIM_SUBTYPE_CONNECTION)); | |
504 | |
505 gaim_signal_register(handle, "signed-off", | |
506 gaim_marshal_VOID__POINTER, NULL, 1, | |
507 gaim_value_new(GAIM_TYPE_SUBTYPE, | |
508 GAIM_SUBTYPE_CONNECTION)); | |
509 } | |
510 | |
511 void | |
512 gaim_connections_uninit(void) | |
513 { | |
514 gaim_signals_unregister_by_instance(gaim_connections_get_handle()); | |
515 } | |
516 | |
517 void * | |
518 gaim_connections_get_handle(void) | |
519 { | |
520 return &connections_handle; | |
521 } |