# Tabimages plugin for Tkabber
# Written by Renji
# jabber id: rejjin@jabber.dk
# e-mail: webrenji@gmail.com

namespace eval tabimages {

	set space [namespace current]
	
	::msgcat::mcload [file join [file dirname [info script]] msgs]
	
	custom::defgroup Plugins [::msgcat::mc "Plugins options."] \
	-group Tkabber	
	
    custom::defgroup Tabimages [::msgcat::mc "Tabimages"] \
	-group Plugins	
	
	variable state
	custom::defvar state 1 \
	[::msgcat::mc "Enable or disable this plugin.\
		Values:\nChats - using for CHATS ONLY\nGroupchats -\
		using for GROUPCHATS ONLY\nEnable - using for\
		chats and groupchats\nDisable - disable this plugin"] \
	-type options -group Tabimages \
	-command [list ${space}::tab_images_init -reload 1] \
	-values [list \
		1 [::msgcat::mc "Enable"] \
		0 [::msgcat::mc "Disable"] \
		2 [::msgcat::mc "Chats"] \
		3 [::msgcat::mc "Groupchats"] \
	]
	
	variable font
	custom::defvar font $::default_Roster_font \
	[::msgcat::mc "Font to use in tabs bar window"] \
	-group Tabimages -type font \
	-command ${space}::switch_font
	
	variable compound
	custom::defvar compound {left} \
	[::msgcat::mc "Position of icon in tab"] \
	-type options -group Tabimages \
	-command [list ${space}::tab_images_init -reload 1] \
	-values [list \
		left [::msgcat::mc "Left"] \
		right [::msgcat::mc "Right"] \
		top [::msgcat::mc "Top"] \
		bottom [::msgcat::mc "Bottom"] \
	]
	
	hook::add finload_hook [list ${space}::tab_images_init -reload 0] 100	
	hook::add finload_hook ${space}::switch_font 100
	hook::add open_chat_post_hook ${space}::update_tab 90
	hook::add client_presence_hook ${space}::change_presence 60
	hook::add disconnected_hook [list ${space}::tab_images_init -reload 1] 100
	
	proc update_tab { chatid type } {
		variable state
	
		if !$::usetabbar return
	
		switch -- $state {
			0 return
			2 { if [chat::is_groupchat $chatid] return }
			3 { if [chat::is_chat $chatid] return }
		}
		
		set xlib [chat::get_xlib $chatid]
		set jid [chat::get_jid $chatid]
		
		set icon [get_icon $xlib $jid]
		set tab [get_tab_button $chatid]
		
		configure_tab_button $tab $icon
	}	
		
	proc change_presence { xlib jid type x args } {
		variable state
	
		if {!$::usetabbar} {
			return
		}
		
		set chatid [chat::chatid $xlib $jid]
		
		switch -- $state {
			0 return
			2 { 
				if [chat::is_groupchat $chatid] return 
			}
			3 { 
				if [chat::is_chat $chatid] return 
			}
		}
		
		if {![chat::is_opened $chatid]} {
			return
		}
		
		set icon [get_icon $xlib $jid]
		set tab [get_tab_button $chatid]
		
		configure_tab_button $tab $icon
	}
	
	proc get_icon {xlib jid} {
		return [::ifacetk::roster::get_jid_icon $xlib $jid]
	}
	
	proc get_tab_button chatid {
		return [ifacetk::nbpage [chat::winid $chatid]]
	}
	
	proc chat_type chatid {
		return $chat::chats(type,$chatid)
	}
	
	proc configure_tab_button {tab_path icon} {
		variable options
		variable compound
		
		ButtonBar::itemconfigure .nb $tab_path  \
			-image $icon \
			-compound $compound
	}
	
	proc tab_images_init { reload? reload args } {
		foreach chatid [chat::opened] {
			if $reload { 
				configure_tab_button \
					[get_tab_button $chatid] {}
			}
			update_tab $chatid [chat_type $chatid]
		}
	}
	
	proc switch_font args {
		variable font
		
		if {!$::usetabbar} {
			return
		}
		
		option add *ButtonBar*Button*Font $font
		foreach wbutton [ButtonBar::pages .nb] {
			ButtonBar::itemconfigure .nb $wbutton -font $font
		}
	}
}