1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 04:33:01 +01:00
toxic/src/friendlist.c

297 lines
8.0 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>
2013-08-28 11:46:09 +02:00
#include <stdbool.h>
2013-08-28 20:47:47 +02:00
#include <stdlib.h>
#include <tox/tox.h>
2013-08-13 00:50:43 +02:00
#include "friendlist.h"
2013-08-28 11:46:09 +02:00
extern char *DATA_FILE;
extern ToxWindow *prompt;
typedef struct {
2013-08-23 23:03:44 +02:00
uint8_t name[TOX_MAX_NAME_LENGTH];
uint8_t namelength;
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
uint16_t statusmsg_len;
2013-08-16 19:11:09 +02:00
int num;
int chatwin;
bool active;
bool online;
TOX_USERSTATUS status;
} 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;
2013-08-23 23:03:44 +02:00
void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
2013-08-07 00:27:51 +02:00
{
if (num < 0 || num >= num_friends)
2013-08-16 19:11:09 +02:00
return;
2013-09-04 08:05:36 +02:00
if (friends[num].chatwin == -1)
friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num));
}
void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{
if (num < 0 || num >= num_friends)
return;
2013-09-15 22:38:38 +02:00
friends[num].online = status == 1 ? true : false;
}
2013-09-09 07:08:06 +02:00
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
2013-08-07 00:27:51 +02:00
{
if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends)
2013-08-16 19:11:09 +02:00
return;
2013-08-16 19:11:09 +02:00
memcpy((char *) &friends[num].name, (char *) str, len);
friends[num].namelength = len;
}
void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{
if (num < 0 || num >= num_friends)
return;
friends[num].status = status;
}
void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
2013-08-07 00:27:51 +02:00
{
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends)
2013-08-16 19:11:09 +02:00
return;
friends[num].statusmsg_len = len;
2013-09-09 06:56:47 +02:00
memcpy((char *) &friends[num].statusmsg, (char *) str, len);
}
2013-08-23 23:03:44 +02:00
int friendlist_onFriendAdded(Tox *m, int num)
2013-08-07 00:27:51 +02:00
{
2013-08-28 20:47:47 +02:00
if (num_friends < 0 || num_friends >= MAX_FRIENDS_NUM)
2013-08-16 19:11:09 +02:00
return -1;
2013-08-28 11:46:09 +02:00
int i;
for (i = 0; i <= num_friends; ++i) {
if (!friends[i].active) {
friends[i].num = num;
friends[i].active = true;
friends[i].chatwin = -1;
friends[i].online = false;
friends[i].status = TOX_USERSTATUS_NONE;
friends[i].namelength = tox_getname(m, num, friends[i].name);
if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
2013-09-09 12:54:36 +02:00
strcpy((char *) friends[i].name, UNKNOWN_NAME);
friends[i].namelength = strlen(UNKNOWN_NAME) + 1;
}
2013-08-28 11:46:09 +02:00
if (i == num_friends)
++num_friends;
return 0;
}
}
return -1;
}
static void select_friend(Tox *m, wint_t key)
2013-08-07 00:27:51 +02:00
{
2013-08-28 11:46:09 +02:00
if (num_friends < 1)
return;
int n = num_selected;
2013-08-16 19:11:09 +02:00
if (key == KEY_UP) {
while (--n != num_selected) {
if (n < 0) n = num_friends - 1;
2013-08-28 11:46:09 +02:00
if (friends[n].active) {
num_selected = n;
return;
}
}
2013-08-16 19:11:09 +02:00
} else if (key == KEY_DOWN) {
2013-09-06 06:56:55 +02:00
while (++n != num_selected) {
n = n % num_friends;
2013-08-28 11:46:09 +02:00
if (friends[n].active) {
num_selected = n;
return;
}
}
} else return; /* Bad key input */
/* If we reach this something is wrong */
endwin();
tox_kill(m);
2013-09-12 07:33:41 +02:00
fprintf(stderr, "select_friend() failed. Aborting...\n");
exit(EXIT_FAILURE);
2013-08-28 11:46:09 +02:00
}
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
{
tox_delfriend(m, f_num);
memset(&(friends[f_num]), 0, sizeof(friend_t));
int i;
2013-08-28 20:47:47 +02:00
for (i = num_friends; i > 0; --i) {
2013-08-28 11:53:47 +02:00
if (friends[i-1].active)
2013-08-28 11:46:09 +02:00
break;
}
num_friends = i;
store_data(m, DATA_FILE);
select_friend(m, KEY_DOWN);
2013-08-28 11:46:09 +02:00
}
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
{
if (key == KEY_UP || key == KEY_DOWN) {
select_friend(m, key);
2013-08-16 19:11:09 +02:00
} 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, prompt, friends[num_selected].num));
set_active_window(friends[num_selected].chatwin);
}
2013-08-28 11:46:09 +02:00
} else if (key == 0x107 || key == 0x8 || key == 0x7f)
delete_friend(m, self, num_selected, key);
}
2013-08-23 23:03:44 +02:00
static void friendlist_onDraw(ToxWindow *self, Tox *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(CYAN) | A_BOLD);
wprintw(self->window, " Open chat with up/down keys and enter.\n");
wprintw(self->window, " Delete friends with the backspace key.\n\n");
wattroff(self->window, COLOR_PAIR(CYAN) | A_BOLD);
2013-08-16 19:11:09 +02:00
}
int i;
for (i = 0; i < num_friends; ++i) {
2013-08-28 11:53:47 +02:00
if (friends[i].active) {
2013-08-28 11:46:09 +02:00
if (i == num_selected)
2013-09-02 04:11:47 +02:00
wprintw(self->window, " > ");
else
wprintw(self->window, " ");
2013-09-08 09:18:34 +02:00
if (friends[i].online) {
TOX_USERSTATUS status = friends[i].status;
int colour = WHITE;
2013-09-02 04:11:47 +02:00
switch(status) {
case TOX_USERSTATUS_NONE:
colour = GREEN;
2013-09-02 04:11:47 +02:00
break;
case TOX_USERSTATUS_AWAY:
colour = YELLOW;
2013-09-02 04:11:47 +02:00
break;
case TOX_USERSTATUS_BUSY:
colour = RED;
2013-09-02 04:11:47 +02:00
break;
}
wprintw(self->window, "[");
wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "O");
wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "]%s (", friends[i].name);
int x, y;
getmaxyx(self->window, y, x);
/* Reset friends[i].statusmsg on window resize */
if (x != self->x) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_copy_statusmessage(m, friends[i].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
snprintf(friends[i].statusmsg, sizeof(friends[i].statusmsg), "%s", statusmsg);
2013-09-15 22:38:38 +02:00
friends[i].statusmsg_len = tox_get_statusmessage_size(m, self->num);
}
self->x = x;
/* Truncate note if it doesn't fit on one line */
uint16_t maxlen = x - getcurx(self->window) - 2;
if (friends[i].statusmsg_len > maxlen) {
friends[i].statusmsg[maxlen] = '\0';
friends[i].statusmsg_len = maxlen;
}
wprintw(self->window, "%s)\n", friends[i].statusmsg);
2013-09-02 04:11:47 +02:00
} else {
2013-09-09 07:08:06 +02:00
wprintw(self->window, "[O]%s\n", friends[i].name);
2013-09-02 04:11:47 +02:00
}
2013-08-28 11:46:09 +02:00
}
2013-08-16 19:11:09 +02:00
}
2013-08-16 19:11:09 +02:00
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;
}
/* Returns the respective friend number of name. Returns -1 on no match
This should be used instead of tox_getname for retrieving */
int get_friendnum(uint8_t *name)
{
int i;
int len = strlen(name);
for (i = 0; i < num_friends; ++i) {
if (strncmp(friends[i].name, name, len) == 0)
return friends[i].num;
}
return -1;
}
2013-08-23 23:03:44 +02:00
static void friendlist_onInit(ToxWindow *self, Tox *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.onConnectionChange = &friendlist_onConnectionChange;
2013-08-16 19:11:09 +02:00
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
2013-08-16 19:11:09 +02:00
strcpy(ret.name, "friends");
2013-08-16 19:11:09 +02:00
return ret;
}