fetch messages for channels and display them

This commit is contained in:
2019-07-08 18:38:24 +02:00
parent 8718ef85a0
commit c3f4f5a180
13 changed files with 397 additions and 8 deletions

View File

@@ -58,6 +58,8 @@ wchar_t *util_readkey(int esc, WINDOW *win);
void exit_main(void);
wchar_t *s_convert(char const *s);
int strwidth(char const *string);
char *read_char(FILE *stream);

View File

@@ -39,6 +39,11 @@ bool ncdc_cmd_login(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
goto cleanup;
}
if (!dc_api_get_friends(api, acc)) {
LOG(n, L"login: %ls: failed to load friends", av[1]);
goto cleanup;
}
current_account = dc_ref(acc);
LOG(n, L"login: %ls: authentication successful", av[1]);
ret = true;

View File

@@ -48,12 +48,16 @@ bool ncdc_cmd_msg(ncdc_mainwindow_t n, size_t ac, wchar_t **av)
if (c == NULL) {
/* no? create a new window and switch to it
*/
if (!dc_api_create_channel(api, current_account, &f, 1, &c)) {
LOG(n, L"msg: failed to create channel");
goto cleanup;
}
if (!dc_api_get_messages(api, current_account, c)) {
LOG(n, L"msg: failed to fetch messages in channel");
goto cleanup;
}
v = ncdc_textview_new();
goto_if_true(v == NULL, cleanup);

View File

@@ -133,16 +133,11 @@ wchar_t const *ncdc_textview_nthline(ncdc_textview_t v, size_t idx)
return g_ptr_array_index(v->par, idx);
}
void ncdc_textview_render(ncdc_textview_t v, WINDOW *win, int lines, int cols)
static void
ncdc_textview_render_par(ncdc_textview_t v, WINDOW *win, int lines, int cols)
{
ssize_t i = 0, needed_lines = 0, atline = 0;
werase(win);
if (v->par == NULL || v->par->len == 0) {
return;
}
for (i = v->par->len-1; i >= 0; i--) {
wchar_t const *w = ncdc_textview_nthline(v, i);
size_t sz = wcslen(w);
@@ -159,3 +154,92 @@ void ncdc_textview_render(ncdc_textview_t v, WINDOW *win, int lines, int cols)
}
}
}
static wchar_t *ncdc_textview_format(dc_message_t m)
{
wchar_t *c = NULL, *author = NULL, *message = NULL;
size_t clen = 0;
FILE *f = open_wmemstream(&c, &clen);
wchar_t *ret = NULL;
dc_account_t a = dc_message_author(m);
return_if_true(f == NULL, NULL);
author = s_convert(dc_account_fullname(a));
goto_if_true(author == NULL, cleanup);
message = s_convert(dc_message_content(m));
goto_if_true(message == NULL, cleanup);
fwprintf(f, L"< %ls> %ls", author, message);
fclose(f);
f = NULL;
ret = c;
c = NULL;
cleanup:
if (f != NULL) {
fclose(f);
}
free(author);
free(message);
free(c);
return ret;
}
static void
ncdc_textview_render_msgs(ncdc_textview_t v, WINDOW *win, int lines, int cols)
{
ssize_t i = 0, atline = 0, msgs = 0;
msgs = dc_channel_messages(v->channel);
atline = lines;
for (i = msgs-1; i >= 0; i--) {
dc_message_t m = dc_channel_nthmessage(v->channel, i);
wchar_t *s = ncdc_textview_format(m);
wchar_t const *end = s, *last = NULL;
size_t len = 0;
size_t needed_lines = 0;
/* count each line, and, see if it is longer than COLS
*/
while ((end = wcschr(end, '\n')) != NULL) {
++needed_lines;
len = wcswidth(last, (end - last));
needed_lines += (len % cols);
last = end;
}
if (last == NULL) {
last = s;
}
len = wcswidth(last, wcslen(last));
needed_lines += (len / cols) + 1;
if ((atline - needed_lines) >= 0) {
atline -= needed_lines;
mvwaddwstr(win, atline, 0, s);
}
free(s);
}
}
void ncdc_textview_render(ncdc_textview_t v, WINDOW *win, int lines, int cols)
{
werase(win);
if (v->par != NULL && v->par->len > 0) {
ncdc_textview_render_par(v, win, lines, cols);
} else if (v->channel != NULL) {
ncdc_textview_render_msgs(v, win, lines, cols);
}
}

View File

@@ -168,3 +168,17 @@ char *w_convert(wchar_t const *w)
wcstombs(ptr, w, sz);
return ptr;
}
wchar_t *s_convert(char const *w)
{
size_t sz = 0;
wchar_t *ptr = NULL;
sz = mbstowcs(NULL, w, 0);
ptr = calloc(sz+1, sizeof(wchar_t));
return_if_true(ptr == NULL, NULL);
mbstowcs(ptr, w, sz);
return ptr;
}