[Tkabber-dev] r491 - trunk/plugins/notes

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Wed Jul 13 23:08:21 MSD 2011


Author: Totktonada
Date: 2011-07-13 23:08:21 +0400 (Wed, 13 Jul 2011)
New Revision: 491

Modified:
   trunk/plugins/notes/notes.tcl
Log:
Notes plugin:

1. Fix errors with load/unload plugin.
2. Add/remove connections to/from GUI without reopen notes tab.


Modified: trunk/plugins/notes/notes.tcl
===================================================================
--- trunk/plugins/notes/notes.tcl	2011-06-05 21:43:57 UTC (rev 490)
+++ trunk/plugins/notes/notes.tcl	2011-07-13 19:08:21 UTC (rev 491)
@@ -20,23 +20,25 @@
 proc notes::load {} {
     variable notes
     array set notes {}
-    variable current_xlib
 
     hook::add finload_hook [namespace current]::setup_menu
     hook::add disconnected_hook [namespace current]::disconnected
     hook::add connected_hook [namespace current]::connected
+
+    if {[llength [connections]] > 0} {
+        request_notes [lindex [connections 0]]
+    }
+
     setup_menu
 }
 
 proc notes::unload {} {
-    variable notes
-    variable current_xlib
     hook::remove finload_hook [namespace current]::setup_menu
     hook::remove disconnected_hook [namespace current]::disconnected
     hook::remove connected_hook [namespace current]::connected
     desetup_menu
-    array unset notes
-    catch { unset current_xlib }
+    destroy_win .notes
+    free_all_notes
 }
 
 proc notes::open_window {} {
@@ -47,7 +49,7 @@
         return
     }
 
-    if {[llength [connections]] == 0} return
+#    if {[llength [connections]] == 0} return
 
     add_win $w -title [::msgcat::mc "Notes"] \
         -tabtitle [::msgcat::mc "Notes"] \
@@ -85,9 +87,33 @@
 
 ##############################################################################
 
+proc notes::free_all_notes {} {
+    variable notes
+    variable current_xlib
+
+    catch { unset current_xlib }
+    array unset notes
+
+    update_gui
+}
+
 proc notes::free_notes {xlib} {
     variable notes
+    variable current_xlib
+
+    if {[info exists current_xlib]} {
+        if {[cequal $current_xlib $xlib]} {
+            if {[llength [connections]] > 0} {
+                set current_xlib [lindex [connections] 0]
+            } else {
+                catch { unset current_xlib }
+                # TODO: need unset if {![cequal $current_xlib $xlib]} ?
+            }
+        }
+    }
+
     array unset notes $xlib
+    update_gui
 }
 
 proc notes::request_notes {xlib} {
@@ -96,38 +122,11 @@
 }
 
 proc notes::disconnected {xlib} {
-    variable current_xlib
-
     free_notes $xlib
-
-    if {[llength [connections]] == 0} {
-        unset current_xlib
-# TODO: if exist one connection, notes tab closed after reconnect
-#        catch { destroy_win .notes }
-        return
-    }
-
-    if {[cequal $current_xlib $xlib]} {
-        set current_xlib [lindex [connections] 0]
-    }
-
-# TODO: remove $xlib from #conn_button.menu
 }
 
 proc notes::connected {xlib} {
-    variable current_xlib
-
     request_notes $xlib
-
-    if {![info exist current_xlib]} {
-        set current_xlib $xlib
-#        set current_xlib [lindex [connections] 0]
-    }
-
-# TODO: add $xlib to $conn_button.menu
-#    if {[winfo exist .notes]} {        }
-#        .notes.tools.conn_button.menu
-#{    }
 }
 
 proc notes::process_notes {xlib status noteslist} {
@@ -138,10 +137,18 @@
 
     free_notes $xlib
     set notes($xlib) $noteslist
+    if {![info exist current_xlib]} {
+        set current_xlib $xlib
+    }
 
-    if {[expr [winfo exists .notes] && [cequal $current_xlib $xlib]]} {
-        update_lbox
+    if {[winfo exists .notes]} {
+        update_connections_menu
+        update_connections_menu_label
+        if {[cequal $current_xlib $xlib]} {
+            update_lbox
+        }
     }
+
     return
 }
 
@@ -181,22 +188,13 @@
 
 # TODO: from menubutton to OptionMenu?
 #    set conn_menu [OptionMenu $tools.conn_menu [connections]]
-    
+
     # ==== List of connections ====
     set conn_button [menubutton $tools.conn_button \
-        -text [connection_jid $current_xlib] \
         -menu $tools.conn_button.menu]
-
     set conn_menu [menu $conn_button.menu -tearoff 0]
-    foreach conn [connections] {
-        $conn_menu add command \
-            -label [connection_jid $conn] \
-            -command [namespace code [format {
-                variable current_xlib
-                set current_xlib %1$s
-                update_lbox} $conn]]
-    }
-
+    update_connections_menu
+    update_connections_menu_label
     pack $conn_button -anchor w -side left
     # ==== ====
 
@@ -233,6 +231,42 @@
     grid rowconfigure $lbox_frame 0 -weight 1
 }
 
+proc notes::update_gui {} {
+    if {[winfo exists .notes]} {
+        update_connections_menu
+        update_connections_menu_label
+        update_lbox
+   }
+}
+
+proc notes::update_connections_menu_label {} {
+    variable current_xlib
+    set conn_button .notes.tools.conn_button
+
+    if {[info exists current_xlib]} {
+        set text [connection_jid $current_xlib]
+    } else {
+        set text "Not connected"
+    }
+
+    $conn_button configure -text $text
+}
+
+proc notes::update_connections_menu {} {
+    set conn_menu .notes.tools.conn_button.menu
+    $conn_menu delete 0 end
+
+    foreach conn [connections] {
+        $conn_menu add command \
+            -label [connection_jid $conn] \
+            -command [namespace code [format {
+                variable current_xlib
+                set current_xlib %1$s
+                update_connections_menu_label
+                update_lbox} $conn]]
+    }
+}
+
 proc notes::delete_focused_note {} {
 #TODO: ask "Are you sure..?".
     variable notes
@@ -292,10 +326,10 @@
 
 # Next 3 commands from ${PATH_TO_TKABBER}/plugins/roster/annotations.tcl (annotations::show_dialog).
     bind $dialog_frame.text <Control-Key-Return> "[double% $dialog_w] invoke default
-				       break"
+                                       break"
     bind $dialog_w <Key-Return> { }
     bind $dialog_w <Control-Key-Return> "[double% $dialog_w] invoke default
-				  break"
+                                  break"
 
     $dialog_w draw $dialog_frame.title
 }
@@ -341,8 +375,10 @@
     set lbox .notes.lbox_frame.lbox
     $lbox delete 0 end
 
-    foreach note $notes($current_xlib) {
-        $lbox insert end [get_short_string $note]
+    if {[info exists current_xlib]} {
+        foreach note $notes($current_xlib) {
+            $lbox insert end [get_short_string $note]
+        }
     }
 #    $lbox selection clear 0 end
 #    $lbox selection set 0



More information about the Tkabber-dev mailing list