mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-16 04:13:02 +01:00
show percentage complete for file transfers
This commit is contained in:
parent
94e936575e
commit
b36a8fd8fa
19
src/chat.c
19
src/chat.c
@ -251,6 +251,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
|
|||||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||||
|
|
||||||
friends[num].file_receiver.pending[filenum] = true;
|
friends[num].file_receiver.pending[filenum] = true;
|
||||||
|
friends[num].file_receiver.size[filenum] = filesize;
|
||||||
strcpy(friends[num].file_receiver.filenames[filenum], filename);
|
strcpy(friends[num].file_receiver.filenames[filenum], filename);
|
||||||
|
|
||||||
alert_window(self, WINDOW_ALERT_2, true);
|
alert_window(self, WINDOW_ALERT_2, true);
|
||||||
@ -259,6 +260,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
|
|||||||
static void chat_close_file_receiver(int num, uint8_t filenum)
|
static void chat_close_file_receiver(int num, uint8_t filenum)
|
||||||
{
|
{
|
||||||
friends[num].file_receiver.pending[filenum] = false;
|
friends[num].file_receiver.pending[filenum] = false;
|
||||||
|
friends[num].file_receiver.size[filenum] = 0;
|
||||||
FILE *file = friends[num].file_receiver.files[filenum];
|
FILE *file = friends[num].file_receiver.files[filenum];
|
||||||
|
|
||||||
if (file != NULL)
|
if (file != NULL)
|
||||||
@ -281,7 +283,8 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
|
|||||||
|
|
||||||
switch (control_type) {
|
switch (control_type) {
|
||||||
case TOX_FILECONTROL_ACCEPT:
|
case TOX_FILECONTROL_ACCEPT:
|
||||||
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted.", filename);
|
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1f%%)", filename, 0.0);
|
||||||
|
file_senders[filenum].line_id = self->chatwin->hst->line_end->id + 1;
|
||||||
break;
|
break;
|
||||||
/*case TOX_FILECONTROL_PAUSE:
|
/*case TOX_FILECONTROL_PAUSE:
|
||||||
wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
||||||
@ -314,6 +317,20 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
|
|||||||
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||||
chat_close_file_receiver(num, filenum);
|
chat_close_file_receiver(num, filenum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* refresh line with percentage complete */
|
||||||
|
uint8_t msg[MAX_STR_SIZE];
|
||||||
|
uint64_t size = friends[num].file_receiver.size[filenum];
|
||||||
|
long double remain = (double) tox_file_data_remaining(m, num, filenum, 1);
|
||||||
|
long double pct_remain = 100;
|
||||||
|
|
||||||
|
if (remain)
|
||||||
|
pct_remain = (1 - (remain / size)) * 100;
|
||||||
|
|
||||||
|
const uint8_t *name = friends[num].file_receiver.filenames[filenum];
|
||||||
|
snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1Lf%%)", name, pct_remain);
|
||||||
|
line_info_set(self, friends[num].file_receiver.line_id, msg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, uint8_t *group_pub_key)
|
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int32_t friendnumber, uint8_t *group_pub_key)
|
||||||
|
@ -183,8 +183,9 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
|||||||
|
|
||||||
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
|
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
|
||||||
uint8_t msg[MAX_STR_SIZE];
|
uint8_t msg[MAX_STR_SIZE];
|
||||||
snprintf(msg, sizeof(msg), "Accepted file transfer %u. Saving file as: '%s'", filenum, filename);
|
snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1f%%)", filename, 0.0);
|
||||||
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||||
|
friends[self->num].file_receiver.line_id = self->chatwin->hst->line_end->id;
|
||||||
|
|
||||||
if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) {
|
if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) {
|
||||||
errmsg = "* Error writing to file.";
|
errmsg = "* Error writing to file.";
|
||||||
@ -265,6 +266,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
|||||||
file_senders[i].filenum = filenum;
|
file_senders[i].filenum = filenum;
|
||||||
file_senders[i].friendnum = self->num;
|
file_senders[i].friendnum = self->num;
|
||||||
file_senders[i].timestamp = get_unix_time();
|
file_senders[i].timestamp = get_unix_time();
|
||||||
|
file_senders[i].size = filesize;
|
||||||
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, self->num), file_to_send);
|
tox_file_data_size(m, self->num), file_to_send);
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "toxic_windows.h"
|
#include "toxic_windows.h"
|
||||||
|
#include "line_info.h"
|
||||||
|
|
||||||
FileSender file_senders[MAX_FILES];
|
FileSender file_senders[MAX_FILES];
|
||||||
uint8_t max_file_senders_index;
|
uint8_t max_file_senders_index;
|
||||||
@ -61,12 +62,14 @@ void close_all_file_senders(void)
|
|||||||
|
|
||||||
void do_file_senders(Tox *m)
|
void do_file_senders(Tox *m)
|
||||||
{
|
{
|
||||||
|
uint8_t msg[MAX_STR_SIZE];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < max_file_senders_index; ++i) {
|
for (i = 0; i < max_file_senders_index; ++i) {
|
||||||
if (!file_senders[i].active)
|
if (!file_senders[i].active)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
ToxWindow *self = file_senders[i].toxwin;
|
||||||
uint8_t *pathname = file_senders[i].pathname;
|
uint8_t *pathname = file_senders[i].pathname;
|
||||||
int filenum = file_senders[i].filenum;
|
int filenum = file_senders[i].filenum;
|
||||||
int32_t friendnum = file_senders[i].friendnum;
|
int32_t friendnum = file_senders[i].friendnum;
|
||||||
@ -75,10 +78,9 @@ void do_file_senders(Tox *m)
|
|||||||
|
|
||||||
/* If file transfer has timed out kill transfer and send kill control */
|
/* If file transfer has timed out kill transfer and send kill control */
|
||||||
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
|
if (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
|
||||||
ChatContext *ctx = file_senders[i].toxwin->chatwin;
|
if (self->chatwin->history != NULL) {
|
||||||
|
snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", pathname);
|
||||||
if (ctx != NULL) {
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||||
wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname);
|
|
||||||
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,11 +98,22 @@ void do_file_senders(Tox *m)
|
|||||||
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);
|
||||||
|
|
||||||
if (file_senders[i].piecelen == 0) {
|
/* refresh line with percentage complete */
|
||||||
ChatContext *ctx = file_senders[i].toxwin->chatwin;
|
uint64_t size = file_senders[i].size;
|
||||||
|
long double remain = (double) tox_file_data_remaining(m, friendnum, filenum, 0);
|
||||||
|
long double pct_remain = 100;
|
||||||
|
|
||||||
if (ctx != NULL) {
|
if (remain)
|
||||||
wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname);
|
pct_remain = (1 - (remain / size)) * 100;
|
||||||
|
|
||||||
|
const uint8_t *name = file_senders[filenum].pathname;
|
||||||
|
snprintf(msg, sizeof(msg), "File transfer for '%s' accepted (%.1Lf%%)", name, pct_remain);
|
||||||
|
line_info_set(self, file_senders[filenum].line_id, msg);
|
||||||
|
|
||||||
|
if (file_senders[i].piecelen == 0) {
|
||||||
|
if (self->chatwin->history != NULL) {
|
||||||
|
snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", pathname);
|
||||||
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
||||||
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,6 +325,20 @@ void line_info_print(ToxWindow *self)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void line_info_set(ToxWindow *self, uint32_t id, uint8_t *msg)
|
||||||
|
{
|
||||||
|
struct line_info *line = self->chatwin->hst->line_end;
|
||||||
|
|
||||||
|
while (line) {
|
||||||
|
if (line->id == id) {
|
||||||
|
snprintf(line->msg, sizeof(line->msg), "%s", msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
line = line->prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void line_info_goto_root(struct history *hst)
|
static void line_info_goto_root(struct history *hst)
|
||||||
{
|
{
|
||||||
hst->line_start = hst->line_root;
|
hst->line_start = hst->line_root;
|
||||||
|
@ -77,6 +77,9 @@ void line_info_toggle_scroll(ToxWindow *self, bool scroll);
|
|||||||
/* clears the screen (does not delete anything) */
|
/* clears the screen (does not delete anything) */
|
||||||
void line_info_clear(struct history *hst);
|
void line_info_clear(struct history *hst);
|
||||||
|
|
||||||
|
/* puts msg in specified line_info->msg buffer */
|
||||||
|
void line_info_set(ToxWindow *self, uint32_t id, uint8_t *msg);
|
||||||
|
|
||||||
void line_info_init(struct history *hst);
|
void line_info_init(struct history *hst);
|
||||||
void line_info_onKey(ToxWindow *self, wint_t key);
|
void line_info_onKey(ToxWindow *self, wint_t key);
|
||||||
void line_info_onDraw(ToxWindow *self);
|
void line_info_onDraw(ToxWindow *self);
|
@ -207,12 +207,16 @@ typedef struct {
|
|||||||
uint16_t piecelen;
|
uint16_t piecelen;
|
||||||
uint8_t pathname[MAX_STR_SIZE];
|
uint8_t pathname[MAX_STR_SIZE];
|
||||||
uint64_t timestamp;
|
uint64_t timestamp;
|
||||||
|
uint64_t size;
|
||||||
|
uint32_t line_id;
|
||||||
} FileSender;
|
} FileSender;
|
||||||
|
|
||||||
struct FileReceiver {
|
struct FileReceiver {
|
||||||
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
|
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
|
||||||
FILE *files[MAX_FILES];
|
FILE *files[MAX_FILES];
|
||||||
bool pending[MAX_FILES];
|
bool pending[MAX_FILES];
|
||||||
|
uint64_t size[MAX_FILES];
|
||||||
|
uint32_t line_id;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* End file transfer code */
|
/* End file transfer code */
|
||||||
|
Loading…
Reference in New Issue
Block a user