| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libtbarcfg
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file Task bar configuration
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <sif.h>
|
|---|
| 38 | #include <tbarcfg/tbarcfg.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <str.h>
|
|---|
| 41 | #include "../private/tbarcfg.h"
|
|---|
| 42 |
|
|---|
| 43 | /** Open task bar configuration.
|
|---|
| 44 | *
|
|---|
| 45 | * @param repopath Pathname of the menu repository
|
|---|
| 46 | * @param rtbcfg Place to store pointer to task bar configuration
|
|---|
| 47 | * @return EOK on success or an error code
|
|---|
| 48 | */
|
|---|
| 49 | errno_t tbarcfg_open(const char *repopath, tbarcfg_t **rtbcfg)
|
|---|
| 50 | {
|
|---|
| 51 | tbarcfg_t *tbcfg;
|
|---|
| 52 | sif_sess_t *repo = NULL;
|
|---|
| 53 | sif_node_t *rnode;
|
|---|
| 54 | sif_node_t *nentries;
|
|---|
| 55 | sif_node_t *nentry;
|
|---|
| 56 | const char *ntype;
|
|---|
| 57 | const char *caption;
|
|---|
| 58 | const char *cmd;
|
|---|
| 59 | errno_t rc;
|
|---|
| 60 |
|
|---|
| 61 | tbcfg = calloc(1, sizeof(tbarcfg_t));
|
|---|
| 62 | if (tbcfg == NULL) {
|
|---|
| 63 | rc = ENOMEM;
|
|---|
| 64 | goto error;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | list_initialize(&tbcfg->entries);
|
|---|
| 68 |
|
|---|
| 69 | rc = sif_open(repopath, &repo);
|
|---|
| 70 | if (rc != EOK)
|
|---|
| 71 | goto error;
|
|---|
| 72 |
|
|---|
| 73 | rnode = sif_get_root(repo);
|
|---|
| 74 | nentries = sif_node_first_child(rnode);
|
|---|
| 75 | ntype = sif_node_get_type(nentries);
|
|---|
| 76 | if (str_cmp(ntype, "entries") != 0) {
|
|---|
| 77 | rc = EIO;
|
|---|
| 78 | goto error;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | nentry = sif_node_first_child(nentries);
|
|---|
| 82 | while (nentry != NULL) {
|
|---|
| 83 | ntype = sif_node_get_type(nentry);
|
|---|
| 84 | if (str_cmp(ntype, "entry") != 0) {
|
|---|
| 85 | rc = EIO;
|
|---|
| 86 | goto error;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | caption = sif_node_get_attr(nentry, "caption");
|
|---|
| 90 | if (caption == NULL) {
|
|---|
| 91 | rc = EIO;
|
|---|
| 92 | goto error;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | cmd = sif_node_get_attr(nentry, "cmd");
|
|---|
| 96 | if (cmd == NULL) {
|
|---|
| 97 | rc = EIO;
|
|---|
| 98 | goto error;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | rc = smenu_entry_create(tbcfg, caption, cmd);
|
|---|
| 102 | if (rc != EOK)
|
|---|
| 103 | goto error;
|
|---|
| 104 |
|
|---|
| 105 | nentry = sif_node_next_child(nentry);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | *rtbcfg = tbcfg;
|
|---|
| 109 | return EOK;
|
|---|
| 110 | error:
|
|---|
| 111 | if (repo != NULL)
|
|---|
| 112 | sif_close(repo);
|
|---|
| 113 | if (tbcfg != NULL)
|
|---|
| 114 | free(tbcfg);
|
|---|
| 115 | return rc;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /** Close task bar configuration.
|
|---|
| 119 | *
|
|---|
| 120 | * @param tbcfg Start menu
|
|---|
| 121 | */
|
|---|
| 122 | void tbarcfg_close(tbarcfg_t *tbcfg)
|
|---|
| 123 | {
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Get first start menu entry.
|
|---|
| 127 | *
|
|---|
| 128 | * @param tbcfg Task bar configuration
|
|---|
| 129 | * @return First entry or @c NULL if the menu is empty
|
|---|
| 130 | */
|
|---|
| 131 | smenu_entry_t *tbarcfg_smenu_first(tbarcfg_t *tbcfg)
|
|---|
| 132 | {
|
|---|
| 133 | link_t *link;
|
|---|
| 134 |
|
|---|
| 135 | link = list_first(&tbcfg->entries);
|
|---|
| 136 | if (link == NULL)
|
|---|
| 137 | return NULL;
|
|---|
| 138 |
|
|---|
| 139 | return list_get_instance(link, smenu_entry_t, lentries);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /** Get next start menu entry.
|
|---|
| 143 | *
|
|---|
| 144 | * @param cur Current entry
|
|---|
| 145 | * @return Next entry or @c NULL if @a cur is the last entry
|
|---|
| 146 | */
|
|---|
| 147 | smenu_entry_t *tbarcfg_smenu_next(smenu_entry_t *cur)
|
|---|
| 148 | {
|
|---|
| 149 | link_t *link;
|
|---|
| 150 |
|
|---|
| 151 | link = list_next(&cur->lentries, &cur->smenu->entries);
|
|---|
| 152 | if (link == NULL)
|
|---|
| 153 | return NULL;
|
|---|
| 154 |
|
|---|
| 155 | return list_get_instance(link, smenu_entry_t, lentries);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /** Get start menu entry caption.
|
|---|
| 159 | *
|
|---|
| 160 | * @param entry Start menu entry
|
|---|
| 161 | * @return Caption (with accelerator markup)
|
|---|
| 162 | */
|
|---|
| 163 | const char *smenu_entry_get_caption(smenu_entry_t *entry)
|
|---|
| 164 | {
|
|---|
| 165 | return entry->caption;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /** Get start menu entry command.
|
|---|
| 169 | *
|
|---|
| 170 | * @param entr Start menu entry
|
|---|
| 171 | * @return Command to run
|
|---|
| 172 | */
|
|---|
| 173 | const char *smenu_entry_get_cmd(smenu_entry_t *entry)
|
|---|
| 174 | {
|
|---|
| 175 | return entry->cmd;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /** Create a start menu entry and append it to the start menu (internal).
|
|---|
| 179 | *
|
|---|
| 180 | * This only creates the entry in memory, but does not update the repository.
|
|---|
| 181 | *
|
|---|
| 182 | * @param smenu Start menu
|
|---|
| 183 | * @param caption Caption
|
|---|
| 184 | * @param cmd Command to run
|
|---|
| 185 | */
|
|---|
| 186 | errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption,
|
|---|
| 187 | const char *cmd)
|
|---|
| 188 | {
|
|---|
| 189 | smenu_entry_t *entry;
|
|---|
| 190 | errno_t rc;
|
|---|
| 191 |
|
|---|
| 192 | entry = calloc(1, sizeof(smenu_entry_t));
|
|---|
| 193 | if (entry == NULL) {
|
|---|
| 194 | rc = ENOMEM;
|
|---|
| 195 | goto error;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | entry->caption = str_dup(caption);
|
|---|
| 199 | if (entry->caption == NULL) {
|
|---|
| 200 | rc = ENOMEM;
|
|---|
| 201 | goto error;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | entry->cmd = str_dup(cmd);
|
|---|
| 205 | if (entry->cmd == NULL) {
|
|---|
| 206 | rc = ENOMEM;
|
|---|
| 207 | goto error;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | entry->smenu = smenu;
|
|---|
| 211 | list_append(&entry->lentries, &smenu->entries);
|
|---|
| 212 | return EOK;
|
|---|
| 213 | error:
|
|---|
| 214 | if (entry != NULL) {
|
|---|
| 215 | if (entry->caption != NULL)
|
|---|
| 216 | free(entry->caption);
|
|---|
| 217 | if (entry->cmd != NULL)
|
|---|
| 218 | free(entry->cmd);
|
|---|
| 219 | free(entry);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | return rc;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /** @}
|
|---|
| 226 | */
|
|---|