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

404 lines
12 KiB
C
Raw Normal View History

/*
* Toxic -- Tox Curses Client
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdint.h>
2013-08-28 20:47:47 +02:00
#include <stdlib.h>
#include <tox/tox.h>
#include "chat.h"
2013-08-13 00:50:43 +02:00
#include "friendlist.h"
2013-11-24 23:12:24 +01:00
#include "misc_tools.h"
2013-08-28 11:46:09 +02:00
extern char *DATA_FILE;
extern ToxWindow *prompt;
static int max_friends_index = 0; /* marks the index of the last friend in friends array */
static int num_friends = 0;
2013-07-31 19:20:03 +02:00
static int num_selected = 0;
2013-11-30 00:52:21 +01:00
ToxicFriend friends[MAX_FRIENDS_NUM];
2013-11-24 23:12:24 +01:00
static int friendlist_index[MAX_FRIENDS_NUM] = {0};
2013-11-26 00:49:31 +01:00
static int index_name_cmp(const void *n1, const void *n2)
2013-11-24 23:12:24 +01:00
{
2013-11-25 00:22:48 +01:00
int res = name_compare(friends[*(int *) n1].name, friends[*(int *) n2].name);
int k = 100;
/* Use weight to make qsort always put online friends before offline */
res = friends[*(int *) n1].online ? (res - k) : (res + k);
res = friends[*(int *) n2].online ? (res + k) : (res - k);
return res;
2013-11-24 23:12:24 +01:00
}
2013-11-25 00:22:48 +01:00
/* sorts friendlist_index first by connection status then alphabetically */
void sort_friendlist_index(void)
{
int i;
2013-11-25 04:31:58 +01:00
int n = 0;
for (i = 0; i < max_friends_index; ++i) {
2013-11-25 00:22:48 +01:00
if (friends[i].active)
2013-11-25 04:31:58 +01:00
friendlist_index[n++] = friends[i].num;
}
2013-11-25 04:34:22 +01:00
qsort(friendlist_index, num_friends, sizeof(int), index_name_cmp);
}
2013-10-16 23:59:56 +02:00
static 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 >= max_friends_index)
2013-08-16 19:11:09 +02:00
return;
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);
}
}
}
2013-10-16 23:59:56 +02:00
static void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{
if (num >= max_friends_index)
return;
2013-09-15 22:38:38 +02:00
friends[num].online = status == 1 ? true : false;
sort_friendlist_index();
}
2013-10-16 23:59:56 +02:00
static 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 >= max_friends_index)
2013-08-16 19:11:09 +02:00
return;
2013-10-19 05:08:37 +02:00
str[TOXIC_MAX_NAME_LENGTH] = '\0';
len = strlen(str) + 1;
2013-09-23 07:22:21 +02:00
memcpy(friends[num].name, str, len);
friends[num].namelength = len;
2013-11-25 00:22:48 +01:00
sort_friendlist_index();
}
2013-10-16 23:59:56 +02:00
static void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{
if (num >= max_friends_index)
return;
friends[num].status = status;
}
2013-10-16 23:59:56 +02:00
static 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 >= max_friends_index)
2013-08-16 19:11:09 +02:00
return;
2013-09-23 07:22:21 +02:00
memcpy(friends[num].statusmsg, str, len);
friends[num].statusmsg_len = len;
}
static void friendlist_onFriendAdded(ToxWindow *self, Tox *m, int num, bool sort)
2013-08-07 00:27:51 +02:00
{
if (max_friends_index < 0 || max_friends_index >= MAX_FRIENDS_NUM)
2013-11-15 20:59:49 +01:00
return;
2013-08-16 19:11:09 +02:00
2013-08-28 11:46:09 +02:00
int i;
for (i = 0; i <= max_friends_index; ++i) {
2013-08-28 11:46:09 +02:00
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_get_name(m, num, friends[i].name);
if (friends[i].namelength == -1 || friends[i].name[0] == '\0') {
2013-11-19 00:52:46 +01:00
strcpy(friends[i].name, (uint8_t *) UNKNOWN_NAME);
friends[i].namelength = strlen(UNKNOWN_NAME) + 1;
2013-10-19 05:08:37 +02:00
} else { /* Enforce toxic's maximum name length */
friends[i].name[TOXIC_MAX_NAME_LENGTH] = '\0';
friends[i].namelength = strlen(friends[i].name) + 1;
}
++num_friends;
2013-08-28 11:46:09 +02:00
if (i == max_friends_index)
++max_friends_index;
if (sort)
sort_friendlist_index();
2013-11-15 20:59:49 +01:00
return;
2013-08-28 11:46:09 +02:00
}
}
}
2013-10-16 23:59:56 +02:00
static void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
uint64_t filesize, uint8_t *filename, uint16_t filename_len)
2013-10-11 06:23:39 +02:00
{
if (num >= max_friends_index)
2013-10-11 06:23:39 +02:00
return;
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);
}
}
2013-10-11 06:23:39 +02:00
}
2013-11-10 03:43:56 +01:00
static void friendlist_onGroupInvite(ToxWindow *self, Tox *m, int num, uint8_t *group_pub_key)
{
if (num >= max_friends_index)
2013-11-10 03:43:56 +01:00
return;
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);
}
}
2013-11-10 03:43:56 +01:00
}
2013-11-27 07:54:22 +01:00
static void select_friend(ToxWindow *self, Tox *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;
2013-08-16 19:11:09 +02:00
} else if (key == KEY_DOWN) {
num_selected = (num_selected + 1) % num_friends;
}
2013-08-28 11:46:09 +02:00
}
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
{
tox_del_friend(m, f_num);
memset(&friends[f_num], 0, sizeof(ToxicFriend));
2013-08-28 11:46:09 +02:00
int i;
for (i = max_friends_index; 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;
}
max_friends_index = i;
--num_friends;
/* make sure num_selected stays within num_friends range */
if (num_friends && num_selected == num_friends)
2013-11-18 04:28:22 +01:00
--num_selected;
sort_friendlist_index();
store_data(m, DATA_FILE);
2013-08-28 11:46:09 +02:00
}
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
{
2013-11-18 04:28:22 +01:00
if (num_friends == 0)
return;
int f = friendlist_index[num_selected];
2013-11-18 04:28:22 +01:00
if (key == '\n') {
2013-08-16 19:11:09 +02:00
/* 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) {
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);
}
2013-11-18 04:28:22 +01:00
} else if (key == 0x107 || key == 0x8 || key == 0x7f) {
delete_friend(m, self, f, key);
2013-11-18 04:28:22 +01:00
} else {
2013-11-27 07:54:22 +01:00
select_friend(self, m, key);
2013-11-18 04:28:22 +01:00
}
}
2013-12-03 09:44:02 +01:00
#define FLIST_OFST 3 /* Accounts for the three lines at top */
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);
int x2, y2;
getmaxyx(self->window, y2, x2);
2013-11-15 23:03:24 +01:00
bool fix_statuses = x2 != self->x; /* true if window x axis has changed */
2013-08-16 19:11:09 +02:00
2013-11-18 04:28:22 +01:00
if (num_friends == 0) {
2013-08-16 19:11:09 +02:00
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
}
2013-12-03 09:44:02 +01:00
if ((y2 - FLIST_OFST) <= 0) /* don't allow division by zero */
2013-12-03 00:34:14 +01:00
return;
/* Determine which portion of friendlist to draw based on current position */
int page = num_selected / (y2 - FLIST_OFST);
int start = (y2 - FLIST_OFST) * page;
int end = y2 - FLIST_OFST + start;
2013-08-16 19:11:09 +02:00
int i;
for (i = start; i < num_friends && i < end; ++i) {
int f = friendlist_index[i];
2013-11-18 04:28:22 +01:00
bool f_selected = false;
if (friends[f].active) {
if (i == num_selected) {
wattron(self->window, A_BOLD);
2013-09-02 04:11:47 +02:00
wprintw(self->window, " > ");
wattroff(self->window, A_BOLD);
f_selected = true;
} else {
2013-09-02 04:11:47 +02:00
wprintw(self->window, " ");
}
2013-09-08 09:18:34 +02:00
if (friends[f].online) {
TOX_USERSTATUS status = friends[f].status;
int colour = WHITE;
2013-09-02 04:11:47 +02:00
2013-11-12 07:50:04 +01:00
switch (status) {
2013-09-02 04:11:47 +02:00
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, "]");
if (f_selected)
wattron(self->window, A_BOLD);
wprintw(self->window, "%s", friends[f].name);
if (f_selected)
wattroff(self->window, A_BOLD);
/* Reset friends[f].statusmsg on window resize */
2013-11-15 23:03:24 +01:00
if (fix_statuses) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_get_status_message(m, friends[f].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
snprintf(friends[f].statusmsg, sizeof(friends[f].statusmsg), "%s", statusmsg);
friends[f].statusmsg_len = tox_get_status_message_size(m, f);
}
/* Truncate note if it doesn't fit on one line */
uint16_t maxlen = x2 - getcurx(self->window) - 4;
if (friends[f].statusmsg_len > maxlen) {
2013-11-15 23:03:24 +01:00
friends[f].statusmsg[maxlen-3] = '\0';
strcat(friends[f].statusmsg, "...");
friends[f].statusmsg[maxlen] = '\0';
friends[f].statusmsg_len = maxlen;
}
wprintw(self->window, " (%s)\n", friends[f].statusmsg);
2013-09-02 04:11:47 +02:00
} else {
wprintw(self->window, "[O]");
if (f_selected)
wattron(self->window, A_BOLD);
wprintw(self->window, "%s\n", friends[f].name);
if (f_selected)
wattroff(self->window, A_BOLD);
2013-09-02 04:11:47 +02:00
}
2013-11-10 03:43:56 +01:00
}
2013-08-16 19:11:09 +02:00
}
self->x = x2;
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
{
}
2013-11-03 01:32:35 +01:00
ToxWindow new_friendlist(void)
2013-08-16 19:11:09 +02:00
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.active = true;
2013-08-16 19:11:09 +02:00
ret.onKey = &friendlist_onKey;
ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit;
2013-11-15 20:59:49 +01:00
ret.onFriendAdded = &friendlist_onFriendAdded;
2013-08-16 19:11:09 +02:00
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-10-11 06:23:39 +02:00
ret.onFileSendRequest = &friendlist_onFileSendRequest;
2013-11-10 03:43:56 +01:00
ret.onGroupInvite = &friendlist_onGroupInvite;
2013-08-16 19:11:09 +02:00
strcpy(ret.name, "friends");
2013-08-16 19:11:09 +02:00
return ret;
}