mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-22 21:53:05 +01:00
small refactor for incoming file transfers
This commit is contained in:
parent
92d5b2fefc
commit
d1153f96ca
37
src/chat.c
37
src/chat.c
@ -223,7 +223,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
|
|||||||
strcat(filename, d);
|
strcat(filename, d);
|
||||||
filename[len + strlen(d)] = '\0';
|
filename[len + strlen(d)] = '\0';
|
||||||
|
|
||||||
if (count > 999999) {
|
if (count > 999) {
|
||||||
wprintw(ctx->history, "Error saving file to disk.\n");
|
wprintw(ctx->history, "Error saving file to disk.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -237,6 +237,15 @@ 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(num, filenum)
|
||||||
|
{
|
||||||
|
friends[num].file_receiver.pending[filenum] = false;
|
||||||
|
FILE *file = friends[num].file_receiver.files[filenum];
|
||||||
|
|
||||||
|
if (file != NULL)
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
||||||
static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send,
|
static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send,
|
||||||
uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length)
|
uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length)
|
||||||
{
|
{
|
||||||
@ -245,6 +254,7 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
|
|||||||
|
|
||||||
ChatContext *ctx = self->chatwin;
|
ChatContext *ctx = self->chatwin;
|
||||||
uint8_t *filename;
|
uint8_t *filename;
|
||||||
|
bool close_in_file = false;
|
||||||
|
|
||||||
if (receive_send == 0)
|
if (receive_send == 0)
|
||||||
filename = friends[num].file_receiver.filenames[filenum];
|
filename = friends[num].file_receiver.filenames[filenum];
|
||||||
@ -255,20 +265,20 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
|
|||||||
case TOX_FILECONTROL_ACCEPT:
|
case TOX_FILECONTROL_ACCEPT:
|
||||||
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
|
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
|
||||||
break;
|
break;
|
||||||
case TOX_FILECONTROL_PAUSE:
|
// case TOX_FILECONTROL_PAUSE:
|
||||||
// wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
// wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
||||||
break;
|
// break;
|
||||||
case TOX_FILECONTROL_KILL:
|
case TOX_FILECONTROL_KILL:
|
||||||
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)
|
||||||
friends[num].file_receiver.pending[filenum] = false;
|
close_file_receiver(num, filenum);
|
||||||
else
|
else
|
||||||
close_file_sender(filenum);
|
close_file_sender(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);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,25 +293,12 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
|
|||||||
|
|
||||||
ChatContext *ctx = self->chatwin;
|
ChatContext *ctx = self->chatwin;
|
||||||
|
|
||||||
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
|
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
|
||||||
FILE *file_to_save = fopen(filename, "a");
|
|
||||||
|
|
||||||
if (file_to_save == NULL) {
|
|
||||||
wattron(ctx->history, COLOR_PAIR(RED));
|
|
||||||
wprintw(ctx->history, "* Error writing to file.\n");
|
|
||||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
|
||||||
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fwrite(data, length, 1, file_to_save) != 1) {
|
|
||||||
wattron(ctx->history, COLOR_PAIR(RED));
|
wattron(ctx->history, COLOR_PAIR(RED));
|
||||||
wprintw(ctx->history, "* Error writing to file.\n");
|
wprintw(ctx->history, "* Error writing to file.\n");
|
||||||
wattroff(ctx->history, COLOR_PAIR(RED));
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||||
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(file_to_save);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
|
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
|
||||||
|
@ -139,10 +139,18 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
|
|||||||
|
|
||||||
uint8_t *filename = friends[self->num].file_receiver.filenames[filenum];
|
uint8_t *filename = friends[self->num].file_receiver.filenames[filenum];
|
||||||
|
|
||||||
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0)
|
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
|
||||||
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
|
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
|
||||||
else
|
|
||||||
|
if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) {
|
||||||
|
wattron(window, COLOR_PAIR(RED));
|
||||||
|
wprintw(window, "* Error writing to file.\n");
|
||||||
|
wattroff(window, COLOR_PAIR(RED));
|
||||||
|
tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
wprintw(window, "File transfer failed.\n");
|
wprintw(window, "File transfer failed.\n");
|
||||||
|
}
|
||||||
|
|
||||||
friends[self->num].file_receiver.pending[filenum] = false;
|
friends[self->num].file_receiver.pending[filenum] = false;
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,6 @@ void init_logging_session(uint8_t *name, uint8_t *key, struct chatlog *log)
|
|||||||
|
|
||||||
if (path_len > MAX_STR_SIZE) {
|
if (path_len > MAX_STR_SIZE) {
|
||||||
log->log_on = false;
|
log->log_on = false;
|
||||||
log->file = NULL;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,11 +99,10 @@ void write_to_log(uint8_t *msg, uint8_t *name, struct chatlog *log, bool event)
|
|||||||
|
|
||||||
uint64_t curtime = (uint64_t) time(NULL);
|
uint64_t curtime = (uint64_t) time(NULL);
|
||||||
|
|
||||||
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT))
|
if (timed_out(log->lastwrite, curtime, LOG_FLUSH_LIMIT)) {
|
||||||
fflush(log->file);
|
fflush(log->file);
|
||||||
|
log->lastwrite = curtime;
|
||||||
log->lastwrite = curtime;
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
|
void log_enable(uint8_t *name, uint8_t *key, struct chatlog *log)
|
||||||
|
@ -206,6 +206,7 @@ typedef struct {
|
|||||||
|
|
||||||
struct FileReceiver {
|
struct FileReceiver {
|
||||||
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
|
uint8_t filenames[MAX_FILES][MAX_STR_SIZE];
|
||||||
|
FILE *files[MAX_FILES];
|
||||||
bool pending[MAX_FILES];
|
bool pending[MAX_FILES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user