remove redundant manifest
[blorb-thumbnailer.git] / scripts / xdg-mime
blob7c56b7d61f3c6bcbdee65b6d036845340ec88ba2
1 #!/bin/sh
2 #---------------------------------------------
3 # xdg-mime
5 # Utility script to manipulate MIME related information
6 # on XDG compliant systems.
8 # Refer to the usage() function below for usage.
10 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
11 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
13 # LICENSE:
15 # Permission is hereby granted, free of charge, to any person obtaining a
16 # copy of this software and associated documentation files (the "Software"),
17 # to deal in the Software without restriction, including without limitation
18 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 # and/or sell copies of the Software, and to permit persons to whom the
20 # Software is furnished to do so, subject to the following conditions:
22 # The above copyright notice and this permission notice shall be included
23 # in all copies or substantial portions of the Software.
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 # OTHER DEALINGS IN THE SOFTWARE.
33 #---------------------------------------------
35 manualpage()
37 cat << _MANUALPAGE
38 Name
40 xdg-mime - command line tool for querying information about file type handling
41 and adding descriptions for new file types
43 Synopsis
45 xdg-mime query { filetype | default } ...
47 xdg-mime default application mimetype(s)
49 xdg-mime install [--mode mode] [--novendor] mimetypes-file
51 xdg-mime uninstall [--mode mode] mimetypes-file
53 xdg-mime { --help | --manual | --version }
55 Description
57 The xdg-mime program can be used to query information about file types and to
58 add descriptions for new file types.
60 Commands
62 query
64 Returns information related to file types.
66 The query option is for use inside a desktop session only. It is not
67 recommended to use xdg-mime query as root.
69 The following queries are supported:
71 query filetype FILE: Returns the file type of FILE in the form of a MIME
72 type.
74 query default mimetype: Returns the default application that the desktop
75 environment uses for opening files of type mimetype. The default
76 application is identified by its *.desktop file.
78 default
80 Ask the desktop environment to make application the default application for
81 opening files of type mimetype. An application can be made the default for
82 several file types by specifying multiple mimetypes.
84 application is the desktop file id of the application and has the form
85 vendor-name.desktop application must already be installed in the desktop
86 menu before it can be made the default handler. The aplication's desktop
87 file must list support for all the MIME types that it wishes to be the
88 default handler for.
90 Requests to make an application a default handler may be subject to system
91 policy or approval by the end-user. xdg-mime query can be used to verify
92 whether an application is the actual default handler for a specific file
93 type.
95 The default option is for use inside a desktop session only. It is not
96 recommended to use xdg-mime default as root.
98 install
99 Adds the file type descriptions provided in mimetypes-file to the desktop
100 environment. mimetypes-file must be a XML file that follows the
101 freedesktop.org Shared MIME-info Database specification and that has a
102 mime-info element as its document root. For each new file type one or more
103 icons with name major-minor must be installed with the xdg-icon-resource
104 command in the mimetypes context. For example the application/
105 vnd.oasis.opendocument.text filetype requires an icon by the name of
106 application-vnd.oasis.opendocument.text to be installed.
107 uninstall
108 Removes the file type descriptions provided in mimetypes-file and
109 previously added with xdg-mime install from the desktop environment.
110 mimetypes-file must be a XML file that follows the freedesktop.org Shared
111 MIME-info Database specification and that has a mime-info element as its
112 document root.
114 Options
116 --mode mode
118 mode can be user or system. In user mode the file is (un)installed for the
119 current user only. In system mode the file is (un)installed for all users
120 on the system. Usually only root is allowed to install in system mode.
122 The default is to use system mode when called by root and to use user mode
123 when called by a non-root user.
125 --novendor
127 Normally, xdg-mime checks to ensure that the mimetypes-file to be installed
128 has a proper vendor prefix. This option can be used to disable that check.
130 A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated
131 with a dash ("-"). Companies and organizations are encouraged to use a word
132 or phrase, preferably the organizations name, for which they hold a
133 trademark as their vendor prefix. The purpose of the vendor prefix is to
134 prevent name conflicts.
136 --help
137 Show command synopsis.
138 --manual
139 Show this manualpage.
140 --version
141 Show the xdg-utils version information.
143 Environment Variables
145 xdg-mime honours the following environment variables:
147 XDG_UTILS_DEBUG_LEVEL
148 Setting this environment variable to a non-zero numerical value makes
149 xdg-mime do more verbose reporting on stderr. Setting a higher value
150 increases the verbosity.
151 XDG_UTILS_INSTALL_MODE
152 This environment variable can be used by the user or administrator to
153 override the installation mode. Valid values are user and system.
155 Exit Codes
157 An exit code of 0 indicates success while a non-zero exit code indicates
158 failure. The following failure codes can be returned:
161 Error in command line syntax.
163 One of the files passed on the command line did not exist.
165 A required tool could not be found.
167 The action failed.
169 No permission to read one of the files passed on the command line.
171 See Also
173 xdg-icon-resource(1), xdg-desktop-menu(1)
175 Examples
177 xdg-mime query filetype /tmp/foobar.png
179 Prints the MIME type of the file /tmp/foobar.png, in this case image/png
181 xdg-mime query default image/png
183 Prints the .desktop filename of the application which is registered to open PNG
184 files.
186 xdg-mime install shinythings-shiny.xml
188 Adds a file type description for "shiny"-files. "shinythings-" is used as the
189 vendor prefix. The file type description could look as folows.
191 shinythings-shiny.xml:
193 <?xml version="1.0"?>
194 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
195 <mime-type type="text/x-shiny">
196 <comment>Shiny new file type</comment>
197 <glob pattern="*.shiny"/>
198 <glob pattern="*.shi"/>
199 </mime-type>
200 </mime-info>
202 An icon for this new file type must also be installed, for example with:
204 xdg-icon-resource install --context mimetypes --size 64 shiny-file-icon.png text-x-shiny
206 _MANUALPAGE
209 usage()
211 cat << _USAGE
212 xdg-mime - command line tool for querying information about file type handling
213 and adding descriptions for new file types
215 Synopsis
217 xdg-mime query { filetype | default } ...
219 xdg-mime default application mimetype(s)
221 xdg-mime install [--mode mode] [--novendor] mimetypes-file
223 xdg-mime uninstall [--mode mode] mimetypes-file
225 xdg-mime { --help | --manual | --version }
227 _USAGE
230 #@xdg-utils-common@
232 #----------------------------------------------------------------------------
233 # Common utility functions included in all XDG wrapper scripts
234 #----------------------------------------------------------------------------
236 DEBUG()
238 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
239 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
240 shift
241 echo "$@" >&2
244 #-------------------------------------------------------------
245 # Exit script on successfully completing the desired operation
247 exit_success()
249 if [ $# -gt 0 ]; then
250 echo "$@"
251 echo
254 exit 0
258 #-----------------------------------------
259 # Exit script on malformed arguments, not enough arguments
260 # or missing required option.
261 # prints usage information
263 exit_failure_syntax()
265 if [ $# -gt 0 ]; then
266 echo "xdg-mime: $@" >&2
267 echo "Try 'xdg-mime --help' for more information." >&2
268 else
269 usage
270 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
273 exit 1
276 #-------------------------------------------------------------
277 # Exit script on missing file specified on command line
279 exit_failure_file_missing()
281 if [ $# -gt 0 ]; then
282 echo "xdg-mime: $@" >&2
285 exit 2
288 #-------------------------------------------------------------
289 # Exit script on failure to locate necessary tool applications
291 exit_failure_operation_impossible()
293 if [ $# -gt 0 ]; then
294 echo "xdg-mime: $@" >&2
297 exit 3
300 #-------------------------------------------------------------
301 # Exit script on failure returned by a tool application
303 exit_failure_operation_failed()
305 if [ $# -gt 0 ]; then
306 echo "xdg-mime: $@" >&2
309 exit 4
312 #------------------------------------------------------------
313 # Exit script on insufficient permission to read a specified file
315 exit_failure_file_permission_read()
317 if [ $# -gt 0 ]; then
318 echo "xdg-mime: $@" >&2
321 exit 5
324 #------------------------------------------------------------
325 # Exit script on insufficient permission to read a specified file
327 exit_failure_file_permission_write()
329 if [ $# -gt 0 ]; then
330 echo "xdg-mime: $@" >&2
333 exit 6
336 check_input_file()
338 if [ ! -e "$1" ]; then
339 exit_failure_file_missing "file '$1' does not exist"
341 if [ ! -r "$1" ]; then
342 exit_failure_file_permission_read "no permission to read file '$1'"
346 check_vendor_prefix()
348 file_label="$2"
349 [ -n "$file_label" ] || file_label="filename"
350 file=`basename "$1"`
351 case "$file" in
352 [a-zA-Z]*-*)
353 return
355 esac
357 echo "xdg-mime: $file_label '$file' does not have a proper vendor prefix" >&2
358 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
359 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
360 echo "Use --novendor to override or 'xdg-mime --manual' for additional info." >&2
361 exit 1
364 check_output_file()
366 # if the file exists, check if it is writeable
367 # if it does not exists, check if we are allowed to write on the directory
368 if [ -e "$1" ]; then
369 if [ ! -w "$1" ]; then
370 exit_failure_file_permission_write "no permission to write to file '$1'"
372 else
373 DIR=`dirname "$1"`
374 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
375 exit_failure_file_permission_write "no permission to create file '$1'"
380 #----------------------------------------
381 # Checks for shared commands, e.g. --help
383 check_common_commands()
385 while [ $# -gt 0 ] ; do
386 parm="$1"
387 shift
389 case "$parm" in
390 --help)
391 usage
392 echo "Use 'man xdg-mime' or 'xdg-mime --manual' for additional info."
393 exit_success
396 --manual)
397 manualpage
398 exit_success
401 --version)
402 echo "xdg-mime 1.0.1"
403 exit_success
405 esac
406 done
409 check_common_commands "$@"
411 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
412 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
413 # Be silent
414 xdg_redirect_output=" > /dev/null 2> /dev/null"
415 else
416 # All output to stderr
417 xdg_redirect_output=" >&2"
420 #--------------------------------------
421 # Checks for known desktop environments
422 # set variable DE to the desktop environments name, lowercase
424 detectDE()
426 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
427 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
428 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
432 #----------------------------------------------------------------------------
433 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
434 # It also always returns 1 in KDE 3.4 and earlier
435 # Simply return 0 in such case
437 kfmclient_fix_exit_code()
439 version=`kde-config --version 2>/dev/null | grep KDE`
440 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
441 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
442 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
443 test "$major" -gt 3 && return $1
444 test "$minor" -gt 5 && return $1
445 test "$release" -gt 4 && return $1
446 return 0
449 update_mime_database()
451 if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then
452 detectDE
453 if [ x"$DE" = x"kde" ] ; then
454 DEBUG 1 "Running kbuildsycoca"
455 eval 'kbuildsycoca'$xdg_redirect_output
458 for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do
459 if [ -x $x/update-mime-database ] ; then
460 DEBUG 1 "Running $x/update-mime-database $1"
461 eval '$x/update-mime-database $1'$xdg_redirect_output
462 return
464 done
467 info_kde()
469 DEBUG 1 "Running kfile \"$1\""
470 kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1
472 exit_success
475 info_gnome()
477 file=`readlink -f "$1"` # Normalize path
478 DEBUG 1 "Running gnomevfs-info \"$file\""
479 gnomevfs-info "$file" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "//
481 exit_success
484 info_generic()
486 DEBUG 1 "Running /usr/bin/file -i \"$1\""
487 /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "//
489 exit_success
492 make_default_kde()
494 # $1 is vendor-name.desktop
495 # $2 is mime/type
496 # Add to $KDE_HOME/share/config/profilerc:
497 # [$2 - 1]
498 # Application=$1
500 # Remove all [$2 - *] sections, or even better,
501 # renumber [$2 - *] sections and remove duplicate
503 default_file="$HOME/.kde/share/config/profilerc"
504 DEBUG 2 "make_default_kde $1 $2"
505 DEBUG 1 "Updating $default_file"
506 mkdir -p "$HOME/.kde/share/config"
507 [ -f $default_file ] || touch $default_file
508 awk -v application="$1" -v mimetype="$2" '
509 BEGIN {
510 header_start="[" mimetype " - "
511 supress=0
514 if (index($0, header_start) == 1 )
515 supress=1
516 else
517 if (/^\[/) { supress=0 }
519 if (!supress) {
520 print $0
523 END {
524 print ""
525 print "[" mimetype " - 1]"
526 print "Application=" application
527 print "AllowAsDefault=true"
528 print "GenericServiceType=Application"
529 print "Preference=1"
530 print "ServiceType=" mimetype
532 ' $default_file > ${default_file}.new && mv ${default_file}.new $default_file
535 make_default_generic()
537 # $1 is vendor-name.desktop
538 # $2 is mime/type
539 # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list
540 xdg_user_dir="$XDG_DATA_HOME"
541 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
542 default_file="$xdg_user_dir/applications/defaults.list"
543 DEBUG 2 "make_default_generic $1 $2"
544 DEBUG 1 "Updating $default_file"
545 grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null
546 if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then
547 echo "[Default Applications]" >> ${default_file}.new
549 echo $2=$1 >> ${default_file}.new
550 mv ${default_file}.new $default_file
553 defapp_generic()
555 MIME="$1"
556 xdg_user_dir="$XDG_DATA_HOME"
557 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
558 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
559 xdg_system_dirs="$XDG_DATA_DIRS"
560 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
562 for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
563 DEBUG 2 "Checking $x/applications/defaults.list"
564 trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1`
565 if [ -n "$trader_result" ] ; then
566 echo $trader_result
567 exit_success
569 done
570 exit_success
573 defapp_kde()
575 MIME="$1"
576 ktradertest=`which ktradertest 2> /dev/null`
577 if [ -n "$ktradertest" ] ; then
578 DEBUG 1 "Running ktradertest \"$MIME\" Application"
579 trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep DesktopEntryPath \
580 | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2`
581 if [ -n "$trader_result" ] ; then
582 basename "$trader_result"
583 exit_success
584 else
585 exit_failure_operation_failed
587 else
588 defapp_generic "$1"
592 [ x"$1" != x"" ] || exit_failure_syntax
594 mode=
595 action=
596 filename=
597 mimetype=
599 case $1 in
600 install)
601 action=install
604 uninstall)
605 action=uninstall
608 query)
609 shift
611 if [ -z "$1" ] ; then
612 exit_failure_syntax "query type argument missing"
615 case $1 in
616 filetype)
617 action=info
619 filename="$2"
620 if [ -z "$filename" ] ; then
621 exit_failure_syntax "FILE argument missing"
623 case $filename in
625 exit_failure_syntax "unexpected option '$filename'"
627 esac
628 check_input_file "$filename"
631 default)
632 action=defapp
633 mimetype="$2"
634 if [ -z "$mimetype" ] ; then
635 exit_failure_syntax "mimetype argument missing"
637 case $mimetype in
639 exit_failure_syntax "unexpected option '$mimetype'"
642 */*)
643 # Ok
647 exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'"
649 esac
653 exit_failure_syntax "unknown query type '$1'"
655 esac
658 default)
659 action=makedefault
660 shift
662 if [ -z "$1" ] ; then
663 exit_failure_syntax "application argument missing"
665 case $1 in
667 exit_failure_syntax "unexpected option '$1'"
670 *.desktop)
671 filename="$1"
675 exit_failure_syntax "malformed argument '$1', expected *.desktop"
677 esac
681 exit_failure_syntax "unknown command '$1'"
683 esac
685 shift
688 if [ "$action" = "makedefault" ]; then
689 if [ -z "$1" ] ; then
690 exit_failure_syntax "mimetype argument missing"
693 while [ $# -gt 0 ] ; do
694 case $1 in
696 exit_failure_syntax "unexpected option '$1'"
698 esac
699 mimetype="$1"
700 shift
702 make_default_kde "$filename" "$mimetype"
703 make_default_generic "$filename" "$mimetype"
704 done
705 exit_success
708 if [ "$action" = "info" ]; then
709 detectDE
711 if [ x"$DE" = x"" ]; then
712 if [ -x /usr/bin/file ]; then
713 DE=generic
717 case "$DE" in
718 kde)
719 info_kde "$filename"
722 gnome)
723 info_gnome "$filename"
726 generic)
727 info_generic "$filename"
729 esac
730 exit_failure_operation_impossible "no method available for quering MIME type of '$filename'"
733 if [ "$action" = "defapp" ]; then
734 detectDE
736 case "$DE" in
737 kde)
738 defapp_kde "$mimetype"
742 defapp_generic "$mimetype"
744 esac
745 exit_failure_operation_impossible "no method available for quering default application for '$mimetype'"
748 vendor=true
749 while [ $# -gt 0 ] ; do
750 parm="$1"
751 shift
753 case $parm in
754 --mode)
755 if [ -z "$1" ] ; then
756 exit_failure_syntax "mode argument missing for --mode"
758 case "$1" in
759 user)
760 mode="user"
763 system)
764 mode="system"
768 exit_failure_syntax "unknown mode '$1'"
770 esac
771 shift
774 --novendor)
775 vendor=false
779 exit_failure_syntax "unexpected option '$parm'"
783 if [ -n "$filename" ] ; then
784 exit_failure_syntax "unexpected argument '$parm'"
787 filename="$parm"
788 check_input_file "$filename"
790 esac
791 done
793 if [ -z "$action" ] ; then
794 exit_failure_syntax "command argument missing"
797 if [ -n "$XDG_UTILS_INSTALL_MODE" ] ; then
798 if [ "$XDG_UTILS_INSTALL_MODE" = "system" ] ; then
799 mode="system"
800 elif [ "$XDG_UTILS_INSTALL_MODE" = "user" ] ; then
801 mode="user"
805 if [ -z "$mode" ] ; then
806 if [ `whoami` = "root" ] ; then
807 mode="system"
808 else
809 mode="user"
813 if [ -z "$filename" ] ; then
814 exit_failure_syntax "mimetypes-file argument missing"
817 if [ "$vendor" = "true" -a "$action" = "install" ] ; then
818 check_vendor_prefix "$filename"
821 xdg_base_dir=
822 xdg_dir_name=mime/packages/
824 xdg_user_dir="$XDG_DATA_HOME"
825 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
826 [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime"
827 xdg_user_dir="$xdg_user_dir/$xdg_dir_name"
829 xdg_system_dirs="$XDG_DATA_DIRS"
830 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
831 for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do
832 if [ -w $x/$xdg_dir_name ] ; then
833 [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime"
834 xdg_global_dir="$x/$xdg_dir_name"
835 break
837 done
838 [ -w $xdg_global_dir ] || xdg_global_dir=
839 DEBUG 3 "xdg_user_dir: $xdg_user_dir"
840 DEBUG 3 "xdg_global_dir: $xdg_global_dir"
842 # Find KDE3 mimelnk directory
843 kde_user_dir=
844 kde_global_dir=
845 kde_global_dirs=`kde-config --path mime 2> /dev/null`
846 DEBUG 3 "kde_global_dirs: $kde_global_dirs"
847 first=
848 for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do
849 if [ -z "$first" ] ; then
850 first=false
851 kde_user_dir="$x"
852 elif [ -w $x ] ; then
853 kde_global_dir="$x"
855 done
856 DEBUG 3 "kde_user_dir: $kde_user_dir"
857 DEBUG 3 "kde_global_dir: $kde_global_dir"
859 # TODO: Gnome legacy support
860 # See http://forums.fedoraforum.org/showthread.php?t=26875
861 gnome_user_dir="$HOME/.gnome/apps"
862 gnome_global_dir=/usr/share/gnome/apps
863 [ -w $gnome_global_dir ] || gnome_global_dir=
864 DEBUG 3 "gnome_user_dir: $gnome_user_dir"
865 DEBUG 3 "gnome_global_dir: $gnome_global_dir"
867 if [ x"$mode" = x"user" ] ; then
868 xdg_dir="$xdg_user_dir"
869 kde_dir="$kde_user_dir"
870 gnome_dir="$gnome_user_dir"
871 my_umask=077
872 else
873 xdg_dir="$xdg_global_dir"
874 kde_dir="$kde_global_dir"
875 gnome_dir="$gnome_global_dir"
876 my_umask=022
877 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then
878 exit_failure_operation_impossible "No writable system mimetype directory found."
882 # echo "[xdg|$xdg_user_dir|$xdg_global_dir]"
883 # echo "[kde|$kde_user_dir|$kde_global_dir]"
884 # echo "[gnome|$gnome_user_dir|$gnome_global_dir]"
885 # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]"
887 basefile=`basename "$filename"`
888 #[ -z $vendor ] || basefile="$vendor-$basefile"
890 mimetypes=
891 if [ -n "$kde_dir" ] ; then
892 DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file"
894 mimetypes=`awk < "$filename" '
895 # Strip XML comments
896 BEGIN {
897 supress=0
901 if (supress) {
902 if (match($0,/-->/)) {
903 $0=substr($0,RSTART+RLENGTH)
904 supress=0
906 else {
907 break
910 else {
911 if (match($0,/<!--/)) {
912 if (RSTART>1) print substr($0,0,RSTART)
913 $0=substr($0,RSTART+RLENGTH)
914 supress=1
916 else {
917 if ($0) print $0
918 break
921 while(1)
923 ' | awk '
924 # List MIME types listed in <mime-type> tags
925 BEGIN {
926 RS="<"
928 /^mime-info/, /^\/mime-info/ {
929 if (match($0,/^mime-type/)) {
930 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
931 print substr($0,RSTART+6,RLENGTH-6)
937 DEBUG 1 "$action mimetype in $xdg_dir"
939 case $action in
940 install)
941 save_umask=`umask`
942 umask $my_umask
944 for x in $xdg_dir ; do
945 mkdir -p $x
946 eval 'cp $filename $x/$basefile'$xdg_redirect_output
947 done
949 if [ -n "$mimetypes" ] ; then
950 # No quotes around $mimetypes
951 for x in $mimetypes ; do
952 DEBUG 1 "Installing $kde_dir/$x.desktop (KDE 3.x support)"
953 mkdir -p `dirname $kde_dir/$x.desktop`
954 awk < "$filename" '
955 # Strip XML comments
956 BEGIN {
957 supress=0
961 if (supress) {
962 if (match($0,/-->/)) {
963 $0=substr($0,RSTART+RLENGTH)
964 supress=0
966 else {
967 break
970 else {
971 if (match($0,/<!--/)) {
972 if (RSTART>1) print substr($0,0,RSTART)
973 $0=substr($0,RSTART+RLENGTH)
974 supress=1
976 else {
977 if ($0) print $0
978 break
981 while(1)
983 ' | awk > $kde_dir/$x.desktop '
984 # Extract mimetype $x from the XML file $filename
985 # Note that bash requires us to escape a single quote as '"'"'
986 BEGIN {
987 the_type=ARGV[1]
988 the_source=ARGV[2]
989 ARGC=1
990 RS="<"
991 found=0
992 glob_patterns=""
994 /^mime-info/, /^\/mime-info/ {
995 if (match($0,/^mime-type/)) {
996 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
997 if (substr($0,RSTART+6,RLENGTH-6) == the_type) {
998 found=1
999 print "[Desktop Entry]"
1000 print "# Installed by xdg-mime from " the_source
1001 print "Type=MimeType"
1002 print "MimeType=" the_type
1003 the_icon=the_type
1004 sub("/", "-", the_icon)
1005 print "Icon=" the_icon
1009 else if (found) {
1010 if (match($0,/^\/mime-type/)) {
1011 if (glob_patterns)
1012 print "Patterns=" glob_patterns
1013 exit 0
1016 if (match($0,/^sub-class-of/)) {
1017 if (match($0,/type="[^"]*/) || match($0,/type='"'"'[^'"'"']*/)) {
1018 print "X-KDE-IsAlso=" substr($0,RSTART+6,RLENGTH-6)
1020 else {
1021 print "Error: '"'"'type'"'"' argument missing in " RS $0
1022 exit 1
1025 if (match($0,/^glob/)) {
1026 if (match($0,/pattern="[^"]*/) || match($0,/pattern='"'"'[^'"'"']*/)) {
1027 glob_patterns = glob_patterns substr($0,RSTART+9,RLENGTH-9) ";"
1029 else {
1030 print "Error: '"'"'pattern'"'"' argument missing in " RS $0
1031 exit 1
1034 if (match($0,/^comment/)) {
1035 if (match($0,/xml:lang="[^"]*/) || match($0,/xml:lang='"'"'[^'"'"']*/)) {
1036 lang=substr($0,RSTART+10,RLENGTH-10)
1038 else {
1039 lang=""
1041 if (match($0,/>/)) {
1042 comment=substr($0,RSTART+1)
1043 sub("&lt;", "<", comment)
1044 sub("&gt;", ">", comment)
1045 sub("&amp;", "\\&", comment)
1046 if (lang)
1047 print "Comment[" lang "]=" comment
1048 else
1049 print "Comment=" comment
1054 END {
1055 if (!found) {
1056 print "Error: Mimetype '"'"'" the_type "'"'"' not found"
1057 exit 1
1060 ' $x $basefile
1061 if [ "$?" = "1" ] ; then
1062 grep -A 10 "^Error:" $kde_dir/$x.desktop >&2
1063 rm $kde_dir/$x.desktop
1064 exit 1
1066 done
1069 umask $save_umask
1072 uninstall)
1073 for x in $xdg_dir ; do
1074 rm -f $x/$basefile
1075 done
1077 # No quotes around $mimetypes
1078 for x in $mimetypes ; do
1079 if grep '^# Installed by xdg-mime' $kde_dir/$x.desktop &>/dev/null ; then
1080 DEBUG 1 "Removing $kde_dir/$x.desktop (KDE 3.x support)"
1081 rm -f $kde_dir/$x.desktop
1083 done
1085 esac
1087 update_mime_database $xdg_base_dir
1089 exit_success