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>
51 #include "wmmenugen.h"
53 /* LocaleString match levels */
59 MATCH_LANG_COUNTRY_MODIFIER
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 */
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
)
92 fp
= fopen(file
, "r");
95 fprintf(stderr
, "Error opening file %s: %s\n", file
, strerror(errno
));
100 xdg
= (XDGMenuEntry
*)wmalloc(sizeof(XDGMenuEntry
));
101 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
103 memset(buf
, 0, sizeof(buf
));
105 while (fgets(buf
, sizeof(buf
), fp
)) {
109 /* skip whitespaces */
112 /* skip comments, empty lines */
113 if (*p
== '\r' || *p
== '\n' || *p
== '#') {
114 memset(buf
, 0, sizeof(buf
));
118 buf
[strcspn(buf
, "\r\n")] = '\0';
119 if (strlen(buf
) == 0)
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
);
132 /* start processing group */
133 memset(buf
, 0, sizeof(buf
));
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
143 memset(buf
, 0, sizeof(buf
));
148 if (key
== NULL
) { /* not `key' = `value' */
149 memset(buf
, 0, sizeof(buf
));
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 */
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 */
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 if (xdg
->Category
== NULL
)
182 xdg
->Category
= wstrdup(_("Other"));
190 /* at the end of the file, might as well try to menuize what we have
191 * unless there was no group at all or it was marked as hidden
193 if (InGroup
&& xdg_to_wm(&xdg
, &wm
))
194 (*addWMMenuEntryCallback
)(wm
);
199 /* coerce an xdg entry type into a wm entry type
201 static Bool
xdg_to_wm(XDGMenuEntry
**xdg
, WMMenuEntry
**wm
)
205 /* Exec or TryExec is mandatory */
206 if (!((*xdg
)->Exec
|| (*xdg
)->TryExec
))
209 /* if there's no Name, use the first word of Exec or TryExec
212 (*wm
)->Name
= (*xdg
)->Name
;
215 (*wm
)->Name
= wstrdup((*xdg
)->Exec
);
216 else /* (*xdg)->TryExec */
217 (*wm
)->Name
= wstrdup((*xdg
)->TryExec
);
219 p
= strchr((*wm
)->Name
, ' ');
225 (*wm
)->CmdLine
= (*xdg
)->Exec
;
226 else /* (*xdg)->TryExec */
227 (*wm
)->CmdLine
= (*xdg
)->TryExec
;
229 (*wm
)->SubMenu
= (*xdg
)->Category
;
230 (*wm
)->Flags
= (*xdg
)->Flags
;
235 /* (re-)initialize a XDGMenuEntry storage
237 static void init_xdg_storage(XDGMenuEntry
**xdg
)
243 wfree((*xdg
)->TryExec
);
246 if ((*xdg
)->Category
)
247 wfree((*xdg
)->Category
);
252 (*xdg
)->TryExec
= NULL
;
254 (*xdg
)->Category
= NULL
;
257 (*xdg
)->MatchLevel
= -1;
260 /* (re-)initialize a WMMenuEntry storage
262 static void init_wm_storage(WMMenuEntry
**wm
)
265 (*wm
)->CmdLine
= NULL
;
269 /* get a key from line. allocates target, which must be wfreed later */
270 static void getKey(char **target
, const char *line
)
277 if (strchr(p
, '=') == NULL
) { /* not `key' = `value' */
284 /* skip whitespace */
285 while (isspace(*(p
+ kstart
)))
288 /* skip up until first whitespace or '[' (localestring) or '=' */
290 while (*(p
+ kend
) && !isspace(*(p
+ kend
)) && *(p
+ kend
) != '=' && *(p
+ kend
) != '[')
293 *target
= wstrndup(p
+ kstart
, kend
- kstart
);
296 /* get a string value from line. allocates target, which must be wfreed later. */
297 static void getStringValue(char **target
, const char *line
)
305 /* skip until after '=' */
306 while (*(p
+ kstart
) && *(p
+ kstart
) != '=')
310 /* skip whitespace */
311 while (*(p
+ kstart
) && isspace(*(p
+ kstart
)))
314 *target
= wstrdup(p
+ kstart
);
317 /* get a localized string value from line. allocates target, which must be wfreed later.
318 * matching is dependent on the current value of target as well as on the
319 * level the current value is matched on. guts matching algorithm is in
320 * compare_matchlevel().
322 static void getLocalizedStringValue(char **target
, const char *line
, int *match_level
)
327 int sqbstart
, sqbend
;
335 /* skip until after '=', mark if '[' and ']' is found */
336 while (*(p
+ kstart
) && *(p
+ kstart
) != '=') {
337 switch (*(p
+ kstart
)) {
338 case '[': sqbstart
= kstart
+ 1;break;
339 case ']': sqbend
= kstart
; break;
346 /* skip whitespace */
347 while (isspace(*(p
+ kstart
)))
350 if (sqbstart
> 0 && sqbend
> sqbstart
)
351 locale
= wstrndup(p
+ sqbstart
, sqbend
- sqbstart
);
353 /* if there is no value yet and this is the default key, return */
354 if (!*target
&& !locale
) {
355 *match_level
= MATCH_DEFAULT
;
356 *target
= wstrdup(p
+ kstart
);
360 if (compare_matchlevel(match_level
, locale
)) {
362 *target
= wstrdup(p
+ kstart
);
369 /* get a boolean value from line */
370 static Bool
getBooleanValue(const char *line
)
375 getStringValue(&p
, line
);
376 ret
= strcmp(p
, "true") == 0 ? True
: False
;
382 /* perform locale matching by implementing the algorithm specified in
383 * xdg desktop entry specification, section "localized values for keys".
385 static Bool
compare_matchlevel(int *current_level
, const char *found_locale
)
387 /* current key locale */
388 char *key_lang
, *key_ctry
, *key_enc
, *key_mod
;
390 parse_locale(found_locale
, &key_lang
, &key_ctry
, &key_enc
, &key_mod
);
392 if (env_lang
&& key_lang
&& /* Shortcut: if key and env languages don't match, */
393 strcmp(env_lang
, key_lang
) != 0) /* don't even bother. This takes care of the great */
394 return False
; /* majority of the cases without having to go through */
395 /* the more theoretical parts of the spec'd algo. */
397 if (!env_mod
&& key_mod
) /* If LC_MESSAGES does not have a MODIFIER field, */
398 return False
; /* then no key with a modifier will be matched. */
400 if (!env_ctry
&& key_ctry
) /* Similarly, if LC_MESSAGES does not have a COUNTRY field, */
401 return False
; /* then no key with a country specified will be matched. */
403 /* LC_MESSAGES value: lang_COUNTRY@MODIFIER */
404 if (env_lang
&& env_ctry
&& env_mod
) { /* lang_COUNTRY@MODIFIER */
405 if (key_lang
&& key_ctry
&& key_mod
&&
406 strcmp(env_lang
, key_lang
) == 0 &&
407 strcmp(env_ctry
, key_ctry
) == 0 &&
408 strcmp(env_mod
, key_mod
) == 0) {
409 *current_level
= MATCH_LANG_COUNTRY_MODIFIER
;
411 } else if (key_lang
&& key_ctry
&& /* lang_COUNTRY */
412 strcmp(env_lang
, key_lang
) == 0 &&
413 strcmp(env_ctry
, key_ctry
) == 0 &&
414 *current_level
< MATCH_LANG_COUNTRY
) {
415 *current_level
= MATCH_LANG_COUNTRY
;
417 } else if (key_lang
&& key_mod
&& /* lang@MODIFIER */
418 strcmp(env_lang
, key_lang
) == 0 &&
419 strcmp(env_mod
, key_mod
) == 0 &&
420 *current_level
< MATCH_LANG_MODIFIER
) {
421 *current_level
= MATCH_LANG_MODIFIER
;
423 } else if (key_lang
&& /* lang */
424 strcmp(env_lang
, key_lang
) == 0 &&
425 *current_level
< MATCH_LANG
) {
426 *current_level
= MATCH_LANG
;
433 /* LC_MESSAGES value: lang_COUNTRY */
434 if (env_lang
&& env_ctry
) { /* lang_COUNTRY */
435 if (key_lang
&& key_ctry
&&
436 strcmp(env_lang
, key_lang
) == 0 &&
437 strcmp(env_ctry
, key_ctry
) == 0 &&
438 *current_level
< MATCH_LANG_COUNTRY
) {
439 *current_level
= MATCH_LANG_COUNTRY
;
441 } else if (key_lang
&& /* lang */
442 strcmp(env_lang
, key_lang
) == 0 &&
443 *current_level
< MATCH_LANG
) {
444 *current_level
= MATCH_LANG
;
451 /* LC_MESSAGES value: lang@MODIFIER */
452 if (env_lang
&& env_mod
) { /* lang@MODIFIER */
453 if (key_lang
&& key_mod
&&
454 strcmp(env_lang
, key_lang
) == 0 &&
455 strcmp(env_mod
, key_mod
) == 0 &&
456 *current_level
< MATCH_LANG_MODIFIER
) {
457 *current_level
= MATCH_LANG_MODIFIER
;
459 } else if (key_lang
&& /* lang */
460 strcmp(env_lang
, key_lang
) == 0 &&
461 *current_level
< MATCH_LANG
) {
462 *current_level
= MATCH_LANG
;
469 /* LC_MESSAGES value: lang */
470 if (env_lang
) { /* lang */
472 strcmp(env_lang
, key_lang
) == 0 &&
473 *current_level
< MATCH_LANG
) {
474 *current_level
= MATCH_LANG
;
481 /* MATCH_DEFAULT is handled in getLocalizedStringValue */
486 /* get the (first) xdg main category from a list of categories
488 static void getMenuHierarchyFor(char **xdgmenuspec
)
493 if (!*xdgmenuspec
|| !**xdgmenuspec
)
496 category
= wstrdup(*xdgmenuspec
);
498 memset(buf
, 0, sizeof(buf
));
500 p
= strtok(category
, ";");
501 while (p
) { /* get a known category */
502 if (strcmp(p
, "AudioVideo") == 0) {
503 snprintf(buf
, sizeof(buf
), "%s", _("Audio & Video"));
505 } else if (strcmp(p
, "Audio") == 0) {
506 snprintf(buf
, sizeof(buf
), "%s", _("Audio"));
508 } else if (strcmp(p
, "Video") == 0) {
509 snprintf(buf
, sizeof(buf
), "%s", _("Video"));
511 } else if (strcmp(p
, "Development") == 0) {
512 snprintf(buf
, sizeof(buf
), "%s", _("Development"));
514 } else if (strcmp(p
, "Education") == 0) {
515 snprintf(buf
, sizeof(buf
), "%s", _("Education"));
517 } else if (strcmp(p
, "Game") == 0) {
518 snprintf(buf
, sizeof(buf
), "%s", _("Game"));
520 } else if (strcmp(p
, "Graphics") == 0) {
521 snprintf(buf
, sizeof(buf
), "%s", _("Graphics"));
523 } else if (strcmp(p
, "Network") == 0) {
524 snprintf(buf
, sizeof(buf
), "%s", _("Network"));
526 } else if (strcmp(p
, "Office") == 0) {
527 snprintf(buf
, sizeof(buf
), "%s", _("Office"));
529 } else if (strcmp(p
, "Science") == 0) {
530 snprintf(buf
, sizeof(buf
), "%s", _("Science"));
532 } else if (strcmp(p
, "Settings") == 0) {
533 snprintf(buf
, sizeof(buf
), "%s", _("Settings"));
535 } else if (strcmp(p
, "System") == 0) {
536 snprintf(buf
, sizeof(buf
), "%s", _("System"));
538 } else if (strcmp(p
, "Utility") == 0) {
539 snprintf(buf
, sizeof(buf
), "%s", _("Utility"));
541 /* reserved categories */
542 } else if (strcmp(p
, "Screensaver") == 0) {
543 snprintf(buf
, sizeof(buf
), "%s", _("Screensaver"));
545 } else if (strcmp(p
, "TrayIcon") == 0) {
546 snprintf(buf
, sizeof(buf
), "%s", _("Tray Icon"));
548 } else if (strcmp(p
, "Applet") == 0) {
549 snprintf(buf
, sizeof(buf
), "%s", _("Applet"));
551 } else if (strcmp(p
, "Shell") == 0) {
552 snprintf(buf
, sizeof(buf
), "%s", _("Shell"));
555 p
= strtok(NULL
, ";");
559 if (!*buf
) /* come up with something if nothing found */
560 snprintf(buf
, sizeof(buf
), "%s", _("Other"));
562 *xdgmenuspec
= wstrdup(buf
);