1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 02:23:02 +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);
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);
wprintw(ctx->history, "Type \"/join\" to join the chat.\n");
alert_window(self, WINDOW_ALERT_2, true);
@ -477,6 +472,8 @@ ToxWindow new_chat(Tox *m, int friendnum)
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit;

View File

@ -14,7 +14,6 @@
#include "friendlist.h"
extern ToxWindow *prompt;
extern int num_groupchats;
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])
{
if (num_groupchats >= MAX_GROUPCHAT_NUM) {
wprintw(window, "Maximum number of group chats has been reached.\n");
if (num_active_windows() >= MAX_WINDOWS_NUM) {
wattron(window, COLOR_PAIR(RED));
wprintw(window, " * Warning: Too many windows are open.\n");
wattron(window, COLOR_PAIR(RED));
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)
{
if (num < 0 || num >= max_friends_index)
if (num >= max_friends_index)
return;
if (friends[num].chatwin == -1)
if (friends[num].chatwin == -1) {
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)
{
if (num < 0 || num >= max_friends_index)
if (num >= max_friends_index)
return;
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)
{
if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= max_friends_index)
if (len > TOX_MAX_NAME_LENGTH || num >= max_friends_index)
return;
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)
{
if (num < 0 || num >= max_friends_index)
if (num >= max_friends_index)
return;
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)
{
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= max_friends_index)
if (len > TOX_MAX_STATUSMESSAGE_LENGTH || num >= max_friends_index)
return;
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,
uint64_t filesize, uint8_t *filename, uint16_t filename_len)
{
if (num < 0 || num >= max_friends_index)
if (num >= max_friends_index)
return;
if (friends[num].chatwin == -1)
if (friends[num].chatwin == -1) {
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)
{
if (num < 0 || num >= max_friends_index)
if (num >= max_friends_index)
return;
if (friends[num].chatwin == -1)
if (friends[num].chatwin == -1) {
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)
@ -197,9 +239,15 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
/* Jump to chat window if already open */
if (friends[f].chatwin != -1) {
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));
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) {
delete_friend(m, self, f, key);
@ -323,6 +371,8 @@ ToxWindow new_friendlist(void)
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.active = true;
ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw;
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 num_frnd_requests;
extern int num_groupchats;
/* command functions */
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])
{
if (num_groupchats >= MAX_GROUPCHAT_NUM) {
wprintw(window, "Maximum number of group chats has been reached.\n");
if (num_active_windows() >= MAX_WINDOWS_NUM) {
wattron(window, COLOR_PAIR(RED));
wprintw(window, " * Warning: Too many windows are open.\n");
wattron(window, COLOR_PAIR(RED));
return;
}

View File

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

View File

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

View File

@ -19,7 +19,6 @@
#define MAX_WINDOWS_NUM 32
#define MAX_FRIENDS_NUM 100
#define MAX_GROUPCHAT_NUM MAX_WINDOWS_NUM - N_DEFAULT_WINS
#define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32
#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);
char name[TOX_MAX_NAME_LENGTH];
bool active;
bool alert1;
bool alert2;
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_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);
int add_window(Tox *m, ToxWindow w);
void del_window(ToxWindow *w);
void set_active_window(int ch);
int num_active_windows(void);
#endif

View File

@ -13,8 +13,9 @@ extern char *DATA_FILE;
static ToxWindow windows[MAX_WINDOWS_NUM];
static ToxWindow *active_window;
static Tox *m;
static ToxWindow *prompt;
static int num_windows;
extern ToxWindow *prompt;
/* CALLBACKS START */
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;
for (i = 0; i < MAX_WINDOWS_NUM; i++) {
if (windows[i].window)
if (windows[i].active)
continue;
w.window = newwin(LINES - 2, COLS, 0, 0);
@ -246,9 +247,8 @@ void set_active_window(int 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());
if (n_prompt == -1 || add_window(m, new_friendlist()) == -1) {
@ -295,7 +295,7 @@ static void draw_bar(void)
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].window) {
if (windows[i].active) {
if (windows + i == active_window) {
#ifdef URXVT_FIX
attron(A_BOLD | COLOR_PAIR(GREEN));
@ -347,3 +347,16 @@ void draw_active_window(Tox *m)
else if (ch != ERR)
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;
}