~ubuntu-branches/ubuntu/wily/xdg-utils/wily

« back to all changes in this revision

Viewing changes to .pc/xdg-open-browser-multiword.diff/scripts/xdg-open.in

  • Committer: Package Import Robot
  • Author(s): Sean Davis
  • Date: 2015-09-13 12:39:10 UTC
  • mfrom: (13.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20150913123910-uba8c1sph2iwnqlr
Tags: 1.1.0~rc3+git20150907-1ubuntu1
* Merge from Debian unstable.  Remaining changes: (LP: #1495273)
  - debian/patches:
    * gnome-3.0.diff: Correctly open preferred browser with 
      gnome-open (LP: #670128)
    * lp779156-lubuntu.diff: Add open_lxde and run_sylpheed function
      for improved LXDE and sylpheed support (LP: #779156)
    * xdg-screensaver-restore-timeout.diff: restore previous X11 
      screensaver timeout when xdg-screensaver resume is used (LP: #1363540)
* New upstream release fixes the following bugs:
  - xdg-mime query filetype does not work on KDE 5 (LP: #1454833)
  - xdg-open doesn't properly detect Xfce/Xubuntu (LP: #1388922)
  - xserver-blanking patch in Ubuntu duplicates code (LP: #1330386)
  - Typo in manpage of 'xdg-icon-resource' (LP: #996304)
  - xdg-open (to gnome-open) fails to launch file:// URL with query string 
    (LP: #396162)

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
last_word()
 
34
{
 
35
    read first rest
 
36
    echo "$rest"
 
37
}
 
38
 
 
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.
 
42
get_key()
 
43
{
 
44
    local file="${1}"
 
45
    local key="${2}"
 
46
    local desktop_entry=""
 
47
 
 
48
    IFS_="${IFS}"
 
49
    IFS=""
 
50
    while read line
 
51
    do
 
52
        case "$line" in
 
53
            "[Desktop Entry]")
 
54
                desktop_entry="y"
 
55
            ;;
 
56
            # Reset match flag for other groups
 
57
            "["*)
 
58
                desktop_entry=""
 
59
            ;;
 
60
            "${key}="*)
 
61
                # Only match Desktop Entry group
 
62
                if [ -n "${desktop_entry}" ]
 
63
                then
 
64
                    echo "${line}" | cut -d= -f 2-
 
65
                fi
 
66
        esac
 
67
    done < "${file}"
 
68
    IFS="${IFS_}"
 
69
}
 
70
 
 
71
open_cygwin()
 
72
{
 
73
    cygstart "$1"
 
74
 
 
75
    if [ $? -eq 0 ]; then
 
76
        exit_success
 
77
    else
 
78
        exit_failure_operation_failed
 
79
    fi
 
80
}
 
81
 
 
82
open_darwin()
 
83
{
 
84
    open "$1"
 
85
 
 
86
    if [ $? -eq 0 ]; then
 
87
        exit_success
 
88
    else
 
89
        exit_failure_operation_failed
 
90
    fi
 
91
}
 
92
 
 
93
open_kde()
 
94
{
 
95
    if [ -n "${KDE_SESSION_VERSION}" ]; then
 
96
      case "${KDE_SESSION_VERSION}" in
 
97
        4)
 
98
          kde-open "$1"
 
99
        ;;
 
100
        5)
 
101
          kde-open${KDE_SESSION_VERSION} "$1"
 
102
        ;;
 
103
      esac
 
104
    else
 
105
        kfmclient exec "$1"
 
106
        kfmclient_fix_exit_code $?
 
107
    fi
 
108
 
 
109
    if [ $? -eq 0 ]; then
 
110
        exit_success
 
111
    else
 
112
        exit_failure_operation_failed
 
113
    fi
 
114
}
 
115
 
 
116
open_gnome()
 
117
{
 
118
    if gvfs-open --help 2>/dev/null 1>&2; then
 
119
        gvfs-open "$1"
 
120
    else
 
121
        gnome-open "$1"
 
122
    fi
 
123
 
 
124
    if [ $? -eq 0 ]; then
 
125
        exit_success
 
126
    else
 
127
        exit_failure_operation_failed
 
128
    fi
 
129
}
 
130
 
 
131
open_mate()
 
132
{
 
133
    if gvfs-open --help 2>/dev/null 1>&2; then
 
134
        gvfs-open "$1"
 
135
    else
 
136
        mate-open "$1"
 
137
    fi
 
138
 
 
139
    if [ $? -eq 0 ]; then
 
140
        exit_success
 
141
    else
 
142
        exit_failure_operation_failed
 
143
    fi
 
144
}
 
145
 
 
146
open_xfce()
 
147
{
 
148
    exo-open "$1"
 
149
 
 
150
    if [ $? -eq 0 ]; then
 
151
        exit_success
 
152
    else
 
153
        exit_failure_operation_failed
 
154
    fi
 
155
}
 
156
 
 
157
open_enlightenment()
 
158
{
 
159
    enlightenment_open "$1"
 
160
 
 
161
    if [ $? -eq 0 ]; then
 
162
        exit_success
 
163
    else
 
164
        exit_failure_operation_failed
 
165
    fi
 
166
}
 
167
 
 
168
#-----------------------------------------
 
169
# Recursively search .desktop file
 
170
 
 
171
search_desktop_file()
 
172
{
 
173
    local default="$1"
 
174
    local dir="$2"
 
175
    local target="$3"
 
176
 
 
177
    local file=""
 
178
    # look for both vendor-app.desktop, vendor/app.desktop
 
179
    if [ -r "$dir/$default" ]; then
 
180
      file="$dir/$default"
 
181
    elif [ -r "$dir/`echo $default | sed -e 's|-|/|'`" ]; then
 
182
      file="$dir/`echo $default | sed -e 's|-|/|'`"
 
183
    fi
 
184
 
 
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
 
196
        # end then shift.
 
197
        local args=$#
 
198
        local replaced=0
 
199
        while [ $args -gt 0 ]; do
 
200
            case $1 in
 
201
                %[c])
 
202
                    replaced=1
 
203
                    arg="${localised_name}"
 
204
                    shift
 
205
                    set -- "$@" "$arg"
 
206
                    ;;
 
207
                %[fFuU])
 
208
                    replaced=1
 
209
                    arg="$target"
 
210
                    shift
 
211
                    set -- "$@" "$arg"
 
212
                    ;;
 
213
                %[i])
 
214
                    replaced=1
 
215
                    shift
 
216
                    set -- "$@" "--icon" "$icon"
 
217
                    ;;
 
218
                *)
 
219
                    arg="$1"
 
220
                    shift
 
221
                    set -- "$@" "$arg"
 
222
                    ;;
 
223
            esac
 
224
            args=$(( $args - 1 ))
 
225
        done
 
226
        [ $replaced -eq 1 ] || set -- "$@" "$target"
 
227
        "$command_exec" "$@"
 
228
 
 
229
        if [ $? -eq 0 ]; then
 
230
            exit_success
 
231
        fi
 
232
    fi
 
233
 
 
234
    for d in $dir/*/; do
 
235
        [ -d "$d" ] && search_desktop_file "$default" "$d" "$target"
 
236
    done
 
237
}
 
238
 
 
239
 
 
240
open_generic_xdg_mime()
 
241
{
 
242
    filetype="$2"
 
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"
 
247
 
 
248
        xdg_system_dirs="$XDG_DATA_DIRS"
 
249
        [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/share/
 
250
 
 
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"
 
254
        done
 
255
    fi
 
256
}
 
257
 
 
258
open_generic_xdg_file_mime()
 
259
{
 
260
    filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"`
 
261
    open_generic_xdg_mime "$1" "$filetype"
 
262
}
 
263
 
 
264
open_generic_xdg_x_scheme_handler()
 
265
{
 
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"
 
270
    fi
 
271
}
 
272
 
 
273
open_generic()
 
274
{
 
275
    # Paths or file:// URLs
 
276
    if (echo "$1" | grep -q '^file://' ||
 
277
        ! echo "$1" | egrep -q '^[[:alpha:]+\.\-]+:'); then
 
278
 
 
279
        local file="$1"
 
280
 
 
281
        # Decode URLs
 
282
        if echo "$file" | grep -q '^file:///'; then
 
283
            file=${file#file://}
 
284
            file="$(printf "$(echo "$file" | sed -e 's@%\([a-f0-9A-F]\{2\}\)@\\x\1@g')")"
 
285
        fi
 
286
        file_check=${file%%#*}
 
287
        file_check=${file_check%%\?*}
 
288
        check_input_file "$file_check"
 
289
 
 
290
        filetype=`xdg-mime query filetype "$file_check" | sed "s/;.*//"`
 
291
        open_generic_xdg_mime "$file" "$filetype"
 
292
 
 
293
        if which run-mailcap 2>/dev/null 1>&2; then
 
294
            run-mailcap --action=view "$file"
 
295
            if [ $? -eq 0 ]; then
 
296
                exit_success
 
297
            fi
 
298
        fi
 
299
 
 
300
        if mimeopen -v 2>/dev/null 1>&2; then
 
301
            mimeopen -L -n "$file"
 
302
            if [ $? -eq 0 ]; then
 
303
                exit_success
 
304
            fi
 
305
        fi
 
306
    fi
 
307
 
 
308
    open_generic_xdg_x_scheme_handler "$1"
 
309
 
 
310
    IFS=":"
 
311
    for browser in $BROWSER; do
 
312
        if [ x"$browser" != x"" ]; then
 
313
 
 
314
            browser_with_arg=`printf "$browser" "$1" 2>/dev/null`
 
315
            if [ $? -ne 0 ]; then
 
316
                browser_with_arg=$browser;
 
317
            fi
 
318
 
 
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;
 
322
            fi
 
323
 
 
324
            if [ $? -eq 0 ]; then
 
325
                exit_success;
 
326
            fi
 
327
        fi
 
328
    done
 
329
 
 
330
    exit_failure_operation_impossible "no method available for opening '$1'"
 
331
}
 
