#~ Printer Manager (Win)
#~ Ruby+Shoes / 07.11.08 mcwea

NAME = "PrinterManager"
VERSION = "V1.2"
DATE = "12.02.2009"
AUTHORS = "Andreas Weber"
$stdout = $stderr = File.new("c:/temp/printermanager.log", "w")

#~ Shoes.setup do
	#~ gem "win32ole"
#~ end

require 'win32ole'

Shoes.app :width => 400, :height => 300, :margin => 10, :title => "#{NAME} #{VERSION}" do	
	
	#~ Help (German)
	def help_de
		window do
			stack do
				background cornsilk
				caption ins("Ueber")
				para "Programm: ", strong("#{NAME}")
				para "Version: ", strong("#{VERSION}"), " | Datum: #{DATE}"
				para "Autoren: #{AUTHORS}"
			end
			stack do
				caption ins("Hilfe")
				para "... wird fortgesetzt."
			end
		end
	end
	
	#~ Help (English)
	def help_en
		window do
			stack do
				background cornsilk
				caption ins("About")
				para "Program: ", strong("#{NAME}")
				para "Version: ", strong("#{VERSION}"), " | Date: #{DATE}"
				para "Authors: #{AUTHORS}"
			end
			stack do
				caption ins("Help")
				para "... will be continued."
			end
		end
	end
		
	#~ Language: German
	def german
		@caption1.replace strong(ins("Drucker waehlen:"))
		@caption2.replace strong(ins("Aktionen:"))
		@label1.replace link("Drucker-Assistent") { wizard1 }
		@label2.replace link("Druckereinstellungen aendern") { setup(@p) }
		@label3.replace link("Druckereinstellungen sichern") { backup(@p) }
		@label4.replace link("Druckereinstellungen wiederherstellen") { restore(@p) }
		@label5.replace link("Drucker-Server") { wizard2 }
		@label6.replace link("Drucker-Uebersicht") { list_all }
		@list.replace "Drucker: #{p.Caption} | Treiber: #{p.DriverName}\n"
		@help.replace link(strong("HILFE")) { help_de }
		@lang.replace link("English") { english }
	end
	
	#~ Language: English
	def english
		@caption1.replace strong(ins("Select printer:"))
		@caption2.replace strong(ins("Actions:"))
		@label1.replace link("Printer wizard") { wizard1 }
		@label2.replace link("Change printer properties") { setup(@p) }
		@label3.replace link("Backup printer properties") { backup(@p) }
		@label4.replace link("Restore printer properties") { restore(@p) }
		@label5.replace link("Printer server") { wizard2 }
		@label6.replace link("Printer list") { list_all }
		@list.replace "Printer: #{p.Caption} | driver: #{p.DriverName}\n"
		@help.replace link(strong("HELP")) { help_en }
		@lang.replace link("Deutsch") { german }
	end	
	
	#~ Sub's / functions
	def list
		#~ List printers from system
		wmi = WIN32OLE.connect("winmgmts://")
		printer = []
		printers = wmi.ExecQuery("select * from win32_printer")
		printers.each do |p|
			printer << p.Caption
		end
		return printer.sort
	end
	
	def list_all
		#~ List printers from system
		wmi = WIN32OLE.connect("winmgmts://")
		printers = wmi.ExecQuery("select * from win32_printer")
		window :title => "List" do
			printers.each do |p|
				@list = para "Drucker: #{p.Caption} | Treiber: #{p.DriverName}\n"
			end
		end
	end
	
	def setup(p)
		#~ Call printer proprties
		cmd1 = "rundll32 printui.dll,PrintUIEntry /e /n "
		cmd = cmd1 + '"' + p + '"'
		system(cmd)
	end
	
	def backup(p)
		#~ Backup printer properties
		cmd1 = "rundll32 printui.dll,PrintUIEntry /Ss /n "
		logf = "c:\\temp\\#{p}.dat"
		cmd = cmd1 + '"' + p + '" /a "' + logf + '"'
		system(cmd)
		alert "Settings saved => #{logf}"
	end
	
	def restore(p)
		#~ Restore printer properties
		cmd1 = "rundll32 printui.dll,PrintUIEntry /Sr /n "
		logf = "c:\\temp\\#{p}.dat"
		if File.exists?(logf)
			cmd = cmd1 + '"' + p + '" /a "' + logf + '"'
			system(cmd)
			alert "Settings restored <= #{logf}"
		else
			alert "!!! File is missing => #{logf} !!!"
		end
	end
	
	def wizard1
		#~ Call printer wizard
		cmd = "rundll32 printui.dll,PrintUIEntry /il"
		system(cmd)
	end
	
	def wizard2
		#~ Call printer server
		cmd = "rundll32 printui.dll,PrintUIEntry /s /t1"
		system(cmd)
	end
	
	#~ Main program
	flow do
		background cornsilk		
		stack :width => "70%" do
			#~ Maybe change name of environment variables
			para "Computer: ", strong(ENV["COMPUTERNAME"])
			para "Username: ", strong(ENV["USERNAME"])
		end
		
		stack :width => "30%" do
			@lang = para  link("English") { english }
			@help = para  link(strong("HILFE")) { help_de }
		end

		stack :width => "40%", :margin => 3 do
			background bisque
			#~ Select printer
			@caption1 = para strong(ins("Drucker waehlen:"))	
			printers = list
			list_box :items => printers, 
				:width => 150 do |pr|
					@p = pr.text
				end
			para ""
			stack do
				background khaki
				#~ List
				@label6 = para link("Drucker-Uebersicht") { list_all }
				#~ Wizard1
				@label1 = para link("Drucker-Assistent") { wizard1 }
				#~ Wizard2
				@label5 = para link("Drucker-Server") { wizard2 }
			end
		end

		stack :width => "60%", :margin => 3  do
			background khaki
			@caption2 = para strong(ins("Aktionen:"))
			#~ Printer properties	
			@label2 = para link("Druckereinstellungen aendern") { setup(@p) }		
			#~ Backup
			@label3 = para link("Druckereinstellungen sichern") { backup(@p) }		
			#~ Restore
			@label4 = para link("Druckereinstellungen wiederherstellen") { restore(@p) }					
		end
	end	
end