<?xml version="1.0" encoding="windows-1252"?>
<node id="15641" title="finddata.pl" created="2000-05-31 16:45:54" updated="2005-08-15 12:25:49">
<type id="1748">
sourcecode</type>
<author id="5559">
perlcgi</author>
<data>
<field name="doctext">
&lt;code&gt;use Win32::GUI;
use Win32::Internet;
require Win32::Sound;
require Mail::Sender;

($DOShwnd, $DOShinstance) = GUI::GetPerlWindow();
#GUI::CloseWindow($DOShwnd);
#GUI::Hide($DOShwnd);

my $screen_width = Win32::GUI::GetSystemMetrics(0);
my $screen_height = Win32::GUI::GetSystemMetrics(1);
my $minwidth = 600;
my $minheight = 240;


if (@ARGV[0] eq "-t") {
   $test = 1;
}

my $dbv_icon = new Win32::GUI::Icon("gator.ico");

my $dbv_class = new Win32::GUI::Class(
	   -name =&gt; "DatabaseViewer Class",
	   -icon =&gt; $dbv_icon,
);

my $DataMenu = new Win32::GUI::Menu(
    "&amp;File" =&gt; "File",
    "   &gt;   Retrieve &amp;Data" =&gt; "GetData",
    "   &gt;   -" =&gt; 0,
    "   &gt;   E&amp;xit" =&gt; "FileExit",
    "&amp;Search" =&gt; "Search",
    "   &gt;   Find &amp;Name" =&gt; "FindName",
    "   &gt;   Find &amp;Building" =&gt; "FindBuilding",
    "   &gt;   Find &amp;Adapter" =&gt; "FindAdapter",
    "&amp;Tools" =&gt; "Tools",
    "   &gt;   Report &amp;Problem" =&gt; "ReportProblem",
);

$DataWindow = new Win32::GUI::Window(
	-name   =&gt; "DataWindow",
	-top    =&gt; ($screen_width - $minwidth)/2,
	-left   =&gt; ($screen_height - $minheight)/2,
	-width  =&gt; $minwidth,
	-height =&gt; $minheight,
	-title  =&gt; "GatorNet Database Information",
	-menu   =&gt; $DataMenu,
	-class  =&gt; $dbv_class,
);

$Status = $DataWindow-&gt;AddStatusBar(
	-name   =&gt; "Status",
	-text   =&gt; "GatorNet Database : Ready",
);

$DataTab = $DataWindow-&gt;AddTabStrip(
	-left   =&gt; 10,   
	-top    =&gt; 10, 
	-width  =&gt; $DataWindow-&gt;ScaleWidth - 105, 
	-height =&gt; 175,
	-name   =&gt; "DataTab",
);

$DataTab-&gt;InsertItem(-text =&gt; "Name");
$DataTab-&gt;InsertItem(-text =&gt; "Building");
$DataTab-&gt;InsertItem(-text =&gt; "Adapter");

$DataWindow-&gt;AddLabel(
	-name   =&gt; "FN_Label",
	-text   =&gt; "First Name: ",
	-top    =&gt; 55,
	-left   =&gt; 25,
);

$FirstName = $DataWindow-&gt;AddTextfield(
	-top    =&gt; 52,
	-left   =&gt; 90,
	-width   =&gt; 115,
	-height  =&gt; 23,
	-tabstop =&gt; 1,
);

$DataWindow-&gt;AddLabel(
	-name   =&gt; "LN_Label",
	-text   =&gt; "Last Name: ",
	-top    =&gt; 85,
	-left   =&gt; 25,
);

$LastName = $DataWindow-&gt;AddTextfield(
	-top    =&gt; 82,
	-left   =&gt; 90,
	-width   =&gt; 115,
	-height  =&gt; 23,
	-tabstop =&gt; 1,
);

$DataWindow-&gt;AddLabel(
	-name   =&gt; "Bld_Label",
	-text   =&gt; "Building: ",
	-top    =&gt; 55,
	-left   =&gt; 25,
);

$Building = $DataWindow-&gt;AddCombobox( 
	-name   =&gt; "Dropdown",
	-top    =&gt; 52,
	-left   =&gt; 90,
	-width   =&gt; 125,
	-height  =&gt; 110,
	-tabstop =&gt; 1,
	-style =&gt; WS_VISIBLE | 3 | WS_VSCROLL | WS_TABSTOP,
);

