diff options
Diffstat (limited to '')
-rw-r--r-- | src/ui.c | 58 |
1 files changed, 43 insertions, 15 deletions
@@ -9,6 +9,7 @@ #include <locale.h> #include <string.h> #include <assert.h> +#include <signal.h> #define DC_SIMPLEPRINT(w, c, f, ...) do { wattron(w, COLOR_PAIR(c)); wprintw(w, f __VA_OPT__(,) __VA_ARGS__); wrefresh(w); } while (0) /* link with -lncursesw and -lformw */ int dc_ui_print_message (WINDOW * textwin, struct dc_message * msg2do) { @@ -16,7 +17,8 @@ int dc_ui_print_message (WINDOW * textwin, struct dc_message * msg2do) { struct tm timestruct; localtime_r(&msg2do->time, ×truct); strftime(timestring, 64, DC_I18N_MSGTIMEF, ×truct); /* recimo, da je 23 znakov */ - DC_SIMPLEPRINT(textwin, 3, "%023.23s %08.8s: %s\n", timestring, msg2do->username, msg2do->content); + DC_SIMPLEPRINT(textwin, 3, "%018.18s %08.8s: %s\n", timestring, msg2do->username, msg2do->content); + msg2do->status = 1; return 1; } int dc_ui_processline (struct dc_thread_control * t, char * l, WINDOW * textwin) { @@ -69,11 +71,10 @@ int dc_ui_processline (struct dc_thread_control * t, char * l, WINDOW * textwin) DC_CUE(c, c->guilds_lock); break; } - for (i = 0; i < c->guilds[j]->channels[k]->messages_sizeof; i++) + for (i = c->guilds[j]->channels[k]->messages_sizeof-1; i >= 0; i--) dc_ui_print_message(textwin, c->guilds[j]->channels[k]->messages[i]); c->joinedchannel = c->guilds[j]->channels[k]; DC_CUE(c, c->guilds_lock); - break; case 'q': case 'Q': @@ -90,9 +91,35 @@ int dc_ui_processline (struct dc_thread_control * t, char * l, WINDOW * textwin) } t->power_api = atoi(strchr(l, ' ')+1); DC_SIMPLEPRINT(textwin, 4, "t->power_api = %d\n", atoi(strchr(l, ' ')+1)); + break; default: DC_SIMPLEPRINT(textwin, 1, DC_I18N_UI_CNF "\n"); } + else { /* send the message, it's not a command */ + if (!c->joinedchannel) { + DC_SIMPLEPRINT(textwin, 1, "!c->joinedchannel - %s\n", DC_I18N_UI_NOT_JOINED); + return -1; + } + if (!strlen(l)) { + DC_SIMPLEPRINT(textwin, 1, "!strlen(l) - %s\n", DC_I18N_UI_EMPTYMSG); + return -2; + } + if (time(NULL) - c->last_sent_message <= c->joinedchannel->slowmode) { + DC_SIMPLEPRINT(textwin, 1, DC_I18N_UI_SLOWMODE "\n", c->joinedchannel->slowmode, c->joinedchannel->slowmode-(time(NULL)-c->last_sent_message)); + return -3; + } + c->last_sent_message = time(NULL); /* because the other thread may not update counter before the next message is sent */ + /* raise(SIGINT); */ /* To continue from here in GDB: "signal 0". */ + DC_CWLE(c, c->sent_messages_lock); + c->sent_messages = realloc(c->sent_messages, sizeof(struct dc_message *)*++c->sent_messages_sizeof); +#define DC_UISM c->sent_messages[c->sent_messages_sizeof-1] /* ui send messaeg */ + DC_UISM = calloc(1, sizeof(struct dc_message)); + DC_UISM->content = malloc(strlen(l)+1); + strcpy(DC_UISM->content, l); + DC_UISM->channel = c->joinedchannel; + DC_CUE(c, c->sent_messages_lock); + /* DO NOT free it */ + } wrefresh(textwin); return 1; } @@ -108,12 +135,13 @@ int dc_ui_thread (struct dc_thread_control * t) { cbreak(); noecho(); nodelay(stdscr, TRUE); - setlocale(LC_ALL, "sl_SI.UTF-8"); + setlocale(LC_ALL, ""); start_color(); init_pair(2, COLOR_YELLOW, COLOR_BLACK); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_WHITE, COLOR_BLACK); init_pair(4, COLOR_GREEN, COLOR_BLACK); + init_pair(5, COLOR_CYAN, COLOR_BLACK); keypad(stdscr, TRUE); getmaxyx(stdscr, y, x); /* to je macro, zato y in x nista kazalca (;: */ WINDOW * textwin = subwin(stdscr, y-3, x, 0, 0); @@ -130,7 +158,7 @@ int dc_ui_thread (struct dc_thread_control * t) { refresh(); struct dc_channel * prev_joinedchannel = (void *)&i; /* just so we get something that isn't null without warnings (): */ while (t->power_ui != 2) { - if (!(rand() % 100)) { /* roughly every 100 cycles we get errors and messages */ + if (!(rand() % 10)) { /* roughly every 10 cycles we get errors and messages */ assert(!DC_CRLE(c, c->errors_lock)); for (int i = 0; i < c->errors_sizeof; i++) { if (!c->errors[i]->reported) { @@ -141,25 +169,25 @@ int dc_ui_thread (struct dc_thread_control * t) { assert(!pthread_rwlock_unlock(c->errors_lock)); /* deadlock if we unlock errors with error reporting, duh */ if (c->joinedchannel) { DC_CRLE(c, c->guilds_lock); - for (int i = 0; i < c->joinedchannel->messages_sizeof; i++) { + for (int i = c->joinedchannel->messages_sizeof-1; i >= 0; i--) { struct dc_message * msg2do = c->joinedchannel->messages[i]; - if (!msg2do->status) { + if (!msg2do->status) dc_ui_print_message(textwin, msg2do); - msg2do->status = 1; - } } DC_CUE(c, c->guilds_lock); } if (prev_joinedchannel != c->joinedchannel) { curs_set(0); /* too flashy */ - attron(COLOR_PAIR(2)); + attron(COLOR_PAIR(5)); if (c->joinedchannel) { DC_CRLE(c, c->guilds_lock); - mvprintw(y-3, 0, "#%s @ %s | %s", c->joinedchannel->name, c->joinedchannel->guild->name, c->joinedchannel->topic); + mvprintw(y-3, 0, "#%s @ %s%s%s ", c->joinedchannel->name, c->joinedchannel->guild->name, + strlen(c->joinedchannel->topic) ? " - " : "", c->joinedchannel->topic); DC_CUE(c, c->guilds_lock); - } - else + } else { + attron(COLOR_PAIR(2)); mvprintw(y-3, 0, DC_I18N_UI_LINE_BEFORE_JOIN); + } curs_set(1); prev_joinedchannel = c->joinedchannel; } @@ -205,8 +233,8 @@ int dc_ui_thread (struct dc_thread_control * t) { case 10: form_driver(form, REQ_NEXT_FIELD); form_driver(form, REQ_PREV_FIELD); - dc_ui_processline(t, field_buffer(field[0], 0), textwin); - form_driver(form, REQ_CLR_FIELD); + if (dc_ui_processline(t, field_buffer(field[0], 0), textwin) > 0) + form_driver(form, REQ_CLR_FIELD); pos_form_cursor(form); break; default: |