]> pere.pagekite.me Git - homepage.git/blob - blog/data/2023-01-29-debian-mime-desktop-test.txt
Generated.
[homepage.git] / blog / data / 2023-01-29-debian-mime-desktop-test.txt
1 Title: Is the desktop recommending your program for opening its files?
2 Tags: english, debian
3 Date: 2023-01-29 11:00
4
5 <p>Linux desktop systems
6 <a href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">have
7 standardized</a> how programs present themselves to the desktop
8 system. If a package include a .desktop file in
9 /usr/share/applications/, Gnome, KDE, LXDE, Xfce and the other desktop
10 environments will pick up the file and use its content to generate the
11 menu of available programs in the system. A lesser known fact is that
12 a package can also explain to the desktop system how to recognize the
13 files created by the program in question, and use it to open these
14 files on request, for example via a GUI file browser.</p>
15
16 <p>A while back I ran into a package that did not tell the desktop
17 system how to recognize its files and was not used to open its files
18 in the file browser and fixed it. In the process I wrote a simple
19 debian/tests/ script to ensure the setup keep working. It might be
20 useful for other packages too, to ensure any future version of the
21 package keep handling its own files.</p>
22
23 <p>For this to work the file format need a useful MIME type that can
24 be used to identify the format. If the file format do not yet have a
25 MIME type, it should define one and preferably also
26 <a href="https://www.iana.org/assignments/media-types/media-types.xhtml">register
27 it with IANA</a> to ensure the MIME type string is reserved.</p>
28
29 <p>The script uses the <tt>xdg-mime</tt> program from xdg-utils to
30 query the database of standardized package information and ensure it
31 return sensible values. It also need the location of an example file
32 for xdg-mime to guess the format of.</p>
33
34 <pre>
35 #!/bin/sh
36 #
37 # Author: Petter Reinholdtsen
38 # License: GPL v2 or later at your choice.
39 #
40 # Validate the MIME setup, making sure motor types have
41 # application/vnd.openmotor+yaml associated with them and is connected
42 # to the openmotor desktop file.
43
44 retval=0
45
46 mimetype="application/vnd.openmotor+yaml"
47 testfile="test/data/real/o3100/motor.ric"
48 mydesktopfile="openmotor.desktop"
49
50 filemime="$(xdg-mime query filetype "$testfile")"
51
52 if [ "$mimetype" != "$filemime" ] ; then
53 retval=1
54 echo "error: xdg-mime claim motor file MIME type is $filemine, not $mimetype"
55 else
56 echo "success: xdg-mime report correct mime type $mimetype for motor file"
57 fi
58
59 desktop=$(xdg-mime query default "$mimetype")
60
61 if [ "$mydesktopfile" != "$desktop" ]; then
62 retval=1
63 echo "error: xdg-mime claim motor file should be handled by $desktop, not $mydesktopfile"
64 else
65 echo "success: xdg-mime agree motor file should be handled by $mydesktopfile"
66 fi
67
68 exit $retval
69 </pre>
70
71 <p>It is a simple way to ensure your users are not very surprised when
72 they try to open one of your file formats in their file browser.</p>
73
74 <p>As usual, if you use Bitcoin and want to show your support of my
75 activities, please send Bitcoin donations to my address
76 <b><a href="bitcoin:15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b">15oWEoG9dUPovwmUL9KWAnYRtNJEkP1u1b</a></b>.</p>