1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-03 17:07:45 +02:00

update file transfers for core changes

This commit is contained in:
Jfreegman 2014-08-04 16:22:29 -04:00
parent 28633be2dd
commit 64db9f73a2
5 changed files with 55 additions and 38 deletions

View File

@ -345,15 +345,20 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int32_t num, uint8_t
"Incoming file: %s", filename ); "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; if (CTRL > 0)
friends[num].file_receiver.pending[filenum] = false; tox_file_send_control(m, friendnum, 1, filenum, CTRL, 0, 0);
FILE *file = friends[num].file_receiver.files[filenum];
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) { if (file != NULL) {
fclose(file); 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: case TOX_FILECONTROL_KILL:
snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); 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) if (self->active_box != -1)
box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, 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' failed!", filename );
else else
box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, 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' 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; break;
case TOX_FILECONTROL_FINISHED: case TOX_FILECONTROL_FINISHED:
if (receive_send == 0) { if (receive_send == 0) {
snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename); snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename);
chat_close_file_receiver(num, filenum); chat_close_file_receiver(m, filenum, num, TOX_FILECONTROL_FINISHED);
} else {
if (self->active_box != -1) char msg[MAX_STR_SIZE];
box_notify2(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", filename);
self->active_box, "File '%s' successfuly sent!", filename ); close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, num);
else return;
box_notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box,
self->name, "File '%s' successfuly sent!", filename );
} }
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; break;
} }
@ -439,8 +451,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int32_t num, uint8_t filenu
if (fp) { if (fp) {
if (fwrite(data, length, 1, fp) != 1) { if (fwrite(data, length, 1, fp) != 1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Error writing to file."); 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(m, filenum, num, TOX_FILECONTROL_KILL);
chat_close_file_receiver(num, filenum);
} }
} }

View File

@ -26,7 +26,9 @@
#include "windows.h" #include "windows.h"
#include "toxic.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); void kill_chat_window(ToxWindow *self);
ToxWindow new_chat(Tox *m, int32_t friendnum); ToxWindow new_chat(Tox *m, int32_t friendnum);

View File

@ -66,8 +66,7 @@ void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*ar
char name[MAX_STR_SIZE]; char name[MAX_STR_SIZE];
get_file_name(name, sizeof(name), filepath); get_file_name(name, sizeof(name), filepath);
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' canceled.", name); 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(m, filenum, self->num, TOX_FILECONTROL_KILL);
chat_close_file_receiver(self->num, filenum);
return; return;
} else if (strcasecmp(inoutstr, "out") == 0) { /* cancel an outgoing file transfer */ } else if (strcasecmp(inoutstr, "out") == 0) { /* cancel an outgoing file transfer */
int i; int i;

View File

@ -120,13 +120,16 @@ static void set_max_file_senders_index(void)
max_file_senders_index = j; 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) 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); 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); fclose(file_senders[i].file);
memset(&file_senders[i], 0, sizeof(FileSender)); memset(&file_senders[i], 0, sizeof(FileSender));
set_max_file_senders_index(); 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; file_senders[i].bps = 0;
} }
if (file_senders[i].piecelen == 0) { /* file sender is closed in chat_onFileControl callback after receiving reply */
char msg[MAX_STR_SIZE]; if (file_senders[i].piecelen == 0 && !file_senders[i].finished) {
snprintf(msg, sizeof(msg), "File '%s' successfuly sent.", filename); tox_file_send_control(m, friendnum, 0, filenum, TOX_FILECONTROL_FINISHED, 0, 0);
close_file_sender(self, m, i, msg, TOX_FILECONTROL_FINISHED, filenum, friendnum); file_senders[i].finished = true;
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;
} }
} }
} }
@ -208,6 +203,12 @@ void do_file_senders(Tox *m)
int filenum = file_senders[i].filenum; int filenum = file_senders[i].filenum;
int32_t friendnum = file_senders[i].friendnum; 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 file transfer has timed out kill transfer and send kill control */
if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) { if (timed_out(file_senders[i].timestamp, get_unix_time(), TIMEOUT_FILESENDER)) {
char msg[MAX_STR_SIZE]; char msg[MAX_STR_SIZE];
@ -217,10 +218,10 @@ void do_file_senders(Tox *m)
if (self->active_box != -1) if (self->active_box != -1)
box_notify2(self, error, NT_NOFOCUS | NT_WNDALERT_2, 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 else
box_notify(self, error, NT_NOFOCUS | NT_WNDALERT_2, &self->active_box, 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; continue;
} }

View File

@ -36,6 +36,7 @@ typedef struct {
ToxWindow *toxwin; ToxWindow *toxwin;
int32_t friendnum; int32_t friendnum;
bool active; bool active;
bool finished;
int filenum; int filenum;
char nextpiece[FILE_PIECE_SIZE]; char nextpiece[FILE_PIECE_SIZE];
uint16_t piecelen; 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. */ 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 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_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 close_all_file_senders(Tox *m);
void do_file_senders(Tox *m); void do_file_senders(Tox *m);