OLD | NEW |
1 #!/bin/bash | 1 #!/bin/sh |
2 #--------------------------------------------- | 2 #--------------------------------------------- |
3 # xdg-open | 3 # xdg-open |
4 # | 4 # |
5 # Utility script to open a URL in the registered default application. | 5 # Utility script to open a URL in the registered default application. |
6 # | 6 # |
7 # Refer to the usage() function below for usage. | 7 # Refer to the usage() function below for usage. |
8 # | 8 # |
9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> | 9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> | 10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
11 # | 11 # |
12 # LICENSE: | 12 # LICENSE: |
13 # | 13 # |
14 #--------------------------------------------- | 14 #--------------------------------------------- |
15 | 15 |
16 manualpage() | 16 manualpage() |
17 { | 17 { |
18 cat << _MANUALPAGE | 18 cat << _MANUALPAGE |
19 _MANUALPAGE | 19 _MANUALPAGE |
20 } | 20 } |
21 | 21 |
22 usage() | 22 usage() |
23 { | 23 { |
24 cat << _USAGE | 24 cat << _USAGE |
25 _USAGE | 25 _USAGE |
26 } | 26 } |
27 | 27 |
28 #@xdg-utils-common@ | 28 #@xdg-utils-common@ |
29 | 29 |
| 30 # This handles backslashes but not quote marks. |
| 31 first_word() |
| 32 { |
| 33 read first rest |
| 34 echo "$first" |
| 35 } |
| 36 |
30 open_kde() | 37 open_kde() |
31 { | 38 { |
32 kfmclient exec "$1" | 39 if kde-open -v 2>/dev/null 1>&2; then |
33 kfmclient_fix_exit_code $? | 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 |
34 | 49 |
35 if [ $? -eq 0 ]; then | 50 if [ $? -eq 0 ]; then |
36 exit_success | 51 exit_success |
37 else | 52 else |
38 exit_failure_operation_failed | 53 exit_failure_operation_failed |
39 fi | 54 fi |
40 } | 55 } |
41 | 56 |
42 open_gnome() | 57 open_gnome() |
43 { | 58 { |
44 gnome-open "$1" | 59 if gvfs-open --help 2>/dev/null 1>&2; then |
| 60 gvfs-open "$1" |
| 61 else |
| 62 gnome-open "$1" |
| 63 fi |
45 | 64 |
46 if [ $? -eq 0 ]; then | 65 if [ $? -eq 0 ]; then |
47 exit_success | 66 exit_success |
48 else | 67 else |
49 exit_failure_operation_failed | 68 exit_failure_operation_failed |
50 fi | 69 fi |
51 } | 70 } |
52 | 71 |
53 open_xfce() | 72 open_xfce() |
54 { | 73 { |
55 exo-open "$1" | 74 exo-open "$1" |
56 | 75 |
57 if [ $? -eq 0 ]; then | 76 if [ $? -eq 0 ]; then |
58 exit_success | 77 exit_success |
59 else | 78 else |
60 exit_failure_operation_failed | 79 exit_failure_operation_failed |
61 fi | 80 fi |
62 } | 81 } |
63 | 82 |
| 83 open_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/shar
e/ |
| 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 |
64 open_generic() | 110 open_generic() |
65 { | 111 { |
66 if mimeopen -v 2>/dev/null 1>&2; then | 112 # Paths or file:// URLs |
67 mimeopen -n "$1" | 113 if (echo "$1" | grep -q '^file://' || |
68 if [ $? -eq 0 ]; then | |
69 exit_success | |
70 fi | |
71 fi | |
72 | |
73 if which run-mailcap 2>/dev/null 1>&2 && | |
74 (echo "$1" | grep -q '^file://' || | |
75 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then | 114 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then |
76 | 115 |
77 local file=$(echo "$1" | sed 's%^file://%%') | 116 local file=$(echo "$1" | sed 's%^file://%%') |
78 run-mailcap --action=view "$file" | 117 |
79 if [ $? -eq 0 ]; then | 118 # Decode URLs |
80 exit_success | 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 |
81 fi | 138 fi |
82 fi | 139 fi |
83 | 140 |
84 IFS=":" | 141 IFS=":" |
85 for browser in $BROWSER; do | 142 for browser in $BROWSER; do |
86 if [ x"$browser" != x"" ]; then | 143 if [ x"$browser" != x"" ]; then |
87 | 144 |
88 IFS=' ' | 145 browser_with_arg=`printf "$browser" "$1" 2>/dev/null` |
89 browser_with_arg=${browser//'%s'/"$1"} | 146 if [ $? -ne 0 ]; then |
90 | 147 browser_with_arg=$browser; |
91 if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1"; | |
92 else $browser_with_arg; | |
93 fi | 148 fi |
94 | 149 |
95 if [ $? -eq 0 ]; then exit_success; | 150 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; |
96 fi | 157 fi |
97 fi | 158 fi |
98 done | 159 done |
99 | 160 |
100 exit_failure_operation_impossible "no method available for opening '$1'" | 161 exit_failure_operation_impossible "no method available for opening '$1'" |
101 } | 162 } |
102 | 163 |
103 [ x"$1" != x"" ] || exit_failure_syntax | 164 [ x"$1" != x"" ] || exit_failure_syntax |
104 | 165 |
105 url= | 166 url= |
(...skipping 15 matching lines...) Expand all Loading... |
121 esac | 182 esac |
122 done | 183 done |
123 | 184 |
124 if [ -z "${url}" ] ; then | 185 if [ -z "${url}" ] ; then |
125 exit_failure_syntax "file or URL argument missing" | 186 exit_failure_syntax "file or URL argument missing" |
126 fi | 187 fi |
127 | 188 |
128 detectDE | 189 detectDE |
129 | 190 |
130 if [ x"$DE" = x"" ]; then | 191 if [ x"$DE" = x"" ]; then |
131 # if BROWSER variable is not set, check some well known browsers instead | 192 DE=generic |
132 if [ x"$BROWSER" = x"" ]; then | 193 fi |
133 BROWSER=firefox:mozilla:netscape | 194 |
| 195 # if BROWSER variable is not set, check some well known browsers instead |
| 196 if [ x"$BROWSER" = x"" ]; then |
| 197 BROWSER=links2:links:lynx:w3m |
| 198 if [ -n "$DISPLAY" ]; then |
| 199 BROWSER=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrom
e:$BROWSER |
134 fi | 200 fi |
135 DE=generic | |
136 fi | 201 fi |
137 | 202 |
138 case "$DE" in | 203 case "$DE" in |
139 kde) | 204 kde) |
140 open_kde "$url" | 205 open_kde "$url" |
141 ;; | 206 ;; |
142 | 207 |
143 gnome) | 208 gnome) |
144 open_gnome "$url" | 209 open_gnome "$url" |
145 ;; | 210 ;; |
146 | 211 |
147 xfce) | 212 xfce) |
148 open_xfce "$url" | 213 open_xfce "$url" |
149 ;; | 214 ;; |
150 | 215 |
151 generic) | 216 generic) |
152 open_generic "$url" | 217 open_generic "$url" |
153 ;; | 218 ;; |
154 | 219 |
155 *) | 220 *) |
156 exit_failure_operation_impossible "no method available for opening '$url'" | 221 exit_failure_operation_impossible "no method available for opening '$url'" |
157 ;; | 222 ;; |
158 esac | 223 esac |
OLD | NEW |