~saiarcot895/ubuntu/wily/xdg-utils/fix-multiple-execs

« back to all changes in this revision

Viewing changes to scripts/xdg-open.in

  • Committer: Package Import Robot
  • Author(s): Per Olofsson
  • Date: 2011-12-12 12:04:25 UTC
  • mfrom: (1.2.8)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: package-import@ubuntu.com-20111212120425-0rdnrl8tn45vz171
Tags: 1.1.0~rc1+git20111210-1
* New upstream git snapshot b961235b197647d6649ef3d48d7cc2cecafe3d47.
* Drop patches applied upstream:
  - xdg-mime-follow-symlinks.diff
  - x-www-browser.diff
  - xdg-mime-generic-use-mimetype.diff
  - bashisms.diff
  - xdg-screensaver-check-gnome.diff
* Modify patches to make changes to scripts/xdg-*.in, instead of the
  generated scripts.
* Also check for ~/.mutt/muttrc when deciding whether to use mutt as
  MUA or not. Thanks to martin f krafft. Closes: #648733.
* Build scripts/xdg-* from *.in files.
* Build-depend on xmlto and awk, as they are needed when building the
  scripts.
* Also build the manual pages.
* Bump Standards-Version to 3.9.2. No changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#---------------------------------------------
 
17
 
 
18
manualpage()
 
19
{
 
20
cat << _MANUALPAGE
 
21
_MANUALPAGE
 
22
}
 
23
 
 
24
usage()
 
25
{
 
26
cat << _USAGE
 
27
_USAGE
 
28
}
 
29
 
 
30
#@xdg-utils-common@
 
31
 
 
32
# This handles backslashes but not quote marks.
 
33
first_word()
 
34
{
 
35
    read first rest
 
36
    echo "$first"
 
37
}
 
38
 
 
39
last_word()
 
40
{
 
41
    read first rest
 
42
    echo "$rest"
 
43
}
 
44
 
 
45
open_darwin()
 
46
{
 
47
    open "$1"
 
48
 
 
49
    if [ $? -eq 0 ]; then
 
50
        exit_success
 
51
    else
 
52
        exit_failure_operation_failed
 
53
    fi
 
54
}
 
55
 
 
56
open_kde()
 
57
{
 
58
    if kde-open -v 2>/dev/null 1>&2; then
 
59
        kde-open "$1"
 
60
    else
 
61
        if [ x"$KDE_SESSION_VERSION" = x"4" ]; then
 
62
            kfmclient openURL "$1"
 
63
        else
 
64
            kfmclient exec "$1"
 
65
            kfmclient_fix_exit_code $?
 
66
        fi
 
67
    fi
 
68
 
 
69
    if [ $? -eq 0 ]; then
 
70
        exit_success
 
71
    else
 
72
        exit_failure_operation_failed
 
73
    fi
 
74
}
 
75
 
 
76
open_gnome()
 
77
{
 
78
    if gvfs-open --help 2>/dev/null 1>&2; then
 
79
        gvfs-open "$1"
 
80
    else
 
81
        gnome-open "$1"
 
82
    fi
 
83
 
 
84
    if [ $? -eq 0 ]; then
 
85
        exit_success
 
86
    else
 
87
        exit_failure_operation_failed
 
88
    fi
 
89
}
 
90
 
 
91
open_mate()
 
92
{
 
93
    if gvfs-open --help 2>/dev/null 1>&2; then
 
94
        gvfs-open "$1"
 
95
    else
 
96
        mate-open "$1"
 
97
    fi
 
98
 
 
99
    if [ $? -eq 0 ]; then
 
100
        exit_success
 
101
    else
 
102
        exit_failure_operation_failed
 
103
    fi
 
104
}
 
105
 
 
106
open_xfce()
 
107
{
 
108
    exo-open "$1"
 
109
 
 
110
    if [ $? -eq 0 ]; then
 
111
        exit_success
 
112
    else
 
113
        exit_failure_operation_failed
 
114
    fi
 
115
}
 
