1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 22:53:28 +01:00

separate file transfer updates from callbacks

This commit is contained in:
Jfreegman 2014-08-16 23:23:03 -04:00
parent b870679f2c
commit 58b0a04019
5 changed files with 67 additions and 25 deletions

View File

@ -244,7 +244,7 @@ int dir_match(ToxWindow *self, Tox *m, const wchar_t *line)
if (wcs_to_mbs_buf(b_path, tmpline, sizeof(b_path)) == -1) if (wcs_to_mbs_buf(b_path, tmpline, sizeof(b_path)) == -1)
return -1; return -1;
if (!strncmp(b_path, "~/", 2)) if (b_path[0] == '~')
complt_home_dir(self, b_path, sizeof(b_path)); complt_home_dir(self, b_path, sizeof(b_path));
int si = char_rfind(b_path, '/', strlen(b_path)); int si = char_rfind(b_path, '/', strlen(b_path));

View File

@ -345,6 +345,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
Friends.list[num].file_receiver[filenum].pending = true; Friends.list[num].file_receiver[filenum].pending = true;
Friends.list[num].file_receiver[filenum].size = filesize; Friends.list[num].file_receiver[filenum].size = filesize;
Friends.list[num].file_receiver[filenum].filenum = filenum;
strcpy(Friends.list[num].file_receiver[filenum].filename, filename); strcpy(Friends.list[num].file_receiver[filenum].filename, filename);
if (self->active_box != -1) if (self->active_box != -1)
@ -375,7 +376,7 @@ static void chat_resume_file_transfers(Tox *m, int fnum)
int i; int i;
for (i = 0; i < MAX_FILES; ++i) { for (i = 0; i < MAX_FILES; ++i) {
if (Friends.list[fnum].file_receiver[i].active && !Friends.list[fnum].file_receiver[i].paused) { if (Friends.list[fnum].file_receiver[i].active) {
uint8_t bytes_recv[sizeof(uint64_t)]; uint8_t bytes_recv[sizeof(uint64_t)];
memcpy(bytes_recv, &Friends.list[fnum].file_receiver[i].bytes_recv, sizeof(uint64_t)); memcpy(bytes_recv, &Friends.list[fnum].file_receiver[i].bytes_recv, sizeof(uint64_t));
net_to_host(bytes_recv, sizeof(uint64_t)); net_to_host(bytes_recv, sizeof(uint64_t));
@ -472,6 +473,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
case TOX_FILECONTROL_FINISHED: case TOX_FILECONTROL_FINISHED:
if (receive_send == 0) { if (receive_send == 0) {
print_progress_bar(self, filenum, num, 100.0);
snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename); snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename);
chat_close_file_receiver(m, filenum, num, TOX_FILECONTROL_FINISHED); chat_close_file_receiver(m, filenum, num, TOX_FILECONTROL_FINISHED);
} else { } else {
@ -532,17 +534,6 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
Friends.list[num].file_receiver[filenum].bps += length; Friends.list[num].file_receiver[filenum].bps += length;
Friends.list[num].file_receiver[filenum].bytes_recv += length; Friends.list[num].file_receiver[filenum].bytes_recv += length;
double remain = (double) tox_file_data_remaining(m, num, filenum, 1);
uint64_t curtime = get_unix_time();
/* refresh line with percentage complete and transfer speed (must be called once per second) */
if (!remain || timed_out(Friends.list[num].file_receiver[filenum].last_progress, curtime, 1)) {
Friends.list[num].file_receiver[filenum].last_progress = curtime;
uint64_t size = Friends.list[num].file_receiver[filenum].size;
double pct_done = remain > 0 ? (1 - (remain / size)) * 100 : 100;
print_progress_bar(self, filenum, num, pct_done);
Friends.list[num].file_receiver[filenum].bps = 0;
}
} }
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, const char *group_pub_key) static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, const char *group_pub_key)

View File

