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

refactored file saving/fixed bugs

This commit is contained in:
Jfreegman 2013-10-17 19:53:29 -04:00
parent 973bd3f396
commit 2ff1da396c
6 changed files with 61 additions and 55 deletions

View File

@ -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 uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths
if (filename != NULL) { if (filename != NULL) {
++filename; if (!strlen(++filename))
filename = pathname;
} else { } else {
filename = strrchr(pathname, '\\'); // Try windows style paths 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); wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
pending_file_transfers[filenum] = num; friends[num].file_receiver.pending_file_transfers[filenum] = num;
strcpy(filenames[filenum], filename); strcpy(friends[num].file_receiver.filenames[filenum], filename);
self->blink = true; self->blink = true;
beep(); beep();
@ -144,18 +145,20 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive
ChatContext *ctx = (ChatContext *) self->chatwin; ChatContext *ctx = (ChatContext *) self->chatwin;
uint8_t *filename = friends[num].file_receiver.filenames[filenum];
switch(control_type) { switch(control_type) {
case TOX_FILECONTROL_ACCEPT: case TOX_FILECONTROL_ACCEPT:
wprintw(ctx->history, "File transfer accepted.\n"); 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 paused.\n"); 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 failed.\n"); wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
break; break;
case TOX_FILECONTROL_FINISHED: case TOX_FILECONTROL_FINISHED:
wprintw(ctx->history, "File successfully recieved.\n"); wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
break; break;
} }
@ -171,7 +174,7 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
ChatContext *ctx = (ChatContext *) self->chatwin; 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"); FILE *file_to_save = fopen(filename, "a");
if (fwrite(data, length, 1, file_to_save) != 1) { 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; ++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) static void print_chat_help(ChatContext *ctx)
{ {
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD); 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 ")); chat_groupinvite(self, ctx, m, line + strlen("/invite "));
else if(!strncmp(line, "/sendfile ", strlen("/sendfile "))) else if(!strncmp(line, "/sendfile ", strlen("/sendfile ")))
chat_sendfile(self, ctx, m, line + 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 else
execute(ctx->history, self->prompt, m, line, ctx->pos); execute(ctx->history, self->prompt, m, line, ctx->pos);
} else { } else {

View File

@ -196,29 +196,6 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
free(binary_string); 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) void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
{ {
int ngc = get_num_groupchats(); int ngc = get_num_groupchats();

View File

@ -7,7 +7,6 @@ void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **);
void cmd_add(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_clear(WINDOW *, ToxWindow *, Tox *m, int, char **);
void cmd_connect(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_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char **);
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **); void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
void cmd_msg(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); void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
#define NUM_COMMANDS 15 #define NUM_COMMANDS 14
static struct { static struct {
char *name; char *name;
@ -30,7 +29,6 @@ static struct {
{ "/clear", cmd_clear }, { "/clear", cmd_clear },
{ "/connect", cmd_connect }, { "/connect", cmd_connect },
{ "/exit", cmd_quit }, { "/exit", cmd_quit },
{ "/savefile", cmd_savefile },
{ "/groupchat", cmd_groupchat }, { "/groupchat", cmd_groupchat },
{ "/join", cmd_join }, { "/join", cmd_join },
{ "/msg", cmd_msg }, { "/msg", cmd_msg },

View File

@ -17,23 +17,9 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern ToxWindow *prompt; 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_friends = 0;
static int num_selected = 0; static int num_selected = 0;
static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
{ {
if (num < 0 || num >= num_friends) if (num < 0 || num >= num_friends)

View File

@ -356,20 +356,25 @@ void do_file_senders(Tox *m)
continue; continue;
while (true) { while (true) {
if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenum, uint8_t filenum = file_senders[i].filenum;
file_senders[i].nextpiece, file_senders[i].piecelen)) int friendnum = file_senders[i].friendnum;
if (!tox_file_senddata(m, friendnum, filenum, file_senders[i].nextpiece,
file_senders[i].piecelen))
return; return;
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m, 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) { if (file_senders[i].piecelen == 0) {
fclose(file_senders[i].file); fclose(file_senders[i].file);
file_senders[i].file = NULL; 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); 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; return;
} }
} }

View File

@ -114,11 +114,28 @@ typedef struct {
FileSender file_senders[NUM_FILE_SENDERS]; FileSender file_senders[NUM_FILE_SENDERS];
uint8_t num_file_senders; uint8_t num_file_senders;
uint8_t pending_file_transfers[MAX_FILENUMBER]; typedef struct {
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE]; uint8_t pending_file_transfers[MAX_FILENUMBER];
uint8_t filenames[MAX_FILENUMBER][MAX_STR_SIZE];
} FileReceiver;
/* End file transfer code */ /* 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_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_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); void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);