[Tkabber-dev] r1356 - in branches/xml-import-export: . plugins/roster

tkabber-svn at jabber.ru tkabber-svn at jabber.ru
Tue Jan 29 04:40:06 MSK 2008


Author: kostix
Date: 2008-01-29 04:40:06 +0300 (Tue, 29 Jan 2008)
New Revision: 1356

Modified:
   branches/xml-import-export/plugins/roster/backup.tcl
   branches/xml-import-export/roster.tcl
Log:
roster.tcl: Fixed an old-standing bug regarding separate handling
 of roster items' categories and subtypes.

plugins/roster/backup.tcl: Implemented import of roster contacts.
 Misc fixes, code cleanups.


Modified: branches/xml-import-export/plugins/roster/backup.tcl
===================================================================
--- branches/xml-import-export/plugins/roster/backup.tcl	2008-01-28 19:23:49 UTC (rev 1355)
+++ branches/xml-import-export/plugins/roster/backup.tcl	2008-01-29 01:40:06 UTC (rev 1356)
@@ -1,12 +1,16 @@
 # $Id$
 # Export/import of the roster items using an XML file.
 # This code provides basic framework for handling roster backup
-# files and it's able to serialize/deserialize regular roster
-# contacts. Hooks provided to make possible storage of other
-# kinds of data in roster backup files.
+# files and it's able to serialize/deserialize regular roster contacts.
+# Hooks provided to facilitate implementations of storing/restoring
+# other kinds of data logically pertaining to the roster
+# such as conference bookmarks, annotations, etc.
 
 namespace eval rosterbackup {
-    variable rootns http://tkabber.jabber.ru/contactlist
+    variable NS; array set NS {
+	root   http://tkabber.jabber.ru/contactlist
+	roster jabber:iq:roster
+    }
 
     hook::add connected_hook \
 	[namespace current]::setup_import_export_menus
@@ -78,13 +82,13 @@
 ###############################################################################
 
 proc rosterbackup::serialize_roster {connid} {
-    variable rootns
+    variable NS
 
     set subtags [list]
     hook::run serialize_roster_hook $connid #[info level] subtags
 
     jlib::wrapper:createxml [jlib::wrapper:createtag contactlist \
-	    -vars [list xmlns $rootns] -subtags $subtags] \
+	    -vars [list xmlns $NS(root)] -subtags $subtags] \
 	-prettyprint 1
 }
 
