diff options
-rw-r--r-- | testing/toxic/main.c | 114 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 8 | ||||
-rw-r--r-- | testing/toxic/windows.c | 6 | ||||
-rw-r--r-- | testing/toxic/windows.h | 1 |
4 files changed, 81 insertions, 48 deletions
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 @@ | |||
25 | #include "prompt.h" | 25 | #include "prompt.h" |
26 | #include "friendlist.h" | 26 | #include "friendlist.h" |
27 | 27 | ||
28 | /* Export for use in Callbacks */ | ||
29 | char *DATA_FILE = NULL; | ||
28 | 30 | ||
29 | void on_window_resize(int sig) | 31 | void on_window_resize(int sig) |
30 | { | 32 | { |
@@ -145,67 +147,90 @@ static void do_tox(Messenger *m, ToxWindow * prompt) | |||
145 | doMessenger(m); | 147 | doMessenger(m); |
146 | } | 148 | } |
147 | 149 | ||
148 | static void load_data(Messenger *m, char *path) | 150 | /* |
151 | * Store Messenger to given location | ||
152 | * Return 0 stored successfully | ||
153 | * Return 1 malloc failed | ||
154 | * Return 2 opening path failed | ||
155 | * Return 3 fwrite failed | ||
156 | */ | ||
157 | int store_data(Messenger *m, char *path) | ||
149 | { | 158 | { |
150 | FILE *fd; | 159 | FILE *fd; |
151 | size_t len; | 160 | size_t len; |
152 | uint8_t *buf; | 161 | uint8_t *buf; |
153 | |||
154 | if ((fd = fopen(path, "r")) != NULL) { | ||
155 | fseek(fd, 0, SEEK_END); | ||
156 | len = ftell(fd); | ||
157 | fseek(fd, 0, SEEK_SET); | ||
158 | 162 | ||
159 | buf = malloc(len); | ||
160 | if (buf == NULL) { | ||
161 | fprintf(stderr, "malloc() failed.\n"); | ||
162 | fclose(fd); | ||
163 | endwin(); | ||
164 | exit(1); | ||
165 | } | ||
166 | if (fread(buf, len, 1, fd) != 1){ | ||
167 | fprintf(stderr, "fread() failed.\n"); | ||
168 | free(buf); | ||
169 | fclose(fd); | ||
170 | endwin(); | ||
171 | exit(1); | ||
172 | } | ||
173 | Messenger_load(m, buf, len); | ||
174 | } | ||
175 | else { | ||
176 | len = Messenger_size(m); | 163 | len = Messenger_size(m); |
177 | buf = malloc(len); | 164 | buf = malloc(len); |
178 | if (buf == NULL) { | 165 | if (buf == NULL) { |
179 | fprintf(stderr, "malloc() failed.\n"); | 166 | return 1; |
180 | endwin(); | ||
181 | exit(1); | ||
182 | } | 167 | } |
183 | Messenger_save(m, buf); | 168 | Messenger_save(m, buf); |
184 | 169 | ||
185 | fd = fopen(path, "w"); | 170 | fd = fopen(path, "w"); |
186 | if (fd == NULL) { | 171 | if (fd == NULL) { |
187 | fprintf(stderr, "fopen() failed.\n"); | 172 | free(buf); |
188 | free(buf); | 173 | return 2; |
189 | endwin(); | ||
190 | exit(1); | ||
191 | } | 174 | } |
192 | 175 | ||
193 | if (fwrite(buf, len, 1, fd) != 1){ | 176 | if (fwrite(buf, len, 1, fd) != 1) { |
194 | fprintf(stderr, "fwrite() failed.\n"); | 177 | free(buf); |
195 | free(buf); | 178 | fclose(fd); |
196 | fclose(fd); | 179 | return 3; |
197 | endwin(); | 180 | } |
198 | exit(1); | 181 | |
182 | free(buf); | ||
183 | fclose(fd); | ||
184 | return 0; | ||
185 | } | ||
186 | |||
187 | static void load_data(Messenger *m, char *path) | ||
188 | { | ||
189 | FILE *fd; | ||
190 | size_t len; | ||
191 | uint8_t *buf; | ||
192 | |||
193 | if ((fd = fopen(path, "r")) != NULL) { | ||
194 | fseek(fd, 0, SEEK_END); | ||
195 | len = ftell(fd); | ||
196 | fseek(fd, 0, SEEK_SET); | ||
197 | |||
198 | buf = malloc(len); | ||
199 | if (buf == NULL) { | ||
200 | fprintf(stderr, "malloc() failed.\n"); | ||
201 | fclose(fd); | ||
202 | endwin(); | ||
203 | exit(1); | ||
204 | } | ||
205 | if (fread(buf, len, 1, fd) != 1) { | ||
206 | fprintf(stderr, "fread() failed.\n"); | ||
207 | free(buf); | ||
208 | fclose(fd); | ||
209 | endwin(); | ||
210 | exit(1); | ||
211 | } | ||
212 | Messenger_load(m, buf, len); | ||
213 | |||
214 | uint32_t i; | ||
215 | for (i = 0; i < m->numfriends; i++) { | ||
216 | on_friendadded(m, i); | ||
217 | } | ||
218 | |||
219 | free(buf); | ||
220 | fclose(fd); | ||
221 | } else { | ||
222 | int st; | ||
223 | if ((st = store_data(m, path)) != 0) { | ||
224 | fprintf(stderr, "Store messenger failed with return code: %d\n", st); | ||
225 | endwin(); | ||
226 | exit(1); | ||
227 | } | ||
199 | } | 228 | } |
200 | } | ||
201 | free(buf); | ||
202 | fclose(fd); | ||
203 | } | 229 | } |
204 | 230 | ||
205 | int main(int argc, char *argv[]) | 231 | int main(int argc, char *argv[]) |
206 | { | 232 | { |
207 | char *user_config_dir = get_user_config_dir(); | 233 | char *user_config_dir = get_user_config_dir(); |
208 | char *DATA_FILE = NULL; | ||
209 | int config_err = 0; | 234 | int config_err = 0; |
210 | 235 | ||
211 | int f_loadfromfile = 1; | 236 | int f_loadfromfile = 1; |
@@ -246,7 +271,6 @@ int main(int argc, char *argv[]) | |||
246 | 271 | ||
247 | if(f_loadfromfile) | 272 | if(f_loadfromfile) |
248 | load_data(m, DATA_FILE); | 273 | load_data(m, DATA_FILE); |
249 | free(DATA_FILE); | ||
250 | 274 | ||
251 | if (f_flag == -1) { | 275 | if (f_flag == -1) { |
252 | attron(COLOR_PAIR(3) | A_BOLD); | 276 | attron(COLOR_PAIR(3) | A_BOLD); |
@@ -268,6 +292,8 @@ int main(int argc, char *argv[]) | |||
268 | /* Draw */ | 292 | /* Draw */ |
269 | draw_active_window(m); | 293 | draw_active_window(m); |
270 | } | 294 | } |
295 | |||
271 | cleanupMessenger(m); | 296 | cleanupMessenger(m); |
297 | free(DATA_FILE); | ||
272 | return 0; | 298 | return 0; |
273 | } | 299 | } |
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 @@ | |||
16 | uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX | 16 | uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX |
17 | uint8_t num_requests=0; // XXX | 17 | uint8_t num_requests=0; // XXX |
18 | 18 | ||
19 | static friendAddedFn *on_friendadded; | 19 | static friendAddedFn *on_friendadded_cb; |
20 | static char prompt_buf[MAX_STR_SIZE] = {0}; | 20 | static char prompt_buf[MAX_STR_SIZE] = {0}; |
21 | static int prompt_buf_pos = 0; | 21 | static int prompt_buf_pos = 0; |
22 | 22 | ||
@@ -88,7 +88,7 @@ void cmd_accept(ToxWindow *self, Messenger *m, char **args) | |||
88 | wprintw(self->window, "Failed to add friend.\n"); | 88 | wprintw(self->window, "Failed to add friend.\n"); |
89 | else { | 89 | else { |
90 | wprintw(self->window, "Friend accepted as: %d.\n", num); | 90 | wprintw(self->window, "Friend accepted as: %d.\n", num); |
91 | on_friendadded(m, num); | 91 | on_friendadded_cb(m, num); |
92 | } | 92 | } |
93 | } | 93 | } |
94 | 94 | ||
@@ -152,7 +152,7 @@ void cmd_add(ToxWindow *self, Messenger *m, char **args) | |||
152 | break; | 152 | break; |
153 | default: | 153 | default: |
154 | wprintw(self->window, "Friend added as %d.\n", num); | 154 | wprintw(self->window, "Friend added as %d.\n", num); |
155 | on_friendadded(m, num); | 155 | on_friendadded_cb(m, num); |
156 | break; | 156 | break; |
157 | } | 157 | } |
158 | } | 158 | } |
@@ -436,7 +436,7 @@ static void prompt_onInit(ToxWindow *self, Messenger *m) | |||
436 | 436 | ||
437 | ToxWindow new_prompt(friendAddedFn *f) | 437 | ToxWindow new_prompt(friendAddedFn *f) |
438 | { | 438 | { |
439 | on_friendadded = f; | 439 | on_friendadded_cb = f; |
440 | ToxWindow ret; | 440 | ToxWindow ret; |
441 | memset(&ret, 0, sizeof(ret)); | 441 | memset(&ret, 0, sizeof(ret)); |
442 | ret.onKey = &prompt_onKey; | 442 | 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 @@ | |||
3 | #include "dhtstatus.h" | 3 | #include "dhtstatus.h" |
4 | #include "windows.h" | 4 | #include "windows.h" |
5 | 5 | ||
6 | extern char *DATA_FILE; | ||
7 | extern int store_data(Messenger *m, char *path); | ||
8 | |||
6 | /* Holds status of chat windows */ | 9 | /* Holds status of chat windows */ |
7 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; | 10 | char WINDOW_STATUS[MAX_WINDOW_SLOTS]; |
8 | 11 | ||
@@ -74,6 +77,9 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l | |||
74 | void on_friendadded(Messenger *m, int friendnumber) | 77 | void on_friendadded(Messenger *m, int friendnumber) |
75 | { | 78 | { |
76 | friendlist_onFriendAdded(m, friendnumber); | 79 | friendlist_onFriendAdded(m, friendnumber); |
80 | if (store_data(m, DATA_FILE) != 0) { | ||
81 | wprintw(prompt->window, "\nCould not store Messenger data\n"); | ||
82 | } | ||
77 | } | 83 | } |
78 | /* CALLBACKS END */ | 84 | /* CALLBACKS END */ |
79 | 85 | ||
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 | |||
47 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 47 | void on_action(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); |
48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 48 | void on_nickchange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); |
49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); | 49 | void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata); |
50 | void on_friendadded(Messenger *m, int friendnumber); | ||
50 | void init_window_status(); | 51 | void init_window_status(); |
51 | ToxWindow * init_windows(); | 52 | ToxWindow * init_windows(); |
52 | void draw_active_window(Messenger * m); | 53 | void draw_active_window(Messenger * m); |