Fix building
[gmpc-magnatune.git] / data / xdg-open
blobf66df5ec5bf6ae5e4523de2e948a05d85b9763c5
1 #!/bin/sh
2 #---------------------------------------------
3 # xdg-open
5 # Utility script to open a URL in the registered default application.
7 # Refer to the usage() function below for usage.
9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com>
12 # LICENSE:
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:
21 # The above copyright notice and this permission notice shall be included
22 # in all copies or substantial portions of the Software.
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.
32 #---------------------------------------------
34 manualpage()
36 cat << _MANUALPAGE
37 Name
39 xdg-open - opens a file or URL in the user's preferred application
41 Synopsis
43 xdg-open { file | URL }
45 xdg-open { --help | --manual | --version }
47 Description
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.
54 xdg-open is for use inside a desktop session only. It is not recommended to use
55 xdg-open as root.
57 Options
59 --help
60 Show command synopsis.
61 --manual
62 Show this manualpage.
63 --version
64 Show the xdg-utils version information.
66 Exit Codes
68 An exit code of 0 indicates success while a non-zero exit code indicates
69 failure. The following failure codes can be returned:
72 Error in command line syntax.
74 One of the files passed on the command line did not exist.
76 A required tool could not be found.
78 The action failed.
80 Examples
82 xdg-open 'http://www.freedesktop.org/'
84 Opens the Freedesktop.org website in the user's default browser
86 xdg-open /tmp/foobar.png
88 Opens the PNG image file /tmp/foobar.png in the user's default image viewing
89 application.
91 _MANUALPAGE
94 usage()
96 cat << _USAGE
97 xdg-open - opens a file or URL in the user's preferred application
99 Synopsis
101 xdg-open { file | URL }
103 xdg-open { --help | --manual | --version }
105 _USAGE
108 #@xdg-utils-common@
110 #----------------------------------------------------------------------------
111 # Common utility functions included in all XDG wrapper scripts
112 #----------------------------------------------------------------------------
114 DEBUG()
116 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && return 0;
117 [ ${XDG_UTILS_DEBUG_LEVEL} -lt $1 ] && return 0;
118 shift
119 echo "$@" >&2
122 #-------------------------------------------------------------
123 # Exit script on successfully completing the desired operation
125 exit_success()
127 if [ $# -gt 0 ]; then
128 echo "$@"
129 echo
132 exit 0
136 #-----------------------------------------
137 # Exit script on malformed arguments, not enough arguments
138 # or missing required option.
139 # prints usage information
141 exit_failure_syntax()
143 if [ $# -gt 0 ]; then
144 echo "xdg-open: $@" >&2
145 echo "Try 'xdg-open --help' for more information." >&2
146 else
147 usage
148 echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
151 exit 1
154 #-------------------------------------------------------------
155 # Exit script on missing file specified on command line
157 exit_failure_file_missing()
159 if [ $# -gt 0 ]; then
160 echo "xdg-open: $@" >&2
163 exit 2
166 #-------------------------------------------------------------
167 # Exit script on failure to locate necessary tool applications
169 exit_failure_operation_impossible()
171 if [ $# -gt 0 ]; then
172 echo "xdg-open: $@" >&2
175 exit 3
178 #-------------------------------------------------------------
179 # Exit script on failure returned by a tool application
181 exit_failure_operation_failed()
183 if [ $# -gt 0 ]; then
184 echo "xdg-open: $@" >&2
187 exit 4
190 #------------------------------------------------------------
191 # Exit script on insufficient permission to read a specified file
193 exit_failure_file_permission_read()
195 if [ $# -gt 0 ]; then
196 echo "xdg-open: $@" >&2
199 exit 5
202 #------------------------------------------------------------
203 # Exit script on insufficient permission to read a specified file
205 exit_failure_file_permission_write()
207 if [ $# -gt 0 ]; then
208 echo "xdg-open: $@" >&2
211 exit 6
214 check_input_file()
216 if [ ! -e "$1" ]; then
217 exit_failure_file_missing "file '$1' does not exist"
219 if [ ! -r "$1" ]; then
220 exit_failure_file_permission_read "no permission to read file '$1'"
224 check_vendor_prefix()
226 file_label="$2"
227 [ -n "$file_label" ] || file_label="filename"
228 file=`basename "$1"`
229 case "$file" in
230 [a-zA-Z]*-*)
231 return
233 esac
235 echo "xdg-open: $file_label '$file' does not have a proper vendor prefix" >&2
236 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is terminated' >&2
237 echo 'with a dash ("-"). An example '"$file_label"' is '"'example-$file'" >&2
238 echo "Use --novendor to override or 'xdg-open --manual' for additional info." >&2
239 exit 1
242 check_output_file()
244 # if the file exists, check if it is writeable
245 # if it does not exists, check if we are allowed to write on the directory
246 if [ -e "$1" ]; then
247 if [ ! -w "$1" ]; then
248 exit_failure_file_permission_write "no permission to write to file '$1'"
250 else
251 DIR=`dirname "$1"`
252 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then
253 exit_failure_file_permission_write "no permission to create file '$1'"
258 #----------------------------------------
259 # Checks for shared commands, e.g. --help
261 check_common_commands()
263 while [ $# -gt 0 ] ; do
264 parm="$1"
265 shift
267 case "$parm" in
268 --help)
269 usage
270 echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info."
271 exit_success
274 --manual)
275 manualpage
276 exit_success
279 --version)
280 echo "xdg-open 1.0"
281 exit_success
283 esac
284 done
287 check_common_commands "$@"
289 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL;
290 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then
291 # Be silent
292 xdg_redirect_output=" > /dev/null 2> /dev/null"
293 else
294 # All output to stderr
295 xdg_redirect_output=" >&2"
298 #--------------------------------------
299 # Checks for known desktop environments
300 # set variable DE to the desktop environments name, lowercase
302 detectDE()
304 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
305 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
306 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
310 #----------------------------------------------------------------------------
311 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4
312 # It also always returns 1 in KDE 3.4 and earlier
313 # Simply return 0 in such case
315 kfmclient_fix_exit_code()
317 version=`kde-config --version 2>/dev/null | grep KDE`
318 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'`
319 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'`
320 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'`
321 test "$major" -gt 3 && return $1
322 test "$minor" -gt 5 && return $1
323 test "$release" -gt 4 && return $1
324 return 0
327 open_kde()
329 which kfmclient >/dev/null || open_generic "$1"
331 kfmclient exec "$1"
332 kfmclient_fix_exit_code $?
334 if [ $? -eq 0 ]; then
335 exit_success
336 else
337 exit_failure_operation_failed
341 open_gnome()
343 which gnome-open >/dev/null || open_generic "$1"
345 gnome-open "$1"
347 if [ $? -eq 0 ]; then
348 exit_success
349 else
350 exit_failure_operation_failed
354 open_xfce()
356 which exo-open >/dev/null || open_generic "$1"
358 exo-open "$1"
360 if [ $? -eq 0 ]; then
361 exit_success
362 else
363 exit_failure_operation_failed
367 open_generic()
369 if which run-mailcap >/dev/null &&
370 (echo "$1" | grep -q '^file://' ||
371 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
373 local file=$(echo "$1" | sed 's%^file://%%')
375 run-mailcap --action=view "$file"
376 else
377 sensible-browser "$1"
380 if [ $? -eq 0 ]; then
381 exit_success
382 else
383 exit_failure_operation_failed
387 [ x"$1" != x"" ] || exit_failure_syntax
389 url=
390 while [ $# -gt 0 ] ; do
391 parm="$1"
392 shift
394 case "$parm" in
396 exit_failure_syntax "unexpected option '$parm'"
400 if [ -n "$url" ] ; then
401 exit_failure_syntax "unexpected argument '$parm'"
403 url="$parm"
405 esac
406 done
408 if [ -z "${url}" ] ; then
409 exit_failure_syntax "file or URL argument missing"
412 detectDE
414 if [ x"$DE" = x"" ]; then
415 DE=generic
418 case "$DE" in
419 kde)
420 open_kde "$url"
423 gnome)
424 open_gnome "$url"
427 xfce)
428 open_xfce "$url"
431 xfce)
432 open_xfce "$url"
435 generic)
436 open_generic "$url"
440 exit_failure_operation_impossible "no method available for opening '$url'"
442 esac