properly use parent_ids by storing them in a hash first

This commit is contained in:
Florian Stinglmayr 2019-07-20 09:09:22 +02:00
parent b07b716a56
commit d9c276c68c
3 changed files with 34 additions and 14 deletions

View File

@ -52,6 +52,7 @@ dc_channel_t dc_channel_from_json(json_t *j);
char const *dc_channel_id(dc_channel_t c); char const *dc_channel_id(dc_channel_t c);
char const *dc_channel_name(dc_channel_t c); char const *dc_channel_name(dc_channel_t c);
char const *dc_channel_parent_id(dc_channel_t c);
dc_channel_type_t dc_channel_type(dc_channel_t c); dc_channel_type_t dc_channel_type(dc_channel_t c);
bool dc_channel_is_dm(dc_channel_t c); bool dc_channel_is_dm(dc_channel_t c);

View File

@ -249,6 +249,12 @@ char const *dc_channel_id(dc_channel_t c)
return c->id; return c->id;
} }
char const *dc_channel_parent_id(dc_channel_t c)
{
return_if_true(c == NULL, NULL);
return c->parent_id;
}
dc_channel_type_t dc_channel_type(dc_channel_t c) dc_channel_type_t dc_channel_type(dc_channel_t c)
{ {
return_if_true(c == NULL, -1); return_if_true(c == NULL, -1);

View File

@ -251,6 +251,7 @@ void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n)
GHashTableIter iter; GHashTableIter iter;
gpointer key = NULL, value = NULL; gpointer key = NULL, value = NULL;
size_t idx = 0; size_t idx = 0;
GHashTable *parents = NULL;
ncdc_treeitem_clear(n->root); ncdc_treeitem_clear(n->root);
@ -258,6 +259,8 @@ void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n)
return; return;
} }
parents = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_iter_init(&iter, dc_session_guilds(current_session)); g_hash_table_iter_init(&iter, dc_session_guilds(current_session));
while (g_hash_table_iter_next(&iter, &key, &value)) { while (g_hash_table_iter_next(&iter, &key, &value)) {
dc_guild_t g = (dc_guild_t)value; dc_guild_t g = (dc_guild_t)value;
@ -291,38 +294,48 @@ void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n)
continue; continue;
} }
/* skip categories, and shop channels
*/
if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_CATEGORY ||
dc_channel_type(c) == CHANNEL_TYPE_GUILD_NEWS ||
dc_channel_type(c) == CHANNEL_TYPE_GUILD_STORE) {
continue;
}
ci = ncdc_treeitem_new(); ci = ncdc_treeitem_new();
if (ci == NULL) { if (ci == NULL) {
continue; continue;
} }
if (dc_channel_type(c) == CHANNEL_TYPE_GUILD_VOICE ||
dc_channel_type(c) == CHANNEL_TYPE_GUILD_TEXT) {
aswprintf(&name, L"[%s] %s", aswprintf(&name, L"[%s] %s",
(dc_channel_type(c) == CHANNEL_TYPE_GUILD_VOICE ? (dc_channel_type(c) == CHANNEL_TYPE_GUILD_VOICE ?
"<" : "#"), "<" : "#"),
dc_channel_name(c) dc_channel_name(c)
); );
} else {
aswprintf(&name, L"%s", dc_channel_name(c));
}
if (name == NULL) { if (name == NULL) {
continue; continue;
} }
g_hash_table_insert(parents, (void*)dc_channel_id(c), ci);
ncdc_treeitem_set_label(ci, name); ncdc_treeitem_set_label(ci, name);
free(name); free(name);
name = NULL; name = NULL;
ncdc_treeitem_set_tag(ci, c); ncdc_treeitem_set_tag(ci, c);
if (dc_channel_parent_id(c) != NULL &&
g_hash_table_contains(parents, dc_channel_parent_id(c))) {
ncdc_treeitem_t parent = g_hash_table_lookup(
parents, dc_channel_parent_id(c)
);
ncdc_treeitem_add(parent, ci);
} else {
ncdc_treeitem_add(i, ci); ncdc_treeitem_add(i, ci);
} }
}
ncdc_treeitem_add(n->root, i); ncdc_treeitem_add(n->root, i);
} }
g_hash_table_unref(parents);
} }
void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n) void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n)