From d1153f96caf42572e357959eb9e9742e7aff0548 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Wed, 5 Mar 2014 03:31:12 -0500 Subject: [PATCH] small refactor for incoming file transfers --- src/chat.c | 37 +++++++++++++++++-------------------- src/chat_commands.c | 12 ++++++++++-- src/log.c | 8 +++----- src/toxic_windows.h | 1 + 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/chat.c b/src/chat.c index f459171..c23d0ff 100644 --- a/src/chat.c +++ b/src/chat.c @@ -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) diff --git a/src/chat_commands.c b/src/chat_commands.c index 18d8646..f69e081 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -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; } diff --git a/src/log.c b/src/log.c index 40a36c0..0b41b43 100644 --- a/src/log.c +++ b/src/log.c @@ -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) diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 9f87880..23b36dc 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -206,6 +206,7 @@ typedef struct { struct FileReceiver { uint8_t filenames[MAX_FILES][MAX_STR_SIZE]; + FILE *files[MAX_FILES]; bool pending[MAX_FILES]; };