[Tkabber-dev] r779 - in trunk/tkabber: . plugins/filetransfer

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Sat Nov 4 13:07:21 MSK 2006


Author: sergei
Date: 2006-11-04 13:07:17 +0300 (Sat, 04 Nov 2006)
New Revision: 779

Modified:
   trunk/tkabber/ChangeLog
   trunk/tkabber/filetransfer.tcl
   trunk/tkabber/plugins/filetransfer/si.tcl
Log:
	* filetransfer.tcl, plugins/filetransfer/si.tcl: Fixed bug with
	  'stream id is in use' after choosing a file, which cannot be
	  written. Show error messages in the main file transfer windows,
	  not in separate message boxes. Inserted several checks of
	  transfer window existense (helps when the other side reply
	  is received after closing the window).


Modified: trunk/tkabber/ChangeLog
===================================================================
--- trunk/tkabber/ChangeLog	2006-11-03 21:27:53 UTC (rev 778)
+++ trunk/tkabber/ChangeLog	2006-11-04 10:07:17 UTC (rev 779)
@@ -1,5 +1,12 @@
 2006-11-04  Sergei Golovan  <sgolovan at nes.ru>
 
+	* filetransfer.tcl, plugins/filetransfer/si.tcl: Fixed bug with
+	  'stream id is in use' after choosing a file, which cannot be
+	  written. Show error messages in the main file transfer windows,
+	  not in separate message boxes. Inserted several checks of
+	  transfer window existense (helps when the other side reply
+	  is received after closing the window).
+
 	* plugins/richtext/emoticons.tcl: Fixed escaping regexp
 	  metacharacters. Also fixed emoticons::add procedure.
 

Modified: trunk/tkabber/filetransfer.tcl
===================================================================
--- trunk/tkabber/filetransfer.tcl	2006-11-03 21:27:53 UTC (rev 778)
+++ trunk/tkabber/filetransfer.tcl	2006-11-04 10:07:17 UTC (rev 779)
@@ -46,24 +46,34 @@
 }
 
 proc ft::get_POSIX_error_desc {} {
-	global errorCode
-	set class [lindex $errorCode 0]
-	if {$class != "POSIX"} {
-		return [::msgcat::mc "unknown"]
-	} else {
-		return [::msgcat::mc [lindex $errorCode 2]]
-	}
+    global errorCode
+    set class [lindex $errorCode 0]
+    if {$class != "POSIX"} {
+	return [::msgcat::mc "unknown"]
+    } else {
+	return [::msgcat::mc [lindex $errorCode 2]]
+    }
 }
 
