3

Similar to this question, I have some applications (Calibre, texdoc) open PDFs with Mendeley. Opening PDFs from Thunar, Thunderbird, Firefox etc. opens evince, the expected default.

It seems that those applications use xdg-open since:

$ xdg-mime query default application/pdf
mendeleydesktop.desktop

I tried to find where this comes from but was unsuccessful; I fixed it with

xdg-mime default evince.desktop application/pdf

The question remains: where did xdg-open get the idea that Mendeley should be the default PDF viewer from?

I'm using Ubuntu 16.04 with i3 4.11. xdg-open is at version 1.1.0 rc3.

2

The question remains: where did xdg-open get the idea that Mendeley should be the default PDF viewer from?

This is an eminently reasonable question. Here's a somewhat long answer in three parts.

Option 1: read the documentation

For example, the FreeDesktop standard on mimetype associations has this to say:

Association between MIME types and applications

Users, system administrators, application vendors and distributions can change associations between applications and mimetypes by writing into a file called mimeapps.list.

The lookup order for this file is as follows:

$XDG_CONFIG_HOME/$desktop-mimeapps.list            user overrides, desktop-specific (for advanced users)
$XDG_CONFIG_HOME/mimeapps.list                     user overrides (recommended location for user configuration GUIs)
$XDG_CONFIG_DIRS/$desktop-mimeapps.list            sysadmin and ISV overrides, desktop-specific
$XDG_CONFIG_DIRS/mimeapps.list                     sysadmin and ISV overrides
$XDG_DATA_HOME/applications/$desktop-mimeapps.list for completeness, deprecated, desktop-specific
$XDG_DATA_HOME/applications/mimeapps.list          for compatibility, deprecated
$XDG_DATA_DIRS/applications/$desktop-mimeapps.list distribution-provided defaults, desktop-specific
$XDG_DATA_DIRS/applications/mimeapps.list          distribution-provided defaults

In this table, $desktop is one of the names of the current desktop, lowercase (for instance, kde, gnome, xfce, etc.)

Note that if the environment variables such as XDG_CONFIG_HOME and XDG_DATA_HOME are not set, they will revert to their default values.

$XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.

$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

This illustrates one of the trickiest aspects of mimetype associations: they can be set in many different locations, and those settings might be overridden in a different location. However, ~/.config/mimeapps.list is the one that we should use to set our own associations.

This also matches the documentation for the GNOME desktop.

To override the system defaults for individual users, you need to create a ~/.config/mimeapps.list file with a list of MIME types for which you want to override the default registered application.

There's also this helpful tidbit:

You can use the gio mime command to verify that the default registered application has been set correctly:

$ gio mime text/html
Default application for “text/html”: myapplication1.desktop
Registered applications:
  myapplication1.desktop
  epiphany.desktop
Recommended applications:
  myapplication1.desktop
  epiphany.desktop

The cross-platform command to check mimetype associations is:

xdg-mime query default application/pdf

For GNOME, the command is:

gio mime application/pdf

For KDE Plasma the command is:

ktraderclient5 --mimetype application/pdf

When I look at my ~/.config/mimeapps.list file, it looks something like this:

[Added Associations]
application/epub+zip=calibre-ebook-viewer.desktop;org.gnome.FileRoller.desktop;
<snip>
application/pdf=evince.desktop;qpdfview.desktop;okularApplication_pdf.desktop;<snip>
<snip>
[Default Applications]
application/epub+zip=calibre-ebook-viewer.desktop
<snip>
application/pdf=evince.desktop;

You can see there only one entry for application/pdf under [Default Applications]; so evince.desktop is the default handler for PDF files. I don't have Mendeley installed, but one way to make it the default PDF handler is to put its desktop file here instead of evince.desktop.

Notice we're trusting the documentation here that ~/.config/mimeapps.list is the correct file; we don't actually know that for sure. We'll come back to this in part 3.

Option 2: read the source code.

xdg-open is a shell script that behaves differently depending on the value of $XDG_CURRENT_DESKTOP. You can see how this works here:

if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
  case "${XDG_CURRENT_DESKTOP}" in
     # only recently added to menu-spec, pre-spec X- still in use
     Cinnamon|X-Cinnamon)
       DE=cinnamon;
       ;;
     ENLIGHTENMENT)
       DE=enlightenment;
       ;;
     # GNOME, GNOME-Classic:GNOME, or GNOME-Flashback:GNOME
     GNOME*)
       DE=gnome;
       ;;
     KDE)
       DE=kde;
       ;;

Since you are using i3, the DE variable will be set to generic and the script will call its open_generic() function, which in turn will call either run-mailcap or mimeopen depending on what is installed.

