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

small optimization

This commit is contained in:
Jfreegman 2014-03-06 19:39:57 -05:00
parent d712d6c898
commit 33e16fe870
6 changed files with 32 additions and 33 deletions

View File

@ -296,7 +296,9 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, "* Error writing to file.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
chat_close_file_receiver(num, filenum);
}
}

View File

@ -90,7 +90,7 @@ 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_active_windows() >= MAX_WINDOWS_NUM) {
if (get_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));

View File

@ -77,7 +77,7 @@ static void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str,
return;
if (friends[num].chatwin == -1) {
if (num_active_windows() < MAX_WINDOWS_NUM) {
if (get_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'};
@ -178,7 +178,7 @@ static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8
return;
if (friends[num].chatwin == -1) {
if (num_active_windows() < MAX_WINDOWS_NUM) {
if (get_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);
@ -203,7 +203,7 @@ static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int num, uint8_t *
return;
if (friends[num].chatwin == -1) {
if (num_active_windows() < MAX_WINDOWS_NUM) {
if (get_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'};
@ -264,7 +264,7 @@ 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 if (num_active_windows() < MAX_WINDOWS_NUM) {
} else if (get_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 {

View File

@ -197,7 +197,7 @@ 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_active_windows() >= MAX_WINDOWS_NUM) {
if (get_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));

View File

@ -234,7 +234,7 @@ 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);
int get_num_active_windows(void);
/* cleans up all chat and groupchat windows (should only be called on shutdown) */
void kill_all_windows(void);

View File

@ -39,12 +39,14 @@ static ToxWindow *active_window;
extern ToxWindow *prompt;
static int num_active_windows;
/* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onFriendRequest != NULL)
windows[i].onFriendRequest(&windows[i], public_key, data, length);
}
@ -54,7 +56,7 @@ void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdat
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onConnectionChange != NULL)
windows[i].onConnectionChange(&windows[i], m, friendnumber, status);
}
@ -64,7 +66,7 @@ void on_typing_change(Tox *m, int friendnumber, int is_typing, void *userdata)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onTypingChange != NULL)
windows[i].onTypingChange(&windows[i], m, friendnumber, is_typing);
}
@ -74,7 +76,7 @@ void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onMessage != NULL)
windows[i].onMessage(&windows[i], m, friendnumber, string, length);
}
@ -84,7 +86,7 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onAction != NULL)
windows[i].onAction(&windows[i], m, friendnumber, string, length);
}
@ -97,7 +99,7 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onNickChange != NULL)
windows[i].onNickChange(&windows[i], m, friendnumber, string, length);
}
@ -110,7 +112,7 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onStatusMessageChange != NULL)
windows[i].onStatusMessageChange(&windows[i], friendnumber, string, length);
}
@ -120,7 +122,7 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], m, friendnumber, status);
}
@ -130,7 +132,7 @@ void on_friendadded(Tox *m, int friendnumber, bool sort)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onFriendAdded != NULL)
windows[i].onFriendAdded(&windows[i], m, friendnumber, sort);
}
@ -144,7 +146,7 @@ void on_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message,
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onGroupMessage != NULL)
windows[i].onGroupMessage(&windows[i], m, groupnumber, peernumber, message, length);
}
@ -155,7 +157,7 @@ void on_groupaction(Tox *m, int groupnumber, int peernumber, uint8_t *action, ui
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onGroupAction != NULL)
windows[i].onGroupAction(&windows[i], m, groupnumber, peernumber, action, length);
}
@ -165,7 +167,7 @@ void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *user
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onGroupInvite != NULL)
windows[i].onGroupInvite(&windows[i], m, friendnumber, group_pub_key);
}
@ -175,7 +177,7 @@ void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t ch
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onGroupNamelistChange != NULL)
windows[i].onGroupNamelistChange(&windows[i], m, groupnumber, peernumber, change);
}
@ -186,7 +188,7 @@ void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onFileSendRequest != NULL)
windows[i].onFileSendRequest(&windows[i], m, friendnumber, filenumber, filesize,
filename, filename_length);
@ -198,7 +200,7 @@ void on_file_control (Tox *m, int friendnumber, uint8_t receive_send, uint8_t fi
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onFileControl != NULL)
windows[i].onFileControl(&windows[i], m, friendnumber, receive_send, filenumber,
control_type, data, length);
@ -210,7 +212,7 @@ void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, u
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
for (i = 0; i < num_active_windows; ++i) {
if (windows[i].onFileData != NULL)
windows[i].onFileData(&windows[i], m, friendnumber, filenumber, data, length);
}
@ -240,6 +242,8 @@ int add_window(Tox *m, ToxWindow w)
windows[i] = w;
w.onInit(&w, m);
++num_active_windows;
return i;
}
@ -256,6 +260,7 @@ void del_window(ToxWindow *w)
clear();
refresh();
--num_active_windows;
}
/* Shows next window when tab or back-tab is pressed */
@ -398,17 +403,9 @@ void draw_active_window(Tox *m)
a->onKey(a, m, ch);
}
int num_active_windows(void)
int get_num_active_windows(void)
{
int count = 0;
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].active)
++count;
}
return count;
return num_active_windows;
}
/* destroys all chat and groupchat windows (should only be called on shutdown) */