[Tkabber-dev] r723 - in trunk/tkabber-plugins: . traffic

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Tue Sep 19 10:27:50 MSD 2006


Author: sergei
Date: 2006-09-19 10:27:47 +0400 (Tue, 19 Sep 2006)
New Revision: 723

Modified:
   trunk/tkabber-plugins/ChangeLog
   trunk/tkabber-plugins/traffic/traffic.tcl
Log:
	* traffic/traffic.tcl: Code cleanup.


Modified: trunk/tkabber-plugins/ChangeLog
===================================================================
--- trunk/tkabber-plugins/ChangeLog	2006-09-18 21:01:30 UTC (rev 722)
+++ trunk/tkabber-plugins/ChangeLog	2006-09-19 06:27:47 UTC (rev 723)
@@ -1,5 +1,9 @@
 2006-09-16  Sergei Golovan  <sgolovan at nes.ru>
 
+	* traffic/traffic.tcl: Code cleanup.
+
+2006-09-16  Sergei Golovan  <sgolovan at nes.ru>
+
 	* chess/chess.tcl, checkers/checkers.tcl, reversi/reversi.tcl:
 	  Made games using hook::run with upvar instead of hook::foldl
 	  (it looks more "ticklish"). Improved packing of game history

Modified: trunk/tkabber-plugins/traffic/traffic.tcl
===================================================================
--- trunk/tkabber-plugins/traffic/traffic.tcl	2006-09-18 21:01:30 UTC (rev 722)
+++ trunk/tkabber-plugins/traffic/traffic.tcl	2006-09-19 06:27:47 UTC (rev 723)
@@ -10,8 +10,8 @@
     ::msgcat::mcload [file join [file dirname [info script]] msgs]
     encoding system $sysenc
     unset sysenc
-    
-    array set stats {}    
+
+    array set stats {}
     array set jids {}
 
     # for table
@@ -27,24 +27,24 @@
     set columns [list presence,in presence,out iq,in iq,out message,in message,out total,in total,out]
     set sort_order total,in
 
-    set current_connid 0 
+    set current_connid 0
     set wished_connid "0 None"
 
     set timer 5
     set timer_state 0
     set timer_id {}
-    array set timer_names [list 0 [::msgcat::mc "On"] 1 [::msgcat::mc "Off"] ]
-    
+    array set timer_names [list 0 [::msgcat::mc "On"] 1 [::msgcat::mc "Off"]]
 
+
     # Options
     custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber
-    
+
     custom::defgroup Traffic [::msgcat::mc "Traffic plugin options."] -group Plugins
 
     custom::defvar options(show_nick) 1 \
         [::msgcat::mc "Show nick instead of JID."] \
         -type boolean -group Traffic
-    
+
     custom::defvar options(human_readable) 1 \
         [::msgcat::mc "Use human-readable counters with K, M, G."] \
         -type boolean -group Traffic
@@ -53,25 +53,25 @@
 }
 
 # ############################
-# Handlers 
+# Handlers
 
 proc traffic::handle_inout {connid xmldata size prefix attr} {
     variable stat
     variable jids
     variable columns
     variable names
-    
+
     jlib::wrapper:splitxml $xmldata tag vars isempty chdata children
-    
+
     set jid [jlib::wrapper:getattr $vars $attr]
-    if { [cequal $jid {}] } { 
+    if {[cequal $jid ""]} {
 	set jid "SERVER"
     } else {
 	catch { set jid [node_and_server_from_jid $jid] }
     }
-    
+
     if {![info exists jids($connid,$jid)]} {
-	
+
 	# create
         set jids($connid,$jid) 1
 	lappend jids($connid,jids) $jid
@@ -79,12 +79,12 @@
 	# initialize counters
 	foreach column $columns {
 	    set stat($connid,$jid,$column) 0
-	}    
-    }	
-    
+	}
+    }
+
     # increment counters
-    if { [info exists names($tag,$prefix)] } {
-        incr stat($connid,$jid,$tag,$prefix) $size 
+    if {[info exists names($tag,$prefix)]} {
+        incr stat($connid,$jid,$tag,$prefix) $size
     }
     incr stat($connid,$jid,total,$prefix) $size
 }
@@ -99,7 +99,7 @@
 # ############################
 # GUI
 
-proc traffic::get_connections { } {
+proc traffic::get_connections {} {
     set res [list "0 None"]
     foreach con [jlib::connections] {
 	lappend res [list $con [jlib::connection_jid $con]]
@@ -107,15 +107,15 @@
     return $res
 }
 
-proc traffic::open_window { } {
-    
+proc traffic::open_window {} {
+
     global font
     variable columns
     variable names
-    variable current_connid    
-    variable timer_state 
+    variable current_connid
+    variable timer_state
     variable timer_names
-    
+
     # create widgets
     set w .traffic_stats
     if {[winfo exists $w]} {
@@ -129,19 +129,19 @@
 
     # button box
     set tools [frame $w.tools -borderwidth 5]
-    pack $tools -side top -anchor w -fill x        
-	
+    pack $tools -side top -anchor w -fill x
+
     button $tools.refresh -text [::msgcat::mc "Refresh"] \
         -command [list [namespace current]::refresh]
     pack $tools.refresh -side right -anchor w
 
-    set connections [get_connections]    
+    set connections [get_connections]
     ComboBox $tools.connection -textvariable [namespace current]::wished_connid \
 	-values $connections -height [expr [llength $connections] + 1] -editable 0 \
 	-modifycmd [list [namespace current]::refresh]
     pack $tools.connection -side right -anchor w
     unset connections
-    
+
     button $tools.show_xml -text [::msgcat::mc "Show XML"] \
         -command [list ::plugins::rawxml::open_window]
     pack $tools.show_xml -side left -anchor w
@@ -164,8 +164,8 @@
     button $tools.timer -text $timer_names($timer_state) \
         -command [list [namespace current]::turn_timer]
     pack $tools.timer -side left -anchor w
-    
 
+
     # creatе grid
     set sw [ScrolledWindow $w.sw -relief sunken -bd 1]
     pack $sw -side top -fill both -expand yes
@@ -173,20 +173,20 @@
     set sf [ScrollableFrame $w.sf]
     $sw setwidget $sf
     set f [$sf getframe]
-              
-    
+
+
     # fill grid header
     grid [label $f.titlejid -text [::msgcat::mc "JID"] -width 20] -row 0 -column 0 -sticky w
     set i 1
     foreach col $columns {
-	
+
         set b [Button $f.titlelabel$i -text $names($col) \
 		    -command [list [namespace current]::sort $col]]
         grid $b -row 0 -column $i -sticky we
 
         incr i
     }
-    
+
     refresh
 }
 
@@ -194,36 +194,38 @@
     variable stat
     variable jids
     variable columns
-    variable wished_connid 
+    variable wished_connid
     variable current_connid
     variable options
 
     set w .traffic_stats
-    if {![winfo exists $w]} { return } 
+    if {![winfo exists $w]} return
     set f [$w.sf getframe]
 
-    
+
     # clear if connection was changed
-    if {![cequal [lindex $wished_connid 0] $current_connid]} {	
+    if {![cequal [lindex $wished_connid 0] $current_connid]} {
 	clear_table $f
 	set current_connid [lindex $wished_connid 0]
     }
-            
-    if {!$current_connid} { return }
 
+    if {!$current_connid} return
+
     # init sum counters
     array set sum {}
-    foreach col $columns { set sum($col) 0 }
-    
+    foreach col $columns {
+	set sum($col) 0
+    }
+
     # fill the table
     set row 1
     foreach jid [lsort -decreasing -command compare $jids($current_connid,jids)] {
 
-	set tag [jid_to_tag $jid]	
+	set tag [jid_to_tag $jid]
 
 	# set first column - jid
-	if {! [winfo exists $f.jid$tag]} { 	    
-	    label $f.jid$tag -text $jid 
+	if {![winfo exists $f.jid$tag]} {
+	    label $f.jid$tag -text $jid
 
 	    if {$options(show_nick)} {
 		set nick [roster::itemconfig $current_connid [roster::find_jid $current_connid $jid] -name]
@@ -233,13 +235,15 @@
 	    }
 	}
 	grid $f.jid$tag -row $row -column 0 -sticky w
-	
+
         set i 1
-	
+
 	# set other columns - counters
 	foreach col $columns  {
-	    
-	    if {![winfo exists $f.value$i$tag] } { label $f.value$i$tag }
+
+	    if {![winfo exists $f.value$i$tag]} {
+		label $f.value$i$tag
+	    }
 	    set value $stat($current_connid,$jid,$col)
 
 	    incr sum($col) $value
@@ -248,21 +252,25 @@
 
     	    incr i
 	}
-	
+
 	incr row
     }
-    
+
     # add sum
-    if {! [winfo exists $f.sumname]} { label $f.sumname -text [::msgcat::mc "Total"] }
+    if {![winfo exists $f.sumname]} {
+	label $f.sumname -text [::msgcat::mc "Total"]
+    }
     grid $f.sumname -row $row -column 0 -sticky w
 
     set i 1
     foreach col $columns {
-	if {![winfo exists $f.sumval$i] } { label $f.sumval$i }
+	if {![winfo exists $f.sumval$i]} {
+	    label $f.sumval$i
+	}
 	$f.sumval$i configure -text [convert_to_hr $sum($col) 10]
-	grid $f.sumval$i -row $row -column $i -sticky e	
+	grid $f.sumval$i -row $row -column $i -sticky e
 	incr i
-    }    
+    }
 }
 
 proc traffic::convert_to_hr {value limit} {
@@ -272,21 +280,23 @@
 	return $value
     }
 
-    set cur "" 
+    set cur ""
     foreach c {K M G} {
-      if { [expr $value / 1024] > $limit} {
-          set value [expr $value / 1024]
-          set cur $c
-      } else { break }
-    }  
-   return $value$cur
+	if {($value / 1024) > $limit} {
+	    set value [expr {$value / 1024}]
+	    set cur $c
+	} else {
+	    break
+	}
+    }
+    return $value$cur
 }
 
 proc traffic::clear_table {f} {
-    
-    set max [expr [string length $f.title] -1]
-    foreach slave [grid slaves $f] {	
-	if { ![cequal [string range $slave 0 $max] $f.title] } {
+
+    set max [expr [string length $f.title] - 1]
+    foreach slave [grid slaves $f] {
+	if {![cequal [string range $slave 0 $max] $f.title]} {
 	    grid remove $slave
 	    destroy $slave
 	}
@@ -296,7 +306,7 @@
 proc traffic::clear_and_refresh {args} {
 
     set w .traffic_stats
-    if {![winfo exists $w]} { return } 
+    if {![winfo exists $w]} return
     set f [$w.sf getframe]
 
     clear_table $f
@@ -305,19 +315,19 @@
 
 proc traffic::update_connections {type connid} {
     variable wished_connid
-    
+
     set w .traffic_stats
-    if {![winfo exists $w]} { return } 
-    
-    set connections [get_connections] 
-    
-    if { [cequal $type off] && [expr [lsearch $connections $wished_connid] < 0 ] } {
+    if {![winfo exists $w]} return
+
+    set connections [get_connections]
+
+    if {[cequal $type off] && [lsearch $connections $wished_connid] < 0} {
 	set wished_connid "0 None"
     }
 
     $w.tools.connection configure -values $connections  -height [expr [llength $connections] + 1]
 
-    if { [cequal $type on] && [cequal $wished_connid "0 None"] } {
+    if {[cequal $type on] && [cequal $wished_connid "0 None"]} {
 	set wished_connid [list $connid [jlib::connection_jid $connid]]
     }
 
@@ -329,38 +339,39 @@
     variable timer
 
     refresh
-    set timer_id [after [expr $timer * 1000] [list [namespace current]::tick]]    
+    set timer_id [after [expr $timer * 1000] [list [namespace current]::tick]]
 }
 
 proc traffic::turn_timer {args} {
-    variable timer_state 
-    variable timer_names  
-    variable timer_id  
+    variable timer_state
+    variable timer_names
+    variable timer_id
     variable timer
- 
+
     set timer_state [expr 1 - $timer_state]
 
     if {$timer_state}  {
-	set timer_id [after [expr $timer * 1000] [list [namespace current]::tick]]    
+	set timer_id [after [expr $timer * 1000] [list [namespace current]::tick]]
     } else {
 	after cancel $timer_id
     }
 
     set w .traffic_stats
-    if {![winfo exists $w]} { return } 
+    if {![winfo exists $w]} return
 
-    $w.tools.timer configure -text $timer_names($timer_state)    
+    $w.tools.timer configure -text $timer_names($timer_state)
 }
 
 # ############################
-# Other functions 
+# Other functions
 
 proc traffic::compare {arg1 arg2} {
     variable stat
     variable sort_order
     variable current_connid
 
-    return [expr $stat($current_connid,$arg1,$sort_order) - $stat($current_connid,$arg2,$sort_order)]
+    return [expr $stat($current_connid,$arg1,$sort_order) - \
+		 $stat($current_connid,$arg2,$sort_order)]
 }
 
 proc traffic::sort {tag} {
@@ -370,9 +381,9 @@
     refresh
 }
 
-proc traffic::save_stat { } {
+proc traffic::save_stat {} {
     variable names
-    variable columns 
+    variable columns
     variable stat
     variable current_connid
     variable jids
@@ -380,22 +391,25 @@
     set filename [tk_getSaveFile \
                       -initialdir ~/.tkabber/]
     if {$filename != ""} {
-
 	set fd [open $filename w]
 
 	# Header
 	set str {}
 	lappend str "JID"
-	foreach col $columns {lappend str $names($col)}
+	foreach col $columns {
+	    lappend str $names($col)
+	}
 	puts $fd [join $str "\t"]
-	
+
 	# Data
 	foreach jid $jids($current_connid,jids) {
 	    set str {}
 	    lappend str $jid
-	    foreach col $columns {lappend str $stat($current_connid,$jid,$col)}
-	    puts $fd [join $str "\t"]	    	    
-	}	
+	    foreach col $columns {
+		lappend str $stat($current_connid,$jid,$col)
+	    }
+	    puts $fd [join $str "\t"]
+	}
 	close $fd
     }
 }
@@ -405,22 +419,27 @@
     variable columns
     variable jids
     variable stat
-    
-    if {!$current_connid} {return}
-    
-    if { [MessageDlg .request_reset_traffic -aspect 50000 -icon warning \
-	      -type user -buttons {yes no} -default 1 \
-	      -cancel 1 -message [::msgcat::mc "A you sure you want to reset counters?"]] } {
-	return 
+
+    if {!$current_connid} return
+
+    if {[MessageDlg .request_reset_traffic \
+	     -aspect 50000 \
+	     -icon warning \
+	     -type user \
+	     -buttons {yes no} \
+	     -default 1 \
+	     -cancel 1 \
+	     -message [::msgcat::mc "A you sure you want to reset counters?"]]} {
+	return
     }
 
     # clear stats
     foreach jid $jids($current_connid,jids) {
-	foreach col $columns  {
+	foreach col $columns {
 	    unset stat($current_connid,$jid,$col)
-	}	
+	}
 	unset jids($current_connid,$jid)
-    }    
+    }
     set jids($current_connid,jids) {}
 
     # clear table
@@ -433,7 +452,6 @@
 proc traffic::setup_menu {} {
 
     catch {
-
 	set m [.mainframe getmenu plugins]
 	set ind [$m index end]
 
@@ -446,5 +464,8 @@
 # ##############################################
 # Hooks on connect and disconnect (update GUI)
 
-hook::add connected_hook [list [namespace current]::traffic::update_connections on]
-hook::add disconnected_hook [list [namespace current]::traffic::update_connections off]
+hook::add connected_hook \
+	  [list [namespace current]::traffic::update_connections on]
+hook::add disconnected_hook \
+	  [list [namespace current]::traffic::update_connections off]
+



More information about the Tkabber-dev mailing list