Search code examples
x11gnomewindow-managersfile-associationxdgutils

How do you make xdg-utils detect a file association for my application from a distribution package?


I am building a distribution package for xnec2c and I want .nec files to associate with xnec2c and also display the associated icon.

(This question is different than the many xdg-utils answers out there because it asks about packaging and whether xdg-* calls are actually necessary.)

I know I can register them with these:

xdg-mime install --mode system x-nec2c.xml

xdg-icon-resource install --mode system --context mimetypes --size 256 xnec2c.png application-x-nec2

xdg-mime default xnec2c.desktop # but `man` says not as root?
  • Should it be sufficient just to drop the .xml and .desktop and .png in the right place on the filesystem?
  • Does the post-install in the package need to run some or all of these commands?
  • I read here that the icon would be application-x-nec2 and not application/x-nec2. Is this correct?

I would like the files to work by placing them in the right spot without running the xdg-* tools if supported.

(The package is a .rpm, but type and distro shouldn't matter since xdg is a standard. I want the same basic process to work for .deb, too.)

Everything should associate as the mime type application/x-nec2 since "nec2" is the file format for ".nec" files. This is reflected in the .desktop and .xml definitions below unless I have an error. Please correct me if I have something out of place!

Here are the relevant files that deploy:

  • /usr/share/applications/xnec2c.desktop (see below)
  • /usr/share/mime/packages/x-nec2.xml (see below)
  • /usr/share/pixmaps/xnec2c.svg
  • /usr/share/icons/hicolor/256x256/apps/xnec2c.png
  • /usr/share/icons/hicolor/scalable/apps/xnec2c.svg

and here are is the content:

==> /usr/share/applications/xnec2c.desktop <==

[Desktop Entry]
Name=Xnec2c
GenericName=NEC2 Simulator
Comment=Numerical Electromagnetics Code software
Exec=xnec2c
Icon=xnec2c
Terminal=false
Type=Application
Categories=Science;
Keywords=nec2;em;simulator;
X-Desktop-File-Install-Version=0.22
MimeType=application/x-nec2;

==> /usr/share/mime/packages/x-nec2.xml <==

<?xml version="1.0"?>
<mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
    <mime-type type="application/x-nec2">
        <comment>NEC2 Antenna Simulation File</comment>
        <icon name="application-x-nec2"/>
        <glob-deleteall/>
        <glob pattern="*.nec"/>
    </mime-type>
</mime-info>

Solution

  • You don't need to run the mime-* tools, but you do need to update the desktop and icon associations for the freedesktop environment after install like so:

    update-mime-database /usr/share/mime
    update-desktop-database
    gtk-update-icon-cache /usr/share/icons/hicolor
    

    These should be run after uninstall, too, to cleanup after removal. In RPMs this is the %postun section.

    Substitute /usr/share with whatever deployment variable you might use like %{_datadir} for RPM .spec's.

    Also the icon name had an issue. It should have just been named 'xnec2c' not 'application-x-nec2' because the 'xnec2c' icon gets installed with the installer as follows:

    %{_datadir}/icons/hicolor/scalable/apps/%{name}.svg
    %{_datadir}/icons/hicolor/256x256/apps/%{name}.png