From 16b8f85d955c24fce4827c7156b64705f4660e49 Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Tue, 11 Feb 2014 18:52:04 -0500 Subject: [PATCH] send name instead of full path with file send request --- src/chat.c | 18 +----------------- src/chat_commands.c | 3 ++- src/groupchat.c | 4 +--- src/misc_tools.c | 23 +++++++++++++++++++++++ src/misc_tools.h | 3 +++ 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/chat.c b/src/chat.c index e3703b0..8077b10 100644 --- a/src/chat.c +++ b/src/chat.c @@ -136,23 +136,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil ChatContext *ctx = self->chatwin; - int idx = strlen(pathname) - 1; - while (idx >= 0 && pathname[idx] == '/') { - pathname[idx--] = 0; - } - - /* try to get file name from path */ - uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths - - if (filename != NULL) { - if (!strlen(++filename)) - filename = pathname; - } else { - filename = strrchr(pathname, '\\'); // Try windows style paths - - if (filename == NULL) - filename = pathname; - } + uint8_t *filename = get_file_name(pathname); wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename, (long long unsigned int)filesize); diff --git a/src/chat_commands.c b/src/chat_commands.c index 17459df..32dedde 100644 --- a/src/chat_commands.c +++ b/src/chat_commands.c @@ -164,7 +164,8 @@ void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv uint64_t filesize = ftell(file_to_send); fseek(file_to_send, 0, SEEK_SET); - int filenum = tox_new_file_sender(m, self->num, filesize, path, path_len + 1); + uint8_t *filename = get_file_name(path); + int filenum = tox_new_file_sender(m, self->num, filesize, filename, strlen(filename) + 1); if (filenum == -1) { wprintw(window, "Error sending file.\n"); diff --git a/src/groupchat.c b/src/groupchat.c index dc5e9f1..f4fa94e 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -216,11 +216,9 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu /* get old peer name before updating name list */ uint8_t oldpeername[TOX_MAX_NAME_LENGTH] = {0}; - if (change != TOX_CHAT_CHANGE_PEER_ADD) { + if (change != TOX_CHAT_CHANGE_PEER_ADD) memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum*TOX_MAX_NAME_LENGTH], sizeof(oldpeername)); - oldpeername[TOXIC_MAX_NAME_LENGTH] = '\0'; - } /* Update name lists */ uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH]; diff --git a/src/misc_tools.c b/src/misc_tools.c index a94087b..d3669b6 100644 --- a/src/misc_tools.c +++ b/src/misc_tools.c @@ -193,3 +193,26 @@ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x) int end_x = len % max_x; wmove(w, end_y, end_x); } + +/* Returns base file name from path or original file name if no path is supplied */ +uint8_t *get_file_name(uint8_t *pathname) +{ + int idx = strlen(pathname) - 1; + + while (idx >= 0 && pathname[idx] == '/') + pathname[idx--] = '\0'; + + uint8_t *filename = strrchr(pathname, '/'); // Try unix style paths + + if (filename != NULL) { + if (!strlen(++filename)) + filename = pathname; + } else { + filename = strrchr(pathname, '\\'); // Try windows style paths + + if (filename == NULL) + filename = pathname; + } + + return filename; +} diff --git a/src/misc_tools.h b/src/misc_tools.h index 93aa907..1aa6c48 100644 --- a/src/misc_tools.h +++ b/src/misc_tools.h @@ -47,3 +47,6 @@ bool valid_nick(uint8_t *nick); /* Moves the cursor to the end of the line in given window */ void mv_curs_end(WINDOW *w, size_t len, int max_y, int max_x); + +/* Returns base file name from path or original file name if no path is supplied */ +uint8_t *get_file_name(uint8_t *pathname);