blob: f0304ec0538fe26b3c51be71ecb65433923c1f56 [file] [log] [blame]
mdm@chromium.org56df6d32010-08-31 18:16:49 +00001#!/bin/sh
dkegel@google.comf20da1f2009-06-30 19:16:32 +00002#---------------------------------------------
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#---------------------------------------------
15
16manualpage()
17{
18cat << _MANUALPAGE
19_MANUALPAGE
20}
21
22usage()
23{
24cat << _USAGE
25_USAGE
26}
27
28#@xdg-utils-common@
29
mdm@chromium.org56df6d32010-08-31 18:16:49 +000030# This handles backslashes but not quote marks.
31first_word()
32{
33 read first rest
34 echo "$first"
35}
36
dkegel@google.comf20da1f2009-06-30 19:16:32 +000037open_kde()
38{
mdm@chromium.org56df6d32010-08-31 18:16:49 +000039 if kde-open -v 2>/dev/null 1>&2; then
40 kde-open "$1"
41 else
42 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
43 kfmclient openURL "$1"
44 else
45 kfmclient exec "$1"
46 kfmclient_fix_exit_code $?
47 fi
48 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +000049
50 if [ $? -eq 0 ]; then
51 exit_success
52 else
53 exit_failure_operation_failed
54 fi
55}
56
57open_gnome()
58{
mdm@chromium.org56df6d32010-08-31 18:16:49 +000059 if gvfs-open --help 2>/dev/null 1>&2; then
60 gvfs-open "$1"
61 else
62 gnome-open "$1"
63 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +000064
65 if [ $? -eq 0 ]; then
66 exit_success
67 else
68 exit_failure_operation_failed
69 fi
70}
71
72open_xfce()
73{
74 exo-open "$1"
75
76 if [ $? -eq 0 ]; then
77 exit_success
78 else
79 exit_failure_operation_failed
80 fi
81}
82
mdm@chromium.org56df6d32010-08-31 18:16:49 +000083open_generic_xdg_mime()
84{
85 filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
86 default=`xdg-mime query default "$filetype"`
87 if [ -n "$default" ] ; then
88 xdg_user_dir="$XDG_DATA_HOME"
89 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
90
91 xdg_system_dirs="$XDG_DATA_DIRS"
92 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
93
94 for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
95 file="$x/applications/$default"
96 if [ -r "$file" ] ; then
97 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
98 command_exec=`which $command 2>/dev/null`
99 if [ -x "$command_exec" ] ; then
100 $command_exec "$1"
101 if [ $? -eq 0 ]; then
102 exit_success
103 fi
104 fi
105 fi
106 done
107 fi
108}
109
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000110open_generic()
111{
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000112 # Paths or file:// URLs
113 if (echo "$1" | grep -q '^file://' ||
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000114 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then
115
116 local file=$(echo "$1" | sed 's%^file://%%')
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000117
118 # Decode URLs
119 # TODO
120
121 check_input_file "$file"
122
123 open_generic_xdg_mime "$file"
124
125 if [ -f /etc/debian_version ] &&
126 which run-mailcap 2>/dev/null 1>&2; then
127 run-mailcap --action=view "$file"
128 if [ $? -eq 0 ]; then
129 exit_success
130 fi
131 fi
132
133 if mimeopen -v 2>/dev/null 1>&2; then
134 mimeopen -L -n "$file"
135 if [ $? -eq 0 ]; then
136 exit_success
137 fi
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000138 fi
139 fi
140
141 IFS=":"
142 for browser in $BROWSER; do
143 if [ x"$browser" != x"" ]; then
144
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000145 browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
146 if [ $? -ne 0 ]; then
147 browser_with_arg=$browser;
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000148 fi
149
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000150 if [ x"$browser_with_arg" = x"$browser" ]; then
151 "$browser" "$1";
152 else eval '$browser_with_arg'$xdg_redirect_output;
153 fi
154
155 if [ $? -eq 0 ]; then
156 exit_success;
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000157 fi
158 fi
159 done
160
161 exit_failure_operation_impossible "no method available for opening '$1'"
162}
163
164[ x"$1" != x"" ] || exit_failure_syntax
165
166url=
167while [ $# -gt 0 ] ; do
168 parm="$1"
169 shift
170
171 case "$parm" in
172 -*)
173 exit_failure_syntax "unexpected option '$parm'"
174 ;;
175
176 *)
177 if [ -n "$url" ] ; then
178 exit_failure_syntax "unexpected argument '$parm'"
179 fi
180 url="$parm"
181 ;;
182 esac
183done
184
185if [ -z "${url}" ] ; then
186 exit_failure_syntax "file or URL argument missing"
187fi
188
189detectDE
190
191if [ x"$DE" = x"" ]; then
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000192 DE=generic
193fi
194
mdm@chromium.org56df6d32010-08-31 18:16:49 +0000195# if BROWSER variable is not set, check some well known browsers instead
196if [ x"$BROWSER" = x"" ]; then
197 BROWSER=links2:links:lynx:w3m
198 if [ -n "$DISPLAY" ]; then
199 BROWSER=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
200 fi
201fi
202
dkegel@google.comf20da1f2009-06-30 19:16:32 +0000203case "$DE" in
204 kde)
205 open_kde "$url"
206 ;;
207
208 gnome)
209 open_gnome "$url"
210 ;;
211
212 xfce)
213 open_xfce "$url"
214 ;;
215
216 generic)
217 open_generic "$url"
218 ;;
219
220 *)
221 exit_failure_operation_impossible "no method available for opening '$url'"
222 ;;
223esac