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

Merge pull request #36 from JFreegman/master

Client specific max name length / status messages now dynamically resize
This commit is contained in:
JFreegman 2013-09-13 17:15:06 -07:00
commit 657b65dd19
6 changed files with 86 additions and 46 deletions

View File

@ -44,7 +44,7 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
if (self->friendnum != num)
return;
ChatContext *ctx = (ChatContext *) self->x;
ChatContext *ctx = (ChatContext *) self->chatwin;
struct tm *timeinfo = get_time();
@ -68,7 +68,7 @@ void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
if (self->friendnum != num)
return;
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
statusbar->is_online = status == 1 ? true : false;
}
@ -77,7 +77,7 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin
if (self->friendnum != num)
return;
ChatContext *ctx = (ChatContext *) self->x;
ChatContext *ctx = (ChatContext *) self->chatwin;
struct tm *timeinfo = get_time();
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
@ -108,7 +108,7 @@ static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS
if (self->friendnum != num)
return;
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
statusbar->status = status;
}
@ -117,7 +117,7 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
if (self->friendnum != num)
return;
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
statusbar->statusmsg_len = len;
snprintf(statusbar->statusmsg, len, "%s", status);
}
@ -316,8 +316,14 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox
return;
}
nick++;
tox_setname(m, nick, strlen(nick) + 1);
int len = strlen(++nick);
if (len > TOXIC_MAX_NAME_LENGTH) {
nick[TOXIC_MAX_NAME_LENGTH] = L'\0';
len = TOXIC_MAX_NAME_LENGTH;
}
tox_setname(m, nick, len+1);
prompt_update_nick(self->prompt, nick);
wprintw(ctx->history, "Nickname set to: %s\n", nick);
}
@ -343,8 +349,8 @@ static void execute(ToxWindow *self, ChatContext *ctx, StatusBar *statusbar, Tox
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
{
ChatContext *ctx = (ChatContext *) self->x;
StatusBar *statusbar = (StatusBar *) self->s;
ChatContext *ctx = (ChatContext *) self->chatwin;
StatusBar *statusbar = (StatusBar *) self->stb;
struct tm *timeinfo = get_time();
@ -436,10 +442,10 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
int x, y;
getmaxyx(self->window, y, x);
ChatContext *ctx = (ChatContext *) self->x;
ChatContext *ctx = (ChatContext *) self->chatwin;
/* Draw status bar */
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
mvwhline(statusbar->topline, 1, 0, '-', x);
wmove(statusbar->topline, 0, 0);
@ -471,7 +477,6 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
wprintw(statusbar->topline, "[%s]", status_text);
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
} else {
wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " %s ", self->name);
@ -479,19 +484,30 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
wprintw(statusbar->topline, "[Offline]");
}
/* Reset statusbar->statusmsg on window resize */
if (x != self->x) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_copy_statusmessage(m, self->friendnum, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->friendnum);
}
self->x = x;
/* Truncate note if it doesn't fit in statusbar */
uint16_t maxlen = x - getcurx(statusbar->topline) - 5;
uint16_t maxlen = x - getcurx(statusbar->topline) - 6;
if (statusbar->statusmsg_len > maxlen) {
statusbar->statusmsg[maxlen] = '\0';
statusbar->statusmsg_len = maxlen;
}
if (statusbar->statusmsg[0]) {
wattron(statusbar->topline, A_BOLD);
wprintw(statusbar->topline, " | %s | ", statusbar->statusmsg);
wattroff(statusbar->topline, A_BOLD);
}
wprintw(statusbar->topline, "\n");
mvwhline(ctx->linewin, 0, 0, '_', x);
wrefresh(self->window);
}
@ -500,9 +516,10 @@ static void chat_onInit(ToxWindow *self, Tox *m)
{
int x, y;
getmaxyx(self->window, y, x);
self->x = x;
/* Init statusbar info */
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
statusbar->status = tox_get_userstatus(m, self->friendnum);
statusbar->is_online = tox_get_friend_connectionstatus(m, self->friendnum) == 1;
@ -512,7 +529,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
statusbar->statusmsg_len = tox_get_statusmessage_size(m, self->friendnum);
/* Init subwindows */
ChatContext *ctx = (ChatContext *) self->x;
ChatContext *ctx = (ChatContext *) self->chatwin;
statusbar->topline = subwin(self->window, 2, x, 0, 0);
ctx->history = subwin(self->window, y-3, x, 0, 0);
scrollok(ctx->history, 1);
@ -541,12 +558,12 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum)
tox_getname(m, friendnum, name);
snprintf(ret.name, sizeof(ret.name), "%s", name);
ChatContext *x = calloc(1, sizeof(ChatContext));
StatusBar *s = calloc(1, sizeof(StatusBar));
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar));
if (s != NULL && x != NULL) {
ret.x = x;
ret.s = s;
if (stb != NULL && chatwin != NULL) {
ret.chatwin = chatwin;
ret.stb = stb;
} else {
endwin();
fprintf(stderr, "calloc() failed. Aborting...\n");

View File

@ -219,11 +219,21 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "]%s (", friends[i].name);
/* Truncate note if it doesn't fit on one line */
int x, y;
getmaxyx(self->window, y, x);
uint16_t maxlen = x - getcurx(self->window) - 2;
/* Reset friends[i].statusmsg on window resize */
if (x != self->x) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_copy_statusmessage(m, friends[i].num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
snprintf(friends[i].statusmsg, sizeof(friends[i].statusmsg), "%s", statusmsg);
friends[i].statusmsg_len = tox_get_statusmessage_size(m, self->friendnum);
}
self->x = x;
/* Truncate note if it doesn't fit on one line */
uint16_t maxlen = x - getcurx(self->window) - 2;
if (friends[i].statusmsg_len > maxlen) {
friends[i].statusmsg[maxlen] = '\0';
friends[i].statusmsg_len = maxlen;

View File

@ -359,7 +359,7 @@ void exit_toxic(Tox *m)
store_data(m, DATA_FILE);
free(DATA_FILE);
free(SRVLIST_FILE);
free(prompt->s);
free(prompt->stb);
tox_kill(m);
endwin();
exit(EXIT_SUCCESS);

View File

@ -57,28 +57,28 @@ static struct {
/* Updates own nick in prompt statusbar */
void prompt_update_nick(ToxWindow *prompt, uint8_t *nick)
{
StatusBar *statusbar = (StatusBar *) prompt->s;
StatusBar *statusbar = (StatusBar *) prompt->stb;
snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick);
}
/* Updates own statusmessage in prompt statusbar */
void prompt_update_statusmessage(ToxWindow *prompt, uint8_t *statusmsg)
{
StatusBar *statusbar = (StatusBar *) prompt->s;
StatusBar *statusbar = (StatusBar *) prompt->stb;
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
}
/* Updates own status in prompt statusbar */
void prompt_update_status(ToxWindow *prompt, TOX_USERSTATUS status)
{
StatusBar *statusbar = (StatusBar *) prompt->s;
StatusBar *statusbar = (StatusBar *) prompt->stb;
statusbar->status = status;
}
/* Updates own connection status in prompt statusbar */
void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected)
{
StatusBar *statusbar = (StatusBar *) prompt->s;
StatusBar *statusbar = (StatusBar *) prompt->stb;
statusbar->is_online = is_connected;
}
@ -385,10 +385,20 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
return;
}
if (nick[0] == '\"')
nick[strlen(++nick)-1] = L'\0';
int len = strlen(nick);
tox_setname(m, nick, strlen(nick) + 1);
if (nick[0] == '\"') {
++nick;
len -= 2;
nick[len] = L'\0';
}
if (len > TOXIC_MAX_NAME_LENGTH) {
nick[TOXIC_MAX_NAME_LENGTH] = L'\0';
len = TOXIC_MAX_NAME_LENGTH;
}
tox_setname(m, nick, len+1);
prompt_update_nick(self, nick);
store_data(m, DATA_FILE);
@ -604,7 +614,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
--y;
}
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
werase(statusbar->topline);
@ -668,7 +678,7 @@ void prompt_init_statusbar(ToxWindow *self, Tox *m)
getmaxyx(self->window, y, x);
/* Init statusbar info */
StatusBar *statusbar = (StatusBar *) self->s;
StatusBar *statusbar = (StatusBar *) self->stb;
statusbar->status = TOX_USERSTATUS_NONE;
statusbar->is_online = false;
@ -695,10 +705,10 @@ ToxWindow new_prompt()
ret.onFriendRequest = &prompt_onFriendRequest;
strcpy(ret.name, "prompt");
StatusBar *s = calloc(1, sizeof(StatusBar));
StatusBar *stb = calloc(1, sizeof(StatusBar));
if (s != NULL)
ret.s = s;
if (stb != NULL)
ret.stb = stb;
else {
endwin();
fprintf(stderr, "calloc() failed. Aborting...\n");

View File

@ -16,12 +16,9 @@
#define MAX_FRIENDS_NUM 100
#define MAX_STR_SIZE 256
#define KEY_SIZE_BYTES 32
/* number of permanent default windows */
#define N_DEFAULT_WINS 3
#define TOXIC_MAX_NAME_LENGTH 30 /* Not to be confused with TOX_MAX_NAME_LENGTH */
#define N_DEFAULT_WINS 3 /* number of permanent default windows */
#define UNKNOWN_NAME "Unknown"
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
@ -55,9 +52,10 @@ struct ToxWindow_ {
char name[TOX_MAX_NAME_LENGTH];
int friendnum;
int x;
void *x;
void *s;
void *chatwin;
void *stb;
void *prompt;
bool blink;

View File

@ -81,6 +81,11 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void
void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{
if (length >= TOXIC_MAX_NAME_LENGTH) { /* length includes null byte */
string[TOXIC_MAX_NAME_LENGTH] = L'\0';
length = TOXIC_MAX_NAME_LENGTH+1;
}
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {