#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1
 Author:         
 Date: 			 03.02.2012

 Script Function: List all available printers for Installation

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <File.au3>

Dim $aTmp[1]
_FileReadToArray(@ScriptDir & "\Printer.txt",$aTmp)
Global $aDrucker[$aTmp[0]+1][4]

For $i = 1 to $aTmp[0]
    $aSplit = StringSplit($aTmp[$i],@TAB)
    for $j = 1 to $aSplit[0]
        $aDrucker[$i][$j-1] = $aSplit[$j]
    Next
Next
$aDrucker[0][0] = $aTmp[0]

$sYourChoice = "Bitte wählen"

Global $sGeb, $sEtage, $sRaeume

$hGui = GUICreate("Druckerverbindung", 280, 280)		
GUICtrlCreateLabel("Netzwerk-Drucker verbinden", 20,15,800)
GUICtrlSetFont (-1, 9.5, 800, 0, "Arial")
GUICtrlCreateLabel("Gebäude:",20,40)
GUICtrlCreateLabel("Etage:",20,80)
GUICtrlCreateLabel("Raum:",20,120)
GUICtrlCreateLabel("Gerät:",20,160)
$idCboGeb = GUICtrlCreateCombo ($sYourChoice,20,55,240)
_GetGeb()
$idCboEtage = GUICtrlCreateCombo ($sYourChoice,20,95,240)
$idCboRaeume = GUICtrlCreateCombo ($sYourChoice,20,135,240)
$idCboPrinter = GUICtrlCreateCombo ($sYourChoice,20,175,240)
GUISetState()
$default = GUICtrlCreateCheckbox ("Als Standarddrucker festlegen", 20,205,200)
$install = GUICtrlCreateButton("Installieren",40,235,200)
GUISetState()	

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idCboGeb
            $sGeb = GUICtrlRead($idCboGeb)
            if $sGeb <> $sYourChoice Then
                _GetEtage($sGeb)
                if $sGeb ="alle" Then
                    GUICtrlSetData($idCboEtage,"alle","alle")
                    GUICtrlSetData($idCboRaeume,"alle","alle")
                    _GetPrinter($sGeb,"alle","alle")
                EndIf
            EndIf
        Case $idCboEtage
            $sEtage = GUICtrlRead($idCboEtage)
            if $sEtage <> $sYourChoice Then _GetRaeume($sGeb,$sEtage)
		Case $idCboRaeume
			$sRaeume = GUICtrlRead($idCboRaeume)
			if $sRaeume <> $sYourChoice Then _GetPrinter($sGeb,$sEtage,$sRaeume)
		Case $install ;Connect to Printer
			$command1 = ('rundll32 printui.dll,PrintUIEntry /in /n "\\demonas11\' & GUICtrlRead($idCboPrinter) & '"')
			$command2 = ('rundll32 printui.dll,PrintUIEntry /y /n "\\demonas11\' & GUICtrlRead($idCboPrinter) & '"')
			If GUICtrlRead($default) = $gui_checked Then
			RunWait(@comspec & ' /c ' & $command1,'', @SW_HIDE)
			ProcessWaitClose ( "rundll32.exe")
			RunWait(@comspec & ' /c ' & $command2,'', @SW_HIDE)
			else
			RunWait(@comspec & ' /c ' & $command1,'', @SW_HIDE)
			Endif
    EndSwitch
WEnd

Func _GetGeb()
    Local $aGeb[1]
    GUICtrlSetData($idCboGeb,"")
    GUICtrlSetData($idCboGeb,$sYourChoice,$sYourChoice)
    GUICtrlSetData($idCboGeb,"alle")
    for $i = 1 To $aDrucker[0][0]
            _ArrayAdd($aGeb,$aDrucker[$i][1])
    Next
    $aGeb = _ArrayUnique($aGeb)
    for $i =1 to $aGeb[0]
        if $aGeb[$i] <> "" Then GUICtrlSetData($idCboGeb,$aGeb[$i])
    Next
EndFunc

Func _GetEtage($sGeb)
    Local $aEtage[1]
    GUICtrlSetData($idCboEtage,"")
    GUICtrlSetData($idCboEtage,$sYourChoice,$sYourChoice)
    GUICtrlSetData($idCboEtage,"alle")
    for $i = 1 To $aDrucker[0][0]
        if $sGeb = $aDrucker[$i][1] Then
            _ArrayAdd($aEtage,$aDrucker[$i][2])
        EndIf
    Next
    $aEtage = _ArrayUnique($aEtage)
    for $i =1 to $aEtage[0]
        if $aEtage[$i] <> "" Then GUICtrlSetData($idCboEtage,$aEtage[$i])
    Next
EndFunc

Func _GetRaeume($sGeb,$sEtage)
    Local $aRaeume[1]
    GUICtrlSetData($idCboRaeume,"")
    GUICtrlSetData($idCboRaeume,$sYourChoice,$sYourChoice)
	GUICtrlSetData($idCboRaeume,"alle")
    for $i = 1 To $aDrucker[0][0]
        if (($sGeb = $aDrucker[$i][1]) or ($sGeb = "alle")) And (($sEtage = $aDrucker[$i][2]) or ($sEtage = "alle")) Then
            _ArrayAdd($aRaeume,$aDrucker[$i][3])
        EndIf
    Next
    $aRaeume = _ArrayUnique($aRaeume)
    for $i =1 to $aRaeume[0]
        if $aRaeume[$i] <> "" Then GUICtrlSetData($idCboRaeume,$aRaeume[$i])
    Next
EndFunc

Func _GetPrinter($sGeb,$sEtage,$sRaeume)
    Local $aPrinter[1]
    GUICtrlSetData($idCboPrinter,"")
    GUICtrlSetData($idCboPrinter,$sYourChoice,$sYourChoice)
    for $i = 1 To $aDrucker[0][0]
        if (($sGeb = $aDrucker[$i][1]) or ($sGeb = "alle")) And (($sEtage = $aDrucker[$i][2]) or ($sEtage = "alle")) AND (($sRaeume = $aDrucker[$i][3]) or ($sRaeume = "alle"))  Then
            _ArrayAdd($aPrinter,$aDrucker[$i][0])
        EndIf
    Next
    $aPrinter = _ArrayUnique($aPrinter)
    for $i =1 to $aPrinter[0]
        if $aPrinter[$i] <> "" Then GUICtrlSetData($idCboPrinter,$aPrinter[$i])
    Next
EndFunc