Mercurial > pidgin
annotate libgaim/ft.c @ 15090:9cdc8bb39cf2
[gaim-migrate @ 17876]
First stab at attempting to keep track of our rate limiting average
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Sat, 02 Dec 2006 10:43:06 +0000 |
| parents | dff16b654f14 |
| children | 59189ce09f10 |
| rev | line source |
|---|---|
| 14192 | 1 /** |
| 2 * @file ft.c File Transfer API | |
| 3 * | |
| 4 * gaim | |
| 5 * | |
| 6 * Gaim is the legal property of its developers, whose names are too numerous | |
| 7 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 8 * source distribution. | |
| 9 * | |
| 10 * This program is free software; you can redistribute it and/or modify | |
| 11 * it under the terms of the GNU General Public License as published by | |
| 12 * the Free Software Foundation; either version 2 of the License, or | |
| 13 * (at your option) any later version. | |
| 14 * | |
| 15 * This program is distributed in the hope that it will be useful, | |
| 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 * GNU General Public License for more details. | |
| 19 * | |
| 20 * You should have received a copy of the GNU General Public License | |
| 21 * along with this program; if not, write to the Free Software | |
| 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 23 * | |
| 24 */ | |
| 25 #include "internal.h" | |
| 26 #include "ft.h" | |
| 27 #include "network.h" | |
| 28 #include "notify.h" | |
| 29 #include "prefs.h" | |
| 30 #include "proxy.h" | |
| 31 #include "request.h" | |
| 32 #include "util.h" | |
| 33 | |
| 34 static GaimXferUiOps *xfer_ui_ops = NULL; | |
| 35 | |
| 14905 | 36 static int gaim_xfer_choose_file(GaimXfer *xfer); |
| 37 | |
| 14192 | 38 GaimXfer * |
| 39 gaim_xfer_new(GaimAccount *account, GaimXferType type, const char *who) | |
| 40 { | |
| 41 GaimXfer *xfer; | |
| 42 GaimXferUiOps *ui_ops; | |
| 43 | |
| 44 g_return_val_if_fail(type != GAIM_XFER_UNKNOWN, NULL); | |
| 45 g_return_val_if_fail(account != NULL, NULL); | |
| 46 g_return_val_if_fail(who != NULL, NULL); | |
| 47 | |
| 48 xfer = g_new0(GaimXfer, 1); | |
| 49 | |
| 50 xfer->ref = 1; | |
| 51 xfer->type = type; | |
| 52 xfer->account = account; | |
| 53 xfer->who = g_strdup(who); | |
| 54 xfer->ui_ops = gaim_xfers_get_ui_ops(); | |
| 55 xfer->message = NULL; | |
| 56 | |
| 57 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 58 | |
| 59 if (ui_ops != NULL && ui_ops->new_xfer != NULL) | |
| 60 ui_ops->new_xfer(xfer); | |
| 61 | |
| 62 return xfer; | |
| 63 } | |
| 64 | |
| 65 static void | |
| 66 gaim_xfer_destroy(GaimXfer *xfer) | |
| 67 { | |
| 68 GaimXferUiOps *ui_ops; | |
| 69 | |
| 70 g_return_if_fail(xfer != NULL); | |
| 71 | |
| 72 /* Close the file browser, if it's open */ | |
| 73 gaim_request_close_with_handle(xfer); | |
| 74 | |
| 75 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_STARTED) | |
| 76 gaim_xfer_cancel_local(xfer); | |
| 77 | |
| 78 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 79 | |
| 80 if (ui_ops != NULL && ui_ops->destroy != NULL) | |
| 81 ui_ops->destroy(xfer); | |
| 82 | |
| 83 g_free(xfer->who); | |
| 84 g_free(xfer->filename); | |
| 85 g_free(xfer->remote_ip); | |
| 86 g_free(xfer->local_filename); | |
| 87 | |
| 88 g_free(xfer); | |
| 89 } | |
| 90 | |
| 91 void | |
| 92 gaim_xfer_ref(GaimXfer *xfer) | |
| 93 { | |
| 94 g_return_if_fail(xfer != NULL); | |
| 95 | |
| 96 xfer->ref++; | |
| 97 } | |
| 98 | |
| 99 void | |
| 100 gaim_xfer_unref(GaimXfer *xfer) | |
| 101 { | |
| 102 g_return_if_fail(xfer != NULL); | |
| 103 g_return_if_fail(xfer->ref > 0); | |
| 104 | |
| 105 xfer->ref--; | |
| 106 | |
| 107 if (xfer->ref == 0) | |
| 108 gaim_xfer_destroy(xfer); | |
| 109 } | |
| 110 | |
| 111 static void | |
| 112 gaim_xfer_set_status(GaimXfer *xfer, GaimXferStatusType status) | |
| 113 { | |
| 114 g_return_if_fail(xfer != NULL); | |
| 115 | |
| 116 if(xfer->type == GAIM_XFER_SEND) { | |
| 117 switch(status) { | |
| 118 case GAIM_XFER_STATUS_ACCEPTED: | |
| 119 gaim_signal_emit(gaim_xfers_get_handle(), "file-send-accept", xfer); | |
| 120 break; | |
| 121 case GAIM_XFER_STATUS_STARTED: | |
| 122 gaim_signal_emit(gaim_xfers_get_handle(), "file-send-start", xfer); | |
| 123 break; | |
| 124 case GAIM_XFER_STATUS_DONE: | |
| 125 gaim_signal_emit(gaim_xfers_get_handle(), "file-send-complete", xfer); | |
| 126 break; | |
| 127 case GAIM_XFER_STATUS_CANCEL_LOCAL: | |
| 128 case GAIM_XFER_STATUS_CANCEL_REMOTE: | |
| 129 gaim_signal_emit(gaim_xfers_get_handle(), "file-send-cancel", xfer); | |
| 130 break; | |
| 131 default: | |
| 132 break; | |
| 133 } | |
| 134 } else if(xfer->type == GAIM_XFER_RECEIVE) { | |
| 135 switch(status) { | |
| 136 case GAIM_XFER_STATUS_ACCEPTED: | |
| 137 gaim_signal_emit(gaim_xfers_get_handle(), "file-recv-accept", xfer); | |
| 138 break; | |
| 139 case GAIM_XFER_STATUS_STARTED: | |
| 140 gaim_signal_emit(gaim_xfers_get_handle(), "file-recv-start", xfer); | |
| 141 break; | |
| 142 case GAIM_XFER_STATUS_DONE: | |
| 143 gaim_signal_emit(gaim_xfers_get_handle(), "file-recv-complete", xfer); | |
| 144 break; | |
| 145 case GAIM_XFER_STATUS_CANCEL_LOCAL: | |
| 146 case GAIM_XFER_STATUS_CANCEL_REMOTE: | |
| 147 gaim_signal_emit(gaim_xfers_get_handle(), "file-recv-cancel", xfer); | |
| 148 break; | |
| 149 default: | |
| 150 break; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 xfer->status = status; | |
| 155 } | |
| 156 | |
| 157 void gaim_xfer_conversation_write(GaimXfer *xfer, char *message, gboolean is_error) | |
| 158 { | |
| 159 GaimConversation *conv = NULL; | |
| 160 GaimMessageFlags flags = GAIM_MESSAGE_SYSTEM; | |
| 161 char *escaped; | |
| 162 | |
| 163 g_return_if_fail(xfer != NULL); | |
| 164 g_return_if_fail(message != NULL); | |
| 165 | |
| 166 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, xfer->who, | |
| 167 gaim_xfer_get_account(xfer)); | |
| 168 | |
| 169 if (conv == NULL) | |
| 170 return; | |
| 171 | |
| 172 escaped = g_markup_escape_text(message, -1); | |
| 173 | |
| 174 if (is_error) | |
| 175 flags = GAIM_MESSAGE_ERROR; | |
| 176 | |
| 177 gaim_conversation_write(conv, NULL, escaped, flags, time(NULL)); | |
| 178 g_free(escaped); | |
| 179 } | |
| 180 | |
| 181 static void gaim_xfer_show_file_error(GaimXfer *xfer, const char *filename) | |
| 182 { | |
| 183 int err = errno; | |
| 184 gchar *msg = NULL, *utf8; | |
| 185 GaimXferType xfer_type = gaim_xfer_get_type(xfer); | |
| 186 GaimAccount *account = gaim_xfer_get_account(xfer); | |
| 187 | |
| 188 utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL); | |
| 189 switch(xfer_type) { | |
| 190 case GAIM_XFER_SEND: | |
| 191 msg = g_strdup_printf(_("Error reading %s: \n%s.\n"), | |
| 192 utf8, strerror(err)); | |
| 193 break; | |
| 194 case GAIM_XFER_RECEIVE: | |
| 195 msg = g_strdup_printf(_("Error writing %s: \n%s.\n"), | |
| 196 utf8, strerror(err)); | |
| 197 break; | |
| 198 default: | |
| 199 msg = g_strdup_printf(_("Error accessing %s: \n%s.\n"), | |
| 200 utf8, strerror(err)); | |
| 201 break; | |
| 202 } | |
| 203 g_free(utf8); | |
| 204 | |
| 205 gaim_xfer_conversation_write(xfer, msg, TRUE); | |
| 206 gaim_xfer_error(xfer_type, account, xfer->who, msg); | |
| 207 g_free(msg); | |
| 208 } | |
| 209 | |
| 210 static void | |
| 211 gaim_xfer_choose_file_ok_cb(void *user_data, const char *filename) | |
| 212 { | |
| 213 GaimXfer *xfer; | |
| 214 struct stat st; | |
| 14905 | 215 gchar *dir; |
| 14192 | 216 |
| 217 xfer = (GaimXfer *)user_data; | |
| 218 | |
| 219 if (g_stat(filename, &st) != 0) { | |
| 220 /* File not found. */ | |
| 221 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) { | |
| 14905 | 222 dir = g_path_get_dirname(filename); |
| 223 | |
| 224 if (g_access(dir, W_OK) == 0) { | |
| 225 gaim_xfer_request_accepted(xfer, filename); | |
| 226 } else { | |
| 227 gaim_xfer_ref(xfer); | |
| 228 gaim_notify_message( | |
| 229 NULL, GAIM_NOTIFY_MSG_ERROR, NULL, | |
| 230 _("Directory is not writable."), NULL, | |
| 231 (GaimNotifyCloseCallback)gaim_xfer_choose_file, xfer); | |
| 232 } | |
| 233 | |
| 234 g_free(dir); | |
| 14192 | 235 } |
| 236 else { | |
| 237 gaim_xfer_show_file_error(xfer, filename); | |
| 238 gaim_xfer_request_denied(xfer); | |
| 239 } | |
| 240 } | |
| 241 else if ((gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) && | |
| 242 (st.st_size == 0)) { | |
| 243 | |
| 244 gaim_notify_error(NULL, NULL, | |
| 245 _("Cannot send a file of 0 bytes."), NULL); | |
| 246 | |
| 247 gaim_xfer_request_denied(xfer); | |
| 248 } | |
| 249 else if ((gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) && | |
| 250 S_ISDIR(st.st_mode)) { | |
| 251 /* | |
| 252 * XXX - Sending a directory should be valid for some protocols. | |
| 253 */ | |
| 254 gaim_notify_error(NULL, NULL, | |
| 255 _("Cannot send a directory."), NULL); | |
| 256 | |
| 257 gaim_xfer_request_denied(xfer); | |
| 258 } | |
| 259 else if ((gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) && | |
| 260 S_ISDIR(st.st_mode)) { | |
| 261 char *msg, *utf8; | |
| 262 utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL); | |
| 263 msg = g_strdup_printf( | |
| 264 _("%s is not a regular file. Cowardly refusing to overwrite it.\n"), utf8); | |
| 265 g_free(utf8); | |
| 266 gaim_notify_error(NULL, NULL, msg, NULL); | |
| 267 g_free(msg); | |
| 268 gaim_xfer_request_denied(xfer); | |
| 269 } | |
| 270 else { | |
| 271 gaim_xfer_request_accepted(xfer, filename); | |
| 272 } | |
| 273 | |
| 274 gaim_xfer_unref(xfer); | |
| 275 } | |
| 276 | |
| 277 static void | |
| 278 gaim_xfer_choose_file_cancel_cb(void *user_data, const char *filename) | |
| 279 { | |
| 280 GaimXfer *xfer = (GaimXfer *)user_data; | |
| 281 | |
| 282 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_CANCEL_LOCAL); | |
| 283 gaim_xfer_request_denied(xfer); | |
| 284 } | |
| 285 | |
| 286 static int | |
| 287 gaim_xfer_choose_file(GaimXfer *xfer) | |
| 288 { | |
| 289 gaim_request_file(xfer, NULL, gaim_xfer_get_filename(xfer), | |
| 290 (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE), | |
| 291 G_CALLBACK(gaim_xfer_choose_file_ok_cb), | |
| 292 G_CALLBACK(gaim_xfer_choose_file_cancel_cb), xfer); | |
| 293 | |
| 294 return 0; | |
| 295 } | |
| 296 | |
| 297 static int | |
| 298 cancel_recv_cb(GaimXfer *xfer) | |
| 299 { | |
| 300 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_CANCEL_LOCAL); | |
| 301 gaim_xfer_request_denied(xfer); | |
| 302 gaim_xfer_unref(xfer); | |
| 303 | |
| 304 return 0; | |
| 305 } | |
| 306 | |
| 307 static void | |
| 308 gaim_xfer_ask_recv(GaimXfer *xfer) | |
| 309 { | |
| 310 char *buf, *size_buf; | |
| 311 size_t size; | |
| 312 | |
| 313 /* If we have already accepted the request, ask the destination file | |
| 314 name directly */ | |
| 315 if (gaim_xfer_get_status(xfer) != GAIM_XFER_STATUS_ACCEPTED) { | |
| 316 GaimBuddy *buddy = gaim_find_buddy(xfer->account, xfer->who); | |
| 317 | |
| 318 if (gaim_xfer_get_filename(xfer) != NULL) | |
| 319 { | |
| 320 size = gaim_xfer_get_size(xfer); | |
| 321 size_buf = gaim_str_size_to_units(size); | |
| 322 buf = g_strdup_printf(_("%s wants to send you %s (%s)"), | |
| 323 buddy ? gaim_buddy_get_alias(buddy) : xfer->who, | |
| 324 gaim_xfer_get_filename(xfer), size_buf); | |
| 325 g_free(size_buf); | |
| 326 } | |
| 327 else | |
| 328 { | |
| 329 buf = g_strdup_printf(_("%s wants to send you a file"), | |
| 330 buddy ? gaim_buddy_get_alias(buddy) : xfer->who); | |
| 331 } | |
| 332 | |
| 333 if (xfer->message != NULL) | |
| 334 serv_got_im(gaim_account_get_connection(xfer->account), | |
| 335 xfer->who, xfer->message, 0, time(NULL)); | |
| 336 | |
| 337 gaim_request_accept_cancel(xfer, NULL, buf, NULL, | |
| 338 GAIM_DEFAULT_ACTION_NONE, xfer, | |
| 339 G_CALLBACK(gaim_xfer_choose_file), | |
| 340 G_CALLBACK(cancel_recv_cb)); | |
| 341 | |
| 342 g_free(buf); | |
| 343 } else | |
| 344 gaim_xfer_choose_file(xfer); | |
| 345 } | |
| 346 | |
| 347 static int | |
| 348 ask_accept_ok(GaimXfer *xfer) | |
| 349 { | |
| 350 gaim_xfer_request_accepted(xfer, NULL); | |
| 351 | |
| 352 return 0; | |
| 353 } | |
| 354 | |
| 355 static int | |
| 356 ask_accept_cancel(GaimXfer *xfer) | |
| 357 { | |
| 358 gaim_xfer_request_denied(xfer); | |
| 359 gaim_xfer_unref(xfer); | |
| 360 | |
| 361 return 0; | |
| 362 } | |
| 363 | |
| 364 static void | |
| 365 gaim_xfer_ask_accept(GaimXfer *xfer) | |
| 366 { | |
| 367 char *buf, *buf2 = NULL; | |
| 368 GaimBuddy *buddy = gaim_find_buddy(xfer->account, xfer->who); | |
| 369 | |
| 370 buf = g_strdup_printf(_("Accept file transfer request from %s?"), | |
| 371 buddy ? gaim_buddy_get_alias(buddy) : xfer->who); | |
| 372 if (gaim_xfer_get_remote_ip(xfer) && | |
| 373 gaim_xfer_get_remote_port(xfer)) | |
| 374 buf2 = g_strdup_printf(_("A file is available for download from:\n" | |
| 375 "Remote host: %s\nRemote port: %d"), | |
| 376 gaim_xfer_get_remote_ip(xfer), | |
| 377 gaim_xfer_get_remote_port(xfer)); | |
| 378 gaim_request_accept_cancel(xfer, NULL, buf, buf2, | |
| 379 GAIM_DEFAULT_ACTION_NONE, xfer, | |
| 380 G_CALLBACK(ask_accept_ok), | |
| 381 G_CALLBACK(ask_accept_cancel)); | |
| 382 g_free(buf); | |
| 383 g_free(buf2); | |
| 384 } | |
| 385 | |
| 386 void | |
| 387 gaim_xfer_request(GaimXfer *xfer) | |
| 388 { | |
| 389 g_return_if_fail(xfer != NULL); | |
| 390 g_return_if_fail(xfer->ops.init != NULL); | |
| 391 | |
| 392 gaim_xfer_ref(xfer); | |
| 393 | |
| 394 if (gaim_xfer_get_type(xfer) == GAIM_XFER_RECEIVE) | |
| 395 { | |
| 396 gaim_signal_emit(gaim_xfers_get_handle(), "file-recv-request", xfer); | |
| 397 if (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) | |
| 398 { | |
| 399 /* The file-transfer was cancelled by a plugin */ | |
| 400 gaim_xfer_cancel_local(xfer); | |
| 401 } | |
| 402 else if (gaim_xfer_get_filename(xfer) || | |
| 403 gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_ACCEPTED) | |
| 404 { | |
| 405 gchar* message = NULL; | |
| 406 GaimBuddy *buddy = gaim_find_buddy(xfer->account, xfer->who); | |
| 407 message = g_strdup_printf(_("%s is offering to send file %s"), | |
| 408 buddy ? gaim_buddy_get_alias(buddy) : xfer->who, gaim_xfer_get_filename(xfer)); | |
| 409 gaim_xfer_conversation_write(xfer, message, FALSE); | |
| 410 g_free(message); | |
| 411 /* Ask for a filename to save to if it's not already given by a plugin */ | |
| 412 if (xfer->local_filename == NULL) | |
| 413 gaim_xfer_ask_recv(xfer); | |
| 414 } | |
| 415 else | |
| 416 { | |
| 417 gaim_xfer_ask_accept(xfer); | |
| 418 } | |
| 419 } | |
| 420 else | |
| 421 { | |
| 422 gaim_xfer_choose_file(xfer); | |
| 423 } | |
| 424 } | |
| 425 | |
| 426 void | |
| 427 gaim_xfer_request_accepted(GaimXfer *xfer, const char *filename) | |
| 428 { | |
| 429 GaimXferType type; | |
| 430 struct stat st; | |
| 431 char *msg, *utf8; | |
| 432 GaimAccount *account; | |
| 433 GaimBuddy *buddy; | |
| 434 | |
| 435 if (xfer == NULL) | |
| 436 return; | |
| 437 | |
| 438 type = gaim_xfer_get_type(xfer); | |
| 439 account = gaim_xfer_get_account(xfer); | |
| 440 | |
| 441 if (!filename && type == GAIM_XFER_RECEIVE) { | |
| 442 xfer->status = GAIM_XFER_STATUS_ACCEPTED; | |
| 443 xfer->ops.init(xfer); | |
| 444 return; | |
| 445 } | |
| 446 | |
| 447 buddy = gaim_find_buddy(account, xfer->who); | |
| 448 | |
| 449 if (type == GAIM_XFER_SEND) { | |
| 450 /* Sending a file */ | |
| 451 /* Check the filename. */ | |
| 452 #ifdef _WIN32 | |
| 453 if (g_strrstr(filename, "../") || g_strrstr(filename, "..\\")) { | |
| 454 #else | |
| 455 if (g_strrstr(filename, "../")) { | |
| 456 #endif | |
| 457 char *utf8 = g_filename_to_utf8(filename, -1, NULL, NULL, NULL); | |
| 458 | |
| 459 msg = g_strdup_printf(_("%s is not a valid filename.\n"), utf8); | |
| 460 gaim_xfer_error(type, account, xfer->who, msg); | |
| 461 g_free(utf8); | |
| 462 g_free(msg); | |
| 463 | |
| 464 gaim_xfer_unref(xfer); | |
| 465 return; | |
| 466 } | |
| 467 | |
| 468 if (g_stat(filename, &st) == -1) { | |
| 469 gaim_xfer_show_file_error(xfer, filename); | |
| 470 gaim_xfer_unref(xfer); | |
| 471 return; | |
| 472 } | |
| 473 | |
| 474 gaim_xfer_set_local_filename(xfer, filename); | |
| 475 gaim_xfer_set_size(xfer, st.st_size); | |
| 476 | |
| 477 utf8 = g_filename_to_utf8(g_basename(filename), -1, NULL, NULL, NULL); | |
| 478 gaim_xfer_set_filename(xfer, utf8); | |
| 479 | |
| 480 msg = g_strdup_printf(_("Offering to send %s to %s"), | |
| 481 utf8, buddy ? gaim_buddy_get_alias(buddy) : xfer->who); | |
| 482 g_free(utf8); | |
| 483 | |
| 484 gaim_xfer_conversation_write(xfer, msg, FALSE); | |
| 485 g_free(msg); | |
| 486 } | |
| 487 else { | |
| 488 /* Receiving a file */ | |
| 489 xfer->status = GAIM_XFER_STATUS_ACCEPTED; | |
| 490 gaim_xfer_set_local_filename(xfer, filename); | |
| 491 | |
| 492 msg = g_strdup_printf(_("Starting transfer of %s from %s"), | |
| 493 xfer->filename, buddy ? gaim_buddy_get_alias(buddy) : xfer->who); | |
| 494 gaim_xfer_conversation_write(xfer, msg, FALSE); | |
| 495 g_free(msg); | |
| 496 } | |
| 497 | |
| 498 gaim_xfer_add(xfer); | |
| 499 xfer->ops.init(xfer); | |
| 500 | |
| 501 } | |
| 502 | |
| 503 void | |
| 504 gaim_xfer_request_denied(GaimXfer *xfer) | |
| 505 { | |
| 506 g_return_if_fail(xfer != NULL); | |
| 507 | |
| 508 if (xfer->ops.request_denied != NULL) | |
| 509 xfer->ops.request_denied(xfer); | |
| 510 | |
| 511 gaim_xfer_unref(xfer); | |
| 512 } | |
| 513 | |
| 514 GaimXferType | |
| 515 gaim_xfer_get_type(const GaimXfer *xfer) | |
| 516 { | |
| 517 g_return_val_if_fail(xfer != NULL, GAIM_XFER_UNKNOWN); | |
| 518 | |
| 519 return xfer->type; | |
| 520 } | |
| 521 | |
| 522 GaimAccount * | |
| 523 gaim_xfer_get_account(const GaimXfer *xfer) | |
| 524 { | |
| 525 g_return_val_if_fail(xfer != NULL, NULL); | |
| 526 | |
| 527 return xfer->account; | |
| 528 } | |
| 529 | |
| 530 GaimXferStatusType | |
| 531 gaim_xfer_get_status(const GaimXfer *xfer) | |
| 532 { | |
| 533 g_return_val_if_fail(xfer != NULL, GAIM_XFER_STATUS_UNKNOWN); | |
| 534 | |
| 535 return xfer->status; | |
| 536 } | |
| 537 | |
| 538 gboolean | |
| 539 gaim_xfer_is_canceled(const GaimXfer *xfer) | |
| 540 { | |
| 541 g_return_val_if_fail(xfer != NULL, TRUE); | |
| 542 | |
| 543 if ((gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_LOCAL) || | |
| 544 (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_CANCEL_REMOTE)) | |
| 545 return TRUE; | |
| 546 else | |
| 547 return FALSE; | |
| 548 } | |
| 549 | |
| 550 gboolean | |
| 551 gaim_xfer_is_completed(const GaimXfer *xfer) | |
| 552 { | |
| 553 g_return_val_if_fail(xfer != NULL, TRUE); | |
| 554 | |
| 555 return (gaim_xfer_get_status(xfer) == GAIM_XFER_STATUS_DONE); | |
| 556 } | |
| 557 | |
| 558 const char * | |
| 559 gaim_xfer_get_filename(const GaimXfer *xfer) | |
| 560 { | |
| 561 g_return_val_if_fail(xfer != NULL, NULL); | |
| 562 | |
| 563 return xfer->filename; | |
| 564 } | |
| 565 | |
| 566 const char * | |
| 567 gaim_xfer_get_local_filename(const GaimXfer *xfer) | |
| 568 { | |
| 569 g_return_val_if_fail(xfer != NULL, NULL); | |
| 570 | |
| 571 return xfer->local_filename; | |
| 572 } | |
| 573 | |
| 574 size_t | |
| 575 gaim_xfer_get_bytes_sent(const GaimXfer *xfer) | |
| 576 { | |
| 577 g_return_val_if_fail(xfer != NULL, 0); | |
| 578 | |
| 579 return xfer->bytes_sent; | |
| 580 } | |
| 581 | |
| 582 size_t | |
| 583 gaim_xfer_get_bytes_remaining(const GaimXfer *xfer) | |
| 584 { | |
| 585 g_return_val_if_fail(xfer != NULL, 0); | |
| 586 | |
| 587 return xfer->bytes_remaining; | |
| 588 } | |
| 589 | |
| 590 size_t | |
| 591 gaim_xfer_get_size(const GaimXfer *xfer) | |
| 592 { | |
| 593 g_return_val_if_fail(xfer != NULL, 0); | |
| 594 | |
| 595 return xfer->size; | |
| 596 } | |
| 597 | |
| 598 double | |
| 599 gaim_xfer_get_progress(const GaimXfer *xfer) | |
| 600 { | |
| 601 g_return_val_if_fail(xfer != NULL, 0.0); | |
| 602 | |
| 603 if (gaim_xfer_get_size(xfer) == 0) | |
| 604 return 0.0; | |
| 605 | |
| 606 return ((double)gaim_xfer_get_bytes_sent(xfer) / | |
| 607 (double)gaim_xfer_get_size(xfer)); | |
| 608 } | |
| 609 | |
| 610 unsigned int | |
| 611 gaim_xfer_get_local_port(const GaimXfer *xfer) | |
| 612 { | |
| 613 g_return_val_if_fail(xfer != NULL, -1); | |
| 614 | |
| 615 return xfer->local_port; | |
| 616 } | |
| 617 | |
| 618 const char * | |
| 619 gaim_xfer_get_remote_ip(const GaimXfer *xfer) | |
| 620 { | |
| 621 g_return_val_if_fail(xfer != NULL, NULL); | |
| 622 | |
| 623 return xfer->remote_ip; | |
| 624 } | |
| 625 | |
| 626 unsigned int | |
| 627 gaim_xfer_get_remote_port(const GaimXfer *xfer) | |
| 628 { | |
| 629 g_return_val_if_fail(xfer != NULL, -1); | |
| 630 | |
| 631 return xfer->remote_port; | |
| 632 } | |
| 633 | |
| 634 void | |
| 635 gaim_xfer_set_completed(GaimXfer *xfer, gboolean completed) | |
| 636 { | |
| 637 GaimXferUiOps *ui_ops; | |
| 638 | |
| 639 g_return_if_fail(xfer != NULL); | |
| 640 | |
| 641 if (completed == TRUE) { | |
| 642 char *msg = NULL; | |
| 643 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_DONE); | |
| 644 | |
| 645 if (gaim_xfer_get_filename(xfer) != NULL) | |
| 646 msg = g_strdup_printf(_("Transfer of file %s complete"), | |
| 647 gaim_xfer_get_filename(xfer)); | |
| 648 else | |
| 649 msg = g_strdup_printf(_("File transfer complete")); | |
| 650 gaim_xfer_conversation_write(xfer, msg, FALSE); | |
| 651 g_free(msg); | |
| 652 } | |
| 653 | |
| 654 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 655 | |
| 656 if (ui_ops != NULL && ui_ops->update_progress != NULL) | |
| 657 ui_ops->update_progress(xfer, gaim_xfer_get_progress(xfer)); | |
| 658 } | |
| 659 | |
| 660 void | |
| 661 gaim_xfer_set_message(GaimXfer *xfer, const char *message) | |
| 662 { | |
| 663 g_return_if_fail(xfer != NULL); | |
| 664 | |
| 665 g_free(xfer->message); | |
| 666 xfer->message = g_strdup(message); | |
| 667 } | |
| 668 | |
| 669 void | |
| 670 gaim_xfer_set_filename(GaimXfer *xfer, const char *filename) | |
| 671 { | |
| 672 g_return_if_fail(xfer != NULL); | |
| 673 | |
| 674 g_free(xfer->filename); | |
| 675 xfer->filename = g_strdup(filename); | |
| 676 } | |
| 677 | |
| 678 void | |
| 679 gaim_xfer_set_local_filename(GaimXfer *xfer, const char *filename) | |
| 680 { | |
| 681 g_return_if_fail(xfer != NULL); | |
| 682 | |
| 683 g_free(xfer->local_filename); | |
| 684 xfer->local_filename = g_strdup(filename); | |
| 685 } | |
| 686 | |
| 687 void | |
| 688 gaim_xfer_set_size(GaimXfer *xfer, size_t size) | |
| 689 { | |
| 690 g_return_if_fail(xfer != NULL); | |
| 691 | |
| 692 if (xfer->size == 0) | |
| 693 xfer->bytes_remaining = size - xfer->bytes_sent; | |
| 694 | |
| 695 xfer->size = size; | |
| 696 } | |
| 697 | |
| 698 GaimXferUiOps * | |
| 699 gaim_xfer_get_ui_ops(const GaimXfer *xfer) | |
| 700 { | |
| 701 g_return_val_if_fail(xfer != NULL, NULL); | |
| 702 | |
| 703 return xfer->ui_ops; | |
| 704 } | |
| 705 | |
| 706 void | |
| 707 gaim_xfer_set_init_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 708 { | |
| 709 g_return_if_fail(xfer != NULL); | |
| 710 | |
| 711 xfer->ops.init = fnc; | |
| 712 } | |
| 713 | |
| 714 void gaim_xfer_set_request_denied_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 715 { | |
| 716 g_return_if_fail(xfer != NULL); | |
| 717 | |
| 718 xfer->ops.request_denied = fnc; | |
| 719 } | |
| 720 | |
| 721 void | |
| 722 gaim_xfer_set_read_fnc(GaimXfer *xfer, gssize (*fnc)(guchar **, GaimXfer *)) | |
| 723 { | |
| 724 g_return_if_fail(xfer != NULL); | |
| 725 | |
| 726 xfer->ops.read = fnc; | |
| 727 } | |
| 728 | |
| 729 void | |
| 730 gaim_xfer_set_write_fnc(GaimXfer *xfer, | |
| 731 gssize (*fnc)(const guchar *, size_t, GaimXfer *)) | |
| 732 { | |
| 733 g_return_if_fail(xfer != NULL); | |
| 734 | |
| 735 xfer->ops.write = fnc; | |
| 736 } | |
| 737 | |
| 738 void | |
| 739 gaim_xfer_set_ack_fnc(GaimXfer *xfer, | |
| 740 void (*fnc)(GaimXfer *, const guchar *, size_t)) | |
| 741 { | |
| 742 g_return_if_fail(xfer != NULL); | |
| 743 | |
| 744 xfer->ops.ack = fnc; | |
| 745 } | |
| 746 | |
| 747 void | |
| 748 gaim_xfer_set_start_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 749 { | |
| 750 g_return_if_fail(xfer != NULL); | |
| 751 | |
| 752 xfer->ops.start = fnc; | |
| 753 } | |
| 754 | |
| 755 void | |
| 756 gaim_xfer_set_end_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 757 { | |
| 758 g_return_if_fail(xfer != NULL); | |
| 759 | |
| 760 xfer->ops.end = fnc; | |
| 761 } | |
| 762 | |
| 763 void | |
| 764 gaim_xfer_set_cancel_send_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 765 { | |
| 766 g_return_if_fail(xfer != NULL); | |
| 767 | |
| 768 xfer->ops.cancel_send = fnc; | |
| 769 } | |
| 770 | |
| 771 void | |
| 772 gaim_xfer_set_cancel_recv_fnc(GaimXfer *xfer, void (*fnc)(GaimXfer *)) | |
| 773 { | |
| 774 g_return_if_fail(xfer != NULL); | |
| 775 | |
| 776 xfer->ops.cancel_recv = fnc; | |
| 777 } | |
| 778 | |
| 779 gssize | |
| 780 gaim_xfer_read(GaimXfer *xfer, guchar **buffer) | |
| 781 { | |
| 782 gssize s, r; | |
| 783 | |
| 784 g_return_val_if_fail(xfer != NULL, 0); | |
| 785 g_return_val_if_fail(buffer != NULL, 0); | |
| 786 | |
| 787 if (gaim_xfer_get_size(xfer) == 0) | |
| 788 s = 4096; | |
| 789 else | |
| 790 s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); | |
| 791 | |
| 792 if (xfer->ops.read != NULL) | |
| 793 r = (xfer->ops.read)(buffer, xfer); | |
| 794 else { | |
| 795 *buffer = g_malloc0(s); | |
| 796 | |
| 797 r = read(xfer->fd, *buffer, s); | |
| 798 if (r < 0 && errno == EAGAIN) | |
| 799 r = 0; | |
| 800 else if (r < 0) | |
| 801 r = -1; | |
| 802 else if ((gaim_xfer_get_size(xfer) > 0) && | |
| 803 ((gaim_xfer_get_bytes_sent(xfer)+r) >= gaim_xfer_get_size(xfer))) | |
| 804 gaim_xfer_set_completed(xfer, TRUE); | |
| 805 else if (r == 0) | |
| 806 r = -1; | |
| 807 } | |
| 808 | |
| 809 return r; | |
| 810 } | |
| 811 | |
| 812 gssize | |
| 813 gaim_xfer_write(GaimXfer *xfer, const guchar *buffer, gsize size) | |
| 814 { | |
| 815 gssize r, s; | |
| 816 | |
| 817 g_return_val_if_fail(xfer != NULL, 0); | |
| 818 g_return_val_if_fail(buffer != NULL, 0); | |
| 819 g_return_val_if_fail(size != 0, 0); | |
| 820 | |
| 821 s = MIN(gaim_xfer_get_bytes_remaining(xfer), size); | |
| 822 | |
| 823 if (xfer->ops.write != NULL) { | |
| 824 r = (xfer->ops.write)(buffer, s, xfer); | |
| 825 } else { | |
| 826 r = write(xfer->fd, buffer, s); | |
| 827 if (r < 0 && errno == EAGAIN) | |
| 828 r = 0; | |
| 829 if ((gaim_xfer_get_bytes_sent(xfer)+r) >= gaim_xfer_get_size(xfer)) | |
| 830 gaim_xfer_set_completed(xfer, TRUE); | |
| 831 } | |
| 832 | |
| 833 return r; | |
| 834 } | |
| 835 | |
| 836 static void | |
| 837 transfer_cb(gpointer data, gint source, GaimInputCondition condition) | |
| 838 { | |
| 839 GaimXferUiOps *ui_ops; | |
| 840 GaimXfer *xfer = (GaimXfer *)data; | |
| 841 guchar *buffer = NULL; | |
| 842 gssize r = 0; | |
| 843 | |
| 844 if (condition & GAIM_INPUT_READ) { | |
| 845 r = gaim_xfer_read(xfer, &buffer); | |
| 846 if (r > 0) { | |
| 847 fwrite(buffer, 1, r, xfer->dest_fp); | |
| 848 } else if(r <= 0) { | |
| 849 gaim_xfer_cancel_remote(xfer); | |
| 850 return; | |
| 851 } | |
| 852 } | |
| 853 | |
| 854 if (condition & GAIM_INPUT_WRITE) { | |
| 855 size_t s = MIN(gaim_xfer_get_bytes_remaining(xfer), 4096); | |
| 856 | |
| 857 /* this is so the prpl can keep the connection open | |
| 858 if it needs to for some odd reason. */ | |
| 859 if (s == 0) { | |
| 860 if (xfer->watcher) { | |
| 861 gaim_input_remove(xfer->watcher); | |
| 862 xfer->watcher = 0; | |
| 863 } | |
| 864 return; | |
| 865 } | |
| 866 | |
| 867 buffer = g_malloc0(s); | |
| 868 | |
| 869 fread(buffer, 1, s, xfer->dest_fp); | |
| 870 | |
| 871 /* Write as much as we're allowed to. */ | |
| 872 r = gaim_xfer_write(xfer, buffer, s); | |
| 873 | |
| 874 if (r == -1) { | |
| 875 gaim_xfer_cancel_remote(xfer); | |
| 876 g_free(buffer); | |
| 877 return; | |
| 878 } else if (r < s) { | |
| 879 /* We have to seek back in the file now. */ | |
| 880 fseek(xfer->dest_fp, r - s, SEEK_CUR); | |
| 881 } | |
| 882 } | |
| 883 | |
| 884 if (r > 0) { | |
| 885 if (gaim_xfer_get_size(xfer) > 0) | |
| 886 xfer->bytes_remaining -= r; | |
| 887 | |
| 888 xfer->bytes_sent += r; | |
| 889 | |
| 890 if (xfer->ops.ack != NULL) | |
| 891 xfer->ops.ack(xfer, buffer, r); | |
| 892 | |
| 893 g_free(buffer); | |
| 894 | |
| 895 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 896 | |
| 897 if (ui_ops != NULL && ui_ops->update_progress != NULL) | |
| 898 ui_ops->update_progress(xfer, | |
| 899 gaim_xfer_get_progress(xfer)); | |
| 900 } | |
| 901 | |
| 902 if (gaim_xfer_is_completed(xfer)) | |
| 903 gaim_xfer_end(xfer); | |
| 904 } | |
| 905 | |
| 906 static void | |
| 907 begin_transfer(GaimXfer *xfer, GaimInputCondition cond) | |
| 908 { | |
| 909 GaimXferType type = gaim_xfer_get_type(xfer); | |
| 910 | |
| 911 xfer->dest_fp = g_fopen(gaim_xfer_get_local_filename(xfer), | |
| 912 type == GAIM_XFER_RECEIVE ? "wb" : "rb"); | |
| 913 | |
| 914 if (xfer->dest_fp == NULL) { | |
| 915 gaim_xfer_show_file_error(xfer, gaim_xfer_get_local_filename(xfer)); | |
| 916 gaim_xfer_cancel_local(xfer); | |
| 917 return; | |
| 918 } | |
| 919 | |
| 920 xfer->watcher = gaim_input_add(xfer->fd, cond, transfer_cb, xfer); | |
| 921 | |
| 922 xfer->start_time = time(NULL); | |
| 923 | |
| 924 if (xfer->ops.start != NULL) | |
| 925 xfer->ops.start(xfer); | |
| 926 } | |
| 927 | |
| 928 static void | |
| 929 connect_cb(gpointer data, gint source, const gchar *error_message) | |
| 930 { | |
| 931 GaimXfer *xfer = (GaimXfer *)data; | |
| 932 | |
| 933 xfer->fd = source; | |
| 934 | |
| 935 begin_transfer(xfer, GAIM_INPUT_READ); | |
| 936 } | |
| 937 | |
| 938 void | |
| 939 gaim_xfer_start(GaimXfer *xfer, int fd, const char *ip, | |
| 940 unsigned int port) | |
| 941 { | |
| 942 GaimInputCondition cond; | |
| 943 GaimXferType type; | |
| 944 | |
| 945 g_return_if_fail(xfer != NULL); | |
| 946 g_return_if_fail(gaim_xfer_get_type(xfer) != GAIM_XFER_UNKNOWN); | |
| 947 | |
| 948 type = gaim_xfer_get_type(xfer); | |
| 949 | |
| 950 xfer->bytes_remaining = gaim_xfer_get_size(xfer); | |
| 951 xfer->bytes_sent = 0; | |
| 952 | |
| 953 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_STARTED); | |
| 954 | |
| 955 if (type == GAIM_XFER_RECEIVE) { | |
| 956 cond = GAIM_INPUT_READ; | |
| 957 | |
| 958 if (ip != NULL) { | |
| 959 xfer->remote_ip = g_strdup(ip); | |
| 960 xfer->remote_port = port; | |
| 961 | |
| 962 /* Establish a file descriptor. */ | |
| 14837 | 963 gaim_proxy_connect(NULL, xfer->account, xfer->remote_ip, |
| 14192 | 964 xfer->remote_port, connect_cb, xfer); |
| 965 | |
| 966 return; | |
| 967 } | |
| 968 else { | |
| 969 xfer->fd = fd; | |
| 970 } | |
| 971 } | |
| 972 else { | |
| 973 cond = GAIM_INPUT_WRITE; | |
| 974 | |
| 975 xfer->fd = fd; | |
| 976 } | |
| 977 | |
| 978 begin_transfer(xfer, cond); | |
| 979 } | |
| 980 | |
| 981 void | |
| 982 gaim_xfer_end(GaimXfer *xfer) | |
| 983 { | |
| 984 g_return_if_fail(xfer != NULL); | |
| 985 | |
| 986 /* See if we are actually trying to cancel this. */ | |
| 987 if (!gaim_xfer_is_completed(xfer)) { | |
| 988 gaim_xfer_cancel_local(xfer); | |
| 989 return; | |
| 990 } | |
| 991 | |
| 992 xfer->end_time = time(NULL); | |
| 993 if (xfer->ops.end != NULL) | |
| 994 xfer->ops.end(xfer); | |
| 995 | |
| 996 if (xfer->watcher != 0) { | |
| 997 gaim_input_remove(xfer->watcher); | |
| 998 xfer->watcher = 0; | |
| 999 } | |
| 1000 | |
| 1001 if (xfer->fd != 0) | |
| 1002 close(xfer->fd); | |
| 1003 | |
| 1004 if (xfer->dest_fp != NULL) { | |
| 1005 fclose(xfer->dest_fp); | |
| 1006 xfer->dest_fp = NULL; | |
| 1007 } | |
| 1008 | |
| 1009 gaim_xfer_unref(xfer); | |
| 1010 } | |
| 1011 | |
| 1012 void | |
| 1013 gaim_xfer_add(GaimXfer *xfer) | |
| 1014 { | |
| 1015 GaimXferUiOps *ui_ops; | |
| 1016 | |
| 1017 g_return_if_fail(xfer != NULL); | |
| 1018 | |
| 1019 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 1020 | |
| 1021 if (ui_ops != NULL && ui_ops->add_xfer != NULL) | |
| 1022 ui_ops->add_xfer(xfer); | |
| 1023 } | |
| 1024 | |
| 1025 void | |
| 1026 gaim_xfer_cancel_local(GaimXfer *xfer) | |
| 1027 { | |
| 1028 GaimXferUiOps *ui_ops; | |
| 1029 char *msg = NULL; | |
| 1030 | |
| 1031 g_return_if_fail(xfer != NULL); | |
| 1032 | |
| 1033 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_CANCEL_LOCAL); | |
| 1034 xfer->end_time = time(NULL); | |
| 1035 | |
| 1036 if (gaim_xfer_get_filename(xfer) != NULL) | |
| 1037 { | |
| 1038 msg = g_strdup_printf(_("You canceled the transfer of %s"), | |
| 1039 gaim_xfer_get_filename(xfer)); | |
| 1040 } | |
| 1041 else | |
| 1042 { | |
| 1043 msg = g_strdup_printf(_("File transfer cancelled")); | |
| 1044 } | |
| 1045 gaim_xfer_conversation_write(xfer, msg, FALSE); | |
| 1046 g_free(msg); | |
| 1047 | |
| 1048 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) | |
| 1049 { | |
| 1050 if (xfer->ops.cancel_send != NULL) | |
| 1051 xfer->ops.cancel_send(xfer); | |
| 1052 } | |
| 1053 else | |
| 1054 { | |
| 1055 if (xfer->ops.cancel_recv != NULL) | |
| 1056 xfer->ops.cancel_recv(xfer); | |
| 1057 } | |
| 1058 | |
| 1059 if (xfer->watcher != 0) { | |
| 1060 gaim_input_remove(xfer->watcher); | |
| 1061 xfer->watcher = 0; | |
| 1062 } | |
| 1063 | |
| 1064 if (xfer->fd != 0) | |
| 1065 close(xfer->fd); | |
| 1066 | |
| 1067 if (xfer->dest_fp != NULL) { | |
| 1068 fclose(xfer->dest_fp); | |
| 1069 xfer->dest_fp = NULL; | |
| 1070 } | |
| 1071 | |
| 1072 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 1073 | |
| 1074 if (ui_ops != NULL && ui_ops->cancel_local != NULL) | |
| 1075 ui_ops->cancel_local(xfer); | |
| 1076 | |
| 1077 xfer->bytes_remaining = 0; | |
| 1078 | |
| 1079 gaim_xfer_unref(xfer); | |
| 1080 } | |
| 1081 | |
| 1082 void | |
| 1083 gaim_xfer_cancel_remote(GaimXfer *xfer) | |
| 1084 { | |
| 1085 GaimXferUiOps *ui_ops; | |
| 1086 gchar *msg; | |
| 1087 GaimAccount *account; | |
| 1088 GaimBuddy *buddy; | |
| 1089 | |
| 1090 g_return_if_fail(xfer != NULL); | |
| 1091 | |
| 1092 gaim_request_close_with_handle(xfer); | |
| 1093 gaim_xfer_set_status(xfer, GAIM_XFER_STATUS_CANCEL_REMOTE); | |
| 1094 xfer->end_time = time(NULL); | |
| 1095 | |
| 1096 account = gaim_xfer_get_account(xfer); | |
| 1097 buddy = gaim_find_buddy(account, xfer->who); | |
| 1098 | |
| 1099 if (gaim_xfer_get_filename(xfer) != NULL) | |
| 1100 { | |
| 1101 msg = g_strdup_printf(_("%s canceled the transfer of %s"), | |
| 1102 buddy ? gaim_buddy_get_alias(buddy) : xfer->who, gaim_xfer_get_filename(xfer)); | |
| 1103 } | |
| 1104 else | |
| 1105 { | |
| 1106 msg = g_strdup_printf(_("%s canceled the file transfer"), | |
| 1107 buddy ? gaim_buddy_get_alias(buddy) : xfer->who); | |
| 1108 } | |
| 1109 gaim_xfer_conversation_write(xfer, msg, TRUE); | |
| 1110 gaim_xfer_error(gaim_xfer_get_type(xfer), account, xfer->who, msg); | |
| 1111 g_free(msg); | |
| 1112 | |
| 1113 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) | |
| 1114 { | |
| 1115 if (xfer->ops.cancel_send != NULL) | |
| 1116 xfer->ops.cancel_send(xfer); | |
| 1117 } | |
| 1118 else | |
| 1119 { | |
| 1120 if (xfer->ops.cancel_recv != NULL) | |
| 1121 xfer->ops.cancel_recv(xfer); | |
| 1122 } | |
| 1123 | |
| 1124 if (xfer->watcher != 0) { | |
| 1125 gaim_input_remove(xfer->watcher); | |
| 1126 xfer->watcher = 0; | |
| 1127 } | |
| 1128 | |
| 1129 if (xfer->fd != 0) | |
| 1130 close(xfer->fd); | |
| 1131 | |
| 1132 if (xfer->dest_fp != NULL) { | |
| 1133 fclose(xfer->dest_fp); | |
| 1134 xfer->dest_fp = NULL; | |
| 1135 } | |
| 1136 | |
| 1137 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 1138 | |
| 1139 if (ui_ops != NULL && ui_ops->cancel_remote != NULL) | |
| 1140 ui_ops->cancel_remote(xfer); | |
| 1141 | |
| 1142 xfer->bytes_remaining = 0; | |
| 1143 | |
| 1144 gaim_xfer_unref(xfer); | |
| 1145 } | |
| 1146 | |
| 1147 void | |
| 1148 gaim_xfer_error(GaimXferType type, GaimAccount *account, const char *who, const char *msg) | |
| 1149 { | |
| 1150 char *title; | |
| 1151 | |
| 1152 g_return_if_fail(msg != NULL); | |
| 1153 g_return_if_fail(type != GAIM_XFER_UNKNOWN); | |
| 1154 | |
| 1155 if (account) { | |
| 1156 GaimBuddy *buddy; | |
| 1157 buddy = gaim_find_buddy(account, who); | |
| 1158 if (buddy) | |
| 1159 who = gaim_buddy_get_alias(buddy); | |
| 1160 } | |
| 1161 | |
| 1162 if (type == GAIM_XFER_SEND) | |
| 1163 title = g_strdup_printf(_("File transfer to %s failed."), who); | |
| 1164 else | |
| 1165 title = g_strdup_printf(_("File transfer from %s failed."), who); | |
| 1166 | |
| 1167 gaim_notify_error(NULL, NULL, title, msg); | |
| 1168 | |
| 1169 g_free(title); | |
| 1170 } | |
| 1171 | |
| 1172 void | |
| 1173 gaim_xfer_update_progress(GaimXfer *xfer) | |
| 1174 { | |
| 1175 GaimXferUiOps *ui_ops; | |
| 1176 | |
| 1177 g_return_if_fail(xfer != NULL); | |
| 1178 | |
| 1179 ui_ops = gaim_xfer_get_ui_ops(xfer); | |
| 1180 if (ui_ops != NULL && ui_ops->update_progress != NULL) | |
| 1181 ui_ops->update_progress(xfer, gaim_xfer_get_progress(xfer)); | |
| 1182 } | |
| 1183 | |
| 1184 | |
| 1185 /************************************************************************** | |
| 1186 * File Transfer Subsystem API | |
| 1187 **************************************************************************/ | |
| 1188 void * | |
| 1189 gaim_xfers_get_handle(void) { | |
| 1190 static int handle = 0; | |
| 1191 | |
| 1192 return &handle; | |
| 1193 } | |
| 1194 | |
| 1195 void | |
| 1196 gaim_xfers_init(void) { | |
| 1197 void *handle = gaim_xfers_get_handle(); | |
| 1198 | |
| 1199 /* register signals */ | |
| 1200 gaim_signal_register(handle, "file-recv-accept", | |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1201 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1202 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1203 GAIM_SUBTYPE_XFER)); |
| 14192 | 1204 gaim_signal_register(handle, "file-send-accept", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1205 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1206 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1207 GAIM_SUBTYPE_XFER)); |
| 14192 | 1208 gaim_signal_register(handle, "file-recv-start", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1209 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1210 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1211 GAIM_SUBTYPE_XFER)); |
| 14192 | 1212 gaim_signal_register(handle, "file-send-start", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1213 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1214 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1215 GAIM_SUBTYPE_XFER)); |
| 14192 | 1216 gaim_signal_register(handle, "file-send-cancel", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1217 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1218 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1219 GAIM_SUBTYPE_XFER)); |
| 14192 | 1220 gaim_signal_register(handle, "file-recv-cancel", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1221 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1222 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1223 GAIM_SUBTYPE_XFER)); |
| 14192 | 1224 gaim_signal_register(handle, "file-send-complete", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1225 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1226 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1227 GAIM_SUBTYPE_XFER)); |
| 14192 | 1228 gaim_signal_register(handle, "file-recv-complete", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1229 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1230 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1231 GAIM_SUBTYPE_XFER)); |
| 14192 | 1232 gaim_signal_register(handle, "file-recv-request", |
|
14618
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1233 gaim_marshal_VOID__POINTER, NULL, 1, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1234 gaim_value_new(GAIM_TYPE_SUBTYPE, |
|
2f0b4d0de5bb
[gaim-migrate @ 17346]
Etan Reisner <pidgin@unreliablesource.net>
parents:
14192
diff
changeset
|
1235 GAIM_SUBTYPE_XFER)); |
| 14192 | 1236 } |
| 1237 | |
| 1238 void | |
| 1239 gaim_xfers_uninit(void) { | |
| 1240 gaim_signals_disconnect_by_handle(gaim_xfers_get_handle()); | |
| 1241 } | |
| 1242 | |
| 1243 void | |
| 1244 gaim_xfers_set_ui_ops(GaimXferUiOps *ops) { | |
| 1245 xfer_ui_ops = ops; | |
| 1246 } | |
| 1247 | |
| 1248 GaimXferUiOps * | |
| 1249 gaim_xfers_get_ui_ops(void) { | |
| 1250 return xfer_ui_ops; | |
| 1251 } |
