source: mainline/uspace/lib/tbarcfg/src/tbarcfg.c@ b1397ab

topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1397ab was b1397ab, checked in by Jiri Svoboda <jiri@…>, 2 years ago

Start menu editor is editing

You can change an entry and it will be saved to the configuration repo.
(Cannot create/delete yet). To see the change in the editor, you need
to restart it. To see the change in the taskbar, you need to restart it.

  • Property mode set to 100644
File size: 6.7 KB
Line 
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 Taskbar 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 taskbar configuration.
44 *
45 * @param repopath Pathname of the menu repository
46 * @param rtbcfg Place to store pointer to taskbar configuration
47 * @return EOK on success or an error code
48 */
49errno_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 tbcfg->repo = repo;
74
75 rnode = sif_get_root(repo);
76 nentries = sif_node_first_child(rnode);
77 ntype = sif_node_get_type(nentries);
78 if (str_cmp(ntype, "entries") != 0) {
79 rc = EIO;
80 goto error;
81 }
82
83 nentry = sif_node_first_child(nentries);
84 while (nentry != NULL) {
85 ntype = sif_node_get_type(nentry);
86 if (str_cmp(ntype, "entry") != 0) {
87 rc = EIO;
88 goto error;
89 }
90
91 caption = sif_node_get_attr(nentry, "caption");
92 if (caption == NULL) {
93 rc = EIO;
94 goto error;
95 }
96
97 cmd = sif_node_get_attr(nentry, "cmd");
98 if (cmd == NULL) {
99 rc = EIO;
100 goto error;
101 }
102
103 rc = smenu_entry_create(tbcfg, nentry, caption, cmd);
104 if (rc != EOK)
105 goto error;
106
107 nentry = sif_node_next_child(nentry);
108 }
109
110 *rtbcfg = tbcfg;
111 return EOK;
112error:
113 if (repo != NULL)
114 sif_close(repo);
115 if (tbcfg != NULL)
116 free(tbcfg);
117 return rc;
118}
119
120/** Close taskbar configuration.
121 *
122 * @param tbcfg Start menu
123 */
124void tbarcfg_close(tbarcfg_t *tbcfg)
125{
126 (void)sif_close(tbcfg->repo);
127}
128
129/** Get first start menu entry.
130 *
131 * @param tbcfg Taskbar configuration
132 * @return First entry or @c NULL if the menu is empty
133 */
134smenu_entry_t *tbarcfg_smenu_first(tbarcfg_t *tbcfg)
135{
136 link_t *link;
137
138 link = list_first(&tbcfg->entries);
139 if (link == NULL)
140 return NULL;
141
142 return list_get_instance(link, smenu_entry_t, lentries);
143}
144
145/** Get next start menu entry.
146 *
147 * @param cur Current entry
148 * @return Next entry or @c NULL if @a cur is the last entry
149 */
150smenu_entry_t *tbarcfg_smenu_next(smenu_entry_t *cur)
151{
152 link_t *link;
153
154 link = list_next(&cur->lentries, &cur->smenu->entries);
155 if (link == NULL)
156 return NULL;
157
158 return list_get_instance(link, smenu_entry_t, lentries);
159}
160
161/** Get start menu entry caption.
162 *
163 * @param entry Start menu entry
164 * @return Caption (with accelerator markup)
165 */
166const char *smenu_entry_get_caption(smenu_entry_t *entry)
167{
168 return entry->caption;
169}
170
171/** Get start menu entry command.
172 *
173 * @param entr Start menu entry
174 * @return Command to run
175 */
176const char *smenu_entry_get_cmd(smenu_entry_t *entry)
177{
178 return entry->cmd;
179}
180
181/** Set start menu entry caption.
182 *
183 * Note: To make the change visible to others and persistent,
184 * you must call @c smenu_entry_save()
185 *
186 * @param entry Start menu entry
187 * @param caption New caption
188 * @return EOK on success, ENOMEM if out of memory
189 */
190errno_t smenu_entry_set_caption(smenu_entry_t *entry, const char *caption)
191{
192 char *dcap;
193
194 dcap = str_dup(caption);
195 if (dcap == NULL)
196 return ENOMEM;
197
198 free(entry->caption);
199 entry->caption = dcap;
200 return EOK;
201}
202
203/** Set start menu entry command.
204 *
205 * Note: To make the change visible to others and persistent,
206 * you must call @c smenu_entry_save()
207 *
208 * @param entry Start menu entry
209 * @param cmd New command
210 * @return EOK on success, ENOMEM if out of memory
211 */
212errno_t smenu_entry_set_cmd(smenu_entry_t *entry, const char *cmd)
213{
214 char *dcmd;
215
216 dcmd = str_dup(cmd);
217 if (dcmd == NULL)
218 return ENOMEM;
219
220 free(entry->cmd);
221 entry->cmd = dcmd;
222 return EOK;
223}
224
225/** Save any changes to start menu entry.
226 *
227 * @param entry Start menu entry
228 */
229errno_t smenu_entry_save(smenu_entry_t *entry)
230{
231 sif_trans_t *trans;
232 errno_t rc;
233
234 rc = sif_trans_begin(entry->smenu->repo, &trans);
235 if (rc != EOK)
236 goto error;
237
238 rc = sif_node_set_attr(trans, entry->nentry, "cmd", entry->cmd);
239 if (rc != EOK)
240 goto error;
241
242 rc = sif_node_set_attr(trans, entry->nentry, "caption", entry->caption);
243 if (rc != EOK)
244 goto error;
245
246 rc = sif_trans_end(trans);
247 if (rc != EOK)
248 goto error;
249
250 return EOK;
251error:
252 if (trans != NULL)
253 sif_trans_abort(trans);
254 return rc;
255}
256
257/** Create a start menu entry and append it to the start menu (internal).
258 *
259 * This only creates the entry in memory, but does not update the repository.
260 *
261 * @param smenu Start menu
262 * @param nentry Backing SIF node
263 * @param caption Caption
264 * @param cmd Command to run
265 */
266errno_t smenu_entry_create(tbarcfg_t *smenu, sif_node_t *nentry,
267 const char *caption, const char *cmd)
268{
269 smenu_entry_t *entry;
270 errno_t rc;
271
272 entry = calloc(1, sizeof(smenu_entry_t));
273 if (entry == NULL) {
274 rc = ENOMEM;
275 goto error;
276 }
277
278 entry->nentry = nentry;
279
280 entry->caption = str_dup(caption);
281 if (entry->caption == NULL) {
282 rc = ENOMEM;
283 goto error;
284 }
285
286 entry->cmd = str_dup(cmd);
287 if (entry->cmd == NULL) {
288 rc = ENOMEM;
289 goto error;
290 }
291
292 entry->smenu = smenu;
293 list_append(&entry->lentries, &smenu->entries);
294 return EOK;
295error:
296 if (entry != NULL) {
297 if (entry->caption != NULL)
298 free(entry->caption);
299 if (entry->cmd != NULL)
300 free(entry->cmd);
301 free(entry);
302 }
303
304 return rc;
305}
306
307/** @}
308 */
Note: See TracBrowser for help on using the repository browser.