mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-23 01:13:01 +01:00
save files to original name and some bug fixes
This commit is contained in:
parent
7046772a03
commit
973bd3f396
42
src/chat.c
42
src/chat.c
@ -102,15 +102,27 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
|
|||||||
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
|
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
|
||||||
uint64_t filesize, uint8_t *pathname, uint16_t path_len)
|
uint64_t filesize, uint8_t *pathname, uint16_t path_len)
|
||||||
{
|
{
|
||||||
if (self-> num != num)
|
if (self->num != num)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", pathname,
|
/* try to get file name from path */
|
||||||
|
uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths
|
||||||
|
|
||||||
|
if (filename != NULL) {
|
||||||
|
++filename;
|
||||||
|
} else {
|
||||||
|
filename = strrchr(pathname, '\\'); // Try windows style paths
|
||||||
|
|
||||||
|
if (filename == NULL)
|
||||||
|
filename = pathname;
|
||||||
|
}
|
||||||
|
|
||||||
|
wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename,
|
||||||
(long long unsigned int)filesize);
|
(long long unsigned int)filesize);
|
||||||
|
|
||||||
if (filenum > MAX_FILENUMBER) {
|
if (filenum >= MAX_FILENUMBER) {
|
||||||
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
|
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -118,10 +130,10 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
|
|||||||
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
|
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
|
||||||
|
|
||||||
pending_file_transfers[filenum] = num;
|
pending_file_transfers[filenum] = num;
|
||||||
|
strcpy(filenames[filenum], filename);
|
||||||
|
|
||||||
self->blink = true;
|
self->blink = true;
|
||||||
beep();
|
beep();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
@ -133,14 +145,17 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
|
|||||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
switch(control_type) {
|
switch(control_type) {
|
||||||
case 0:
|
case TOX_FILECONTROL_ACCEPT:
|
||||||
wprintw(ctx->history, "File transfer accepted.\n");
|
wprintw(ctx->history, "File transfer accepted.\n");
|
||||||
break;
|
break;
|
||||||
case 3:
|
case TOX_FILECONTROL_PAUSE:
|
||||||
wprintw(ctx->history, "File successfully recieved.\n");
|
wprintw(ctx->history, "File transfer paused.\n");
|
||||||
break;
|
break;
|
||||||
default:
|
case TOX_FILECONTROL_KILL:
|
||||||
wprintw(ctx->history, "Control %u receieved.\n", control_type);
|
wprintw(ctx->history, "File transfer failed.\n");
|
||||||
|
break;
|
||||||
|
case TOX_FILECONTROL_FINISHED:
|
||||||
|
wprintw(ctx->history, "File successfully recieved.\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,10 +171,8 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
|
|||||||
|
|
||||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
char pathname[MAX_STR_SIZE];
|
uint8_t *filename = filenames[filenum];
|
||||||
snprintf(pathname, sizeof(pathname), "%d.%u.bin", num, filenum);
|
FILE *file_to_save = fopen(filename, "a");
|
||||||
|
|
||||||
FILE *file_to_save = fopen(pathname, "a");
|
|
||||||
|
|
||||||
if (fwrite(data, length, 1, file_to_save) != 1) {
|
if (fwrite(data, length, 1, file_to_save) != 1) {
|
||||||
wattron(ctx->history, COLOR_PAIR(RED));
|
wattron(ctx->history, COLOR_PAIR(RED));
|
||||||
@ -221,12 +234,11 @@ static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *pa
|
|||||||
memcpy(file_senders[num_file_senders].pathname, path, path_len + 1);
|
memcpy(file_senders[num_file_senders].pathname, path, path_len + 1);
|
||||||
file_senders[num_file_senders].chatwin = ctx->history;
|
file_senders[num_file_senders].chatwin = ctx->history;
|
||||||
file_senders[num_file_senders].file = file_to_send;
|
file_senders[num_file_senders].file = file_to_send;
|
||||||
file_senders[num_file_senders].filenum = filenum;
|
file_senders[num_file_senders].filenum = (uint8_t) filenum;
|
||||||
file_senders[num_file_senders].friendnum = friendnum;
|
file_senders[num_file_senders].friendnum = friendnum;
|
||||||
file_senders[num_file_senders].piecelen = fread(file_senders[num_file_senders].nextpiece, 1,
|
file_senders[num_file_senders].piecelen = fread(file_senders[num_file_senders].nextpiece, 1,
|
||||||
tox_filedata_size(m, friendnum), file_to_send);
|
tox_filedata_size(m, friendnum), file_to_send);
|
||||||
|
|
||||||
|
|
||||||
wprintw(ctx->history, "Sending file '%s'...\n", path);
|
wprintw(ctx->history, "Sending file '%s'...\n", path);
|
||||||
++num_file_senders;
|
++num_file_senders;
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv
|
|||||||
|
|
||||||
int num = atoi(argv[1]);
|
int num = atoi(argv[1]);
|
||||||
|
|
||||||
if (num < 0 || num > num_frnd_requests) {
|
if (num < 0 || num >= MAX_FRIENDS_NUM) {
|
||||||
wprintw(window, "No pending friend request with that number.\n");
|
wprintw(window, "No pending friend request with that number.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -205,16 +205,16 @@ void cmd_savefile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **ar
|
|||||||
|
|
||||||
uint8_t filenum = atoi(argv[1]);
|
uint8_t filenum = atoi(argv[1]);
|
||||||
|
|
||||||
if (filenum < 0 || filenum > MAX_FILENUMBER) {
|
if (filenum < 0 || filenum >= MAX_FILENUMBER) {
|
||||||
wprintw(window, "File transfer failed.\n");
|
wprintw(window, "File transfer failed.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int friendnum = pending_file_transfers[filenum];
|
int friendnum = pending_file_transfers[filenum];
|
||||||
|
uint8_t *filename = filenames[filenum];
|
||||||
|
|
||||||
if (tox_file_sendcontrol(m, friendnum, 1, filenum, 0, 0, 0))
|
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
|
||||||
wprintw(window, "Accepted file transfer %u. Saving file as %d.%u.bin.\n", filenum, friendnum,
|
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'.\n", filenum, filename);
|
||||||
filenum);
|
|
||||||
else
|
else
|
||||||
wprintw(window, "File transfer failed.\n");
|
wprintw(window, "File transfer failed.\n");
|
||||||
}
|
}
|
||||||
|
@ -104,7 +104,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
FILE *file;
|
FILE *file;
|
||||||
WINDOW *chatwin;
|
WINDOW *chatwin;
|
||||||
uint16_t friendnum;
|
int friendnum;
|
||||||
uint8_t filenum;
|
uint8_t filenum;
|
||||||
uint8_t nextpiece[FILE_PIECE_SIZE];
|
uint8_t nextpiece[FILE_PIECE_SIZE];
|
||||||
uint16_t piecelen;
|
uint16_t piecelen;
|
||||||
@ -115,6 +115,7 @@ FileSender file_senders[NUM_FILE_SENDERS];
|
|||||||
uint8_t num_file_senders;
|
uint8_t num_file_senders;
|
||||||
|
|
||||||
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
||||||
|
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE];
|
||||||
|
|
||||||
/* End file transfer code */
|
/* End file transfer code */
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user