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

show percentage complete for file transfers

This commit is contained in:
Jfreegman 2014-03-27 23:05:50 -04:00
parent 94e936575e
commit b36a8fd8fa
6 changed files with 63 additions and 10 deletions

View File

@ -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);
friends[num].file_receiver.pending[filenum] = true;
friends[num].file_receiver.size[filenum] = filesize;
strcpy(friends[num].file_receiver.filenames[filenum], filename);
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)
{
friends[num].file_receiver.pending[filenum] = false;
friends[num].file_receiver.size[filenum] = 0;
FILE *file = friends[num].file_receiver.files[filenum];
if (file != NULL)
@ -281,7 +283,8 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
switch (control_type) {
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;
/*case TOX_FILECONTROL_PAUSE:
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);
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)

View File

@ -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) {
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);
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) {
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].friendnum = self->num;
file_senders[i].timestamp = get_unix_time();
file_senders[i].size = filesize;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_file_data_size(m, self->num), file_to_send);

View File

@ -29,6 +29,7 @@
#include <time.h>
#include "toxic_windows.h"
#include "line_info.h"
FileSender file_senders[MAX_FILES];
uint8_t max_file_senders_index;
@ -61,12 +62,14 @@ void close_all_file_senders(void)
void do_file_senders(Tox *m)
{
uint8_t msg[MAX_STR_SIZE];
int i;
for (i = 0; i < max_file_senders_index; ++i) {
if (!file_senders[i].active)
continue;
ToxWindow *self = file_senders[i].toxwin;
uint8_t *pathname = file_senders[i].pathname;
int filenum = file_senders[i].filenum;
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 (timed_out(file_senders[i].timestamp, current_time, TIMEOUT_FILESENDER)) {
ChatContext *ctx = file_senders[i].toxwin->chatwin;
if (ctx != NULL) {
wprintw(ctx->history, "File transfer for '%s' timed out.\n", pathname);
if (self->chatwin->history != NULL) {
snprintf(msg, sizeof(msg), "File transfer for '%s' timed out.", pathname);
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
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,
tox_file_data_size(m, friendnum), fp);
if (file_senders[i].piecelen == 0) {
ChatContext *ctx = file_senders[i].toxwin->chatwin;
/* refresh line with percentage complete */
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) {
wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname);
if (remain)
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);
}

View File

@ -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)
{
hst->line_start = hst->line_root;

View File

@ -77,6 +77,9 @@ void line_info_toggle_scroll(ToxWindow *self, bool scroll);
/* clears the screen (does not delete anything) */
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_onKey(ToxWindow *self, wint_t key);
void line_info_onDraw(ToxWindow *self);

View File

@ -207,12 +207,16 @@ typedef struct {
uint16_t piecelen;
uint8_t pathname[MAX_STR_SIZE];
uint64_t timestamp;
uint64_t size;
uint32_t line_id;
} FileSender;
struct FileReceiver {
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
FILE *files[MAX_FILES];
bool pending[MAX_FILES];
uint64_t size[MAX_FILES];
uint32_t line_id;
};
/* End file transfer code */