332
 
 
333
open_lxde()
 
334
{
 
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:]+\.\-]+:')
 
338
    then
 
339
        local file="$1"
 
340
 
 
341
        # handle relative paths
 
342
        if ! echo "$file" | egrep -q '^(file://)?/'; then
 
343
            file="$(pwd)/$file"
 
344
        fi
 
345
 
 
346
        pcmanfm "$file"
 
347
 
 
348
    else
 
349
        open_generic "$1"
 
350
    fi
 
351
 
 
352
    if [ $? -eq 0 ]; then
 
353
        exit_success
 
354
    else
 
355
        exit_failure_operation_failed
 
356
    fi
 
357
}
 
358
 
 
359
[ x"$1" != x"" ] || exit_failure_syntax
 
360
 
 
361
url=
 
362
while [ $# -gt 0 ] ; do
 
363
    parm="$1"
 
364
    shift
 
365
 
 
366
    case "$parm" in
 
367
      -*)
 
368
        exit_failure_syntax "unexpected option '$parm'"
 
369
        ;;
 
370
 
 
371
      *)
 
372
        if [ -n "$url" ] ; then
 
373
            exit_failure_syntax "unexpected argument '$parm'"
 
374
        fi
 
375
        url="$parm"
 
376
        ;;
 
377
    esac
 
