[Tkabber-dev] r1353 - in branches/xml-import-export: . ifacetk

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Mon Jan 28 05:27:04 MSK 2008


Author: kostix
Date: 2008-01-28 05:27:04 +0300 (Mon, 28 Jan 2008)
New Revision: 1353

Modified:
   branches/xml-import-export/ifacetk/iroster.tcl
   branches/xml-import-export/roster.tcl
Log:
roster.tcl: Added preliminary framework for working with XML
 roster backup. Storing of roster items and deserialization of them works.

ifacetk/iroster.tcl: export_to_xml_file and import_from_xml_file are now
 called by the corresponding "Roster" menu entries.


Modified: branches/xml-import-export/ifacetk/iroster.tcl
===================================================================
--- branches/xml-import-export/ifacetk/iroster.tcl	2008-01-27 23:43:12 UTC (rev 1352)
+++ branches/xml-import-export/ifacetk/iroster.tcl	2008-01-28 02:27:04 UTC (rev 1353)
@@ -1933,8 +1933,8 @@
     foreach c [jlib::connections] {
 	set jid [jlib::connection_jid $c]
 	set label [format [::msgcat::mc "Roster of %s"] $jid]
-	set ecommand [list roster::export_to_file $c]
-	set icommand [list roster::import_from_file $c]
+	set ecommand [list roster::export_to_xml_file $c]
+	set icommand [list roster::import_from_xml_file $c]
 	$emenu add command -label $label -command $ecommand
 	$imenu add command -label $label -command $icommand
     }

Modified: branches/xml-import-export/roster.tcl
===================================================================
--- branches/xml-import-export/roster.tcl	2008-01-27 23:43:12 UTC (rev 1352)
+++ branches/xml-import-export/roster.tcl	2008-01-28 02:27:04 UTC (rev 1353)
@@ -708,4 +708,153 @@
 
 ###############################################################################
 
+proc roster::export_to_xml_file {connid} {
+    set filename [tk_getSaveFile \
+		      -initialdir $::configdir \
+		      -initialfile [jlib::connection_user $connid]-roster.xml \
+		      -filetypes [list \
+				      [list [::msgcat::mc "Roster Files"] \
+					   .xml] \
+				      [list [::msgcat::mc "All Files"] *]]]
+    if {$filename == ""} return
+
+    set fd [open $filename w]
+    fconfigure $fd -encoding utf-8
+
+    puts $fd [serialize_to_xml $connid]
+
+    close $fd
+}
+
+proc roster::serialize_to_xml {connid} {
+    variable roster
+
+    set items [list]
+    foreach jid $roster(jids,$connid) {
+	lappend items [item_to_xml $connid $jid]
+    }
+
+    jlib::wrapper:createxml [jlib::wrapper:createtag contactlist \
+	-vars {xmlns http://tkabber.jabber.ru/contactlist} \
+	-subtags [list \
+	    [jlib::wrapper:createtag roster \
+		-vars {xmlns jabber:iq:roster} \
+		-subtags $items]]]
+}
+
+proc roster::import_from_xml_file {connid} {
+    set filename [tk_getOpenFile \
+		      -initialdir $::configdir \
+		      -initialfile [jlib::connection_user $connid]-roster.xml \
+		      -filetypes [list \
+				      [list [::msgcat::mc "Roster Files"] \
+					   .xml] \
+				      [list [::msgcat::mc "All Files"] *]]]
+    if {$filename == ""} return
+
+    set fd [open $filename r]
+    fconfigure $fd -encoding utf-8
+    set xml [read $fd]
+    close $fd
+
+    lassign [deserialize_from_xml $xml] roster bookmarks
+    puts "roster $roster\nbookmarks: $bookmarks"
+
+    if 0 {
+    if {$items != {}} {
+	jlib::send_iq set \
+	    [jlib::wrapper:createtag query \
+		 -vars [list xmlns "jabber:iq:roster"] \
+		 -subtags $items] \
+	    -connection $connid
+    }
+    }
+}
+
+proc roster::deserialize_from_xml {data} {
+    set parser [jlib::wrapper:new "#" "#" \
+	[namespace current]::xml_parse]
+    jlib::wrapper:elementstart $parser stream:stream {} {}
+    puts [info level]
+    set items [jlib::wrapper:parser $parser parse $data]
+    jlib::wrapper:parser $parser configure -final 0
+    jlib::wrapper:free $parser
+    return $items
+}
+
+proc roster::xml_parse {resultVar data} {
+    puts [info level]
+
+    jlib::wrapper:splitxml $data tag vars isempty chdata children
+
+    if {![string equal $tag contactlist]} {
+	return -code error "Bad root element \"$tag\":\
+	    must be contactlist"
+    }
+
+    set roster    {}
+    set bookmarks {}
+
+    foreach child $children {
+	jlib::wrapper:splitxml $child ctag cvars cisempty cchdata cchildren
+	switch -- $ctag {
+	    roster {
+		if {$roster != ""} {
+		    return -code error "At most one roster section is allowed"
+		}
+		set roster $cchildren
+	    }
+	    bookmarks {
+		if {$bookmarks != ""} {
+		    return -code error "At most one bookmarks section is allowed"
+		}
+		set bookmarks $cchildren
+	    }
+	    default {
+		return -code error "Bad element: \"$ctag\":\
+		    must be one of roster or bookmarks"
+	    }
+	}
+    }
+
+    list $roster $bookmarks
+}
+
+# TODO drop it
+proc roster::xml_parse_roster_item {data} {
+    jlib::wrapper:splitxml $data tag vars isempty chdata children
+
+    set out [list]
+
+    foreach child $children {
+	jlib::wrapper:splitxml $child ctag cvars cisempty cchdata cchildren
+
+	set vars [list jid [jlib::wrapper:getattr $cvars jid]]
+
+	foreach item {name subscription ask} {
+	    set val [jlib::wrapper:getattr $cvars $item]
+	    if {$val != ""} {
+		lappend vars $item $val]
+	    }
+	}
+
+	set grtags [list]
+	foreach subchild $cchildren {
+	    jlib::wrapper:splitxml $subchild subtag tmp tmp subchdata tmp
+
+	    switch -- $subtag {
+		group {
+		    lappend grtags [jlib::wrapper:createtag group -chdata $subchdata]
+		}
+	    }
+	}
+
+	lappend out [jlib::wrapper:createtag item -vars $vars -subtags $grtags]
+    }
+
+    set out
+}
+
+###############################################################################
+
 # vim:ts=8:sw=4:sts=4:noet



More information about the Tkabber-dev mailing list