[Tkabber-dev] r1360 - branches/xml-import-export/plugins/roster

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Wed Jan 30 05:32:19 MSK 2008


Author: kostix
Date: 2008-01-30 05:32:19 +0300 (Wed, 30 Jan 2008)
New Revision: 1360

Added:
   branches/xml-import-export/plugins/roster/bkup_categories.tcl
Modified:
   branches/xml-import-export/plugins/roster/backup.tcl
   branches/xml-import-export/plugins/roster/conferences.tcl
Log:
plugins/roster/backup.tcl: Added two new hooks -- roster_deserializing_hook
 and roster_deserialized_hook to aid restoration of MUC bookmarks and their groups.

plugins/roster/bkup_categories.tcl: Implemented new internal plugin for
 exporting/importing of MUC bookmarks.

plugins/roster/conferences.tcl: Several changes to fit the needs of the MUC
 bookmarks backup plugin:
 * The bulk of the [store_bookmarks] moved to the new proc [serialize_bookmarks].
 * Implemented [create_muc_bookmark] and [create_muc_bmgroup] procs to handle
   parsing of corresponding xml data and adding bookmarks/groups to their
   internal data structures.
 * Implemented [push_bookmarks_to_roster] which simulates a "roster push"
   for the MUC bookmarks so that they appear in the roster widget.

NOTE: restoration of MUC bookmarks doesn't honor the bookmarks' groups
 due to some unknown bug.


Modified: branches/xml-import-export/plugins/roster/backup.tcl
===================================================================
--- branches/xml-import-export/plugins/roster/backup.tcl	2008-01-29 15:52:03 UTC (rev 1359)
+++ branches/xml-import-export/plugins/roster/backup.tcl	2008-01-30 02:32:19 UTC (rev 1360)
@@ -137,12 +137,16 @@
 ###############################################################################
 
 proc rosterbackup::deserialize_roster {connid data} {
+    hook::run roster_deserializing_hook $connid
+
     set parser [jlib::wrapper:new "#" "#" \
 	[list [namespace current]::parse_roster_xml $connid]]
     jlib::wrapper:elementstart $parser stream:stream {} {}
     jlib::wrapper:parser $parser parse $data
     jlib::wrapper:parser $parser configure -final 0
     jlib::wrapper:free $parser
+
+    hook::run roster_deserialized_hook $connid
 }
 
 ###############################################################################

Added: branches/xml-import-export/plugins/roster/bkup_categories.tcl
===================================================================
--- branches/xml-import-export/plugins/roster/bkup_categories.tcl	                        (rev 0)
+++ branches/xml-import-export/plugins/roster/bkup_categories.tcl	2008-01-30 02:32:19 UTC (rev 1360)
@@ -0,0 +1,88 @@
+# $Id$
+# Depends on: conferences.tcl, backup.tcl
+
+namespace eval mucbackup {
+    variable updated; array set updated {}
+
+    # Should probably go after the roster contacts, so we set prio to 70:
+    hook::add serialize_roster_hook \
+	[namespace current]::serialize_muc_bookmarks 70
+    hook::add deserialize_roster_hook \
+	[namespace current]::deserialize_muc_bookmarks 70
+    hook::add roster_deserializing_hook \
+	[namespace current]::prepare_deserialization
+    hook::add roster_deserialized_hook \
+	[namespace current]::push_bookmarks_to_roster
+}
+
+###############################################################################
+
+proc mucbackup::serialize_muc_bookmarks {connid level varName} {
+    upvar $level $varName subtags
+    global NS
+
+    foreach xmldata [::plugins::conferences::serialize_bookmarks $connid] {
+	lappend subtags [jlib::wrapper:createtag privstorage \
+	    -vars [list xmlns $NS(private)] \
+	    -subtags [list $xmldata]]
+    }
+}
+
+###############################################################################
+
+proc mucbackup::prepare_deserialization {connid} {
+    variable updated
+
+    set updated($connid) 0
+}
+
+###############################################################################
+
+proc mucbackup::deserialize_muc_bookmarks {connid data} {
+    variable updated
+    upvar 0 updated($connid) upd
+    global NS
+
+    jlib::wrapper:splitxml $data tag vars isempty cdata children
+    if {![string equal $tag privstorage]} return
+    set xmlns [jlib::wrapper:getattr $vars xmlns]
+    if {![string equal $xmlns $NS(private)]} {
+	return -code error "Bad roster element namespace \"$xmlns\":\
+	    must be \"$NS(private)\""
+    }
+
+    foreach storage $children {
+	jlib::wrapper:splitxml $storage ctag cvars cisempty ccdata cchildren
+	if {![string equal $ctag storage]} return
+	set xmlns [jlib::wrapper:getattr $cvars xmlns]
+	switch -- $xmlns \
+	    $NS(bookmarks) {
+		foreach child $cchildren {
+		    set added [::plugins::conferences::create_muc_bookmark \
+			    $connid $child -merge yes]
+		    set upd [expr {$upd || $added}]
+		}
+	    } \
+	    $NS(tkabber:groups) {
+		foreach child $cchildren {
+		    set added [::plugins::conferences::create_muc_bmgroup \
+			    $connid $child -merge yes]
+		    set upd [expr {$upd || $added}]
+		}
+	    }
+    }
+}
+
+###############################################################################
+
+proc mucbackup::push_bookmarks_to_roster {connid} {
+    variable updated
+
+    if {$updated($connid)} {
+	::plugins::conferences::push_bookmarks_to_roster $connid
+    }
+
+    unset updated($connid)
+}
+
+# vim:ts=8:sw=4:sts=4:noet


