slitaz-tools rev 1032

tazbox: add network printer
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Aug 15 11:22:17 2020 +0000 (2020-08-15)
parents aa81af0a90f9
children 96dacc7bbf6d
files applications/tazbox-printer.desktop tazbox/tazbox
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/applications/tazbox-printer.desktop	Sat Aug 15 11:22:17 2020 +0000
     1.3 @@ -0,0 +1,9 @@
     1.4 +[Desktop Entry]
     1.5 +Type=Application
     1.6 +Name=Network printer
     1.7 +Name[fr]=Imprimante réseau
     1.8 +Comment=Print PDF or PostScript files
     1.9 +Comment[fr]=Imprime des fichiers PDF ou PostScript
    1.10 +Exec=tazbox print
    1.11 +Icon=printer
    1.12 +Categories=GTK;Office;
     2.1 --- a/tazbox/tazbox	Sun Oct 20 12:35:57 2019 +0200
     2.2 +++ b/tazbox/tazbox	Sat Aug 15 11:22:17 2020 +0000
     2.3 @@ -29,6 +29,7 @@
     2.4  zi='/usr/share/zoneinfo/'
     2.5  ztab="${zi}zone-mini.tab"
     2.6  csv='/tmp/freegeoip.csv'
     2.7 +prncf=~/.config/slitaz/printer
     2.8  
     2.9  
    2.10  #
    2.11 @@ -57,7 +58,8 @@
    2.12  notify		$(_ 'Notify user with a desktop centered box')
    2.13  tazapps		$(_ 'Configure SliTaz default applications')
    2.14  vnc		$(_ 'Launch a framebuffer VNC viewer')
    2.15 -ssh		$(_ 'Open a remote terminal')"
    2.16 +ssh		$(_ 'Open a remote terminal')
    2.17 +print		$(_ 'Print PDF or PS files on a network printer')"
    2.18  	newline
    2.19  }
    2.20  
    2.21 @@ -681,6 +683,8 @@
    2.22  }
    2.23  
    2.24  
    2.25 +# VNC gui
    2.26 +
    2.27  vnc_main() {
    2.28  	icon='video-display'
    2.29  	yad --title="$(_n 'Framebuffer VNC viewer')" --window-icon="$icon" \
    2.30 @@ -696,6 +700,8 @@
    2.31  }
    2.32  
    2.33  
    2.34 +# SSH gui
    2.35 +
    2.36  ssh_main() {
    2.37  	icon='xterm'
    2.38  	yad --title="$(_n 'Remote connection')" --window-icon="$icon" \
    2.39 @@ -712,6 +718,199 @@
    2.40  }
    2.41  
    2.42  
    2.43 +# Network printer gui
    2.44 +
    2.45 +printer0_main() {
    2.46 +	icon='printer'
    2.47 +	yad --title="$(_n 'Printer server')" --window-icon="$icon" \
    2.48 +		--width='250' --height='180' --on-top --center \
    2.49 +		--image="$icon" --image-on-top --icon="$icon" \
    2.50 +		--form \
    2.51 +		--text="<b>$(_n 'Network printer server')</b>" \
    2.52 +		--field="$(_n 'Printer IP'):CB" 0.0.0.0!$(ifconfig | sed '/inet6\? addr/!d;s|[^:]*: *||;s| .*||;s|^127|^&|' | xargs | sed 's| |!|g') \
    2.53 +		"9100" --field="$(_n 'Port')" \
    2.54 +		--field="$(_n 'Device'):CB" $(ls /dev/*lp? | xargs | sed 's| |!|g') \
    2.55 +		--always-print-result
    2.56 +}
    2.57 +
    2.58 +
    2.59 +printer1_main() {
    2.60 +	icon='printer'
    2.61 +	yad --title="$(_n 'Remote printer')" --window-icon="$icon" \
    2.62 +		--width='250' --height='180' --on-top --center \
    2.63 +		--image="$icon" --image-on-top --icon="$icon" \
    2.64 +		--form \
    2.65 +		--text="<b>$(_n 'Remote PDF or PS printer')</b>" "192.168.0.254" \
    2.66 +		--field="$(_n 'Printer IP')" "9100" \
    2.67 +		--field="$(_n 'Port')" \
    2.68 +		$([ "$(ls /dev/*lp? 2> /dev/null)" ] && echo --button "$(_ 'Server'):2") \
    2.69 +		--button "gtk-cancel:1" --button "gtk-ok:0" \
    2.70 +		--always-print-result
    2.71 +}
    2.72 +
    2.73 +
    2.74 +# Parse printer configuration file
    2.75 +
    2.76 +conf_printer()
    2.77 +{
    2.78 +awk 'BEGIN { state = -1 }
    2.79 +{
    2.80 +	if (state < 0) {
    2.81 +		if (/@PJL INFO VARIABLES/) state=0
    2.82 +	}
    2.83 +	else {
    2.84 +		if (state == 0) {
    2.85 +			printf "%s " $0
    2.86 +			sub(/.*\[/,"")
    2.87 +			state=$0 + 0
    2.88 +		}
    2.89 +		else {
    2.90 +			printf "%s " $1
    2.91 +			state--
    2.92 +		}
    2.93 +		if (state == 0) print ""
    2.94 +	}
    2.95 +}' < $prncf
    2.96 +}
    2.97 +
    2.98 +var_printer()
    2.99 +{
   2.100 +while read key name; do
   2.101 +	conf_printer | grep "^ *$key=" | grep -v '\[1 ' | \
   2.102 +	if [ "$1" ]; then
   2.103 +		#[ "$key=$1" = "COPIES=1" ] ||
   2.104 +		echo "@PJL SET $key=$1"
   2.105 +	else
   2.106 +		awk -vn=$name '{
   2.107 +    sub(/.*=/,"")
   2.108 +    if (/ RANGE\]/) {
   2.109 +	sub(/ \[.*\] /,"!")
   2.110 +	sub(/ /,"..")
   2.111 +	print "--field=" n ":NUM " $0 "!1!0"
   2.112 +    }
   2.113 +    else if (/ ENUMERATED\]/) {
   2.114 +	def=$0
   2.115 +	sub(/ .*/,"",def)
   2.116 +	sub(/.*\] /,"")
   2.117 +	gsub(/ /,"!")
   2.118 +	pos=index($0,def)
   2.119 +	print "--field=" n ":CB " substr($0,0,pos-1) "^" substr($0,pos)
   2.120 +    }
   2.121 +}'
   2.122 +	fi
   2.123 +	shift
   2.124 +done <<EOT
   2.125 +COPIES		$(_n 'Number')
   2.126 +RESOLUTION	$(_n 'Resolution')
   2.127 +ORIENTATION	$(_n 'Orientation')
   2.128 +PAPER		$(_n 'Paper')
   2.129 +LPAPERSOURCE	$(_n 'Source')
   2.130 +DUPLEX		$(_n 'Twosided')
   2.131 +BINDING		$(_n 'Binding')
   2.132 +ECONOMODE	$(_n 'Eco-Mode')
   2.133 +DENSITY		$(_n 'Density')
   2.134 +MANUALFEED	$(_n 'Manual-Feed')
   2.135 +EOT
   2.136 +}
   2.137 +
   2.138 +
   2.139 +# File print gui
   2.140 +
   2.141 +printer2_main() {
   2.142 +	icon='printer'
   2.143 +	support=
   2.144 +	filter=
   2.145 +	grep -q PDF $prncf && support="PDF" && filter="*.pdf *.PDF "
   2.146 +	if grep -q POSTSCRIPT $prncf ; then
   2.147 +		[ "$support" ] && support="$support or PostScript" ||
   2.148 +		support="PostScript"
   2.149 +		filter="${filter}*.ps *.eps *.PS *.EPS"
   2.150 +	fi
   2.151 +	yad --title="$(_n 'Print %s files' "$support")" --window-icon="$icon" \
   2.152 +		--width='250' --height='180' --on-top --center \
   2.153 +		--image="$icon" --image-on-top --icon="$icon" \
   2.154 +		--form \
   2.155 +		--text="<b>$(_n 'Print %s files' "$support")</b>" "" \
   2.156 +		--field="$(_n 'Files'):MFL" \
   2.157 +		--file-filter="$(_ 'All printable files')|$filter" \
   2.158 +		--file-filter="$(_ 'All files')|*" \
   2.159 +		$(var_printer) \
   2.160 +		--button "$(_ 'Printer'):2" \
   2.161 +		--button "gtk-cancel:1" --button "gtk-ok:0" \
   2.162 +		--always-print-result
   2.163 +}
   2.164 +
   2.165 +printer_server() {
   2.166 +	while true; do
   2.167 +		nc -l -p $2 $1 < $3 > $3
   2.168 +	done
   2.169 +}
   2.170 +
   2.171 +printer_query() {
   2.172 +	echo -e "\033%-12345X@PJL"
   2.173 +	echo "@PJL INFO CONFIG"
   2.174 +	echo "@PJL INFO VARIABLES"
   2.175 +	echo -e "\033%-12345X"
   2.176 +	sleep 5
   2.177 +}
   2.178 +
   2.179 +printer() {
   2.180 +	local tmpprncf=$prncf
   2.181 +	while true; do
   2.182 +		if [ ! -s "$tmpprncf" ]; then
   2.183 +			while true; do
   2.184 +				ip="$(printer1_main)"
   2.185 +				case "$?" in
   2.186 +				0) break ;;
   2.187 +				1) return ;;
   2.188 +				esac
   2.189 +				ip="$(printer0_main)"
   2.190 +				[ $? = 0 ] && $0 prnsvr ${ip//|/ }
   2.191 +			done
   2.192 +			echo -e "${ip//|/ }\n" > $prncf
   2.193 +			printer_query | nc ${ip//|/ } | sed 's|\r||;s|'$'^\f''|\n|g' >> $prncf
   2.194 +		fi
   2.195 +		if ! grep -q PDF$ $prncf && ! grep -q POSTSCRIPT$ $prncf; then
   2.196 +			yad --title="$(_n 'Print')" --window-icon="$icon" \
   2.197 +				--width='280' --height='80' --on-top --center \
   2.198 +				--image="$icon" --image-on-top --icon="$icon" \
   2.199 +				--text="Your printer doesn't support PDF or POSTSCRIPT" \
   2.200 +				--button="$(_n 'Remove'):0" \
   2.201 +				--button="$(_n 'Quit'):1"
   2.202 +			[ $? -eq 0 ] && rm -f $prncf
   2.203 +			return
   2.204 +		fi
   2.205 +		tmpprncf=
   2.206 +		result="$(printer2_main)"
   2.207 +		case "$?" in
   2.208 +		0) break ;;
   2.209 +		1) return ;;
   2.210 +		esac
   2.211 +	done
   2.212 +	for file in $(echo ${result%%|*} | sed 's|!| |g'); do
   2.213 +		case $(sed 1q "$file") in
   2.214 +		%PDF*) LG="PDF" ;;
   2.215 +		%!PS-ADOBE*) LG="POSTSCRIPT" ;;
   2.216 +		esac
   2.217 +		if grep -q $LG$ $prncf; then
   2.218 +		{
   2.219 +			echo -e "\033%-12345X@PJL"
   2.220 +			var_printer $(echo ${result#*|} | sed 's/|/ /g')
   2.221 +			echo "@PJL ENTER LANGUAGE = $LG"
   2.222 +			cat $file
   2.223 +			echo -e "\033%-12345X"
   2.224 +			sleep 5
   2.225 +		} | nc $(sed 1q $prncf)
   2.226 +		else
   2.227 +			yad --title="$(_n 'Print')" --window-icon="$icon" \
   2.228 +				--width='280' --height='80' --on-top --center \
   2.229 +				--image="$icon" --image-on-top --icon="$icon" \
   2.230 +				--text="Your printer can't print the $LG file $file"
   2.231 +		fi
   2.232 +	done
   2.233 +}
   2.234 +
   2.235 +
   2.236  video() {
   2.237  	icon='video-x-generic'
   2.238  
   2.239 @@ -994,6 +1193,13 @@
   2.240  			--button 'OK' < /usr/share/licenses/gpl.txt
   2.241  		;;
   2.242  
   2.243 +	print)
   2.244 +		printer
   2.245 +		;;
   2.246 +	prnsvr)
   2.247 +		shift
   2.248 +		printer_server $@ &
   2.249 +		;;
   2.250  	ssh)
   2.251  		ssh
   2.252  		;;