@ -89,6 +89,64 @@ void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_done
line_info_set(self, line_id, msg); line_info_set(self, line_id, msg);
} }
/* refreshes active file receiver status bars */
static void refresh_recv_prog(Tox *m)
{
int i;
for (i = 2; i < MAX_WINDOWS_NUM; ++i) {
ToxWindow *toxwin = get_window_ptr(i);
if (toxwin == NULL || !toxwin->is_chat)
continue;
int fnum = toxwin->num;
int j;
for (j = 0; j < MAX_FILES; ++j) {
if (!Friends.list[fnum].file_receiver[j].active)
continue;
int filenum = Friends.list[fnum].file_receiver[j].filenum;
double remain = (double) tox_file_data_remaining(m, fnum, filenum, 1);
uint64_t curtime = get_unix_time();
/* must be called once per second */
if (!remain || timed_out(Friends.list[fnum].file_receiver[filenum].last_progress, curtime, 1)) {
Friends.list[fnum].file_receiver[filenum].last_progress = curtime;
uint64_t size = Friends.list[fnum].file_receiver[filenum].size;
double pct_done = remain > 0 ? (1 - (remain / size)) * 100 : 100;
print_progress_bar(toxwin, filenum, fnum, pct_done);
Friends.list[fnum].file_receiver[filenum].bps = 0;
}
}
}
}
/* refreshes active file sender status bars */
static void refresh_sender_prog(Tox *m)
{
int i;
for (i = 0; i < max_file_senders_index; ++i) {
if (!file_senders[i].active)
continue;
int filenum = file_senders[i].filenum;
int32_t friendnum = file_senders[i].friendnum;
double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0);
/* must be called once per second */
if (timed_out(file_senders[i].last_progress, file_senders[i].timestamp, 1)
|| (!remain && !file_senders[i].finished)) {
file_senders[i].last_progress = get_unix_time();
double pct_done = remain > 0 ? (1 - (remain / file_senders[i].size)) * 100 : 100;
print_progress_bar(file_senders[i].toxwin, i, -1, pct_done);
file_senders[i].bps = 0;
}
}
}
static void set_max_file_senders_index(void) static void set_max_file_senders_index(void)
{ {
int j; int j;
@ -161,18 +219,9 @@ static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, in
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_file_data_size(m, friendnum), fp); 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) */
if (timed_out(file_senders[i].last_progress, curtime, 1) || (!remain && !file_senders[i].finished)) {
file_senders[i].last_progress = curtime;
double pct_done = remain > 0 ? (1 - (remain / file_senders[i].size)) * 100 : 100;
print_progress_bar(self, i, -1, pct_done);
file_senders[i].bps = 0;
}
/* file sender is closed in chat_onFileControl callback after receiving reply */ /* file sender is closed in chat_onFileControl callback after receiving reply */
if (file_senders[i].piecelen == 0 && !file_senders[i].finished) { if (file_senders[i].piecelen == 0 && !file_senders[i].finished) {
print_progress_bar(self, i, -1, 100.0);
tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
file_senders[i].finished = true; file_senders[i].finished = true;
} }
@ -222,4 +271,7 @@ void do_file_senders(Tox *m)
file_senders[i].queue_pos = num_active_file_senders - 1; file_senders[i].queue_pos = num_active_file_senders - 1;
} }
refresh_sender_prog(m);
refresh_recv_prog(m);
} }

View File

@ -35,7 +35,6 @@ struct FileReceiver {
FILE *file; FILE *file;
bool pending; bool pending;
bool active; bool active;
bool paused;
uint64_t size; uint64_t size;
uint64_t bytes_recv; uint64_t bytes_recv;
double bps; double bps;

View File

@ -481,7 +481,7 @@ ToxWindow *get_window_ptr(int i)
{ {
ToxWindow *toxwin = NULL; ToxWindow *toxwin = NULL;
if (windows[i].active) if (i >= 0 && i <= MAX_WINDOWS_NUM && windows[i].active)
toxwin = &windows[i]; toxwin = &windows[i];
return toxwin; return toxwin;