1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 02:33:02 +01:00

Merge branch 'master' of https://github.com/rose-m/ProjectTox-Core into rose-m-master

This commit is contained in:
irungentoo 2013-08-15 19:59:43 -04:00
commit 4c529180e4
4 changed files with 81 additions and 48 deletions

72
main.c
View File

@ -25,6 +25,8 @@
#include "prompt.h" #include "prompt.h"
#include "friendlist.h" #include "friendlist.h"
/* Export for use in Callbacks */
char *DATA_FILE = NULL;
void on_window_resize(int sig) void on_window_resize(int sig)
{ {
@ -145,6 +147,43 @@ static void do_tox(Messenger *m, ToxWindow * prompt)
doMessenger(m); doMessenger(m);
} }
/*
* 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;
len = Messenger_size(m);
buf = malloc(len);
if (buf == NULL) {
return 1;
}
Messenger_save(m, buf);
fd = fopen(path, "w");
if (fd == NULL) {
free(buf);
return 2;
}
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) static void load_data(Messenger *m, char *path)
{ {
FILE *fd; FILE *fd;
@ -163,7 +202,7 @@ static void load_data(Messenger *m, char *path)
endwin(); endwin();
exit(1); exit(1);
} }
if (fread(buf, len, 1, fd) != 1){ if (fread(buf, len, 1, fd) != 1) {
fprintf(stderr, "fread() failed.\n"); fprintf(stderr, "fread() failed.\n");
free(buf); free(buf);
fclose(fd); fclose(fd);
@ -171,41 +210,27 @@ static void load_data(Messenger *m, 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"); uint32_t i;
if (fd == NULL) { for (i = 0; i < m->numfriends; i++) {
fprintf(stderr, "fopen() failed.\n"); on_friendadded(m, i);
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(m, path)) != 0) {
fprintf(stderr, "Store messenger failed with return code: %d\n", st);
endwin(); endwin();
exit(1); exit(1);
} }
} }
free(buf);
fclose(fd);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *user_config_dir = get_user_config_dir(); char *user_config_dir = get_user_config_dir();
char *DATA_FILE = NULL;
int config_err = 0; int config_err = 0;
int f_loadfromfile = 1; int f_loadfromfile = 1;
@ -246,7 +271,6 @@ int main(int argc, char *argv[])
if(f_loadfromfile) if(f_loadfromfile)
load_data(m, DATA_FILE); load_data(m, 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);
@ -268,6 +292,8 @@ int main(int argc, char *argv[])
/* Draw */ /* Draw */
draw_active_window(m); draw_active_window(m);
} }
cleanupMessenger(m); cleanupMessenger(m);
free(DATA_FILE);
return 0; return 0;
} }

View File

@ -16,7 +16,7 @@
uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX uint8_t pending_requests[MAX_STR_SIZE][CLIENT_ID_SIZE]; // XXX
uint8_t num_requests=0; // 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 char prompt_buf[MAX_STR_SIZE] = {0};
static int prompt_buf_pos = 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"); wprintw(self->window, "Failed to add friend.\n");
else { else {
wprintw(self->window, "Friend accepted as: %d.\n", num); 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; break;
default: default:
wprintw(self->window, "Friend added as %d.\n", num); wprintw(self->window, "Friend added as %d.\n", num);
on_friendadded(m, num); on_friendadded_cb(m, num);
break; break;
} }
} }
@ -436,7 +436,7 @@ static void prompt_onInit(ToxWindow *self, Messenger *m)
ToxWindow new_prompt(friendAddedFn *f) ToxWindow new_prompt(friendAddedFn *f)
{ {
on_friendadded = f; on_friendadded_cb = f;
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.onKey = &prompt_onKey; ret.onKey = &prompt_onKey;

View File

@ -3,6 +3,9 @@
#include "dhtstatus.h" #include "dhtstatus.h"
#include "windows.h" #include "windows.h"
extern char *DATA_FILE;
extern int store_data(Messenger *m, char *path);
/* Holds status of chat windows */ /* Holds status of chat windows */
char WINDOW_STATUS[MAX_WINDOW_SLOTS]; 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) void on_friendadded(Messenger *m, int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
if (store_data(m, DATA_FILE) != 0) {
wprintw(prompt->window, "\nCould not store Messenger data\n");
}
} }
/* CALLBACKS END */ /* CALLBACKS END */

View File

@ -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_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_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_statuschange(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void* userdata);
void on_friendadded(Messenger *m, int friendnumber);
void init_window_status(); void init_window_status();
ToxWindow * init_windows(); ToxWindow * init_windows();
void draw_active_window(Messenger * m); void draw_active_window(Messenger * m);