1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-09-29 04:15:34 +02:00

move file sender stuff to its own files

This commit is contained in:
Jfreegman 2014-03-05 05:06:21 -05:00
parent 675c8fa89f
commit 2fcfa954ab
6 changed files with 145 additions and 84 deletions

View File

@ -27,7 +27,9 @@ toxic_SOURCES = $(top_srcdir)/src/main.c \
$(top_srcdir)/src/toxic_strings.c \ $(top_srcdir)/src/toxic_strings.c \
$(top_srcdir)/src/toxic_strings.h \ $(top_srcdir)/src/toxic_strings.h \
$(top_srcdir)/src/log.c \ $(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) \ toxic_CFLAGS = -I$(top_srcdir) \
$(NCURSES_CFLAGS) \ $(NCURSES_CFLAGS) \

View File

@ -237,7 +237,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
alert_window(self, WINDOW_ALERT_2, true); 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; friends[num].file_receiver.pending[filenum] = false;
FILE *file = friends[num].file_receiver.files[filenum]; 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); wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
if (receive_send == 0) if (receive_send == 0)
close_file_receiver(num, filenum); chat_close_file_receiver(num, filenum);
else else
close_file_sender(filenum); chat_close_file_receiver(num, filenum);
break; break;
case TOX_FILECONTROL_FINISHED: case TOX_FILECONTROL_FINISHED:
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename); wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
close_file_receiver(num, filenum); chat_close_file_receiver(num, filenum);
break; break;
} }

109
src/file_senders.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <stdlib.h>
#include <time.h>
#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;
}
}
}
}

26
src/file_senders.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
/* Should only be called on exit */
void close_all_file_senders(void);
void do_file_senders(Tox *m);

View File

@ -57,6 +57,7 @@
#include "friendlist.h" #include "friendlist.h"
#include "prompt.h" #include "prompt.h"
#include "misc_tools.h" #include "misc_tools.h"
#include "file_senders.h"
#ifndef PACKAGE_DATADIR #ifndef PACKAGE_DATADIR
#define 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 */ 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) void on_window_resize(int sig)
{ {
endwin(); 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) void exit_toxic(Tox *m)
{ {
store_data(m, DATA_FILE); store_data(m, DATA_FILE);
int i; close_all_file_senders();
for (i = 0; i < max_file_senders_index; ++i) {
if (file_senders[i].active)
fclose(file_senders[i].file);
}
kill_all_windows(); kill_all_windows();
free(DATA_FILE); free(DATA_FILE);
free(prompt->stb); free(prompt->stb);

View File

@ -236,6 +236,6 @@ void del_window(ToxWindow *w);
void set_active_window(int ch); void set_active_window(int ch);
int num_active_windows(void); 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); void kill_all_windows(void);
#endif #endif