1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-23 15:23:26 +01:00

Merge pull request #15 from JFreegman/master

implemented friend deletion
This commit is contained in:
Sean Qureshi 2013-08-31 18:27:31 -07:00
commit 5b9c5d9c0c
3 changed files with 120 additions and 45 deletions

View File

@ -119,7 +119,6 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
wattroff(ctx->history, COLOR_PAIR(2));
status[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
@ -213,10 +212,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
bool close_win = false;
if (line[0] == '/')
execute(self, ctx, m, line);
else {
if (line[0] == '/') {
if (close_win = !strncmp(line, "/close", strlen("/close"))) {
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self);
disable_chatwin(f_num);
} else {
execute(self, ctx, m, line);
}
} else {
/* make sure the string has at least non-space character */
if (!string_is_empty(line)) {
uint8_t selfname[TOX_MAX_NAME_LENGTH];
@ -238,8 +245,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
}
}
ctx->line[0] = L'\0';
ctx->pos = 0;
if (close_win)
free(ctx);
else {
ctx->line[0] = L'\0';
ctx->pos = 0;
}
free(line);
}
}
@ -369,13 +381,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
wprintw(ctx->history, "%s\n", id);
}
else if (strcmp(cmd, "/close") == 0) {
int f_num = ctx->friendnum;
delwin(ctx->linewin);
del_window(self);
disable_chatwin(f_num);
}
else
wprintw(ctx->history, "Invalid command.\n");
}
@ -440,7 +445,7 @@ ToxWindow new_chat(Tox *m, int friendnum)
snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
ChatContext *x = calloc(1, sizeof(ChatContext));
ret.x = x;
x->friendnum = friendnum;
ret.x = (void *) x;
return ret;
}

View File

@ -9,17 +9,22 @@
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <tox/tox.h>
#include "friendlist.h"
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
typedef struct {
uint8_t name[TOX_MAX_NAME_LENGTH];
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH];
int num;
int chatwin;
bool active;
} friend_t;
static friend_t friends[MAX_FRIENDS_NUM];
@ -33,7 +38,7 @@ void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16
return;
if (friends[num].chatwin == -1) {
friends[num].chatwin = add_window(m, new_chat(m, num));
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
}
}
@ -57,33 +62,97 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t
int friendlist_onFriendAdded(Tox *m, int num)
{
if (num_friends == MAX_FRIENDS_NUM)
if (num_friends < 0 || num_friends >= MAX_FRIENDS_NUM)
return -1;
friends[num_friends].num = num;
tox_getname(m, num, friends[num_friends].name);
strcpy((char *) friends[num_friends].name, "unknown");
strcpy((char *) friends[num_friends].status, "unknown");
friends[num_friends++].chatwin = -1;
return 0;
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");
strcpy((char *) friends[i].status, "Offline");
if (i == num_friends)
++num_friends;
return 0;
}
}
return -1;
}
static void select_friend(wint_t key)
{
if (num_friends < 1)
return;
int n = num_selected;
int f_inf = num_selected;
if (key == KEY_UP) {
while (true) {
if (--n < 0) n = num_friends-1;
if (friends[n].active) {
num_selected = n;
return;
}
if (n == f_inf) {
endwin();
exit(2);
}
}
} else if (key == KEY_DOWN) {
while (true) {
n = (n + 1) % num_friends;
if (friends[n].active) {
num_selected = n;
return;
}
if (n == f_inf) {
endwin();
exit(2);
}
}
}
}
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;
for (i = num_friends; i > 0; --i) {
if (friends[i-1].active)
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) {
if (--num_selected < 0)
num_selected = num_friends - 1;
} else if (key == KEY_DOWN) {
if (num_friends != 0)
num_selected = (num_selected + 1) % num_friends;
if (key == KEY_UP || key == KEY_DOWN) {
select_friend(key);
} else if (key == '\n') {
/* Jump to chat window if already open */
if (friends[num_selected].chatwin != -1) {
set_active_window(friends[num_selected].chatwin);
} else {
friends[num_selected].chatwin = add_window(m, new_chat(m, num_selected));
friends[num_selected].chatwin = add_window(m, new_chat(m, friends[num_selected].num));
}
}
} else if (key == 0x107 || key == 0x8 || key == 0x7f)
delete_friend(m, self, num_selected, key);
}
static void friendlist_onDraw(ToxWindow *self, Tox *m)
@ -95,25 +164,29 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
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, enter)\n");
wprintw(self->window, " * Open chat with up/down keys and enter. ");
wprintw(self->window, "Delete friends with the backspace key\n\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD);
}
wprintw(self->window, "\n");
int i;
for (i = 0; i < num_friends; ++i) {
if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
if (friends[i].active) {
if (i == num_selected)
wattron(self->window, COLOR_PAIR(3));
wprintw(self->window, " [#%d] ", friends[i].num);
wprintw(self->window, " > ");
if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
if (i == num_selected)
wattroff(self->window, COLOR_PAIR(3));
attron(A_BOLD);
wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD);
attron(A_BOLD);
wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD);
wprintw(self->window, "(%s)\n", friends[i].status);
wprintw(self->window, "(%s)\n", friends[i].status);
}
}
wrefresh(self->window);

View File

@ -120,13 +120,10 @@ int add_window(Tox *m, ToxWindow w)
void del_window(ToxWindow *w)
{
active_window = windows; // Go to prompt screen
delwin(w->window);
if (w->x)
free(w->x);
w->window = NULL;
memset(w, 0, sizeof(ToxWindow));
clear();
refresh();
}
@ -195,7 +192,7 @@ static void draw_bar()
int i;
for (i = 0; i < (MAX_WINDOWS_NUM); ++i) {
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].window) {
if (windows + i == active_window)
attron(A_BOLD);