2
#---------------------------------------------
5
# Utility script to open a URL in the registered default application.
7
# Refer to the usage() function below for usage.
9
# Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
10
# Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
11
# Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
12
# Copyright 2006, Jeremy White <jwhite@codeweavers.com>
16
#---------------------------------------------
32
# This handles backslashes but not quote marks.
39
# Get the value of a key in a desktop file's Desktop Entry group.
40
# Example: Use get_key foo.desktop Exec
41
# to get the values of the Exec= key for the Desktop Entry group.
46
local desktop_entry=""
56
# Reset match flag for other groups
61
# Only match Desktop Entry group
62
if [ -n "${desktop_entry}" ]
64
echo "${line}" | cut -d= -f 2-
78
exit_failure_operation_failed
89
exit_failure_operation_failed
95
if [ -n "${KDE_SESSION_VERSION}" ]; then
96
case "${KDE_SESSION_VERSION}" in
101
kde-open${KDE_SESSION_VERSION} "$1"
106
kfmclient_fix_exit_code $?
109
if [ $? -eq 0 ]; then
112
exit_failure_operation_failed
118
if gvfs-open --help 2>/dev/null 1>&2; then
124
if [ $? -eq 0 ]; then
127
exit_failure_operation_failed
133
if gvfs-open --help 2>/dev/null 1>&2; then
139
if [ $? -eq 0 ]; then
142
exit_failure_operation_failed
150
if [ $? -eq 0 ]; then
153
exit_failure_operation_failed
159
enlightenment_open "$1"
161
if [ $? -eq 0 ]; then
164
exit_failure_operation_failed
168
#-----------------------------------------
169
# Recursively search .desktop file
171
search_desktop_file()
178
# look for both vendor-app.desktop, vendor/app.desktop
179
if [ -r "$dir/$default" ]; then
181
elif [ -r "$dir/`echo $default | sed -e 's|-|/|'`" ]; then
182
file="$dir/`echo $default | sed -e 's|-|/|'`"
185
if [ -r "$file" ] ; then
186
command="$(get_key "${file}" "Exec" | first_word)"
187
command_exec=`which $command 2>/dev/null`
188
icon="$(get_key "${file}" "Icon")"
189
# FIXME: Actually LC_MESSAGES should be used as described in
190
# http://standards.freedesktop.org/desktop-entry-spec/latest/ar01s04.html
191
localised_name="$(get_key "${file}" "Name")"
192
set -- $(get_key "${file}" "Exec" | last_word)
193
# We need to replace any occurrence of "%f", "%F" and
194
# the like by the target file. We examine each
195
# argument and append the modified argument to the
199
while [ $args -gt 0 ]; do
203
arg="${localised_name}"
216
set -- "$@" "--icon" "$icon"
224
args=$(( $args - 1 ))
226
[ $replaced -eq 1 ] || set -- "$@" "$target"
229
if [ $? -eq 0 ]; then
235
[ -d "$d" ] && search_desktop_file "$default" "$d" "$target"
240
open_generic_xdg_mime()
243
default=`xdg-mime query default "$filetype"`
244
if [ -n "$default" ] ; then
245
xdg_user_dir="$XDG_DATA_HOME"
246
[ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
248
xdg_system_dirs="$XDG_DATA_DIRS"
249
[ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
251
DEBUG 3 "$xdg_user_dir:$xdg_system_dirs"
252
for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
253
search_desktop_file "$default" "$x/applications/" "$1"
258
open_generic_xdg_file_mime()
260
filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
261
open_generic_xdg_mime "$1" "$filetype"
264
open_generic_xdg_x_scheme_handler()
266
scheme="`echo $1 | sed -n 's/\(^[[:alnum:]+\.-]*\):.*$/\1/p'`"
267
if [ -n $scheme ]; then
268
filetype="x-scheme-handler/$scheme"
269
open_generic_xdg_mime "$1" "$filetype"
275
# Paths or file:// URLs
276
if (echo "$1" | grep -q '^file://' ||
277
! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:'); then
282
if echo "$file" | grep -q '^file:///'; then
284
file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
286
file_check=${file%%#*}
287
file_check=${file_check%%\?*}
288
check_input_file "$file_check"
290
filetype=`xdg-mime query filetype "$file_check" | sed "s/;.*//"`
291
open_generic_xdg_mime "$file" "$filetype"
293
if which run-mailcap 2>/dev/null 1>&2; then
294
run-mailcap --action=view "$file"
295
if [ $? -eq 0 ]; then
300
if mimeopen -v 2>/dev/null 1>&2; then
301
mimeopen -L -n "$file"
302
if [ $? -eq 0 ]; then
308
open_generic_xdg_x_scheme_handler "$1"
311
for browser in $BROWSER; do
312
if [ x"$browser" != x"" ]; then
314
browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
315
if [ $? -ne 0 ]; then
316
browser_with_arg=$browser;
319
if [ x"$browser_with_arg" = x"$browser" ]; then
320
eval '$browser "$1"'$xdg_redirect_output;
321
else eval '$browser_with_arg'$xdg_redirect_output;
324
if [ $? -eq 0 ]; then
330
exit_failure_operation_impossible "no method available for opening '$1'"
335
# pcmanfm only knows how to handle file:// urls and filepaths, it seems.
336
if (echo "$1" | grep -q '^file://' ||
337
! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:')
341
# handle relative paths
342
if ! echo "$file" | egrep -q '^(file://)?/'; then
352
if [ $? -eq 0 ]; then
355
exit_failure_operation_failed
359
[ x"$1" != x"" ] || exit_failure_syntax
362
while [ $# -gt 0 ] ; do
368
exit_failure_syntax "unexpected option '$parm'"
372
if [ -n "$url" ] ; then
373
exit_failure_syntax "unexpected argument '$parm'"
380
if [ -z "${url}" ] ; then
381
exit_failure_syntax "file or URL argument missing"
386
if [ x"$DE" = x"" ]; then
390
DEBUG 2 "Selected DE $DE"
392
# sanitize BROWSER (avoid caling ourselves in particular)
394
*:"xdg-open"|"xdg-open":*)
395
BROWSER=$(echo $BROWSER | sed -e 's|:xdg-open||g' -e 's|xdg-open:||g')
402
# if BROWSER variable is not set, check some well known browsers instead
403
if [ x"$BROWSER" = x"" ]; then
404
BROWSER=links2:elinks:links:lynx:w3m
405
if [ -n "$DISPLAY" ]; then
406
BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
432
open_enlightenment "$url"
448
exit_failure_operation_impossible "no method available for opening '$url'"