#!perl -w

#
#	crealink.pl: per generare al volo i link per il sito ftp
#
# Versione compilata con PerlCompile.bat
#
# v.1.0 - 23/5/2008


use strict;
use warnings;
use Win32::GUI();
use Win32::Clipboard;
use Config::IniFiles;


	my $versione = "1.0" ;
  
	# finestra principale
	my $WIN = new Win32::GUI::Window(
		-name => "Window",
		-text => "crealink versione: $versione - andrea guido sommaruga",
		-width => 1040,
		-height => 320,
		-left => 100,
		-top => 100,
	);
 
	my $FontNormal = new Win32::GUI::Font(
		-name => "Courier New", 
		-height => 10,
		-bold => 0,
	);

	my $FontBold = new Win32::GUI::Font(
		-name => "Courier New", 
		-height => 10,
		-bold => 1,
	);

	# nasconde la finestra dos
	my ($DOS) = Win32::GUI::GetPerlWindow();
	Win32::GUI::Hide($DOS);

	# crea i controlli
	#
	# seleziona file	top 10
	#	TLnomefile
	#	TFLDnomefile
	#	BTselfile
	#
	# seleziona utente	top 110
	#	TLnomeutente
	#	TFLDnomeutente
	#	BTselutente
	#
	# nome host		top 210
	# 	TLnomehost
	#	TFLDnomehost
	#
	# networkroot	top 310
	#	TLnetroot
	#	TFLDnetroot
	#
	# ftproot		top 410
	#	TLftproot
	# 	TFLDftproot
	#
	# link			top 510
	#	TLlink
	#	TFLDlink
	#	BTcopialink
	
	my $col1 = 5; 
	my $col2 = 100;
	my $col3 = 900;
	my $height = 25;
	my $TLwidth = 90;
	my $TFDwidth = 780;
	my $BTwidth = 120;
	my $vtab = 35;
	my $riga = 1;
	
	# riga 1: nomefile
	$riga = 1;
	
	$WIN->AddLabel(
		-name   => "TLfile",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text=>"nome file",
	);

	$WIN->AddTextfield(
		-name   => "TFLDfile",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text=>"seleziona un file",
	);
 
	my $BTfile = $WIN->AddButton(
		-name => "BTfile",
		-text => "seleziona file",
		-left   => $col3,
		-top    => $vtab * $riga,
		-width  => $BTwidth,
		-height => $height,
        );

	# riga 2: utente
	$riga = 2;

	$WIN->AddLabel(
		-name   => "TLutente",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text=>"nome utente",
	);

	$WIN->AddTextfield(
		-name   => "TFLDutente",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text=>"seleziona utente:password",
	);
 
	my $BTutente = $WIN->AddButton(
		-name => "BTutente",
		-text => "seleziona utente",
		-left   => $col3,
		-top    => $vtab * $riga,
		-width  => $BTwidth,
		-height => $height,
        );
	  $BTutente->Disable(); 

	# riga 3: nome host
	$riga = 3;

	$WIN->AddLabel(
		-name   => "TLhost",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text => "server ftp",
	);

	$WIN->AddTextfield(
		-name   => "TFLDhost",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text => "127.0.0.0",
	);
 
	# riga 4: network root
	$riga = 4;

	$WIN->AddLabel(
		-name   => "TLnetroot",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text => "network root",
	);

	$WIN->AddTextfield(
		-name   => "TFLDnetroot",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text => "C:\\Programmi\\",
	);
 
	# riga 5: ftp root
	$riga = 5;

	$WIN->AddLabel(
		-name   => "TLftproot",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text => "ftp root",
	);

	$WIN->AddTextfield(
		-name   => "TFLDftproot",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text => "/",
	);

