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
));
138 memset(buf
, 0, sizeof(buf
));
143 if (key
== NULL
) { /* not `key' = `value' */
144 memset(buf
, 0, sizeof(buf
));
148 if (strcmp(key
, "Type") == 0) {
149 getStringValue(&tmp
, p
);
150 if (strcmp(tmp
, "Application") != 0)
151 InGroup
= 0; /* if not application, skip current group */
154 } else if (strcmp(key
, "Name") == 0) {
155 getLocalizedStringValue(&xdg
->Name
, p
, &xdg
->MatchLevel
);
156 } else if (strcmp(key
, "NoDisplay") == 0) {
157 if (getBooleanValue(p
)) /* if nodisplay, skip current group */
159 } else if (strcmp(key
, "Hidden") == 0) {
160 if (getBooleanValue(p
))
161 InGroup
= 0; /* if hidden, skip current group */
162 } else if (strcmp(key
, "TryExec") == 0) {
163 getStringValue(&xdg
->TryExec
, p
);
164 } else if (strcmp(key
, "Exec") == 0) {
165 getStringValue(&xdg
->Exec
, p
);
166 } else if (strcmp(key
, "Path") == 0) {
167 getStringValue(&xdg
->Path
, p
);
168 } else if (strcmp(key
, "Terminal") == 0) {
169 if (getBooleanValue(p
))
170 xdg
->Flags
|= F_TERMINAL
;
171 } else if (strcmp(key
, "Categories") == 0) {
172 getStringValue(&xdg
->Category
, p
);
173 getMenuHierarchyFor(&xdg
->Category
);
182 /* at the end of the file, might as well try to menuize what we have
183 * unless there was no group at all or it was marked as hidden
185 if (InGroup
&& xdg_to_wm(&xdg
, &wm
))
186 (*addWMMenuEntryCallback
)(wm
);
191 /* coerce an xdg entry type into a wm entry type
193 static Bool
xdg_to_wm(XDGMenuEntry
**xdg
, WMMenuEntry
**wm
)
197 /* Exec or TryExec is mandatory */
198 if (!((*xdg
)->Exec
|| (*xdg
)->TryExec
))
201 /* if there's no Name, use the first word of Exec or TryExec
204 (*wm
)->Name
= (*xdg
)->Name
;
207 (*wm
)->Name
= wstrdup((*xdg
)->Exec
);
208 else /* (*xdg)->TryExec */
209 (*wm
)->Name
= wstrdup((*xdg
)->TryExec
);
211 p
= strchr((*wm
)->Name
, ' ');
217 (*wm
)->CmdLine
= (*xdg
)->Exec
;
218 else /* (*xdg)->TryExec */
219 (*wm
)->CmdLine
= (*xdg
)->TryExec
;
221 (*wm
)->SubMenu
= (*xdg
)->Category
;
222 (*wm
)->Flags
= (*xdg
)->Flags
;
227 /* (re-)initialize a XDGMenuEntry storage
229 static void init_xdg_storage(XDGMenuEntry
**xdg
)
235 wfree((*xdg
)->TryExec
);
238 if ((*xdg
)->Category
)
239 wfree((*xdg
)->Category
);
244 (*xdg
)->TryExec
= NULL
;
246 (*xdg
)->Category
= NULL
;
249 (*xdg
)->MatchLevel
= -1;
252 /* (re-)initialize a WMMenuEntry storage
254 static void init_wm_storage(WMMenuEntry
**wm
)
257 (*wm
)->CmdLine
= NULL
;
261 /* get a key from line. allocates target, which must be wfreed later */
262 static void getKey(char **target
, const char *line
)
269 if (strchr(p
, '=') == NULL
) { /* not `key' = `value' */
276 /* skip whitespace */
277 while (isspace(*(p
+ kstart
)))
280 /* skip up until first whitespace or '[' (localestring) or '=' */
282 while (*(p
+ kend
) && !isspace(*(p
+ kend
)) && *(p
+ kend
) != '=' && *(p
+ kend
) != '[')
285 *target
= wstrndup(p
+ kstart
, kend
- kstart
);
288 /* get a string value from line. allocates target, which must be wfreed later. */
289 static void getStringValue(char **target
, const char *line
)
297 /* skip until after '=' */
298 while (*(p
+ kstart
) && *(p
+ kstart
) != '=')
302 /* skip whitespace */
303 while (*(p
+ kstart
) && isspace(*(p
+ kstart
)))
306 *target
= wstrdup(p
+ kstart
);
309 /* get a localized string value from line. allocates target, which must be wfreed later.
310 * matching is dependent on the current value of target as well as on the
311 * level the current value is matched on. guts matching algorithm is in
312 * compare_matchlevel().
314 static void getLocalizedStringValue(char **target
, const char *line
, int *match_level
)
319 int sqbstart
, sqbend
;
327 /* skip until after '=', mark if '[' and ']' is found */
328 while (*(p
+ kstart
) && *(p
+ kstart
) != '=') {
329 switch (*(p
+ kstart
)) {
330 case '[': sqbstart
= kstart
+ 1;break;
331 case ']': sqbend
= kstart
; break;
338 /* skip whitespace */
339 while (isspace(*(p
+ kstart
)))
342 if (sqbstart
> 0 && sqbend
> sqbstart
)
343 locale
= wstrndup(p
+ sqbstart
, sqbend
- sqbstart
);
345 /* if there is no value yet and this is the default key, return */
346 if (!*target
&& !locale
) {
347 *match_level
= MATCH_DEFAULT
;
348 *target
= wstrdup(p
+ kstart
);
352 if (compare_matchlevel(match_level
, locale
)) {
354 *target
= wstrdup(p
+ kstart
);
361 /* get a boolean value from line */
362 static Bool
getBooleanValue(const char *line
)
367 getStringValue(&p
, line
);
368 ret
= strcmp(p
, "true") == 0 ? True
: False
;
374 /* perform locale matching by implementing the algorithm specified in
375 * xdg desktop entry specification, section "localized values for keys".
377 static Bool
compare_matchlevel(int *current_level
, const char *found_locale
)
379 /* current key locale */
380 char *key_lang
, *key_ctry
, *key_enc
, *key_mod
;
382 parse_locale(found_locale
, &key_lang
, &key_ctry
, &key_enc
, &key_mod
);
384 if (env_lang
&& key_lang
&& /* Shortcut: if key and env languages don't match, */
385 strcmp(env_lang
, key_lang
) != 0) /* don't even bother. This takes care of the great */
386 return False
; /* majority of the cases without having to go through */
387 /* the more theoretical parts of the spec'd algo. */
389 if (!env_mod
&& key_mod
) /* If LC_MESSAGES does not have a MODIFIER field, */
390 return False
; /* then no key with a modifier will be matched. */
392 if (!env_ctry
&& key_ctry
) /* Similarly, if LC_MESSAGES does not have a COUNTRY field, */
393 return False
; /* then no key with a country specified will be matched. */
395 /* LC_MESSAGES value: lang_COUNTRY@MODIFIER */
396 if (env_lang
&& env_ctry
&& env_mod
) { /* lang_COUNTRY@MODIFIER */
397 if (key_lang
&& key_ctry
&& key_mod
&&
398 strcmp(env_lang
, key_lang
) == 0 &&
399 strcmp(env_ctry
, key_ctry
) == 0 &&
400 strcmp(env_mod
, key_mod
) == 0) {
401 *current_level
= MATCH_LANG_COUNTRY_MODIFIER
;
403 } else if (key_lang
&& key_ctry
&& /* lang_COUNTRY */
404 strcmp(env_lang
, key_lang
) == 0 &&
405 strcmp(env_ctry
, key_ctry
) == 0 &&
406 *current_level
< MATCH_LANG_COUNTRY
) {
407 *current_level
= MATCH_LANG_COUNTRY
;
409 } else if (key_lang
&& key_mod
&& /* lang@MODIFIER */
410 strcmp(env_lang
, key_lang
) == 0 &&
411 strcmp(env_mod
, key_mod
) == 0 &&
412 *current_level
< MATCH_LANG_MODIFIER
) {
413 *current_level
= MATCH_LANG_MODIFIER
;
415 } else if (key_lang
&& /* lang */
416 strcmp(env_lang
, key_lang
) == 0 &&
417 *current_level
< MATCH_LANG
) {
418 *current_level
= MATCH_LANG
;
425 /* LC_MESSAGES value: lang_COUNTRY */
426 if (env_lang
&& env_ctry
) { /* lang_COUNTRY */
427 if (key_lang
&& key_ctry
&&
428 strcmp(env_lang
, key_lang
) == 0 &&
429 strcmp(env_ctry
, key_ctry
) == 0 &&
430 *current_level
< MATCH_LANG_COUNTRY
) {
431 *current_level
= MATCH_LANG_COUNTRY
;
433 } else if (key_lang
&& /* lang */
434 strcmp(env_lang
, key_lang
) == 0 &&
435 *current_level
< MATCH_LANG
) {
436 *current_level
= MATCH_LANG
;
443 /* LC_MESSAGES value: lang@MODIFIER */
444 if (env_lang
&& env_mod
) { /* lang@MODIFIER */
445 if (key_lang
&& key_mod
&&
446 strcmp(env_lang
, key_lang
) == 0 &&
447 strcmp(env_mod
, key_mod
) == 0 &&
448 *current_level
< MATCH_LANG_MODIFIER
) {
449 *current_level
= MATCH_LANG_MODIFIER
;
451 } else if (key_lang
&& /* lang */
452 strcmp(env_lang
, key_lang
) == 0 &&
453 *current_level
< MATCH_LANG
) {
454 *current_level
= MATCH_LANG
;
461 /* LC_MESSAGES value: lang */
462 if (env_lang
) { /* lang */
464 strcmp(env_lang
, key_lang
) == 0 &&
465 *current_level
< MATCH_LANG
) {
466 *current_level
= MATCH_LANG
;
473 /* MATCH_DEFAULT is handled in getLocalizedStringValue */
478 /* get the (first) xdg main category from a list of categories
480 static void getMenuHierarchyFor(char **xdgmenuspec
)
485 if (!*xdgmenuspec
|| !**xdgmenuspec
)
488 category
= wstrdup(*xdgmenuspec
);
490 memset(buf
, 0, sizeof(buf
));
492 p
= strtok(category
, ";");
493 while (p
) { /* get a known category */
494 if (strcmp(p
, "AudioVideo") == 0) {
495 snprintf(buf
, sizeof(buf
), "%s", _("Audio & Video"));
497 } else if (strcmp(p
, "Audio") == 0) {
498 snprintf(buf
, sizeof(buf
), "%s", _("Audio"));
500 } else if (strcmp(p
, "Video") == 0) {
501 snprintf(buf
, sizeof(buf
), "%s", _("Video"));
503 } else if (strcmp(p
, "Development") == 0) {
504 snprintf(buf
, sizeof(buf
), "%s", _("Development"));
506 } else if (strcmp(p
, "Education") == 0) {
507 snprintf(buf
, sizeof(buf
), "%s", _("Education"));
509 } else if (strcmp(p
, "Game") == 0) {
510 snprintf(buf
, sizeof(buf
), "%s", _("Game"));
512 } else if (strcmp(p
, "Graphics") == 0) {
513 snprintf(buf
, sizeof(buf
), "%s", _("Graphics"));
515 } else if (strcmp(p
, "Network") == 0) {
516 snprintf(buf
, sizeof(buf
), "%s", _("Network"));
518 } else if (strcmp(p
, "Office") == 0) {
519 snprintf(buf
, sizeof(buf
), "%s", _("Office"));
521 } else if (strcmp(p
, "Settings") == 0) {
522 snprintf(buf
, sizeof(buf
), "%s", _("Settings"));
524 } else if (strcmp(p
, "System") == 0) {
525 snprintf(buf
, sizeof(buf
), "%s", _("System"));
527 } else if (strcmp(p
, "Utility") == 0) {
528 snprintf(buf
, sizeof(buf
), "%s", _("Utility"));
531 p
= strtok(NULL
, ";");
535 if (!*buf
) /* come up with something if nothing found */
536 snprintf(buf
, sizeof(buf
), "%s", _("Applications"));
538 *xdgmenuspec
= wstrdup(buf
);