From 08d1fcc15df840aa4da9a0d0861adb0f69dfd318 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Sat, 20 Jul 2019 13:39:37 +0200 Subject: [PATCH] fix join, and implement joining from guild view --- libdc/src/session.c | 9 ++++- ncdc/include/ncdc/treeview.h | 2 ++ ncdc/src/join.c | 4 +-- ncdc/src/mainwindow.c | 65 +++++++++++++++++++++++++++++++++--- ncdc/src/treeview.c | 12 +++++++ 5 files changed, 84 insertions(+), 8 deletions(-) diff --git a/libdc/src/session.c b/libdc/src/session.c index 391b655..36394c4 100644 --- a/libdc/src/session.c +++ b/libdc/src/session.c @@ -83,7 +83,7 @@ static void dc_session_handle_ready(dc_session_t s, dc_event_t e) json_t *user = NULL; json_t *relationships = NULL; json_t *presences = NULL; - size_t idx = 0; + size_t idx = 0, i = 0; json_t *c = NULL; json_t *channels = NULL; json_t *guilds = NULL; @@ -143,6 +143,13 @@ static void dc_session_handle_ready(dc_session_t s, dc_event_t e) dc_guild_t guild = dc_guild_from_json(c); continue_if_true(guild == NULL); dc_session_add_guild(s, guild); + + /* add their channels to our own thing + */ + for (i = 0; i < dc_guild_channels(guild); i++) { + dc_channel_t chan = dc_guild_nth_channel(guild, i); + dc_session_add_channel(s, chan); + } } } diff --git a/ncdc/include/ncdc/treeview.h b/ncdc/include/ncdc/treeview.h index e2ff1ba..7dff29d 100644 --- a/ncdc/include/ncdc/treeview.h +++ b/ncdc/include/ncdc/treeview.h @@ -16,6 +16,7 @@ void ncdc_treeitem_set_label(ncdc_treeitem_t i, wchar_t const *s); void *ncdc_treeitem_tag(ncdc_treeitem_t i); void ncdc_treeitem_set_tag(ncdc_treeitem_t i, void *t); +ncdc_treeitem_t ncdc_treeitem_parent(ncdc_treeitem_t i); size_t ncdc_treeitem_size(ncdc_treeitem_t i); void ncdc_treeitem_clear(ncdc_treeitem_t i); @@ -27,6 +28,7 @@ typedef struct ncdc_treeview_ *ncdc_treeview_t; ncdc_treeview_t ncdc_treeview_new(void); ncdc_treeitem_t ncdc_treeview_root(ncdc_treeview_t t); +ncdc_treeitem_t ncdc_treeview_current(ncdc_treeview_t t); void ncdc_treeview_render(ncdc_treeview_t t, WINDOW *w, int lines, int cols); /* move the cursor around, and collapse/expand items diff --git a/ncdc/src/join.c b/ncdc/src/join.c index 001fdf4..4a43102 100644 --- a/ncdc/src/join.c +++ b/ncdc/src/join.c @@ -20,7 +20,7 @@ ncdc_cmd_join(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f) return false; } - if (ac == 2) { + if (ac == 3) { guild = w_convert(av[1]); channel = w_convert(av[2]); @@ -35,7 +35,7 @@ ncdc_cmd_join(ncdc_mainwindow_t n, size_t ac, wchar_t **av, wchar_t const *f) LOG(n, L"join: no such channel %s in guild %s", channel, guild); goto cleanup; } - } else if (ac == 1) { + } else if (ac == 2) { id = w_convert(av[1]); c = dc_session_channel_by_id(current_session, id); diff --git a/ncdc/src/mainwindow.c b/ncdc/src/mainwindow.c index 1f8d2f1..d50d831 100644 --- a/ncdc/src/mainwindow.c +++ b/ncdc/src/mainwindow.c @@ -126,11 +126,9 @@ ncdc_mainwindow_callback(ncdc_input_t i, wchar_t const *s, if (s[0] == '/') { ret = ncdc_dispatch(mainwin, s); } else { - wchar_t *post = calloc(wcslen(s)+7, sizeof(wchar_t)); - - wcscat(post, L"/post "); - wcscat(post, s); + wchar_t *post = NULL; + aswprintf(&post, L"/post %ls", s); ret = ncdc_dispatch(mainwin, post); free(post); } @@ -249,6 +247,34 @@ static void ncdc_mainwindow_render_status(ncdc_mainwindow_t n) free(status); } +static void ncdc_mainwindow_open_guildchat(ncdc_mainwindow_t n) +{ + ncdc_treeitem_t cur = ncdc_treeview_current(n->guildview); + dc_channel_t channel = NULL; + wchar_t *cmd = NULL; + + return_if_true(cur == NULL || + /* not the root, thanks + */ + cur == ncdc_treeview_root(n->guildview) || + /* not one root (again) + */ + ncdc_treeitem_parent(cur) == NULL || + /* not a guild who are the first level after root + */ + ncdc_treeitem_parent(cur) == ncdc_treeview_root(n->guildview), + ); + + channel = ncdc_treeitem_tag(cur); + return_if_true(channel == NULL,); + + aswprintf(&cmd, L"/join %s", dc_channel_id(channel)); + return_if_true(cmd == NULL,); + + ncdc_dispatch(n, cmd); + free(cmd); +} + void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n) { GHashTableIter iter; @@ -378,7 +404,7 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n) { if (key != NULL && (k = ncdc_find_keybinding(keys_chat, key, keylen)) != NULL) { - k->handler(n->guildview); + k->handler(n->chat); } } break; @@ -387,6 +413,8 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n) if (key != NULL && (k = ncdc_find_keybinding(keys_guilds, key, keylen)) != NULL) { k->handler(n->guildview); + } else if (i == '\r') { + ncdc_mainwindow_open_guildchat(n); } } break; @@ -472,6 +500,7 @@ ncdc_textview_t ncdc_mainwindow_switch_or_add(ncdc_mainwindow_t n, dc_channel_t c) { ncdc_textview_t v = NULL; + wchar_t *name = NULL; return_if_true(n == NULL || c == NULL, NULL); return_if_true(!is_logged_in(), NULL); @@ -486,6 +515,32 @@ ncdc_mainwindow_switch_or_add(ncdc_mainwindow_t n, dc_channel_t c) ncdc_textview_set_account(v, dc_session_me(current_session)); ncdc_textview_set_channel(v, c); + if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_TEXT) { + aswprintf(&name, L"#%s", dc_channel_name(c)); + } else if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_VOICE) { + aswprintf(&name, L">%s", dc_channel_name(c)); + } else if (dc_channel_is_dm(c)) { + size_t namelen = 0, i = 0; + FILE *f = open_wmemstream(&name, &namelen); + + for (i = 0; i < dc_channel_recipients(c); i++) { + dc_account_t rec = dc_channel_nth_recipient(c, i); + if (dc_account_fullname(rec) != NULL) { + fwprintf(f, L"%s", dc_account_fullname(rec)); + if (i < dc_channel_recipients(c)-1) { + fputwc('/', f); + } + } + } + + fclose(f); + } + + if (name != NULL) { + ncdc_textview_set_title(v, name); + free(name); + } + g_ptr_array_add(n->views, v); ncdc_mainwindow_switch_view(n, v); } diff --git a/ncdc/src/treeview.c b/ncdc/src/treeview.c index 07a545f..2180dea 100644 --- a/ncdc/src/treeview.c +++ b/ncdc/src/treeview.c @@ -122,6 +122,12 @@ void ncdc_treeitem_set_tag(ncdc_treeitem_t i, void *t) i->tag = t; } +ncdc_treeitem_t ncdc_treeitem_parent(ncdc_treeitem_t i) +{ + return_if_true(i == NULL, NULL); + return i->parent; +} + wchar_t const *ncdc_treeitem_label(ncdc_treeitem_t i) { return_if_true(i == NULL, NULL); @@ -220,6 +226,12 @@ void ncdc_treeview_render(ncdc_treeview_t t, WINDOW *w, int lines, int cols) ncdc_treeitem_render(t->root, w, lines, cols, 0, 0); } +ncdc_treeitem_t ncdc_treeview_current(ncdc_treeview_t t) +{ + return_if_true(t == NULL, NULL); + return t->current; +} + ncdc_treeitem_t ncdc_treeview_root(ncdc_treeview_t t) { return_if_true(t == NULL, NULL);