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

small refactor for incoming file transfers

This commit is contained in:
Jfreegman 2014-03-05 03:31:12 -05:00
parent 92d5b2fefc
commit d1153f96ca
4 changed files with 31 additions and 27 deletions

View File

@ -223,7 +223,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
strcat(filename, d);
filename[len + strlen(d)] = '\0';
if (count > 999999) {
if (count > 999) {
wprintw(ctx->history, "Error saving file to disk.\n");
return;
}
@ -237,6 +237,15 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
alert_window(self, WINDOW_ALERT_2, true);
}
static void close_file_receiver(num, filenum)
{
friends[num].file_receiver.pending[filenum] = false;
FILE *file = friends[num].file_receiver.files[filenum];
if (file != NULL)
fclose(file);
}
static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send,
uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length)
{
@ -245,6 +254,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
ChatContext *ctx = self->chatwin;
uint8_t *filename;
bool close_in_file = false;
if (receive_send == 0)
filename = friends[num].file_receiver.filenames[filenum];
@ -255,20 +265,20 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
case TOX_FILECONTROL_ACCEPT:
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
break;
case TOX_FILECONTROL_PAUSE:
// wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
break;
// case TOX_FILECONTROL_PAUSE:
// wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
// break;
case TOX_FILECONTROL_KILL:
wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
if (receive_send == 0)
friends[num].file_receiver.pending[filenum] = false;
close_file_receiver(num, filenum);
else
close_file_sender(filenum);
break;
case TOX_FILECONTROL_FINISHED:
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
close_file_receiver(num, filenum);
break;
}
@ -283,25 +293,12 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
ChatContext *ctx = self->chatwin;
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
FILE *file_to_save = fopen(filename, "a");
if (file_to_save == NULL) {
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, "* Error writing to file.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
return;
}
if (fwrite(data, length, 1, file_to_save) != 1) {
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, "* Error writing to file.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
}
fclose(file_to_save);
}
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)

View File

@ -139,10 +139,18 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
uint8_t *filename = friends[self->num].file_receiver.filenames[filenum];
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) {
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
else
if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) {
wattron(window, COLOR_PAIR(RED));
wprintw(window, "* Error writing to file.\n");
wattroff(window, COLOR_PAIR(RED));
tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
}
} else {
wprintw(window, "File transfer failed.\n");
}
friends[self->num].file_receiver.pending[filenum] = false;
}

View File

@ -56,7 +56,6 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
if (path_len > MAX_STR_SIZE) {
log->log_on = false;
log->file = NULL;
return;
}
@ -100,11 +99,10 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
uint64_t curtime = (uint64_t) time(NULL);
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT))
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) {
fflush(log->file);
log->lastwrite = curtime;
log->lastwrite = curtime;
}
}
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)

View File

@ -206,6 +206,7 @@ typedef struct {
struct FileReceiver {
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
FILE *files[MAX_FILES];
bool pending[MAX_FILES];
};