1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-13 02:13:02 +01:00

add popup alert on friend delete

This commit is contained in:
Jfreegman 2014-03-08 23:57:21 -05:00
parent 33e16fe870
commit fe0641e981
5 changed files with 73 additions and 7 deletions

View File

@ -44,6 +44,8 @@ static int num_friends = 0;
ToxicFriend friends[MAX_FRIENDS_NUM];
static int friendlist_index[MAX_FRIENDS_NUM] = {0};
static PendingDel pendingdelete;
#define S_WEIGHT 100
static int index_name_cmp(const void *n1, const void *n2)
@ -230,7 +232,7 @@ static void select_friend(ToxWindow *self, Tox *m, wint_t key)
}
}
static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
static void delete_friend(Tox *m, int f_num)
{
tox_del_friend(m, f_num);
memset(&friends[f_num], 0, sizeof(ToxicFriend));
@ -253,6 +255,48 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
store_data(m, DATA_FILE);
}
/* activates delete friend popup */
static void del_friend_activate(ToxWindow *self, Tox *m, int f_num)
{
int x2, y2;
getmaxyx(self->window, y2, x2);
self->popup = newwin(3, 22 + TOXIC_MAX_NAME_LENGTH, 8, 8);
wattron(self->popup, A_BOLD);
box(self->popup, ACS_VLINE, ACS_HLINE);
wattroff(self->popup, A_BOLD);
pendingdelete.active = true;
pendingdelete.num = f_num;
}
/* deactivates delete friend popup and deletes friend if instructed */
static void del_friend_deactivate(ToxWindow *self, Tox *m, wint_t key)
{
if (key == 'y')
delete_friend(m, pendingdelete.num);
memset(&pendingdelete, 0, sizeof(PendingDel));
delwin(self->popup);
self->popup = NULL;
clear();
refresh();
}
static void friendlist_onPopup(ToxWindow *self, Tox *m)
{
if (self->popup == NULL)
return;
wmove(self->popup, 1, 1);
wprintw(self->popup, "Delete contact ");
wattron(self->popup, A_BOLD);
wprintw(self->popup, "%s", friends[pendingdelete.num].name);
wattroff(self->popup, A_BOLD);
wprintw(self->popup, "? y/n");
wrefresh(self->popup);
}
static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
{
if (num_friends == 0)
@ -260,6 +304,14 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
int f = friendlist_index[num_selected];
/* lock screen and force decision on deletion popup */
if (pendingdelete.active) {
if (key == 'y' || key == 'n')
del_friend_deactivate(self, m, key);
return;
}
if (key == '\n') {
/* Jump to chat window if already open */
if (friends[f].chatwin != -1) {
@ -276,7 +328,7 @@ static void friendlist_onKey(ToxWindow *self, Tox *m, wint_t key)
alert_window(prompt, WINDOW_ALERT_1, true);
}
} else if (key == KEY_DC) {
delete_friend(m, self, f, key);
del_friend_activate(self, m, f);
} else {
select_friend(self, m, key);
}
@ -306,7 +358,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattroff(self->window, COLOR_PAIR(CYAN));
wattron(self->window, A_BOLD);
wprintw(self->window, " Friends: %d/%d \n\n", tox_get_num_online_friends(m), num_friends);
wprintw(self->window, " Online: %d/%d \n\n", tox_get_num_online_friends(m), num_friends);
wattroff(self->window, A_BOLD);
if ((y2 - FLIST_OFST) <= 0) /* don't allow division by zero */
@ -421,6 +473,7 @@ ToxWindow new_friendlist(void)
ret.active = true;
ret.onKey = &friendlist_onKey;
ret.onPopup = &friendlist_onPopup;
ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit;
ret.onFriendAdded = &friendlist_onFriendAdded;

View File

@ -42,6 +42,11 @@ typedef struct {
struct FileReceiver file_receiver;
} ToxicFriend;
typedef struct {
int num;
bool active;
} PendingDel;
ToxWindow new_friendlist(void);
void disable_chatwin(int f_num);
int get_friendnum(uint8_t *name);

View File

@ -105,7 +105,7 @@ int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n)
return len;
}
/* convert wide characters to multibyte string: string returned must be free'd */
/* convert wide characters to multibyte string: string returned must be freed */
uint8_t *wcs_to_mbs(wchar_t *string)
{
uint8_t *ret = NULL;

View File

@ -88,6 +88,7 @@ typedef struct ChatContext ChatContext;
struct ToxWindow {
void(*onKey)(ToxWindow *, Tox *, wint_t);
void(*onDraw)(ToxWindow *, Tox *);
void(*onPopup)(ToxWindow *, Tox*);
void(*onInit)(ToxWindow *, Tox *);
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
void(*onFriendAdded)(ToxWindow *, Tox *, int, bool);
@ -124,6 +125,7 @@ struct ToxWindow {
PromptBuf *promptbuf;
StatusBar *stb;
WINDOW *popup;
WINDOW *window;
};

View File

@ -387,8 +387,14 @@ void draw_active_window(Tox *m)
wresize(a->window, LINES - 2, COLS);
#endif
a->onDraw(a, m);
wrefresh(a->window);
/* ignore main window if popup is active */
if (a->popup) {
a->onPopup(a, m);
wrefresh(a->popup);
} else {
a->onDraw(a, m);
wrefresh(a->window);
}
/* Handle input */
#ifdef HAVE_WIDECHAR
@ -397,7 +403,7 @@ void draw_active_window(Tox *m)
ch = getch();
#endif
if (ch == T_KEY_NEXT || ch == T_KEY_PREV)
if ((ch == T_KEY_NEXT || ch == T_KEY_PREV) && !a->popup) /* lock window if active popup */
set_next_window((int) ch);
else if (ch != ERR)
a->onKey(a, m, ch);