Bug 30449

Summary: xdg-mime ought to use shared-mime-info
Product: Portland Reporter: Jim Duchek <jim.duchek>
Component: xdg-utilsAssignee: Portland Bugs <portland-bugs>
Status: RESOLVED WONTFIX QA Contact:
Severity: enhancement    
Priority: medium CC: danmbox, fabo
Version: 1.0   
Hardware: Other   
OS: All   
Whiteboard:
i915 platform: i915 features:
Attachments: A (partial) shared-mime-info using mimetype checker

Description Jim Duchek 2010-09-29 01:39:07 UTC
xdg-mime falls back (if not using GNOME or KDE) to 'file -i' to determine a file's type.  As there is no current way to get shared-mime-info to pop out a file(1)-compatible magic file, this leaves xdg-utils entirely separated from shared-mime-info, which is (I believe) freedesktop's preferred way of parsing mime types. 

Specific case, file(1)'s magic DB does not have a mime entry for vCalendar (ics) files (which is a file(1) problem, not xdg-utils), so 'xdg-mime query filetype test.ics' returns text/plain.
Comment 1 Rex Dieter 2010-11-18 16:06:30 UTC
would be nice, but there seems to be no shell-accessible api for shared-mime-info
Comment 2 Dan Muresan 2011-01-05 14:25:33 UTC
Yes, there is no shell-accessible api. However, how about using info_gnome or info_kde for determining the mime type? Either would be superior to "file -i".

I can understand the point about *default application* being different under KDE vs Gnome vs XFCE. But the Mime Type? 

Please, consider that there are people using Fluxbox (or even "worse"). There is no point in trying to make the scripts overly neutral (or whatever the thinking was that led to creating separate info_gnome, info_kde and info_generic functions).

The fix is simple: In info_generic, check

if which gvfs_info; then info_gnome "$@"
elif which kmimetypefinder; then info_kde "$@"
else
# use file -i
fi
Comment 3 Dan Muresan 2011-01-05 14:29:05 UTC
As a specific case, I'm using Fluxbox, and "file -i" fails for most video files, forcing xdg-mime (and xdg-open implicitly) to fall back on mailcap.

The idea behind xdg-open is good... Hopefully the implementation will improve.
Comment 4 Rex Dieter 2011-01-06 05:35:41 UTC
excellent points, using those non-native utilities is indeed better than getting stuff wrong.
Comment 5 Fathi Boudra 2011-01-06 07:59:26 UTC
Funny, some days ago I received a mail from Stefan Schneider-Kennedy for extending 'xdg-mime query filetype' to use fdo.org's shared-mime-info:

> I don't use Gnome or KDE, and hence when performing the 'xdg-mime query
> filetype file.extension' command xdg-mime falls back to the info_generic
> function. I also have the shared-mime-info package (also from
> freedesktop) installed and found it surprising that the command doesn't
> use this if it's available.
> 
> I wrote a short awk script which uses the globs2 list of file extensions
> from shared-mime-info to guess the filetype. This works better than the
> info_generic function in most cases. I haven't used the 'magic numbers'
> file yet, so it won't be able to distinguish, for example, between a file
> called *.pdf and an *actual* PDF file. The 'file' utility (used by info_generic)
> can do this in a number of cases.
> 
> I'm planning on getting it to use the magic numbers (which I believe
> should be possible with standard shell commands), and submitting a
> patch. Before I do so, I thought I'd see if you agree that querying of
> shared-mime-info doesn't belong in the 'file' utility. In my opinion the
> freedesktop.org stuff isn't part of the core utilities of a Unix distribution,
> whereas 'file' is. shared-mime-info's dependence on an XML library would
> also make joining the two less desirable.
> 
> I'm thinking of adding my stuff in as a new function, 'info_shared_mime'
> and placing it higher in the testing priority than info_generic (but
> below KDE and Gnome).

I'm pointing to this bug.
Comment 6 Per Olofsson 2011-01-14 01:47:15 UTC
I've filed #33094 with a patch for xdg-mime to use the "mimetype" command from File::MimeInfo.

Of course, this doesn't work unless you have File::MimeInfo installed. In Debian, I've made xdg-utils recommend this package so that it will be pulled in. For other distros, maybe some awk hack is needed. However, mimetype is most certainly better if available, because it uses all features of shared-mime-info including magic.
Comment 7 splondike 2011-01-28 17:09:42 UTC
Created attachment 42680 [details]
A (partial) shared-mime-info using mimetype checker

Hi, I'm the guy who wrote the email referenced in comment #5. I've finally got around to continuing where I left off, and this is my take on the situation.

I wrote a partial implementation of shared-mime-info's recommended mime type checking algorithm (http://standards.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html#id2554407) purely using standard shell utilities. My current implementation is attached, it doesn't yet use the subclasses file or the mask and byte swap options in the magic file.

While doing the glob and subclasses checking was/would be pretty easy, using the magic file (i.e. checking for binary matches) was a bit of a square peg/round hole situation.

I would suggest adding a patch similar to the one mentioned in comment #6 by Per, keeping info_generic as a fallback and not worrying about my solution. I think using File:MimeInfo would be acceptable to alternate DE users like me. The only situation where it wouldn't be I think is where Perl would not otherwise be installed.

Another solution would be to write a small-ish C program to implement the mime type checking. shared-mime-info has a mmappable file called mime.cache for this purpose.
Comment 8 Dan Muresan 2011-01-29 04:20:44 UTC
Yes, Per's solution in #33094 works very well and has solved my problems on Ubuntu Lucid. I don't think we need to worry about a desktop Debian install not having Perl (or an embedded system needing xdg-open to work flawlessly).

I hope that patch (and the related symlink patch) gets incorporated in the mainline.
Comment 9 Rex Dieter 2011-03-31 07:27:54 UTC
My take is supporting this stuff directly (sans shared-mime-info or the like providing a shell-usable api) is a rabbit-hole I'm not willing to go down.

That said, using mimeinfo.cache is still a possibility, see also bug #31629
Comment 10 Dan Muresan 2011-03-31 08:49:23 UTC
Rex, it is somewhat unclear what comment you're responding to. Probably referring to Comment 7.

From your other activity, I would summarize that you

1) accept Per Olofsson's solution (using mimetype)

2) reject other, lower level ways to access shared-mime-info

Which is OK with me. See also my comments in bug #31629 -- I think there's some confusion over there.
Comment 11 Rex Dieter 2011-03-31 10:04:04 UTC
correct. :)

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.