| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/sh |
| 2 #--------------------------------------------- | 2 #--------------------------------------------- |
| 3 # xdg-open | 3 # xdg-open |
| 4 # | 4 # |
| 5 # Utility script to open a URL in the registered default application. | 5 # Utility script to open a URL in the registered default application. |
| 6 # | 6 # |
| 7 # Refer to the usage() function below for usage. | 7 # Refer to the usage() function below for usage. |
| 8 # | 8 # |
| 9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> | 9 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> | 10 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
| 11 # | 11 # |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 exit_failure_file_permission_read() | 193 exit_failure_file_permission_read() |
| 194 { | 194 { |
| 195 if [ $# -gt 0 ]; then | 195 if [ $# -gt 0 ]; then |
| 196 echo "xdg-open: $@" >&2 | 196 echo "xdg-open: $@" >&2 |
| 197 fi | 197 fi |
| 198 | 198 |
| 199 exit 5 | 199 exit 5 |
| 200 } | 200 } |
| 201 | 201 |
| 202 #------------------------------------------------------------ | 202 #------------------------------------------------------------ |
| 203 # Exit script on insufficient permission to read a specified file | 203 # Exit script on insufficient permission to write a specified file |
| 204 | 204 |
| 205 exit_failure_file_permission_write() | 205 exit_failure_file_permission_write() |
| 206 { | 206 { |
| 207 if [ $# -gt 0 ]; then | 207 if [ $# -gt 0 ]; then |
| 208 echo "xdg-open: $@" >&2 | 208 echo "xdg-open: $@" >&2 |
| 209 fi | 209 fi |
| 210 | 210 |
| 211 exit 6 | 211 exit 6 |
| 212 } | 212 } |
| 213 | 213 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info.
" | 270 echo "Use 'man xdg-open' or 'xdg-open --manual' for additional info.
" |
| 271 exit_success | 271 exit_success |
| 272 ;; | 272 ;; |
| 273 | 273 |
| 274 --manual) | 274 --manual) |
| 275 manualpage | 275 manualpage |
| 276 exit_success | 276 exit_success |
| 277 ;; | 277 ;; |
| 278 | 278 |
| 279 --version) | 279 --version) |
| 280 echo "xdg-open 1.0.1" | 280 echo "xdg-open 1.0.2" |
| 281 exit_success | 281 exit_success |
| 282 ;; | 282 ;; |
| 283 esac | 283 esac |
| 284 done | 284 done |
| 285 } | 285 } |
| 286 | 286 |
| 287 check_common_commands "$@" | 287 check_common_commands "$@" |
| 288 | 288 |
| 289 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL; | 289 [ -z "${XDG_UTILS_DEBUG_LEVEL}" ] && unset XDG_UTILS_DEBUG_LEVEL; |
| 290 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then | 290 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then |
| 291 # Be silent | 291 # Be silent |
| 292 xdg_redirect_output=" > /dev/null 2> /dev/null" | 292 xdg_redirect_output=" > /dev/null 2> /dev/null" |
| 293 else | 293 else |
| 294 # All output to stderr | 294 # All output to stderr |
| 295 xdg_redirect_output=" >&2" | 295 xdg_redirect_output=" >&2" |
| 296 fi | 296 fi |
| 297 | 297 |
| 298 #-------------------------------------- | 298 #-------------------------------------- |
| 299 # Checks for known desktop environments | 299 # Checks for known desktop environments |
| 300 # set variable DE to the desktop environments name, lowercase | 300 # set variable DE to the desktop environments name, lowercase |
| 301 | 301 |
| 302 detectDE() | 302 detectDE() |
| 303 { | 303 { |
| 304 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; | 304 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 305 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; | 305 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
| 306 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then
DE=xfce; | 306 elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/D
Bus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/nul
l 2>&1` ; then DE=gnome; |
| 307 elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/nul
l 2>&1; then DE=xfce; |
| 307 fi | 308 fi |
| 308 } | 309 } |
| 309 | 310 |
| 310 #---------------------------------------------------------------------------- | 311 #---------------------------------------------------------------------------- |
| 311 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4 | 312 # kfmclient exec/openURL can give bogus exit value in KDE <= 3.5.4 |
| 312 # It also always returns 1 in KDE 3.4 and earlier | 313 # It also always returns 1 in KDE 3.4 and earlier |
| 313 # Simply return 0 in such case | 314 # Simply return 0 in such case |
| 314 | 315 |
| 315 kfmclient_fix_exit_code() | 316 kfmclient_fix_exit_code() |
| 316 { | 317 { |
| 317 version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE` | 318 version=`kde${KDE_SESSION_VERSION}-config --version 2>/dev/null | grep KDE` |
| 318 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'` | 319 major=`echo $version | sed 's/KDE: \([0-9]\).*/\1/'` |
| 319 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'` | 320 minor=`echo $version | sed 's/KDE: [0-9]*\.\([0-9]\).*/\1/'` |
| 320 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'` | 321 release=`echo $version | sed 's/KDE: [0-9]*\.[0-9]*\.\([0-9]\).*/\1/'` |
| 321 test "$major" -gt 3 && return $1 | 322 test "$major" -gt 3 && return $1 |
| 322 test "$minor" -gt 5 && return $1 | 323 test "$minor" -gt 5 && return $1 |
| 323 test "$release" -gt 4 && return $1 | 324 test "$release" -gt 4 && return $1 |
| 324 return 0 | 325 return 0 |
| 325 } | 326 } |
| 326 | 327 |
| 328 # This handles backslashes but not quote marks. |
| 329 first_word() |
| 330 { |
| 331 read first rest |
| 332 echo "$first" |
| 333 } |
| 334 |
| 327 open_kde() | 335 open_kde() |
| 328 { | 336 { |
| 329 kfmclient exec "$1" | 337 if kde-open -v 2>/dev/null 1>&2; then |
| 330 kfmclient_fix_exit_code $? | 338 kde-open "$1" |
| 339 else |
| 340 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 341 kfmclient openURL "$1" |
| 342 else |
| 343 kfmclient exec "$1" |
| 344 kfmclient_fix_exit_code $? |
| 345 fi |
| 346 fi |
| 331 | 347 |
| 332 if [ $? -eq 0 ]; then | 348 if [ $? -eq 0 ]; then |
| 333 exit_success | 349 exit_success |
| 334 else | 350 else |
| 335 exit_failure_operation_failed | 351 exit_failure_operation_failed |
| 336 fi | 352 fi |
| 337 } | 353 } |
| 338 | 354 |
| 339 open_gnome() | 355 open_gnome() |
| 340 { | 356 { |
| 341 gnome-open "$1" | 357 if gvfs-open --help 2>/dev/null 1>&2; then |
| 358 gvfs-open "$1" |
| 359 else |
| 360 gnome-open "$1" |
| 361 fi |
| 342 | 362 |
| 343 if [ $? -eq 0 ]; then | 363 if [ $? -eq 0 ]; then |
| 344 exit_success | 364 exit_success |
| 345 else | 365 else |
| 346 exit_failure_operation_failed | 366 exit_failure_operation_failed |
| 347 fi | 367 fi |
| 348 } | 368 } |
| 349 | 369 |
| 350 open_xfce() | 370 open_xfce() |
| 351 { | 371 { |
| 352 exo-open "$1" | 372 exo-open "$1" |
| 353 | 373 |
| 354 if [ $? -eq 0 ]; then | 374 if [ $? -eq 0 ]; then |
| 355 exit_success | 375 exit_success |
| 356 else | 376 else |
| 357 exit_failure_operation_failed | 377 exit_failure_operation_failed |
| 358 fi | 378 fi |
| 359 } | 379 } |
| 360 | 380 |
| 381 open_generic_xdg_mime() |
| 382 { |
| 383 filetype=`xdg-mime query filetype "$1" | sed "s/;.*//"` |
| 384 default=`xdg-mime query default "$filetype"` |
| 385 if [ -n "$default" ] ; then |
| 386 xdg_user_dir="$XDG_DATA_HOME" |
| 387 [ -n "$xdg_user_dir" ] || xdg_user_dir="$HOME/.local/share" |
| 388 |
| 389 xdg_system_dirs="$XDG_DATA_DIRS" |
| 390 [ -n "$xdg_system_dirs" ] || xdg_system_dirs=/usr/local/share/:/usr/shar
e/ |
| 391 |
| 392 for x in `echo "$xdg_user_dir:$xdg_system_dirs" | sed 's/:/ /g'`; do |
| 393 file="$x/applications/$default" |
| 394 if [ -r "$file" ] ; then |
| 395 command="`grep -E "^Exec(\[[^]=]*])?=" "$file" | cut -d= -f 2- |
first_word`" |
| 396 command_exec=`which $command 2>/dev/null` |
| 397 if [ -x "$command_exec" ] ; then |
| 398 $command_exec "$1" |
| 399 if [ $? -eq 0 ]; then |
| 400 exit_success |
| 401 fi |
| 402 fi |
| 403 fi |
| 404 done |
| 405 fi |
| 406 } |
| 407 |
| 361 open_generic() | 408 open_generic() |
| 362 { | 409 { |
| 363 if mimeopen -v 2>/dev/null 1>&2; then | 410 # Paths or file:// URLs |
| 364 mimeopen -n "$1" | 411 if (echo "$1" | grep -q '^file://' || |
| 365 if [ $? -eq 0 ]; then | |
| 366 exit_success | |
| 367 fi | |
| 368 fi | |
| 369 | |
| 370 if which run-mailcap 2>/dev/null 1>&2 && | |
| 371 (echo "$1" | grep -q '^file://' || | |
| 372 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then | 412 ! echo "$1" | egrep -q '^[a-zA-Z+\.\-]+:'); then |
| 373 | 413 |
| 374 local file=$(echo "$1" | sed 's%^file://%%') | 414 local file=$(echo "$1" | sed 's%^file://%%') |
| 375 run-mailcap --action=view "$file" | 415 |
| 376 if [ $? -eq 0 ]; then | 416 # Decode URLs |
| 377 exit_success | 417 # TODO |
| 418 |
| 419 check_input_file "$file" |
| 420 |
| 421 open_generic_xdg_mime "$file" |
| 422 |
| 423 if [ -f /etc/debian_version ] && |
| 424 which run-mailcap 2>/dev/null 1>&2; then |
| 425 run-mailcap --action=view "$file" |
| 426 if [ $? -eq 0 ]; then |
| 427 exit_success |
| 428 fi |
| 429 fi |
| 430 |
| 431 if mimeopen -v 2>/dev/null 1>&2; then |
| 432 mimeopen -L -n "$file" |
| 433 if [ $? -eq 0 ]; then |
| 434 exit_success |
| 435 fi |
| 378 fi | 436 fi |
| 379 fi | 437 fi |
| 380 | 438 |
| 381 IFS=":" | 439 IFS=":" |
| 382 for browser in $BROWSER; do | 440 for browser in $BROWSER; do |
| 383 if [ x"$browser" != x"" ]; then | 441 if [ x"$browser" != x"" ]; then |
| 384 | 442 |
| 385 IFS=' ' | 443 browser_with_arg=`printf "$browser" "$1" 2>/dev/null` |
| 386 browser_with_arg=${browser//'%s'/"$1"} | 444 if [ $? -ne 0 ]; then |
| 387 | 445 browser_with_arg=$browser; |
| 388 if [ x"$browser_with_arg" = x"$browser" ]; then "$browser" "$1"; | |
| 389 else $browser_with_arg; | |
| 390 fi | 446 fi |
| 391 | 447 |
| 392 if [ $? -eq 0 ]; then exit_success; | 448 if [ x"$browser_with_arg" = x"$browser" ]; then |
| 449 "$browser" "$1"; |
| 450 else eval '$browser_with_arg'$xdg_redirect_output; |
| 451 fi |
| 452 |
| 453 if [ $? -eq 0 ]; then |
| 454 exit_success; |
| 393 fi | 455 fi |
| 394 fi | 456 fi |
| 395 done | 457 done |
| 396 | 458 |
| 397 exit_failure_operation_impossible "no method available for opening '$1'" | 459 exit_failure_operation_impossible "no method available for opening '$1'" |
| 398 } | 460 } |
| 399 | 461 |
| 400 [ x"$1" != x"" ] || exit_failure_syntax | 462 [ x"$1" != x"" ] || exit_failure_syntax |
| 401 | 463 |
| 402 url= | 464 url= |
| (...skipping 15 matching lines...) Expand all Loading... |
| 418 esac | 480 esac |
| 419 done | 481 done |
| 420 | 482 |
| 421 if [ -z "${url}" ] ; then | 483 if [ -z "${url}" ] ; then |
| 422 exit_failure_syntax "file or URL argument missing" | 484 exit_failure_syntax "file or URL argument missing" |
| 423 fi | 485 fi |
| 424 | 486 |
| 425 detectDE | 487 detectDE |
| 426 | 488 |
| 427 if [ x"$DE" = x"" ]; then | 489 if [ x"$DE" = x"" ]; then |
| 428 # if BROWSER variable is not set, check some well known browsers instead | 490 DE=generic |
| 429 if [ x"$BROWSER" = x"" ]; then | 491 fi |
| 430 BROWSER=firefox:mozilla:netscape | 492 |
| 493 # if BROWSER variable is not set, check some well known browsers instead |
| 494 if [ x"$BROWSER" = x"" ]; then |
| 495 BROWSER=links2:links:lynx:w3m |
| 496 if [ -n "$DISPLAY" ]; then |
| 497 BROWSER=firefox:mozilla:epiphany:konqueror:chromium-browser:google-chrom
e:$BROWSER |
| 431 fi | 498 fi |
| 432 DE=generic | |
| 433 fi | 499 fi |
| 434 | 500 |
| 435 case "$DE" in | 501 case "$DE" in |
| 436 kde) | 502 kde) |
| 437 open_kde "$url" | 503 open_kde "$url" |
| 438 ;; | 504 ;; |
| 439 | 505 |
| 440 gnome) | 506 gnome) |
| 441 open_gnome "$url" | 507 open_gnome "$url" |
| 442 ;; | 508 ;; |
| 443 | 509 |
| 444 xfce) | 510 xfce) |
| 445 open_xfce "$url" | 511 open_xfce "$url" |
| 446 ;; | 512 ;; |
| 447 | 513 |
| 448 generic) | 514 generic) |
| 449 open_generic "$url" | 515 open_generic "$url" |
| 450 ;; | 516 ;; |
| 451 | 517 |
| 452 *) | 518 *) |
| 453 exit_failure_operation_impossible "no method available for opening '$url'" | 519 exit_failure_operation_impossible "no method available for opening '$url'" |
| 454 ;; | 520 ;; |
| 455 esac | 521 esac |
| OLD | NEW |