$Building-&gt;InsertItem("Baldwin");
$Building-&gt;InsertItem("Brooks");
$Building-&gt;InsertItem("Caflish");
$Building-&gt;InsertItem("College Court");
$Building-&gt;InsertItem("Crawford");
$Building-&gt;InsertItem("Edwards");
$Building-&gt;InsertItem("PKP");
$Building-&gt;InsertItem("Ravine");
$Building-&gt;InsertItem("Schultz");
$Building-&gt;InsertItem("South Highland");
$Building-&gt;InsertItem("Walker");
$Building-&gt;InsertItem("Walker Annex");

$DataWindow-&gt;AddLabel(
	-name   =&gt; "Adapt_Label",
	-text   =&gt; "Adapter: ",
	-top    =&gt; 55,
	-left   =&gt; 25,
);

$Adapter = $DataWindow-&gt;AddTextfield(
	-top    =&gt; 52,
	-left   =&gt; 90,
	-width   =&gt; 115,
	-height  =&gt; 23,
	-tabstop =&gt; 1,
);

$FindNow = $DataWindow-&gt;AddButton(
	-name   =&gt; "FindNow",
	-text   =&gt; "F&amp;ind Now",
	-top    =&gt; 30,
	-left   =&gt; $DataWindow-&gt;ScaleWidth - 83,
	-width  =&gt; 71,
	-height =&gt; 25,
);

$Stop = $DataWindow-&gt;AddButton(
	-name   =&gt; "Stop",
	-text   =&gt; "Sto&amp;p",
	-top    =&gt; 60,
	-left   =&gt; $DataWindow-&gt;ScaleWidth - 83,
	-width  =&gt; 71,
	-height =&gt; 25,
);

$NewSearch = $DataWindow-&gt;AddButton(
	-name   =&gt; "NewSearch",
	-text   =&gt; "Ne&amp;w Search",
	-top    =&gt; 90,
	-left   =&gt; $DataWindow-&gt;ScaleWidth - 83,
	-width  =&gt; 71,
	-height =&gt; 25,
);

$DataView = new Win32::GUI::ListView($DataWindow,
	-name   =&gt; "DataView",
	-top    =&gt; $DataWindow-&gt;ScaleHeight,
	-left   =&gt; 0,
	-width  =&gt; $DataWindow-&gt;ScaleWidth,
	-height =&gt; $DataWindow-&gt;ScaleHeight - 213,
);

$DataView-&gt;InsertColumn(
	-name   =&gt; "Col1",
	-inndex =&gt; 0,
	-width  =&gt; 95,
	-text   =&gt; "First Name",
);

$DataView-&gt;InsertColumn(
	-index  =&gt; 1,
	-subitem=&gt; 1,
	-width  =&gt; 95,
	-text   =&gt; "Last Name",
);

$DataView-&gt;InsertColumn(
	-index  =&gt; 2,
	-subitem=&gt; 1,
	-width  =&gt; 100,
	-text   =&gt; "Building",
);

$DataView-&gt;InsertColumn(
	-index  =&gt; 3,
	-subitem=&gt; 1,
	-width  =&gt; 55,
	-text   =&gt; "Room",
);

$DataView-&gt;InsertColumn(
	-index  =&gt; 4,
	-subitem=&gt; 1,
	-width  =&gt; 114,
	-text   =&gt; "Adapter Address",
);

$DataView-&gt;InsertColumn(
	-index  =&gt; 5,
	-subitem=&gt; 1,
	-width  =&gt; 117,
	-text   =&gt; "IP Address",
);

$Report = new Win32::GUI::Window(
	-name   =&gt; "Report",
	-top    =&gt; 100,
	-left   =&gt; 100,
	-width  =&gt; $minwidth - 50,
	-height =&gt; $minheight + 100,
	-title  =&gt; "Report Problem",
	-class  =&gt; $dbv_class,
);

$Report-&gt;AddLabel(
	-name   =&gt; "YN_Label",
	-text   =&gt; "Your Name: ",
	-top    =&gt; 5,
	-left   =&gt; 5,
);

$YourName = $Report-&gt;AddTextfield(
	-name   =&gt; "YourName",
	-top    =&gt; 2,
	-left   =&gt; 70,
	-width  =&gt; 300,
	-height =&gt; 23,
	-tabstop=&gt; 1,
);