Note that you can get some extra information by setting the XDG_UTILS_DEBUG_LEVEL, e.g.

XDG_UTILS_DEBUG_LEVEL=4 xdg-open ~/path/to/example.pdf

However, the debug information is not that informative for our purposes.

Option 3: trace the opened files.

From the previous investigations, we know that mimetype associations are stored in files somewhere on the hard drive, not e.g. as environment variables or dconf settings. This means we don't have to rely on documentation, we can use strace to determine what files the xdg-open command actually opens. For the application/pdf mimetype, we can use this:

strace -f -e trace=open,openat,creat -o strace_log.txt xdg-open /path/to/example.pdf

The -f is to trace child processes since xdg-open doesn't do everything by itself.

The -e trace=open,openat,creat is to trace just the syscalls open, openat, and creat. These are from the man page from man 2 open or online.

The -o strace_log.txt is to save to a log file to inspect later.

The output is somewhat voluminous, but we can ignore the lines that say ENOENT (No such file or directory) since these files do not exist.

You can also use other commands such as xdg-mime or gio mime. I found that gio mime read these files in my home directory:

  • ~/.local/share//mime/mime.cache
  • ~/.config/mimeapps.list
  • ~/.local/share/applications
  • ~/.local/share/applications/mimeapps.list
  • ~/.local/share/applications/defaults.list
  • ~/.local/share/applications/mimeinfo.cache

It also read these system-level files:

  • /usr/share/mime/mime.cache
  • /usr/share/applications/defaults.list
  • /usr/share/applications/mimeinfo.cache
  • /var/lib/snapd/desktop/applications
  • /var/lib/snapd/desktop/applications/mimeinfo.cache

To look for application/pdf associations, this should do the trick:

grep 'application/pdf' ~/.local/share//mime/mime.cache ~/.config/mimeapps.list ~/.local/share/applications ~/.local/share/applications/mimeapps.list ~/.local/share/applications/defaults.list ~/.local/share/applications/mimeinfo.cache /usr/share/mime/mime.cache /usr/share/applications/defaults.list /usr/share/applications/mimeinfo.cache /var/lib/snapd/desktop/applications /var/lib/snapd/desktop/applications/mimeinfo.cache | less

From here you can see where Mendeley's desktop file is getting added.

I have some applications (Calibre, texdoc) open PDFs with Mendeley. Opening PDFs from Thunar, Thunderbird, Firefox etc. opens evince, the expected default.

Firefox and Thunderbird have their own default application settings. I believe texdoc relies on xdg-open. I'm not sure about Thunar, but I doubt it is relying on xdg-open.

So ultimately this is probably due to:

  • xdg-open having different fallbacks than other applications on i3; and

  • Mendeley's installer adding mimetype associations in some files but not others.

Addendum: xdg-open should not use the mimeinfo.cache file on i3, but if you need to regenerate it, this is the command to use:

update-desktop-database ~/.local/share/applications

and here is the documentation:

Caching MIME Types

To make parsing of all the desktop files less costly, a update-desktop-database program is provided that will generate a cache file. The concept is identical to that of the 'update-mime-database' program in that it lets applications avoid reading in (potentially) hundreds of files. It will need to be run after every desktop file is installed. One cache file is created for every directory in $XDG_DATA_DIRS/applications/, and will create a file called $XDG_DATA_DIRS/applications/mimeinfo.cache.

https://specifications.freedesktop.org/desktop-entry-spec/0.9.5/ar01s07.html

Related:

| improve this answer | |
  • Thanks for this very elaborate answer! I will note that documentation and reality seem to be only mildly related. None of the environment variables contain any paths in my home directory, and yet an strace ... xdg-mime query ... list numerous files in there (similar to what you found). Also, what are these caches and when are they refreshed? Why does xdg-mime keep looking at files it knows it will ignore (for image/png, match in first file but four more files checked). All this has quite the over-engineered look to it. Sigh. – Raphael Jun 29 at 19:57
  • 1
    I wrote a little script based on option 3. – Raphael Jun 29 at 20:41
  • 1
    @Raphael I've update the question to explain this better, but briefly the environment variables have default values when they're not set, so xdg-open will still look in your home directory. Since they might be overridden later, xdg-mime can't skip any files. On your system, are you using run-mailcap or mimeopen? Also, I'm curious: where was the Mendeley file association stored? – Nathaniel M. Beaver Jul 14 at 14:07
  • 1
    Thanks for the clarifications! Regarding run-mailcap/mimeopen -- not consciously, but who knows? "where was the Mendeley file association stored?" -- No idea, I changed it years back. Maybe I'll remember to check when I re-install my machine. :) – Raphael Jul 14 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.