Mon, 02 Jun 2003 21:51:06 +0000
[gaim-migrate @ 6094]
I've been meaning to do this for a LONG time. The conversation API now
follows the naming convention of the rest of the new APIs. I'll get some
g_return_*_if_fail() checks in there soon.
| 5563 | 1 | /** |
| 2 | * @file connection.c Connection API | |
| 3 | * @ingroup core | |
| 4 | * | |
| 5 | * gaim | |
| 6 | * | |
| 7 | * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
| 5631 | 8 | * |
| 5563 | 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
1779a1bfbdb8
[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 | return gc; | |
| 44 | } | |
| 45 | ||
| 46 | void | |
| 47 | gaim_connection_destroy(GaimConnection *gc) | |
| 48 | { | |
| 49 | g_return_if_fail(gc != NULL); | |
| 50 | ||
| 51 | if (gaim_connection_get_state(gc) != GAIM_DISCONNECTED) { | |
| 52 | gaim_connection_disconnect(gc); | |
| 53 | ||
| 54 | return; | |
| 55 | } | |
| 56 | ||
| 57 | if (gc->display_name != NULL) | |
| 58 | g_free(gc->display_name); | |
| 59 | ||
| 60 | if (gc->away != NULL) | |
| 61 | g_free(gc->away); | |
| 62 | ||
| 63 | if (gc->away_state != NULL) | |
| 64 | g_free(gc->away_state); | |
| 65 | ||
| 66 | g_free(gc); | |
| 67 | } | |
| 68 | ||
| 69 | void | |
| 70 | gaim_connection_connect(GaimConnection *gc) | |
| 71 | { | |
| 72 | GaimAccount *account; | |
| 73 | GaimConnectionUiOps *ops; | |
| 74 | GaimPluginProtocolInfo *prpl_info = NULL; | |
| 75 | ||
| 76 | g_return_if_fail(gc != NULL); | |
| 77 | ||
| 78 | ops = gaim_get_connection_ui_ops(); | |
| 79 | ||
| 80 | prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl); | |
| 81 | ||
| 82 | account = gaim_connection_get_account(gc); | |
| 83 | ||
| 84 | if ((prpl_info->options & OPT_PROTO_NO_PASSWORD) && | |
| 85 | !(prpl_info->options & OPT_PROTO_PASSWORD_OPTIONAL) && | |
| 86 | gaim_account_get_password(account) == NULL) { | |
| 87 | ||
|
5581
646cda748437
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
88 | gaim_debug(GAIM_DEBUG_INFO, "connection", "Requestin password\n"); |
|
646cda748437
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
89 | |
| 5563 | 90 | if (ops != NULL && ops->request_pass != NULL) |
| 91 | ops->request_pass(gc); | |
| 92 | ||
| 93 | return; | |
| 94 | } | |
| 95 | ||
| 96 | gaim_connection_set_state(gc, GAIM_CONNECTING); | |
| 97 | ||
|
5581
646cda748437
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
98 | gaim_debug(GAIM_DEBUG_INFO, "connection", "Calling serv_login\n"); |
|
646cda748437
[gaim-migrate @ 5985]
Christian Hammond <chipx86@chipx86.com>
parents:
5571
diff
changeset
|
99 | |
|
5623
9c9bb883154b
[gaim-migrate @ 6030]
Christian Hammond <chipx86@chipx86.com>
parents:
5622
diff
changeset
|
100 | connections = g_list_append(connections, gc); |
|
9c9bb883154b
[gaim-migrate @ 6030]
Christian Hammond <chipx86@chipx86.com>
parents:
5622
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
3180ced42da4
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
120 | connections = g_list_remove(connections, gc); |
|
3180ced42da4
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
121 | |
|
5615
2eb715cbbd9b
[gaim-migrate @ 6022]
Christian Hammond <chipx86@chipx86.com>
parents:
5581
diff
changeset
|
122 | gaim_event_broadcast(event_signoff, gc); |
|
2eb715cbbd9b
[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); |
|
2eb715cbbd9b
[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) { | |
|
5676
d3c2fdaf4821
[gaim-migrate @ 6094]
Christian Hammond <chipx86@chipx86.com>
parents:
5631
diff
changeset
|
130 | GaimWindow *win = (GaimWindow *)wins->data; |
| 5563 | 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
3180ced42da4
[gaim-migrate @ 6029]
Christian Hammond <chipx86@chipx86.com>
parents:
5615
diff
changeset
|
140 | gaim_connection_destroy(gc); |
|
3180ced42da4
[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
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
178 | connections_connecting = g_list_append(connections_connecting, gc); |
|
1779a1bfbdb8
[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) { | |
|
5676
d3c2fdaf4821
[gaim-migrate @ 6094]
Christian Hammond <chipx86@chipx86.com>
parents:
5631
diff
changeset
|
194 | GaimWindow *win = (GaimWindow *)wins->data; |
| 5563 | 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
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
239 | else { |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
240 | connections_connecting = g_list_remove(connections_connecting, gc); |
|
1779a1bfbdb8
[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
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
306 | gaim_connection_notice(GaimConnection *gc, const char *text) |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
307 | { |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
308 | GaimConnectionUiOps *ops; |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
309 | |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
310 | g_return_if_fail(gc != NULL); |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
311 | g_return_if_fail(text != NULL); |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
312 | |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
313 | ops = gaim_get_connection_ui_ops(); |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
314 | |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
315 | if (ops != NULL && ops->notice != NULL) |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
316 | ops->notice(gc, text); |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
317 | } |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
318 | |
|
b709464f507e
[gaim-migrate @ 5973]
Christian Hammond <chipx86@chipx86.com>
parents:
5564
diff
changeset
|
319 | void |
|
5564
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
320 | gaim_connection_error(GaimConnection *gc, const char *text) |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
321 | { |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
322 | GaimConnectionUiOps *ops; |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
323 | |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
324 | g_return_if_fail(gc != NULL); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
325 | g_return_if_fail(text != NULL); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
326 | |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
327 | ops = gaim_get_connection_ui_ops(); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
328 | |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
329 | gaim_connection_disconnect(gc); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
330 | |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
331 | if (ops != NULL && ops->disconnected != NULL) |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
332 | ops->disconnected(gc, text); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
333 | |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
334 | gaim_connection_destroy(gc); |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
335 | } |
|
1779a1bfbdb8
[gaim-migrate @ 5966]
Christian Hammond <chipx86@chipx86.com>
parents:
5563
diff
changeset
|
336 | |
|
1779a1bfbdb8
[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 | ||
|
5624
b1240853c0e7
[gaim-migrate @ 6031]
Christian Hammond <chipx86@chipx86.com>
parents:
5623
diff
changeset
|
342 | while ((l = gaim_connections_get_all()) != NULL) |
| 5563 | 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 |