# The plugin creates a window 
# where you can easily change 
# the theme of the conference.
#
# Renji 
# xmpp: rejjin@jabber.dk
# e-mail: webrenji@gmail.com
#

namespace eval toped { 

	::msgcat::mcload [file join [file dirname [info script]] msgs]
	
	hook::add chat_create_conference_menu_hook \
		[namespace current]::add_menu_item 1
}

proc toped::add_menu_item { m xlib jid } {
	
    set chatid [chat::chatid $xlib [::xmpp::jid::stripResource $jid]]
	
	$m add command -label [::msgcat::mc "Change subject"] \
		-command [list [namespace current]::change_subject $chatid]
	$m add separator
}

proc toped::change_subject { chatid } {
	set node [::xmpp::jid::node [chat::get_jid $chatid]]
	
	if { [winfo exist [set w ._subject_[ifacetk::nbpage $node]]] } {
		return
	}

	add_win $w -title [::msgcat::mc "Change subject"] \
	    -tabtitle [::msgcat::mc "%s subject" $node] \
	    -raisecmd [list focus $w]
		
	set scw [ScrolledWindow $w.sw]
	set text [text $w.subject -wrap word]
		
	$scw setwidget $text
		
	set btn [button $w.apply \
		-text [::msgcat::mc "Save subject"] \
		-command [list [namespace current]::send_new_subject $chatid $text $w]]
		
	set sw [chat::winid $chatid].status.subject
	$text insert end [$sw get]
	
	pack $scw -expand 1 -fill both
	pack $btn -anchor se -pady 4 -padx 4
}

proc toped::send_new_subject { chatid text w } {
    set chatwin [chat::winid $chatid]
    set xlib [chat::get_xlib $chatid]
    set jid [chat::get_jid $chatid]
	set subject [$text get 1.0 "end - 1 char"]

    message::send_msg $xlib $jid -type groupchat -subject $subject
	destroy_win $w
}