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

236 lines
6.1 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 int store_data(Tox *m, char *path);
typedef struct {
2013-08-23 23:03:44 +02:00
uint8_t name[TOX_MAX_NAME_LENGTH];
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
2013-08-16 19:11:09 +02:00
int num;
int chatwin;
2013-09-02 04:11:47 +02:00
bool active;
} 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
{
2013-08-16 19:11:09 +02:00
if (num >= num_friends)
return;
2013-09-04 08:05:36 +02:00
if (friends[num].chatwin == -1)
2013-08-28 11:46:09 +02:00
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
}
2013-08-07 00:27:51 +02:00
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{
2013-08-23 23:03:44 +02:00
if (len >= TOX_MAX_NAME_LENGTH || 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].name[len] = 0;
}
void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
2013-08-07 00:27:51 +02:00
{
2013-08-23 23:03:44 +02:00
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
2013-08-16 19:11:09 +02:00
return;
memcpy((char *) &friends[num].statusmsg, (char *) str, len);
friends[num].statusmsg[len] = 0;
}
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;
if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0')
strcpy((char *) friends[i].name, "unknown");
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) {
while ((n = (n + 1) % num_friends) != num_selected) {
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);
exit(2);
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;
}
if (store_data(m, DATA_FILE))
wprintw(self->window, "\nFailed to store messenger data\n");
num_friends = i;
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 {
2013-08-28 11:46:09 +02:00
friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num));
}
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(2) | 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");
2013-08-16 19:11:09 +02:00
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
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, " ");
if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) {
2013-09-02 11:15:29 +02:00
TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num);
int colour = 7; /* Invalid or other errors default to black */
2013-09-02 04:11:47 +02:00
switch(status) {
case TOX_USERSTATUS_NONE:
colour = 1;
break;
case TOX_USERSTATUS_AWAY:
colour = 5;
break;
case TOX_USERSTATUS_BUSY:
colour = 3;
break;
}
wprintw(self->window, "[");
2013-09-02 04:11:47 +02:00
wattron(self->window, COLOR_PAIR(colour));
wprintw(self->window, "O");
2013-09-02 04:11:47 +02:00
wattroff(self->window, COLOR_PAIR(colour));
2013-09-04 04:05:36 +02:00
wprintw(self->window, "] %s", friends[i].name);
2013-09-02 11:15:29 +02:00
if (friends[i].statusmsg[0])
2013-09-04 04:05:36 +02:00
wprintw(self->window, " (%s)\n", friends[i].statusmsg);
else
wprintw(self->window, "\n");
2013-09-02 04:11:47 +02:00
} else {
2013-09-04 04:05:36 +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;
}
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.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
2013-08-16 19:11:09 +02:00
strcpy(ret.title, "[friends]");
return ret;
}