summaryrefslogtreecommitdiff
path: root/scripts/xdg-open.in
blob: 176d3e4c550cb90440050637bd5407b7968d3859 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/bin/sh
#---------------------------------------------
#   xdg-open
#
#   Utility script to open a URL in the registered default application.
#
#   Refer to the usage() function below for usage.
#
#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#---------------------------------------------

manualpage()
{
cat << _MANUALPAGE
_MANUALPAGE
}

usage()
{
cat << _USAGE
_USAGE
}

#@xdg-utils-common@

# This handles backslashes but not quote marks.
first_word()
{
    read first rest
    echo "$first"
}

last_word()
{
    read first rest
    echo "$rest"
}

open_darwin()
{
    open "$1"

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_kde()
{
    if kde-open -v 2>/dev/null 1>&2; then
        kde-open "$1"
    else
        if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
            kfmclient openURL "$1"
        else
            kfmclient exec "$1"
            kfmclient_fix_exit_code $?
        fi
    fi

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_gnome()
{
    if gvfs-open --help 2>/dev/null 1>&2; then
        gvfs-open "$1"
    else
        gnome-open "$1"
    fi

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_xfce()
{
    exo-open "$1"

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

open_generic_xdg_mime()
{
    filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
    default=`xdg-mime query default "$filetype"`
    if [ -n "$default" ] ; then
        xdg_user_dir="$XDG_DATA_HOME"
        [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"

        xdg_system_dirs="$XDG_DATA_DIRS"
        [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/

        for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
            local file
            # look for both vendor-app.desktop, vendor/app.desktop
            if [ -r "$x/applications/$default" ]; then
              file="$x/applications/$default"
            elif [ -r "$x/applications/`echo $default | sed -e 's|-|/|'`" ]; then
              file="$x/applications/`echo $default | sed -e 's|-|/|'`"
            fi

            if [ -r "$file" ] ; then
                command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
                command_exec=`which $command 2>/dev/null`
                arguments="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | last_word`"
                arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$1"'"*g'`"
                if [ -x "$command_exec" ] ; then
                    if echo $arguments | grep -iq '%[fFuU]' ; then
                        eval $command_exec $arguments_exec
                    else
                        eval $command_exec $arguments_exec "$1"
                    fi

                    if [ $? -eq 0 ]; then
                        exit_success
                    fi
                fi
            fi
        done
    fi
}

open_generic()
{
    # Paths or file:// URLs
    if (echo "$1" | grep -q '^file://' ||
        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then

        local file="$1"

        # Decode URLs
        if echo "$file" | grep -q '^file:///'; then
            file=${file#file://}
            file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
        fi
        check_input_file "$file"

        open_generic_xdg_mime "$file"

        if [ -f /etc/debian_version ] &&
            which run-mailcap 2>/dev/null 1>&2; then
            run-mailcap --action=view "$file"
            if [ $? -eq 0 ]; then
                exit_success
            fi
        fi

        if mimeopen -v 2>/dev/null 1>&2; then
            mimeopen -L -n "$file"
            if [ $? -eq 0 ]; then
                exit_success
            fi
        fi
    fi

    IFS=":"
    for browser in $BROWSER; do
        if [ x"$browser" != x"" ]; then

            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
            if [ $? -ne 0 ]; then
                browser_with_arg=$browser;
            fi

            if [ x"$browser_with_arg" = x"$browser" ]; then
                "$browser" "$1";
            else eval '$browser_with_arg'$xdg_redirect_output;
            fi

            if [ $? -eq 0 ]; then
                exit_success;
            fi
        fi
    done

    exit_failure_operation_impossible "no method available for opening '$1'"
}

open_lxde()
{
    # pcmanfm only knows how to handle file:// urls and filepaths, it seems.
    if (echo "$1" | grep -q '^file://' ||
        ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:')
    then
        local file="$(echo "$1" | sed 's%^file://%%')"

        # handle relative paths
        if ! echo "$file" | grep -q '^/'; then
            file="$(pwd)/$file"
        fi

        pcmanfm "$file"

    else
        open_generic "$1"
    fi

    if [ $? -eq 0 ]; then
        exit_success
    else
        exit_failure_operation_failed
    fi
}

[ x"$1" != x"" ] || exit_failure_syntax

url=
while [ $# -gt 0 ] ; do
    parm="$1"
    shift

    case "$parm" in
      -*)
        exit_failure_syntax "unexpected option '$parm'"
        ;;

      *)
        if [ -n "$url" ] ; then
            exit_failure_syntax "unexpected argument '$parm'"
        fi
        url="$parm"
        ;;
    esac
done

if [ -z "${url}" ] ; then
    exit_failure_syntax "file or URL argument missing"
fi

detectDE

if [ x"$DE" = x"" ]; then
    DE=generic
fi

# if BROWSER variable is not set, check some well known browsers instead
if [ x"$BROWSER" = x"" ]; then
    BROWSER=links2:links:lynx:w3m
    if [ -n "$DISPLAY" ]; then
        BROWSER=firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
    fi
fi

case "$DE" in
    kde)
    open_kde "$url"
    ;;

    gnome*)
    open_gnome "$url"
    ;;

    xfce)
    open_xfce "$url"
    ;;

    lxde)
    open_lxde "$url"
    ;;

    generic)
    open_generic "$url"
    ;;

    *)
    exit_failure_operation_impossible "no method available for opening '$url'"
    ;;
esac