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

fix window related bugs and warn user when max window limit has been reached

This commit is contained in:
Jfreegman 2013-11-30 05:35:25 -05:00
parent 8ff907d719
commit f6a6aecaf5
8 changed files with 101 additions and 39 deletions

View File

@ -246,11 +246,6 @@ static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_
wprintw(ctx->history, "%s has invited you to a group chat.\n", name); wprintw(ctx->history, "%s has invited you to a group chat.\n", name);
if (num_groupchats >= MAX_GROUPCHAT_NUM) {
wprintw(ctx->history, "Maximum number of group chats has been reached. Discarding invite.\n");
return;
}
memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE); memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE);
wprintw(ctx->history, "Type \"/join\" to join the chat.\n"); wprintw(ctx->history, "Type \"/join\" to join the chat.\n");
alert_window(self, WINDOW_ALERT_2, true); alert_window(self, WINDOW_ALERT_2, true);
@ -477,6 +472,8 @@ ToxWindow new_chat(Tox *m, int friendnum)
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &chat_onKey; ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw; ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit; ret.onInit = &chat_onInit;

View File

@ -14,7 +14,6 @@
#include "friendlist.h" #include "friendlist.h"
extern ToxWindow *prompt; extern ToxWindow *prompt;
extern int num_groupchats;
extern ToxicFriend friends[MAX_FRIENDS_NUM]; extern ToxicFriend friends[MAX_FRIENDS_NUM];
@ -72,8 +71,10 @@ void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*a
void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
if (num_groupchats >= MAX_GROUPCHAT_NUM) { if (num_active_windows() >= MAX_WINDOWS_NUM) {
wprintw(window, "Maximum number of group chats has been reached.\n"); wattron(window, COLOR_PAIR(RED));
wprintw(window, " * Warning: Too many windows are open.\n");
wattron(window, COLOR_PAIR(RED));
return; return;
} }

View File

