# $Id: aclosetab.tcl 507 2012-01-03 16:15:48Z rejjin $
# by rejjin <xmpp: rejjin@jabber.dk>

namespace eval aclosetab {

    ::msgcat::mcload [file join [file dirname [info script]] msgs]

    if { ! [::plugins::is_registered aclosetab] } {
	::plugins::register aclosetab \
        -namespace [namespace current] \
        -source [info script] \
        -description [::msgcat::mc "Whether the \"Auto close tab\" plugin is loaded."] \
        -loadcommand [namespace code load] \
        -unloadcommand [namespace code unload]
	return
    }
    
    variable options
    
    custom::defgroup Plugins [::msgcat::mc "Plugins options."] -group Tkabber

    custom::defgroup {Auto close tab} \
	[::msgcat::mc "Auto close tab chat plugin options."] \
	-group Plugins \
	-group Chat

    custom::defvar options(close_after) 300 \
	[::msgcat::mc "Time (in seconds) before auto closing chat."] \
	-group {Auto close tab} \
	-type integer
}

proc aclosetab::load { } {
    ::hook::add open_chat_post_hook [namespace current]::chat_win_open
    ::hook::add close_chat_post_hook [namespace current]::chat_win_close
    ::hook::add lost_focus_hook [namespace current]::chat_lost_focus
    ::hook::add got_focus_hook [namespace current]::chat_got_focus
    ::hook::add draw_message_hook [namespace current]::incoming_message
    
    foreach chatid [::chat::opened] {
        chat_win_open $chatid {}
    }
}

proc aclosetab::unload { } {
    variable actabs
    
    foreach aid [array names actabs aid,*] {
    after cancel $actabs($aid)
    }
    
    foreach chatid [::chat::opened] {
        set winid [::chat::winid $chatid]
        if { [winfo exist $winid.status.aclosetab] } {
            destroy $winid.status.aclosetab
        }
    }
    
    array unset actabs
   
    ::hook::remove open_chat_post_hook [namespace current]::chat_win_open
    ::hook::remove close_chat_post_hook [namespace current]::chat_win_close
    ::hook::remove lost_focus_hook [namespace current]::chat_lost_focus
    ::hook::remove got_focus_hook [namespace current]::chat_got_focus
    ::hook::remove draw_message_hook [namespace current]::incoming_message
}

proc aclosetab::chat_win_open { chatid type } {
    variable actabs
    variable options
    
    if { [::chat::is_chat $chatid] } {
        set actabs(state,$chatid) 1
        set winid [::chat::winid $chatid]
        checkbutton $winid.status.aclosetab \
            -text [::msgcat::mc "Automatically closes chats"] \
            -variable [namespace current]::actabs(state,$chatid)
        pack $winid.status.aclosetab -side right
    }
}

proc aclosetab::chat_win_close { chatid } {
    variable actabs
    
    after cancel $actabs(aid,$chatid)
    array unset actabs *,$chatid
}

proc aclosetab::chat_got_focus { winid } {
    variable actabs
    
    set chatid [::chat::winid_to_chatid $winid]
    
    if { [::chat::is_chat $chatid] && \
            [info exist actabs(aid,$chatid)] } {
        after cancel $actabs(aid,$chatid)
    }
}

proc aclosetab::chat_lost_focus { winid } {
    variable actabs
    variable options
    
    set chatid [::chat::winid_to_chatid $winid]
    
    if { [::chat::is_chat $chatid] } {
        set actabs(aid,$chatid) [after \
                [expr {$options(close_after) * 1000}] \
                [list [namespace current]::close $chatid]]
    }
}

proc aclosetab::close { chatid } {
    variable actabs
    variable options 
    
    if { [info exist actabs(state,$chatid)] \
            && $actabs(state,$chatid) } {
        ::chat::close $chatid 
    }
}

proc aclosetab::incoming_message {chatid from type body x} {
    variable actabs
    
    if { ! [::xmpp::delay::exists $x] } {
        if { [info exist actabs(aid,$chatid)] } {
            after cancel $actabs(aid,$chatid)
        }
    }
}