# tivoweb-tcl resources module
# (C)Copyright 2001 by Turbo (Mike Byrne)
# All rights reserved
global Resources
global NumResources
global ResourceGroups
global ResourceGroupsNum
global ResourceGroupsNdx
proc TVUpdateResources { } {
global db
global swsystemfsid
RetryTransaction {
set objSwSystem [db $db openid $swsystemfsid]
set objResourceGroup [dbobj $objSwSystem get ResourceGroup]
set objMpaaRating [lindex $objResourceGroup 0]
catch { dbobj $objMpaaRating remove CompressedFile }
}
}
proc ResGetGroupStringFromNum {irGrp} {
global ResourceGroups
global ResourceGroupsNum
set index [lsearch $ResourceGroupsNum $irGrp]
if { $index != -1 } {
return [lindex $ResourceGroups $index]
}
return ""
}
proc ResGetItemStringFromNum { irGrp irItm } {
global NumResources
global Resources
global ResourceGroupsNum
global ResourceGroupsNdx
set index [lsearch $ResourceGroupsNum $irGrp]
if { $index != -1 } {
set start [lindex $ResourceGroupsNdx $index]
set end [lindex $ResourceGroupsNdx [expr $index+1]]
if { $end == "" } {
set end $NumResources
}
for { set i $start } { $i <= $end } { incr i } {
if {[string match [lindex $Resources($i) 3] $irItm]} {
return [lindex $Resources($i) 1]
}
}
}
return ""
}
proc ResBuildGroupList {} {
global NumResources
global Resources
global ResourceGroups
global ResourceGroupsNum
global ResourceGroupsNdx
set ResourceGroups ""
set ResourceGroupsNum ""
set ResourceGroupsNdx ""
set lastgrp ""
for { set i 0 } { $i < $NumResources } { incr i } {
set grp [lindex $Resources($i) 0]
set num [lindex $Resources($i) 2]
if {![string match $grp $lastgrp]} {
lappend ResourceGroups $grp
lappend ResourceGroupsNum $num
lappend ResourceGroupsNdx $i
set lastgrp $grp
}
}
}
proc ResGetItemListForGroup { rGrp } {
global NumResources
global Resources
global ResourceGroups
global ResourceGroupsNum
global ResourceGroupsNdx
set result ""
set index [lsearch $ResourceGroupsNum $rGrp]
if { $index != -1 } {
set start [lindex $ResourceGroupsNdx $index]
set end [lindex $ResourceGroupsNdx [expr $index+1]]
if { $end == "" } {
set end $NumResources
}
for { set i $start } { $i < $end } { incr i } {
# set itm [lindex $Resources($i) 1]
set num [lindex $Resources($i) 3]
# lappend result [list $itm $num]
lappend result $num
}
}
return $result
}
proc ResGetStringResourceValue { rGrp rItm } {
global db
global swsystemfsid
RetryTransaction {
set objSwSystem [db $db openid $swsystemfsid]
set objResGroup [dbobj $objSwSystem get ResourceGroup $rGrp]
set result [dbobj [dbobj $objResGroup get Item $rItm] get String]
}
return $result
}
proc ResGetStringResourceValueList { rGrp rlist } {
global db
global swsystemfsid
set result ""
set rlen [llength $rlist]
set incrsize 50
if { $rlen > $incrsize } {
for {set icount 0} {$icount < $rlen} {incr icount $incrsize} {
set maxicount [expr ($rlen > $icount+$incrsize) ? ($icount+$incrsize) : $rlen]
RetryTransaction {
set objSwSystem [db $db openid $swsystemfsid]
set objResGroup [dbobj $objSwSystem get ResourceGroup $rGrp]
set objResItem [dbobj $objResGroup get Item]
for {set count $icount} {$count < $maxicount} {incr count} {
set rItm [lindex $rlist $count]
# puts "'$count' '$rItm'"
lappend result [strim [dbobj [lindex $objResItem $rItm] get String]]
}
}
}
} else {
RetryTransaction {
set objSwSystem [db $db openid $swsystemfsid]
set objResGroup [dbobj $objSwSystem get ResourceGroup $rGrp]
set objResItem [dbobj $objResGroup get Item]
foreach rItm $rlist {
lappend result [strim [dbobj [lindex $objResItem $rItm] get String]]
}
}
}
return $result
}
proc ResSetStringResourceValue { rGrp rItm rVal } {
global db
global swsystemfsid
RetryTransaction {
set objSwSystem [db $db openid $swsystemfsid]
set objResGroup [dbobj $objSwSystem get ResourceGroup $rGrp]
set result [dbobj [dbobj $objResGroup get Item $rItm] set String $rVal]
}
return $result
}
proc build_resources {} {
global Resources
global NumResources
global source_dir
global swsystemfsid
global tivoswversion
set NumResources 0
set resfiles [glob "$source_dir/modules/*.res"]
foreach resfile $resfiles {
# puts [file rootname [file tail $resfile]]
set fd [open $resfile "r"]
set first 1
set line [gets $fd]
while { ![eof $fd] } {
if { [string index $line 0] == "#" } {
} elseif { [string length $line] <= 1 } {
} else {
if { $first == 1 } {
set first 0
if { ![PrefixMatches $line $tivoswversion] } {
# puts "skipping $resfile"
break
}
} else {
set Resources($NumResources) $line
incr NumResources
}
}
set line [gets $fd]
}
close $fd
}
ResBuildGroupList
RetryTransaction {
set swsystemfsid [lindex [mfs find /SwSystem/ACTIVE] 0]
}
puts "Loaded $NumResources resource definitions"
}
proc action_resources {chan path env} {
global Resources
global ResourceGroups
global ResourceGroupsNdx
global ResourceGroupsNum
global db
while {[string index $path 0] == "/"} {
set path [string range $path 1 end]
}
set firstslash [string first "/" $path]
if { $firstslash > -1 } {
set resourcegroup [string range $path 0 [expr $firstslash - 1]]
set resourceitem [string range $path [expr $firstslash + 1] end]
} else {
set resourcegroup "$path"
set resourceitem ""
}
puts $chan [html_start "Resource Editor"]
# puts $chan [h1 "Resource Editor"]
if { $resourcegroup != "" } {
if { $resourceitem != "" } {
set SetValue ""
eval $env
set resourcegroupstr [ResGetGroupStringFromNum $resourcegroup]
set resourceitemstr [ResGetItemStringFromNum $resourcegroup $resourceitem]
if { $SetValue != "" } {
set result [ResSetStringResourceValue $resourcegroup $resourceitem $SetValue]
puts $chan "
Setting value of $resourcegroupstr\:\:$resourceitemstr to $SetValue
"
} else {
set resourcevalue [ResGetStringResourceValue $resourcegroup $resourceitem]
puts $chan "
$resourcegroupstr\:\:$resourceitemstr = $resourcevalue
"
}
} else {
if { [string match $resourcegroup "UpdateResources"] } {
TVUpdateResources
puts $chan [h1 "Resources Updated - Reboot!"]
puts $chan [html_end]
return
}
set rGrpStr [ResGetGroupStringFromNum $resourcegroup]
puts $chan [h1 $rGrpStr]
puts $chan [html_table_start "" "" ""]
puts $chan [tr "ALIGN=CENTER" [th "Resource Name"] [th "Resource Value"]]
set rlist [ResGetItemListForGroup $resourcegroup]
set vallist [ResGetStringResourceValueList $resourcegroup $rlist]
set index [lsearch $ResourceGroupsNum $resourcegroup]
set gstart [lindex $ResourceGroupsNdx $index]
set rlen [llength $rlist]
for {set i 0} {$i < $rlen} {incr i} {
set rStr [lindex $Resources([expr $gstart+$i]) 1]
set rNum [lindex $rlist $i]
set itemstr [lindex $vallist $i]
#do not break up this line
puts $chan [tr "" [td "$rStr"] [td [html_form_start "GET" "$resourcegroup/$rNum"][html_form_text 1 30 "SetValue" $itemstr][html_form_end]]]
}
puts $chan [html_table_end]
}
} else {
puts $chan [html_table_start "" "Resource Groups" "colspan=1"]
foreach index $ResourceGroupsNdx {
set rlGrp $Resources($index)
set rGrpNum [lindex $rlGrp 2]
set rGrpStr [lindex $rlGrp 0]
puts $chan [tr "" [td "ALIGN=CENTER" [html_link /resources/$rGrpNum $rGrpStr]]]
}
puts $chan [html_table_end]
puts $chan [html_link "/resources/UpdateResources" "Update Resources"]
}
puts $chan [html_end]
}
if { $reload == 0 } {
build_resources
}
register_module "resources" "Resource Editor" "Editor for TiVo Resources"