$ReportMessage = $Report-&gt;AddRichEdit(
	-name    =&gt; "Text",
	-text    =&gt; $text,
	-left    =&gt; 2, 
	-top     =&gt; 27,
	-width   =&gt; $Report-&gt;ScaleWidth - 5, 
	-height  =&gt; $Report-&gt;ScaleHeight - 65,
	-style   =&gt; WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
	-exstyle =&gt; WS_EX_CLIENTEDGE,
);

$Send = $Report-&gt;AddButton(
	-name   =&gt; "Send",
	-text   =&gt; "&amp;Send",
	-top    =&gt; $Report-&gt;ScaleHeight - 30,
	-left   =&gt; ($Report-&gt;ScaleWidth - 71)/2,
	-width  =&gt; 71,
	-height =&gt; 25,
);


$DataView-&gt;Hide();
$Status-&gt;Hide();
$Building-&gt;Select(-1);
#$FindNow-&gt;Disable();
$Stop-&gt;Disable();
$NewSearch-&gt;Disable();
$DataTab-&gt;Select(0);
&amp;DataTab_Click;
$resized = 0;

$DataWindow-&gt;Show();
Win32::GUI::Dialog();

sub GetData_Click {
   $FindNow-&gt;Enable();
   $NewSearch-&gt;Enable();
   if ($test != 1) {
      $connect = new Win32::Internet();
      $connect-&gt;FTP($FTP, "server", "userid", "password");
      $FTP-&gt;Cd("directory");
      $FTP-&gt;Ascii();
      $FTP-&gt;Get("filename");
      ($FTPErrNumb, $FTPErrText) = $FTP-&gt;Error();
      $FTP-&gt;Close();
   }
   $dataretrieved = 1;
}

sub FileExit_Click {
   exit(0);
}

sub FindName_Click {
   $DataTab-&gt;Select(0);
   &amp;DataTab_Click;
}

sub FindBuilding_Click {
   $DataTab-&gt;Select(1);
   &amp;DataTab_Click;
}

sub FindAdapter_Click {
   $DataTab-&gt;Select(2);
   &amp;DataTab_Click;
}

sub ReportProblem_Click {
   $Send-&gt;Disable();
   $Report-&gt;Show();
   $Report-&gt;BringToFront();
   $ReportMessage-&gt;Text("");
   $YourName-&gt;Text("");
}

sub DataWindow_Resize {
   $DataView-&gt;Resize($DataWindow-&gt;ScaleWidth, $DataWindow-&gt;ScaleHeight - 213);
   $Status-&gt;Resize($DataWindow-&gt;ScaleWidth, $Status-&gt;Height);
   $Status-&gt;Move(0, $DataWindow-&gt;ScaleHeight-$Status-&gt;Height);
   $DataTab-&gt;Resize($DataWindow-&gt;ScaleWidth - 105, 175);
   $FindNow-&gt;Move($DataWindow-&gt;ScaleWidth - 83, 30);
   $Stop-&gt;Move($DataWindow-&gt;ScaleWidth - 83, 60);
   $NewSearch-&gt;Move($DataWindow-&gt;ScaleWidth - 83, 90);
}

sub Report_Resize {
   $ReportMessage-&gt;Resize($Report-&gt;ScaleWidth - 5, $Report-&gt;ScaleHeight - 65);
   $Send-&gt;Move(($Report-&gt;ScaleWidth - 71)/2, $Report-&gt;ScaleHeight - 30);
}

sub DataTab_Click {
   if ($DataTab-&gt;
   SelectedItem == 0) {
      $DataWindow-&gt;Bld_Label-&gt;Hide();
      $Building-&gt;Hide();
      $DataWindow-&gt;Adapt_Label-&gt;Hide();
      $Adapter-&gt;Hide();
      $DataWindow-&gt;FN_Label-&gt;Show();
      $FirstName-&gt;Show();
      $DataWindow-&gt;LN_Label-&gt;Show();
      $LastName-&gt;Show();
      $FirstName-&gt;SetFocus();
   }
   if ($DataTab-&gt;SelectedItem == 1) {
      $DataWindow-&gt;FN_Label-&gt;Hide();
      $FirstName-&gt;Hide();
      $DataWindow-&gt;LN_Label-&gt;Hide();
      $LastName-&gt;Hide();
      $DataWindow-&gt;Adapt_Label-&gt;Hide();
      $Adapter-&gt;Hide();
      $DataWindow-&gt;Bld_Label-&gt;Show();
      $Building-&gt;Show();
      $Building-&gt;SetFocus();
   }
   if ($DataTab-&gt;SelectedItem == 2) {
      $DataWindow-&gt;FN_Label-&gt;Hide();
      $FirstName-&gt;Hide();
      $DataWindow-&gt;LN_Label-&gt;Hide();
      $LastName-&gt;Hide();
      $DataWindow-&gt;Bld_Label-&gt;Hide();
      $Building-&gt;Hide();
      $DataWindow-&gt;Adapt_Label-&gt;Show();
      $Adapter-&gt;Show();
      $Adapter-&gt;SetFocus();
   }
}

