wmmenugen: Use 'Other' instead of 'Applications' for unknown categories.
[wmaker-crm.git] / util / wmmenugen_parse_xdg.c
blobe909f18c18365c386a240deb171cfc285c2062a8
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * Desktop Entry Specification parser functions
6 * Copyright (c) 2010. Tamas Tevesz <ice@extreme.hu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.1.html
25 * http://standards.freedesktop.org/menu-spec/menu-spec-1.1.html
27 * We will only deal with Type == "Application" entries in [Desktop Entry]
28 * groups. Since there is no passing of file name arguments or anything of
29 * the sort to applications from the menu, execname is determined as follows:
30 * - If `TryExec' is present, use that;
31 * - else use `Exec' with any switches stripped
33 * Only the (first, though there should not be more than one) `Main Category'
34 * is used to place the entry in a submenu.
36 * Basic validation of the .desktop file is done.
39 #include <sys/types.h>
40 #include <sys/stat.h>
42 #include <ctype.h>
43 #include <ftw.h>
44 #if DEBUG
45 #include <errno.h>
46 #endif
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
51 #include "wmmenugen.h"
53 /* LocaleString match levels */
54 enum {
55 MATCH_DEFAULT,
56 MATCH_LANG,
57 MATCH_LANG_MODIFIER,
58 MATCH_LANG_COUNTRY,
59 MATCH_LANG_COUNTRY_MODIFIER
62 typedef struct {
63 char *Name; /* Name */ /* localestring */
64 int MatchLevel; /* LocaleString match type */ /* int */
65 char *TryExec; /* TryExec */ /* string */
66 char *Exec; /* Exec */ /* string */
67 char *Path; /* Path */ /* string */
68 int Flags; /* Flags */
69 char *Category; /* Categories (first item only) */ /* string */
70 } XDGMenuEntry;
72 static void getKey(char **target, const char *line);
73 static void getStringValue(char **target, const char *line);
74 static void getLocalizedStringValue(char **target, const char *line, int *match_level);
75 static int getBooleanValue(const char *line);
76 static void getMenuHierarchyFor(char **xdgmenuspec);
77 static int compare_matchlevel(int *current_level, const char *found_locale);
78 static Bool xdg_to_wm(XDGMenuEntry **xdg, WMMenuEntry **wmentry);
79 static void init_xdg_storage(XDGMenuEntry **xdg);
80 static void init_wm_storage(WMMenuEntry **wm);
83 void parse_xdg(const char *file, cb_add_menu_entry *addWMMenuEntryCallback)
85 FILE *fp;
86 char buf[1024];
87 char *p, *tmp, *key;
88 WMMenuEntry *wm;
89 XDGMenuEntry *xdg;
90 int InGroup;
92 fp = fopen(file, "r");
93 if (!fp) {
94 #if DEBUG
95 fprintf(stderr, "Error opening file %s: %s\n", file, strerror(errno));
96 #endif
97 return;
100 xdg = (XDGMenuEntry *)wmalloc(sizeof(XDGMenuEntry));
101 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
102 InGroup = 0;
103 memset(buf, 0, sizeof(buf));
105 while (fgets(buf, sizeof(buf), fp)) {
107 p = buf;
109 /* skip whitespaces */
110 while (isspace(*p))
111 p++;
112 /* skip comments, empty lines */
113 if (*p == '\r' || *p == '\n' || *p == '#') {
114 memset(buf, 0, sizeof(buf));
115 continue;
117 /* trim crlf */
118 buf[strcspn(buf, "\r\n")] = '\0';
119 if (strlen(buf) == 0)
120 continue;
122 if (strcmp(p, "[Desktop Entry]") == 0) {
123 /* if currently processing a group, we've just hit the
124 * end of its definition, try processing it
126 if (InGroup && xdg_to_wm(&xdg, &wm)) {
127 (*addWMMenuEntryCallback)(wm);
129 init_xdg_storage(&xdg);
130 init_wm_storage(&wm);
131 InGroup = 1;
132 /* start processing group */
133 memset(buf, 0, sizeof(buf));
134 continue;
135 } else if (p[0] == '[') {
136 /* If we find a new group and the previous group was the main one,
137 * we stop all further processing
139 if (InGroup) break;
142 if (!InGroup) {
143 memset(buf, 0, sizeof(buf));
144 continue;
147 getKey(&key, p);
148 if (key == NULL) { /* not `key' = `value' */
149 memset(buf, 0, sizeof(buf));
150 continue;
153 if (strcmp(key, "Type") == 0) {
154 getStringValue(&tmp, p);
155 if (strcmp(tmp, "Application") != 0)
156 InGroup = 0; /* if not application, skip current group */
157 wfree(tmp);
158 tmp = NULL;
159 } else if (strcmp(key, "Name") == 0) {
160 getLocalizedStringValue(&xdg->Name, p, &xdg->MatchLevel);
161 } else if (strcmp(key, "NoDisplay") == 0) {
162 if (getBooleanValue(p)) /* if nodisplay, skip current group */
163 InGroup = 0;
164 } else if (strcmp(key, "Hidden") == 0) {
165 if (getBooleanValue(p))
166 InGroup = 0; /* if hidden, skip current group */
167 } else if (strcmp(key, "TryExec") == 0) {
168 getStringValue(&xdg->TryExec, p);
169 } else if (strcmp(key, "Exec") == 0) {
170 getStringValue(&xdg->Exec, p);
171 } else if (strcmp(key, "Path") == 0) {
172 getStringValue(&xdg->Path, p);
173 } else if (strcmp(key, "Terminal") == 0) {
174 if (getBooleanValue(p))
175 xdg->Flags |= F_TERMINAL;
176 } else if (strcmp(key, "Categories") == 0) {
177 getStringValue(&xdg->Category, p);
178 getMenuHierarchyFor(&xdg->Category);
181 wfree(key);
182 key = NULL;
185 fclose(fp);
187 /* at the end of the file, might as well try to menuize what we have
188 * unless there was no group at all or it was marked as hidden
190 if (InGroup && xdg_to_wm(&xdg, &wm))
191 (*addWMMenuEntryCallback)(wm);
196 /* coerce an xdg entry type into a wm entry type
198 static Bool xdg_to_wm(XDGMenuEntry **xdg, WMMenuEntry **wm)
200 char *p;
202 /* Exec or TryExec is mandatory */
203 if (!((*xdg)->Exec || (*xdg)->TryExec))
204 return False;
206 /* if there's no Name, use the first word of Exec or TryExec
208 if ((*xdg)->Name) {
209 (*wm)->Name = (*xdg)->Name;
210 } else {
211 if ((*xdg)->Exec)
212 (*wm)->Name = wstrdup((*xdg)->Exec);
213 else /* (*xdg)->TryExec */
214 (*wm)->Name = wstrdup((*xdg)->TryExec);
216 p = strchr((*wm)->Name, ' ');
217 if (p)
218 *p = '\0';
221 if ((*xdg)->Exec)
222 (*wm)->CmdLine = (*xdg)->Exec;
223 else /* (*xdg)->TryExec */
224 (*wm)->CmdLine = (*xdg)->TryExec;
226 (*wm)->SubMenu = (*xdg)->Category;
227 (*wm)->Flags = (*xdg)->Flags;
229 return True;
232 /* (re-)initialize a XDGMenuEntry storage
234 static void init_xdg_storage(XDGMenuEntry **xdg)
237 if ((*xdg)->Name)
238 wfree((*xdg)->Name);
239 if ((*xdg)->TryExec)
240 wfree((*xdg)->TryExec);
241 if ((*xdg)->Exec)
242 wfree((*xdg)->Exec);
243 if ((*xdg)->Category)
244 wfree((*xdg)->Category);
245 if ((*xdg)->Path)
246 wfree((*xdg)->Path);
248 (*xdg)->Name = NULL;
249 (*xdg)->TryExec = NULL;
250 (*xdg)->Exec = NULL;
251 (*xdg)->Category = NULL;
252 (*xdg)->Path = NULL;
253 (*xdg)->Flags = 0;
254 (*xdg)->MatchLevel = -1;
257 /* (re-)initialize a WMMenuEntry storage
259 static void init_wm_storage(WMMenuEntry **wm)
261 (*wm)->Name = NULL;
262 (*wm)->CmdLine = NULL;
263 (*wm)->Flags = 0;
266 /* get a key from line. allocates target, which must be wfreed later */
267 static void getKey(char **target, const char *line)
269 const char *p;
270 int kstart, kend;
272 p = line;
274 if (strchr(p, '=') == NULL) { /* not `key' = `value' */
275 *target = NULL;
276 return;
279 kstart = 0;
281 /* skip whitespace */
282 while (isspace(*(p + kstart)))
283 kstart++;
285 /* skip up until first whitespace or '[' (localestring) or '=' */
286 kend = kstart + 1;
287 while (*(p + kend) && !isspace(*(p + kend)) && *(p + kend) != '=' && *(p + kend) != '[')
288 kend++;
290 *target = wstrndup(p + kstart, kend - kstart);
293 /* get a string value from line. allocates target, which must be wfreed later. */
294 static void getStringValue(char **target, const char *line)
296 const char *p;
297 int kstart;
299 p = line;
300 kstart = 0;
302 /* skip until after '=' */
303 while (*(p + kstart) && *(p + kstart) != '=')
304 kstart++;
305 kstart++;
307 /* skip whitespace */
308 while (*(p + kstart) && isspace(*(p + kstart)))
309 kstart++;
311 *target = wstrdup(p + kstart);
314 /* get a localized string value from line. allocates target, which must be wfreed later.
315 * matching is dependent on the current value of target as well as on the
316 * level the current value is matched on. guts matching algorithm is in
317 * compare_matchlevel().
319 static void getLocalizedStringValue(char **target, const char *line, int *match_level)
321 const char *p;
322 char *locale;
323 int kstart;
324 int sqbstart, sqbend;
326 p = line;
327 kstart = 0;
328 sqbstart = 0;
329 sqbend = 0;
330 locale = NULL;
332 /* skip until after '=', mark if '[' and ']' is found */
333 while (*(p + kstart) && *(p + kstart) != '=') {
334 switch (*(p + kstart)) {
335 case '[': sqbstart = kstart + 1;break;
336 case ']': sqbend = kstart; break;
337 default : break;
339 kstart++;
341 kstart++;
343 /* skip whitespace */
344 while (isspace(*(p + kstart)))
345 kstart++;
347 if (sqbstart > 0 && sqbend > sqbstart)
348 locale = wstrndup(p + sqbstart, sqbend - sqbstart);
350 /* if there is no value yet and this is the default key, return */
351 if (!*target && !locale) {
352 *match_level = MATCH_DEFAULT;
353 *target = wstrdup(p + kstart);
354 return;
357 if (compare_matchlevel(match_level, locale)) {
358 wfree(locale);
359 *target = wstrdup(p + kstart);
360 return;
363 return;
366 /* get a boolean value from line */
367 static Bool getBooleanValue(const char *line)
369 char *p;
370 int ret;
372 getStringValue(&p, line);
373 ret = strcmp(p, "true") == 0 ? True : False;
374 wfree(p);
376 return ret;
379 /* perform locale matching by implementing the algorithm specified in
380 * xdg desktop entry specification, section "localized values for keys".
382 static Bool compare_matchlevel(int *current_level, const char *found_locale)
384 /* current key locale */
385 char *key_lang, *key_ctry, *key_enc, *key_mod;
387 parse_locale(found_locale, &key_lang, &key_ctry, &key_enc, &key_mod);
389 if (env_lang && key_lang && /* Shortcut: if key and env languages don't match, */
390 strcmp(env_lang, key_lang) != 0) /* don't even bother. This takes care of the great */
391 return False; /* majority of the cases without having to go through */
392 /* the more theoretical parts of the spec'd algo. */
394 if (!env_mod && key_mod) /* If LC_MESSAGES does not have a MODIFIER field, */
395 return False; /* then no key with a modifier will be matched. */
397 if (!env_ctry && key_ctry) /* Similarly, if LC_MESSAGES does not have a COUNTRY field, */
398 return False; /* then no key with a country specified will be matched. */
400 /* LC_MESSAGES value: lang_COUNTRY@MODIFIER */
401 if (env_lang && env_ctry && env_mod) { /* lang_COUNTRY@MODIFIER */
402 if (key_lang && key_ctry && key_mod &&
403 strcmp(env_lang, key_lang) == 0 &&
404 strcmp(env_ctry, key_ctry) == 0 &&
405 strcmp(env_mod, key_mod) == 0) {
406 *current_level = MATCH_LANG_COUNTRY_MODIFIER;
407 return True;
408 } else if (key_lang && key_ctry && /* lang_COUNTRY */
409 strcmp(env_lang, key_lang) == 0 &&
410 strcmp(env_ctry, key_ctry) == 0 &&
411 *current_level < MATCH_LANG_COUNTRY) {
412 *current_level = MATCH_LANG_COUNTRY;
413 return True;
414 } else if (key_lang && key_mod && /* lang@MODIFIER */
415 strcmp(env_lang, key_lang) == 0 &&
416 strcmp(env_mod, key_mod) == 0 &&
417 *current_level < MATCH_LANG_MODIFIER) {
418 *current_level = MATCH_LANG_MODIFIER;
419 return True;
420 } else if (key_lang && /* lang */
421 strcmp(env_lang, key_lang) == 0 &&
422 *current_level < MATCH_LANG) {
423 *current_level = MATCH_LANG;
424 return True;
425 } else {
426 return False;
430 /* LC_MESSAGES value: lang_COUNTRY */
431 if (env_lang && env_ctry) { /* lang_COUNTRY */
432 if (key_lang && key_ctry &&
433 strcmp(env_lang, key_lang) == 0 &&
434 strcmp(env_ctry, key_ctry) == 0 &&
435 *current_level < MATCH_LANG_COUNTRY) {
436 *current_level = MATCH_LANG_COUNTRY;
437 return True;
438 } else if (key_lang && /* lang */
439 strcmp(env_lang, key_lang) == 0 &&
440 *current_level < MATCH_LANG) {
441 *current_level = MATCH_LANG;
442 return True;
443 } else {
444 return False;
448 /* LC_MESSAGES value: lang@MODIFIER */
449 if (env_lang && env_mod) { /* lang@MODIFIER */
450 if (key_lang && key_mod &&
451 strcmp(env_lang, key_lang) == 0 &&
452 strcmp(env_mod, key_mod) == 0 &&
453 *current_level < MATCH_LANG_MODIFIER) {
454 *current_level = MATCH_LANG_MODIFIER;
455 return True;
456 } else if (key_lang && /* lang */
457 strcmp(env_lang, key_lang) == 0 &&
458 *current_level < MATCH_LANG) {
459 *current_level = MATCH_LANG;
460 return True;
461 } else {
462 return False;
466 /* LC_MESSAGES value: lang */
467 if (env_lang) { /* lang */
468 if (key_lang &&
469 strcmp(env_lang, key_lang) == 0 &&
470 *current_level < MATCH_LANG) {
471 *current_level = MATCH_LANG;
472 return True;
473 } else {
474 return False;
478 /* MATCH_DEFAULT is handled in getLocalizedStringValue */
480 return False;
483 /* get the (first) xdg main category from a list of categories
485 static void getMenuHierarchyFor(char **xdgmenuspec)
487 char *category, *p;
488 char buf[1024];
490 if (!*xdgmenuspec || !**xdgmenuspec)
491 return;
493 category = wstrdup(*xdgmenuspec);
494 wfree(*xdgmenuspec);
495 memset(buf, 0, sizeof(buf));
497 p = strtok(category, ";");
498 while (p) { /* get a known category */
499 if (strcmp(p, "AudioVideo") == 0) {
500 snprintf(buf, sizeof(buf), "%s", _("Audio & Video"));
501 break;
502 } else if (strcmp(p, "Audio") == 0) {
503 snprintf(buf, sizeof(buf), "%s", _("Audio"));
504 break;
505 } else if (strcmp(p, "Video") == 0) {
506 snprintf(buf, sizeof(buf), "%s", _("Video"));
507 break;
508 } else if (strcmp(p, "Development") == 0) {
509 snprintf(buf, sizeof(buf), "%s", _("Development"));
510 break;
511 } else if (strcmp(p, "Education") == 0) {
512 snprintf(buf, sizeof(buf), "%s", _("Education"));
513 break;
514 } else if (strcmp(p, "Game") == 0) {
515 snprintf(buf, sizeof(buf), "%s", _("Game"));
516 break;
517 } else if (strcmp(p, "Graphics") == 0) {
518 snprintf(buf, sizeof(buf), "%s", _("Graphics"));
519 break;
520 } else if (strcmp(p, "Network") == 0) {
521 snprintf(buf, sizeof(buf), "%s", _("Network"));
522 break;
523 } else if (strcmp(p, "Office") == 0) {
524 snprintf(buf, sizeof(buf), "%s", _("Office"));
525 break;
526 } else if (strcmp(p, "Science") == 0) {
527 snprintf(buf, sizeof(buf), "%s", _("Science"));
528 break;
529 } else if (strcmp(p, "Settings") == 0) {
530 snprintf(buf, sizeof(buf), "%s", _("Settings"));
531 break;
532 } else if (strcmp(p, "System") == 0) {
533 snprintf(buf, sizeof(buf), "%s", _("System"));
534 break;
535 } else if (strcmp(p, "Utility") == 0) {
536 snprintf(buf, sizeof(buf), "%s", _("Utility"));
537 break;
538 /* reserved categories */
539 } else if (strcmp(p, "Screensaver") == 0) {
540 snprintf(buf, sizeof(buf), "%s", _("Screensaver"));
541 break;
542 } else if (strcmp(p, "TrayIcon") == 0) {
543 snprintf(buf, sizeof(buf), "%s", _("Tray Icon"));
544 break;
545 } else if (strcmp(p, "Applet") == 0) {
546 snprintf(buf, sizeof(buf), "%s", _("Applet"));
547 break;
548 } else if (strcmp(p, "Shell") == 0) {
549 snprintf(buf, sizeof(buf), "%s", _("Shell"));
550 break;
552 p = strtok(NULL, ";");
556 if (!*buf) /* come up with something if nothing found */
557 snprintf(buf, sizeof(buf), "%s", _("Other"));
559 *xdgmenuspec = wstrdup(buf);