2013-07-30 21:47:40 +02:00
|
|
|
/*
|
|
|
|
* Toxic -- Tox Curses Client
|
|
|
|
*/
|
|
|
|
|
2013-08-23 09:50:04 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-30 21:47:40 +02:00
|
|
|
#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>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-24 04:15:15 +02:00
|
|
|
#include <tox/tox.h>
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-13 00:50:43 +02:00
|
|
|
#include "friendlist.h"
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
extern char *DATA_FILE;
|
|
|
|
extern int store_data(Tox *m, char *path);
|
2013-07-30 21:47:40 +02:00
|
|
|
|
|
|
|
typedef struct {
|
2013-08-23 23:03:44 +02:00
|
|
|
uint8_t name[TOX_MAX_NAME_LENGTH];
|
|
|
|
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH];
|
2013-08-16 19:11:09 +02:00
|
|
|
int num;
|
|
|
|
int chatwin;
|
2013-08-28 11:46:09 +02:00
|
|
|
bool active;
|
2013-07-30 21:47:40 +02:00
|
|
|
} 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-07-30 21:47:40 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-06 00:07:08 +02:00
|
|
|
}
|
2013-07-31 20:20:16 +02:00
|
|
|
}
|
|
|
|
|
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-07-30 21:47:40 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
memcpy((char *) &friends[num].name, (char *) str, len);
|
|
|
|
friends[num].name[len] = 0;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 00:27:51 +02:00
|
|
|
void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
|
|
|
|
{
|
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;
|
2013-07-30 21:47:40 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
memcpy((char *) &friends[num].status, (char *) str, len);
|
|
|
|
friends[num].status[len] = 0;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
tox_getname(m, num, friends[i].name);
|
|
|
|
strcpy((char *) friends[i].name, "unknown");
|
2013-08-28 11:53:47 +02:00
|
|
|
strcpy((char *) friends[i].status, "Offline");
|
2013-08-28 11:46:09 +02:00
|
|
|
|
|
|
|
if (i == num_friends)
|
|
|
|
++num_friends;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
static void select_friend(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-28 20:47:47 +02:00
|
|
|
int f_inf = num_selected;
|
2013-08-28 11:46:09 +02:00
|
|
|
|
2013-08-16 19:11:09 +02:00
|
|
|
if (key == KEY_UP) {
|
2013-08-28 11:46:09 +02:00
|
|
|
while (true) {
|
2013-08-28 20:47:47 +02:00
|
|
|
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-28 20:47:47 +02:00
|
|
|
if (n == f_inf) {
|
|
|
|
endwin();
|
|
|
|
exit(2);
|
|
|
|
}
|
2013-08-28 11:46:09 +02:00
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
} else if (key == KEY_DOWN) {
|
2013-08-28 11:46:09 +02:00
|
|
|
while (true) {
|
|
|
|
n = (n + 1) % num_friends;
|
|
|
|
if (friends[n].active) {
|
|
|
|
num_selected = n;
|
|
|
|
return;
|
|
|
|
}
|
2013-08-28 20:47:47 +02:00
|
|
|
if (n == f_inf) {
|
|
|
|
endwin();
|
|
|
|
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(KEY_DOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
|
|
|
|
{
|
|
|
|
if (key == KEY_UP || key == KEY_DOWN) {
|
|
|
|
select_friend(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) {
|
2013-08-17 10:00:19 +02:00
|
|
|
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-05 07:57:29 +02:00
|
|
|
}
|
2013-08-28 11:46:09 +02:00
|
|
|
} else if (key == 0x107 || key == 0x8 || key == 0x7f)
|
|
|
|
delete_friend(m, self, num_selected, key);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
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);
|
2013-08-28 11:46:09 +02:00
|
|
|
wprintw(self->window, " * Open chat with up/down keys and enter. ");
|
2013-08-30 05:14:09 +02:00
|
|
|
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)
|
|
|
|
wattron(self->window, COLOR_PAIR(3));
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-28 11:53:47 +02:00
|
|
|
wprintw(self->window, " > ");
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
if (i == num_selected)
|
|
|
|
wattroff(self->window, COLOR_PAIR(3));
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
attron(A_BOLD);
|
|
|
|
wprintw(self->window, "%s ", friends[i].name);
|
|
|
|
attroff(A_BOLD);
|
2013-08-16 19:11:09 +02:00
|
|
|
|
2013-08-28 11:46:09 +02:00
|
|
|
wprintw(self->window, "(%s)\n", friends[i].status);
|
|
|
|
}
|
2013-08-16 19:11:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
wrefresh(self->window);
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|
|
|
|
|
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-05 07:57:29 +02:00
|
|
|
}
|
|
|
|
|
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-07-30 21:47:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-17 10:00:19 +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;
|
2013-07-30 21:47:40 +02:00
|
|
|
}
|