source: src-sh/pc-extractoverlay/ports-overlay/usr/local/bin/xdg-open @ 793b028

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2
Last change on this file since 793b028 was 793b028, checked in by Kris Moore <kris@…>, 11 months ago

Update xdg-open to use kioclient exec for opening KDE stuff

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