Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Win32::GUI calling external FTP executable.

Status
Not open for further replies.

jayjaybigs

IS-IT--Management
Jan 12, 2005
191
CA
Hello All,

I have been working with a very generous person to develop a Win32::GUI that asks for records and FTP the record to remote host.

Big thank you to Pengo for helping with this.

However, this code needs to call WS_FTP (which is already installed on my machine) for transmitting data instead of using cpan FTP client.

This is why I am calling for help from anyone who might be so inclined to offer.

As you can see, this code is working except to call WS_FTP.

Thanks for your help

here is the code:





use Win32::GUI;
my ($DOS) = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($DOS);

use Win32;
use File::Listing qw(parse_dir);
use Net::FTP;
use strict;
use warnings;
use File::Copy;

my $ftp;
my($name, $type, $size, $mtime, $mode);
my $debug;
my $file;

###########################################################
# Create the two files that will be needed
# ( i explain later on why they need to be created now}
###########################################################
open FILE, ">>ftp.db";
close FILE;

open FILE, ">record.tmp";
close FILE;


#########################################################
# Set the window in the middle of the screen
#########################################################
my $desk = Win32::GUI::GetDesktopWindow(); #get the screen width and height

my $dw = Win32::GUI::Width($desk);
my $dh = Win32::GUI::Height($desk);
my $x = ($dw - 600) / 2;
my $y = ($dh - 600) / 2;


#########################################################
# Create a menu for the window
#########################################################
my $menu = Win32::GUI::MakeMenu(
"&File" => "File",
" > &New_Location" => "New",
" > -" => 0,
" > E&xit" => "Exit",
);




#########################################################
# Create the MainWindow
#########################################################
my $mw = new Win32::GUI::Window(
-title => "© BC HOUSING Commission 2005 - cOPYRIGHT",
-name => "mw",
-minsize => [600, 600],
-width => '600',
-height => '600',
-pos =>[$x, $y],
-menu => $menu,
-tabstop => 1,
-dialogui => 1
);

my $icon = new Win32::GUI::Icon('small_icon.ico');
$mw->ChangeSmallIcon($icon);

my $logo = new Win32::GUI::Bitmap('logo.bmp');
my $bitmap = $mw->AddLabel(
-bitmap => $logo,
-left => 400,
-top => 0,
-name => "Bitmap",
-visible => 1,
);

$bitmap->SetImage($logo);

##################################################################
# Create the form on the MainWindows for adding and saving records
##################################################################
my $sin = $mw->AddTextfield(
-name => "Sin",
-left => 180,
-top => 30,
-width => 230,
-height => 22,
-prompt => ["SIN:", -80 ],
-tabstop => 1,
-number => 1,
);

my $surname = $mw->AddTextfield(
-name => "Surname",
-left => 180,
-top => 70,
-width => 230,
-height => 22,
-prompt => ["SURNAME:", -80 ],
-tabstop => 1,
-number => 0,
);

my $givenname = $mw->AddTextfield(
-name => "Givenname",
-left => 180,
-top => 110,
-width => 230,
-height => 22,
-prompt => ["GIVEN NAME:", -80 ],
-tabstop => 1,
-number => 0,
);

my $birthdate = $mw->AddTextfield(
-name => "Birthdate",
-left => 260,
-top => 150,
-width => 150,
-height => 22,
-prompt => ["BIRTHDATE (YYYYMMDD):", -160 ],
-tabstop => 1,
-number => 1,
);
$mw->AddButton(
-name => "Addrecord",
-pos => [5,30],
-text => "Add Record",
-width => 80,
-height => 30,
-tabstop => 1,
);

$mw->AddButton(
-name => "Saverecords",
-pos => [450,150],
-text => "Save to File",
-width => 80,
-height => 30,
-tabstop => 1,
);

$mw->AddButton(
-name => "Uploadfile",
-pos => [5,180],
-text => "Upload file",
-width => 80,
-height => 30,
-tabstop => 1,
);

my $connect_photo = new Win32::GUI::Bitmap('conn.bmp');
$mw->AddButton(
-name => "Connection",
-top => 1,
-left => 1,
-width => 25,
-height => 25,
-visible => '1',
-bitmap => $connect_photo
);

