1# $Id$
2
3if {![cequal $::interface tk]} return
4
5if {![info exists ::wmaker_dock] || !$::wmaker_dock} {
6    return
7}
8
9namespace eval ::wmdock {
10    set save_status unavailable
11    set balloon_msg ""
12    set msgs 0
13    array set msgsc {}
14    set msg_afterid ""
15}
16
17proc ::wmdock::change_status {status} {
18    variable save_status
19    variable msgs
20    variable balloon_msg
21
22    if {![winfo exists .icon]} return
23
24    set save_status $status
25    set balloon_msg $status
26    .icon.c itemconfigure text -text [concat $msgs "msgs"]
27    .icon.c itemconfigure icon -image roster/user/$status
28}
29
30
31proc ::wmdock::msg_recv {chatid from type body x} {
32    variable msg_afterid
33    variable balloon_msg
34    variable msgs
35    variable msgsc
36    variable icon
37
38    if {![winfo exists .icon]} return
39
40    if {[chat::is_our_jid $chatid $from] || ![cequal $type chat]} {
41	return
42    }
43
44    foreach xelem $x {
45	::xmpp::xml:split $xelem tag xmlns attrs cdata subels
46
47	# Don't count message if this 'empty' tag is present. It indicates
48	# messages history in chat window.
49	if {[string equal $tag ""] && [string equal $xmlns tkabber:x:nolog]} {
50	    return
51	}
52    }
53
54    set cw [chat::winid $chatid]
55    set page [crange [win_id tab $cw] 1 end]
56    if {$::usetabbar && $page != [.nb raise]} {
57	if {![info exists msgsc($chatid)]} {
58	    set msgsc($chatid) 0
59	}
60	incr msgsc($chatid) 1
61	incr msgs 1
62    }
63
64    #	set balloon_msg [concat "Message from" [roster::get_label $from] ]
65    set balloon_msg [::msgcat::mc "Message from %s" $from]
66
67    after cancel $msg_afterid
68    .icon.c itemconfigure icon -image docking/message
69    .icon.c itemconfigure text -text [::msgcat::mc "%s msgs" $msgs]
70
71    set msg_afterid [after 5000 ::wmdock::clear_msg_status]
72}
73
74proc ::wmdock::msg_read {path chatid} {
75    variable msgs
76    variable msgsc
77
78    if {![winfo exists .icon]} return
79
80    if {[info exists msgsc($chatid)]} {
81	set msgs [expr $msgs - $msgsc($chatid)]
82	unset msgsc($chatid)
83    }
84    .icon.c itemconfigure text -text [::msgcat::mc "%s msgs" $msgs]
85}
86
87proc ::wmdock::presence_recv {who status} {
88    variable msg_afterid
89    variable balloon_msg
90    variable icon
91
92    if {![winfo exists .icon]} return
93
94    set balloon_msg [::msgcat::mc "%s is %s" $who $status]
95
96    after cancel $msg_afterid
97    .icon.c itemconfigure icon -image browser/user
98
99    set msg_afterid [after 10000 ::wmdock::clear_msg_status]
100}
101
102proc ::wmdock::clear_msg_status {} {
103    variable save_status
104    variable balloon_msg
105
106    if {![winfo exists .icon]} return
107
108    set balloon_msg $save_status
109    .icon.c itemconfigure icon -image roster/user/$save_status
110}
111
112
113proc ::wmdock::showhide {} {
114    if {[wm state .] == "withdrawn"} {
115	wm deiconify .
116	wm state . normal
117    } else {
118	wm withdraw .
119    }
120}
121
122proc ::wmdock::balloon {} {
123    variable balloon_msg
124
125    return [list .icon $balloon_msg]
126}
127
128proc ::wmdock::create_dock {} {
129    variable balloon_msg
130
131    if {[cequal [wm iconwindow .] ""]} {
132	toplevel .icon -class TkabberIcon
133	wm iconwindow . .icon
134    }
135
136    wm command . [file join [pwd] $::argv0]
137
138    canvas .icon.c -background black -width 52 -height 52 -relief sunken
139    .icon.c create image 26 26 -anchor s \
140	-image roster/user/unavailable -tag icon
141    .icon.c create text 26 52 -anchor s -text "no" -fill white -tag text
142    pack .icon.c
143
144    bind .icon <<ContextMenu>> ::wmdock::showhide
145    balloon::setup .icon -command [list ::wmdock::balloon]
146}
147
148hook::add postload_hook ::wmdock::create_dock 80
149hook::add change_our_presence_post_hook ::wmdock::change_status 15
150hook::add draw_message_hook ::wmdock::msg_recv 70
151hook::add on_change_user_presence_hook ::wmdock::presence_recv 15
152hook::add raise_chat_tab_hook ::wmdock::msg_read 15
153