2014-03-05 11:06:21 +01:00
|
|
|
/* file_senders.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 <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2014-06-27 00:20:56 +02:00
|
|
|
#include <unistd.h>
|
2014-03-05 11:06:21 +01:00
|
|
|
|
2014-06-12 00:06:55 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2014-07-27 23:14:28 +02:00
|
|
|
#include "friendlist.h"
|
2014-06-12 00:06:55 +02:00
|
|
|
#include "file_senders.h"
|
2014-03-28 04:05:50 +01:00
|
|
|
#include "line_info.h"
|
2014-06-22 03:41:38 +02:00
|
|
|
#include "misc_tools.h"
|
2014-07-21 01:12:13 +02:00
|
|
|
#include "notify.h"
|
2014-03-05 11:06:21 +01:00
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
FileSender file_senders[MAX_FILES];
|
|
|
|
uint8_t max_file_senders_index;
|
2014-07-28 03:19:17 +02:00
|
|
|
uint8_t num_active_file_senders;
|
2014-08-07 19:05:42 +02:00
|
|
|
extern _Friends Friends;
|
2014-07-27 23:14:28 +02:00
|
|
|
|
2014-07-28 03:19:17 +02:00
|
|
|
/* creates initial progress line that will be updated during file transfer.
|
|
|
|
Assumes progline is of size MAX_STR_SIZE */
|
2014-07-27 23:14:28 +02:00
|
|
|
void prep_prog_line(char *progline)
|
|
|
|
{
|
|
|
|
strcpy(progline, "0.0 B/s [");
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_PROG_MARKS; ++i)
|
2014-07-29 01:36:30 +02:00
|
|
|
strcat(progline, "-");
|
2014-07-27 23:14:28 +02:00
|
|
|
|
2014-08-04 20:35:34 +02:00
|
|
|
strcat(progline, "] 0%");
|
2014-07-27 23:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* prints a progress bar for file transfers.
|
|
|
|
if friendnum is -1 we're sending the file, otherwise we're receiving. */
|
2014-08-04 05:11:19 +02:00
|
|
|
void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_done)
|
2014-07-27 23:14:28 +02:00
|
|
|
{
|
|
|
|
double bps;
|
|
|
|
uint32_t line_id;
|
|
|
|
|
|
|
|
if (friendnum < 0) {
|
|
|
|
bps = file_senders[idx].bps;
|
|
|
|
line_id = file_senders[idx].line_id;
|
|
|
|
} else {
|
2014-08-07 19:05:42 +02:00
|
|
|
bps = Friends.list[friendnum].file_receiver[idx].bps;
|
|
|
|
line_id = Friends.list[friendnum].file_receiver[idx].line_id;
|
2014-07-27 23:14:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char msg[MAX_STR_SIZE];
|
2014-08-08 01:24:56 +02:00
|
|
|
bytes_convert_str(msg, sizeof(msg), bps);
|
|
|
|
strcat(msg, "/s [");
|
|
|
|
|
2014-08-04 05:11:19 +02:00
|
|
|
int n = pct_done / (100 / NUM_PROG_MARKS);
|
2014-08-07 19:05:42 +02:00
|
|
|
int i, j;
|
2014-07-27 23:14:28 +02:00
|
|
|
|
|
|
|
for (i = 0; i < n; ++i)
|
|
|
|
strcat(msg, "#");
|
|
|
|
|
|
|
|
for (j = i; j < NUM_PROG_MARKS; ++j)
|
2014-07-29 01:36:30 +02:00
|
|
|
strcat(msg, "-");
|
2014-07-27 23:14:28 +02:00
|
|
|
|
|
|
|
strcat(msg, "] ");
|
|
|
|
|
|
|
|
char pctstr[16];
|
2014-08-04 05:11:19 +02:00
|
|
|
const char *frmt = pct_done == 100 ? "%.f%%" : "%.1f%%";
|
|
|
|
snprintf(pctstr, sizeof(pctstr), frmt, pct_done);
|
2014-07-27 23:14:28 +02:00
|
|
|
strcat(msg, pctstr);
|
|
|
|
|
|
|
|
line_info_set(self, line_id, msg);
|
|
|
|
}
|
2014-06-27 00:20:56 +02:00
|
|
|
|
2014-06-28 18:55:05 +02:00
|
|
|
static void set_max_file_senders_index(void)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = max_file_senders_index; j > 0; --j) {
|
|
|
|
if (file_senders[j - 1].active)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
max_file_senders_index = j;
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:38:33 +02:00
|
|
|
/* called whenever a file sender is opened or closed */
|
|
|
|
void reset_file_sender_queue(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < max_file_senders_index; ++i) {
|
|
|
|
if (file_senders[i].active)
|
|
|
|
file_senders[i].queue_pos = pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 22:22:29 +02:00
|
|
|
/* set CTRL to -1 if we don't want to send a control signal.
|
|
|
|
set msg to NULL if we don't want to display a message */
|
2014-08-04 07:56:43 +02:00
|
|
|
void close_file_sender(ToxWindow *self, Tox *m, int i, const char *msg, int CTRL, int filenum, int32_t friendnum)
|
2014-03-05 11:06:21 +01:00
|
|
|
{
|
2014-08-04 22:22:29 +02:00
|
|
|
if (msg != NULL)
|
2014-08-02 21:35:57 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg);
|
2014-07-21 01:12:13 +02:00
|
|
|
|
2014-08-04 22:22:29 +02:00
|
|
|
if (CTRL > 0)
|
|
|
|
tox_file_send_control(m, friendnum, 0, filenum, CTRL, 0, 0);
|
2014-06-28 18:14:43 +02:00
|
|
|
|
2014-03-05 11:06:21 +01:00
|
|
|
fclose(file_senders[i].file);
|
2014-06-28 18:14:43 +02:00
|
|
|
memset(&file_senders[i], 0, sizeof(FileSender));
|
2014-06-28 18:55:05 +02:00
|
|
|
set_max_file_senders_index();
|
2014-08-05 23:38:33 +02:00
|
|
|
reset_file_sender_queue();
|
2014-07-28 03:19:17 +02:00
|
|
|
--num_active_file_senders;
|
2014-03-05 11:06:21 +01:00
|
|
|
}
|
|
|
|
|
2014-06-28 18:55:05 +02:00
|
|
|
void close_all_file_senders(Tox *m)
|
2014-03-05 11:06:21 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < max_file_senders_index; ++i) {
|
2014-06-28 18:55:05 +02:00
|
|
|
if (file_senders[i].active) {
|
2014-03-05 11:06:21 +01:00
|
|
|
fclose(file_senders[i].file);
|
2014-06-28 18:55:05 +02:00
|
|
|
tox_file_send_control(m, file_senders[i].friendnum, 0, file_senders[i].filenum,
|
|
|
|
TOX_FILECONTROL_KILL, 0, 0);
|
|
|
|
memset(&file_senders[i], 0, sizeof(FileSender));
|
|
|
|
}
|
|
|
|
|
|
|
|
set_max_file_senders_index();
|
2014-03-05 11:06:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-03 21:12:55 +02:00
|
|
|
static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, int filenum, const char *filename)
|
2014-07-28 03:19:17 +02:00
|
|
|
{
|
|
|
|
FILE *fp = file_senders[i].file;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (tox_file_send_data(m, friendnum, filenum, (uint8_t *) file_senders[i].nextpiece,
|
|
|
|
file_senders[i].piecelen) == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint64_t curtime = get_unix_time();
|
|
|
|
file_senders[i].timestamp = curtime;
|
|
|
|
file_senders[i].bps += file_senders[i].piecelen;
|
|
|
|
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
|
|
|
tox_file_data_size(m, friendnum), fp);
|
|
|
|
|
|
|
|
double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0);
|
|
|
|
|
|
|
|
/* refresh line with percentage complete and transfer speed (must be called once per second) */
|
2014-08-05 23:38:33 +02:00
|
|
|
if (timed_out(file_senders[i].last_progress, curtime, 1) || (!remain && !file_senders[i].finished)) {
|
2014-07-28 03:19:17 +02:00
|
|
|
file_senders[i].last_progress = curtime;
|
2014-08-04 05:11:19 +02:00
|
|
|
double pct_done = remain > 0 ? (1 - (remain / file_senders[i].size)) * 100 : 100;
|
|
|
|
print_progress_bar(self, i, -1, pct_done);
|
2014-07-28 03:19:17 +02:00
|
|
|
file_senders[i].bps = 0;
|
|
|
|
}
|
|
|
|
|
2014-08-04 22:22:29 +02:00
|
|
|
/* file sender is closed in chat_onFileControl callback after receiving reply */
|
|
|
|
if (file_senders[i].piecelen == 0 && !file_senders[i].finished) {
|
|
|
|
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
|
|
|
|
file_senders[i].finished = true;
|
2014-07-28 03:19:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
void do_file_senders(Tox *m)
|
2014-03-05 11:06:21 +01:00
|
|
|
{
|
2014-06-28 18:14:43 +02:00
|
|
|
int i;
|
2014-03-05 11:06:21 +01:00
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
for (i = 0; i < max_file_senders_index; ++i) {
|
|
|
|
if (!file_senders[i].active)
|
|
|
|
continue;
|
2014-03-05 11:06:21 +01:00
|
|
|
|
2014-07-28 03:19:17 +02:00
|
|
|
if (file_senders[i].queue_pos > 0) {
|
|
|
|
--file_senders[i].queue_pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
ToxWindow *self = file_senders[i].toxwin;
|
2014-08-03 21:12:55 +02:00
|
|
|
char *filename = file_senders[i].filename;
|
2014-06-28 18:14:43 +02:00
|
|
|
int filenum = file_senders[i].filenum;
|
|
|
|
int32_t friendnum = file_senders[i].friendnum;
|
2014-03-05 11:06:21 +01:00
|
|
|
|
2014-08-04 22:22:29 +02:00
|
|
|
/* kill file transfer if chatwindow is closed */
|
|
|
|
if (self->chatwin == NULL) {
|
|
|
|
close_file_sender(self, m, i, NULL, TOX_FILECONTROL_KILL, filenum, friendnum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-05 11:06:21 +01:00
|
|
|
/* If file transfer has timed out kill transfer and send kill control */
|
2014-06-28 18:14:43 +02:00
|
|
|
if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) {
|
2014-07-29 01:36:30 +02:00
|
|
|
char msg[MAX_STR_SIZE];
|
2014-08-03 21:12:55 +02:00
|
|
|
snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", filename);
|
2014-06-28 18:14:43 +02:00
|
|
|
close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, friendnum);
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, NULL);
|
|
|
|
|
|
|
|
if (self->active_box != -1)
|
2014-08-05 23:38:33 +02:00
|
|
|
box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "%s", msg);
|
2014-08-02 00:37:02 +02:00
|
|
|
else
|
2014-08-05 23:38:33 +02:00
|
|
|
box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "%s", msg);
|
2014-06-28 18:14:43 +02:00
|
|
|
continue;
|
2014-03-05 11:06:21 +01:00
|
|
|
}
|
|
|
|
|
2014-08-03 21:12:55 +02:00
|
|
|
send_file_data(self, m, i, friendnum, filenum, filename);
|
2014-08-05 23:38:33 +02:00
|
|
|
file_senders[i].queue_pos = num_active_file_senders - 1;
|
2014-06-27 00:20:56 +02:00
|
|
|
}
|
2014-06-27 00:22:10 +02:00
|
|
|
}
|