sub DataWindow_Terminate {
   exit(0);
}

sub FindNow_Click {
   my ($index,$fname,$lname,$build,$room,$adap,$ip);
   $lastcolumn = "";
   if ($dataretrieved == 1) {
      if ($resized == 0) {
	 $DataWindow-&gt;Resize($minwidth, $minheight + 118);
      }
      $DataView-&gt;View(1);
      $DataView-&gt;Clear();
      $DataView-&gt;Show();
      $Status-&gt;Show();
      $Stop-&gt;Enable();
      $index = 0;
      %data = "";

      open (DATABASE, "database.txt");
      
      while (&lt;DATABASE&gt;) {
	 chomp;
	 ($id,$fname,$lname,$build,$room,$adap,$ip) = split(/,/);
	 
	 if ($breakloop == 1) {
	    last;
	 }

	 if ((($DataTab-&gt;SelectedItem == 0) &amp;&amp; (lc(substr($fname,0,length($FirstName-&gt;Text))) eq lc($FirstName-&gt;Text)) &amp;&amp; (lc(substr($lname,0,length($LastName-&gt;Text))) eq lc($LastName-&gt;Text))) 
	    || (($DataTab-&gt;SelectedItem == 1) &amp;&amp; (lc($build) eq lc($Building-&gt;Text)))
	    || (($DataTab-&gt;SelectedItem == 2) &amp;&amp; (lc(substr($adap,0,length($Adapter-&gt;Text))) eq lc($Adapter-&gt;Text)))) {
	       
	       my @temp = split/,/,$_;
	       s/^\d{1,3},//;
	       $data{$temp[0]} = $_;

	       $DataView-&gt;InsertItem(-item =&gt; $index, -text =&gt; "$fname");
	       $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 1, -text =&gt; "$lname");
	       $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 2, -text =&gt; "$build");
	       $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 3, -text =&gt; "$room");
	       $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 4, -text =&gt; "$adap");
	       $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 5, -text =&gt; "$ip");
	       ++$index;
	 }
	 $DataView-&gt;Update();
	 $Status-&gt;Text("$index record(s) found");
      }
   close DATABASE;
   $Stop-&gt;Disable();
   $resized = 1;
   }
   else {
      Win32::Sound::Play("SystemDefault", SND_ASYNC);
      Win32::GUI::MessageBox(0,"You must retrieve the data from the network\r\nfirst.  Goto File, Retrieve Data.","Error: No Data Loaded",64);
   }
}

sub Stop_Click {
   print "Stop button pressed\n";
   $Stop-&gt;Disable();
   $breakloop = 1;
}

sub NewSearch_Click {
   $FirstName-&gt;Text("");
   $LastName-&gt;Text("");
   $Building-&gt;Text("");
   $Building-&gt;Select(-1);
   $Adapter-&gt;Text("");
   $DataWindow-&gt;Resize($minwidth, $minheight);
   $DataView-&gt;Hide();
   $DataView-&gt;Clear();
   $Status-&gt;Hide();
   $resized = 0;
}

sub Dropdown_Change {
    $Building-&gt;Text($Building-&gt;GetString($Building-&gt;SelectedItem));
}

sub DataView_ColumnClick {
   my $column = shift;
   if ($lastcolumn == $column) {
      $sortorder = 1 - $sortorder;
   }
   else {
      $sortorder = 0;
   }
   my %sortcol = &amp;NewList($column, %data);
   &amp;SortListItem(\%data,\%sortcol);
   $lastcolumn = $column;
}

