1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-13 02:43:02 +01:00

add ability to cancel file transfers

This commit is contained in:
Jfreegman 2014-08-04 01:56:43 -04:00
parent 38ed0c86ad
commit 6fdafceda8
11 changed files with 86 additions and 20 deletions

View File

@ -63,15 +63,16 @@ static void kill_infobox(ToxWindow *self);
#endif /* _AUDIO */
#ifdef _AUDIO
#define AC_NUM_CHAT_COMMANDS 25
#define AC_NUM_CHAT_COMMANDS 26
#else
#define AC_NUM_CHAT_COMMANDS 18
#define AC_NUM_CHAT_COMMANDS 19
#endif /* _AUDIO */
/* Array of chat command names used for tab completion. */
static const char chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/accept" },
{ "/add" },
{ "/cancel" },
{ "/clear" },
{ "/close" },
{ "/connect" },
@ -341,11 +342,13 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
"Incoming file: %s", filename );
else
box_notify(self, transfer_pending, NT_WNDALERT_2 | NT_NOFOCUS, &self->active_box, self->name,
"Incoming file: %s", filename );
"Incoming file: %s", filename );
}
static void chat_close_file_receiver(int32_t num, uint8_t filenum)
void chat_close_file_receiver(int num, int filenum)
{
friends[num].file_receiver.active[filenum] = false;
friends[num].file_receiver.pending[filenum] = false;
FILE *file = friends[num].file_receiver.files[filenum];
if (file != NULL) {
@ -378,8 +381,8 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t rec
switch (control_type) {
case TOX_FILECONTROL_ACCEPT:
if (receive_send == 1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' accepted.", filename);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%d] for '%s' accepted.",
filenum, filename);
/* prep progress bar line */
char progline[MAX_STR_SIZE];
prep_prog_line(progline);

View File

@ -26,6 +26,7 @@
#include "windows.h"
#include "toxic.h"
void chat_close_file_receiver(int num, int filenum);
void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int32_t friendnum);

View File

@ -30,6 +30,8 @@
#include "execute.h"
#include "line_info.h"
#include "groupchat.h"
#include "chat.h"
#include "file_senders.h"
extern ToxWindow *prompt;
@ -39,6 +41,67 @@ extern FileSender file_senders[MAX_FILES];
extern uint8_t max_file_senders_index;
extern uint8_t num_active_file_senders;
void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
if (argc < 2) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid syntax.");
return;
}
const char *inoutstr = argv[1];
int filenum = atoi(argv[2]);
if (filenum > MAX_FILES || filenum < 0) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
return;
}
int inout;
if (strcasecmp(inoutstr, "in") == 0) {
inout = 1;
} else if (strcasecmp(inoutstr, "out") == 0) {
inout = 0;
} else {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error: Type must be 'in' or 'out'.");
return;
}
if (inout == 1) { /* cancel an incoming file transfer */
if (!friends[self->num].file_receiver.active[filenum]) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
return;
}
const char *filepath = friends[self->num].file_receiver.filenames[filenum];
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);
} else { /* cancel an outgoing file transfer */
int i;
bool match = false;
for (i = 0; i < MAX_FILES; ++i) {
if (file_senders[i].active && file_senders[i].filenum == filenum) {
match = true;
break;
}
}
if (!match) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
return;
}
const char *filename = file_senders[i].filename;
char msg[MAX_STR_SIZE];
snprintf(msg, sizeof(msg), "File transfer for '%s' canceled.", filename);
close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, self->num);
}
}
void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{
const char *errmsg;
@ -127,7 +190,7 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
const char *filename = friends[self->num].file_receiver.filenames[filenum];
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file as: '%s'", filename);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file as [%d]: '%s'", filenum, filename);
/* prep progress bar line */
char progline[MAX_STR_SIZE];
@ -146,6 +209,7 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
}
friends[self->num].file_receiver.pending[filenum] = false;
friends[self->num].file_receiver.active[filenum] = true;
}
void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
@ -221,7 +285,7 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
tox_file_data_size(m, self->num), file_to_send);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file: '%s'", filename);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Sending file [%d]: '%s'", filenum, filename);
++num_active_file_senders;

View File

@ -26,6 +26,7 @@
#include "windows.h"
#include "toxic.h"
void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_groupinvite(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_join_group(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
void cmd_savefile(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);

View File

@ -61,6 +61,7 @@ static struct cmd_func global_commands[] = {
};
static struct cmd_func chat_commands[] = {
{ "/cancel", cmd_cancelfile },
{ "/invite", cmd_groupinvite },
{ "/join", cmd_join_group },
{ "/savefile", cmd_savefile },

View File

@ -30,10 +30,10 @@
#ifdef _AUDIO
#define GLOBAL_NUM_COMMANDS 16
#define CHAT_NUM_COMMANDS 11
#define CHAT_NUM_COMMANDS 12
#else
#define GLOBAL_NUM_COMMANDS 14
#define CHAT_NUM_COMMANDS 4
#define CHAT_NUM_COMMANDS 5
#endif /* _AUDIO */
enum {

View File

@ -120,7 +120,7 @@ static void set_max_file_senders_index(void)
max_file_senders_index = j;
}
static void close_file_sender(ToxWindow *self, Tox *m, int i, const char *msg, int CTRL, int filenum, int32_t friendnum)
void close_file_sender(ToxWindow *self, Tox *m, int i, const char *msg, int CTRL, int filenum, int32_t friendnum)
{
if (self->chatwin != NULL)
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg);

View File

@ -56,6 +56,7 @@ 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);
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);

View File

@ -396,14 +396,6 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, u
if (friends[num].chatwin == -1) {
if (get_num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
if (self->active_box != -1)
box_notify2(self, transfer_pending, NT_NOFOCUS, self->active_box,
"Incoming file reaquest: %s", filename);
else
box_notify(self, transfer_pending, NT_NOFOCUS, &self->active_box, self->name,
"Incoming file reaquest: %s", filename);
} else {
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);

View File

@ -33,10 +33,12 @@ struct FileReceiver {
char filenames[MAX_FILES][MAX_STR_SIZE];
FILE *files[MAX_FILES];
bool pending[MAX_FILES];
bool active[MAX_FILES];
uint64_t size[MAX_FILES];
double bps[MAX_FILES];
uint64_t last_progress[MAX_FILES];
uint32_t line_id[MAX_FILES];
int filenums[MAX_FILES];
};
struct LastOnline {

View File

@ -177,7 +177,8 @@ static void help_draw_chat(ToxWindow *self)
wprintw(win, " /invite <n> : Invite contact to a group chat\n");
wprintw(win, " /join : Join a pending group chat\n");
wprintw(win, " /sendfile <path> : Send a file\n");
wprintw(win, " /savefile <n> : Receive a file\n");
wprintw(win, " /savefile <id> : Receive a file\n");
wprintw(win, " /cancel <type> <id> : Cancel file transfer where type: in|out\n");
#ifdef _AUDIO
wattron(win, A_BOLD);