From 64db9f73a245f4e103f08bbce9fae1009ba9e369 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 4 Aug 2014 16:22:29 -0400 Subject: [PATCH] update file transfers for core changes --- src/chat.c | 49 +++++++++++++++++++++++++++------------------ src/chat.h | 4 +++- src/chat_commands.c | 3 +-- src/file_senders.c | 33 +++++++++++++++--------------- src/file_senders.h | 4 ++++ 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/chat.c b/src/chat.c index 44a8eeb..6e1015b 100644 --- a/src/chat.c +++ b/src/chat.c @@ -345,15 +345,20 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t "Incoming file: %s", filename ); } -void chat_close_file_receiver(int num, int filenum) +/* set CTRL to -1 if we don't want to send a control signal. + set msg to NULL if we don't want to display a message */ +void chat_close_file_receiver(Tox *m, int filenum, int friendnum, int CTRL) { - friends[num].file_receiver.active[filenum] = false; - friends[num].file_receiver.pending[filenum] = false; - FILE *file = friends[num].file_receiver.files[filenum]; + if (CTRL > 0) + tox_file_send_control(m, friendnum, 1, filenum, CTRL, 0, 0); + + friends[friendnum].file_receiver.active[filenum] = false; + friends[friendnum].file_receiver.pending[filenum] = false; + FILE *file = friends[friendnum].file_receiver.files[filenum]; if (file != NULL) { fclose(file); - friends[num].file_receiver.files[filenum] = NULL; + friends[friendnum].file_receiver.files[filenum] = NULL; } } @@ -396,31 +401,38 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec case TOX_FILECONTROL_KILL: snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); - if (receive_send == 0) - chat_close_file_receiver(num, filenum); - if (self->active_box != -1) box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, self->active_box, "File transfer for '%s' failed!", filename ); else box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, self->name, "File transfer for '%s' failed!", filename ); + + if (receive_send == 0) + chat_close_file_receiver(m, filenum, num, -1); + else + close_file_sender(self, m, i, NULL, -1, filenum, num); + break; case TOX_FILECONTROL_FINISHED: if (receive_send == 0) { snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename); - chat_close_file_receiver(num, filenum); - - if (self->active_box != -1) - box_notify2(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, - self->active_box, "File '%s' successfuly sent!", filename ); - else - box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, - self->name, "File '%s' successfuly sent!", filename ); - + chat_close_file_receiver(m, filenum, num, TOX_FILECONTROL_FINISHED); + } else { + char msg[MAX_STR_SIZE]; + snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", filename); + close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, num); + return; } + if (self->active_box != -1) + box_notify2(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, + self->active_box, "%s", msg); + else + box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, + self->name, "%s", msg); + break; } @@ -439,8 +451,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu if (fp) { if (fwrite(data, length, 1, fp) != 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Error writing to file."); - tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); - chat_close_file_receiver(num, filenum); + chat_close_file_receiver(m, filenum, num, TOX_FILECONTROL_KILL); } } diff --git a/src/chat.h b/src/chat.h index 74acf20..f7fa779 100644 --- a/src/chat.h +++ b/src/chat.h @@ -26,7 +26,9 @@ #include "windows.h" #include "toxic.h" -void chat_close_file_receiver(int num, int filenum); +/* set CTRL to -1 if we don't want to send a control signal. + set msg to NULL if we don't want to display a message */ +void chat_close_file_receiver(Tox *m, int filenum, int friendnum, int CTRL); void kill_chat_window(ToxWindow *self); ToxWindow new_chat(Tox *m, int32_t friendnum); diff --git a/src/chat_commands.c b/src/chat_commands.c index 046fbfd..056d54e 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -66,8 +66,7 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar char name[MAX_STR_SIZE]; get_file_name(name, sizeof(name), filepath); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' canceled.", name); - tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0); - chat_close_file_receiver(self->num, filenum); + chat_close_file_receiver(m, filenum, self->num, TOX_FILECONTROL_KILL); return; } else if (strcasecmp(inoutstr, "out") == 0) { /* cancel an outgoing file transfer */ int i; diff --git a/src/file_senders.c b/src/file_senders.c index 4d1c281..ec2a303 100644 --- a/src/file_senders.c +++ b/src/file_senders.c @@ -120,13 +120,16 @@ static void set_max_file_senders_index(void) max_file_senders_index = j; } +/* set CTRL to -1 if we don't want to send a control signal. + set msg to NULL if we don't want to display a message */ void close_file_sender(ToxWindow *self, Tox *m, int i, const char *msg, int CTRL, int filenum, int32_t friendnum) { - if (self->chatwin != NULL) + if (msg != NULL) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg); + if (CTRL > 0) + tox_file_send_control(m, friendnum, 0, filenum, CTRL, 0, 0); - tox_file_send_control(m, friendnum, 0, filenum, CTRL, 0, 0); fclose(file_senders[i].file); memset(&file_senders[i], 0, sizeof(FileSender)); set_max_file_senders_index(); @@ -174,18 +177,10 @@ static void send_file_data(ToxWindow *self, Tox *m, int i, int32_t friendnum, in file_senders[i].bps = 0; } - if (file_senders[i].piecelen == 0) { - char msg[MAX_STR_SIZE]; - snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", filename); - close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, friendnum); - - if (self->active_box != -1) - box_notify2(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, - self->active_box, "File '%s' successfuly sent!", filename ); - else - box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, - self->name, "File '%s' successfuly sent!", filename ); - return; + /* file sender is closed in chat_onFileControl callback after receiving reply */ + if (file_senders[i].piecelen == 0 && !file_senders[i].finished) { + tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); + file_senders[i].finished = true; } } } @@ -208,6 +203,12 @@ void do_file_senders(Tox *m) int filenum = file_senders[i].filenum; int32_t friendnum = file_senders[i].friendnum; + /* kill file transfer if chatwindow is closed */ + if (self->chatwin == NULL) { + close_file_sender(self, m, i, NULL, TOX_FILECONTROL_KILL, filenum, friendnum); + continue; + } + /* If file transfer has timed out kill transfer and send kill control */ if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) { char msg[MAX_STR_SIZE]; @@ -217,10 +218,10 @@ void do_file_senders(Tox *m) if (self->active_box != -1) box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, - self->active_box, "File transfer for '%s' failed!", filename ); + self->active_box, "File transfer for '%s' timed out.", filename ); else box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, - self->name, "File transfer for '%s' failed!", filename ); + self->name, "File transfer for '%s' timed out.", filename ); continue; } diff --git a/src/file_senders.h b/src/file_senders.h index 84e093a..3ee5fc4 100644 --- a/src/file_senders.h +++ b/src/file_senders.h @@ -36,6 +36,7 @@ typedef struct { ToxWindow *toxwin; int32_t friendnum; bool active; + bool finished; int filenum; char nextpiece[FILE_PIECE_SIZE]; uint16_t piecelen; @@ -56,7 +57,10 @@ void prep_prog_line(char *progline); if friendnum is -1 we're sending the file, otherwise we're receiving. */ void print_progress_bar(ToxWindow *self, int idx, int friendnum, double pct_remain); +/* set CTRL to -1 if we don't want to send a control signal. + set msg to NULL if we don't want to display a message */ void close_file_sender(ToxWindow *self, Tox *m, int i, const char *msg, int CTRL, int filenum, int32_t friendnum); + void close_all_file_senders(Tox *m); void do_file_senders(Tox *m);