[7d78e466] | 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 |
|
---|
[b279899] | 29 | /** @addtogroup libtbarcfg
|
---|
[7d78e466] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
[b279899] | 33 | * @file Task bar configuration
|
---|
[7d78e466] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <sif.h>
|
---|
[b279899] | 38 | #include <tbarcfg/tbarcfg.h>
|
---|
[7d78e466] | 39 | #include <stdlib.h>
|
---|
| 40 | #include <str.h>
|
---|
[b279899] | 41 | #include "../private/tbarcfg.h"
|
---|
[7d78e466] | 42 |
|
---|
[b279899] | 43 | /** Open task bar configuration.
|
---|
[7d78e466] | 44 | *
|
---|
| 45 | * @param repopath Pathname of the menu repository
|
---|
[b279899] | 46 | * @param rtbcfg Place to store pointer to task bar configuration
|
---|
[7d78e466] | 47 | * @return EOK on success or an error code
|
---|
| 48 | */
|
---|
[b279899] | 49 | errno_t tbarcfg_open(const char *repopath, tbarcfg_t **rtbcfg)
|
---|
[7d78e466] | 50 | {
|
---|
[b279899] | 51 | tbarcfg_t *tbcfg;
|
---|
[7d78e466] | 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 |
|
---|
[b279899] | 61 | tbcfg = calloc(1, sizeof(tbarcfg_t));
|
---|
| 62 | if (tbcfg == NULL) {
|
---|
[7d78e466] | 63 | rc = ENOMEM;
|
---|
| 64 | goto error;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[b279899] | 67 | list_initialize(&tbcfg->entries);
|
---|
[7d78e466] | 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 |
|
---|
[b279899] | 101 | rc = smenu_entry_create(tbcfg, caption, cmd);
|
---|
[7d78e466] | 102 | if (rc != EOK)
|
---|
| 103 | goto error;
|
---|
| 104 |
|
---|
| 105 | nentry = sif_node_next_child(nentry);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[b279899] | 108 | *rtbcfg = tbcfg;
|
---|
[7d78e466] | 109 | return EOK;
|
---|
| 110 | error:
|
---|
| 111 | if (repo != NULL)
|
---|
| 112 | sif_close(repo);
|
---|
[b279899] | 113 | if (tbcfg != NULL)
|
---|
| 114 | free(tbcfg);
|
---|
[7d78e466] | 115 | return rc;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[b279899] | 118 | /** Close task bar configuration.
|
---|
[7d78e466] | 119 | *
|
---|
[b279899] | 120 | * @param tbcfg Start menu
|
---|
[7d78e466] | 121 | */
|
---|
[b279899] | 122 | void tbarcfg_close(tbarcfg_t *tbcfg)
|
---|
[7d78e466] | 123 | {
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** Get first start menu entry.
|
---|
| 127 | *
|
---|
[b279899] | 128 | * @param tbcfg Task bar configuration
|
---|
[7d78e466] | 129 | * @return First entry or @c NULL if the menu is empty
|
---|
| 130 | */
|
---|
[b279899] | 131 | smenu_entry_t *tbarcfg_smenu_first(tbarcfg_t *tbcfg)
|
---|
[7d78e466] | 132 | {
|
---|
| 133 | link_t *link;
|
---|
| 134 |
|
---|
[b279899] | 135 | link = list_first(&tbcfg->entries);
|
---|
[7d78e466] | 136 | if (link == NULL)
|
---|
| 137 | return NULL;
|
---|
| 138 |
|
---|
[b279899] | 139 | return list_get_instance(link, smenu_entry_t, lentries);
|
---|
[7d78e466] | 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 | */
|
---|
[b279899] | 147 | smenu_entry_t *tbarcfg_smenu_next(smenu_entry_t *cur)
|
---|
[7d78e466] | 148 | {
|
---|
| 149 | link_t *link;
|
---|
| 150 |
|
---|
| 151 | link = list_next(&cur->lentries, &cur->smenu->entries);
|
---|
| 152 | if (link == NULL)
|
---|
| 153 | return NULL;
|
---|
| 154 |
|
---|
[b279899] | 155 | return list_get_instance(link, smenu_entry_t, lentries);
|
---|
[7d78e466] | 156 | }
|
---|
| 157 |
|
---|
| 158 | /** Get start menu entry caption.
|
---|
| 159 | *
|
---|
| 160 | * @param entry Start menu entry
|
---|
| 161 | * @return Caption (with accelerator markup)
|
---|
| 162 | */
|
---|
[b279899] | 163 | const char *smenu_entry_get_caption(smenu_entry_t *entry)
|
---|
[7d78e466] | 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 | */
|
---|
[b279899] | 173 | const char *smenu_entry_get_cmd(smenu_entry_t *entry)
|
---|
[7d78e466] | 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 | */
|
---|
[b279899] | 186 | errno_t smenu_entry_create(tbarcfg_t *smenu, const char *caption,
|
---|
[7d78e466] | 187 | const char *cmd)
|
---|
| 188 | {
|
---|
[b279899] | 189 | smenu_entry_t *entry;
|
---|
[7d78e466] | 190 | errno_t rc;
|
---|
| 191 |
|
---|
[b279899] | 192 | entry = calloc(1, sizeof(smenu_entry_t));
|
---|
[7d78e466] | 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 | */
|
---|