Mercurial > pidgin
annotate src/connection.c @ 5605:4fa8ca4f4259
[gaim-migrate @ 6009]
i'm a search-and-replace machine!
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 31 May 2003 15:40:32 +0000 |
parents | 3a9b54f260e3 |
children | 6500a6c8d679 |
rev | line source |
---|---|
5563 | 1 /** |
2 * @file connection.c Connection API | |
3 * @ingroup core | |
4 * | |
5 * gaim | |
6 * | |
7 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 */ | |
23 #include "connection.h" | |
24 | |
25 static GList *connections = NULL; | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
26 static GList *connections_connecting = NULL; |
5563 | 27 static GaimConnectionUiOps *connection_ui_ops = NULL; |
28 | |
29 GaimConnection * | |
30 gaim_connection_new(GaimAccount *account) | |
31 { | |
32 GaimConnection *gc; | |
33 | |
34 g_return_val_if_fail(account != NULL, NULL); | |
35 | |
36 gc = g_new0(GaimConnection, 1); | |
37 | |
38 gc->prpl = gaim_find_prpl(gaim_account_get_protocol(account)); | |
39 | |
40 gaim_connection_set_account(gc, account); | |
41 gaim_account_set_connection(account, gc); | |
42 | |
43 connections = g_list_append(connections, gc); | |
44 | |
45 return gc; | |
46 } | |
47 | |
48 void | |
49 gaim_connection_destroy(GaimConnection *gc) | |
50 { | |
51 g_return_if_fail(gc != NULL); | |
52 | |
53 if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) { | |
54 gaim_connection_disconnect(gc); | |
55 | |
56 return; | |
57 } | |
58 | |
59 if (gc->display_name != NULL) | |
60 g_free(gc->display_name); | |
61 | |
62 if (gc->away != NULL) | |
63 g_free(gc->away); | |
64 | |
65 if (gc->away_state != NULL) | |
66 g_free(gc->away_state); | |
67 | |
68 connections = g_list_remove(connections, gc); | |
69 | |
70 g_free(gc); | |
71 } | |
72 | |
73 void | |
74 gaim_connection_connect(GaimConnection *gc) | |
75 { | |
76 GaimAccount *account; | |
77 GaimConnectionUiOps *ops; | |
78 GaimPluginProtocolInfo *prpl_info = NULL; | |
79 | |
80 g_return_if_fail(gc != NULL); | |
81 | |
82 ops = gaim_get_connection_ui_ops(); | |
83 | |
84 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
85 | |
86 account = gaim_connection_get_account(gc); | |
87 | |
88 if ((prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
89 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) && | |
90 gaim_account_get_password(account) == NULL) { | |
91 | |
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
92 gaim_debug(GAIM_DEBUG_INFO, "connection", "Requestin password\n"); |
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
93 |
5563 | 94 if (ops != NULL && ops->request_pass != NULL) |
95 ops->request_pass(gc); | |
96 | |
97 return; | |
98 } | |
99 | |
100 gaim_connection_set_state(gc, GAIM_CONNECTING); | |
101 | |
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
102 gaim_debug(GAIM_DEBUG_INFO, "connection", "Calling serv_login\n"); |
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
103 |
5563 | 104 serv_login(account); |
105 } | |
106 | |
107 void | |
108 gaim_connection_disconnect(GaimConnection *gc) | |
109 { | |
110 GList *wins; | |
111 | |
112 g_return_if_fail(gc != NULL); | |
113 g_return_if_fail(gaim_connection_get_state(gc) != GAIM_DISCONNECTED); | |
114 | |
115 if (gaim_connection_get_state(gc) != GAIM_CONNECTING) | |
116 gaim_blist_remove_account(gaim_connection_get_account(gc)); | |
117 | |
118 gaim_event_broadcast(event_signoff, gc); | |
119 system_log(log_signoff, gc, NULL, OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
120 | |
121 serv_close(gc); | |
122 | |
123 gaim_connection_set_state(gc, GAIM_DISCONNECTED); | |
124 | |
125 /* XXX Evil UI stuff!! */ | |
126 do_away_menu(); | |
127 do_proto_menu(); | |
128 | |
129 /* | |
130 * XXX This is a hack! Remove this and replace it with a better event | |
131 * notification system. | |
132 */ | |
133 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
134 struct gaim_window *win = (struct gaim_window *)wins->data; | |
135 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), | |
136 GAIM_CONV_ACCOUNT_OFFLINE); | |
137 } | |
138 | |
139 gaim_request_close_with_handle(gc); | |
140 gaim_notify_close_with_handle(gc); | |
141 | |
142 update_privacy_connections(); | |
143 | |
144 /* XXX More UI stuff! */ | |
145 if (connections != NULL) | |
146 return; | |
147 | |
148 destroy_all_dialogs(); | |
149 gaim_blist_destroy(); | |
150 | |
151 show_login(); | |
152 } | |
153 | |
154 /* | |
155 * d:)->-< | |
156 * | |
157 * d:O-\-< | |
158 * | |
159 * d:D-/-< | |
160 * | |
161 * d8D->-< DANCE! | |
162 */ | |
163 | |
164 void | |
165 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state) | |
166 { | |
167 g_return_if_fail(gc != NULL); | |
168 | |
169 gc->state = state; | |
170 | |
171 if (gc->state == GAIM_CONNECTED) { | |
172 GaimConnectionUiOps *ops = gaim_get_connection_ui_ops(); | |
173 GaimBlistNode *gnode,*bnode; | |
174 GList *wins; | |
175 GList *add_buds=NULL; | |
176 | |
177 /* Set the time the account came online */ | |
178 time(&gc->login_time); | |
179 | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
180 connections_connecting = g_list_append(connections_connecting, gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
181 |
5563 | 182 if (ops != NULL && ops->connected != NULL) |
183 ops->connected(gc); | |
184 | |
185 gaim_blist_show(); | |
186 gaim_blist_add_account(gc->account); | |
187 | |
188 /* I hate this. -- ChipX86 */ | |
189 gaim_setup(gc); | |
190 | |
191 /* | |
192 * XXX This is a hack! Remove this and replace it with a better event | |
193 * notification system. | |
194 */ | |
195 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
196 struct gaim_window *win = (struct gaim_window *)wins->data; | |
197 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), | |
198 GAIM_CONV_ACCOUNT_ONLINE); | |
199 } | |
200 | |
201 gaim_event_broadcast(event_signon, gc); | |
202 system_log(log_signon, gc, NULL, | |
203 OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
204 | |
205 #if 0 | |
206 /* away option given? */ | |
207 if (opt_away) { | |
208 away_on_login(opt_away_arg); | |
209 /* don't do it again */ | |
210 opt_away = 0; | |
211 } else if (awaymessage) { | |
212 serv_set_away(gc, GAIM_AWAY_CUSTOM, awaymessage->message); | |
213 } | |
214 if (opt_away_arg != NULL) { | |
215 g_free(opt_away_arg); | |
216 opt_away_arg = NULL; | |
217 } | |
218 #endif | |
219 | |
220 /* let the prpl know what buddies we pulled out of the local list */ | |
221 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) { | |
222 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
223 continue; | |
224 for(bnode = gnode->child; bnode; bnode = bnode->next) { | |
225 if(GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
226 struct buddy *b = (struct buddy *)bnode; | |
227 if(b->account == gc->account) { | |
228 add_buds = g_list_append(add_buds, b->name); | |
229 } | |
230 } | |
231 } | |
232 } | |
233 | |
234 if(add_buds) { | |
235 serv_add_buddies(gc, add_buds); | |
236 g_list_free(add_buds); | |
237 } | |
238 | |
239 serv_set_permit_deny(gc); | |
240 } | |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
241 else { |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
242 connections_connecting = g_list_remove(connections_connecting, gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
243 } |
5563 | 244 } |
245 | |
246 void | |
247 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account) | |
248 { | |
249 g_return_if_fail(gc != NULL); | |
250 g_return_if_fail(account != NULL); | |
251 | |
252 gc->account = account; | |
253 } | |
254 | |
255 void | |
256 gaim_connection_set_display_name(GaimConnection *gc, const char *name) | |
257 { | |
258 g_return_if_fail(gc != NULL); | |
259 | |
260 if (gc->display_name != NULL) | |
261 g_free(gc->display_name); | |
262 | |
263 gc->display_name = (name == NULL ? NULL : g_strdup(name)); | |
264 } | |
265 | |
266 GaimConnectionState | |
267 gaim_connection_get_state(const GaimConnection *gc) | |
268 { | |
269 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED); | |
270 | |
271 return gc->state; | |
272 } | |
273 | |
274 GaimAccount * | |
275 gaim_connection_get_account(const GaimConnection *gc) | |
276 { | |
277 g_return_val_if_fail(gc != NULL, NULL); | |
278 | |
279 return gc->account; | |
280 } | |
281 | |
282 const char * | |
283 gaim_connection_get_display_name(const GaimConnection *gc) | |
284 { | |
285 g_return_val_if_fail(gc != NULL, NULL); | |
286 | |
287 return gc->display_name; | |
288 } | |
289 | |
290 void | |
291 gaim_connection_update_progress(GaimConnection *gc, const char *text, | |
292 size_t step, size_t count) | |
293 { | |
294 GaimConnectionUiOps *ops; | |
295 | |
296 g_return_if_fail(gc != NULL); | |
297 g_return_if_fail(text != NULL); | |
298 g_return_if_fail(step < count); | |
299 g_return_if_fail(count > 1); | |
300 | |
301 ops = gaim_get_connection_ui_ops(); | |
302 | |
303 if (ops != NULL && ops->connect_progress != NULL) | |
304 ops->connect_progress(gc, text, step, count); | |
305 } | |
306 | |
307 void | |
5571
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
308 gaim_connection_notice(GaimConnection *gc, const char *text) |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
309 { |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
310 GaimConnectionUiOps *ops; |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
311 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
312 g_return_if_fail(gc != NULL); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
313 g_return_if_fail(text != NULL); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
314 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
315 ops = gaim_get_connection_ui_ops(); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
316 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
317 if (ops != NULL && ops->notice != NULL) |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
318 ops->notice(gc, text); |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
319 } |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
320 |
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
321 void |
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
322 gaim_connection_error(GaimConnection *gc, const char *text) |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
323 { |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
324 GaimConnectionUiOps *ops; |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
325 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
326 g_return_if_fail(gc != NULL); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
327 g_return_if_fail(text != NULL); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
328 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
329 ops = gaim_get_connection_ui_ops(); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
330 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
331 gaim_connection_disconnect(gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
332 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
333 if (ops != NULL && ops->disconnected != NULL) |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
334 ops->disconnected(gc, text); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
335 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
336 gaim_connection_destroy(gc); |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
337 } |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
338 |
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
339 void |
5563 | 340 gaim_connections_disconnect_all(void) |
341 { | |
342 GList *l; | |
343 | |
344 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
345 gaim_connection_destroy(l->data); | |
346 } | |
347 | |
348 GList * | |
349 gaim_connections_get_all(void) | |
350 { | |
351 return connections; | |
352 } | |
353 | |
354 void | |
355 gaim_set_connection_ui_ops(GaimConnectionUiOps *ops) | |
356 { | |
357 connection_ui_ops = ops; | |
358 } | |
359 | |
360 GaimConnectionUiOps * | |
361 gaim_get_connection_ui_ops(void) | |
362 { | |
363 return connection_ui_ops; | |
364 } | |
365 |