#!/bin/sh #--------------------------------------------- # xdg-mime # # Utility script to manipulate MIME related information # on XDG compliant systems. # # Refer to the usage() function below for usage. # # Copyright 2006, Kevin Krammer # Copyright 2006, Jeremy White # # LICENSE: # #--------------------------------------------- manualpage() { cat << _MANUALPAGE _MANUALPAGE } usage() { cat << _USAGE _USAGE } #@xdg-utils-common@ update_mime_database() { if [ x"$mode" = x"user" -a -n "$DISPLAY" ] ; then detectDE if [ x"$DE" = x"kde" ] ; then DEBUG 1 "Running kbuildsycoca" eval 'kbuildsycoca'$xdg_redirect_output fi fi for x in `echo "$PATH:/opt/gnome/bin" | sed 's/:/ /g'`; do if [ -x $x/update-mime-database ] ; then DEBUG 1 "Running $x/update-mime-database $1" eval '$x/update-mime-database $1'$xdg_redirect_output return fi done } info_kde() { DEBUG 1 "Running kfile \"$1\"" kfile "$1" 2> /dev/null | head -n 1 | cut -d "(" -f 2 | cut -d ")" -f 1 exit_success } info_gnome() { file=`readlink -f "$1"` # Normalize path DEBUG 1 "Running gnomevfs-info \"$file\"" gnomevfs-info "$file" 2> /dev/null | grep MIME | cut -d ":" -f 2 | sed s/"^ "// exit_success } info_generic() { DEBUG 1 "Running /usr/bin/file -i \"$1\"" /usr/bin/file -i "$1" 2> /dev/null | cut -d ":" -f 2 | sed s/"^ "// exit_success } make_default_kde() { # $1 is vendor-name.desktop # $2 is mime/type # Add to $KDE_HOME/share/config/profilerc: # [$2 - 1] # Application=$1 # # Remove all [$2 - *] sections, or even better, # renumber [$2 - *] sections and remove duplicate default_file="$HOME/.kde/share/config/profilerc" DEBUG 2 "make_default_kde $1 $2" DEBUG 1 "Updating $default_file" mkdir -p "$HOME/.kde/share/config" [ -f $default_file ] || touch $default_file awk -v application="$1" -v mimetype="$2" ' BEGIN { header_start="[" mimetype " - " supress=0 } { if (index($0, header_start) == 1 ) supress=1 else if (/^\[/) { supress=0 } if (!supress) { print $0 } } END { print "" print "[" mimetype " - 1]" print "Application=" application print "AllowAsDefault=true" print "GenericServiceType=Application" print "Preference=1" print "ServiceType=" mimetype } ' $default_file > ${default_file}.new && mv ${default_file}.new $default_file } make_default_generic() { # $1 is vendor-name.desktop # $2 is mime/type # Add $2=$1 to XDG_DATA_HOME/applications/defaults.list xdg_user_dir="$XDG_DATA_HOME" [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" default_file="$xdg_user_dir/applications/defaults.list" DEBUG 2 "make_default_generic $1 $2" DEBUG 1 "Updating $default_file" grep -v "$2=" $default_file > ${default_file}.new 2> /dev/null if ! grep "[Default Applications]" ${default_file}.new > /dev/null; then echo "[Default Applications]" >> ${default_file}.new fi echo $2=$1 >> ${default_file}.new mv ${default_file}.new $default_file } defapp_kde() { MIME="$1" DEBUG 1 "Running ktradertest \"$MIME\" Application" trader_result=`ktradertest "$MIME" Application 2>/dev/null | grep DesktopEntryPath \ | head -n 1 | cut -d ':' -f 2 | cut -d \' -f 2` if [ -n "$trader_result" ] ; then basename "$trader_result" exit_success else exit_failure_operation_failed fi } defapp_generic() { # FIXME: # Find XDG-DATA-DIRS/applications/defaults.list MIME="$1" xdg_user_dir="$XDG_DATA_HOME" [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" xdg_user_dir="$xdg_user_dir/$xdg_dir_name" xdg_system_dirs="$XDG_DATA_DIRS" [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/ for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do DEBUG 2 "Checking $x/applications/defaults.list" trader_result=`grep "$MIME=" $x/applications/defaults.list 2> /dev/null | cut -d '=' -f 2 | cut -d ';' -f 1` if [ -n "$trader_result" ] ; then echo $trader_result exit_success fi done exit_success } [ x"$1" != x"" ] || exit_failure_syntax mode= action= filename= mimetype= case $1 in install) action=install ;; uninstall) action=uninstall ;; query) shift if [ -z "$1" ] ; then exit_failure_syntax "query type argument missing" fi case $1 in filetype) action=info filename="$2" if [ -z "$filename" ] ; then exit_failure_syntax "FILE argument missing" fi case $filename in -*) exit_failure_syntax "unexpected option '$filename'" ;; esac check_input_file "$filename" ;; default) action=defapp mimetype="$2" if [ -z "$mimetype" ] ; then exit_failure_syntax "mimetype argument missing" fi case $mimetype in -*) exit_failure_syntax "unexpected option '$mimetype'" ;; */*) # Ok ;; *) exit_failure_syntax "mimetype '$mimetype' is not in the form 'minor/major'" ;; esac ;; *) exit_failure_syntax "unknown query type '$1'" ;; esac ;; default) action=makedefault shift if [ -z "$1" ] ; then exit_failure_syntax "application argument missing" fi case $1 in -*) exit_failure_syntax "unexpected option '$1'" ;; *.desktop) filename="$1" ;; *) exit_failure_syntax "malformed argument '$1', expected *.desktop" ;; esac ;; *) exit_failure_syntax "unknown command '$1'" ;; esac shift if [ "$action" = "makedefault" ]; then if [ -z "$1" ] ; then exit_failure_syntax "mimetype argument missing" fi while [ $# -gt 0 ] ; do case $1 in -*) exit_failure_syntax "unexpected option '$1'" ;; esac mimetype="$1" shift make_default_kde "$filename" "$mimetype" make_default_generic "$filename" "$mimetype" done exit_success fi if [ "$action" = "info" ]; then detectDE if [ x"$DE" = x"" ]; then if [ -x /usr/bin/file ]; then DE=generic fi fi case "$DE" in kde) info_kde "$filename" ;; gnome) info_gnome "$filename" ;; generic) info_generic "$filename" ;; esac exit_failure_operation_impossible "no method available for quering MIME type of '$filename'" fi if [ "$action" = "defapp" ]; then detectDE case "$DE" in kde) defapp_kde "$mimetype" ;; *) defapp_generic "$mimetype" ;; esac exit_failure_operation_impossible "no method available for quering default application for '$mimetype'" fi vendor=true while [ $# -gt 0 ] ; do parm="$1" shift case $parm in --mode) if [ -z "$1" ] ; then exit_failure_syntax "mode argument missing for --mode" fi case "$1" in user) mode=user ;; system) mode=system ;; *) exit_failure_syntax "unknown mode '$1'" ;; esac shift ;; --novendor) vendor=false ;; -*) exit_failure_syntax "unexpected option '$parm'" ;; *) if [ -n "$filename" ] ; then exit_failure_syntax "unexpected argument '$parm'" fi filename="$parm" check_input_file "$filename" ;; esac done if [ -z "$action" ] ; then exit_failure_syntax "command argument missing" fi if [ -z "$mode" ] ; then if [ `whoami` = "root" ] ; then mode=system else mode=user fi fi if [ -z "$filename" ] ; then exit_failure_syntax "mimetypes-file argument missing" fi if [ "$vendor" = "true" -a "$action" = "install" ] ; then check_vendor_prefix "$filename" fi xdg_base_dir= xdg_dir_name=mime/packages/ xdg_user_dir="$XDG_DATA_HOME" [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" [ x"$mode" = x"user" ] && xdg_base_dir="$xdg_user_dir/mime" xdg_user_dir="$xdg_user_dir/$xdg_dir_name" xdg_system_dirs="$XDG_DATA_DIRS" [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/ for x in `echo $xdg_system_dirs | sed 's/:/ /g'`; do if [ -w $x/$xdg_dir_name ] ; then [ x"$mode" = x"system" ] && xdg_base_dir="$x/mime" xdg_global_dir="$x/$xdg_dir_name" break fi done [ -w $xdg_global_dir ] || xdg_global_dir= DEBUG 3 "xdg_user_dir: $xdg_user_dir" DEBUG 3 "xdg_global_dir: $xdg_global_dir" # Find KDE3 mimelnk directory kde_user_dir= kde_global_dir= kde_global_dirs=`kde-config --path mime 2> /dev/null` DEBUG 3 "kde_global_dirs: $kde_global_dirs" first= for x in `echo $kde_global_dirs | sed 's/:/ /g'` ; do if [ -z "$first" ] ; then first=false kde_user_dir="$x" elif [ -w $x ] ; then kde_global_dir="$x" fi done DEBUG 3 "kde_user_dir: $kde_user_dir" DEBUG 3 "kde_global_dir: $kde_global_dir" # TODO: Gnome legacy support # See http://forums.fedoraforum.org/showthread.php?t=26875 gnome_user_dir="$HOME/.gnome/apps" gnome_global_dir=/usr/share/gnome/apps [ -w $gnome_global_dir ] || gnome_global_dir= DEBUG 3 "gnome_user_dir: $gnome_user_dir" DEBUG 3 "gnome_global_dir: $gnome_global_dir" if [ x"$mode" = x"user" ] ; then xdg_dir="$xdg_user_dir" kde_dir="$kde_user_dir" gnome_dir="$gnome_user_dir" my_umask=077 else xdg_dir="$xdg_global_dir" kde_dir="$kde_global_dir" gnome_dir="$gnome_global_dir" my_umask=022 if [ -z "${xdg_dir}${kde_dir}${gnome_dir}" ] ; then exit_failure_operation_impossible "No writable system mimetype directory found." fi fi # echo "[xdg|$xdg_user_dir|$xdg_global_dir]" # echo "[kde|$kde_user_dir|$kde_global_dir]" # echo "[gnome|$gnome_user_dir|$gnome_global_dir]" # echo "[using|$xdg_dir|$kde_dir|$gnome_dir]" basefile=`basename "$filename"` #[ -z $vendor ] || basefile="$vendor-$basefile" mimetypes= if [ -n "$kde_dir" ] ; then DEBUG 2 "KDE3 mimelnk directory found, extracting mimetypes from XML file" mimetypes=`awk < "$filename" ' # Strip XML comments BEGIN { supress=0 } { do if (supress) { if (match($0,/-->/)) { $0=substr($0,RSTART+RLENGTH) supress=0 } else { break } } else { if (match($0,//)) { $0=substr($0,RSTART+RLENGTH) supress=0 } else { break } } else { if (match($0,/