116
 
 
117
open_generic_xdg_mime()
 
118
{
 
119
    filetype="$2"
 
120
    default=`xdg-mime query default "$filetype"`
 
121
    if [ -n "$default" ] ; then
 
122
        xdg_user_dir="$XDG_DATA_HOME"
 
123
        [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share"
 
124
 
 
125
        xdg_system_dirs="$XDG_DATA_DIRS"
 
126
        [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
 
127
 
 
128
DEBUG 3 "$xdg_user_dir:$xdg_system_dirs"
 
129
        for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do
 
130
            local file
 
131
            # look for both vendor-app.desktop, vendor/app.desktop
 
132
            if [ -r "$x/applications/$default" ]; then
 
133
              file="$x/applications/$default"
 
134
            elif [ -r "$x/applications/`echo $default | sed -e 's|-|/|'`" ]; then
 
135
              file="$x/applications/`echo $default | sed -e 's|-|/|'`"
 
136
            fi
 
137
 
 
138
            if [ -r "$file" ] ; then
 
139
                command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | first_word`"
 
140
                command_exec=`which $command 2>/dev/null`
 
141
                arguments="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- | last_word`"
 
142
                arguments_exec="`echo $arguments | sed -e 's*%[fFuU]*"'"$1"'"*g'`"
 
143
                if [ -x "$command_exec" ] ; then
 
144
                    if echo $arguments | grep -iq '%[fFuU]' ; then
 
145
                        eval $command_exec $arguments_exec
 
146
                    else
 
147
                        eval $command_exec $arguments_exec "$1"
 
148
                    fi
 
149
 
 
150
                    if [ $? -eq 0 ]; then
 
151
                        exit_success
 
152
                    fi
 
153
                fi
 
154
            fi
 
155
        done
 
156
    fi
 
157
}
 
158
 
 
159
open_generic_xdg_file_mime()
 
160
{
 
161
    filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
 
162
    open_generic_xdg_mime "$1" "$filetype"
 
163
}
 
164
 
 
165
open_generic_xdg_x_scheme_handler()
 
166
{
 
167
    scheme="`echo $1 | sed -n 's/\(^[[:alnum:]+\.-]*\):.*$/\1/p'`"
 
168
    if [ -n $scheme ]; then
 
169
        filetype="x-scheme-handler/$scheme"
 
170
        open_generic_xdg_mime "$1" "$filetype"
 
171
    fi
 
172
}
 
173
 
 
174
open_generic()
 
175
{
 
176
    # Paths or file:// URLs
 
177
    if (echo "$1" | grep -q '^file://' ||
 
178
        ! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:'); then
 
179
 
 
180
        local file="$1"
 
181
 
 
182
        # Decode URLs
 
183
        if echo "$file" | grep -q '^file:///'; then
 
184
            file=${file#file://}
 
185
            local printf=printf
 
186
            if [ -x /usr/bin/printf ]; then
 
187
                printf=/usr/bin/printf
 
188
            fi
 
189
            file="$($printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
 
190
        fi
 
191
        check_input_file "$file"
 
192
 
 
193
        open_generic_xdg_file_mime "$file"
 
194
 
 
195
        if [ -f /etc/debian_version ] &&
 
196
            which run-mailcap 2>/dev/null 1>&2; then
 
197
            run-mailcap --action=view "$file"
 
198
            if [ $? -eq 0 ]; then
 
199
                exit_success
 
200
            fi
 
201
        fi
 
202
 
 
203
        if mimeopen -v 2>/dev/null 1>&2; then
 
204
            mimeopen -L -n "$file"
 
205
            if [ $? -eq 0 ]; then
 
206
                exit_success
 
207
            fi
 
208
        fi
 
209
    fi
 
210
 
 
211
    open_generic_xdg_x_scheme_handler "$1"
 
212
 
 
213
    OLDIFS="$IFS"
 
214
    IFS=":"
 
215
    for browser in $BROWSER; do
 
216
        IFS="$OLDIFS"
 
217
        if [ x"$browser" != x"" ]; then
 
218
 
 
219
            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
 
220
            if [ $? -ne 0 ]; then
 
221
                browser_with_arg=$browser;
 
222
            fi
 
223
 
 
224
            if [ x"$browser_with_arg" = x"$browser" ]; then
 
225
                eval '$browser $1'$xdg_redirect_output;
 
226
            else eval '$browser_with_arg'$xdg_redirect_output;
 
227
            fi
 
228
 
 
229
            if [ $? -eq 0 ]; then
 
230
                exit_success;
 
231
            fi
 
232
        fi
 
233
    done
 
234
 
 
235
    exit_failure_operation_impossible "no method available for opening '$1'"
 
236
}
 
237
 
 
238
open_lxde()
 
239
{
 
240
    # pcmanfm only knows how to handle file:// urls and filepaths, it seems.
 
241
    if (echo "$1" | grep -q '^file://' ||
 
242
        ! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:')
 
243
    then
 
244
        local file="$(echo "$1" | sed 's%^file://%%')"
 
245
 
 
246
        # handle relative paths
 
247
        if ! echo "$file" | grep -q '^/'; then
 
248
            file="$(pwd)/$file"
 
249
        fi
 
250
 
 
251
        pcmanfm "$file"
 
252
 
 
253
    else
 
254
        open_generic "$1"
 
255
    fi
 
256
 
 
257
    if [ $? -eq 0 ]; then
 
258
        exit_success
 
259
    else
 
260
        exit_failure_operation_failed
 
261
    fi
 
262
}
 
263
 
 
264
[ x"$1" != x"" ] || exit_failure_syntax
 
265
 
 
266
url=
 
267
while [ $# -gt 0 ] ; do
 
268
    parm="$1"
 
269
    shift
 
270
 
 
271
    case "$parm" in
 
272
      -*)
 
273
        exit_failure_syntax "unexpected option '$parm'"
 
274
        ;;
 
275
 
 
276
      *)
 
277
        if [ -n "$url" ] ; then
 
278
            exit_failure_syntax "unexpected argument '$parm'"
 
279
        fi
 
280
        url="$parm"
 
281
        ;;
 
282
    esac
 
283
done
 
284
 
 
285
if [ -z "${url}" ] ; then
 
286
    exit_failure_syntax "file or URL argument missing"
 
287
fi
 
288
 
 
289
detectDE
 
290
 
 
291
if [ x"$DE" = x"" ]; then
 
292
    DE=generic
 
293
fi
 
294
 
 
295
DEBUG 2 "Selected DE $DE"
 
296
 
 
297
# if BROWSER variable is not set, check some well known browsers instead
 
298
if [ x"$BROWSER" = x"" ]; then
 
299
    BROWSER=links2:elinks:links:lynx:w3m
 
300
    if [ -n "$DISPLAY" ]; then
 
301
        BROWSER=x-www-browser:firefox:seamonkey:mozilla:epiphany:konqueror:chromium-browser:google-chrome:$BROWSER
 
302
    fi
 
303
fi
 
304
 
 
305
case "$DE" in
 
306
    kde)
 
307
    open_kde "$url"
 
308
    ;;
 
309
 
 
310
    gnome*)
 
311
    open_gnome "$url"
 
312
    ;;
 
313
 
 
314
    mate)
 
315
    open_mate "$url"
 
316
    ;;
 
317
 
 
318
    xfce)
 
319
    open_xfce "$url"
 
320
    ;;
 
321
 
 
322
    lxde)
 
323
    open_lxde "$url"
 
324
    ;;
 
325
 
 
326
    generic)
 
327
    open_generic "$url"
 
328
    ;;
 
329
 
 
330
    *)
 
331
    exit_failure_operation_impossible "no method available for opening '$url'"
 
332
    ;;
 
333
esac