Mercurial > pidgin
annotate src/connection.c @ 5622:70ae81fc802f
[gaim-migrate @ 6029]
Fixed one bug.. we don't really want a disconnected connection in the list.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Sun, 01 Jun 2003 01:42:09 +0000 |
| parents | 6500a6c8d679 |
| children | 8c895d80159a |
| 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 g_free(gc); | |
| 69 } | |
| 70 | |
| 71 void | |
| 72 gaim_connection_connect(GaimConnection *gc) | |
| 73 { | |
| 74 GaimAccount *account; | |
| 75 GaimConnectionUiOps *ops; | |
| 76 GaimPluginProtocolInfo *prpl_info = NULL; | |
| 77 | |
| 78 g_return_if_fail(gc != NULL); | |
| 79 | |
| 80 ops = gaim_get_connection_ui_ops(); | |
| 81 | |
| 82 prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
| 83 | |
| 84 account = gaim_connection_get_account(gc); | |
| 85 | |
| 86 if ((prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
| 87 !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) && | |
| 88 gaim_account_get_password(account) == NULL) { | |
| 89 | |
|
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
90 gaim_debug(GAIM_DEBUG_INFO, "connection", "Requestin password\n"); |
|
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
91 |
| 5563 | 92 if (ops != NULL && ops->request_pass != NULL) |
| 93 ops->request_pass(gc); | |
| 94 | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 gaim_connection_set_state(gc, GAIM_CONNECTING); | |
| 99 | |
|
5581
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
100 gaim_debug(GAIM_DEBUG_INFO, "connection", "Calling serv_login\n"); |
|
3a9b54f260e3
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
101 |
| 5563 | 102 serv_login(account); |
| 103 } | |
| 104 | |
| 105 void | |
| 106 gaim_connection_disconnect(GaimConnection *gc) | |
| 107 { | |
| 108 GList *wins; | |
| 109 | |
| 110 g_return_if_fail(gc != NULL); | |
| 111 g_return_if_fail(gaim_connection_get_state(gc) != GAIM_DISCONNECTED); | |
| 112 | |
| 113 if (gaim_connection_get_state(gc) != GAIM_CONNECTING) | |
| 114 gaim_blist_remove_account(gaim_connection_get_account(gc)); | |
| 115 | |
| 116 serv_close(gc); | |
| 117 | |
| 118 gaim_connection_set_state(gc, GAIM_DISCONNECTED); | |
| 119 | |
|
5622
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
120 connections = g_list_remove(connections, gc); |
|
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
121 |
|
5615
6500a6c8d679
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5581
diff
changeset
|
122 gaim_event_broadcast(event_signoff, gc); |
|
6500a6c8d679
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5581
diff
changeset
|
123 system_log(log_signoff, gc, NULL, OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); |
|
6500a6c8d679
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5581
diff
changeset
|
124 |
| 5563 | 125 /* |
| 126 * XXX This is a hack! Remove this and replace it with a better event | |
| 127 * notification system. | |
| 128 */ | |
| 129 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
| 130 struct gaim_window *win = (struct gaim_window *)wins->data; | |
| 131 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), | |
| 132 GAIM_CONV_ACCOUNT_OFFLINE); | |
| 133 } | |
| 134 | |
| 135 gaim_request_close_with_handle(gc); | |
| 136 gaim_notify_close_with_handle(gc); | |
| 137 | |
| 138 update_privacy_connections(); | |
| 139 | |
|
5622
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
140 gaim_connection_destroy(gc); |
|
70ae81fc802f
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
141 |
| 5563 | 142 /* XXX More UI stuff! */ |
| 143 if (connections != NULL) | |
| 144 return; | |
| 145 | |
| 146 destroy_all_dialogs(); | |
| 147 gaim_blist_destroy(); | |
| 148 | |
| 149 show_login(); | |
| 150 } | |
| 151 | |
| 152 /* | |
| 153 * d:)->-< | |
| 154 * | |
| 155 * d:O-\-< | |
| 156 * | |
| 157 * d:D-/-< | |
| 158 * | |
| 159 * d8D->-< DANCE! | |
| 160 */ | |
| 161 | |
| 162 void | |
| 163 gaim_connection_set_state(GaimConnection *gc, GaimConnectionState state) | |
| 164 { | |
| 165 g_return_if_fail(gc != NULL); | |
| 166 | |
| 167 gc->state = state; | |
| 168 | |
| 169 if (gc->state == GAIM_CONNECTED) { | |
| 170 GaimConnectionUiOps *ops = gaim_get_connection_ui_ops(); | |
| 171 GaimBlistNode *gnode,*bnode; | |
| 172 GList *wins; | |
| 173 GList *add_buds=NULL; | |
| 174 | |
| 175 /* Set the time the account came online */ | |
| 176 time(&gc->login_time); | |
| 177 | |
|
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
178 connections_connecting = g_list_append(connections_connecting, gc); |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
179 |
| 5563 | 180 if (ops != NULL && ops->connected != NULL) |
| 181 ops->connected(gc); | |
| 182 | |
| 183 gaim_blist_show(); | |
| 184 gaim_blist_add_account(gc->account); | |
| 185 | |
| 186 /* I hate this. -- ChipX86 */ | |
| 187 gaim_setup(gc); | |
| 188 | |
| 189 /* | |
| 190 * XXX This is a hack! Remove this and replace it with a better event | |
| 191 * notification system. | |
| 192 */ | |
| 193 for (wins = gaim_get_windows(); wins != NULL; wins = wins->next) { | |
| 194 struct gaim_window *win = (struct gaim_window *)wins->data; | |
| 195 gaim_conversation_update(gaim_window_get_conversation_at(win, 0), | |
| 196 GAIM_CONV_ACCOUNT_ONLINE); | |
| 197 } | |
| 198 | |
| 199 gaim_event_broadcast(event_signon, gc); | |
| 200 system_log(log_signon, gc, NULL, | |
| 201 OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
| 202 | |
| 203 #if 0 | |
| 204 /* away option given? */ | |
| 205 if (opt_away) { | |
| 206 away_on_login(opt_away_arg); | |
| 207 /* don't do it again */ | |
| 208 opt_away = 0; | |
| 209 } else if (awaymessage) { | |
| 210 serv_set_away(gc, GAIM_AWAY_CUSTOM, awaymessage->message); | |
| 211 } | |
| 212 if (opt_away_arg != NULL) { | |
| 213 g_free(opt_away_arg); | |
| 214 opt_away_arg = NULL; | |
| 215 } | |
| 216 #endif | |
| 217 | |
| 218 /* let the prpl know what buddies we pulled out of the local list */ | |
| 219 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) { | |
| 220 if(!GAIM_BLIST_NODE_IS_GROUP(gnode)) | |
| 221 continue; | |
| 222 for(bnode = gnode->child; bnode; bnode = bnode->next) { | |
| 223 if(GAIM_BLIST_NODE_IS_BUDDY(bnode)) { | |
| 224 struct buddy *b = (struct buddy *)bnode; | |
| 225 if(b->account == gc->account) { | |
| 226 add_buds = g_list_append(add_buds, b->name); | |
| 227 } | |
| 228 } | |
| 229 } | |
| 230 } | |
| 231 | |
| 232 if(add_buds) { | |
| 233 serv_add_buddies(gc, add_buds); | |
| 234 g_list_free(add_buds); | |
| 235 } | |
| 236 | |
| 237 serv_set_permit_deny(gc); | |
| 238 } | |
|
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
239 else { |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
240 connections_connecting = g_list_remove(connections_connecting, gc); |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
241 } |
| 5563 | 242 } |
| 243 | |
| 244 void | |
| 245 gaim_connection_set_account(GaimConnection *gc, GaimAccount *account) | |
| 246 { | |
| 247 g_return_if_fail(gc != NULL); | |
| 248 g_return_if_fail(account != NULL); | |
| 249 | |
| 250 gc->account = account; | |
| 251 } | |
| 252 | |
| 253 void | |
| 254 gaim_connection_set_display_name(GaimConnection *gc, const char *name) | |
| 255 { | |
| 256 g_return_if_fail(gc != NULL); | |
| 257 | |
| 258 if (gc->display_name != NULL) | |
| 259 g_free(gc->display_name); | |
| 260 | |
| 261 gc->display_name = (name == NULL ? NULL : g_strdup(name)); | |
| 262 } | |
| 263 | |
| 264 GaimConnectionState | |
| 265 gaim_connection_get_state(const GaimConnection *gc) | |
| 266 { | |
| 267 g_return_val_if_fail(gc != NULL, GAIM_DISCONNECTED); | |
| 268 | |
| 269 return gc->state; | |
| 270 } | |
| 271 | |
| 272 GaimAccount * | |
| 273 gaim_connection_get_account(const GaimConnection *gc) | |
| 274 { | |
| 275 g_return_val_if_fail(gc != NULL, NULL); | |
| 276 | |
| 277 return gc->account; | |
| 278 } | |
| 279 | |
| 280 const char * | |
| 281 gaim_connection_get_display_name(const GaimConnection *gc) | |
| 282 { | |
| 283 g_return_val_if_fail(gc != NULL, NULL); | |
| 284 | |
| 285 return gc->display_name; | |
| 286 } | |
| 287 | |
| 288 void | |
| 289 gaim_connection_update_progress(GaimConnection *gc, const char *text, | |
| 290 size_t step, size_t count) | |
| 291 { | |
| 292 GaimConnectionUiOps *ops; | |
| 293 | |
| 294 g_return_if_fail(gc != NULL); | |
| 295 g_return_if_fail(text != NULL); | |
| 296 g_return_if_fail(step < count); | |
| 297 g_return_if_fail(count > 1); | |
| 298 | |
| 299 ops = gaim_get_connection_ui_ops(); | |
| 300 | |
| 301 if (ops != NULL && ops->connect_progress != NULL) | |
| 302 ops->connect_progress(gc, text, step, count); | |
| 303 } | |
| 304 | |
| 305 void | |
|
5571
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
306 gaim_connection_notice(GaimConnection *gc, const char *text) |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
307 { |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
308 GaimConnectionUiOps *ops; |
|
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 g_return_if_fail(gc != NULL); |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
311 g_return_if_fail(text != NULL); |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
312 |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
313 ops = gaim_get_connection_ui_ops(); |
|
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 if (ops != NULL && ops->notice != NULL) |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
316 ops->notice(gc, text); |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
317 } |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
318 |
|
113090160626
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
319 void |
|
5564
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
320 gaim_connection_error(GaimConnection *gc, const char *text) |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
321 { |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
322 GaimConnectionUiOps *ops; |
|
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 g_return_if_fail(gc != NULL); |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
325 g_return_if_fail(text != NULL); |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
326 |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
327 ops = gaim_get_connection_ui_ops(); |
|
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 gaim_connection_disconnect(gc); |
|
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 if (ops != NULL && ops->disconnected != NULL) |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
332 ops->disconnected(gc, text); |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
333 |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
334 gaim_connection_destroy(gc); |
|
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 |
|
187c740f2a4e
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
337 void |
| 5563 | 338 gaim_connections_disconnect_all(void) |
| 339 { | |
| 340 GList *l; | |
| 341 | |
| 342 for (l = gaim_connections_get_all(); l != NULL; l = l->next) | |
| 343 gaim_connection_destroy(l->data); | |
| 344 } | |
| 345 | |
| 346 GList * | |
| 347 gaim_connections_get_all(void) | |
| 348 { | |
| 349 return connections; | |
| 350 } | |
| 351 | |
| 352 void | |
| 353 gaim_set_connection_ui_ops(GaimConnectionUiOps *ops) | |
| 354 { | |
| 355 connection_ui_ops = ops; | |
| 356 } | |
| 357 | |
| 358 GaimConnectionUiOps * | |
| 359 gaim_get_connection_ui_ops(void) | |
| 360 { | |
| 361 return connection_ui_ops; | |
| 362 } | |
| 363 |
