From 567efd1f45e1d9794845ee855b09ebdc7e718e0d Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Sat, 21 Apr 2018 10:22:13 -0400 Subject: [PATCH] In config menu squash directories containing a single config - Single configs all by itself in a directory are shown as a member of the parent directory. This allows keeping every config in its own directory without causing an additional level of nesting. Eg., import always put each config in its own directory. Improves the menu navigation. Signed-off-by: Selva Nair --- openvpn_config.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/openvpn_config.c b/openvpn_config.c index 0339aa9..e5b7d00 100644 --- a/openvpn_config.c +++ b/openvpn_config.c @@ -178,6 +178,8 @@ NewConfigGroup(const wchar_t *name, config_group_t *parent, int flags) * All groups that link at least one config to the root are * enabled. Dangling entries with no terminal configs will stay * disabled and are not displayed in the menu tree. + * Also groups with single configs are squashed if the group + * and config names match --- this improves the display. */ static void ActivateConfigGroups(void) @@ -185,6 +187,45 @@ ActivateConfigGroups(void) /* the root group is always active */ o.groups[0].active = true; + /* count children of each group -- this includes groups + * and configs which have it as parent + */ + for (int i = 0; i < o.num_configs; i++) + { + o.conn[i].group->children++; + } + + for (int i = 1; i < o.num_groups; i++) + { + config_group_t *this = &o.groups[i]; + if (this->parent) /* should be true as i = 0 is omitted */ + this->parent->children++; + + /* unless activated below the group stays inactive */ + this->active = false; + } + + /* Squash single config directories with name matching the config + * one depth up. This is done so that automatically imported configs + * which are added as a single config per directory are handled + * as if its in the parent directory. This encourages the + * practice of keeping each config and its dependencies (certs, + * script etc.) in a separate directory, without making the menu structure + * too deeply nested. + */ + for (int i = 0; i < o.num_configs; i++) + { + config_group_t *cg = o.conn[i].group; + + /* if not root and has only this config as child -- squash it */ + if (cg->parent && cg->children == 1 + && !wcscmp(cg->name, o.conn[i].config_name)) + { + cg->children--; + o.conn[i].group = cg->parent; + } + } + /* activate all groups that connect a config to the root */ for (int i = 0; i < o.num_configs; i++) {