From 76bfefff6f312024595fba8fd8115bbf93084946 Mon Sep 17 00:00:00 2001 From: Michael Rose Date: Thu, 15 Aug 2013 12:11:48 +0200 Subject: Merge fix --- testing/toxic/main.c | 114 +++++++++++++++++++++++++++++------------------- testing/toxic/prompt.c | 8 ++-- testing/toxic/windows.c | 6 +++ testing/toxic/windows.h | 1 + 4 files changed, 81 insertions(+), 48 deletions(-) (limited to 'testing/toxic') diff --git a/testing/toxic/main.c b/testing/toxic/main.c index aa025a1a..4d52469a 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c @@ -25,6 +25,8 @@ #include "prompt.h" #include "friendlist.h" +/* Export for use in Callbacks */ +char *DATA_FILE = NULL; void on_window_resize(int sig) { @@ -145,67 +147,90 @@ static void do_tox(Messenger *m, ToxWindow * prompt) doMessenger(m); } -static void load_data(Messenger *m, char *path) +/* + * Store Messenger to given location + * Return 0 stored successfully + * Return 1 malloc failed + * Return 2 opening path failed + * Return 3 fwrite failed + */ +int store_data(Messenger *m, char *path) { - FILE *fd; - size_t len; - uint8_t *buf; - - if ((fd = fopen(path, "r")) != NULL) { - fseek(fd, 0, SEEK_END); - len = ftell(fd); - fseek(fd, 0, SEEK_SET); + FILE *fd; + size_t len; + uint8_t *buf; - buf = malloc(len); - if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - fclose(fd); - endwin(); - exit(1); - } - if (fread(buf, len, 1, fd) != 1){ - fprintf(stderr, "fread() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); - } - Messenger_load(m, buf, len); - } - else { len = Messenger_size(m); buf = malloc(len); if (buf == NULL) { - fprintf(stderr, "malloc() failed.\n"); - endwin(); - exit(1); + return 1; } Messenger_save(m, buf); fd = fopen(path, "w"); if (fd == NULL) { - fprintf(stderr, "fopen() failed.\n"); - free(buf); - endwin(); - exit(1); + free(buf); + return 2; } - if (fwrite(buf, len, 1, fd) != 1){ - fprintf(stderr, "fwrite() failed.\n"); - free(buf); - fclose(fd); - endwin(); - exit(1); + if (fwrite(buf, len, 1, fd) != 1) { + free(buf); + fclose(fd); + return 3; + } + + free(buf); + fclose(fd); + return 0; +} + +static void load_data(Messenger *m, char *path) +{ + FILE *fd; + size_t len; + uint8_t *buf; + + if ((fd = fopen(path, "r")) != NULL) { + fseek(fd, 0, SEEK_END); + len = ftell(fd); + fseek(fd, 0, SEEK_SET); + + buf = malloc(len); + if (buf == NULL) { + fprintf(stderr, "malloc() failed.\n"); + fclose(fd); + endwin(); + exit(1); + } + if (fread(buf, len, 1, fd) != 1) { + fprintf(stderr, "fread() failed.\n"); + free(buf); + fclose(fd); + endwin(); + exit(1); + } + Messenger_load(m, buf, len); + + uint32_t i; + for (i = 0; i < m->numfriends; i++) { + on_friendadded(m, i); + } + + free(buf); + fclose(fd); + } else { + int st; + if ((st = store_data(m, path)) != 0) { + fprintf(stderr, "Store messenger failed with return code: %d\n", st); + endwin(); + exit(1); + } } - } - free(buf); - fclose(fd); } int main(int argc, char *argv[]) { char *user_config_dir = get_user_config_dir(); - char *DATA_FILE = NULL; int config_err = 0; int f_loadfromfile = 1; @@ -246,7 +271,6 @@ int main(int argc, char *argv[]) if(f_loadfromfile) load_data(m, DATA_FILE); - free(DATA_FILE); if (f_flag == -1) { attron(COLOR_PAIR(3) | A_BOLD); @@ -268,6 +292,8 @@ int main(int argc, char *argv[]) /* Draw */ draw_active_window(m); } + cleanupMessenger(m); + free(DATA_FILE); return 0; } diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index b84773af..1d0e0144 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c @@ -16,7 +16,7 @@ uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t num_requests=0; // XXX -static friendAddedFn *on_friendadded; +static friendAddedFn *on_friendadded_cb; static char prompt_buf[MAX_STR_SIZE] = {0}; static int prompt_buf_pos = 0; @@ -88,7 +88,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args) wprintw(self->window, "Failed to add friend.\n"); else { wprintw(self->window, "Friend accepted as: %d.\n", num); - on_friendadded(m, num); + on_friendadded_cb(m, num); } } @@ -152,7 +152,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) break; default: wprintw(self->window, "Friend added as %d.\n", num); - on_friendadded(m, num); + on_friendadded_cb(m, num); break; } } @@ -436,7 +436,7 @@ static void prompt_onInit(ToxWindow *self, Messenger *m) ToxWindow new_prompt(friendAddedFn *f) { - on_friendadded = f; + on_friendadded_cb = f; ToxWindow ret; memset(&ret, 0, sizeof(ret)); ret.onKey = &prompt_onKey; diff --git a/testing/toxic/windows.c b/testing/toxic/windows.c index 6de50741..15caddf7 100644 --- a/testing/toxic/windows.c +++ b/testing/toxic/windows.c @@ -3,6 +3,9 @@ #include "dhtstatus.h" #include "windows.h" +extern char *DATA_FILE; +extern int store_data(Messenger *m, char *path); + /* Holds status of chat windows */ char WINDOW_STATUS[MAX_WINDOW_SLOTS]; @@ -74,6 +77,9 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l void on_friendadded(Messenger *m, int friendnumber) { friendlist_onFriendAdded(m, friendnumber); + if (store_data(m, DATA_FILE) != 0) { + wprintw(prompt->window, "\nCould not store Messenger data\n"); + } } /* CALLBACKS END */ diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 893ccf6f..9f4d74a2 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h @@ -47,6 +47,7 @@ void on_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); +void on_friendadded(Messenger *m, int friendnumber); void init_window_status(); ToxWindow * init_windows(); void draw_active_window(Messenger * m); -- cgit v1.2.3