1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 16:03:27 +01:00

store messenger when friends are added

This commit is contained in:
Michael Rose 2013-08-13 22:07:46 +02:00
parent 2857e6af8f
commit 1158930641

75
main.c
View File

@ -30,6 +30,8 @@ extern void disable_chatwin(int f_num);
extern int add_req(uint8_t *public_key); // XXX extern int add_req(uint8_t *public_key); // XXX
extern unsigned char *hex_string_to_bin(char hex_string[]); extern unsigned char *hex_string_to_bin(char hex_string[]);
static int store_data(char*);
/* Holds status of chat windows */ /* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; char WINDOW_STATUS[MAX_WINDOW_SLOTS];
@ -41,6 +43,7 @@ static ToxWindow windows[MAX_WINDOW_SLOTS];
static ToxWindow* prompt; static ToxWindow* prompt;
static Messenger *m; static Messenger *m;
static char *DATA_FILE;
int w_num; int w_num;
int active_window; int active_window;
@ -106,6 +109,10 @@ void on_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t l
void on_friendadded(int friendnumber) void on_friendadded(int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
int st;
if ((st = store_data(DATA_FILE)) != 0) {
wprintw(prompt->window, "\nCould not store messenger, error code: %d\n", st);
}
} }
/* CALLBACKS END */ /* CALLBACKS END */
@ -279,12 +286,46 @@ static void do_tox()
doMessenger(m); doMessenger(m);
} }
static void load_data(char *path) /*
* Store Messenger data to path
* Return 0 Messenger stored successfully
* Return 1 malloc failed
* Return 2 fopen failed
* Return 3 fwrite failed
*/
static int store_data(char *path)
{ {
FILE *fd; FILE *fd;
size_t len; size_t len;
uint8_t *buf; uint8_t *buf;
len = Messenger_size(m);
buf = malloc(len);
if (buf == NULL) {
return 1;
}
Messenger_save(m, buf);
fd = fopen(path, "w");
if (fd == NULL) {
return 2;
}
if (fwrite(buf, len, 1, fd) != 1) {
return 3;
}
free(buf);
fclose(fd);
return 0;
}
static void load_data(char *path) {
FILE *fd;
size_t len;
uint8_t *buf;
if ((fd = fopen(path, "r")) != NULL) { if ((fd = fopen(path, "r")) != NULL) {
fseek(fd, 0, SEEK_END); fseek(fd, 0, SEEK_END);
len = ftell(fd); len = ftell(fd);
@ -305,35 +346,16 @@ static void load_data(char *path)
exit(1); exit(1);
} }
Messenger_load(m, buf, len); Messenger_load(m, buf, len);
}
else {
len = Messenger_size(m);
buf = malloc(len);
if (buf == NULL) {
fprintf(stderr, "malloc() failed.\n");
endwin();
exit(1);
}
Messenger_save(m, buf);
fd = fopen(path, "w");
if (fd == NULL) {
fprintf(stderr, "fopen() failed.\n");
free(buf);
endwin();
exit(1);
}
if (fwrite(buf, len, 1, fd) != 1){
fprintf(stderr, "fwrite() failed.\n");
free(buf); free(buf);
fclose(fd); fclose(fd);
} else {
int st;
if ((st = store_data(path)) != 0) {
fprintf(stderr, "storing messenger failed with error code: %d", st);
endwin(); endwin();
exit(1); exit(1);
} }
} }
free(buf);
fclose(fd);
} }
static void draw_bar() static void draw_bar()
@ -419,7 +441,6 @@ int main(int argc, char *argv[])
int ch; int ch;
ToxWindow* a; ToxWindow* a;
char *user_config_dir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
char *DATA_FILE;
int config_err = create_user_config_dir(user_config_dir); int config_err = create_user_config_dir(user_config_dir);
if(config_err) { if(config_err) {
DATA_FILE = "data"; DATA_FILE = "data";
@ -457,7 +478,6 @@ int main(int argc, char *argv[])
if(f_loadfromfile) if(f_loadfromfile)
load_data(DATA_FILE); load_data(DATA_FILE);
free(DATA_FILE);
if (f_flag == -1) { if (f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD); attron(COLOR_PAIR(3) | A_BOLD);
@ -490,6 +510,9 @@ int main(int argc, char *argv[])
else if (ch != ERR) else if (ch != ERR)
a->onKey(a, m, ch); a->onKey(a, m, ch);
} }
cleanupMessenger(m); cleanupMessenger(m);
free(DATA_FILE);
return 0; return 0;
} }