sub SortListItem {
  my ($data,$sortcol) = @_;
  my $check;
  my %data = %$data;
  my %sortcol = %$sortcol;

  $check = "$_" foreach (values %sortcol);

  $DataView-&gt;Clear();
  $index = 0;
  if ($sortorder == 0) {
     foreach (sort { uc($sortcol{$a}) cmp uc($sortcol{$b}) } keys %sortcol)  {
	my @newdata = split/,/,$data{$_};
	($fname,$lname,$build,$room,$adap,$ip) = @newdata;
	if ($fname ne "") {
	   $DataView-&gt;InsertItem(-item =&gt; $index, -text =&gt; "$fname");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 1, -text =&gt; "$lname");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 2, -text =&gt; "$build");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 3, -text =&gt; "$room");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 4, -text =&gt; "$adap");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 5, -text =&gt; "$ip");
	   $DataView-&gt;Update();
	   ++$index;
	}
     }
  }
  else {
     foreach (sort { uc($sortcol{$b}) cmp uc($sortcol{$a}) } keys %sortcol)  {
	my @newdata = split/,/,$data{$_};
	($fname,$lname,$build,$room,$adap,$ip) = @newdata;
	if ($fname ne "") {
	   $DataView-&gt;InsertItem(-item =&gt; $index, -text =&gt; "$fname");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 1, -text =&gt; "$lname");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 2, -text =&gt; "$build");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 3, -text =&gt; "$room");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 4, -text =&gt; "$adap");
	   $DataView-&gt;SetItem(-item =&gt; $index, -subitem =&gt; 5, -text =&gt; "$ip");
	   $DataView-&gt;Update();
	   ++$index;
	}
     }
  }
}


sub NewList  {
   ## This creates another hash to use only for sorting purposes.
   my ($column,%sortcol) = @_;
   my $sortthis;
																									  
   foreach (keys %sortcol) {
      my @info = split /,/, $sortcol{$_};
      $sortthis = $info[$column];
      $sortcol{$_} = "$sortthis";
   }
   return(%sortcol);
}


sub Report_Terminate {
   $ReportMessage-&gt;Text("");
   $YourName-&gt;Text("");
   $Report-&gt;Hide();
   return;
}

sub YourName_Change {
   if ($YourName-&gt;Text() ne "") {
      $Send-&gt;Enable();
   }
   else {
      $Send-&gt;Disable();
   }
}

sub Send_Click {
   if ($ReportMessage-&gt;Text() eq "") {
      Win32::Sound::Play("SystemDefault", SND_ASYNC);
      Win32::GUI::MessageBox(0,"Message can not be left blank. Please\r\nenter a message and try again.",64);
   }
   else {
      $name = $YourName-&gt;Text;
      $message = $ReportMessage-&gt;Text;
      if ($test != 1) {
	 ref ($sender = new Mail::Sender ({ from =&gt; $name, smtp =&gt; "mailserver"}));
	 $sender-&gt;OpenMultipart({to =&gt; "recipient", subject =&gt; "Problem in Database"});
	 $sender-&gt;Body;
	 $sender-&gt;Send($message);
	 $sender-&gt;Send(&lt;&lt;'*END*');

*END*
	 $sender-&gt;SendLine("\n$name");
	 $sender-&gt;Close;
      }
      $Report-&gt;Hide();
   }
}
&lt;/code&gt;
</field>
<field name="codedescription">
This cool utility is included as a mini-demonstation of&lt;br&gt;what is possible with Perl on Win32 using &lt;a href="http://dada.perl.it/#gui"&gt;Win32::GUI.&lt;/a&gt;&lt;br&gt;
This code, the associated database and bitmap may be downloaded from &lt;a href="http://www.httptech.com/archives/0004/zip00000.zip"&gt; here&lt;/a&gt; &lt;br&gt;
If anyone can figure out why the "Stop" button does not&lt;br&gt; work properly or how to read the individual items of each &lt;br&gt;column based on the index item clicked on then Jonathan would appreciate the input.&lt;br&gt;
 
Also Jonathan will be happy to answer anybody's questions &lt;br&gt;about the code if he can.&lt;br&gt;  &lt;i&gt;"It was done on a trial and error basis ... definitely not written off the cuff and then worked properly.&lt;br&gt;  It required some rewrites of course. ;]  Anyway, have fun with the code and any experimenting you want to do on it."&lt;/i&gt;
 </field>
<field name="codecategory">
Win32::GUI</field>
<field name="codeauthor">
Jonathan Southwick &lt;a href="index.pl?node_id=15497"&gt;Hydro&lt;/a&gt;
jsouthwi@alleg.edu
Technical and Network Services
Allegheny Col</field>
</data>
</node>
