source: mainline/uspace/lib/startmenu/src/startmenu.c@ 7d78e466

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7d78e466 was 7d78e466, checked in by Jiri Svoboda <jiri@…>, 21 months ago

Load start menu from file using libstartmenu

  • Property mode set to 100644
File size: 5.1 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 libstartmenu
30 * @{
31 */
32/**
33 * @file Start menu
34 */
35
36#include <errno.h>
37#include <sif.h>
38#include <startmenu/startmenu.h>
39#include <stdlib.h>
40#include <str.h>
41#include "../private/startmenu.h"
42
43/** Open start menu.
44 *
45 * @param repopath Pathname of the menu repository
46 * @param rsmenu Place to store pointer to start menu
47 * @return EOK on success or an error code
48 */
49errno_t startmenu_open(const char *repopath, startmenu_t **rsmenu)
50{
51 startmenu_t *smenu;
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 smenu = calloc(1, sizeof(startmenu_t));
62 if (smenu == NULL) {
63 rc = ENOMEM;
64 goto error;
65 }
66
67 list_initialize(&smenu->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 = startmenu_entry_create(smenu, caption, cmd);
102 if (rc != EOK)
103 goto error;
104
105 nentry = sif_node_next_child(nentry);
106 }
107
108 *rsmenu = smenu;
109 return EOK;
110error:
111 if (repo != NULL)
112 sif_close(repo);
113 if (smenu != NULL)
114 free(smenu);
115 return rc;
116}
117
118/** Close start menu.
119 *
120 * @param smenu Start menu
121 */
122void startmenu_close(startmenu_t *smenu)
123{
124}
125
126/** Get first start menu entry.
127 *
128 * @param smenu Start menu
129 * @return First entry or @c NULL if the menu is empty
130 */
131startmenu_entry_t *startmenu_first(startmenu_t *smenu)
132{
133 link_t *link;
134
135 link = list_first(&smenu->entries);
136 if (link == NULL)
137 return NULL;
138
139 return list_get_instance(link, startmenu_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 */
147startmenu_entry_t *startmenu_next(startmenu_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, startmenu_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 */
163const char *startmenu_entry_get_caption(startmenu_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 */
173const char *startmenu_entry_get_cmd(startmenu_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 */
186errno_t startmenu_entry_create(startmenu_t *smenu, const char *caption,
187 const char *cmd)
188{
189 startmenu_entry_t *entry;
190 errno_t rc;
191
192 entry = calloc(1, sizeof(startmenu_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;
213error:
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 */
Note: See TracBrowser for help on using the repository browser.