#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
#  CONFIGURATION INFORMATION 
#  All config info should be in P4CGI.pm
#
#################################################################
#
#  Set Prefereces
#
#################################################################


my $newp = &P4CGI::cgi()->param("SET_PREFERENCES") ;
if(defined $newp) {
    &P4CGI::cgi()->delete("SET_PREFERENCES") ;
    &P4CGI::ONLOADSCRIPT("location='" . &P4CGI::cgi()->url(-query => 1) . "';") ;
}
my $newdp = &P4CGI::cgi()->param("CHANGE_DEPOT") ;
if(defined $newdp) {
    &P4CGI::cgi()->delete("CHANGE_DEPOT") ;
    &P4CGI::ONLOADSCRIPT("location='index.cgi?DP=". &P4CGI::CURR_DEPOT_NO() ."';") ;
}
&P4CGI::ERRLOG("URL: " .&P4CGI::cgi()->url(-query => 1) ) ;

#my $fullURL = &P4CGI::cgi()->url(-full=>1) ;

#if((defined $newp) and 
#   (&P4CGI::CURRENT_CHANGE_LEVEL() >= 0)) {
#    &P4CGI::EXTRAHEADER(-Refresh => "1; URL=index.cgi") ;
#    
#} ;
#&P4CGI::ERRLOG("fullURL: $fullURL") ;
#&P4CGI::ERRLOG("server_name: ". &P4CGI::cgi()->server_name()) ;

#&P4CGI::SET_HELP_TARGET("SetPreferences") ;

print "",
    &P4CGI::start_page("Set preferences") ;    

my %pref_list=&P4CGI::PREF_LIST() ;
my %pref=&P4CGI::PREF() ;

my %pref_help = ("IC"   => "Set to Yes for Windows users",
		 "MX"   => "Max changes to show per page.",
		 "TB"   => "Tab stop in source code files",
		 "HD"   => "Select default value for depot browser",
		 "VC"   => "File viewer can add color to some file types",
		 "ST"   => "Style preferences",
		 "PH"   => "Select header style",
		 "DBG"  => "Used for debugging") ;

my $prefs="" ;

my $p ;
foreach $p (sort { $ {$pref_list{$a}}[0] cmp $ {$pref_list{$b}}[0] }  keys %pref_list) {
    my @data = @{$pref_list{$p}} ;
    my $type = shift @data ;
    $type =~ s/^\w:// ;
    my $desc = shift @data ;
    my $default = shift @data ;
    my $cval = $pref{$p} ;
    my $cell ;
    my $title = $pref_help{$p} ;
    $title = "" unless defined $title ;
    if($type eq "BOOL") {
	$cell = &P4CGI::cgi()->radio_group(-name=>$p,
					   "values"=>[0,1],
					   -default=> $cval,
					   -title=>$title,
					   -labels=>{1 => "Yes", 0 => "No"}) ;
    }
    my $lstsz = 0 ;
    if($type eq "LIST") {
	my $n = -1 ;
	my %alts = map { $n++ ; s/^.*;\s+// ; ($n,$_) ; } @data ;
	$lstsz = @data ;
	my @d = keys %alts ;
	$cell = &P4CGI::cgi()->popup_menu(-name=>$p,
					  "values"=>\@d,
					  -title=>$title,
					  -default=>$cval,
					  -labels=>\%alts) ;
    } 
    if($type eq "INT") {
	$cell = &P4CGI::cgi()->textfield(-name=>$p,
					 -size=>6,
					 -title=>$title,
					 -default=>$cval,
					 -maxlength=>6) ;
    } ;
    $prefs .= &P4CGI::table_row({-class=>"Prompt",
				 -text=>"$desc:"},
				{-align=>"left",
				 -text=>$cell}) ;	
}

if(&P4CGI::NO_OF_DEPOTS() > 1) {
    print 
	&P4CGI::cgi()->start_form(-method=>"GET",
				  -action=>"SetPreferences.cgi"),
	&P4CGI::start_framedTable("P4 server"),
	&P4CGI::start_table() ;

    my $d ;
    my %alts ;    
    my @depotnames ;
    for($d = 0 ; $d < &P4CGI::NO_OF_DEPOTS() ; $d++) {
	my $n = &P4CGI::DEPOT_NAME($d) ;
	$alts{$d} = $n ;
	push @depotnames, $d ;
    }
    my $cell = &P4CGI::cgi()->popup_menu(-name=>"DP",
					 "values"=>\@depotnames,
					 -title=>"Select p4 server",
					 -default=>&P4CGI::CURR_DEPOT_NO(),
					 -labels=>\%alts) ;
    
    print 
	&P4CGI::table_row({-class=>"Prompt",
			   -text=>"Select p4 server"},
			  {-align=>"left",
			   -text=>$cell}),
	&P4CGI::table_row("",
			  &P4CGI::cgi()->submit(-value=>'Change server',
						-name=>'CHANGE_DEPOT')),
	&P4CGI::end_table(),
	&P4CGI::end_framedTable(),
	&P4CGI::cgi()->end_form(),
	"<br>";
}

print 
    &P4CGI::cgi()->start_form(-method=>"GET",
			      -action=>"SetPreferences.cgi"),
    &P4CGI::start_framedTable("User preferences"),
    &P4CGI::start_table(),
    $prefs,
    &P4CGI::table_row("",
		      &P4CGI::cgi()->submit(-value=>'Change preferences',
					    -name=>'SET_PREFERENCES')),
    &P4CGI::end_table(),
    &P4CGI::end_framedTable(),
    &P4CGI::cgi()->hidden("DP",&P4CGI::CURR_DEPOT_NO()),
    &P4CGI::cgi()->end_form() ;


print 
    "<br>",
    &P4CGI::start_framedTable("Your shortcuts") ;

my %shortcuts = &P4CGI::SHORTCUTS() ;
my $nosc = keys %shortcuts ;
my $s = "s" ;
$s = "" if  $nosc == 1 ;
$nosc = "No" if $nosc == 0 ;
print "$nosc shortcut$s defined<br>\n" ;
if($nosc ne "No") {
    print "<table>\n" ;
    print &P4CGI::table_header("Name","Remove","Target") ;
    my @shortcuts = sort { uc($a) cmp uc($b) } keys %shortcuts ;
    while(@shortcuts) {
	my $name = shift @shortcuts ;
	my $target = $shortcuts{$name} ;
	my $nameNoSpace = $name ;
	$nameNoSpace =~ s/ /&nbsp;/g ;	
	print "<tr><td class=\"Description\">$nameNoSpace</td>" ;
	my $t = &P4CGI::urlEncode($target) ;
	my $del=&P4CGI::buttonCell($ENV{SCRIPT_NAME},
				   "Remove shortcut",
				   "RMSHORTCUT=$t\:\:\:$name",
				   "Remove") ;
	print "$del<td>$target</td></tr>\n" ;
    }
    print "</table>\n"
}

print
    &P4CGI::end_framedTable(),
    &P4CGI::end_page() ;

#
# That's all folks
#