tazpanel rev 557

Allow many file choosers on a page; allow file filtering based on MIME type; fix working in Firefox
author Aleksej Bobylev <al.bobylev@gmail.com>
date Tue Dec 08 13:38:20 2015 +0200 (2015-12-08)
parents 694b20d6963c
children a15373a181ff
files index.cgi lib/libtazpanel
line diff
     1.1 --- a/index.cgi	Mon Dec 07 02:36:31 2015 +0200
     1.2 +++ b/index.cgi	Tue Dec 08 13:38:20 2015 +0200
     1.3 @@ -73,12 +73,14 @@
     1.4  		case "$(GET do)" in
     1.5  
     1.6  		*-selection)		# display Yad file/dir picker (AJAX)
     1.7 -			if [ "$(GET do)" == "dir-selection" ]; then
     1.8 +			if [ "$(GET do)" == 'dir-selection' ]; then
     1.9  				title="$(_ 'Choose directory')"
    1.10 -				extra="--directory"
    1.11 +				extra='--directory'
    1.12 +				icon='folder'
    1.13  			else
    1.14  				title="$(_ 'Choose file')"
    1.15 -				extra=""
    1.16 +				extra=''
    1.17 +				icon='text-plain'
    1.18  			fi
    1.19  			while read name arg ; do
    1.20  				case "$(GET do)" in
    1.21 @@ -89,21 +91,45 @@
    1.22  multiple	--multiple
    1.23  preview		--add-preview
    1.24  EOT
    1.25 +
    1.26 +			if [ -n "$(GET type)" ]; then
    1.27 +				# Get description and file pattern: yad can select only allowed file types
    1.28 +				# For example, type='application/x-cd-image'
    1.29 +				# Note, here implemented simplified code: not use mimetype aliases and generic icons
    1.30 +				mimefile="/usr/share/mime/$(GET type).xml"
    1.31 +				if [ -f "$mimefile" ]; then
    1.32 +					if [ -n "$LANG" ]; then
    1.33 +						# Localized description
    1.34 +						desc="$(sed -n "s|^.*xml:lang=\"${LANG%%_*}\">\(.*\)<.*|\1|p" $mimefile)"
    1.35 +					fi
    1.36 +					if [ -z "$LANG" -o -z "$desc" ]; then
    1.37 +						# Default (English) description
    1.38 +						desc="$(sed -n "s|^.*<comment>\(.*\)<.*|\1|p" $mimefile)"
    1.39 +					fi
    1.40 +					# File pattern(s), for example, "*.iso\n*.iso9660"
    1.41 +					pattern=$(sed -n 's|^.*pattern=\"\(.*\)\".*|\1|p' $mimefile)
    1.42 +					extra="$extra --file-filter='$desc|$(echo $pattern)'"
    1.43 +				fi
    1.44 +				icon="$(echo $(GET type) | tr '/' '-')"
    1.45 +			fi
    1.46 +
    1.47  			header
    1.48  			cd ${HOME:-/}
    1.49  			if [ -r $HOME/.Xauthority ]; then
    1.50 -				cat <<EOT
    1.51 -<input type="text" name="$(GET name)" value="$(DISPLAY=':0.0' \
    1.52 -yad --file-selection --on-top --mouse $extra \
    1.53 ---width=500 --height=350 --title="$title")" />
    1.54 -EOT
    1.55 +				XAUTHORITY="$HOME/.Xauthority"
    1.56  			else
    1.57 -				cat <<EOT
    1.58 -<input type="text" name="$(GET name)" value="$(DISPLAY=':0.0' \
    1.59 -XAUTHORITY='/var/run/slim.auth' yad --file-selection --on-top --mouse $extra \
    1.60 ---width=500 --height=350 --title="$title")" />
    1.61 -EOT
    1.62 +				XAUTHORITY='/var/run/slim.auth'
    1.63  			fi
    1.64 +
    1.65 +			# Problem with inline quoting in the yad option --file-filter
    1.66 +			# Save temp script into file and execute it
    1.67 +			tempsh="$(mktemp)"
    1.68 +			echo "DISPLAY=':0.0' XAUTHORITY=\"$XAUTHORITY\" \
    1.69 +yad --file-selection --on-top --mouse $extra --width=500 --height=350 \
    1.70 +--title=\"$title\" --window-icon=\"$icon\"" > "$tempsh"
    1.71 +			echo "<input type=\"text\" name=\"$(GET name)\" value=\"$(sh $tempsh)\"/>"
    1.72 +			rm "$tempsh"
    1.73 +
    1.74  			exit 0 ;;
    1.75  
    1.76  		esac
     2.1 --- a/lib/libtazpanel	Mon Dec 07 02:36:31 2015 +0200
     2.2 +++ b/lib/libtazpanel	Tue Dec 08 13:38:20 2015 +0200
     2.3 @@ -411,17 +411,21 @@
     2.4  # The html 'file' object does not return the full path. Ajax helps on localhost.
     2.5  
     2.6  file_chooser() {
     2.7 +	# 1: <input> name; 2: [predefined value]; 3: [button icon]; 4: [MIME type]
     2.8 +	id="input$RANDOM"
     2.9  	cat <<EOT
    2.10 -<span id="$1"><input type="text" name="$1" ${2:+value="$2" }/></span>
    2.11 -<button data-icon="${3:-conf}" onclick="ajax('index.cgi?do=file-selection&amp;name=$1', '1', '$1'); return false">
    2.12 +<span id="$id"><input type="text" name="$1" ${2:+value="$2" }/></span>\
    2.13 +<button data-icon="${3:-conf}" onclick="ajax('index.cgi?do=file-selection&amp;name=$name&amp;type=$4', '1', '$id'); return false">\
    2.14  $(_n 'Browse')</button>
    2.15  EOT
    2.16  }
    2.17  
    2.18  dir_chooser() {
    2.19 +	# 1: <input> name; 2: [predefined value]; 3: [button icon]
    2.20 +	id="input$RANDOM"
    2.21  	cat <<EOT
    2.22 -<span id="$1"><input type="text" name="$1" ${2:+value="$2" }/></span>
    2.23 -<button data-icon="${3:-folder}" onclick="ajax('index.cgi?do=dir-selection&amp;name=$1', '1', '$1'); return false">
    2.24 +<span id="$id"><input type="text" name="$1" ${2:+value="$2" }/></span>\
    2.25 +<button data-icon="${3:-folder}" onclick="ajax('index.cgi?do=dir-selection&amp;name=$1', '1', '$id'); return false">\
    2.26  $(_n 'Browse')</button>
    2.27  EOT
    2.28  }