Mercurial > gftp.yaz
annotate src/gtk/transfer.c @ 829:74f49d528c68
2006-10-15 Brian Masney <masneyb@gftp.org>
* src/gtk/transfer.c - split the code that checks for finished view and
edit processes into smaller functions.
* src/gtk/transfer.c - after a local file is edited, refresh the local
file status (from Nathaniel M Nelson <xatmes@.net>) (closes #311166)
author | masneyb |
---|---|
date | Sun, 15 Oct 2006 18:45:06 +0000 |
parents | d6fdfcbdb056 |
children | 885038cb945b |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* transfer.c - functions to handle transfering files */ | |
255 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 /*****************************************************************************/ | |
19 | |
518 | 20 #include "gftp-gtk.h" |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
129 | 23 static int num_transfers_in_progress = 0; |
1 | 24 |
48 | 25 int |
496 | 26 ftp_list_files (gftp_window_data * wdata) |
48 | 27 { |
355 | 28 gftpui_callback_data * cdata; |
48 | 29 |
30 gtk_label_set (GTK_LABEL (wdata->hoststxt), _("Receiving file names...")); | |
31 | |
355 | 32 cdata = g_malloc0 (sizeof (*cdata)); |
33 cdata->request = wdata->request; | |
34 cdata->uidata = wdata; | |
35 cdata->run_function = gftpui_common_run_ls; | |
511 | 36 cdata->dont_refresh = 1; |
48 | 37 |
355 | 38 gftpui_common_run_callback_function (cdata); |
48 | 39 |
355 | 40 wdata->files = cdata->files; |
41 g_free (cdata); | |
48 | 42 |
43 if (wdata->files == NULL || !GFTP_IS_CONNECTED (wdata->request)) | |
44 { | |
380 | 45 gftpui_disconnect (wdata); |
48 | 46 return (0); |
47 } | |
48 | |
49 wdata->sorted = 0; | |
129 | 50 sortrows (GTK_CLIST (wdata->listbox), -1, (gpointer) wdata); |
355 | 51 |
48 | 52 if (IS_NONE_SELECTED (wdata)) |
53 gtk_clist_select_row (GTK_CLIST (wdata->listbox), 0, 0); | |
355 | 54 |
48 | 55 return (1); |
56 } | |
57 | |
58 | |
1 | 59 int |
767 | 60 ftp_connect (gftp_window_data * wdata, gftp_request * request) |
1 | 61 { |
62 if (wdata->request == request) | |
367 | 63 gtk_label_set (GTK_LABEL (wdata->hoststxt), _("Connecting...")); |
1 | 64 |
377 | 65 return (gftpui_common_cmd_open (wdata, request, NULL, NULL, NULL)); |
1 | 66 } |
67 | |
68 | |
69 void | |
70 get_files (gpointer data) | |
71 { | |
72 transfer_window_files (&window2, &window1); | |
73 } | |
74 | |
75 | |
76 void | |
77 put_files (gpointer data) | |
78 { | |
79 transfer_window_files (&window1, &window2); | |
80 } | |
81 | |
82 | |
83 void | |
84 transfer_window_files (gftp_window_data * fromwdata, gftp_window_data * towdata) | |
85 { | |
86 gftp_file * tempfle, * newfle; | |
87 GList * templist, * filelist; | |
88 gftp_transfer * transfer; | |
506 | 89 int num, ret, disconnect; |
1 | 90 |
91 if (!check_status (_("Transfer Files"), fromwdata, 1, 0, 1, | |
92 towdata->request->put_file != NULL && fromwdata->request->get_file != NULL)) | |
93 return; | |
94 | |
95 if (!GFTP_IS_CONNECTED (fromwdata->request) || | |
96 !GFTP_IS_CONNECTED (towdata->request)) | |
97 { | |
677 | 98 ftp_log (gftp_logging_error, NULL, |
1 | 99 _("Retrieve Files: Not connected to a remote site\n")); |
100 return; | |
101 } | |
102 | |
103 if (check_reconnect (fromwdata) < 0 || check_reconnect (towdata) < 0) | |
104 return; | |
105 | |
106 transfer = g_malloc0 (sizeof (*transfer)); | |
368 | 107 transfer->fromreq = gftp_copy_request (fromwdata->request); |
108 transfer->toreq = gftp_copy_request (towdata->request); | |
1 | 109 transfer->fromwdata = fromwdata; |
110 transfer->towdata = towdata; | |
111 | |
112 num = 0; | |
525 | 113 templist = gftp_gtk_get_list_selection (fromwdata); |
1 | 114 filelist = fromwdata->files; |
115 while (templist != NULL) | |
116 { | |
117 templist = get_next_selection (templist, &filelist, &num); | |
118 tempfle = filelist->data; | |
119 if (strcmp (tempfle->file, "..") != 0) | |
120 { | |
121 newfle = copy_fdata (tempfle); | |
122 transfer->files = g_list_append (transfer->files, newfle); | |
123 } | |
124 } | |
125 | |
126 if (transfer->files != NULL) | |
127 { | |
63 | 128 gftp_swap_socks (transfer->fromreq, fromwdata->request); |
129 gftp_swap_socks (transfer->toreq, towdata->request); | |
1 | 130 |
767 | 131 ret = gftp_gtk_get_subdirs (transfer); |
506 | 132 if (ret < 0) |
133 disconnect = 1; | |
1 | 134 else |
506 | 135 disconnect = 0; |
1 | 136 |
137 if (!GFTP_IS_CONNECTED (transfer->fromreq)) | |
138 { | |
380 | 139 gftpui_disconnect (fromwdata); |
506 | 140 disconnect = 1; |
1 | 141 } |
142 | |
143 if (!GFTP_IS_CONNECTED (transfer->toreq)) | |
144 { | |
380 | 145 gftpui_disconnect (towdata); |
506 | 146 disconnect = 1; |
147 } | |
148 | |
149 if (disconnect) | |
150 { | |
151 free_tdata (transfer); | |
1 | 152 return; |
506 | 153 } |
1 | 154 |
63 | 155 gftp_swap_socks (fromwdata->request, transfer->fromreq); |
156 gftp_swap_socks (towdata->request, transfer->toreq); | |
1 | 157 } |
158 | |
159 if (transfer->files != NULL) | |
160 { | |
367 | 161 gftpui_common_add_file_transfer (transfer->fromreq, transfer->toreq, |
162 transfer->fromwdata, transfer->towdata, | |
163 transfer->files); | |
1 | 164 g_free (transfer); |
165 } | |
166 else | |
129 | 167 free_tdata (transfer); |
1 | 168 } |
169 | |
506 | 170 |
508 | 171 static int |
511 | 172 gftpui_gtk_tdata_connect (gftpui_callback_data * cdata) |
173 { | |
174 gftp_transfer * tdata; | |
175 int ret; | |
176 | |
177 tdata = cdata->user_data; | |
178 | |
179 if (tdata->fromreq != NULL) | |
180 { | |
181 ret = gftp_connect (tdata->fromreq); | |
182 if (ret < 0) | |
183 return (ret); | |
184 } | |
185 | |
186 if (tdata->toreq != NULL) | |
187 { | |
188 ret = gftp_connect (tdata->toreq); | |
189 if (ret < 0) | |
190 return (ret); | |
191 } | |
192 | |
193 return (0); | |
194 } | |
195 | |
196 | |
197 static void | |
198 gftpui_gtk_tdata_disconnect (gftpui_callback_data * cdata) | |
199 { | |
200 gftp_transfer * tdata; | |
201 | |
202 tdata = cdata->user_data; | |
203 | |
204 if (tdata->fromreq != NULL) | |
205 gftp_disconnect (tdata->fromreq); | |
206 | |
207 if (tdata->toreq != NULL) | |
208 gftp_disconnect (tdata->toreq); | |
209 | |
210 cdata->request->datafd = -1; | |
211 } | |
212 | |
213 | |
214 static int | |
508 | 215 _gftp_getdir_thread (gftpui_callback_data * cdata) |
1 | 216 { |
508 | 217 return (gftp_get_all_subdirs (cdata->user_data, NULL)); |
1 | 218 } |
219 | |
220 | |
506 | 221 int |
767 | 222 gftp_gtk_get_subdirs (gftp_transfer * transfer) |
506 | 223 { |
508 | 224 gftpui_callback_data * cdata; |
506 | 225 long numfiles, numdirs; |
226 guint timeout_num; | |
508 | 227 int ret; |
506 | 228 |
508 | 229 cdata = g_malloc0 (sizeof (*cdata)); |
230 cdata->user_data = transfer; | |
231 cdata->uidata = transfer->fromwdata; | |
232 cdata->request = ((gftp_window_data *) transfer->fromwdata)->request; | |
233 cdata->run_function = _gftp_getdir_thread; | |
511 | 234 cdata->connect_function = gftpui_gtk_tdata_connect; |
235 cdata->disconnect_function = gftpui_gtk_tdata_disconnect; | |
236 cdata->dont_check_connection = 1; | |
237 cdata->dont_refresh = 1; | |
506 | 238 |
508 | 239 timeout_num = gtk_timeout_add (100, progress_timeout, transfer); |
240 ret = gftpui_common_run_callback_function (cdata); | |
241 gtk_timeout_remove (timeout_num); | |
506 | 242 |
508 | 243 numfiles = transfer->numfiles; |
244 numdirs = transfer->numdirs; | |
245 transfer->numfiles = transfer->numdirs = -1; | |
246 update_directory_download_progress (transfer); | |
247 transfer->numfiles = numfiles; | |
248 transfer->numdirs = numdirs; | |
506 | 249 |
508 | 250 g_free (cdata); |
506 | 251 |
508 | 252 return (ret); |
506 | 253 } |
254 | |
255 | |
305 | 256 static void |
770 | 257 remove_file (gftp_viewedit_data * ve_proc) |
48 | 258 { |
770 | 259 if (ve_proc->remote_filename == NULL) |
260 return; | |
261 | |
262 if (unlink (ve_proc->filename) == 0) | |
48 | 263 ftp_log (gftp_logging_misc, NULL, _("Successfully removed %s\n"), |
770 | 264 ve_proc->filename); |
48 | 265 else |
266 ftp_log (gftp_logging_error, NULL, | |
770 | 267 _("Error: Could not remove file %s: %s\n"), ve_proc->filename, |
48 | 268 g_strerror (errno)); |
269 } | |
270 | |
271 | |
272 static void | |
273 free_edit_data (gftp_viewedit_data * ve_proc) | |
274 { | |
275 int i; | |
276 | |
294 | 277 if (ve_proc->torequest) |
278 gftp_request_destroy (ve_proc->torequest, 1); | |
48 | 279 if (ve_proc->filename) |
280 g_free (ve_proc->filename); | |
281 if (ve_proc->remote_filename) | |
282 g_free (ve_proc->remote_filename); | |
283 for (i = 0; ve_proc->argv[i] != NULL; i++) | |
284 g_free (ve_proc->argv[i]); | |
285 g_free (ve_proc->argv); | |
286 g_free (ve_proc); | |
287 } | |
288 | |
289 | |
290 static void | |
291 dont_upload (gftp_viewedit_data * ve_proc, gftp_dialog_data * ddata) | |
1 | 292 { |
770 | 293 remove_file (ve_proc); |
48 | 294 free_edit_data (ve_proc); |
295 } | |
296 | |
297 | |
298 static void | |
299 do_upload (gftp_viewedit_data * ve_proc, gftp_dialog_data * ddata) | |
300 { | |
303 | 301 gftp_transfer * tdata; |
48 | 302 gftp_file * tempfle; |
303 GList * newfile; | |
1 | 304 |
48 | 305 tempfle = g_malloc0 (sizeof (*tempfle)); |
555 | 306 tempfle->destfile = gftp_build_path (ve_proc->torequest, |
307 ve_proc->torequest->directory, | |
294 | 308 ve_proc->remote_filename, NULL); |
48 | 309 ve_proc->remote_filename = NULL; |
310 tempfle->file = ve_proc->filename; | |
311 ve_proc->filename = NULL; | |
312 tempfle->done_rm = 1; | |
313 newfile = g_list_append (NULL, tempfle); | |
367 | 314 tdata = gftpui_common_add_file_transfer (ve_proc->fromwdata->request, |
315 ve_proc->torequest, | |
316 ve_proc->fromwdata, | |
317 ve_proc->towdata, newfile); | |
48 | 318 free_edit_data (ve_proc); |
303 | 319 |
320 if (tdata != NULL) | |
321 tdata->conn_error_no_timeout = 1; | |
48 | 322 } |
323 | |
1 | 324 |
829 | 325 static int |
326 _check_viewedit_process_status (gftp_viewedit_data * ve_proc, int ret) | |
327 { | |
328 int procret; | |
329 | |
330 if (WIFEXITED (ret)) | |
331 { | |
332 procret = WEXITSTATUS (ret); | |
333 if (procret != 0) | |
334 { | |
335 ftp_log (gftp_logging_error, NULL, | |
336 _("Error: Child %d returned %d\n"), ve_proc->pid, procret); | |
337 if (ve_proc->view) | |
338 remove_file (ve_proc); | |
339 | |
340 return (0); | |
341 } | |
342 else | |
343 { | |
344 ftp_log (gftp_logging_misc, NULL, | |
345 _("Child %d returned successfully\n"), ve_proc->pid); | |
346 return (1); | |
347 } | |
348 } | |
349 else | |
350 { | |
351 ftp_log (gftp_logging_error, NULL, | |
352 _("Error: Child %d did not terminate properly\n"), | |
353 ve_proc->pid); | |
354 return (0); | |
355 } | |
356 } | |
357 | |
358 | |
359 static int | |
360 _prompt_to_upload_edited_file (gftp_viewedit_data * ve_proc) | |
361 { | |
362 struct stat st; | |
363 char *str; | |
364 | |
365 if (stat (ve_proc->filename, &st) == -1) | |
366 { | |
367 ftp_log (gftp_logging_error, NULL, | |
368 _("Error: Cannot get information about file %s: %s\n"), | |
369 ve_proc->filename, g_strerror (errno)); | |
370 return (0); | |
371 } | |
372 else if (st.st_mtime == ve_proc->st.st_mtime) | |
373 { | |
374 ftp_log (gftp_logging_misc, NULL, _("File %s was not changed\n"), | |
375 ve_proc->filename); | |
376 remove_file (ve_proc); | |
377 return (0); | |
378 } | |
379 else | |
380 { | |
381 memcpy (&ve_proc->st, &st, sizeof (ve_proc->st)); | |
382 str = g_strdup_printf (_("File %s has changed.\nWould you like to upload it?"), | |
383 ve_proc->remote_filename); | |
384 | |
385 MakeYesNoDialog (_("Edit File"), str, do_upload, ve_proc, dont_upload, | |
386 ve_proc); | |
387 g_free (str); | |
388 return (1); | |
389 } | |
390 } | |
391 | |
392 | |
48 | 393 static void |
829 | 394 do_check_done_process (pid_t pid, int ret) |
48 | 395 { |
396 gftp_viewedit_data * ve_proc; | |
397 GList * curdata, *deldata; | |
829 | 398 int ok; |
399 | |
400 curdata = viewedit_processes; | |
401 while (curdata != NULL) | |
402 { | |
403 ve_proc = curdata->data; | |
404 if (ve_proc->pid != pid) | |
405 continue; | |
406 | |
407 printf ("Found pid %d in the linked list\n", pid); | |
408 deldata = curdata; | |
409 curdata = curdata->next; | |
410 | |
411 viewedit_processes = g_list_remove_link (viewedit_processes, | |
412 deldata); | |
413 | |
414 ok = _check_viewedit_process_status (ve_proc, ret); | |
415 if (!ve_proc->view && ve_proc->dontupload) | |
416 gftpui_refresh (ve_proc->fromwdata, 1); | |
417 | |
418 if (ok && !ve_proc->view && !ve_proc->dontupload) | |
419 { | |
420 /* We were editing the file. Upload it */ | |
421 if (_prompt_to_upload_edited_file (ve_proc)) | |
422 break; /* Don't free the ve_proc structure */ | |
423 } | |
424 else if (ve_proc->view && ve_proc->rm) | |
425 { | |
426 /* After viewing the file delete the tmp file */ | |
427 remove_file (ve_proc); | |
428 } | |
429 | |
430 free_edit_data (ve_proc); | |
431 break; | |
432 } | |
433 } | |
434 | |
435 | |
436 static void | |
437 check_done_process (void) | |
438 { | |
48 | 439 pid_t pid; |
829 | 440 int ret; |
48 | 441 |
470 | 442 gftpui_common_child_process_done = 0; |
48 | 443 while ((pid = waitpid (-1, &ret, WNOHANG)) > 0) |
1 | 444 { |
829 | 445 printf ("FIXME - PID %d returned %d\n", pid, ret); |
446 do_check_done_process (pid, ret); | |
1 | 447 } |
448 } | |
449 | |
450 | |
451 static void | |
452 on_next_transfer (gftp_transfer * tdata) | |
453 { | |
326 | 454 int fd; |
455 intptr_t refresh_files; | |
1 | 456 gftp_file * tempfle; |
457 | |
458 tdata->next_file = 0; | |
459 for (; tdata->updfle != tdata->curfle; tdata->updfle = tdata->updfle->next) | |
460 { | |
461 tempfle = tdata->updfle->data; | |
462 | |
463 if (tempfle->is_fd) | |
58 | 464 fd = tempfle->fd; |
1 | 465 else |
466 fd = 0; | |
467 | |
468 if (tempfle->done_view) | |
469 { | |
470 if (tempfle->transfer_action != GFTP_TRANS_ACTION_SKIP) | |
471 view_file (tempfle->destfile, fd, 1, tempfle->done_rm, 1, 0, | |
472 tempfle->file, NULL); | |
473 | |
474 if (tempfle->is_fd) | |
475 { | |
58 | 476 close (tempfle->fd); |
477 tempfle->fd = -1; | |
1 | 478 } |
479 } | |
480 else if (tempfle->done_edit) | |
481 { | |
482 if (tempfle->transfer_action != GFTP_TRANS_ACTION_SKIP) | |
483 view_file (tempfle->destfile, fd, 0, tempfle->done_rm, 1, 0, | |
484 tempfle->file, NULL); | |
485 | |
486 if (tempfle->is_fd) | |
487 { | |
58 | 488 close (tempfle->fd); |
489 tempfle->fd = -1; | |
1 | 490 } |
491 } | |
492 else if (tempfle->done_rm) | |
493 tdata->fromreq->rmfile (tdata->fromreq, tempfle->file); | |
494 | |
495 if (tempfle->transfer_action == GFTP_TRANS_ACTION_SKIP) | |
129 | 496 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), tempfle->user_data, 1, |
1 | 497 _("Skipped")); |
498 else | |
129 | 499 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), tempfle->user_data, 1, |
1 | 500 _("Finished")); |
501 } | |
502 | |
129 | 503 gftp_lookup_request_option (tdata->fromreq, "refresh_files", &refresh_files); |
504 | |
1 | 505 if (refresh_files && tdata->curfle && tdata->curfle->next && |
506 compare_request (tdata->toreq, | |
507 ((gftp_window_data *) tdata->towdata)->request, 1)) | |
514 | 508 gftpui_refresh (tdata->towdata, 1); |
1 | 509 } |
510 | |
511 | |
512 static void | |
19 | 513 get_trans_password (gftp_request * request, gftp_dialog_data * ddata) |
514 { | |
515 gftp_set_password (request, gtk_entry_get_text (GTK_ENTRY (ddata->edit))); | |
516 request->stopable = 0; | |
517 } | |
518 | |
519 | |
520 static void | |
521 cancel_get_trans_password (gftp_transfer * tdata, gftp_dialog_data * ddata) | |
522 { | |
523 if (tdata->fromreq->stopable == 0) | |
524 return; | |
525 | |
819 | 526 gftpui_common_cancel_file_transfer (tdata); |
19 | 527 } |
528 | |
529 | |
530 static void | |
1 | 531 show_transfer (gftp_transfer * tdata) |
532 { | |
533 GdkPixmap * closedir_pixmap, * opendir_pixmap; | |
534 GdkBitmap * closedir_bitmap, * opendir_bitmap; | |
367 | 535 gftpui_common_curtrans_data * transdata; |
1 | 536 gftp_file * tempfle; |
537 GList * templist; | |
397 | 538 char *text[2]; |
1 | 539 |
540 gftp_get_pixmap (dlwdw, "open_dir.xpm", &opendir_pixmap, &opendir_bitmap); | |
541 gftp_get_pixmap (dlwdw, "dir.xpm", &closedir_pixmap, &closedir_bitmap); | |
542 | |
129 | 543 text[0] = tdata->fromreq->hostname; |
1 | 544 text[1] = _("Waiting..."); |
129 | 545 tdata->user_data = gtk_ctree_insert_node (GTK_CTREE (dlwdw), NULL, NULL, |
546 text, 5, | |
1 | 547 closedir_pixmap, closedir_bitmap, |
548 opendir_pixmap, opendir_bitmap, | |
549 FALSE, | |
550 tdata->numdirs + tdata->numfiles < 50); | |
551 transdata = g_malloc (sizeof (*transdata)); | |
552 transdata->transfer = tdata; | |
553 transdata->curfle = NULL; | |
129 | 554 gtk_ctree_node_set_row_data (GTK_CTREE (dlwdw), tdata->user_data, transdata); |
1 | 555 tdata->show = 0; |
556 tdata->curfle = tdata->updfle = tdata->files; | |
557 | |
558 tdata->total_bytes = 0; | |
559 for (templist = tdata->files; templist != NULL; templist = templist->next) | |
560 { | |
561 tempfle = templist->data; | |
397 | 562 |
563 text[0] = gftpui_gtk_get_utf8_file_pos (tempfle); | |
1 | 564 if (tempfle->transfer_action == GFTP_TRANS_ACTION_SKIP) |
565 text[1] = _("Skipped"); | |
566 else | |
567 { | |
568 tdata->total_bytes += tempfle->size; | |
569 text[1] = _("Waiting..."); | |
570 } | |
571 | |
129 | 572 tempfle->user_data = gtk_ctree_insert_node (GTK_CTREE (dlwdw), |
573 tdata->user_data, | |
1 | 574 NULL, text, 5, NULL, NULL, NULL, |
575 NULL, FALSE, FALSE); | |
576 transdata = g_malloc (sizeof (*transdata)); | |
577 transdata->transfer = tdata; | |
578 transdata->curfle = templist; | |
129 | 579 gtk_ctree_node_set_row_data (GTK_CTREE (dlwdw), tempfle->user_data, |
580 transdata); | |
1 | 581 } |
582 | |
553 | 583 if (!tdata->toreq->stopable && gftp_need_password (tdata->toreq)) |
1 | 584 { |
585 tdata->toreq->stopable = 1; | |
586 MakeEditDialog (_("Enter Password"), | |
587 _("Please enter your password for this site"), NULL, 0, | |
19 | 588 NULL, gftp_dialog_button_connect, |
589 get_trans_password, tdata->toreq, | |
590 cancel_get_trans_password, tdata); | |
1 | 591 } |
592 | |
553 | 593 if (!tdata->fromreq->stopable && gftp_need_password (tdata->fromreq)) |
1 | 594 { |
595 tdata->fromreq->stopable = 1; | |
596 MakeEditDialog (_("Enter Password"), | |
597 _("Please enter your password for this site"), NULL, 0, | |
19 | 598 NULL, gftp_dialog_button_connect, |
599 get_trans_password, tdata->fromreq, | |
600 cancel_get_trans_password, tdata); | |
1 | 601 } |
602 } | |
603 | |
604 | |
605 static void | |
606 transfer_done (GList * node) | |
607 { | |
367 | 608 gftpui_common_curtrans_data * transdata; |
1 | 609 gftp_transfer * tdata; |
610 gftp_file * tempfle; | |
611 GList * templist; | |
612 | |
613 tdata = node->data; | |
614 if (tdata->started) | |
615 { | |
297 | 616 if (GFTP_IS_SAME_HOST_STOP_TRANS ((gftp_window_data *) tdata->fromwdata, |
617 tdata->fromreq)) | |
309 | 618 { |
619 gftp_copy_param_options (((gftp_window_data *) tdata->fromwdata)->request, tdata->fromreq); | |
620 | |
621 gftp_swap_socks (((gftp_window_data *) tdata->fromwdata)->request, | |
622 tdata->fromreq); | |
623 } | |
1 | 624 else |
297 | 625 gftp_disconnect (tdata->fromreq); |
626 | |
627 if (GFTP_IS_SAME_HOST_STOP_TRANS ((gftp_window_data *) tdata->towdata, | |
628 tdata->toreq)) | |
309 | 629 { |
630 gftp_copy_param_options (((gftp_window_data *) tdata->towdata)->request, tdata->toreq); | |
631 | |
632 gftp_swap_socks (((gftp_window_data *) tdata->towdata)->request, | |
633 tdata->toreq); | |
634 } | |
297 | 635 else |
636 gftp_disconnect (tdata->toreq); | |
1 | 637 |
305 | 638 if (tdata->towdata != NULL && compare_request (tdata->toreq, |
639 ((gftp_window_data *) tdata->towdata)->request, 1)) | |
514 | 640 gftpui_refresh (tdata->towdata, 1); |
305 | 641 |
129 | 642 num_transfers_in_progress--; |
1 | 643 } |
644 | |
785 | 645 if ((!tdata->show && tdata->started) || |
646 (tdata->done && !tdata->started)) | |
1 | 647 { |
129 | 648 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), |
649 tdata->user_data); | |
1 | 650 if (transdata != NULL) |
651 g_free (transdata); | |
652 | |
653 for (templist = tdata->files; templist != NULL; templist = templist->next) | |
654 { | |
655 tempfle = templist->data; | |
129 | 656 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), |
657 tempfle->user_data); | |
1 | 658 if (transdata != NULL) |
659 g_free (transdata); | |
660 } | |
661 | |
129 | 662 gtk_ctree_remove_node (GTK_CTREE (dlwdw), tdata->user_data); |
1 | 663 } |
129 | 664 |
367 | 665 g_static_mutex_lock (&gftpui_common_transfer_mutex); |
129 | 666 gftp_file_transfers = g_list_remove_link (gftp_file_transfers, node); |
367 | 667 g_static_mutex_unlock (&gftpui_common_transfer_mutex); |
668 | |
320 | 669 gdk_window_set_title (gtk_widget_get_parent_window (GTK_WIDGET(dlwdw)), |
670 gftp_version); | |
129 | 671 |
1 | 672 free_tdata (tdata); |
673 } | |
674 | |
675 | |
367 | 676 static void * |
677 _gftpui_transfer_files (void *data) | |
678 { | |
679 int ret; | |
680 | |
681 pthread_detach (pthread_self ()); | |
682 ret = gftpui_common_transfer_files (data); | |
683 return (GINT_TO_POINTER(ret)); | |
684 } | |
685 | |
686 | |
1 | 687 static void |
688 create_transfer (gftp_transfer * tdata) | |
689 { | |
690 pthread_t tid; | |
691 | |
692 if (!tdata->fromreq->stopable) | |
693 { | |
297 | 694 if (GFTP_IS_SAME_HOST_START_TRANS ((gftp_window_data *) tdata->fromwdata, |
695 tdata->fromreq)) | |
696 { | |
697 gftp_swap_socks (tdata->fromreq, | |
698 ((gftp_window_data *) tdata->fromwdata)->request); | |
699 update_window (tdata->fromwdata); | |
700 } | |
701 | |
702 if (GFTP_IS_SAME_HOST_START_TRANS ((gftp_window_data *) tdata->towdata, | |
703 tdata->toreq)) | |
704 { | |
63 | 705 gftp_swap_socks (tdata->toreq, |
706 ((gftp_window_data *) tdata->towdata)->request); | |
297 | 707 update_window (tdata->towdata); |
1 | 708 } |
297 | 709 |
129 | 710 num_transfers_in_progress++; |
1 | 711 tdata->started = 1; |
712 tdata->stalled = 1; | |
129 | 713 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), tdata->user_data, 1, |
1 | 714 _("Connecting...")); |
367 | 715 pthread_create (&tid, NULL, _gftpui_transfer_files, tdata); |
1 | 716 } |
717 } | |
718 | |
719 | |
720 static void | |
753 | 721 _setup_dlstr (gftp_transfer * tdata, gftp_file * fle, char *dlstr, |
722 size_t dlstr_len) | |
723 { | |
767 | 724 int hours, mins, secs, stalled, usesentdescr; |
753 | 725 unsigned long remaining_secs, lkbs; |
726 char gotstr[50], ofstr[50]; | |
727 struct timeval tv; | |
728 | |
767 | 729 stalled = 1; |
753 | 730 gettimeofday (&tv, NULL); |
731 usesentdescr = (tdata->fromreq->protonum == GFTP_LOCAL_NUM); | |
732 | |
733 insert_commas (fle->size, ofstr, sizeof (ofstr)); | |
734 insert_commas (tdata->curtrans + tdata->curresumed, gotstr, sizeof (gotstr)); | |
735 | |
736 if (tv.tv_sec - tdata->lasttime.tv_sec <= 5) | |
737 { | |
767 | 738 remaining_secs = (fle->size - tdata->curtrans - tdata->curresumed) / 1024; |
739 | |
740 lkbs = (unsigned long) tdata->kbs; | |
741 if (lkbs > 0) | |
742 remaining_secs /= lkbs; | |
753 | 743 |
767 | 744 hours = remaining_secs / 3600; |
745 remaining_secs -= hours * 3600; | |
746 mins = remaining_secs / 60; | |
747 remaining_secs -= mins * 60; | |
748 secs = remaining_secs; | |
753 | 749 |
750 if (!(hours < 0 || mins < 0 || secs < 0)) | |
751 { | |
767 | 752 stalled = 0; |
753 | 753 if (usesentdescr) |
754 { | |
755 g_snprintf (dlstr, dlstr_len, | |
756 _("Sent %s of %s at %.2fKB/s, %02d:%02d:%02d est. time remaining"), gotstr, ofstr, tdata->kbs, hours, mins, secs); | |
757 } | |
758 else | |
759 { | |
760 g_snprintf (dlstr, dlstr_len, | |
761 _("Recv %s of %s at %.2fKB/s, %02d:%02d:%02d est. time remaining"), gotstr, ofstr, tdata->kbs, hours, mins, secs); | |
762 } | |
763 } | |
764 } | |
765 | |
767 | 766 if (stalled) |
753 | 767 { |
768 tdata->stalled = 1; | |
769 if (usesentdescr) | |
770 { | |
771 g_snprintf (dlstr, dlstr_len, | |
772 _("Sent %s of %s, transfer stalled, unknown time remaining"), | |
773 gotstr, ofstr); | |
774 } | |
775 else | |
776 { | |
777 g_snprintf (dlstr, dlstr_len, | |
778 _("Recv %s of %s, transfer stalled, unknown time remaining"), | |
779 gotstr, ofstr); | |
780 } | |
781 } | |
782 } | |
783 | |
784 | |
785 static void | |
1 | 786 update_file_status (gftp_transfer * tdata) |
787 { | |
753 | 788 char totstr[150], winstr[150], dlstr[150]; |
246 | 789 unsigned long remaining_secs, lkbs; |
767 | 790 int hours, mins, secs, pcent; |
463 | 791 intptr_t show_trans_in_title; |
1 | 792 gftp_file * tempfle; |
320 | 793 |
129 | 794 g_static_mutex_lock (&tdata->statmutex); |
1 | 795 tempfle = tdata->curfle->data; |
796 | |
220 | 797 remaining_secs = (tdata->total_bytes - tdata->trans_bytes - tdata->resumed_bytes) / 1024; |
246 | 798 |
799 lkbs = (unsigned long) tdata->kbs; | |
800 if (lkbs > 0) | |
801 remaining_secs /= lkbs; | |
1 | 802 |
220 | 803 hours = remaining_secs / 3600; |
804 remaining_secs -= hours * 3600; | |
805 mins = remaining_secs / 60; | |
806 remaining_secs -= mins * 60; | |
807 secs = remaining_secs; | |
1 | 808 |
809 if (hours < 0 || mins < 0 || secs < 0) | |
810 { | |
129 | 811 g_static_mutex_unlock (&tdata->statmutex); |
1 | 812 return; |
813 } | |
814 | |
246 | 815 if ((double) tdata->total_bytes > 0) |
816 pcent = (int) ((double) (tdata->trans_bytes + tdata->resumed_bytes) / (double) tdata->total_bytes * 100.0); | |
817 else | |
1 | 818 pcent = 0; |
819 | |
602 | 820 if (pcent > 100) |
821 g_snprintf (totstr, sizeof (totstr), | |
822 _("Unknown percentage complete. (File %ld of %ld)"), | |
823 tdata->current_file_number, tdata->numdirs + tdata->numfiles); | |
824 else | |
825 g_snprintf (totstr, sizeof (totstr), | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
826 _("%d%% complete, %02d:%02d:%02d est. time remaining. (File %ld of %ld)"), |
1 | 827 pcent, hours, mins, secs, tdata->current_file_number, |
828 tdata->numdirs + tdata->numfiles); | |
829 | |
830 *dlstr = '\0'; | |
831 if (!tdata->stalled) | |
753 | 832 _setup_dlstr (tdata, tempfle, dlstr, sizeof (dlstr)); |
1 | 833 |
129 | 834 g_static_mutex_unlock (&tdata->statmutex); |
1 | 835 |
129 | 836 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), tdata->user_data, 1, totstr); |
320 | 837 |
838 gftp_lookup_global_option ("show_trans_in_title", &show_trans_in_title); | |
839 if (gftp_file_transfers->data == tdata && show_trans_in_title) | |
840 { | |
841 g_snprintf (winstr, sizeof(winstr), "%s: %s", gftp_version, totstr); | |
842 gdk_window_set_title (gtk_widget_get_parent_window (GTK_WIDGET(dlwdw)), | |
843 winstr); | |
844 } | |
1 | 845 |
846 if (*dlstr != '\0') | |
129 | 847 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), tempfle->user_data, 1, dlstr); |
1 | 848 } |
849 | |
377 | 850 |
56 | 851 static void |
852 update_window_transfer_bytes (gftp_window_data * wdata) | |
853 { | |
854 char *tempstr, *temp1str; | |
855 | |
856 if (wdata->request->gotbytes == -1) | |
857 { | |
793 | 858 update_window (wdata); |
56 | 859 wdata->request->gotbytes = 0; |
860 } | |
861 else | |
862 { | |
863 tempstr = insert_commas (wdata->request->gotbytes, NULL, 0); | |
864 temp1str = g_strdup_printf (_("Retrieving file names...%s bytes"), | |
865 tempstr); | |
866 gtk_label_set (GTK_LABEL (wdata->hoststxt), temp1str); | |
867 g_free (tempstr); | |
868 g_free (temp1str); | |
869 } | |
870 } | |
871 | |
1 | 872 |
48 | 873 gint |
874 update_downloads (gpointer data) | |
875 { | |
433 | 876 intptr_t do_one_transfer_at_a_time, start_transfers; |
48 | 877 GList * templist, * next; |
878 gftp_transfer * tdata; | |
879 | |
129 | 880 if (gftp_file_transfer_logs != NULL) |
48 | 881 display_cached_logs (); |
882 | |
56 | 883 if (window1.request->gotbytes != 0) |
884 update_window_transfer_bytes (&window1); | |
48 | 885 if (window2.request->gotbytes != 0) |
56 | 886 update_window_transfer_bytes (&window2); |
48 | 887 |
470 | 888 if (gftpui_common_child_process_done) |
48 | 889 check_done_process (); |
890 | |
129 | 891 for (templist = gftp_file_transfers; templist != NULL;) |
48 | 892 { |
893 tdata = templist->data; | |
894 if (tdata->ready) | |
895 { | |
129 | 896 g_static_mutex_lock (&tdata->structmutex); |
48 | 897 |
898 if (tdata->next_file) | |
899 on_next_transfer (tdata); | |
900 else if (tdata->show) | |
901 show_transfer (tdata); | |
902 else if (tdata->done) | |
903 { | |
904 next = templist->next; | |
129 | 905 g_static_mutex_unlock (&tdata->structmutex); |
48 | 906 transfer_done (templist); |
907 templist = next; | |
908 continue; | |
909 } | |
910 | |
911 if (tdata->curfle != NULL) | |
912 { | |
151 | 913 gftp_lookup_global_option ("one_transfer", |
129 | 914 &do_one_transfer_at_a_time); |
433 | 915 gftp_lookup_global_option ("start_transfers", &start_transfers); |
129 | 916 |
433 | 917 if (!tdata->started && start_transfers && |
129 | 918 (num_transfers_in_progress == 0 || !do_one_transfer_at_a_time)) |
48 | 919 create_transfer (tdata); |
920 | |
921 if (tdata->started) | |
922 update_file_status (tdata); | |
923 } | |
129 | 924 g_static_mutex_unlock (&tdata->structmutex); |
48 | 925 } |
926 templist = templist->next; | |
927 } | |
928 | |
929 gtk_timeout_add (500, update_downloads, NULL); | |
930 return (0); | |
931 } | |
932 | |
933 | |
1 | 934 void |
935 start_transfer (gpointer data) | |
936 { | |
367 | 937 gftpui_common_curtrans_data * transdata; |
1 | 938 GtkCTreeNode * node; |
939 | |
940 if (GTK_CLIST (dlwdw)->selection == NULL) | |
941 { | |
677 | 942 ftp_log (gftp_logging_error, NULL, |
1 | 943 _("There are no file transfers selected\n")); |
944 return; | |
945 } | |
946 node = GTK_CLIST (dlwdw)->selection->data; | |
947 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
948 | |
129 | 949 g_static_mutex_lock (&transdata->transfer->structmutex); |
1 | 950 if (!transdata->transfer->started) |
951 create_transfer (transdata->transfer); | |
129 | 952 g_static_mutex_unlock (&transdata->transfer->structmutex); |
1 | 953 } |
954 | |
955 | |
956 void | |
957 stop_transfer (gpointer data) | |
958 { | |
367 | 959 gftpui_common_curtrans_data * transdata; |
1 | 960 GtkCTreeNode * node; |
961 | |
962 if (GTK_CLIST (dlwdw)->selection == NULL) | |
963 { | |
677 | 964 ftp_log (gftp_logging_error, NULL, |
1 | 965 _("There are no file transfers selected\n")); |
966 return; | |
967 } | |
819 | 968 |
1 | 969 node = GTK_CLIST (dlwdw)->selection->data; |
970 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
819 | 971 gftpui_common_cancel_file_transfer (transdata->transfer); |
1 | 972 } |
973 | |
974 | |
975 void | |
976 skip_transfer (gpointer data) | |
977 { | |
367 | 978 gftpui_common_curtrans_data * transdata; |
1 | 979 GtkCTreeNode * node; |
980 gftp_file * curfle; | |
40 | 981 char *file; |
1 | 982 |
983 if (GTK_CLIST (dlwdw)->selection == NULL) | |
984 { | |
677 | 985 ftp_log (gftp_logging_error, NULL, |
1 | 986 _("There are no file transfers selected\n")); |
987 return; | |
988 } | |
819 | 989 |
1 | 990 node = GTK_CLIST (dlwdw)->selection->data; |
991 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
992 | |
819 | 993 gftpui_common_skip_file_transfer (transdata->transfer, |
994 transdata->transfer->curfle->data); | |
1 | 995 } |
996 | |
997 | |
998 void | |
999 remove_file_transfer (gpointer data) | |
1000 { | |
367 | 1001 gftpui_common_curtrans_data * transdata; |
1 | 1002 GtkCTreeNode * node; |
1003 gftp_file * curfle; | |
1004 | |
1005 if (GTK_CLIST (dlwdw)->selection == NULL) | |
1006 { | |
677 | 1007 ftp_log (gftp_logging_error, NULL, |
1 | 1008 _("There are no file transfers selected\n")); |
1009 return; | |
1010 } | |
1011 | |
1012 node = GTK_CLIST (dlwdw)->selection->data; | |
1013 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
1014 | |
1015 if (transdata->curfle == NULL || transdata->curfle->data == NULL) | |
1016 return; | |
1017 | |
1018 curfle = transdata->curfle->data; | |
819 | 1019 gftpui_common_skip_file_transfer (transdata->transfer, curfle); |
1 | 1020 |
819 | 1021 gtk_ctree_node_set_text (GTK_CTREE (dlwdw), curfle->user_data, 1, |
1022 _("Skipped")); | |
1 | 1023 } |
1024 | |
1025 | |
1026 void | |
1027 move_transfer_up (gpointer data) | |
1028 { | |
1029 GList * firstentry, * secentry, * lastentry; | |
367 | 1030 gftpui_common_curtrans_data * transdata; |
1 | 1031 GtkCTreeNode * node; |
1032 | |
1033 if (GTK_CLIST (dlwdw)->selection == NULL) | |
1034 { | |
677 | 1035 ftp_log (gftp_logging_error, NULL, |
1 | 1036 _("There are no file transfers selected\n")); |
1037 return; | |
1038 } | |
1039 node = GTK_CLIST (dlwdw)->selection->data; | |
1040 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
1041 | |
1042 if (transdata->curfle == NULL) | |
1043 return; | |
1044 | |
129 | 1045 g_static_mutex_lock (&transdata->transfer->structmutex); |
1 | 1046 if (transdata->curfle->prev != NULL && (!transdata->transfer->started || |
1047 (transdata->transfer->curfle != transdata->curfle && | |
1048 transdata->transfer->curfle != transdata->curfle->prev))) | |
1049 { | |
1050 if (transdata->curfle->prev->prev == NULL) | |
1051 { | |
1052 firstentry = transdata->curfle->prev; | |
1053 lastentry = transdata->curfle->next; | |
1054 transdata->transfer->files = transdata->curfle; | |
1055 transdata->curfle->next = firstentry; | |
1056 transdata->transfer->files->prev = NULL; | |
1057 firstentry->prev = transdata->curfle; | |
1058 firstentry->next = lastentry; | |
1059 if (lastentry != NULL) | |
1060 lastentry->prev = firstentry; | |
1061 } | |
1062 else | |
1063 { | |
1064 firstentry = transdata->curfle->prev->prev; | |
1065 secentry = transdata->curfle->prev; | |
1066 lastentry = transdata->curfle->next; | |
1067 firstentry->next = transdata->curfle; | |
1068 transdata->curfle->prev = firstentry; | |
1069 transdata->curfle->next = secentry; | |
1070 secentry->prev = transdata->curfle; | |
1071 secentry->next = lastentry; | |
1072 if (lastentry != NULL) | |
1073 lastentry->prev = secentry; | |
1074 } | |
1075 | |
1076 gtk_ctree_move (GTK_CTREE (dlwdw), | |
129 | 1077 ((gftp_file *) transdata->curfle->data)->user_data, |
1078 transdata->transfer->user_data, | |
1 | 1079 transdata->curfle->next != NULL ? |
129 | 1080 ((gftp_file *) transdata->curfle->next->data)->user_data: NULL); |
1 | 1081 } |
129 | 1082 g_static_mutex_unlock (&transdata->transfer->structmutex); |
1 | 1083 } |
1084 | |
367 | 1085 |
1 | 1086 void |
1087 move_transfer_down (gpointer data) | |
1088 { | |
1089 GList * firstentry, * secentry, * lastentry; | |
367 | 1090 gftpui_common_curtrans_data * transdata; |
1 | 1091 GtkCTreeNode * node; |
1092 | |
1093 if (GTK_CLIST (dlwdw)->selection == NULL) | |
1094 { | |
677 | 1095 ftp_log (gftp_logging_error, NULL, |
1 | 1096 _("There are no file transfers selected\n")); |
1097 return; | |
1098 } | |
1099 node = GTK_CLIST (dlwdw)->selection->data; | |
1100 transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node); | |
1101 | |
1102 if (transdata->curfle == NULL) | |
1103 return; | |
1104 | |
129 | 1105 g_static_mutex_lock (&transdata->transfer->structmutex); |
1 | 1106 if (transdata->curfle->next != NULL && (!transdata->transfer->started || |
1107 (transdata->transfer->curfle != transdata->curfle && | |
1108 transdata->transfer->curfle != transdata->curfle->next))) | |
1109 { | |
1110 if (transdata->curfle->prev == NULL) | |
1111 { | |
1112 firstentry = transdata->curfle->next; | |
1113 lastentry = transdata->curfle->next->next; | |
1114 transdata->transfer->files = firstentry; | |
1115 transdata->transfer->files->prev = NULL; | |
1116 transdata->transfer->files->next = transdata->curfle; | |
1117 transdata->curfle->prev = transdata->transfer->files; | |
1118 transdata->curfle->next = lastentry; | |
1119 if (lastentry != NULL) | |
1120 lastentry->prev = transdata->curfle; | |
1121 } | |
1122 else | |
1123 { | |
1124 firstentry = transdata->curfle->prev; | |
1125 secentry = transdata->curfle->next; | |
1126 lastentry = transdata->curfle->next->next; | |
1127 firstentry->next = secentry; | |
1128 secentry->prev = firstentry; | |
1129 secentry->next = transdata->curfle; | |
1130 transdata->curfle->prev = secentry; | |
1131 transdata->curfle->next = lastentry; | |
1132 if (lastentry != NULL) | |
1133 lastentry->prev = transdata->curfle; | |
1134 } | |
1135 | |
1136 gtk_ctree_move (GTK_CTREE (dlwdw), | |
129 | 1137 ((gftp_file *) transdata->curfle->data)->user_data, |
1138 transdata->transfer->user_data, | |
1 | 1139 transdata->curfle->next != NULL ? |
129 | 1140 ((gftp_file *) transdata->curfle->next->data)->user_data: NULL); |
1 | 1141 } |
129 | 1142 g_static_mutex_unlock (&transdata->transfer->structmutex); |
1 | 1143 } |
1144 |