2 #---------------------------------------------
5 # Utility script to manipulate MIME related information
6 # on XDG compliant systems.
8 # Refer to the usage() function below for usage.
10 # Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
11 # Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
12 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
13 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
17 # Permission is hereby granted, free of charge, to any person obtaining a
18 # copy of this software and associated documentation files (the "Software"),
19 # to deal in the Software without restriction, including without limitation
20 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 # and/or sell copies of the Software, and to permit persons to whom the
22 # Software is furnished to do so, subject to the following conditions:
24 # The above copyright notice and this permission notice shall be included
25 # in all copies or substantial portions of the Software.
27 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 # OTHER DEALINGS IN THE SOFTWARE.
35 #---------------------------------------------
42 xdg-mime - command line tool for querying information about file type handling
43 and adding descriptions for new file types
47 xdg-mime query { filetype | default } ...
49 xdg-mime default application mimetype(s)
51 xdg-mime install [--mode mode] [--novendor] mimetypes-file
53 xdg-mime uninstall [--mode mode] mimetypes-file
55 xdg-mime { --help | --manual | --version }
59 The xdg-mime program can be used to query information about file types and to
60 add descriptions for new file types.
66 Returns information related to file types.
68 The query option is for use inside a desktop session only. It is not
69 recommended to use xdg-mime query as root.
71 The following queries are supported:
73 query filetype FILE: Returns the file type of FILE in the form of a MIME
76 query default mimetype: Returns the default application that the desktop
77 environment uses for opening files of type mimetype. The default
78 application is identified by its *.desktop file.
82 Ask the desktop environment to make application the default application for
83 opening files of type mimetype. An application can be made the default for
84 several file types by specifying multiple mimetypes.
86 application is the desktop file id of the application and has the form
87 vendor-name.desktop application must already be installed in the desktop
88 menu before it can be made the default handler. The aplication's desktop
89 file must list support for all the MIME types that it wishes to be the
92 Requests to make an application a default handler may be subject to system
93 policy or approval by the end-user. xdg-mime query can be used to verify
94 whether an application is the actual default handler for a specific file
97 The default option is for use inside a desktop session only. It is not
98 recommended to use xdg-mime default as root.
101 Adds the file type descriptions provided in mimetypes-file to the desktop
102 environment. mimetypes-file must be a XML file that follows the
103 freedesktop.org Shared MIME-info Database specification and that has a
104 mime-info element as its document root. For each new file type one or more
105 icons with name type-subtype must be installed with the xdg-icon-resource
106 command in the mimetypes context. For example the filetype application/
107 vnd.oasis.opendocument.text requires an icon named
108 application-vnd.oasis.opendocument.text to be installed (unless the file
109 type recommends another icon name).
111 Removes the file type descriptions provided in mimetypes-file and
112 previously added with xdg-mime install from the desktop environment.
113 mimetypes-file must be a XML file that follows the freedesktop.org Shared
114 MIME-info Database specification and that has a mime-info element as its
121 mode can be user or system. In user mode the file is (un)installed for the
122 current user only. In system mode the file is (un)installed for all users
123 on the system. Usually only root is allowed to install in system mode.
125 The default is to use system mode when called by root and to use user mode
126 when called by a non-root user.
130 Normally, xdg-mime checks to ensure that the mimetypes-file to be installed
131 has a proper vendor prefix. This option can be used to disable that check.
133 A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated
134 with a dash ("-"). Companies and organizations are encouraged to use a word
135 or phrase, preferably the organizations name, for which they hold a
136 trademark as their vendor prefix. The purpose of the vendor prefix is to
137 prevent name conflicts.
140 Show command synopsis.
142 Show this manualpage.
144 Show the xdg-utils version information.
146 Environment Variables
148 xdg-mime honours the following environment variables:
150 XDG_UTILS_DEBUG_LEVEL
151 Setting this environment variable to a non-zero numerical value makes
152 xdg-mime do more verbose reporting on stderr. Setting a higher value
153 increases the verbosity.
154 XDG_UTILS_INSTALL_MODE
155 This environment variable can be used by the user or administrator to
156 override the installation mode. Valid values are user and system.
160 An exit code of 0 indicates success while a non-zero exit code indicates
161 failure. The following failure codes can be returned:
164 Error in command line syntax.
166 One of the files passed on the command line did not exist.
168 A required tool could not be found.
172 No permission to read one of the files passed on the command line.
176 xdg-icon-resource(1), xdg-desktop-menu(1)
180 xdg-mime query filetype /tmp/foobar.png
182 Prints the MIME type of the file /tmp/foobar.png, in this case image/png
184 xdg-mime query default image/png
186 Prints the .desktop filename of the application which is registered to open PNG
189 xdg-mime install shinythings-shiny.xml
191 Adds a file type description for "shiny"-files. "shinythings-" is used as the
192 vendor prefix. The file type description could look as folows.
194 shinythings-shiny.xml:
196 <?xml version="1.0"?>
197 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
198 <mime-type type="text/x-shiny">
199 <comment>Shiny new file type</comment>
200 <glob pattern="*.shiny"/>
201 <glob pattern="*.shi"/>
205 An icon for this new file type must also be installed, for example with:
207 xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny
215 xdg-mime - command line tool for querying information about file type handling
216 and adding descriptions for new file types
220 xdg-mime query { filetype | default } ...
222 xdg-mime default application mimetype(s)
224 xdg-mime install [--mode mode] [--novendor] mimetypes-file
226 xdg-mime uninstall [--mode mode] mimetypes-file
228 xdg-mime { --help | --manual | --version }
235 #----------------------------------------------------------------------------
236 # Common utility functions included in all XDG wrapper scripts
237 #----------------------------------------------------------------------------
241 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
242 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
247 # This handles backslashes but not quote marks.
254 #-------------------------------------------------------------
255 # map a binary to a .desktop file
256 binary_to_desktop_file()
258 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
259 binary="`which "$1"`"
260 binary="`readlink -f "$binary"`"
261 base="`basename "$binary"`"
263 for dir in $search; do
265 [ "$dir" ] || continue
266 [ -d "$dir/applications" -o -d "$dir/applnk" ] || continue
267 for file in "$dir"/applications/*.desktop "$dir"/applications/*/*.desktop "$dir"/applnk/*.desktop "$dir"/applnk/*/*.desktop; do
268 [ -r "$file" ] || continue
269 # Check to make sure it's worth the processing.
270 grep -q "^Exec.*$base" "$file" || continue
271 # Make sure it's a visible desktop file (e.g. not "preferred-web-browser.desktop").
272 grep -Eq "^(NoDisplay|Hidden)=true" "$file" && continue
273 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
274 command="`which "$command"`"
275 if [ x"`readlink -f "$command"`" = x"$binary" ]; then
276 # Fix any double slashes that got added path composition
277 echo "$file" | sed -e 's,//*,/,g'
284 #-------------------------------------------------------------
285 # map a .desktop file to a binary
286 ## FIXME: handle vendor dir case
287 desktop_file_to_binary()
289 search="${XDG_DATA_HOME:-$HOME/.local/share}:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
290 desktop="`basename "$1"`"
292 for dir in $search; do
294 [ "$dir" -a -d "$dir/applications" ] || continue
295 file="$dir/applications/$desktop"
296 [ -r "$file" ] || continue
297 # Remove any arguments (%F, %f, %U, %u, etc.).
298 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
299 command="`which "$command"`"
300 readlink -f "$command"
305 #-------------------------------------------------------------
306 # Exit script on successfully completing the desired operation
310 if [ $# -gt 0 ]; then
319 #-----------------------------------------
320 # Exit script on malformed arguments, not enough arguments
321 # or missing required option.
322 # prints usage information
324 exit_failure_syntax()
326 if [ $# -gt 0 ]; then
327 echo "xdg-mime: $@" >&2
328 echo "Try 'xdg-mime --help' for more information." >&2
331 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
337 #-------------------------------------------------------------
338 # Exit script on missing file specified on command line
340 exit_failure_file_missing()
342 if [ $# -gt 0 ]; then
343 echo "xdg-mime: $@" >&2
349 #-------------------------------------------------------------
350 # Exit script on failure to locate necessary tool applications
352 exit_failure_operation_impossible()
354 if [ $# -gt 0 ]; then
355 echo "xdg-mime: $@" >&2
361 #-------------------------------------------------------------
362 # Exit script on failure returned by a tool application
364 exit_failure_operation_failed()
366 if [ $# -gt 0 ]; then
367 echo "xdg-mime: $@" >&2
373 #------------------------------------------------------------
374 # Exit script on insufficient permission to read a specified file
376 exit_failure_file_permission_read()
378 if [ $# -gt 0 ]; then
379 echo "xdg-mime: $@" >&2
385 #------------------------------------------------------------
386 # Exit script on insufficient permission to write a specified file
388 exit_failure_file_permission_write()
390 if [ $# -gt 0 ]; then
391 echo "xdg-mime: $@" >&2
399 if [ ! -e "$1" ]; then
400 exit_failure_file_missing "file '$1' does not exist"
402 if [ ! -r "$1" ]; then
403 exit_failure_file_permission_read "no permission to read file '$1'"
407 check_vendor_prefix()
410 [ -n "$file_label" ] || file_label="filename"
418 echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&2
419 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
420 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
421 echo "Use --novendor to override or 'xdg-mime --manual' for additional info." >&2
427 # if the file exists, check if it is writeable
428 # if it does not exists, check if we are allowed to write on the directory
430 if [ ! -w "$1" ]; then
431 exit_failure_file_permission_write "no permission to write to file '$1'"
435 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
436 exit_failure_file_permission_write "no permission to create file '$1'"
441 #----------------------------------------
442 # Checks for shared commands, e.g. --help
444 check_common_commands()
446 while [ $# -gt 0 ] ; do
453 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
463 echo "xdg-mime 1.1.0 rc1"
470 check_common_commands "$@"
472 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
473 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
475 xdg_redirect_output=" > /dev/null 2> /dev/null"
477 # All output to stderr
478 xdg_redirect_output=" >&2"
481 #--------------------------------------
482 # Checks for known desktop environments
483 # set variable DE to the desktop environments name, lowercase
487 # see https://bugs.freedesktop.org/show_bug.cgi?id=34164
490 if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
491 case "${XDG_CURRENT_DESKTOP}" in
506 if [ x"$DE" = x"" ]; then
508 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
509 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
510 elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
511 elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
512 elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
516 if [ x"$DE" = x"" ]; then
517 # fallback to checking $DESKTOP_SESSION
518 case "$DESKTOP_SESSION" in
531 if [ x"$DE" = x"" ]; then
532 # fallback to uname output for other platforms
533 case "$(uname 2>/dev/null)" in
540 if [ x"$DE" = x"gnome" ]; then
541 # gnome-default-applications-properties is only available in GNOME 2.x
542 # but not in GNOME 3.x
543 which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome3"
547 #----------------------------------------------------------------------------
548 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
549 # It also always returns 1 in KDE 3.4 and earlier
550 # Simply return 0 in such case
552 kfmclient_fix_exit_code()
554 version=`LC_ALL=C.UTF-8 kde-config --version 2>/dev/null | grep '^KDE'`
555 major=`echo $version | sed 's/KDE.*: \([0-9]\).*/\1/'`
556 minor=`echo $version | sed 's/KDE.*: [0-9]*\.\([0-9]\).*/\1/'`
557 release=`echo $version | sed 's/KDE.*: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
558 test "$major" -gt 3 && return $1
559 test "$minor" -gt 5 && return $1
560 test "$release" -gt 4 && return $1
564 update_mime_database()
566 if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
568 if [ x"$DE" = x"kde" ] ; then
569 DEBUG 1 "Running kbuildsycoca"
570 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
571 eval 'kbuildsycoca4'$xdg_redirect_output
573 eval 'kbuildsycoca'$xdg_redirect_output
577 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
578 if [ -x $x/update-mime-database ] ; then
579 DEBUG 1 "Running $x/update-mime-database $1"
580 eval '$x/update-mime-database $1'$xdg_redirect_output
588 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
589 DEBUG 1 "Running kmimetypefinder \"$1\""
590 kmimetypefinder "$1" 2>/dev/null | head -n 1
592 DEBUG 1 "Running kfile \"$1\""
593 kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
596 if [ $? -eq 0 ]; then
599 exit_failure_operation_failed
605 if gvfs-info --help 2>/dev/null 1>&2; then
606 DEBUG 1 "Running gvfs-info \"$1\""
607 gvfs-info "$1" 2> /dev/null | grep standard::content-type | cut -d' ' -f4
608 elif gnomevfs-info --help 2>/dev/null 1>&2; then
609 DEBUG 1 "Running gnomevfs-info \"$1\""
610 gnomevfs-info --slow-mime "$1" 2> /dev/null | grep "^MIME" | cut -d ":" -f 2 | sed s/"^ "//
612 # according to https://bugs.freedesktop.org/show_bug.cgi?id=33094#c5
613 # neither gvfs-info or gnomevfs-info are present in a default Ubuntu Natty
614 # install, so fallback to info_generic
618 if [ $? -eq 0 ]; then
621 exit_failure_operation_failed
627 if mimetype --version >/dev/null 2>&1; then
628 DEBUG 1 "Running mimetype -b \"$1\""
631 DEBUG 1 "Running file --mime-type \"$1\""
632 /usr/bin/file --mime-type "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
635 if [ $? -eq 0 ]; then
638 exit_failure_operation_failed
644 # $1 is vendor-name.desktop
647 # On KDE 3, add to $KDE_CONFIG_PATH/profilerc:
651 # Remove all [$2 - *] sections, or even better,
652 # renumber [$2 - *] sections and remove duplicate
654 # On KDE 4, add $2=$1 to $XDG_DATA_APPS/mimeapps.list
658 # [Added Associations]
659 # text/plain=kde4-kate.desktop;kde4-kwrite.desktop;
661 # [Removed Associations]
662 # text/plain=gnome-gedit.desktop;gnu-emacs.desktop;
665 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
666 default_dir=`kde4-config --path xdgdata-apps 2> /dev/null | cut -d ':' -f 1`
667 default_file="$default_dir/mimeapps.list"
669 default_dir=`kde-config --path config 2> /dev/null | cut -d ':' -f 1`
670 default_file="$default_dir/profilerc"
672 if [ -z "$default_dir" ]; then
673 DEBUG 2 "make_default_kde: No kde runtime detected"
676 DEBUG 2 "make_default_kde $vendor $mimetype"
677 DEBUG 1 "Updating $default_file"
678 mkdir -p "$default_dir"
679 [ -f "$default_file" ] || touch "$default_file"
680 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
681 [ -f "$default_file" ] || touch "$default_file"
682 awk -v application="$vendor" -v mimetype="$mimetype" '
691 if (index($0, "[Added Associations]") == 1) {
693 } else if (index($0, "[") == 1) {
694 if (associations && !found) {
695 print prefix application
699 } else if ($0 == "") {
702 } else if (associations && index($0, prefix) == 1) {
703 value=substr($0, length(prefix) + 1, length)
704 split(value, apps, ";")
705 value=application ";"
710 for (i=0; i < count; i++) {
711 if (apps[i] != application && apps[i] != "") {
712 value=value apps[i] ";"
729 print "[Added Associations]"
731 print prefix application
738 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
739 eval 'kbuildsycoca4'$xdg_redirect_output
741 awk -v application="$vendor" -v mimetype="$mimetype" '
743 header_start="[" mimetype " - "
747 if (index($0, header_start) == 1 )
750 if (/^\[/) { suppress=0 }
758 print "[" mimetype " - 1]"
759 print "Application=" application
760 print "AllowAsDefault=true"
761 print "GenericServiceType=Application"
763 print "ServiceType=" mimetype
765 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
769 make_default_generic()
771 # $1 is vendor-name.desktop
773 # Add $2=$1 to XDG_DATA_HOME/applications/mimeapps.list
774 xdg_user_dir="$XDG_DATA_HOME"
775 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
776 default_file="$xdg_user_dir/applications/mimeapps.list"
777 DEBUG 2 "make_default_generic $1 $2"
778 DEBUG 1 "Updating $default_file"
779 [ -f "$default_file" ] || touch "$default_file"
780 awk -v mimetype="$2" -v application="$1" '
790 if (index($0, "[Default Applications]") == 1) {
793 } else if (index($0, "[") == 1) {
794 if (!added && indefault) {
795 print prefix application
799 } else if ($0 == "") {
802 } else if (indefault && !added && index($0, prefix) == 1) {
803 $0=prefix application
818 print "[Default Applications]"
820 print prefix application
827 ' "$default_file" > "${default_file}.new" && mv "${default_file}.new" "$default_file"
833 xdg_user_dir="$XDG_DATA_HOME"
834 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
835 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
836 xdg_system_dirs="$XDG_DATA_DIRS"
837 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
839 for x in `echo "$xdg_user_dir" | sed 's/:/ /g'`; do
840 mimeapps_list="$x/applications/mimeapps.list"
841 if [ -f "$mimeapps_list" ] ; then
842 DEBUG 2 "Checking $mimeapps_list"
843 trader_result=`awk -v mimetype="$MIME" '
850 if (index($0, "[Default Applications]") == 1) {
852 } else if (index($0, "[") == 1) {
854 } else if (!found && indefault && index($0, prefix) == 1) {
855 print substr($0, length(prefix) +1, length)
860 if [ -n "$trader_result" ] ; then
867 for x in `echo "$xdg_system_dirs" | sed 's/:/ /g'`; do
868 DEBUG 2 "Checking $x/applications/defaults.list"
869 trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
870 if [ -n "$trader_result" ] ; then
881 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
882 KTRADER=`which ktraderclient 2> /dev/null`
883 MIMETYPE="--mimetype"
884 SERVICETYPE="--servicetype"
886 KTRADER=`which ktradertest 2> /dev/null`
888 if [ -n "$KTRADER" ] ; then
889 DEBUG 1 "Running KDE trader query \"$MIME\" mimetype and \"Application\" servicetype"
890 trader_result=`$KTRADER $MIMETYPE "$MIME" $SERVICETYPE Application 2>/dev/null \
891 | grep DesktopEntryPath | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
892 if [ -n "$trader_result" ] ; then
893 basename "$trader_result"
896 exit_failure_operation_failed
903 [ x"$1" != x"" ] || exit_failure_syntax
922 if [ -z "$1" ] ; then
923 exit_failure_syntax "query type argument missing"
931 if [ -z "$filename" ] ; then
932 exit_failure_syntax "FILE argument missing"
936 exit_failure_syntax "unexpected option '$filename'"
939 check_input_file "$filename"
940 filename=`readlink -f -- "$filename"`
946 if [ -z "$mimetype" ] ; then
947 exit_failure_syntax "mimetype argument missing"
951 exit_failure_syntax "unexpected option '$mimetype'"
959 exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
965 exit_failure_syntax "unknown query type '$1'"
974 if [ -z "$1" ] ; then
975 exit_failure_syntax "application argument missing"
979 exit_failure_syntax "unexpected option '$1'"
987 exit_failure_syntax "malformed argument '$1', expected *.desktop"
993 exit_failure_syntax "unknown command '$1'"
1000 if [ "$action" = "makedefault" ]; then
1001 if [ -z "$1" ] ; then
1002 exit_failure_syntax "mimetype argument missing"
1005 while [ $# -gt 0 ] ; do
1008 exit_failure_syntax "unexpected option '$1'"
1014 make_default_kde "$filename" "$mimetype"
1015 make_default_generic "$filename" "$mimetype"
1020 if [ "$action" = "info" ]; then
1023 if [ x"$DE" = x"" ]; then
1024 if [ -x /usr/bin/file ]; then
1031 info_kde "$filename"
1035 info_gnome "$filename"
1039 info_generic "$filename"
1042 exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
1045 if [ "$action" = "defapp" ]; then
1050 defapp_kde "$mimetype"
1054 defapp_generic "$mimetype"
1057 exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
1061 while [ $# -gt 0 ] ; do
1067 if [ -z "$1" ] ; then
1068 exit_failure_syntax "mode argument missing for --mode"
1080 exit_failure_syntax "unknown mode '$1'"
1091 exit_failure_syntax "unexpected option '$parm'"
1095 if [ -n "$filename" ] ; then
1096 exit_failure_syntax "unexpected argument '$parm'"
1100 check_input_file "$filename"
1105 if [ -z "$action" ] ; then
1106 exit_failure_syntax "command argument missing"
1109 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
1110 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
1112 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
1117 if [ -z "$mode" ] ; then
1118 if [ `whoami` = "root" ] ; then
1125 if [ -z "$filename" ] ; then
1126 exit_failure_syntax "mimetypes-file argument missing"
1129 if [ "$vendor" = "true" -a "$action" = "install" ] ; then
1130 check_vendor_prefix "$filename"
1134 xdg_dir_name=mime/packages/
1136 xdg_user_dir="$XDG_DATA_HOME"
1137 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
1138 [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
1139 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
1141 xdg_system_dirs="$XDG_DATA_DIRS"
1142 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
1143 for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
1144 if [ -w $x/$xdg_dir_name ] ; then
1145 [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
1146 xdg_global_dir="$x/$xdg_dir_name"
1150 [ -w $xdg_global_dir ] || xdg_global_dir=
1151 DEBUG 3 "xdg_user_dir: $xdg_user_dir"
1152 DEBUG 3 "xdg_global_dir: $xdg_global_dir"
1154 # Find KDE3 mimelnk directory
1157 kde_global_dirs=`kde${KDE_SESSION_VERSION}-config --path mime 2> /dev/null`
1158 DEBUG 3 "kde_global_dirs: $kde_global_dirs"
1160 for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
1161 if [ -z "$first" ] ; then
1164 elif [ -w $x ] ; then
1168 DEBUG 3 "kde_user_dir: $kde_user_dir"
1169 DEBUG 3 "kde_global_dir: $kde_global_dir"
1171 # TODO: Gnome legacy support
1172 # See http://forums.fedoraforum.org/showthread.php?t=26875
1173 gnome_user_dir="$HOME/.gnome/apps"
1174 gnome_global_dir=/usr/share/gnome/apps
1175 [ -w $gnome_global_dir ] || gnome_global_dir=
1176 DEBUG 3 "gnome_user_dir: $gnome_user_dir"
1177 DEBUG 3 "gnome_global_dir: $gnome_global_dir"
1179 if [ x"$mode" = x"user" ] ; then
1180 xdg_dir="$xdg_user_dir"
1181 kde_dir="$kde_user_dir"
1182 gnome_dir="$gnome_user_dir"
1185 xdg_dir="$xdg_global_dir"
1186 kde_dir="$kde_global_dir"
1187 gnome_dir="$gnome_global_dir"
1189 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
1190 exit_failure_operation_impossible "No writable system mimetype directory found."
1194 # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
1195 # echo "[kde|$kde_user_dir|$kde_global_dir]"
1196 # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
1197 # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
1199 basefile=`basename "$filename"`
1200 #[ -z $vendor ] || basefile="$vendor-$basefile"
1203 if [ -n "$kde_dir" ] ; then
1204 DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
1206 mimetypes=`awk < "$filename" '
1207 # Strip XML comments
1214 if (match($0,/-->/)) {
1215 $0=substr($0,RSTART+RLENGTH)
1223 if (match($0,/<!--/)) {
1224 if (RSTART>1) print substr($0,0,RSTART)
1225 $0=substr($0,RSTART+RLENGTH)
1236 # List MIME types listed in <mime-type> tags
1240 /^mime-info/, /^\/mime-info/ {
1241 if (match($0,/^mime-type/)) {
1242 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1243 print substr($0,RSTART+6,RLENGTH-6)
1249 DEBUG 1 "$action mimetype in $xdg_dir"
1256 for x in $xdg_dir ; do
1258 eval 'cp $filename $x/$basefile'$xdg_redirect_output
1261 if [ -n "$mimetypes" ] ; then
1262 # No quotes around $mimetypes
1263 for x in $mimetypes ; do
1264 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
1265 mkdir -p `dirname $kde_dir/$x.desktop`
1267 # Strip XML comments
1274 if (match($0,/-->/)) {
1275 $0=substr($0,RSTART+RLENGTH)
1283 if (match($0,/<!--/)) {
1284 if (RSTART>1) print substr($0,0,RSTART)
1285 $0=substr($0,RSTART+RLENGTH)
1295 ' | awk > $kde_dir/$x.desktop '
1296 # Extract mimetype $x from the XML file $filename
1297 # Note that bash requires us to escape a single quote as '"'"'
1306 /^mime-info/, /^\/mime-info/ {
1307 if (match($0,/^mime-type/)) {
1308 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1309 if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
1311 print "[Desktop Entry]"
1312 print "# Installed by xdg-mime from " the_source
1313 print "Type=MimeType"
1314 print "MimeType=" the_type
1316 sub("/", "-", the_icon)
1317 print "Icon=" the_icon
1322 if (match($0,/^\/mime-type/)) {
1324 print "Patterns=" glob_patterns
1328 if (match($0,/^sub-class-of/)) {
1329 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1330 print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
1333 print "Error: '"'"'type'"'"' argument missing in " RS $0
1337 if (match($0,/^glob/)) {
1338 if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
1339 glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
1342 print "Error: '"'"'pattern'"'"' argument missing in " RS $0
1346 if (match($0,/^comment/)) {
1347 if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
1348 lang=substr($0,RSTART+10,RLENGTH-10)
1353 if (match($0,/>/)) {
1354 comment=substr($0,RSTART+1)
1355 sub("<", "<", comment)
1356 sub(">", ">", comment)
1357 sub("&", "\\&", comment)
1359 print "Comment[" lang "]=" comment
1361 print "Comment=" comment
1368 print "Error: Mimetype '"'"'" the_type "'"'"' not found"
1373 if [ "$?" = "1" ] ; then
1374 grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
1375 rm $kde_dir/$x.desktop
1385 for x in $xdg_dir ; do
1389 # No quotes around $mimetypes
1390 for x in $mimetypes ; do
1391 if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop >/dev/null 2>&1; then
1392 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
1393 rm -f $kde_dir/$x.desktop
1399 update_mime_database $xdg_base_dir