my $disconnect_photo = new Win32::GUI::Bitmap('disconn.bmp');
$mw->AddButton(
-name => "Disconnection",
-top => 1,
-left => 30,
-width => 25,
-height => 25,
-visible => '1',
-bitmap => $disconnect_photo
);
##################################################################################################################
# Create Textfield for displaying the path that you are, when you are browsing the ftp server
##################################################################################################################
my $pathtext = $mw->AddTextfield(
-name => "PathText",
-left => 5,
-top => 220,
-width => 445,
-height => 25,
-readonly => 1,
);



#########################################################
# Create a button to take you back one folder
#########################################################
my $goback_photo = new Win32::GUI::Bitmap('goback.bmp');

my $goback = $mw->AddButton(
-name => "GoBack",
-top => 220,
-width => '25',
-height => '25',
-left => '460',
-visible => '1',
-bitmap => $goback_photo,
);



#########################################################
# Create an ImageList for the listview
#########################################################

my $IL = new GUI::ImageList(
16,
16,
32,
3,
10
);
$IL->Add("folder.bmp");
$IL->Add("file.bmp");


#####################################################################
# Create a listview so you can see files (name, size, type, modifies)
#####################################################################
my $lw = $mw->ScaleWidth;
my $lh = $mw->ScaleHeight;

my $listview = $mw->AddListView(
-name => "ListView",
-text => "List",
-top => '260',
-left => '5',
-width => $lw-10,
-height => $lh-265,
-imagelist => $IL,
-fullrowselect => 1,
#-gridlines => 1,
);


#$mw->ListView->View(1);

my $width = $listview->ScaleWidth;

my $namecolumn = $listview->InsertColumn(
-text => "Name",
-index => 0,
-width => $width/4,

);

my $sizecolumn = $listview->InsertColumn(
-text => "Size",
-index => 1,
-align => 'right',
-subitem => 1,
-width => $width/4,
);

my $typecolumn = $listview->InsertColumn(
-text => "Type",
-index => 2,
-align => 'right',
-subitem => 2,
-width => $width/4,
);

my $modifycolumn = $listview->InsertColumn(
-text => "Modified",
-index => 3,
-align => 'right',
-subitem => 3,
-width => $width/4,
);


#############################################################################
# Create a new window that comes up when you press 'New' to the 'File' menu
#############################################################################
my $new = new Win32::GUI::DialogBox(
-name => "NewFtp",
-text => "New FTP Server",
-pos => [ 400, 400 ],
-size => [ 300, 250 ],
-tabstop => 1,
-resizable => 0,
-minimizebox => 0,
-maximizebox => 0,
);

my $server = $new->AddTextfield(
-name => "Server",
-left => 100,
-top => 10,
-width => 180,
-height => 25,
-prompt => ["FTP Server:", -80 ],
-tabstop => 1,
);

my $username = $new->AddTextfield(
-name => "Username",
-left => 100,
-top => 50,
-width => 180,
-height => 25,
-prompt => ["User name:", -80 ],
-tabstop => 1,
);

my $password = $new->AddTextfield(
-name => "Password",
-left => 100,
-top => 90,
-width => 180,
-height => 25,
-prompt => ["Password:", -80 ],
-password => 1,
-tabstop => 1,
);

$new->AddCheckbox(
-name => "Checkbox",
-left => 30,
-top => 134,
-width => 30,
-height => 30,
-tabstop => 1,
);

$new->AddLabel(
-text => "Anonymous LogOn",
-pos => [60,142],
);

$new->AddButton(
-name => "Connect",
-pos => [30,180],
-text => "Connect",
-width => 100,
-height => 30,
-tabstop => 1,
);

$new->AddButton(
-name => "Cancel",
-pos => [150,180],
-text => "Cancel",
-width => 100,
-height => 30,
-tabstop => 1,
);


######################################################
# Create a Dialog box which approves
# the saving of a record if it allready exists
######################################################

my $saverec = new Win32::GUI::DialogBox(
-name => "Saverec",
-text => "Records",
-pos => [ 400, 400 ],
-size => [ 300, 100 ],
-tabstop => 1,
-resizable => 0,
-minimizebox => 0,
-maximizebox => 0,
);

$saverec->AddButton(
-name => "Yesadd",
-pos => [80,45],
-text => "Yes",
-width => 40,
-height => 20,
-tabstop => 1,
);