@ -54,16 +54,30 @@ void sort_friendlist_index(void)
static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
{ {
if (num < 0 || num >= max_friends_index) if (num >= max_friends_index)
return; return;
if (friends[num].chatwin == -1) if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); if (num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else {
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
wprintw(prompt->window, "%s: %s\n", nick, str);
wattron(prompt->window, COLOR_PAIR(RED));
wprintw(prompt->window, "* Warning: Too many windows are open.\n");
wattron(prompt->window, COLOR_PAIR(RED));
alert_window(prompt, WINDOW_ALERT_1, true);
}
}
} }
static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status) static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{ {
if (num < 0 || num >= max_friends_index) if (num >= max_friends_index)
return; return;
friends[num].online = status == 1 ? true : false; friends[num].online = status == 1 ? true : false;
@ -72,7 +86,7 @@ static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint
static void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) static void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= max_friends_index) if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index)
return; return;
str[TOXIC_MAX_NAME_LENGTH] = '\0'; str[TOXIC_MAX_NAME_LENGTH] = '\0';
@ -84,7 +98,7 @@ static void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint
static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status) static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{ {
if (num < 0 || num >= max_friends_index) if (num >= max_friends_index)
return; return;
friends[num].status = status; friends[num].status = status;
@ -92,7 +106,7 @@ static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USER
static void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) static void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= max_friends_index) if (len > TOX_MAX_STATUSMESSAGE_LENGTH || num >= max_friends_index)
return; return;
memcpy(friends[num].statusmsg, str, len); memcpy(friends[num].statusmsg, str, len);
@ -137,20 +151,48 @@ static void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num)
static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum, static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
uint64_t filesize, uint8_t *filename, uint16_t filename_len) uint64_t filesize, uint8_t *filename, uint16_t filename_len)
{ {
if (num < 0 || num >= max_friends_index) if (num >= max_friends_index)
return; return;
if (friends[num].chatwin == -1) if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); if (num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else {
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
wattron(prompt->window, COLOR_PAIR(RED));
wprintw(prompt->window, "* File transfer from %s failed: too many windows are open.\n", nick);
wattron(prompt->window, COLOR_PAIR(RED));
alert_window(prompt, WINDOW_ALERT_1, true);
}
}
} }
static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int num, uint8_t *group_pub_key) static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int num, uint8_t *group_pub_key)
{ {
if (num < 0 || num >= max_friends_index) if (num >= max_friends_index)
return; return;
if (friends[num].chatwin == -1) if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); if (num_active_windows() < MAX_WINDOWS_NUM) {
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} else {
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick);
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
wattron(prompt->window, COLOR_PAIR(RED));
wprintw(prompt->window, "* Group chat invite from %s failed: too many windows are open.\n", nick);
wattron(prompt->window, COLOR_PAIR(RED));
alert_window(prompt, WINDOW_ALERT_1, true);
}
}
} }
static void select_friend(ToxWindow *self, Tox *m, wint_t key) static void select_friend(ToxWindow *self, Tox *m, wint_t key)
@ -197,9 +239,15 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (friends[f].chatwin != -1) { if (friends[f].chatwin != -1) {
set_active_window(friends[f].chatwin); set_active_window(friends[f].chatwin);
} else { } else if (num_active_windows() < MAX_WINDOWS_NUM) {
friends[f].chatwin = add_window(m, new_chat(m, friends[f].num)); friends[f].chatwin = add_window(m, new_chat(m, friends[f].num));
set_active_window(friends[f].chatwin); set_active_window(friends[f].chatwin);
} else {
wattron(prompt->window, COLOR_PAIR(RED));
wprintw(prompt->window, "* Warning: Too many windows are open.\n");
wattron(prompt->window, COLOR_PAIR(RED));
alert_window(prompt, WINDOW_ALERT_1, true);
} }
} else if (key == 0x107 || key == 0x8 || key == 0x7f) { } else if (key == 0x107 || key == 0x8 || key == 0x7f) {
delete_friend(m, self, f, key); delete_friend(m, self, f, key);
@ -323,6 +371,8 @@ ToxWindow new_friendlist(void)
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &friendlist_onKey; ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw; ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit; ret.onInit = &friendlist_onInit;

View File

@ -21,8 +21,6 @@ extern ToxicFriend friends[MAX_FRIENDS_NUM];
extern uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE]; extern uint8_t pending_frnd_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
extern uint8_t num_frnd_requests; extern uint8_t num_frnd_requests;
extern int num_groupchats;
/* command functions */ /* command functions */
void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
@ -181,8 +179,10 @@ void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)
void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
{ {
if (num_groupchats >= MAX_GROUPCHAT_NUM) { if (num_active_windows() >= MAX_WINDOWS_NUM) {
wprintw(window, "Maximum number of group chats has been reached.\n"); wattron(window, COLOR_PAIR(RED));
wprintw(window, " * Warning: Too many windows are open.\n");
wattron(window, COLOR_PAIR(RED));
return; return;
} }

View File

@ -18,8 +18,7 @@
extern char *DATA_FILE; extern char *DATA_FILE;
extern int store_data(Tox *m, char *path); extern int store_data(Tox *m, char *path);
static GroupChat groupchats[MAX_GROUPCHAT_NUM]; static GroupChat groupchats[MAX_WINDOWS_NUM];
int num_groupchats = 0;
static int max_groupchat_index = 0; static int max_groupchat_index = 0;
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum) int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
@ -42,8 +41,6 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
if (i == max_groupchat_index) if (i == max_groupchat_index)
++max_groupchat_index; ++max_groupchat_index;
++num_groupchats;
return 0; return 0;
} }
} }
@ -66,7 +63,6 @@ static void close_groupchatwin(Tox *m, int groupnum)
break; break;
} }
--num_groupchats;
max_groupchat_index = i; max_groupchat_index = i;
} }
@ -354,6 +350,8 @@ ToxWindow new_group_chat(Tox *m, int groupnum)
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &groupchat_onKey; ret.onKey = &groupchat_onKey;
ret.onDraw = &groupchat_onDraw; ret.onDraw = &groupchat_onDraw;
ret.onInit = &groupchat_onInit; ret.onInit = &groupchat_onInit;