Property changes on: branches/xml-import-export/plugins/roster/bkup_categories.tcl
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: branches/xml-import-export/plugins/roster/conferences.tcl
===================================================================
--- branches/xml-import-export/plugins/roster/conferences.tcl	2008-01-29 15:52:03 UTC (rev 1359)
+++ branches/xml-import-export/plugins/roster/conferences.tcl	2008-01-30 02:32:19 UTC (rev 1360)
@@ -87,59 +87,111 @@
 
 	if {[jlib::wrapper:getattr $vars1 xmlns] == $::NS(bookmarks)} {
 	    foreach bookmark $children1 {
-		jlib::wrapper:splitxml $bookmark btag bvars bisempty bcdata bchildren
+		create_muc_bookmark $connid $bookmark
+	    }
+	} elseif {[jlib::wrapper:getattr $vars1 xmlns] == $::NS(tkabber:groups)} {
+	    foreach bookmark $children1 {
+		create_muc_bmgroup $connid $bookmark
+	    }
+	}
+    }
 
-		if {$btag != "conference"} continue
+    if {$responds($connid) < 2} return
 
-		set jid [string tolower [jlib::wrapper:getattr $bvars jid]]
-		set bookmarks($connid,jid,$jid) $jid
+    push_bookmarks_to_roster $connid
+    after idle [list [namespace current]::autojoin_groups $connid]
+}
 
