Advertisement
Guest User

xdg-open

a guest
Feb 3rd, 2011
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 12.86 KB | None | 0 0
  1. #!/bin/sh
  2. #---------------------------------------------
  3. #   xdg-open
  4. #
  5. #   Utility script to open a URL in the registered default application.
  6. #
  7. #   Refer to the usage() function below for usage.
  8. #
  9. #   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
  10. #   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
  11. #
  12. #   LICENSE:
  13. #
  14. #   Permission is hereby granted, free of charge, to any person obtaining a
  15. #   copy of this software and associated documentation files (the "Software"),
  16. #   to deal in the Software without restriction, including without limitation
  17. #   the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. #   and/or sell copies of the Software, and to permit persons to whom the
  19. #   Software is furnished to do so, subject to the following conditions:
  20. #
  21. #   The above copyright notice and this permission notice shall be included
  22. #   in all copies or substantial portions of the Software.
  23. #
  24. #   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. #   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. #   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. #   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. #   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  29. #   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  30. #   OTHER DEALINGS IN THE SOFTWARE.
  31. #
  32. #---------------------------------------------
  33.  
  34. manualpage()
  35. {
  36. cat << _MANUALPAGE
  37. Name
  38.  
  39. xdg-open - opens a file or URL in the user's preferred application
  40.  
  41. Synopsis
  42.  
  43. xdg-open { file | URL }
  44.  
  45. xdg-open { --help | --manual | --version }
  46.  
  47. Description
  48.  
  49. xdg-open opens a file or URL in the user's preferred application. If a URL is
  50. provided the URL will be opened in the user's preferred web browser. If a file
  51. is provided the file will be opened in the preferred application for files of
  52. that type. xdg-open supports file, ftp, http and https URLs.
  53.  
  54. xdg-open is for use inside a desktop session only. It is not recommended to use
  55. xdg-open as root.
  56.  
  57. Options
  58.  
  59. --help
  60.    Show command synopsis.
  61. --manual
  62.    Show this manualpage.
  63. --version
  64.    Show the xdg-utils version information.
  65.  
  66. Exit Codes
  67.  
  68. An exit code of 0 indicates success while a non-zero exit code indicates
  69. failure. The following failure codes can be returned:
  70.  
  71. 1
  72.    Error in command line syntax.
  73. 2
  74.    One of the files passed on the command line did not exist.
  75. 3
  76.    A required tool could not be found.
  77. 4
  78.    The action failed.
  79.  
  80. Examples
  81.  
  82. xdg-open 'http://www.freedesktop.org/'
  83.  
  84. Opens the Freedesktop.org website in the user's default browser
  85.  
  86. xdg-open /tmp/foobar.png
  87.  
  88. Opens the PNG image file /tmp/foobar.png in the user's default image viewing
  89. application.
  90.  
  91. _MANUALPAGE
  92. }
  93.  
  94. usage()
  95. {
  96. cat << _USAGE
  97. xdg-open - opens a file or URL in the user's preferred application
  98.  
  99. Synopsis
  100.  
  101. xdg-open { file | URL }
  102.  
  103. xdg-open { --help | --manual | --version }
  104.  
  105. _USAGE
  106. }
  107.  
  108. #@xdg-utils-common@
  109.  
  110. #----------------------------------------------------------------------------
  111. #   Common utility functions included in all XDG wrapper scripts
  112. #----------------------------------------------------------------------------
  113.  
  114. DEBUG()
  115. {
  116.   [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
  117.   [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
  118.   shift
  119.   echo "$@" >&2
  120. }
  121.  
  122. #-------------------------------------------------------------
  123. # Exit script on successfully completing the desired operation
  124.  
  125. exit_success()
  126. {
  127.     if [ $# -gt 0 ]; then
  128.         echo "$@"
  129.         echo
  130.     fi
  131.  
  132.     exit 0
  133. }
  134.  
  135.  
  136. #-----------------------------------------
  137.  
  138. # Exit script on malformed arguments, not enough arguments
  139. # or missing required option.
  140. # prints usage information
  141.  
  142. exit_failure_syntax()
  143. {
  144.     if [ $# -gt 0 ]; then
  145.         echo "xdg-open: $@" >&2
  146.         echo "Try 'xdg-open --help' for more information." >&2
  147.     else
  148.         usage
  149.         echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  150.     fi
  151.  
  152.     exit 1
  153. }
  154.  
  155. #-------------------------------------------------------------
  156. # Exit script on missing file specified on command line
  157.  
  158. exit_failure_file_missing()
  159. {
  160.     if [ $# -gt 0 ]; then
  161.         echo "xdg-open: $@" >&2
  162.     fi
  163.  
  164.     exit 2
  165. }
  166.  
  167. #-------------------------------------------------------------
  168. # Exit script on failure to locate necessary tool applications
  169.  
  170. exit_failure_operation_impossible()
  171. {
  172.     if [ $# -gt 0 ]; then
  173.         echo "xdg-open: $@" >&2
  174.     fi
  175.  
  176.     exit 3
  177. }
  178.  
  179. #-------------------------------------------------------------
  180. # Exit script on failure returned by a tool application
  181.  
  182. exit_failure_operation_failed()
  183. {
  184.  
  185.     if [ $# -gt 0 ]; then
  186.         echo "xdg-open: $@" >&2
  187.     fi
  188.  
  189.     exit 4
  190. }
  191.  
  192. #------------------------------------------------------------
  193. # Exit script on insufficient permission to read a specified file
  194.  
  195. exit_failure_file_permission_read()
  196. {
  197.     if [ $# -gt 0 ]; then
  198.         echo "xdg-open: $@" >&2
  199.     fi
  200.  
  201.     exit 5
  202. }
  203.  
  204. #------------------------------------------------------------
  205. # Exit script on insufficient permission to write a specified file
  206.  
  207. exit_failure_file_permission_write()
  208. {
  209.     if [ $# -gt 0 ]; then
  210.         echo "xdg-open: $@" >&2
  211.     fi
  212.  
  213.     exit 6
  214. }
  215.  
  216. check_input_file()
  217. {
  218.     if [ ! -e "$1" ]; then
  219.         exit_failure_file_missing "file '$1' does not exist"
  220.     fi
  221.     if [ ! -r "$1" ]; then
  222.         exit_failure_file_permission_read "no permission to read file '$1'"
  223.     fi
  224. }
  225.  
  226. check_vendor_prefix()
  227. {
  228.     file_label="$2"
  229.     [ -n "$file_label" ] || file_label="filename"
  230.     file=`basename "$1"`
  231.  
  232.    case "$file" in
  233.        [a-zA-Z]*-*)
  234.          return
  235.          ;;
  236.     esac
  237.  
  238.     echo "xdg-open: $file_label '$file' does not have a proper vendor prefix" >&2
  239.     echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
  240.     echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
  241.     echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
  242.     exit 1
  243. }
  244.  
  245. check_output_file()
  246. {
  247.     # if the file exists, check if it is writeable
  248.     # if it does not exists, check if we are allowed to write on the directory
  249.     if [ -e "$1" ]; then
  250.         if [ ! -w "$1" ]; then
  251.             exit_failure_file_permission_write "no permission to write to file '$1'"
  252.         fi
  253.     else
  254.         DIR=`dirname "$1"`
  255.         if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
  256.             exit_failure_file_permission_write "no permission to create file '$1'"
  257.         fi
  258.     fi
  259. }
  260.  
  261. #----------------------------------------
  262. # Checks for shared commands, e.g. --help
  263.  
  264. check_common_commands()
  265. {
  266.     while [ $# -gt 0 ] ; do
  267.         parm="$1"
  268.         shift
  269.  
  270.         case "$parm" in
  271.             --help)
  272.             usage
  273.             echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
  274.             exit_success
  275.             ;;
  276.  
  277.             --manual)
  278.  
  279.   manualpage
  280.             exit_success
  281.             ;;
  282.  
  283.             --version)
  284.             echo "xdg-open 1.0.2"
  285.             exit_success
  286.             ;;
  287.         esac
  288.     done
  289. }
  290.  
  291. check_common_commands "$@"
  292.  
  293. [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
  294. if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
  295.     # Be silent
  296.     xdg_redirect_output=" > /dev/null 2> /dev/null"
  297. else
  298.     # All output to stderr
  299.     xdg_redirect_output=" >&2"
  300. fi
  301.  
  302. #--------------------------------------
  303. # Checks for known desktop environments
  304. # set variable DE to the desktop environments name, lowercase
  305.  
  306. detectDE()
  307. {
  308.     if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  309.     elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  310.     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;
  311.     elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  312.     fi
  313. }
  314.  
  315. #----------------------------------------------------------------------------
  316. # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
  317. # It also always returns 1 in KDE 3.4 and earlier
  318. # Simply return 0 in such case
  319.  
  320. kfmclient_fix_exit_code()
  321. {
  322.     version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE`
  323.     major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
  324.  
  325.   minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
  326.     release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
  327.     test "$major" -gt 3 && return $1
  328.     test "$minor" -gt 5 && return $1
  329.     test "$release" -gt 4 && return $1
  330.     return 0
  331. }
  332.  
  333. # This handles backslashes but not quote marks.
  334. first_word()
  335. {
  336.     read first rest
  337.     echo "$first"
  338. }
  339.  
  340. open_kde()
  341. {
  342.     if kde-open -v 2>/dev/null 1>&2; then
  343.         kde-open "$1"
  344.     else
  345.         if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
  346.             kfmclient openURL "$1"
  347.         else
  348.             kfmclient exec "$1"
  349.             kfmclient_fix_exit_code $?
  350.         fi
  351.     fi
  352.  
  353.     if [ $? -eq 0 ]; then
  354.         exit_success
  355.     else
  356.         exit_failure_operation_failed
  357.     fi
  358. }
  359.  
  360. open_gnome()
  361. {
  362.     if gvfs-open --help 2>/dev/null 1>&2; then
  363.         gvfs-open "$1"
  364.     else
  365.         gnome-open "$1"
  366.     fi
  367.  
  368.     if [ $? -eq 0 ]; then
  369.         exit_success
  370.     else
  371.  
  372.   exit_failure_operation_failed
  373.     fi
  374. }
  375.  
  376. open_xfce()
  377. {
  378.     exo-open "$1"
  379.  
  380.     if [ $? -eq 0 ]; then
  381.         exit_success
  382.     else
  383.         exit_failure_operation_failed
  384.     fi
  385. }
  386.  
  387. open_generic_xdg_mime()
  388. {
  389.     filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
  390.     default=`xdg-mime query default "$filetype"`
  391.     if [ -n "$default" ] ; then
  392.         xdg_user_dir="$XDG_DATA_HOME"
  393.         [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
  394.  
  395.         xdg_system_dirs="$XDG_DATA_DIRS"
  396.         [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
  397.  
  398.         for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
  399.             file="$x/applications/$default"
  400.             if [ -r "$file" ] ; then
  401.                 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
  402.                 command_exec=`which $command 2>/dev/null`
  403.                 if [ -x "$command_exec" ] ; then
  404.                     $command_exec "$1"
  405.                     if [ $? -eq 0 ]; then
  406.                         exit_success
  407.                     fi
  408.                 fi
  409.             fi
  410.         done
  411.     fi
  412. }
  413.  
  414. open_generic()
  415. {
  416.     # Paths or file:// URLs
  417.     if (echo "$1" | grep -q '^file://' ||
  418.  
  419. ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
  420.  
  421.         local file=$(echo "$1" | sed 's%^file://%%')
  422.  
  423.         # Decode URLs
  424.         # TODO
  425.  
  426.         check_input_file "$file"
  427.  
  428.         open_generic_xdg_mime "$file"
  429.  
  430.         if [ -f /etc/debian_version ] &&
  431.             which run-mailcap 2>/dev/null 1>&2; then
  432.             run-mailcap --action=view "$file"
  433.             if [ $? -eq 0 ]; then
  434.                 exit_success
  435.             fi
  436.         fi
  437.  
  438.         if mimeopen -v 2>/dev/null 1>&2; then
  439.             mimeopen -n "$file"
  440.             if [ $? -eq 0 ]; then
  441.                 exit_success
  442.             fi
  443.         fi
  444.     fi
  445.  
  446.     IFS=":"
  447.     for browser in $BROWSER; do
  448.         if [ x"$browser" != x"" ]; then
  449.  
  450.             browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
  451.             if [ $? -ne 0 ]; then
  452.                 browser_with_arg=$browser;
  453.             fi
  454.  
  455.             if [ x"$browser_with_arg" = x"$browser" ]; then
  456.                 "$browser" "$1";
  457.             else eval '$browser_with_arg'$xdg_redirect_output;
  458.             fi
  459.  
  460.             if [ $? -eq 0 ]; then
  461.                 exit_success;
  462.             fi
  463.         fi
  464.     done
  465.  
  466.  exit_failure_operation_impossible "no method available for opening '$1'"
  467. }
  468.  
  469. [ x"$1" != x"" ] || exit_failure_syntax
  470.  
  471. url=
  472. while [ $# -gt 0 ] ; do
  473.     parm="$1"
  474.     shift
  475.  
  476.     case "$parm" in
  477.       -*)
  478.         exit_failure_syntax "unexpected option '$parm'"
  479.         ;;
  480.  
  481.       *)
  482.         if [ -n "$url" ] ; then
  483.             exit_failure_syntax "unexpected argument '$parm'"
  484.         fi
  485.         url="$parm"
  486.         ;;
  487.     esac
  488. done
  489.  
  490. if [ -z "${url}" ] ; then
  491.     exit_failure_syntax "file or URL argument missing"
  492. fi
  493.  
  494. detectDE
  495.  
  496. if [ x"$DE" = x"" ]; then
  497.     DE=generic
  498. fi
  499.  
  500. # if BROWSER variable is not set, check some well known browsers instead
  501. if [ x"$BROWSER" = x"" ]; then
  502.     BROWSER=links2:links:lynx:w3m
  503.     if [ -n "$DISPLAY" ]; then
  504.         BROWSER=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
  505.     fi
  506. fi
  507.  
  508. case "$DE" in
  509.     kde)
  510.     open_kde "$url"
  511.     ;;
  512.  gnome)
  513.     open_gnome "$url"
  514.     ;;
  515.  
  516.     xfce)
  517.     open_xfce "$url"
  518.     ;;
  519.  
  520.     generic)
  521.     open_generic "$url"
  522.     ;;
  523.  
  524.     *)
  525.     exit_failure_operation_impossible "no method available for opening '$url'"
  526.     ;;
  527. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement