1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-06 04:07:55 +02:00

Merge pull request #25 from JFreegman/master

Show offline friends names and some cosmetic changes
This commit is contained in:
JFreegman 2013-09-04 15:54:07 -07:00
commit 1df9e09896
4 changed files with 38 additions and 42 deletions

View File

@ -19,6 +19,9 @@
#define CURS_Y_OFFSET 3 #define CURS_Y_OFFSET 3
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
typedef struct { typedef struct {
int friendnum; int friendnum;
wchar_t line[MAX_STR_SIZE]; wchar_t line[MAX_STR_SIZE];
@ -102,7 +105,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(2));
nick[len - 1] = '\0'; nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); snprintf(self->title, sizeof(self->title), "[%s]", nick);
wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick);
} }
@ -270,6 +273,8 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
endwin(); endwin();
store_data(m, DATA_FILE);
tox_kill(m);
exit(0); exit(0);
} }
@ -444,7 +449,7 @@ ToxWindow new_chat(Tox *m, int friendnum)
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
tox_getname(m, friendnum, (uint8_t *) &nick); tox_getname(m, friendnum, (uint8_t *) &nick);
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); snprintf(ret.title, sizeof(ret.title), "[%s]", nick);
ChatContext *x = calloc(1, sizeof(ChatContext)); ChatContext *x = calloc(1, sizeof(ChatContext));
ret.x = x; ret.x = x;

View File

@ -37,9 +37,8 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16
if (num >= num_friends) if (num >= num_friends)
return; return;
if (friends[num].chatwin == -1) { if (friends[num].chatwin == -1)
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
}
} }
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
@ -72,9 +71,9 @@ int friendlist_onFriendAdded(Tox *m, int num)
friends[i].num = num; friends[i].num = num;
friends[i].active = true; friends[i].active = true;
friends[i].chatwin = -1; friends[i].chatwin = -1;
//tox_getname(m, num, friends[i].name);
strcpy((char *) friends[i].name, "unknown"); if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0')
strcpy((char *) friends[i].statusmsg, NOSTATUSMSG); strcpy((char *) friends[i].name, "unknown");
if (i == num_friends) if (i == num_friends)
++num_friends; ++num_friends;
@ -86,39 +85,34 @@ int friendlist_onFriendAdded(Tox *m, int num)
return -1; return -1;
} }
static void select_friend(wint_t key) static void select_friend(Tox *m, wint_t key)
{ {
if (num_friends < 1) if (num_friends < 1)
return; return;
int n = num_selected; int n = num_selected;
int f_inf = num_selected;
if (key == KEY_UP) { if (key == KEY_UP) {
while (true) { while (--n != num_selected) {
if (--n < 0) n = num_friends-1; if (n < 0) n = num_friends - 1;
if (friends[n].active) { if (friends[n].active) {
num_selected = n; num_selected = n;
return; return;
} }
if (n == f_inf) {
endwin();
exit(2);
}
} }
} else if (key == KEY_DOWN) { } else if (key == KEY_DOWN) {
while (true) { while ((n = (n + 1) % num_friends) != num_selected) {
n = (n + 1) % num_friends;
if (friends[n].active) { if (friends[n].active) {
num_selected = n; num_selected = n;
return; return;
} }
if (n == f_inf) {
endwin();
exit(2);
}
} }
} } else return; /* Bad key input */
/* If we reach this something is wrong */
endwin();
tox_kill(m);
exit(2);
} }
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key) static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
@ -137,13 +131,13 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
wprintw(self->window, "\nFailed to store messenger data\n"); wprintw(self->window, "\nFailed to store messenger data\n");
num_friends = i; num_friends = i;
select_friend(KEY_DOWN); select_friend(m, KEY_DOWN);
} }
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key) static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
{ {
if (key == KEY_UP || key == KEY_DOWN) { if (key == KEY_UP || key == KEY_DOWN) {
select_friend(key); select_friend(m, key);
} else if (key == '\n') { } else if (key == '\n') {
/* Jump to chat window if already open */ /* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) { if (friends[num_selected].chatwin != -1) {
@ -164,8 +158,8 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wprintw(self->window, "Empty. Add some friends! :-)\n"); wprintw(self->window, "Empty. Add some friends! :-)\n");
} else { } else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wprintw(self->window, " * Open chat with up/down keys and enter.\n"); wprintw(self->window, " Open chat with up/down keys and enter.\n");
wprintw(self->window, " * Delete friends with the backspace key.\n\n"); wprintw(self->window, " Delete friends with the backspace key.\n\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD); wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
} }
@ -173,46 +167,43 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
for (i = 0; i < num_friends; ++i) { for (i = 0; i < num_friends; ++i) {
if (friends[i].active) { if (friends[i].active) {
bool is_online = tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE;
if (i == num_selected) if (i == num_selected)
wprintw(self->window, " > "); wprintw(self->window, " > ");
else else
wprintw(self->window, " "); wprintw(self->window, " ");
if (is_online) { if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) {
TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num);
int colour; int colour = 7; /* Invalid or other errors default to black */
switch(status) { switch(status) {
case TOX_USERSTATUS_NONE: case TOX_USERSTATUS_NONE:
colour = 1; colour = 1;
break; break;
case TOX_USERSTATUS_AWAY: case TOX_USERSTATUS_AWAY:
colour = 5; colour = 5;
break; break;
case TOX_USERSTATUS_BUSY: case TOX_USERSTATUS_BUSY:
case TOX_USERSTATUS_INVALID:
default:
colour = 3; colour = 3;
break; break;
} }
wprintw(self->window, "[");
wattron(self->window, COLOR_PAIR(colour)); wattron(self->window, COLOR_PAIR(colour));
wprintw(self->window, "%s ", friends[i].name); wprintw(self->window, "O");
wattroff(self->window, COLOR_PAIR(colour)); wattroff(self->window, COLOR_PAIR(colour));
wprintw(self->window, "] %s", friends[i].name);
if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG))) if (friends[i].statusmsg[0])
wprintw(self->window, "(%s)\n", friends[i].statusmsg); wprintw(self->window, " (%s)\n", friends[i].statusmsg);
else
wprintw(self->window, "\n");
} else { } else {
wprintw(self->window, "%s (Offline)\n", friends[i].name); wprintw(self->window, "[O] %s\n", friends[i].name);
} }
} }
} }
wrefresh(self->window); wrefresh(self->window);
} }

View File

@ -4,8 +4,6 @@
#include "toxic_windows.h" #include "toxic_windows.h"
#include "chat.h" #include "chat.h"
#define NOSTATUSMSG "NOSTATUSMSG" /* Friends' default status message */
ToxWindow new_friendlist(); ToxWindow new_friendlist();
int friendlist_onFriendAdded(Tox *m, int num); int friendlist_onFriendAdded(Tox *m, int num);
void disable_chatwin(int f_num); void disable_chatwin(int f_num);

View File

@ -235,6 +235,8 @@ void cmd_connect(ToxWindow *self, Tox *m, int argc, char **argv)
void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv)
{ {
endwin(); endwin();
store_data(m, DATA_FILE);
tox_kill(m);
exit(0); exit(0);
} }