-		set bookmarks($connid,name,$jid) [jlib::wrapper:getattr $bvars name]
-		set bookmarks($connid,nick,$jid) ""
-		set bookmarks($connid,password,$jid) ""
-		if {![info exists bookmarks($connid,groups,$jid)]} {
-		    set bookmarks($connid,groups,$jid) {}
-		}
+proc conferences::create_muc_bookmark {connid xmldata args} {
+    variable bookmarks
 
-		set autojoin [jlib::wrapper:getattr $bvars autojoin]
-		switch -- $autojoin {
-		    1 -
-		    true { set bookmarks($connid,autojoin,$jid) 1 }
-		    default { set bookmarks($connid,autojoin,$jid) 0 }
-		}
-		
-		foreach bch $bchildren {
-		    jlib::wrapper:splitxml \
-			$bch tag2 vars2 isempty2 cdata2 children2
-		    switch -- $tag2 {
-			nick { set bookmarks($connid,nick,$jid) $cdata2 }
-			password { set bookmarks($connid,password,$jid) $cdata2 }
-		    }
-		}
+    set merge 0
+    foreach {opt val} $args {
+	switch -- $opt {
+	    -merge { set merge $val }
+	    default {
+		return -code error "Bad option \"$opt\":\
+		    must be -merge"
 	    }
-	} elseif {[jlib::wrapper:getattr $vars1 xmlns] == $::NS(tkabber:groups)} {
-	    foreach bookmark $children1 {
-		jlib::wrapper:splitxml $bookmark btag bvars bisempty bcdata bchildren
+	}
+    }
 
-		if {$btag != "conference"} continue
+    jlib::wrapper:splitxml $xmldata btag bvars bisempty bcdata bchildren
 
-		set jid [string tolower [jlib::wrapper:getattr $bvars jid]]
+    if {![string equal $btag conference]} { return 0 }
 
-		set groups {}
-		foreach bch $bchildren {
-		    jlib::wrapper:splitxml \
-			$bch tag2 vars2 isempty2 cdata2 children2
-		    switch -- $tag2 {
-			group { lappend groups $cdata2 }
-		    }
-		}
-		set bookmarks($connid,groups,$jid) $groups
+    set jid [string tolower [jlib::wrapper:getattr $bvars jid]]
+
+    if {$merge && [info exists bookmarks($connid,jid,$jid)]} {
+	return 0
+    } else {
+	set bookmarks($connid,jid,$jid) $jid
+
+	set bookmarks($connid,name,$jid) [jlib::wrapper:getattr $bvars name]
+	set bookmarks($connid,nick,$jid) ""
+	set bookmarks($connid,password,$jid) ""
+	if {![info exists bookmarks($connid,groups,$jid)]} {
+	    set bookmarks($connid,groups,$jid) {}
+	}
+
+	set autojoin [jlib::wrapper:getattr $bvars autojoin]
+	switch -- $autojoin {
+	    1 -
+	    true { set bookmarks($connid,autojoin,$jid) 1 }
+	    default { set bookmarks($connid,autojoin,$jid) 0 }
+	}
+	
+	foreach bch $bchildren {
+	    jlib::wrapper:splitxml \
+		$bch tag2 vars2 isempty2 cdata2 children2
+	    switch -- $tag2 {
+		nick { set bookmarks($connid,nick,$jid) $cdata2 }
+		password { set bookmarks($connid,password,$jid) $cdata2 }
 	    }
 	}
+	return 1
     }
+}
 
-    if {$responds($connid) < 2} return
+proc conferences::create_muc_bmgroup {connid xmldata args} {
+    variable bookmarks
 
+    set merge 0
+    foreach {opt val} $args {
+	switch -- $opt {
+	    -merge { set merge $val }
+	    default {
+		return -code error "Bad option \"$opt\":\
+		    must be -merge"
+	    }
+	}
+    }
+
+    jlib::wrapper:splitxml $xmldata btag bvars bisempty bcdata bchildren
+
+    if {![string equal $btag conference]} return
+
+    set jid [string tolower [jlib::wrapper:getattr $bvars jid]]
+
+    set groups [list]
+    foreach bch $bchildren {
+	jlib::wrapper:splitxml $bch tag2 vars2 isempty2 cdata2 children2
+	if {[string equal $tag2 group]} {
+	    lappend groups $cdata2
+	}
+    }
+
+    if {$merge && [info exists bookmarks($connid,groups,$jid)]} {
+	return 0
+    } else {
+	set bookmarks($connid,groups,$jid) $groups
+	return 1
+    }
+}
+
+proc conferences::push_bookmarks_to_roster {connid} {
+    variable bookmarks
+
     foreach idx [array names bookmarks $connid,jid,*] {
 	set jid $bookmarks($idx)
 	client:roster_push $connid $jid $bookmarks($connid,name,$jid) \
@@ -147,7 +199,6 @@
 			   bookmark ""
 	roster::override_category_and_subtype $connid $jid conference ""
     }
-    after idle [list [namespace current]::autojoin_groups $connid]
 }
 
 ###############################################################################
@@ -155,7 +206,7 @@
 #   Store bookmarks
 #
 
-proc conferences::store_bookmarks {connid} {
+proc conferences::serialize_bookmarks {connid} {
     variable bookmarks
 
     set bookmarklist {}
@@ -189,17 +240,20 @@
 			       -subtags $groups]
     }
 
-    private::store [list [jlib::wrapper:createtag storage \
-			      -vars [list xmlns $::NS(bookmarks)] \
-			      -subtags $bookmarklist]] \
-	-command [list [namespace current]::store_bookmarks_result $connid] \
-	-connection $connid
+    list [jlib::wrapper:createtag storage \
+	    -vars [list xmlns $::NS(bookmarks)] \
+	    -subtags $bookmarklist] \
+	[jlib::wrapper:createtag storage \
+	    -vars [list xmlns $::NS(tkabber:groups)] \
+	    -subtags $grouplist]
+}
 
-    private::store [list [jlib::wrapper:createtag storage \
-			      -vars [list xmlns $::NS(tkabber:groups)] \
-			      -subtags $grouplist]] \
-	-command [list [namespace current]::store_bookmarks_result $connid] \
-	-connection $connid
+proc conferences::store_bookmarks {connid} {
+    foreach item [serialize_bookmarks $connid] {
+	private::store [list $item] \
+	    -command [list [namespace current]::store_bookmarks_result $connid] \
+	    -connection $connid
+    }
 }
 
 proc conferences::store_bookmarks_result {connid res child} {
@@ -865,5 +919,4 @@
 hook::add disco_node_menu_hook \
 	  [namespace current]::conferences::disco_node_menu_setup 50
 
-###############################################################################
-
+# vim:ts=8:sw=4:sts=4:noet



More information about the Tkabber-dev mailing list