[Tkabber-dev] r1319 - in trunk/tkabber: . plugins/chat

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sun Nov 25 14:14:39 MSK 2007


Author: sergei
Date: 2007-11-25 14:14:38 +0300 (Sun, 25 Nov 2007)
New Revision: 1319

Modified:
   trunk/tkabber/ChangeLog
   trunk/tkabber/chats.tcl
   trunk/tkabber/plugins/chat/nick_colors.tcl
Log:
	* chats.tcl, plugins/chat/nick_colors.tcl: Implemented storing and
	  using nick colors using more efficient algorithm.


Modified: trunk/tkabber/ChangeLog
===================================================================
--- trunk/tkabber/ChangeLog	2007-11-20 18:54:06 UTC (rev 1318)
+++ trunk/tkabber/ChangeLog	2007-11-25 11:14:38 UTC (rev 1319)
@@ -1,3 +1,8 @@
+2007-11-25  Sergei Golovan  <sgolovan at nes.ru>
+
+	* chats.tcl, plugins/chat/nick_colors.tcl: Implemented storing and
+	  using nick colors using more efficient algorithm.
+
 2007-11-20  Sergei Golovan  <sgolovan at nes.ru>
 
 	* plugins/chat/draw_timestamp.tcl: Replaced [clock scan] by [expr] in

