summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/toxic/main.c116
1 files changed, 71 insertions, 45 deletions
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 40ff5d09..fd3c732f 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -30,6 +30,8 @@ extern void disable_chatwin(int f_num);
30extern int add_req(uint8_t *public_key); // XXX 30extern int add_req(uint8_t *public_key); // XXX
31extern unsigned char *hex_string_to_bin(char hex_string[]); 31extern unsigned char *hex_string_to_bin(char hex_string[]);
32 32
33static int store_data(char*);
34
33/* Holds status of chat windows */ 35/* Holds status of chat windows */
34char WINDOW_STATUS[MAX_WINDOW_SLOTS]; 36char WINDOW_STATUS[MAX_WINDOW_SLOTS];
35 37
@@ -41,6 +43,7 @@ static ToxWindow windows[MAX_WINDOW_SLOTS];
41static ToxWindow* prompt; 43static ToxWindow* prompt;
42 44
43static Messenger *m; 45static Messenger *m;
46static char *DATA_FILE;
44 47
45int w_num; 48int w_num;
46int active_window; 49int active_window;
@@ -105,7 +108,11 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
105 108
106void on_friendadded(int friendnumber) 109void on_friendadded(int friendnumber)
107{ 110{
108 friendlist_onFriendAdded(m, friendnumber); 111 friendlist_onFriendAdded(m, friendnumber);
112 int st;
113 if ((st = store_data(DATA_FILE)) != 0) {
114 wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st);
115 }
109} 116}
110/* CALLBACKS END */ 117/* CALLBACKS END */
111 118
@@ -279,61 +286,79 @@ static void do_tox()
279 doMessenger(m); 286 doMessenger(m);
280} 287}
281 288
282static void load_data(char *path) 289/*
290 * Store Messenger data to path
291 * Return 0 Messenger stored successfully
292 * Return 1 malloc failed
293 * Return 2 fopen failed
294 * Return 3 fwrite failed
295 */
296static int store_data(char *path)
283{ 297{
284 FILE *fd; 298 FILE *fd;
285 size_t len; 299 size_t len;
286 uint8_t *buf; 300 uint8_t *buf;
287
288 if ((fd = fopen(path, "r")) != NULL) {
289 fseek(fd, 0, SEEK_END);
290 len = ftell(fd);
291 fseek(fd, 0, SEEK_SET);
292 301
293 buf = malloc(len);
294 if (buf == NULL) {
295 fprintf(stderr, "malloc() failed.\n");
296 fclose(fd);
297 endwin();
298 exit(1);
299 }
300 if (fread(buf, len, 1, fd) != 1){
301 fprintf(stderr, "fread() failed.\n");
302 free(buf);
303 fclose(fd);
304 endwin();
305 exit(1);
306 }
307 Messenger_load(m, buf, len);
308 }
309 else {
310 len = Messenger_size(m); 302 len = Messenger_size(m);
311 buf = malloc(len); 303 buf = malloc(len);
312 if (buf == NULL) { 304 if (buf == NULL) {
313 fprintf(stderr, "malloc() failed.\n"); 305 return 1;
314 endwin();
315 exit(1);
316 } 306 }
317 Messenger_save(m, buf); 307 Messenger_save(m, buf);
318 308
319 fd = fopen(path, "w"); 309 fd = fopen(path, "w");
320 if (fd == NULL) { 310 if (fd == NULL) {
321 fprintf(stderr, "fopen() failed.\n"); 311 free(buf);
322 free(buf); 312 return 2;
323 endwin();
324 exit(1);
325 } 313 }
326 314
327 if (fwrite(buf, len, 1, fd) != 1){ 315 if (fwrite(buf, len, 1, fd) != 1) {
328 fprintf(stderr, "fwrite() failed.\n"); 316 free(buf);
329 free(buf); 317 fclose(fd);
330 fclose(fd); 318 return 3;
331 endwin(); 319 }
332 exit(1); 320
321 free(buf);
322 fclose(fd);
323
324 return 0;
325}
326
327static void load_data(char *path) {
328 FILE *fd;
329 size_t len;
330 uint8_t *buf;
331
332 if ((fd = fopen(path, "r")) != NULL) {
333 fseek(fd, 0, SEEK_END);
334 len = ftell(fd);
335 fseek(fd, 0, SEEK_SET);
336
337 buf = malloc(len);
338 if (buf == NULL) {
339 fprintf(stderr, "malloc() failed.\n");
340 fclose(fd);
341 endwin();
342 exit(1);
343 }
344 if (fread(buf, len, 1, fd) != 1) {
345 fprintf(stderr, "fread() failed.\n");
346 free(buf);
347 fclose(fd);
348 endwin();
349 exit(1);
350 }
351 Messenger_load(m, buf, len);
352 free(buf);
353 fclose(fd);
354 } else {
355 int st;
356 if ((st = store_data(path)) != 0) {
357 fprintf(stderr, "storing messenger failed with error code: %d", st);
358 endwin();
359 exit(1);
360 }
333 } 361 }
334 }
335 free(buf);
336 fclose(fd);
337} 362}
338 363
339static void draw_bar() 364static void draw_bar()
@@ -419,7 +444,6 @@ int main(int argc, char *argv[])
419 int ch; 444 int ch;
420 ToxWindow* a; 445 ToxWindow* a;
421 char *user_config_dir = get_user_config_dir(); 446 char *user_config_dir = get_user_config_dir();
422 char *DATA_FILE;
423 int config_err = create_user_config_dir(user_config_dir); 447 int config_err = create_user_config_dir(user_config_dir);
424 if(config_err) { 448 if(config_err) {
425 DATA_FILE = "data"; 449 DATA_FILE = "data";
@@ -457,7 +481,6 @@ int main(int argc, char *argv[])
457 481
458 if(f_loadfromfile) 482 if(f_loadfromfile)
459 load_data(DATA_FILE); 483 load_data(DATA_FILE);
460 free(DATA_FILE);
461 484
462 if (f_flag == -1) { 485 if (f_flag == -1) {
463 attron(COLOR_PAIR(3) | A_BOLD); 486 attron(COLOR_PAIR(3) | A_BOLD);
@@ -490,6 +513,9 @@ int main(int argc, char *argv[])
490 else if (ch != ERR) 513 else if (ch != ERR)
491 a->onKey(a, m, ch); 514 a->onKey(a, m, ch);
492 } 515 }
516
493 cleanupMessenger(m); 517 cleanupMessenger(m);
518 free(DATA_FILE);
519
494 return 0; 520 return 0;
495} 521}