-proc ft::report_cannot_open_file {w filename error} {
-    tk_messageBox -parent $w -icon error -type ok \
-	    -title [::msgcat::mc "File transfer error"] \
-	    -message [format \
-		[::msgcat::mc "Failed to open file \"%s\".\nThe error was: %s"] \
-	    	$filename $error
-	    ]
+proc ft::report_cannot_open_file {f filename error} {
+    report_error $f [::msgcat::mc "Can't open file \"%s\": %s" \
+				  $filename $error]
 }
 
+proc ft::report_error {f errormsg} {
+    set m $f.errormsg
+    catch {destroy $m}
+    message $m -aspect 50000 \
+	       -text $errormsg \
+	       -pady 1m
+    $m configure -foreground [option get $m errorForeground Message]
+    grid $m -row 0 -column 0 -sticky ewns -columnspan 4
+}
+
+proc ft::hide_error_msg {f} {
+    catch {destroy $f.errormsg}
+}
+
 hook::add chat_create_user_menu_hook \
     [namespace current]::ft::create_menu 46
 hook::add roster_create_groupchat_user_menu_hook \

Modified: trunk/tkabber/plugins/filetransfer/si.tcl
===================================================================
--- trunk/tkabber/plugins/filetransfer/si.tcl	2006-11-03 21:27:53 UTC (rev 778)
+++ trunk/tkabber/plugins/filetransfer/si.tcl	2006-11-04 10:07:17 UTC (rev 779)
@@ -66,17 +66,19 @@
     set state(pb) $f.pb
     set state(progress) 0
 
-    grid $f.lfile      -row 0 -column 0 -sticky e
-    grid $f.file       -row 0 -column 1 -sticky ew
-    grid $f.browsefile -row 0 -column 2 -sticky ew
+    # grid row 0 is used for displaying error messages
+
+    grid $f.lfile      -row 1 -column 0 -sticky e
+    grid $f.file       -row 1 -column 1 -sticky ew
+    grid $f.browsefile -row 1 -column 2 -sticky ew
     
-    grid $f.ldesc -row 1 -column 0 -sticky en
-    grid $f.desc  -row 1 -column 1 -sticky ewns -columnspan 2 -pady 1m
+    grid $f.ldesc -row 2 -column 0 -sticky en
+    grid $f.desc  -row 2 -column 1 -sticky ewns -columnspan 2 -pady 1m
 
-    grid $f.pb -row 2 -column 0 -sticky ew -columnspan 3 -pady 2m
+    grid $f.pb -row 3 -column 0 -sticky ew -columnspan 3 -pady 2m
 
     grid columnconfigure $f 1 -weight 1
-    grid rowconfigure $f 1 -weight 1
+    grid rowconfigure $f 2 -weight 1
 
     $w add -text [::msgcat::mc "Send"] \
 	   -command [list [namespace current]::send_file_negotiate $token]
@@ -103,12 +105,13 @@
     upvar #0 $token state
     variable chunk_size
 
+    ft::hide_error_msg $state(f)
     $state(w) itemconfigure 0 -state disabled
 
     set desc [$state(f).desc get 0.0 "end -1c"]
 
     if {[catch {open $state(filename)} fd]} {
-	ft::report_cannot_open_file $state(w) $state(filename) \
+	ft::report_cannot_open_file $state(f) $state(filename) \
 				    [ft::get_POSIX_error_desc]
 	$state(w) itemconfigure 0 -state normal
 	return
@@ -149,11 +152,12 @@
 proc si::send_file {token res} {
     upvar #0 $token state
 
+    # Peer's reply may arrive after window is closed.
+    if {![info exists state(w)] || ![winfo exists $state(w)]} return
+
     if {![lindex $res 0]} {
-	MessageDlg .auth_err -aspect 50000 -icon error \
-	    -message [format [::msgcat::mc "Request failed: %s"] \
-			  [lindex $res 1]] -type user \
-	    -buttons ok -default 0 -cancel 0
+	ft::report_error $state(f) \
+			 [::msgcat::mc "Request failed: %s" [lindex $res 1]]
 	close $state(fd)
 	$state(w) itemconfigure 0 -state normal
 	return
@@ -168,6 +172,8 @@
     upvar #0 $token state
     variable chunk_size
 
+    if {![info exists state(fd)]} return
+
     set chunk [read $state(fd) $chunk_size]
     if {$chunk != ""} {
 	si::send_data $state(stream) $chunk \
@@ -180,13 +186,13 @@
 proc si::send_chunk_response {token res} {
     upvar #0 $token state
 
+    # Peer's reply may arrive after window is closed.
+    if {![info exists state(w)] || ![winfo exists $state(w)]} return
     if {![info exists state(stream)]} return
 
     if {![lindex $res 0]} {
-	MessageDlg .auth_err -aspect 50000 -icon error \
-	    -message [format [::msgcat::mc "Transfer failed: %s"] \
-			  [lindex $res 1]] -type user \
-	    -buttons ok -default 0 -cancel 0
+	ft::report_error $state(f) \
+			 [::msgcat::mc "Transfer failed: %s" [lindex $res 1]]
 	if {$state(size) > 0} {
 	    set state(progress) 0
 	}
@@ -252,6 +258,8 @@
     label $f.lsaveas -text [::msgcat::mc "Save as:"]
     entry $f.saveas -textvariable ${token}(filename)
 
+    set state(f) $f
+
     set state(dir) $dir
     set state(name) $name
     set state(filename) [file join $dir $name]
@@ -265,38 +273,42 @@
     if {$size > 0} {
 	ProgressBar $f.pb -variable ${token}(progress)
 	$f.pb configure -maximum $size
-	grid $f.pb  -row 4 -column 0 -sticky ew -columnspan 3 -pady 2m
+	grid $f.pb  -row 5 -column 0 -sticky ew -columnspan 3 -pady 2m
     }
 
-    grid $f.lname   -row 0 -column 0 -sticky e
-    grid $f.name    -row 0 -column 1 -sticky w
-    
-    grid $f.lsize   -row 1 -column 0 -sticky e
-    grid $f.size    -row 1 -column 1 -sticky w
-    
-    grid $f.ldesc   -row 2 -column 0 -sticky en
-    grid $f.desc    -row 2 -column 1 -sticky ewns -columnspan 2 -pady 1m
+    # grid row 0 is used for displaying error messages
 
-    grid $f.lsaveas -row 3 -column 0 -sticky e
-    grid $f.saveas  -row 3 -column 1 -sticky ew
-    grid $f.browsefile  -row 3 -column 2 -sticky ew
+    grid $f.lname   -row 1 -column 0 -sticky e
+    grid $f.name    -row 1 -column 1 -sticky w
 
+    grid $f.lsize   -row 2 -column 0 -sticky e
+    grid $f.size    -row 2 -column 1 -sticky w
+
+    grid $f.ldesc   -row 3 -column 0 -sticky en
+    grid $f.desc    -row 3 -column 1 -sticky ewns -columnspan 2 -pady 1m
+
+    grid $f.lsaveas -row 4 -column 0 -sticky e
+    grid $f.saveas  -row 4 -column 1 -sticky ew
+    grid $f.browsefile  -row 4 -column 2 -sticky ew
+
     grid columnconfigure $f 1 -weight 1 -minsize 8c
-    grid rowconfigure $f 2 -weight 1
+    grid rowconfigure $f 3 -weight 1
     
     $w add -text [::msgcat::mc "Receive"] -command \
 	[list [namespace current]::recv_file_start $token]
     $w add -text [::msgcat::mc "Cancel"] -command \
 	[list [namespace current]::recv_file_cancel $token]
     
+    bind $w <Destroy> [list [namespace current]::recv_file_close $token $w %W]
+
     $w draw
 
     # Can't avoid vwait, because this procedure must return result or error
     vwait ${token}(result)
 
-    set result $state(result)
-    if {[lindex $result 0] == "error"} {
-	catch { unset $token }
+    lassign $state(result) destroy result
+    if {$destroy} {
+	destroy $state(w)
     }
 
     return $result
@@ -317,8 +329,8 @@
 proc si::recv_file_cancel {token} {
     upvar #0 $token state
 
-    set state(result) [list error cancel not-allowed]
-    destroy $state(w)
+    set state(result) [list 1 [list error cancel not-allowed \
+					  -text "Transfer is rejecterd"]]
 }
 
 ###############################################################################
@@ -326,36 +338,38 @@
 proc si::recv_file_start {token} {
     upvar #0 $token state
 
-    if {[catch {si::newin $state(connid) $state(jid) $state(id)} stream]} {
-	set state(result) [list error modify bad-request \
-				-text "Stream ID is in use"]
-	destroy $state(w)
-	return
-    }
+    ft::hide_error_msg $state(f)
 
-    set state(stream) $stream
-
     if {[catch {open $state(filename) w} fd]} {
-	ft::report_cannot_open_file $state(w) $state(filename) \
+	ft::report_cannot_open_file $state(f) $state(filename) \
 				    [ft::get_POSIX_error_desc]
 	return
     }
 
     fconfigure $fd -translation binary
 
+    $state(w) itemconfigure 0 -state disabled
+
+    if {[catch {si::newin $state(connid) $state(jid) $state(id)} stream]} {
+	# Return error to the sender but leave transfer window with disabled
+	# 'Receive' button and error message.
+	set state(result) [list 0 [list error modify bad-request \
+					    -text "Stream ID is in use"]]
+	ft::report_error $state(f) \
+	    [error_to_string [::msgcat::mc "Receive error: Stream ID is in use"]]
+	return
+    }
+
+    set state(stream) $stream
+
     set state(fd) $fd
-    set w $state(w)
 
-    $state(w) itemconfigure 0 -state disabled
-    bind $state(w) <Destroy> [list [namespace current]::recv_file_close \
-				   $token $state(w) %W]
-
     si::set_readable_handler \
 	$stream [list [namespace current]::recv_file_chunk $token]
     si::set_closed_handler \
 	$stream [list [namespace current]::closed $token]
 
-    set state(result) {}
+    set state(result) [list 0 {}]
 }
 
 ###############################################################################
@@ -363,19 +377,18 @@
 proc si::recv_file_chunk {token stream} {
     upvar #0 $token state
 
-    if {[info exists state(stream)] && $state(stream) == $stream} {
-	set fd $state(fd)
-	set filename $state(filename)
-	set data [si::read_data $stream]
+    if {![info exists state(w)] || ![winfo exists $state(w)]} {return 0}
+    if {![info exists state(stream)] || !$state(stream) == $stream} {return 0}
 
-	debugmsg filetransfer "RECV into $filename data $data"
+    set fd $state(fd)
+    set filename $state(filename)
+    set data [si::read_data $stream]
 
-	puts -nonewline $fd $data
-	set state(progress) [tell $fd]
-	return 1
-    } else {
-	return 0
-    }
+    debugmsg filetransfer "RECV into $filename data $data"
+
+    puts -nonewline $fd $data
+    set state(progress) [tell $fd]
+    return 1
 }
 
 ###############################################################################
@@ -383,10 +396,11 @@
 proc si::closed {token stream} {
     upvar #0 $token state
 
-    if {[info exists state(stream)] && $state(stream) == $stream} {
-	debugmsg filetransfer CLOSE
-    	destroy $state(w)
-    }
+    if {![info exists state(w)] || ![winfo exists $state(w)]} {return 0}
+    if {![info exists state(stream)] || !$state(stream) == $stream} {return 0}
+
+    debugmsg filetransfer "CLOSE"
+    destroy $state(w)
 }
 
 ###############################################################################
@@ -402,6 +416,7 @@
 }
 
 ###############################################################################
+###############################################################################
 
 proc si::si_handler {connid from id mimetype child} {
     debugmsg filetransfer "SI set: [list $from $child]"



More information about the Tkabber-dev mailing list