View File

@ -261,6 +261,8 @@ ToxWindow new_prompt(void)
ToxWindow ret; ToxWindow ret;
memset(&ret, 0, sizeof(ret)); memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &prompt_onKey; ret.onKey = &prompt_onKey;
ret.onDraw = &prompt_onDraw; ret.onDraw = &prompt_onDraw;
ret.onInit = &prompt_onInit; ret.onInit = &prompt_onInit;

View File

@ -19,7 +19,6 @@
#define MAX_WINDOWS_NUM 32 #define MAX_WINDOWS_NUM 32
#define MAX_FRIENDS_NUM 100 #define MAX_FRIENDS_NUM 100
#define MAX_GROUPCHAT_NUM MAX_WINDOWS_NUM - N_DEFAULT_WINS
#define MAX_STR_SIZE 256 #define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32 #define KEY_SIZE_BYTES 32
#define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */ #define TOXIC_MAX_NAME_LENGTH 32 /* Must be <= TOX_MAX_NAME_LENGTH */
@ -75,6 +74,7 @@ struct ToxWindow {
void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t); void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t);
char name[TOX_MAX_NAME_LENGTH]; char name[TOX_MAX_NAME_LENGTH];
bool active;
bool alert1; bool alert1;
bool alert2; bool alert2;
int num; int num;
@ -145,9 +145,10 @@ void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
void on_file_control(Tox *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata); void on_file_control(Tox *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata);
void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata); void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata);
ToxWindow *init_windows(Tox *mToAssign); ToxWindow *init_windows(Tox *m);
void draw_active_window(Tox *m); void draw_active_window(Tox *m);
int add_window(Tox *m, ToxWindow w); int add_window(Tox *m, ToxWindow w);
void del_window(ToxWindow *w); void del_window(ToxWindow *w);
void set_active_window(int ch); void set_active_window(int ch);
int num_active_windows(void);
#endif #endif

View File

@ -13,8 +13,9 @@ extern char *DATA_FILE;
static ToxWindow windows[MAX_WINDOWS_NUM]; static ToxWindow windows[MAX_WINDOWS_NUM];
static ToxWindow *active_window; static ToxWindow *active_window;
static Tox *m; static int num_windows;
static ToxWindow *prompt;
extern ToxWindow *prompt;
/* CALLBACKS START */ /* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
@ -182,7 +183,7 @@ int add_window(Tox *m, ToxWindow w)
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; i++) { for (i = 0; i < MAX_WINDOWS_NUM; i++) {
if (windows[i].window) if (windows[i].active)
continue; continue;
w.window = newwin(LINES - 2, COLS, 0, 0); w.window = newwin(LINES - 2, COLS, 0, 0);
@ -246,9 +247,8 @@ void set_active_window(int index)
active_window = windows + index; active_window = windows + index;
} }
ToxWindow *init_windows(Tox *mToAssign) ToxWindow *init_windows(Tox *m)
{ {
m = mToAssign;
int n_prompt = add_window(m, new_prompt()); int n_prompt = add_window(m, new_prompt());
if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) { if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) {
@ -295,7 +295,7 @@ static void draw_bar(void)
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].window) { if (windows[i].active) {
if (windows + i == active_window) { if (windows + i == active_window) {
#ifdef URXVT_FIX #ifdef URXVT_FIX
attron(A_BOLD | COLOR_PAIR(GREEN)); attron(A_BOLD | COLOR_PAIR(GREEN));
@ -347,3 +347,16 @@ void draw_active_window(Tox *m)
else if (ch != ERR) else if (ch != ERR)
a->onKey(a, m, ch); a->onKey(a, m, ch);
} }
int num_active_windows(void)
{
int count = 0;
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].active)
++count;
}
return count;
}