Modified: trunk/tkabber/chats.tcl
===================================================================
--- trunk/tkabber/chats.tcl	2007-11-20 18:54:06 UTC (rev 1318)
+++ trunk/tkabber/chats.tcl	2007-11-25 11:14:38 UTC (rev 1319)
@@ -1134,7 +1134,7 @@
 	    if {$::plugins::nickcolors::options(use_colored_roster_nicks)} {
 		ifacetk::roster::changeforeground $userswin \
 		    [list $connid $jid] $status \
-		    [plugins::nickcolors::get_nick_color $nick]
+		    [plugins::nickcolors::get_color $nick]
 	    } else {
 		ifacetk::roster::changeforeground $userswin \
 		    [list $connid $jid] $status

Modified: trunk/tkabber/plugins/chat/nick_colors.tcl
===================================================================
--- trunk/tkabber/plugins/chat/nick_colors.tcl	2007-11-20 18:54:06 UTC (rev 1318)
+++ trunk/tkabber/plugins/chat/nick_colors.tcl	2007-11-25 11:14:38 UTC (rev 1319)
@@ -5,6 +5,8 @@
 #
 # $Id$
 
+package require crc16
+
 namespace eval nickcolors {
 
     custom::defvar options(use_colored_nicks) 0 \
@@ -23,6 +25,7 @@
 	-command [namespace current]::change_options
 
     hook::add open_chat_post_hook [namespace current]::chat_add_nick_colors
+    hook::add close_chat_post_hook [namespace current]::chat_delete_nick_colors
     hook::add quit_hook           [namespace current]::save_nick_colors
     hook::add draw_message_hook   [namespace current]::check_nick 60
     hook::add finload_hook        [namespace current]::init_nick_colors
@@ -102,74 +105,68 @@
     close $f
 }
 
+proc nickcolors::get_color {nick} {
+    variable NickColors
+    variable NickColorPool
+
+    if {[info exists NickColors($nick)]} {
+	return $NickColors($nick)
+    } else {
+	set index [expr {[crc::crc16 $nick] % [llength $NickColorPool]}]
+	return [lindex $NickColorPool $index]
+    }
+}
+
+proc nickcolors::set_color {chatid nick color} {
+    variable options
+
+    if {[catch {set w [chat::chat_win $chatid]}] || \
+	    ![winfo exists $w]} {
+        return
+    }
+
+    if {$options(use_colored_nicks)} {
+	$w tag configure NICK-$nick -foreground $color
+	$w tag configure NICKMSG-$nick -foreground $color
+    }
+    if {$options(use_colored_messages)} {
+	$w tag configure MSG-$nick -foreground $color
+	$w tag lower MSG-$nick
+    }
+}
+
 # Called upon opening a new chat window. This added all the currently defined
 # nick-colors as tags into the text widget.
 #
 proc nickcolors::chat_add_nick_colors {chatid type} {
-    variable options
-    variable NickColors
+    variable NicksInChat
 
     debugmsg chat "on_open_chat $chatid $type"
-    if {[catch { set W [chat::chat_win $chatid] }]} {
-        return
-    }
-    
-    foreach {nick clr} [array get NickColors] {
-	if {$options(use_colored_nicks)} {
-	    $W tag configure NICK-$nick -foreground $clr
-	    $W tag configure NICKMSG-$nick -foreground $clr
-	}
-	if {$options(use_colored_messages)} {
-	    $W tag configure MSG-$nick -foreground $clr
-	    $W tag lower MSG-$nick
-	}
-    }
+    set NicksInChat($chatid) {}
 }
 
-proc nickcolors::get_nick_color {nick} {
-    variable options
-    variable NickColorPool
-    variable NickColors
+proc nickcolors::chat_delete_nick_colors {chatid} {
+    variable NicksInChat
 
-    if {![info exists NickColors($nick)]} {
-        set ndx [expr {int(rand() * [llength $NickColorPool])}]
-        set NickColors($nick) [lindex $NickColorPool $ndx]
-	foreach {id wn} [array get chat::chats chatwin,*] {
-	    if {[winfo exists $wn]} {
-		if {$options(use_colored_nicks)} {
-		    $wn tag configure NICK-$nick -foreground $NickColors($nick)
-		    $wn tag configure NICKMSG-$nick -foreground $NickColors($nick)
-		}
-		if {$options(use_colored_messages)} {
-		    $wn tag configure MSG-$nick -foreground $NickColors($nick)
-		    $wn tag lower MSG-$nick
-		}
-	    }
-	}
-    }
-    return $NickColors($nick)
+    debugmsg chat "on_close_chat $chatid"
+    catch {unset NicksInChat($chatid)}
 }
 
 # draw_message hook used to check that the nick exists as a color and tag.
 proc nickcolors::check_nick {chatid from type body x} {
-    variable options
-    variable NickColorPool
-    variable NickColors
+    variable NicksInChat
 
     set connid [chat::get_connid $chatid]
-
     set nick [chat::get_nick $connid $from $type]
-    if {$nick == "ijchain"} {
-        set e [string first > $body]
-        set nick [string trim [string range $body 0 $e] <>]
+    if {[lsearch -exact $NicksInChat($chatid) $nick] < 0} {
+	lappend NicksInChat($chatid) $nick
+	set_color $chatid $nick [get_color $nick]
     }
-    get_nick_color $nick
 }
 
 proc nickcolors::edit_nick_colors {} {
     global font
     variable NickColors
-    variable NickColorEditDlg
     variable NickColorEdits
 
     array set NickColorEdits [array get NickColors]
@@ -228,7 +225,7 @@
         array set NickColors [array get NickColorEdits]
 	change_options
     }
-    catch { unset NickColorEdits }
+    catch {unset NickColorEdits}
 }
 
 proc nickcolors::on_nick_hover {w nick event} {
@@ -259,7 +256,7 @@
 
 proc nickcolors::change_options {args} {
     variable options
-    variable NickColors
+    variable NicksInChat
 
     foreach chatid [chat::opened] {
 	set wn [chat::chat_win $chatid]
@@ -267,7 +264,8 @@
 	    if {[chat::is_groupchat $chatid]} {
 		chat::redraw_roster_after_idle $chatid
 	    }
-            foreach {nick clr} [array get NickColors] {
+            foreach nick $NicksInChat($chatid) {
+		set clr [get_color $nick]
                 $wn tag configure NICK-$nick \
 		    -foreground [expr {$options(use_colored_nicks) ? $clr : ""}]
                 $wn tag configure NICKMSG-$nick \
@@ -319,11 +317,13 @@
 proc nickcolors::edit_nick_color {chatwin nick} {
     variable NickColors
 
-    set new [tk_chooseColor -initialcolor $NickColors($nick) -parent $chatwin \
+    set new [tk_chooseColor -initialcolor [get_color $nick] -parent $chatwin \
 		 -title [format [::msgcat::mc "Edit %s color"] $nick]]
     if {$new == ""} return
 
-    set NickColors($nick) $new
-    change_options
+    if {$new != [get_color $nick]} {
+	set NickColors($nick) $new
+	change_options
+    }
 }
 



More information about the Tkabber-dev mailing list