$saverec->AddButton(
-name => "Noadd",
-pos => [170,45],
-text => "No",
-width => 40,
-height => 20,
-tabstop => 1,
);

$saverec->AddLabel(
-text => "This SIN already exist, Do you want to save it ?",
-pos => [30,15],
);

##########################################################
# Create a Dialog box which approves
# the saving if you try to add new record without saving
##########################################################

my $addrec = new Win32::GUI::DialogBox(
-name => "Addrec",
-text => "Records",
-pos => [ 400, 400 ],
-size => [ 310, 100 ],
-tabstop => 1,
-resizable => 0,
-minimizebox => 0,
-maximizebox => 0,
);

$addrec->AddButton(
-name => "Yes",
-pos => [80,45],
-text => "Yes",
-width => 40,
-height => 20,
-tabstop => 1,
);

$addrec->AddButton(
-name => "No",
-pos => [170,45],
-text => "No",
-width => 40,
-height => 20,
-tabstop => 1,
);

$addrec->AddLabel(
-text => "The records has not been saved, Do you want to save now ?",
-pos => [5,15],
);


#########################################################
# Make the window visible
#########################################################

$mw->Show();

Win32::GUI::Dialog();

Win32::GUI::Show($DOS);

########################################################
# Terminate program
#######################################################
sub mw_Terminate{
exit 0;
}


sub NewFtp_Terminate {
$new->Hide();
return 0;
}



sub Saverec_Terminate {
$saverec->Hide();
return 0;
}


#########################################################
# Resize the List when you resize the window
#########################################################
sub mw_Resize{
$listview->Resize($mw->ScaleWidth-10, $mw->ScaleHeight-265);
}


#########################################################
# Exit program when you press 'Exit' from 'File' menu
#########################################################
sub Exit_Click {
exit 0;
}


#########################################################
# Make the 'New window' visible
#########################################################
sub New_Click {

$mw->Disable();
$new->Show();
}


#########################################################
# Close window when you press cancel, and go back to main
#########################################################
sub Cancel_Click{

$new->Hide();
$mw->Enable();
$mw->BringWindowToTop();
}

sub ListView_DblClick{

my($item) = @_;
$item = $listview->SelectedItems();
my $text = $listview->GetItemText($item);
my $ftype = $listview->GetItemText($item, 2);

if (($ftype eq 'File') or ($text =~ /\./)){
$mw->Disable();
download($text);
}else{
my $path = $ftp->cwd($text);
$pathtext->Text($ftp->pwd());
listfiles($text);
}
}
########################################################################################
# Save the entries you make to 'ftp.db' with [FTP SERVER], [USERNAME], [PASSWORD]
########################################################################################
sub Connect_Click{

my $host = $server->Text();
my $user = $username->Text();
my $pass = $password->Text();
if($new->Checkbox->Checked()) {
$user = "anonymous";
$pass = "anonymous";#\@mysite.com";
}
$new->Hide();
$mw->Enable();
$mw->BringWindowToTop();
saveftp();
ftpclient($host, $user, $pass,);
}


########################################################
# Create the FTP Client and connect the ftp
########################################################
sub ftpclient{

my ($host, $user, $pass) = @_;
if ($host =~ /(.*?)(\/.*)/){
$host = $1;
}
$ftp = Net::FTP->new(
$host,
Passive => 1,
Debug => 1,
);
$ftp->login($user, $pass);
if ($host = $1){
$ftp->cwd($2);
}
my $path = $ftp->pwd();
$pathtext->Text($path);
listfiles($path);
}

#######################################################
# List the directories and files of the ftp
#######################################################
sub listfiles{

my $item = "@_";


$listview->Clear();

my ($image, $filetype);

foreach $file (parse_dir($ftp->ls('-l'))) {

($name, $type, $size, $mtime, $mode) = @$file;

if ($type eq 'd'){
$filetype = 'Folder';
$image = '0';
}elsif($type eq 'f'){
$filetype = 'File';
$image = '1';
}else{
$filetype = 'Symbolic Link';
$image = '1';
}

my $item = $listview->InsertItem(
-item =>$listview->Count(),
-text => $name,
-image => $image
);
if ($size){
$listview->SetItem(
-item => $item,
-text => $size,
-subitem => 1,
);
}
$listview->SetItem(
-item => $item,
-text => $filetype,
-subitem => 2,
);
$listview->SetItem(
-item => $item,
-text => $mode,
-subitem => 3,
);
}
}