@@ -93,14 +97,14 @@
 proc rosterbackup::serialize_roster_contacts {connid level varName} {
     upvar $level $varName subtags
 
-    variable ::roster::roster
-    # TODO change to something more reasonable:
-    variable ::plugins::conferences::bookmarks
-
     set items [list]
-    foreach jid $roster(jids,$connid) {
-	if {![info exists bookmarks($connid,jid,$jid)]} {
-	    lappend items [::roster::item_to_xml $connid $jid]
+    foreach jid [::roster::get_jids $connid] {
+	set category [::roster::itemconfig $connid $jid -category]
+	switch -- $category {
+	    user -
+	    gateway {
+		lappend items [::roster::item_to_xml $connid $jid]
+	    }
 	}
     }
 
@@ -123,20 +127,11 @@
 
     set fd [open $filename r]
     fconfigure $fd -encoding utf-8
+    #set xml [string map {\n "" \t ""} [read $fd]]
     set xml [read $fd]
     close $fd
 
     deserialize_roster $connid $xml
-
-    if 0 {
-    if {$items != {}} {
-	jlib::send_iq set \
-	    [jlib::wrapper:createtag query \
-		 -vars [list xmlns "jabber:iq:roster"] \
-		 -subtags $items] \
-	    -connection $connid
-    }
-    }
 }
 
 ###############################################################################
@@ -153,59 +148,73 @@
 ###############################################################################
 
 proc rosterbackup::parse_roster_xml {connid data} {
-    variable rootns
+    variable NS
 
-    jlib::wrapper:splitxml $data tag vars isempty chdata children
+    jlib::wrapper:splitxml $data tag vars isempty cdata children
 
     if {![string equal $tag contactlist]} {
 	return -code error "Bad root element \"$tag\":\
 	    must be contactlist"
     }
-    # TODO fix it:
-    if 0 {
-    set ix [lsearch -exact $vars xmlns]
-    if {![string equal [lindex $vars $ix] $rootns]} {
-	return -code error "Bad file format:\
-	    root element doesn't posess a proper \"xmlns\" attribute"
+    set xmlns [jlib::wrapper:getattr $vars xmlns]
+    if {![string equal $xmlns $NS(root)]} {
+	return -code error "Bad root element namespace \"$xmlns\":\
+	    must be \"$NS(root)\""
     }
-    }
 
-    array set seen {}
     foreach child $children {
-	jlib::wrapper:splitxml $child ctag cvars cisempty cchdata cchildren
-	if {[info exists seen($ctag)]} {
-	    return -code error "At most one \"$ctag\" section is allowed"
-	}
-	hook::run roster_deserialize_hook $connid \
-		$ctag $cvars $cisempty $cchdata $cchildren
-	set seen($ctag) {}
+	hook::run deserialize_roster_hook $connid $child
     }
 }
 
 ###############################################################################
 
-proc rosterbackup::deserialize_roster_contacts {connid
-	tag vars isempty cdata children} {
+proc rosterbackup::deserialize_roster_contacts {connid data} {
+    variable NS
+
+    jlib::wrapper:splitxml $data tag vars isempty cdata children
+
     if {![string equal $tag roster]} return
-    # TODO check xmlns for presence and validity
+    set xmlns [jlib::wrapper:getattr $vars xmlns]
+    if {![string equal $xmlns $NS(roster)]} {
+	return -code error "Bad roster element namespace \"$xmlns\":\
+	    must be \"$NS(roster)\""
+    }
 
-    variable ::roster::roster
+    array set jids {}
+    foreach jid [::roster::get_jids $connid] {
+	set jids($jid) {}
+    }
 
     foreach child $children {
-	puts "another roster contact..."
+	set jid [get_item_jid $child]
+	if {![info exists jids($jid)]} {
+	    jlib::send_iq set \
+		[jlib::wrapper:createtag query \
+		     -vars [list xmlns $NS(roster)] \
+		     -subtags [list $child]] \
+		-connection $connid
+	}
     }
 }
 
 ###############################################################################
 
+proc rosterbackup::get_item_jid {data} {
+    jlib::wrapper:splitxml $data ? vars ? ? ?
+    jlib::wrapper:getattr $vars jid
+}
+
+###############################################################################
+
 # TODO drop it
 proc rosterbackup::xml_parse_roster_item {data} {
-    jlib::wrapper:splitxml $data tag vars isempty chdata children
+    jlib::wrapper:splitxml $data tag vars isempty cdata children
 
     set out [list]
 
     foreach child $children {
-	jlib::wrapper:splitxml $child ctag cvars cisempty cchdata cchildren
+	jlib::wrapper:splitxml $child ctag cvars cisempty ccdata cchildren
 
 	set vars [list jid [jlib::wrapper:getattr $cvars jid]]
 
@@ -218,7 +227,7 @@
 
 	set grtags [list]
 	foreach subchild $cchildren {
-	    jlib::wrapper:splitxml $subchild subtag tmp tmp subchdata tmp
+	    jlib::wrapper:splitxml $subchild subtag tmp tmp subcdata tmp
 
 	    switch -- $subtag {
 		group {

Modified: branches/xml-import-export/roster.tcl
===================================================================
--- branches/xml-import-export/roster.tcl	2008-01-28 19:23:49 UTC (rev 1355)
+++ branches/xml-import-export/roster.tcl	2008-01-29 01:40:06 UTC (rev 1356)
@@ -194,12 +194,20 @@
 	    -name     {set param name}
 	    -subsc    {set param subsc}
 	    -ask      {set param ask}
-	    -category {set param category}
-	    -subtype  {set param subtype}
+	    -category {
+		return [lindex [get_category_and_subtype $connid $jid] 0]
+	    }
+	    -subtype  {
+		return [lindex [get_category_and_subtype $connid $jid] 1]
+	    }
 	    -isuser   {
 		return [cequal [lindex [get_category_and_subtype $connid $jid] 0] "user"]
 	    }
-	    default   {return -code error "Illegal option"}
+	    default   {
+		return -code error "Bad option \"$attr\":\
+		    must be one of: -group, -name, -subsc, -ask,\
+		    -category, -subtype or -isuser"
+	    }
 	}
 	if {[info exists roster($param,$connid,$jid)]} {
 	    return $roster($param,$connid,$jid)
@@ -213,8 +221,14 @@
 		-name     {set param name}
 		-subsc    {set param subsc}
 		-ask      {set param ask}
-		-category {set param category}
-		-subtype  {set param subtype}
+		-category {
+		    override_category $connid $jid $val
+		    continue
+		}
+		-subtype  {
+		    override_subtype $connid $jid $val
+		    continue
+		}
 		default   {return -code error "Illegal option"}
 	    }
 	    set roster($param,$connid,$jid) $val
@@ -292,6 +306,23 @@
 	[list $category $subtype]
 }
 
+proc roster::override_category {connid jid category} {
+    variable roster
+
+    set roster(overridden_category_and_subtype,$connid,$jid) \
+	[list $category \
+	    [lindex $roster(overridden_category_and_subtype,$connid,$jid) 1]]
+}
+
+proc roster::override_subtype {connid jid subtype} {
+    variable roster
+
+    set roster(overridden_category_and_subtype,$connid,$jid) \
+	[list \
+	    [lindex $roster(overridden_category_and_subtype,$connid,$jid) 0] \
+	    $subtype]
+}
+
 proc roster::get_category_and_subtype {connid jid} {
     variable roster
 
@@ -401,7 +432,6 @@
     array unset roster name,*
     array unset roster subsc,*
     array unset roster ask,*
-    array unset roster category,*
     array unset roster subtype,*
     array unset roster cached_category_and_subtype,*
     array unset roster overridden_category_and_subtype,*
@@ -416,7 +446,6 @@
     array unset roster name,$connid,*
     array unset roster subsc,$connid,*
     array unset roster ask,$connid,*
-    array unset roster category,$connid,*
     array unset roster subtype,$connid,*
     array unset roster cached_category_and_subtype,$connid,*
     array unset roster overridden_category_and_subtype,$connid,*



More information about the Tkabber-dev mailing list