2015-03-28 07:56:54 +01:00
|
|
|
/* file_transfers.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <string.h>
|
2015-03-28 07:56:54 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "file_transfers.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "friendlist.h"
|
2015-03-28 07:56:54 +01:00
|
|
|
#include "line_info.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "notify.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2015-03-28 07:56:54 +01:00
|
|
|
|
|
|
|
extern FriendsList Friends;
|
|
|
|
|
2015-08-18 21:12:48 +02:00
|
|
|
/* number of "#"'s in file transfer progress bar. Keep well below MAX_STR_SIZE */
|
|
|
|
#define NUM_PROG_MARKS 50
|
2020-10-30 01:28:09 +01:00
|
|
|
#define STR_BUF_SIZE 30
|
2015-08-18 21:12:48 +02:00
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
/* creates initial progress line that will be updated during file transfer.
|
2015-12-05 01:41:50 +01:00
|
|
|
Assumes progline has room for at least MAX_STR_SIZE bytes */
|
2015-08-18 21:12:48 +02:00
|
|
|
void init_progress_bar(char *progline)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2015-12-05 01:41:50 +01:00
|
|
|
strcpy(progline, "0% [");
|
2015-03-28 07:56:54 +01:00
|
|
|
int i;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (i = 0; i < NUM_PROG_MARKS; ++i) {
|
2015-03-28 07:56:54 +01:00
|
|
|
strcat(progline, "-");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-12-05 01:41:50 +01:00
|
|
|
strcat(progline, "] 0.0 B/s");
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2015-12-05 01:41:50 +01:00
|
|
|
/* prints a progress bar for file transfers. */
|
2015-03-28 07:56:54 +01:00
|
|
|
void print_progress_bar(ToxWindow *self, double bps, double pct_done, uint32_t line_id)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (bps < 0 || pct_done < 0 || pct_done > 100) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
|
2020-11-26 21:35:04 +01:00
|
|
|
char pct_str[STR_BUF_SIZE];
|
2015-12-05 01:41:50 +01:00
|
|
|
snprintf(pct_str, sizeof(pct_str), "%.1f%%", pct_done);
|
|
|
|
|
2020-11-26 21:35:04 +01:00
|
|
|
char bps_str[STR_BUF_SIZE];
|
2015-12-05 01:41:50 +01:00
|
|
|
bytes_convert_str(bps_str, sizeof(bps_str), bps);
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2020-11-26 21:35:04 +01:00
|
|
|
char prog_line[NUM_PROG_MARKS + 1];
|
|
|
|
prog_line[0] = 0;
|
|
|
|
|
2015-03-28 07:56:54 +01:00
|
|
|
int n = pct_done / (100 / NUM_PROG_MARKS);
|
|
|
|
int i, j;
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (i = 0; i < n; ++i) {
|
2015-12-05 01:41:50 +01:00
|
|
|
strcat(prog_line, "=");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (pct_done < 100) {
|
2015-12-05 01:41:50 +01:00
|
|
|
strcpy(prog_line + n, ">");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
for (j = i; j < NUM_PROG_MARKS - 1; ++j) {
|
2015-12-05 01:41:50 +01:00
|
|
|
strcat(prog_line, "-");
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2020-10-28 05:54:06 +01:00
|
|
|
size_t line_buf_size = strlen(pct_str) + NUM_PROG_MARKS + strlen(bps_str) + 7;
|
|
|
|
char *full_line = malloc(line_buf_size);
|
|
|
|
|
|
|
|
if (full_line == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(full_line, line_buf_size, "%s [%s] %s/s", pct_str, prog_line, bps_str);
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-12-05 01:41:50 +01:00
|
|
|
line_info_set(self, line_id, full_line);
|
2020-10-28 05:54:06 +01:00
|
|
|
|
|
|
|
free(full_line);
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 19:57:00 +01:00
|
|
|
static void refresh_progress_helper(ToxWindow *self, struct FileTransfer *ft)
|
2015-03-30 00:33:51 +02:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->state == FILE_TRANSFER_INACTIVE) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
|
|
|
|
/* Timeout must be set to 1 second to show correct bytes per second */
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!timed_out(ft->last_line_progress, 1)) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
double remain = ft->file_size - ft->position;
|
|
|
|
double pct_done = remain > 0 ? (1 - (remain / ft->file_size)) * 100 : 100;
|
|
|
|
print_progress_bar(self, ft->bps, pct_done, ft->line_id);
|
|
|
|
|
|
|
|
ft->bps = 0;
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->last_line_progress = get_unix_time();
|
2015-03-30 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 21:12:48 +02:00
|
|
|
/* refreshes active file transfer status bars. */
|
2020-11-14 01:38:33 +01:00
|
|
|
void refresh_file_transfer_progress(ToxWindow *self, uint32_t friendnumber)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2020-11-14 01:38:33 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
|
|
|
refresh_progress_helper(self, &Friends.list[friendnumber].file_receiver[i]);
|
|
|
|
refresh_progress_helper(self, &Friends.list[friendnumber].file_sender[i]);
|
2015-03-30 00:33:51 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 03:25:27 +01:00
|
|
|
static void clear_file_transfer(struct FileTransfer *ft)
|
|
|
|
{
|
|
|
|
*ft = (struct FileTransfer) {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
/* Returns a pointer to friendnumber's FileTransfer struct associated with filenumber.
|
|
|
|
* Returns NULL if filenumber is invalid.
|
2015-03-30 00:33:51 +02:00
|
|
|
*/
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *get_file_transfer_struct(uint32_t friendnumber, uint32_t filenumber)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2020-11-11 03:25:27 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *ft_send = &Friends.list[friendnumber].file_sender[i];
|
2015-03-30 00:33:51 +02:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
if (ft_send->state != FILE_TRANSFER_INACTIVE && ft_send->filenumber == filenumber) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return ft_send;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *ft_recv = &Friends.list[friendnumber].file_receiver[i];
|
2015-03-30 00:33:51 +02:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
if (ft_recv->state != FILE_TRANSFER_INACTIVE && ft_recv->filenumber == filenumber) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return ft_recv;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-30 01:05:24 +02:00
|
|
|
/* Returns a pointer to the FileTransfer struct associated with index with the direction specified.
|
2015-03-30 00:33:51 +02:00
|
|
|
* Returns NULL on failure.
|
|
|
|
*/
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *get_file_transfer_struct_index(uint32_t friendnumber, uint32_t index,
|
2016-09-25 03:07:04 +02:00
|
|
|
FILE_TRANSFER_DIRECTION direction)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (direction != FILE_TRANSFER_RECV && direction != FILE_TRANSFER_SEND) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return NULL;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
|
2020-11-11 03:25:27 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
2015-03-30 00:33:51 +02:00
|
|
|
struct FileTransfer *ft = direction == FILE_TRANSFER_SEND ?
|
2020-11-14 01:38:33 +01:00
|
|
|
&Friends.list[friendnumber].file_sender[i] :
|
|
|
|
&Friends.list[friendnumber].file_receiver[i];
|
2015-03-30 01:26:16 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->state != FILE_TRANSFER_INACTIVE && ft->index == index) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return ft;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
/* Returns a pointer to an unused file sender.
|
|
|
|
* Returns NULL if all file senders are in use.
|
|
|
|
*/
|
2020-11-14 01:38:33 +01:00
|
|
|
static struct FileTransfer *new_file_sender(ToxWindow *window, uint32_t friendnumber, uint32_t filenumber, uint8_t type)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2020-11-11 03:25:27 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *ft = &Friends.list[friendnumber].file_sender[i];
|
2015-03-30 00:33:51 +02:00
|
|
|
|
|
|
|
if (ft->state == FILE_TRANSFER_INACTIVE) {
|
2020-11-11 03:25:27 +01:00
|
|
|
clear_file_transfer(ft);
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->window = window;
|
2015-03-30 00:33:51 +02:00
|
|
|
ft->index = i;
|
2020-11-14 01:38:33 +01:00
|
|
|
ft->friendnumber = friendnumber;
|
|
|
|
ft->filenumber = filenumber;
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->file_type = type;
|
|
|
|
ft->state = FILE_TRANSFER_PENDING;
|
2015-03-30 00:33:51 +02:00
|
|
|
return ft;
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
/* Returns a pointer to an unused file receiver.
|
|
|
|
* Returns NULL if all file receivers are in use.
|
|
|
|
*/
|
2020-11-14 01:38:33 +01:00
|
|
|
static struct FileTransfer *new_file_receiver(ToxWindow *window, uint32_t friendnumber, uint32_t filenumber,
|
|
|
|
uint8_t type)
|
2015-03-30 00:33:51 +02:00
|
|
|
{
|
2020-11-11 03:25:27 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *ft = &Friends.list[friendnumber].file_receiver[i];
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
if (ft->state == FILE_TRANSFER_INACTIVE) {
|
2020-11-11 03:25:27 +01:00
|
|
|
clear_file_transfer(ft);
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->window = window;
|
2015-03-30 00:33:51 +02:00
|
|
|
ft->index = i;
|
2020-11-14 01:38:33 +01:00
|
|
|
ft->friendnumber = friendnumber;
|
|
|
|
ft->filenumber = filenumber;
|
2015-08-18 21:12:48 +02:00
|
|
|
ft->file_type = type;
|
|
|
|
ft->state = FILE_TRANSFER_PENDING;
|
2015-03-30 00:33:51 +02:00
|
|
|
return ft;
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-30 00:33:51 +02:00
|
|
|
|
|
|
|
return NULL;
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2015-08-18 21:12:48 +02:00
|
|
|
/* Initializes an unused file transfer and returns its pointer.
|
|
|
|
* Returns NULL on failure.
|
|
|
|
*/
|
2020-11-14 01:38:33 +01:00
|
|
|
struct FileTransfer *new_file_transfer(ToxWindow *window, uint32_t friendnumber, uint32_t filenumber,
|
2015-08-18 21:12:48 +02:00
|
|
|
FILE_TRANSFER_DIRECTION direction, uint8_t type)
|
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (direction == FILE_TRANSFER_RECV) {
|
2020-11-14 01:38:33 +01:00
|
|
|
return new_file_receiver(window, friendnumber, filenumber, type);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-18 21:12:48 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (direction == FILE_TRANSFER_SEND) {
|
2020-11-14 01:38:33 +01:00
|
|
|
return new_file_sender(window, friendnumber, filenumber, type);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-08-18 21:12:48 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-30 00:33:51 +02:00
|
|
|
/* Closes file transfer ft.
|
|
|
|
*
|
2015-03-28 07:56:54 +01:00
|
|
|
* Set CTRL to -1 if we don't want to send a control signal.
|
|
|
|
* Set message or self to NULL if we don't want to display a message.
|
|
|
|
*/
|
2015-03-30 00:33:51 +02:00
|
|
|
void close_file_transfer(ToxWindow *self, Tox *m, struct FileTransfer *ft, int CTRL, const char *message,
|
|
|
|
Notification sound_type)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (!ft) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->state == FILE_TRANSFER_INACTIVE) {
|
2015-03-30 00:33:51 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (ft->file) {
|
2015-03-30 00:33:51 +02:00
|
|
|
fclose(ft->file);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (CTRL >= 0) {
|
2020-11-14 01:38:33 +01:00
|
|
|
tox_file_control(m, ft->friendnumber, ft->filenumber, (Tox_File_Control) CTRL, NULL);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
|
|
|
if (message && self) {
|
2018-07-18 17:33:16 +02:00
|
|
|
if (self->active_box != -1 && sound_type != silent) {
|
2015-03-28 07:56:54 +01:00
|
|
|
box_notify2(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", message);
|
2018-07-18 17:33:16 +02:00
|
|
|
} else {
|
2015-03-28 07:56:54 +01:00
|
|
|
box_notify(self, sound_type, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", message);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2020-11-30 23:55:57 +01:00
|
|
|
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", message);
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
2015-08-10 20:22:13 +02:00
|
|
|
|
2020-11-11 03:25:27 +01:00
|
|
|
clear_file_transfer(ft);
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
/* Kills active outgoing avatar file transfers for friendnumber */
|
|
|
|
void kill_avatar_file_transfers_friend(Tox *m, uint32_t friendnumber)
|
2015-03-28 07:56:54 +01:00
|
|
|
{
|
2020-11-14 01:38:33 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
|
|
|
struct FileTransfer *ft = &Friends.list[friendnumber].file_sender[i];
|
2015-03-28 07:56:54 +01:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
if (ft->file_type == TOX_FILE_KIND_AVATAR) {
|
|
|
|
close_file_transfer(NULL, m, ft, TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
}
|
2015-03-28 07:56:54 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
/* Kills all active file transfers for friendnumber */
|
|
|
|
void kill_all_file_transfers_friend(Tox *m, uint32_t friendnumber)
|
2015-04-04 09:26:38 +02:00
|
|
|
{
|
2020-11-14 01:38:33 +01:00
|
|
|
for (size_t i = 0; i < MAX_FILES; ++i) {
|
|
|
|
close_file_transfer(NULL, m, &Friends.list[friendnumber].file_sender[i], TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
close_file_transfer(NULL, m, &Friends.list[friendnumber].file_receiver[i], TOX_FILE_CONTROL_CANCEL, NULL, silent);
|
|
|
|
}
|
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
|
2020-11-14 01:38:33 +01:00
|
|
|
void kill_all_file_transfers(Tox *m)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < Friends.max_idx; ++i) {
|
2015-04-04 09:26:38 +02:00
|
|
|
kill_all_file_transfers_friend(m, Friends.list[i].num);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2015-04-04 09:26:38 +02:00
|
|
|
}
|