# riga 6: pwfile
	$riga = 6;

	$WIN->AddLabel(
		-name   => "TLpw",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-text => "file pw",
	);

	$WIN->AddTextfield(
		-name   => "TFLDpw",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-text => "",
	);

	# riga 7: link
	$riga = 7;

	$WIN->AddLabel(
		-name   => "TLlink",
		-left   => $col1,
		-top    => $vtab * $riga,
		-width  => $TLwidth,
		-height => $height,
		-foreground => '#FF0000',
		-text => "link ftp",
	);

	$WIN->AddTextfield(
		-name   => "TFLDlink",
		-left   => $col2,
		-top    => $vtab * $riga,
		-width  => $TFDwidth,
		-height => $height,
		-foreground => '#FF0000',
		-background => '#BBBBBB',
		-font => $FontBold,
		-text => "",
	);
 
	my $BTlink = $WIN->AddButton(
	-name => "BTlink",
	-text => "copia link",
		-left   => $col3,
		-top    => $vtab * $riga,
		-width  => $BTwidth,
		-height => $height,
	);
	$BTlink->Disable(); 


	# controlla se è stato passato da linea di comando il file .INI dei parametri
	# se non è stato passato controlla se esiste il file .INI di default aggiornaPl.ini
	# $ARGV[0] -> valore primo elemento passato
	# $#ARGV   -> indice ultimo elemento
	# @ARGV    -> array

		#
		# se passato inifile... legge il file ini ed assegna i valori
		#
		my $inifile = "crealink.ini";
		#print "$#ARGV\n";
		if ($#ARGV == 0)
		{
			# controllo se passato inifile per parametro
			$inifile = $ARGV[0];
			#print  "passato $inifile\n";
		}
		if (-e $inifile)
		{
			# se inifile esiste...
			my $cfg = new Config::IniFiles( -file => $inifile , -nocase => 1 );
			my $inisection = "crealink";
			$WIN->TFLDutente->Text($cfg->val( $inisection, 'utente' ));
			$WIN->TFLDfile->Text($cfg->val( $inisection, 'file' ));
			$WIN->TFLDnetroot->Text($cfg->val( $inisection, 'netroot' ));
			$WIN->TFLDftproot->Text($cfg->val( $inisection, 'ftproot' ));
			$WIN->TFLDpw->Text($cfg->val( $inisection, 'pathpwfile' ));
			if ($cfg->val( $inisection, 'showdos' )==1) 
			{
				# se richiesta finestra DOS di debug
				Win32::GUI::Show($DOS);
			}
		} else {
			#print  "non trovo $inifile\n";
		}

		# loop
		$WIN->Show();
		Win32::GUI::Dialog();

  
#
# termina programma
#
sub WIN_Terminate {
	return -1;
}


#######################
#
# gestori eventi
#
#######################

sub BTfile_Click  { SelezionaFile(); $BTutente->Enable(); $BTlink->Enable();}

sub BTutente_Click  { SelezionaUtente();  }

sub BTlink_Click  { CopiaLink(); }

#######################
#
# altro codice chiamato dai gestori eventi
#
#######################

sub SelezionaFile() {
	my $file = Win32::GUI::GetOpenFileName (
		-title    => "Seleziona il file", 
		-explorer => 1, 
		-multisel => 0, 
		-filter => [	'Tutti i file', '*' ],
		-directory => "c:\\programmi",
	);
	#print "$file\n";
	$WIN->TFLDfile->Text($file);
}

sub CopiaLink {
	# compone il link
	# 1) estrae dal nome del file la network root e la sostituisce con la ftproot
	# 2) sostituisce gli spazi con %20
	my $tmpfile = $WIN->TFLDfile->Text();
	my $tmpnetroot = $WIN->TFLDnetroot->Text();
	my $tmpftproot = $WIN->TFLDftproot->Text();
	my $tmphost = $WIN->TFLDhost->Text();
	my $tmputente = $WIN->TFLDutente->Text();
	my $tmpstringa;
	#print "tmpfile:$tmpfile\ntmpnetroot:$tmpnetroot\ntmpftproot:$tmpftproot\ntmphost:$tmphost\ntmputente:$tmputente\n";

	$tmpfile =~ s/\\/\//g;
	$tmpnetroot =~ s/\\/\//g;
	
	$tmpfile =~ s/$tmpnetroot/$tmpftproot/i;
	#print "**tmpfile:$tmpfile\n\n";
	$tmpfile =~ s/ /%20/g;
	#print "**tmpfile:$tmpfile\n\n";

	$tmpstringa = "ftp://$tmputente@". "$tmphost" . "$tmpfile";
	$WIN->TFLDlink->Text("$tmpstringa");
	
	# 
	# copia il link in clipboard
        my $CLIP = Win32::Clipboard("$tmpstringa");
}


sub SelezionaUtente() {

	# finestra per selezione utente
	my $SUBWIN = new Win32::GUI::Window(
		-name => "WINutenti",
		-text => "seleziona utente",
		-width => 200,
		-height => 200,
		-left => 200,
		-top => 200,
		-parent => $WIN,
	);
	
	my $LB = $SUBWIN->AddListbox(
		-name   => "LButenti",
		-left   => 5,
		-top    => 5,
		-width  => 185,
		-height => 185,
		-text=>"listbox",
	);

	my $filepw = $WIN->TFLDpw->Text();
	if (-e $filepw)
	{
		# se esiste il file
		my $riga;
		open TESTO,  "$filepw"     or die "Non posso aprire il file delle password: $filepw\n";
		while ($riga = <TESTO>) {
			# fai qualche cosa con riga $riga;
			$LB->InsertItem($riga);
		}
		close TESTO;
		$SUBWIN->DoModal();
	}
}

sub LButenti_Click {
	my $ListPos = $WIN->WINutenti->LButenti->SelectedItem();
	my $ListString = $WIN->WINutenti->LButenti->GetString($ListPos);
	#print ",$ListPos $ListString\n";
	$WIN->TFLDutente->Text($ListString);
	return -1;
}

sub SUBWIN_Terminate {
	return -1;
}

# crealink.pl
