#!/usr/bin/wish # # AGENDA_CHK-2.0 # ================ # # Parameters # ---------- set WIDTH 80 set POS_X 100 set POS_Y 100 set EVENT_FILE "~/.events" set LIST_FONT "times 12" # for UNIX set CALL1 "agenda &" set CALL2 "" # for DOS #set CALL1 "c:/windows/command/wish80.exe" #set CALL2 "c:/dat/bin/agenda" # VAR #---- set event(0) {} set no_events 0 set file 0 set fname $EVENT_FILE set corresp {} set flag(c) 0 proc Abort message { #------------------------------------------------------ # Abort #------------------------------------------------------ puts "## Error ##" puts $message puts "Aborting ..." exit } proc Convert { d m y } { expr $d + $m * 31 + $y * 365 } # MODULES # -------- proc Init_Vars {} { #------------------------------------------------------- # Inicialize vars end others #------------------------------------------------------- global Cday Cmonth Cyear set number [clock seconds] set Cday [clock format $number -format "%d"] set Cmonth [clock format $number -format "%m"] set Cyear [clock format $number -format "%Y"] } proc Open_Event_File {} { #------------------------------------------------------- # Open event file #------------------------------------------------------- global file fname if [catch "open $fname r" file] then { Abort "File $fname not found" } } proc WriteEntry {file i} { global event fname for {set k 0} {$k < 7} {incr k} { puts -nonewline $file [lindex $event($i) $k]: } puts -nonewline $file [lindex $event($i) $k] incr k set s [llength $event($i)] while { $k < $s } { puts $file "\\" puts -nonewline $file [lindex $event($i) $k] incr k } puts $file "" } proc Parse {} { #-------------------------------------------------------- # Read file and parse it creating the data structure #-------------------------------------------------------- global no_events event file set level 0 set line_no 1 set no_events 0 set n 0 set Y 0 while {! [eof $file]} { # Read line set l [gets $file] incr line_no if { $l == ""} continue # check if line continues if {[string range $l end end] == "\\" } then { set x 1 set l [string range $l 0 [expr [string length $l]-2 ]] } else { set x 0 } if { $Y == 1 } then { lappend event($n) "$l" if { $x } then { set Y 1 } else { set Y 0 incr n } continue } set event($n) [split $l :] # check for correct number of parameters if { [llength $event($n)] < 8 } { Abort "Incorrect number of parameters at line $line_no" } if { $x } then { set Y 1 } else { set Y 0 incr n } } set no_events $n close $file } proc ParseAction a { global flag set flag(c) [expr [string first "c" $a] != -1] set flag(o) [expr [string first "o" $a] != -1] set flag(x) [expr [string first "x" $a] != -1] } proc BuildAction {} { global flag set r "" if { $flag(c) } then {set r ${r}c} if { $flag(o) } then {set r ${r}o} if { $flag(x) } then {set r ${r}x} return $r } proc Add_Events l { #------------------------------------------------------- # Decide wich are the events to be noticed and # add to list #------------------------------------------------------- global no_events event Cday Cmonth Cyear corresp flag fname set corresp {} set count 0 if [catch "open $fname.tmp w" file] {Abort "File $fname.tmp could not be created"} if [catch "open $fname.log a+" Logfile] {Abort "File $fname.log could not be created"} for {set n 0} { $n < $no_events } {incr n} { # get fields #----------- set day [lindex $event($n) 0] set month [lindex $event($n) 1] set year [lindex $event($n) 2] set before [lindex $event($n) 3] set after [lindex $event($n) 4] set action [lindex $event($n) 5] set cmd [lindex $event($n) 6] # Solve *'s in datas #------------------- if { $day == "*" } then { set day $Cday } if { $month == "*" } then { set month $Cmonth } if { $year == "*" } then { set year $Cyear } # Convert datas to numeric format #-------------------------------- set firstData [Convert [expr $day - $before] $month $year] set lastData [Convert [expr $day + $after ] $month $year] set Data [Convert $Cday $Cmonth $Cyear] # Check if event should be anounced #---------------------------------- set show 1 set del 0 ParseAction $action if { $Data < $firstData } then { set show 0 } else { if { $Data <= $lastData } { if { $flag(x) } {set show 0} if { $flag(o) } {set flag(x) 1} } else { set show 0 if {$flag(x)} {set flag(x) 0} if {!$flag(c)} {set del 1} } } set action [BuildAction] set event($n) [lreplace $event($n) 5 5 $action] if { $del } then {WriteEntry $Logfile $n} else {WriteEntry $file $n} if { ! $show } continue # Form list entry #---------------- if {[llength $event($n)] <= 8} then { set e " " } else { set e "*" } set Numeric [clock scan "$month/$day/$year"] set e "$e [clock format $Numeric -format "%a %d/%m" ] :" set e "$e [lindex $event($n) 7]" $l insert end $e lappend corresp $n if { $cmd != ""} { exec $cmd & } incr count } close $file close $Logfile file copy -force $fname.tmp $fname file delete -force $fname.tmp return [expr ! $count] } proc Go {} { global CALL1 CALL2; exec $CALL1 $CALL2 exit } proc ShowEv {} { global corresp event top set x [.f.list index active] set i [lindex $corresp $x] destroy .l if {[llength "$event($i)"] > 8 } then { set start 8 set end [llength $event($i)] set txt "" while { $start < $end } { set txt "$txt[lindex $event($i) $start]\n" incr start } label .l -text $txt pack .l -side bottom } } proc Build_GUI {} { #------------------------------------------------------- # Build GUI #------------------------------------------------------- global WIDTH POS_X POS_Y LIST_FONT set number [clock seconds] wm title . [clock format $number -format "%A, %d of %B(%m), %Y"] frame .f pack .f -side top listbox .f.list -background blue -foreground white \ -yscrollcommand [list .f.sy set] \ -width $WIDTH -font $LIST_FONT -selectmode browse scrollbar .f.sy -orient vertical -command [list .f.list yview] pack .f.sy -side right -fill y if [Add_Events .f.list] exit pack .f.list bind .f.list ShowEv frame .cmds pack .f .cmds -side top -pady 5 button .cmds.ok -text "OK !" -command exit button .cmds.edit -text "Call Agenda" -command Go pack .cmds.ok .cmds.edit -side left -padx 10 -pady 4 -ipadx 10 wm geometry . +${POS_X}+${POS_Y} } # MAIN #----- Init_Vars Open_Event_File Parse Build_GUI