378
done
 
379
 
 
380
if [ -z "${url}" ] ; then
 
381
    exit_failure_syntax "file or URL argument missing"
 
382
fi
 
383
 
 
384
detectDE
 
385
 
 
386
if [ x"$DE" = x"" ]; then
 
387
    DE=generic
 
388
fi
 
389
 
 
390
DEBUG 2 "Selected DE $DE"
 
391
 
 
392
# sanitize BROWSER (avoid caling ourselves in particular)
 
393
case "${BROWSER}" in
 
394
    *:"xdg-open"|"xdg-open":*)
 
395
        BROWSER=$(echo $BROWSER | sed -e 's|:xdg-open||g' -e 's|xdg-open:||g')
 
396
        ;;
 
397
    "xdg-open")
 
398
        BROWSER=
 
399
        ;;
 
400
esac
 
401
 
 
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
 
407
    fi
 
408
fi
 
409
 
 
410
case "$DE" in
 
411
    kde)
 
412
    open_kde "$url"
 
413
    ;;
 
414
 
 
415
    gnome*|cinnamon)
 
416
    open_gnome "$url"
 
417
    ;;
 
418
 
 
419
    mate)
 
420
    open_mate "$url"
 
421
    ;;
 
422
 
 
423
    xfce)
 
424
    open_xfce "$url"
 
425
    ;;
 
426
 
 
427
    lxde)
 
428
    open_lxde "$url"
 
429
    ;;
 
430
 
 
431
    enlightenment)
 
432
    open_enlightenment "$url"
 
433
    ;;
 
434
 
 
435
    cygwin)
 
436
    open_cygwin "$url"
 
437
    ;;
 
438
 
 
439
    darwin)
 
440
    open_darwin "$url"
 
441
    ;;
 
442
 
 
443
    generic)
 
444
    open_generic "$url"
 
445
    ;;
 
446
 
 
447
    *)
 
448
    exit_failure_operation_impossible "no method available for opening '$url'"
 
449
    ;;
 
450
esac