diff --git a/build/Makefile.am b/build/Makefile.am index d38b8f5..2c889ca 100644 --- a/build/Makefile.am +++ b/build/Makefile.am @@ -27,7 +27,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \ $(top_srcdir)/src/toxic_strings.c \ $(top_srcdir)/src/toxic_strings.h \ $(top_srcdir)/src/log.c \ - $(top_srcdir)/src/log.h + $(top_srcdir)/src/log.h \ + $(top_srcdir)/src/file_senders.c \ + $(top_srcdir)/src/file_senders.h toxic_CFLAGS = -I$(top_srcdir) \ $(NCURSES_CFLAGS) \ diff --git a/src/chat.c b/src/chat.c index c0b45a8..c165f84 100644 --- a/src/chat.c +++ b/src/chat.c @@ -237,7 +237,7 @@ 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(int num, int filenum) +static void chat_close_file_receiver(int num, uint8_t filenum) { friends[num].file_receiver.pending[filenum] = false; FILE *file = friends[num].file_receiver.files[filenum]; @@ -271,13 +271,13 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive wprintw(ctx->history, "File transfer for '%s' failed.\n", filename); if (receive_send == 0) - close_file_receiver(num, filenum); + chat_close_file_receiver(num, filenum); else - close_file_sender(filenum); + chat_close_file_receiver(num, filenum); break; case TOX_FILECONTROL_FINISHED: wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); - close_file_receiver(num, filenum); + chat_close_file_receiver(num, filenum); break; } diff --git a/src/file_senders.c b/src/file_senders.c new file mode 100644 index 0000000..d643bf4 --- /dev/null +++ b/src/file_senders.c @@ -0,0 +1,109 @@ +/* file_senders.c + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +#include +#include +#include + +#include "toxic_windows.h" + +FileSender file_senders[MAX_FILES]; +uint8_t max_file_senders_index; + +static void close_file_sender(int i) +{ + fclose(file_senders[i].file); + memset(&file_senders[i], 0, sizeof(FileSender)); + + int j; + + for (j = max_file_senders_index; j > 0; --j) { + if (file_senders[j-1].active) + break; + } + + max_file_senders_index = j; +} + +/* Should only be called on exit */ +void close_all_file_senders(void) +{ + int i; + + for (i = 0; i < max_file_senders_index; ++i) { + if (file_senders[i].active) + fclose(file_senders[i].file); + } +} + +void do_file_senders(Tox *m) +{ + int i; + + for (i = 0; i < max_file_senders_index; ++i) { + if (!file_senders[i].active) + continue; + + uint8_t *pathname = file_senders[i].pathname; + uint8_t filenum = file_senders[i].filenum; + int friendnum = file_senders[i].friendnum; + FILE *fp = file_senders[i].file; + uint64_t current_time = (uint64_t) time(NULL); + + /* 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); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); + } + + tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); + close_file_sender(i); + continue; + } + + while (true) { + if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, + file_senders[i].piecelen) == -1) + break; + + file_senders[i].timestamp = current_time; + 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; + + if (ctx != NULL) { + wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname); + alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); + } + + tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); + close_file_sender(i); + break; + } + } + } +} diff --git a/src/file_senders.h b/src/file_senders.h new file mode 100644 index 0000000..9ec1cdc --- /dev/null +++ b/src/file_senders.h @@ -0,0 +1,26 @@ +/* file_senders.h + * + * + * Copyright (C) 2014 Toxic All Rights Reserved. + * + * This file is part of Toxic. + * + * Toxic is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Toxic is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Toxic. If not, see . + * + */ + +/* Should only be called on exit */ +void close_all_file_senders(void); + +void do_file_senders(Tox *m); diff --git a/src/main.c b/src/main.c index 849408c..4bbd615 100644 --- a/src/main.c +++ b/src/main.c @@ -57,6 +57,7 @@ #include "friendlist.h" #include "prompt.h" #include "misc_tools.h" +#include "file_senders.h" #ifndef PACKAGE_DATADIR #define PACKAGE_DATADIR "." @@ -68,9 +69,6 @@ ToxWindow *prompt = NULL; static int f_loadfromfile; /* 1 if we want to load from/save the data file, 0 otherwise */ -FileSender file_senders[MAX_FILES]; -uint8_t max_file_senders_index; - void on_window_resize(int sig) { endwin(); @@ -383,85 +381,11 @@ static void load_data(Tox *m, char *path) } } -void close_file_sender(int i) -{ - fclose(file_senders[i].file); - memset(&file_senders[i], 0, sizeof(FileSender)); - - int j; - - for (j = max_file_senders_index; j > 0; --j) { - if (file_senders[j-1].active) - break; - } - - max_file_senders_index = j; -} - -static void do_file_senders(Tox *m) -{ - int i; - - for (i = 0; i < max_file_senders_index; ++i) { - if (!file_senders[i].active) - continue; - - uint8_t *pathname = file_senders[i].pathname; - uint8_t filenum = file_senders[i].filenum; - int friendnum = file_senders[i].friendnum; - FILE *fp = file_senders[i].file; - uint64_t current_time = (uint64_t) time(NULL); - - /* 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); - alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); - } - - tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_KILL, 0, 0); - close_file_sender(i); - continue; - } - - while (true) { - if (tox_file_send_data(m, friendnum, filenum, file_senders[i].nextpiece, - file_senders[i].piecelen) == -1) - break; - - file_senders[i].timestamp = current_time; - 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; - - if (ctx != NULL) { - wprintw(ctx->history, "File '%s' successfuly sent.\n", pathname); - alert_window(file_senders[i].toxwin, WINDOW_ALERT_2, true); - } - - tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0); - close_file_sender(i); - break; - } - } - } -} - void exit_toxic(Tox *m) { store_data(m, DATA_FILE); - int i; - - for (i = 0; i < max_file_senders_index; ++i) { - if (file_senders[i].active) - fclose(file_senders[i].file); - } - + close_all_file_senders(); kill_all_windows(); free(DATA_FILE); free(prompt->stb); diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 23b36dc..1e10a7b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -236,6 +236,6 @@ void del_window(ToxWindow *w); void set_active_window(int ch); int num_active_windows(void); -/* closes all chat and groupchat windows (should only be called on shutdown) */ +/* cleans up all chat and groupchat windows (should only be called on shutdown) */ void kill_all_windows(void); #endif