1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 16:57:45 +02:00
toxic/src/friendlist.c

149 lines
3.6 KiB
C
Raw Normal View History

/*
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include "Messenger.h"
#include "network.h"
2013-08-13 00:50:43 +02:00
#include "friendlist.h"
typedef struct {
2013-08-16 19:11:09 +02:00
uint8_t name[MAX_NAME_LENGTH];
uint8_t status[MAX_STATUSMESSAGE_LENGTH];
int num;
int chatwin;
} friend_t;
static friend_t friends[MAX_FRIENDS_NUM];
static int num_friends = 0;
2013-07-31 19:20:03 +02:00
static int num_selected = 0;
void friendlist_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *str, uint16_t len)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
if (num >= num_friends)
return;
if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, num));
}
}
2013-08-07 00:27:51 +02:00
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
2013-08-16 19:11:09 +02:00
if (len >= MAX_NAME_LENGTH || num >= num_friends)
return;
2013-08-16 19:11:09 +02:00
memcpy((char *) &friends[num].name, (char *) str, len);
friends[num].name[len] = 0;
}
2013-08-07 00:27:51 +02:00
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
2013-08-16 19:11:09 +02:00
if (len >= MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
return;
2013-08-16 19:11:09 +02:00
memcpy((char *) &friends[num].status, (char *) str, len);
friends[num].status[len] = 0;
}
int friendlist_onFriendAdded(Messenger *m, int num)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
if (num_friends == MAX_FRIENDS_NUM)
return -1;
friends[num_friends].num = num;
getname(m, num, friends[num_friends].name);
strcpy((char *) friends[num_friends].name, "unknown");
strcpy((char *) friends[num_friends].status, "unknown");
friends[num_friends++].chatwin = -1;
return 0;
}
static void friendlist_onKey(ToxWindow *self, Messenger *m, wint_t key)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
if (key == KEY_UP) {
if (--num_selected < 0)
num_selected = num_friends - 1;
} else if (key == KEY_DOWN) {
if (num_friends != 0)
num_selected = (num_selected + 1) % num_friends;
} else if (key == '\n') {
/* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) {
set_active_window(friends[num_selected].chatwin);
2013-08-16 19:11:09 +02:00
} else {
friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
}
}
}
2013-08-20 20:47:32 +02:00
static void friendlist_onDraw(ToxWindow *self, Messenger *m)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
curs_set(0);
werase(self->window);
if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n");
} else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, "Open chat with.. (up/down keys, enter)\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
wprintw(self->window, "\n");
int i;
for (i = 0; i < num_friends; ++i) {
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, " [#%d] ", friends[i].num);
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
attron(A_BOLD);
wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD);
wprintw(self->window, "(%s)\n", friends[i].status);
}
wrefresh(self->window);
}
2013-08-07 00:27:51 +02:00
void disable_chatwin(int f_num)
{
2013-08-16 19:11:09 +02:00
friends[f_num].chatwin = -1;
}
static void friendlist_onInit(ToxWindow *self, Messenger *m)
2013-08-07 00:27:51 +02:00
{
}
ToxWindow new_friendlist()
2013-08-16 19:11:09 +02:00
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
strcpy(ret.title, "[friends]");
return ret;
}