mirror of
https://github.com/Tha14/toxic.git
synced 2024-12-23 15:33:25 +01:00
refactored file saving/fixed bugs
This commit is contained in:
parent
973bd3f396
commit
2ff1da396c
39
src/chat.c
39
src/chat.c
@ -111,7 +111,8 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
|
||||
uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths
|
||||
|
||||
if (filename != NULL) {
|
||||
++filename;
|
||||
if (!strlen(++filename))
|
||||
filename = pathname;
|
||||
} else {
|
||||
filename = strrchr(pathname, '\\'); // Try windows style paths
|
||||
|
||||
@ -129,8 +130,8 @@ 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);
|
||||
|
||||
pending_file_transfers[filenum] = num;
|
||||
strcpy(filenames[filenum], filename);
|
||||
friends[num].file_receiver.pending_file_transfers[filenum] = num;
|
||||
strcpy(friends[num].file_receiver.filenames[filenum], filename);
|
||||
|
||||
self->blink = true;
|
||||
beep();
|
||||
@ -144,18 +145,20 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
|
||||
|
||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||
|
||||
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
|
||||
|
||||
switch(control_type) {
|
||||
case TOX_FILECONTROL_ACCEPT:
|
||||
wprintw(ctx->history, "File transfer accepted.\n");
|
||||
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
|
||||
break;
|
||||
case TOX_FILECONTROL_PAUSE:
|
||||
wprintw(ctx->history, "File transfer paused.\n");
|
||||
wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
|
||||
break;
|
||||
case TOX_FILECONTROL_KILL:
|
||||
wprintw(ctx->history, "File transfer failed.\n");
|
||||
wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
|
||||
break;
|
||||
case TOX_FILECONTROL_FINISHED:
|
||||
wprintw(ctx->history, "File successfully recieved.\n");
|
||||
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -171,7 +174,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
|
||||
|
||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||
|
||||
uint8_t *filename = filenames[filenum];
|
||||
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
|
||||
FILE *file_to_save = fopen(filename, "a");
|
||||
|
||||
if (fwrite(data, length, 1, file_to_save) != 1) {
|
||||
@ -243,6 +246,24 @@ static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *pa
|
||||
++num_file_senders;
|
||||
}
|
||||
|
||||
static void chat_savefile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *num)
|
||||
{
|
||||
uint8_t filenum = atoi(num);
|
||||
|
||||
if (filenum < 0 || filenum >= MAX_FILENUMBER) {
|
||||
wprintw(ctx->history, "File transfer failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int friendnum = self->num;
|
||||
uint8_t *filename = friends[friendnum].file_receiver.filenames[filenum];
|
||||
|
||||
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
|
||||
wprintw(ctx->history, "Accepted file transfer %u. Saving file as: '%s'\n", filenum, filename);
|
||||
else
|
||||
wprintw(ctx->history, "File transfer failed.\n");
|
||||
}
|
||||
|
||||
static void print_chat_help(ChatContext *ctx)
|
||||
{
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||
@ -344,6 +365,8 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
chat_groupinvite(self, ctx, m, line + strlen("/invite "));
|
||||
else if(!strncmp(line, "/sendfile ", strlen("/sendfile ")))
|
||||
chat_sendfile(self, ctx, m, line + strlen("/sendfile "));
|
||||
else if(!strncmp(line, "/savefile ", strlen("/savefile ")))
|
||||
chat_savefile(self, ctx, m, line + strlen("/savefile "));
|
||||
else
|
||||
execute(ctx->history, self->prompt, m, line, ctx->pos);
|
||||
} else {
|
||||
|
@ -196,29 +196,6 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
|
||||
free(binary_string);
|
||||
}
|
||||
|
||||
void cmd_savefile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc < 1) {
|
||||
wprintw(window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t filenum = atoi(argv[1]);
|
||||
|
||||
if (filenum < 0 || filenum >= MAX_FILENUMBER) {
|
||||
wprintw(window, "File transfer failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int friendnum = pending_file_transfers[filenum];
|
||||
uint8_t *filename = filenames[filenum];
|
||||
|
||||
if (tox_file_sendcontrol(m, friendnum, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0))
|
||||
wprintw(window, "Accepted file transfer %u. Saving file as: '%s'.\n", filenum, filename);
|
||||
else
|
||||
wprintw(window, "File transfer failed.\n");
|
||||
}
|
||||
|
||||
void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
int ngc = get_num_groupchats();
|
||||
|
@ -7,7 +7,6 @@ void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_savefile(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_msg(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
@ -19,7 +18,7 @@ void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
|
||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
|
||||
|
||||
#define NUM_COMMANDS 15
|
||||
#define NUM_COMMANDS 14
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
@ -30,7 +29,6 @@ static struct {
|
||||
{ "/clear", cmd_clear },
|
||||
{ "/connect", cmd_connect },
|
||||
{ "/exit", cmd_quit },
|
||||
{ "/savefile", cmd_savefile },
|
||||
{ "/groupchat", cmd_groupchat },
|
||||
{ "/join", cmd_join },
|
||||
{ "/msg", cmd_msg },
|
||||
|
@ -17,23 +17,9 @@
|
||||
extern char *DATA_FILE;
|
||||
extern ToxWindow *prompt;
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[TOX_MAX_NAME_LENGTH];
|
||||
uint16_t namelength;
|
||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
|
||||
uint16_t statusmsg_len;
|
||||
int num;
|
||||
int chatwin;
|
||||
bool active;
|
||||
bool online;
|
||||
TOX_USERSTATUS status;
|
||||
} friend_t;
|
||||
|
||||
static friend_t friends[MAX_FRIENDS_NUM];
|
||||
static int num_friends = 0;
|
||||
static int num_selected = 0;
|
||||
|
||||
|
||||
static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
|
||||
{
|
||||
if (num < 0 || num >= num_friends)
|
||||
|
15
src/main.c
15
src/main.c
@ -356,20 +356,25 @@ void do_file_senders(Tox *m)
|
||||
continue;
|
||||
|
||||
while (true) {
|
||||
if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenum,
|
||||
file_senders[i].nextpiece, file_senders[i].piecelen))
|
||||
uint8_t filenum = file_senders[i].filenum;
|
||||
int friendnum = file_senders[i].friendnum;
|
||||
|
||||
if (!tox_file_senddata(m, friendnum, filenum, file_senders[i].nextpiece,
|
||||
file_senders[i].piecelen))
|
||||
return;
|
||||
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m,
|
||||
file_senders[i].friendnum), file_senders[i].file);
|
||||
friendnum), file_senders[i].file);
|
||||
|
||||
if (file_senders[i].piecelen == 0) {
|
||||
fclose(file_senders[i].file);
|
||||
file_senders[i].file = NULL;
|
||||
tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenum,
|
||||
tox_file_sendcontrol(m, friendnum, 0, filenum,
|
||||
TOX_FILECONTROL_FINISHED, 0, 0);
|
||||
|
||||
wprintw(file_senders[i].chatwin, "File successfuly sent.\n");
|
||||
uint8_t *pathname = file_senders[i].pathname;
|
||||
|
||||
wprintw(file_senders[i].chatwin, "File '%s' successfuly sent.\n", pathname);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -114,11 +114,28 @@ typedef struct {
|
||||
FileSender file_senders[NUM_FILE_SENDERS];
|
||||
uint8_t num_file_senders;
|
||||
|
||||
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
||||
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE];
|
||||
typedef struct {
|
||||
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
||||
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE];
|
||||
} FileReceiver;
|
||||
|
||||
/* End file transfer code */
|
||||
|
||||
typedef struct {
|
||||
uint8_t name[TOX_MAX_NAME_LENGTH];
|
||||
uint16_t namelength;
|
||||
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
|
||||
uint16_t statusmsg_len;
|
||||
int num;
|
||||
int chatwin;
|
||||
bool active;
|
||||
bool online;
|
||||
TOX_USERSTATUS status;
|
||||
FileReceiver file_receiver;
|
||||
} friend_t;
|
||||
|
||||
friend_t friends[MAX_FRIENDS_NUM];
|
||||
|
||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
|
||||
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata);
|
||||
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
||||
|
Loading…
Reference in New Issue
Block a user