GNOME Bugzilla – Bug 139974
a .desktop file parser
Last modified: 2011-02-18 16:09:28 UTC
.desktop files are an important part of most applications now. It would be useful if glib provided a means for saying "this .desktop file belongs to this program". Then it could automatically determine the application name from the .desktop file, and higher level libraries could use other information from the .desktop file (for instance gtk+ could set the application icon, etc).
Created attachment 26635 [details] [review] .desktop file parsing functions The attached patch provides functions for parsing .desktop files as well as functions for associating a .desktop file with an application. Note this patch depends on the patches in bug 139971 and bug 139973.
Note this is sitting in libegg now with fixes and api improvements as libegg/desktopentries Still needs more testing and fixed documentation, though.
Discussion with Anders has lead to the following tasks: - change egg_desktop_entries_ to g_desktop_entry_ - make parse_data and flush_parse_buffer static - add from_data constructor - make add_entry and add_group static - provide set methods that mirror the get methods (which would called add_entry and add_group under the hood) - make new_from_file not search $XDG_DATA_DIRS by default and provide g_desktop_entry_new_from_data_dirs for searching from $XDG_DATA_DIRS.
Comment on attachment 26635 [details] [review] .desktop file parsing functions Ray, did you ever produce an updated patch ?
I've updated the code in libegg. Over the weekend I'll pull the code from libegg and produce a patch applyable to glib.
Created attachment 32715 [details] [review] Desktop Entry File Parser This is the latest incarnation of the Desktop Entry parser. It uses some rather new glib apis, including g_get_*_data_dirs() and g_get_language_names()
Notes from a very quick discussion with owen: - a lot of code, 3000 lines - having some unit tests would be very nice - we have a ~1000 line parser for .icon files which also use the .ini file syntax and have things like localized string lists. it would be very nice, if it was possible to make gtk+ use the new glib code for its .ini file parsing needs
- it is a fairly sizable chunk of code, but on the other hand it has had a reasonable amount of testing and bugfixes because the libegg code is used by gnome-vfs and desktop-file-utils. - unit tests would definitely be a good idea. It may take a while for me to write a good complete set of tests, however. - jrb mentioned the .icon parser to me before and I think it would be a very good idea to switch it over, rather than keeping duplicate functionality in the library stack.
So it turns out that what we are actually adding here is not a desktop entry parser, but rather a generic parser for the ini file format (possibly with some restrictions/extension compared to the "true" win32 ini format). I think it would be a good idea to make this clear by renaming the api to something more generic, e.g g_config_... or g_ini_file_...
I think it would be good to remove the special casing of legal_start_groups == NULL, which is currently tailored towards desktop entries. Instead, I would propose to let legal_start_groups == NULL mean that there is no restriction. For parsing desktop entries, one would then have to set the legal start groups explicitly.
Discussion from gtk team meeting: - Name should be GKeyFile - While the format is similiar to .ini files, it does have some significant differences (comment syntax, translated keys, etc), so it shouldn't aim to support .ini files. - legal_start_groups stuff should be removed. If the user of the API cares what the first group is they can call get_groups and look. - Legacy/Mixed encoding support should be removed. It adds significant complexity and a seldom-used code path. - should default to discarding comments and translations to optimize for the read case which is the most common. - should drop the new_from_file and new_from_data constructors and add load_from_file and load_from_data functions - should remove GError's from the setters--they can't fail.
More discussion led to: - Don't escape tabs and spaces in the middle of values . Still need to escape spaces and tabs at the beginning and end of values . Should probably escape newline and carriage return regardless of where they are in the string - set_value and get_value were made static in favor of an escape_strings on/off toggle function. That change should be reverted. - The latest version of the desktop entry specification supports escaping ; for lists with \; - Provide method for specifying list delimiter because icon theme spec uses , instead of ;
I committed my latest version, which incorporates all the recent discussion.