# $Id$

namespace eval search {
    set winid 0
}


proc search::open {jid} {
    variable winid

    set sw .search$winid
    toplevel $sw
    wm title $sw "Search in $jid"

    frame $sw.buttons
    button $sw.buttons.cancel -text Cancel -command [list destroy $sw]
    button $sw.buttons.ok -text Ok -command [list search::search $sw $jid]
    pack $sw.buttons.cancel $sw.buttons.ok -side right
    pack $sw.buttons -fill x -side bottom

    frame $sw.fields
    pack $sw.fields -expand yes -fill both

    jlib::send_iq get \
	[jlib::wrapper:createtag item \
	     -vars {xmlns jabber:iq:search}] \
	-to $jid -command [list search::recv_fields $sw $jid]
    
    incr winid
}


proc search::recv_fields {sw jid res child} {
    debugmsg search "$res $child"

    if {![cequal $res OK]} {
	return
    }

    jlib::wrapper:splitxml $child tag vars isempty chdata children

    if {[cequal [jlib::wrapper:getattr $vars xmlns] jabber:iq:search]} {
	data::fill_fields $sw.fields $children
    }


}


proc search::search {sw jid} {
    variable data

    set f $sw.fields
    set restags [data::get_tags $sw.fields]

    jlib::send_iq set [jlib::wrapper:createtag query \
			   -vars {xmlns jabber:iq:search} \
			   -subtags $restags] \
	-to $jid -command [list search::recv_items $sw $jid]
}


proc search::recv_items {sw jid res child} {
    variable data

    debugmsg search "$res $child"

    if {![cequal $res OK]} {
	return
    }

    #frame $sw.items
    #pack $sw.items -expand yes -fill both -after $sw.fields -anchor nw
    #pack forget $sw.fields

    set sww [ScrolledWindow $sw.items]
    set sf [ScrollableFrame $sww.f]
    $sww setwidget $sf
    pack $sww -expand yes -fill both -after $sw.fields -anchor nw
    pack forget $sw.fields

    set reported_fields [data::get_reported_fields $sw.fields]

    jlib::wrapper:splitxml $child tag vars isempty chdata children

    if {[cequal [jlib::wrapper:getattr $vars xmlns] jabber:iq:search]} {
	fill_items [$sf getframe] $jid $reported_fields $children
    }


}


proc search::fill_items {g jid reported_fields items} {
    variable data
    global font

    set row 0
    set col 0

    foreach field [concat jid $reported_fields] {
	set fieldcol($field) $col

	label $g.l$row$col -text $field
	grid $g.l$row$col -row $row -column $col -sticky w

	grid columnconfig $g $col -weight 1 -minsize 0

	incr col
    }

    incr row

    foreach item $items {
	jlib::wrapper:splitxml $item tag vars isempty chdata children

	switch -- $tag {
	    item {
		set itemjid [jlib::wrapper:getattr $vars jid]
		Button $g.l${row}0 -text $itemjid -font $font \
		    -helptext "Add user" \
		    -command [list message::send_subscribe_dlg $itemjid]
		grid $g.l${row}0 -row $row -column 0 -sticky w

		foreach field $children {
		    jlib::wrapper:splitxml $field tag vars isempty \
			chdata children1

		    if {[info exists fieldcol($tag)]} {
			set col $fieldcol($tag)
			label $g.l$row$col -text $chdata -font $font
			grid $g.l$row$col -row $row -column $col -sticky w
		    }
		    debugmsg search "$tag $chdata"
		}
		incr row
	    }
	    default {}
	}	
    }
}