sub GoBack_Click{
if ($ftp){
$ftp->cwd('..');
$pathtext->Text($ftp->pwd());
listfiles('..');
}
}

sub download{

my $download = "@_";

my $ret = GUI::GetSaveFileName(
-title => "Save '$download' As...",
-file => $download,
-filter => [
"All files", "*.*",
],
);
if($ret) {
$ftp->binary;
$ftp->get("$download","$ret");
Win32::MsgBox("Download of $download completed!",64+0,'Download');
}
$mw->Enable();
$mw->BringWindowToTop();
}


##################################################
# It does what it says 'creates record file'
##################################################
sub recordfile{

open FILE, "record.tmp"; #opens record.tmp and pushes every line into an array
my @prevrec;
while (<FILE>) {
chomp($_);
push (@prevrec,$_);
}
close FILE;

my @surname_leters = split(/ */,$surname->Text()); #create the fixed spaces
my @givenname_leters = split(/ */,$givenname->Text());
my @birthdate_leters = split(/ */,$birthdate->Text());
my $space1 = " " x (30-scalar @surname_leters);
my $space2 = " " x (30-scalar @givenname_leters);
my $space3 = " " x (28-scalar @birthdate_leters);

my ($a,$b,$c,$day,$month,$year,$g,$h,$i) = localtime(time);
$year += 1900;
$month += 1;
if($month < 10){
$month = "0" . $month ; # add a leading zero to one-digit months
}
if($day < 10){
$day = "0" . $day ; # add a leading zero to one-digit months
}
print $year, $month, $day ;
my $date = $year.$month.$day;


$prevrec[0] = "7100"; #create the first line 'header'
$prevrec[0] .= " " x 24;
$prevrec[0] .= "$date bchgA00001";
$prevrec[0] .= " " x 99;
$prevrec[0] .= "0\n";




open RECORD,">record.tmp"; # open the file again and rights
# all records with the needed spaces
print RECORD $prevrec[0]; # and the first and last line

for (my $i=1;$i<($#prevrec-1);$i++) {
print RECORD $prevrec[$i],"\n";
}
print RECORD "7101", $sin->Text(), " " x 4,
"0020", $surname->Text(), $space1, $givenname->Text(), $space2,
$birthdate-> Text(), $space3, "bchg", " " x 33,"0\n";
close RECORD;

open RECORD,"record.tmp";
my @newrec;
while (<RECORD>) {
chomp($_);
push (@newrec,$_);
}
my $dec = scalar @newrec;
my @numdec = split(//,$dec);
my $zeros = 8 - scalar @numdec;
my $count = "0" x $zeros;
my $lines = 1 + scalar @newrec;
$count .= $lines;
close RECORD;

open RECORD,">>record.tmp";
$prevrec[$#prevrec] = "7102"; #create the last line 'footer'
$prevrec[$#prevrec].= " " x 24;
$prevrec[$#prevrec].= "$date bchgA00001";
$prevrec[$#prevrec].= " " x 6;
$prevrec[$#prevrec].= $count;
$prevrec[$#prevrec].= " " x 85;
$prevrec[$#prevrec].= "0\n";


print RECORD $prevrec[$#prevrec],"\n";;

close RECORD;

clearfields();
}
################################################
# Clears the fields for new entry
################################################
sub clearfields{

$sin->Text('');

$surname->Text('');

$givenname->Text('');

$birthdate->Text('');
}



sub Addrecord_Click {
if (!$sin->Text() or !$surname->Text() or !$givenname->Text() or !$birthdate->Text()){

Win32::MsgBox('You must fill all fields before save!',48,'Alert ! Alert');

}else{
open FILE, ">>record.tmp";
close FILE;
my @prevrec;

open FILE, "record.tmp";

while (<FILE>) {
chomp($_);
push (@prevrec,$_);
}
close FILE;


my $i = 0;
my $sin = $sin->Text();
foreach (@prevrec) {
if ($_ =~ /$sin/){
$i++;
}
}
if ($i ne 0) {
$mw->Disable();
$saverec->Show();
}else{
recordfile();
}
}
}

######################################################
# It is being used by the subroutine Saverecord
######################################################
sub savefile {

$mw->Disable();

my $ret = GUI::GetSaveFileName(
-title => "Save As...",
-file => "\0" . " " x 256,

-filter => [
"Text documents (*.txt)" => "*.txt",
"All files (*.*)" => "*.*",
],
-defaultfilter => 0,
);
if ($ret){
$mw->Enable();
$mw->BringWindowToTop();
copy("record.tmp","$ret");

open SEQ, ">>seq";
close SEQ;
my @num;
open SEQ, "seq";
while (<SEQ>){
chomp($_);
push(@num,$_);
}
close SEQ;
my $lines = scalar @num;
my $filename = "record.old$lines";
rename "record.tmp",$filename;
open SEQ,">>seq";
print SEQ scalar localtime,"\n";
close SEQ;

open TEMP,">>record.tmp";
close TEMP;
}else{
$mw->Enable();
$mw->BringWindowToTop();
}
}

##################################################
# The same as before
#################################################
sub saveftp{

my @prevftp;

open FILE, "ftp.db";

while (<FILE>) {
chomp($_);
push (@prevftp,$_);
}
close FILE;

my $i = 0;

foreach (@prevftp) {
if ($_ eq ($server->Text())){
$i++;
}
}

unless ($i ne 0) {
ftpfile()
}

}

######################################################
# It is being used by the subroutine saveftp
#####################################################
sub ftpfile {

open FILE, ">>ftp.db" or exit 0; #opens a ftp.db file and saves the ftp servers
print FILE $server->Text(),"\n";
print FILE $username->Text(),"\n";
print FILE $password->Text(),"\n";
print FILE "----------------------\n\n";
print FILE "----------------------\n\n";
close FILE;

}

sub Saverecords_Click {

savefile();
}


sub Uploadfile_Click{
$mw->Disable();
my $ret = GUI::GetOpenFileName(
-title => "Open file",
-filter => [
"Text documents (*.txt)" => "*.txt",
"Perl stuff (*.pl, *.pm)" => "*.pl;*.pm",
"All files", "*.*",
],
-defaultfilter => 0,
-hidereadonly => 1,
-filemustexist => 1,
#-defaultextention => 'zzz',
);
if ($ret){
my $remote = $ret;
$remote =~ s/.*\\//;
$mw->Enable();
$mw->BringWindowToTop();
#$ftp->binary;
#$ftp->put("$ret","$remote");
}else{
$mw->Enable();
$mw->BringWindowToTop();
}
}



###############################################################
# The buttons Yes and No for saving record that exist or not
##############################################################
sub Yesadd_Click {

$saverec->Hide();
$mw->Enable();
$mw->BringWindowToTop();
recordfile();

}


sub Noadd_Click {

$saverec->Hide();
$mw->Enable();
$mw->BringWindowToTop();
}

######################################################################################
# The buttons Yes and No for saving record if button AddRecord pressed before save
######################################################################################
sub Yes_Click {

$addrec->Hide();
$mw->Enable();
$mw->BringWindowToTop();
Saverecords_Click();
}


sub No_Click {

$addrec->Hide();
$mw->Enable();
$mw->BringWindowToTop();
}

sub Connection_Click{
my $host = "cpan.org";
my $user = "anonymous";
my $pass = "anon\@anon.com";
ftpclient($host, $user, $pass);
}

sub Disconnection_Click{
$ftp->quit ();
$listview->Clear();
$pathtext->Text('');
}
 
Where is the rest of the code? :)

Anyway i told you that cpan.org is an ftp server, that i put inside as a default option to connect to, and test the program.

If you go to line '941' and change the
Code:
sub Connection_Click{
    my $host = "cpan.org";
    my $user = "anonymous";
    my $pass = "anon\@anon.com";
    ftpclient($host, $user, $pass);
}

to whatever host, like (bla.bla.bla) then if you press the button connect(under menu File), it will connect to the 'bla.bla.bla' server
unless you give any other ftp server(host), username and password, on the window 'connect'(File->New_Location).

And i repeat we made an 'ftp client'
We dont need another to connect with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top