diff --git a/src/line_info.c b/src/line_info.c index 54113ee..2523c8b 100644 --- a/src/line_info.c +++ b/src/line_info.c @@ -31,6 +31,8 @@ #include "groupchat.h" #include "settings.h" #include "notify.h" +#include "message_queue.h" +#include "misc_tools.h" extern struct user_settings *user_settings_; @@ -129,7 +131,7 @@ static struct line_info *line_info_ret_queue(struct history *hst) /* creates new line_info line and puts it in the queue. SYS_MSG lines may contain an arbitrary number of arguments for string formatting */ -void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint8_t type, uint8_t bold, +void line_info_add(ToxWindow *self, char *timestr, char *name1, char *name2, uint8_t type, uint8_t bold, uint8_t colour, const char *msg, ...) { struct history *hst = self->chatwin->hst; @@ -153,9 +155,12 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint /* for type-specific formatting in print function */ switch (type) { + case GROUP_ACTION: + len += 3; + break; + case OUT_ACTION: case IN_ACTION: - case GROUP_ACTION: len += 5; break; @@ -191,9 +196,9 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint } } - if (tmstmp) { - snprintf(new_line->timestamp, sizeof(new_line->timestamp), "%s", tmstmp); - len += strlen(new_line->timestamp); + if (timestr) { + snprintf(new_line->timestr, sizeof(new_line->timestr), "%s", timestr); + len += strlen(new_line->timestr); } if (name1) { @@ -210,6 +215,7 @@ void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint new_line->type = type; new_line->bold = bold; new_line->colour = colour; + new_line->timestamp = get_unix_time(); hst->queue[hst->queue_sz++] = new_line; } @@ -289,7 +295,7 @@ void line_info_print(ToxWindow *self) case OUT_MSG_READ: case IN_MSG: wattron(win, COLOR_PAIR(BLUE)); - wprintw(win, "%s", line->timestamp); + wprintw(win, "%s", line->timestr); wattroff(win, COLOR_PAIR(BLUE)); int nameclr = GREEN; @@ -311,7 +317,7 @@ void line_info_print(ToxWindow *self) if (line->msg[0] == '>') wattroff(win, COLOR_PAIR(GREEN)); - if (type == OUT_MSG) { /* sent message with no recieve receipt */ + if (type == OUT_MSG && timed_out(line->timestamp, get_unix_time(), CQUEUE_TRY_SEND_INTERVAL)) { wattron(win, COLOR_PAIR(RED)); wprintw(win, " x", line->msg); wattroff(win, COLOR_PAIR(RED)); @@ -325,14 +331,14 @@ void line_info_print(ToxWindow *self) case OUT_ACTION: case IN_ACTION: wattron(win, COLOR_PAIR(BLUE)); - wprintw(win, "%s", line->timestamp); + wprintw(win, "%s", line->timestr); wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(YELLOW)); wprintw(win, "* %s %s", line->name1, line->msg); wattroff(win, COLOR_PAIR(YELLOW)); - if (type == OUT_ACTION) { /* sent action with no recieve receipt */ + if (type == OUT_ACTION && timed_out(line->timestamp, get_unix_time(), CQUEUE_TRY_SEND_INTERVAL)) { wattron(win, COLOR_PAIR(RED)); wprintw(win, " x", line->msg); wattroff(win, COLOR_PAIR(RED)); @@ -342,9 +348,9 @@ void line_info_print(ToxWindow *self) break; case SYS_MSG: - if (line->timestamp[0]) { + if (line->timestr[0]) { wattron(win, COLOR_PAIR(BLUE)); - wprintw(win, "%s", line->timestamp); + wprintw(win, "%s", line->timestr); wattroff(win, COLOR_PAIR(BLUE)); } @@ -377,7 +383,7 @@ void line_info_print(ToxWindow *self) case CONNECTION: wattron(win, COLOR_PAIR(BLUE)); - wprintw(win, "%s", line->timestamp); + wprintw(win, "%s", line->timestr); wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(line->colour)); @@ -391,7 +397,7 @@ void line_info_print(ToxWindow *self) case NAME_CHANGE: wattron(win, COLOR_PAIR(BLUE)); - wprintw(win, "%s", line->timestamp); + wprintw(win, "%s", line->timestr); wattroff(win, COLOR_PAIR(BLUE)); wattron(win, COLOR_PAIR(MAGENTA)); diff --git a/src/line_info.h b/src/line_info.h index 516930e..32f04e9 100644 --- a/src/line_info.h +++ b/src/line_info.h @@ -45,10 +45,11 @@ enum { } LINE_TYPE; struct line_info { - char timestamp[TIME_STR_SIZE]; + char timestr[TIME_STR_SIZE]; char name1[TOXIC_MAX_NAME_LENGTH]; char name2[TOXIC_MAX_NAME_LENGTH]; char msg[TOX_MAX_MESSAGE_LENGTH]; + uint64_t timestamp; uint8_t type; uint8_t bold; uint8_t colour; @@ -73,7 +74,7 @@ struct history { /* creates new line_info line and puts it in the queue. SYS_MSG lines may contain an arbitrary number of arguments for string formatting */ -void line_info_add(ToxWindow *self, char *tmstmp, char *name1, char *name2, uint8_t type, uint8_t bold, +void line_info_add(ToxWindow *self, char *timestr, char *name1, char *name2, uint8_t type, uint8_t bold, uint8_t colour, const char *msg, ...); /* Prints a section of history starting at line_start */ diff --git a/src/message_queue.c b/src/message_queue.c index 47d89d1..b890e84 100644 --- a/src/message_queue.c +++ b/src/message_queue.c @@ -53,13 +53,10 @@ void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, ui new_m->type = type; new_m->line_id = line_id; - if (q->root == NULL) { - new_m->prev = NULL; + if (q->root == NULL) q->root = new_m; - } else { - new_m->prev = q->end; + else q->end->next = new_m; - } q->end = new_m; } @@ -80,39 +77,21 @@ static void cqueue_mark_read(ToxWindow *self, uint32_t id, uint8_t type) } } -/* removes the message with the same receipt number from queue and updates line to show the message was received*/ +/* removes root from queue and updates line to show the message was received. + receipt should always be equal to queue root's receipt */ void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt) { - struct cqueue_msg *q_msg = q->root; + struct cqueue_msg *root = q->root; - while (q_msg) { - struct cqueue_msg *next = q_msg->next; + if (root->receipt != receipt) + return; - if (q_msg->receipt == receipt) { - uint32_t line_id = q_msg->line_id; - uint8_t type = q_msg->type; - - if (q_msg->prev == NULL) { - if (next) - next->prev = NULL; - - free(q->root); - q->root = next; - } else { - q_msg->prev->next = next; - free(q_msg); - } - - cqueue_mark_read(self, line_id, type); - return; - } - - q_msg = next; - } + cqueue_mark_read(self, root->line_id, root->type); + struct cqueue_msg *next = root->next; + free(q->root); + q->root = next; } -#define CQUEUE_TRY_SEND_INTERVAL 5 - /* Tries to send oldest message in queue. If fails, tries again in CQUEUE_TRY_SEND_INTERVAL seconds */ void cqueue_try_send(ToxWindow *self, Tox *m) { diff --git a/src/message_queue.h b/src/message_queue.h index 44c9e5f..d18ac70 100644 --- a/src/message_queue.h +++ b/src/message_queue.h @@ -20,6 +20,8 @@ * */ +#define CQUEUE_TRY_SEND_INTERVAL 5 + struct cqueue_msg { char message[MAX_STR_SIZE]; int len; @@ -27,7 +29,6 @@ struct cqueue_msg { uint8_t type; uint32_t receipt; uint64_t last_send_try; - struct cqueue_msg *prev; struct cqueue_msg *next; }; @@ -40,8 +41,9 @@ struct chat_queue { void cqueue_cleanup(struct chat_queue *q); void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, uint32_t line_id); -/* Tries to send oldest message in queue once every CQUEUE_TRY_SEND_INTERVAL seconds */ +/* Tries to send oldest message in queue. If fails, tries again in CQUEUE_TRY_SEND_INTERVAL seconds */ void cqueue_try_send(ToxWindow *self, Tox *m); -/* removes the message with the same receipt number from queue and updates line to show the message was received*/ +/* removes root from queue and updates line to show the message was received. + receipt should always be equal to queue root's receipt */ void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt);