From fde8059a4cf489d4576f9720f3c6328bfaaa632f Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Mon, 9 Sep 2013 00:56:47 -0400 Subject: [PATCH] tweaks and fixes --- src/chat.c | 3 ++- src/friendlist.c | 16 ++++------------ src/main.c | 33 +++++++++++++++++++++------------ src/prompt.c | 13 +++++++------ src/toxic_windows.h | 2 +- src/windows.c | 5 ++++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/chat.c b/src/chat.c index 4f941e3..f841f45 100644 --- a/src/chat.c +++ b/src/chat.c @@ -98,7 +98,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin beep(); } -static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) +static void chat_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *nick, uint16_t len) { if (self->friendnum != num) return; @@ -276,6 +276,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd) else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); store_data(m, DATA_FILE); + free(DATA_FILE); tox_kill(m); exit(0); } diff --git a/src/friendlist.c b/src/friendlist.c index e0bb309..6926626 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -56,13 +56,12 @@ void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t sta friends[num].online = false; } -void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) +void friendlist_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) { if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends) return; memcpy((char *) &friends[num].name, (char *) str, len); - friends[num].name[len] = 0; } void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) @@ -78,9 +77,7 @@ void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, ui if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends) return; - /* Ignore default "Online" status message */ - if (strncmp(str, "Online", strlen(str))) - memcpy((char *) &friends[num].statusmsg, (char *) str, len); + memcpy((char *) &friends[num].statusmsg, (char *) str, len); } int friendlist_onFriendAdded(Tox *m, int num) @@ -219,14 +216,9 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m) wattron(self->window, COLOR_PAIR(colour) | A_BOLD); wprintw(self->window, "O"); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD); - wprintw(self->window, "]%s", friends[i].name); - - if (friends[i].statusmsg[0]) - wprintw(self->window, " (%s)\n", friends[i].statusmsg); - else - wprintw(self->window, "\n"); + wprintw(self->window, "]%s (%s)\n", friends[i].name, friends[i].statusmsg); } else { - wprintw(self->window, "[O]%s\n", friends[i].name); + wprintw(self->window, "[O]%s\n", friends[i].name, friends[i].statusmsg); } } } diff --git a/src/main.c b/src/main.c index 68808a7..3f0a2a8 100644 --- a/src/main.c +++ b/src/main.c @@ -41,6 +41,7 @@ #ifndef PACKAGE_DATADIR #define PACKAGE_DATADIR "." #endif + /* Export for use in Callbacks */ char *DATA_FILE = NULL; char *SRVLIST_FILE = NULL; @@ -254,15 +255,19 @@ int f_loadfromfile; /* * Store Messenger to given location * Return 0 stored successfully - * Return 1 malloc failed - * Return 2 opening path failed - * Return 3 fwrite failed + * Return 1 file path is NULL + * Return 2 malloc failed + * Return 3 opening path failed + * Return 4 fwrite failed */ int store_data(Tox *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return 0; + if (path == NULL) + return 1; + FILE *fd; size_t len; uint8_t *buf; @@ -271,7 +276,7 @@ int store_data(Tox *m, char *path) buf = malloc(len); if (buf == NULL) { - return 1; + return 2; } tox_save(m, buf); @@ -280,13 +285,13 @@ int store_data(Tox *m, char *path) if (fd == NULL) { free(buf); - return 2; + return 3; } if (fwrite(buf, len, 1, fd) != 1) { free(buf); fclose(fd); - return 3; + return 4; } free(buf); @@ -380,14 +385,18 @@ int main(int argc, char *argv[]) SRVLIST_FILE = strdup(PACKAGE_DATADIR "/DHTservers"); } else { DATA_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("data") + 1); - strcpy(DATA_FILE, user_config_dir); - strcat(DATA_FILE, CONFIGDIR); - strcat(DATA_FILE, "data"); + if (DATA_FILE != NULL) { + strcpy(DATA_FILE, user_config_dir); + strcat(DATA_FILE, CONFIGDIR); + strcat(DATA_FILE, "data"); + } SRVLIST_FILE = malloc(strlen(user_config_dir) + strlen(CONFIGDIR) + strlen("DHTservers") + 1); - strcpy(SRVLIST_FILE, user_config_dir); - strcat(SRVLIST_FILE, CONFIGDIR); - strcat(SRVLIST_FILE, "DHTservers"); + if (SRVLIST_FILE != NULL) { + strcpy(SRVLIST_FILE, user_config_dir); + strcat(SRVLIST_FILE, CONFIGDIR); + strcat(SRVLIST_FILE, "DHTservers"); + } } } diff --git a/src/prompt.c b/src/prompt.c index d82c188..c7cfeec 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -77,7 +77,7 @@ void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status) statusbar->status = status; } -/* Updates own connection status */ +/* Updates own connection status in prompt statusbar */ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected) { StatusBar *statusbar = (StatusBar *) prompt->s; @@ -296,6 +296,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) { endwin(); store_data(m, DATA_FILE); + free(DATA_FILE); tox_kill(m); exit(0); } @@ -520,8 +521,8 @@ static void execute(ToxWindow *self, Tox *m, char *u_cmd) /* read arguments into array */ char **cmdargs = malloc((numargs + 1) * sizeof(char *)); if (!cmdargs) { - wprintw(self->window, "Invalid command: too many arguments.\n"); - return; + wprintw(self->window, "Invalid command: too many arguments.\n"); + return; } int pos = 0; @@ -625,7 +626,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) } wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, "%s ", statusbar->nick); + wprintw(statusbar->topline, " %s ", statusbar->nick); wattron(statusbar->topline, A_BOLD); wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD); wprintw(statusbar->topline, "[%s]", status_text); @@ -638,7 +639,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) } wattron(statusbar->topline, A_BOLD); - wprintw(statusbar->topline, " | %s", statusbar->statusmsg); + wprintw(statusbar->topline, " | %s |", statusbar->statusmsg); wattroff(statusbar->topline, A_BOLD); wprintw(statusbar->topline, "\n"); @@ -676,7 +677,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m) /* temporary until statusmessage saving works */ uint8_t *statusmsg = "Toxing on Toxic v0.2.0"; - // tox_copy_self_statusmessage(m, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH); + m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init statusbar subwindow */ diff --git a/src/toxic_windows.h b/src/toxic_windows.h index 7df1f1c..269ce2b 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -43,7 +43,7 @@ struct ToxWindow_ { void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); - void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); + void(*onNickChange)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); diff --git a/src/windows.c b/src/windows.c index 6ccc70a..bd490c9 100644 --- a/src/windows.c +++ b/src/windows.c @@ -88,8 +88,11 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v for (i = 0; i < MAX_WINDOWS_NUM; ++i) { if (windows[i].onNickChange != NULL) - windows[i].onNickChange(&windows[i], friendnumber, string, length); + windows[i].onNickChange(&windows[i], m, friendnumber, string, length); } + + if (store_data(m, DATA_FILE)) + wprintw(prompt->window, "\nCould not store Tox data\n"); } void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)