Change to the linux kernel coding style
[wmaker-crm.git] / src / menureader.c
blob5f35036ac5a9f83578b408f8010fedbdedc659bf
1 /* menureader.c- root menu definition readers
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 2000-2003 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
21 */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27 #include <unistd.h>
29 #include "WindowMaker.h"
31 #include "misc.h"
33 #include "rootmenu.h"
35 typedef struct PLMenuReaderData {
36 WRootMenuReader *reader;
38 WMPropList *pl;
39 int curIndex;
41 WMPropList **submenu;
42 int *curSubIndex;
43 int submenuDepth;
45 } PLMenuReaderData;
47 typedef struct TextMenuReaderData {
48 WRootMenuReader *reader;
50 FILE *file;
52 } TextMenuReaderData;
54 typedef struct PipeMenuReaderData {
55 WRootMenuReader *reader;
57 } PipeMenuReaderData;
59 typedef struct DirMenuReaderData {
60 WRootMenuReader *reader;
62 char **dirList;
63 int dirCount;
65 } DirMenuReaderData;
67 /*
68 typedef struct GNOMEMenuReaderData {
69 } GNOMEMenuReaderData;
70 */
72 static WRootMenuData *pl_openMenu(WMPropList * pl);
73 static Bool pl_hasMoreData(WRootMenuData * data);
74 static Bool pl_nextCommand(WRootMenuData * data, char **title, char **command, char **parameter, char **shortcut);
75 static void pl_closeMenuFile(WRootMenuData * data);
77 static WRootMenuData *text_openMenuFile(char *path);
78 static Bool text_hasMoreData(WRootMenuData * data);
79 static Bool text_nextCommand(WRootMenuData * data,
80 char **title, char **command, char **parameter, char **shortcut);
81 static void text_closeMenuFile(WRootMenuData * data);
83 static WRootMenuData *dir_openMenuFile(char *path);
84 static Bool dir_hasMoreData(WRootMenuData * data);
85 static Bool dir_nextCommand(WRootMenuData * data, char **title, char **command, char **parameter, char **shortcut);
86 static void dir_closeMenuFile(WRootMenuData * data);
88 static WRootMenuReader PLMenuReader = {
89 pl_hasMoreData,
90 pl_nextCommand,
91 pl_closeMenuFile
92 };
94 static WRootMenuReader TextMenuReader = {
95 text_hasMoreData,
96 text_nextCommand,
97 text_closeMenuFile
98 };
100 static WRootMenuReader DirMenuReaderData = {
101 dir_hasMoreData,
102 dir_nextCommand,
103 dir_closeMenuFile
104 };
106 /*
107 WRootMenuReader GNOMEMenuReaderData = {
108 };
109 */
111 #define LINESIZE 1024
113 static char linebuf[LINESIZE];
115 /* ---------- proplist ---------- */
117 static WRootMenuData *pl_openMenuFile(WMPropList * pl)
119 PLRootMenuData *data = wmalloc(sizeof(PLRootMenuData));
121 data->reader = PLMenuReader;
123 data->pl = pl;
124 data->curIndex = 0;
126 data->submenu = NULL;
127 data->curSubIndex = NULL;
128 data->submenuDepth = 0;
130 return data;
133 static Bool pl_hasMoreData(WRootMenuData * data)
137 static Bool pl_nextCommand(WRootMenuData * data, char **title, char **command, char **parameter, char **shortcut)
141 static void pl_closeMenuFile(WRootMenuData * data)
143 if (data->submenu)
144 wfree(data->submenu);
145 if (data->curSubIndex)
146 wfree(data->curSubIndex);
148 WMReleasePropList(data->pl);
150 wfree(data);
153 /* ---------- text ---------- */
155 static WRootMenuData *text_openMenuFile(char *path)
157 TextMenuReaderData *data;
159 data = wmalloc(sizeof(TextMenuReaderData));
160 data->reader = TextMenuReader;
162 data->file = fopen(path, "rb");
163 if (!data->file) {
165 return NULL;
169 static Bool text_hasMoreData(WRootMenuData * data)
173 static Bool text_nextCommand(WRootMenuData * data, char **title, char **command, char **parameter, char **shortcut)
177 static void text_closeMenuFile(WRootMenuData * data)
181 /* ---------- directory ---------- */
183 static WRootMenuData *dir_openMenuFile(char *paths, time_t * timestamp)
185 DirMenuReaderData *data;
186 char **dirs;
187 int dirN;
188 time_t checksum = 0;
189 int i, c;
191 /* timestamp for directory is a "checksum" of the directory times */
193 wtokensplit(paths, &dirs, &dirN);
195 if (dirN == 0) {
196 return NULL;
199 for (c = 0, i = 0; i < dirN; i++) {
200 char *tmp;
202 if (strcmp(dirs[i], "-noext") == 0) {
203 i++;
204 continue;
207 tmp = wexpandpath(dirs[i]);
208 wfree(dirs[i]);
209 dirs[i] = tmp;
211 if (stat(dirs[i], &stat_buf) < 0) {
212 wsyserror(_("%s:could not stat menu"), dirs[i]);
213 wfree(dirs[i]);
214 dirs[i] = NULL;
215 } else {
216 c++;
217 checksum += stat_buf.st_mtime;
221 if (*timestamp == checksum && *timestamp != 0) {
222 return NULL;
225 if (c == 0) {
226 for (i = 0; i < dirN; i++) {
227 if (dirs[i])
228 wfree(dirs[i]);
230 wfree(dirs);
232 return NULL;
235 data = wmalloc(sizeof(DirMenuReaderData));
236 data->reader = DirMenuReader;
240 static Bool dir_hasMoreData(WRootMenuData * data)
244 static Bool dir_nextCommand(WRootMenuData * data, char **title, char **command, char **parameter, char **shortcut)
248 static void dir_closeMenuFile(WRootMenuData * data)
252 WRootMenuData *OpenMenu(char *path, time_t * menuTime)
254 proplist pl;
255 struct stat stat_buf;
256 WRootMenuData *data;
258 /* check whether it's a piped menu */
259 if (*path == '|') {
260 /* piped menus have the following semantics for menuTime:
261 * if it's 0, then it wasnt loaded yet
262 * if it's 1, it was already loaded, so do not reload
263 * (would be too slow)
264 * now, menuTime will only be set to 1 if the pipe command is
265 * specified as ||command instead of |command
266 * in other words ||command means force the submenu to always refresh
267 */
268 if (*menuTime == 0) {
269 data = pipe_openMenu(path);
271 if (path[1] != '|') {
272 *menuTime = 1;
274 return data;
277 if (stat(path, &stat_buf) < 0) {
278 wsyserror(_("could not stat() menu file '%s'"));
279 return NULL;
282 /* check whether it's a directory */
283 if (S_ISDIR(stat_buf.st_mode)) {
284 return dir_openMenuFile(path, menuTime);
287 if (*menuTime >= stat_buf.st_mtime && *menuTime != 0) {
288 /* no changes in the menu file */
289 return NULL;
292 /* then check whether it's a proplist menu */
293 pl = WMReadPropListFromFile(path);
294 if (pl && WMIsPLArray(pl)) {
295 *menuTime = stat_buf.st_mtime;
296 return pl_openMenu(pl);
299 *menuTime = stat_buf.st_mtime;
300 /* assume its a plain text menu */
301 return text_openMenuFile(path);
304 WRootMenuData *ReopenRootMenu(time_t * checkTime, char **menuPath, time_t * menuTimestamp)
306 proplist pl;
307 struct stat stat_buf;
308 char *path;
310 if (stat(path, &stat_buf) < 0) {
311 wsyserror(_("could not stat() menu file '%s'"));
312 return NULL;
315 if (*menuTime >= stat_buf.st_mtime && *checkTime != 0) {
316 /* no changes in WMRootMenu, see if the contents changed */
317 if (*menuPath != NULL) {
318 return OpenMenu(*menuPath, menuTimestamp);
319 } else {
320 return NULL;
324 *checkTime = stat_buf.st_mtime;
326 pl = WMReadPropListFromFile(path);
327 if (!pl) {
328 wwarning(_("could not load domain %s from user defaults database"), "WMRootMenu");
329 return NULL;
332 if (WMIsPLString(pl)) {
333 char *tmp;
334 char *path;
335 Bool menu_is_default = False;
337 tmp = wexpandpath(WMGetFromPLString(pl));
339 path = getLocalizedMenuFile(tmp);
341 if (!path) {
342 path = wfindfile(DEF_CONFIG_PATHS, tmp);
345 if (!path) {
346 wwarning(_("could not find menu file '%s' referenced in WMRootMenu"), tmp);
347 path = wfindfile(DEF_CONFIG_PATHS, DEF_MENU_FILE);
348 menu_is_default = True;
351 if (!path) {
352 wwarning(_("could not find any usable menu files. Please check '%s'"), tmp);
353 wfree(tmp);
354 return NULL;
357 wfree(tmp);
359 if (*menuPath) {
360 if (strcmp(*menuPath, path) != 0) {
361 *menuTimestamp = 0;
362 wfree(*menuPath);
363 *menuPath = path;
365 if (menu_is_default) {
366 wwarning(_
367 ("using default menu file \"%s\" as the menu referenced in WMRootMenu could not be found "),
368 path);
370 } else {
371 /* the menu path didn't change, but the
372 * pointed file might have changed, so we don't return
373 */
375 } else {
376 *menuPath = path;
379 return OpenMenu(*menuPath, menuTimestamp);
380 } else if (WMIsPLArray(pl)) {
382 *menuTimestamp = stat_buf.st_mtime;
384 return pl_openMenu(pl);
385 } else {
386 wwarning(_
387 ("invalid content in menu file '%s'.\nIt should either be a property list menu or the path to the file, enclosed in \"."),
388 path);
389 return NULL;