File: Carbon.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (251 lines) | stat: -rw-r--r-- 7,045 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Carbon.tcl --
# 
#       Interface for the carbon package.
#
#  Copyright (c) 2007 Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#       
# $Id: Carbon.tcl,v 1.17 2008-06-09 09:50:59 matben Exp $
# 
# @@@ Move the sleep stuff to something more generic.

namespace eval ::Carbon { 

    if {[tk windowingsystem] ne "aqua"} {
	return
    }
    if {[catch {package require carbon}]} {
	return
    }
    component::define Carbon  \
      "Provides Mac OS X specific support such as various dock features."

    # Keep track of number of messages we receive while in the background.
    variable nHiddenMsgs 0
}

proc ::Carbon::Init {} {
    
    component::register Carbon
    
    ::carbon::sleep add ::Carbon::Sleep

    # Add event hooks.
    ::hooks::register newMessageHook      [namespace code NewMsgHook]
    ::hooks::register newChatMessageHook  [namespace code NewMsgHook]
    ::hooks::register newChatThreadHook   [namespace code NotifyHook]
    ::hooks::register newMessageBox       [namespace code NotifyHook]
    ::hooks::register appInFrontHook      [namespace code AppInFrontHook]
    ::hooks::register quitAppHook         [namespace code QuitHook]
    ::hooks::register fileTransferReceiveHook  [namespace code NotifyHook]
    ::hooks::register loginHook           [namespace code LoginHook]

    # Define all hooks for preference settings.
    ::hooks::register prefsInitHook          [namespace code InitPrefsHook]
    ::hooks::register prefsBuildHook         [namespace code BuildPrefsHook]
    ::hooks::register prefsSaveHook          [namespace code SavePrefsHook]
    ::hooks::register prefsCancelHook        [namespace code CancelPrefsHook]
    ::hooks::register prefsUserDefaultsHook  [namespace code UserDefaultsHook]
    
    variable wasSleepLoggedOut 0
}

proc ::Carbon::LoginHook {} {
    variable wasSleepLoggedOut
    set wasSleepLoggedOut 0
}

proc ::Carbon::NewMsgHook {xmldata args} {
    variable nHiddenMsgs
    
    set body [wrapper::getcdata [wrapper::getfirstchildwithtag $xmldata body]]
    if {($body ne {}) && ![::UI::IsAppInFront]} {
	incr nHiddenMsgs
	::carbon::dock overlay -text $nHiddenMsgs
	Bounce
    }
}

proc ::Carbon::NotifyHook {args} {
    
    # Notify only if in background.
    if {![::UI::IsAppInFront]} {
	Bounce
    }
}

proc ::Carbon::Bounce {} {
    after idle { ::carbon::dock bounce 1 }
}

proc ::Carbon::AppInFrontHook {} {
    variable nHiddenMsgs
    
    set nHiddenMsgs 0
    ::carbon::dock overlay -text ""
}

proc ::Carbon::QuitHook {} {
    ::carbon::dock overlay -text ""
}

proc ::Carbon::Sleep {type} {
    global jprefs
    variable wasSleepLoggedOut
    
    switch -- $type {
	sleep - willsleep {
	    if {[::Jabber::IsConnected]} {
		set wasSleepLoggedOut 1
		#::Jabber::DoCloseClientConnection
		::Jabber::SetStatus unavailable -status $jprefs(sleeploutmsg)
	    }
	}
	wakeup {
	    if {$wasSleepLoggedOut && $jprefs(sleeplogin)} {
		if {![::Jabber::IsConnected]} {
		    
		    # We must give the system (WiFi) some time to setup the
		    # network before loggin in. Some kind of trying multiple
		    # times would be better...
		    after 8000 ::Login::LoginCmd
		}
	    }
	}
    }
}

proc ::Carbon::InitPrefsHook {} {
    global jprefs

    set jprefs(sleeplogout)  0
    set jprefs(sleeplogin)   0
    set jprefs(sleeploutmsg) ""
    
    ::PrefUtils::Add [list  \
      [list jprefs(sleeplogout)  jprefs_sleeplogout   $jprefs(sleeplogout)]  \
      [list jprefs(sleeplogin)   jprefs_sleeplogin    $jprefs(sleeplogin)]  \
      [list jprefs(sleeploutmsg) jprefs_sleeploutmsg  $jprefs(sleeploutmsg)]  \
      ]
    
    variable allKeys {sleeplogout  sleeplogin  sleeploutmsg}
}

proc ::Carbon::BuildPrefsHook {wtree nbframe} {
    
    # TRANSLATORS: automatic logout when computer is put in sleep mode and relogin on wakeup; in preferences dialog (Mac OS X only!)
    ::Preferences::NewTableItem {Jabber Sleep} [mc "Sleep"]
    
    set wpage [$nbframe page Sleep]    
    BuildPage $wpage
    bind <Destroy> $nbframe +::Carbon::DestroyPrefsHook
}

proc ::Carbon::BuildPage {page} {
    global jprefs
    variable tmp
    variable allKeys
    
    foreach key $allKeys {
	set tmp($key) $jprefs($key)
    }

    set wc $page.c
    ttk::frame $wc -padding [option get . notebookPageSmallPadding {}]
    pack $wc -side top -anchor [option get . dialogAnchor {}]
    
    ttk::frame $wc.head -padding {0 0 0 6}
    ttk::label $wc.head.l -text [mc "Sleep"]
    ttk::separator $wc.head.s -orient horizontal

    grid  $wc.head.l  $wc.head.s
    grid $wc.head.s -sticky ew
    grid columnconfigure $wc.head 1 -weight 1
    pack  $wc.head  -side top -fill x

    set ws $wc.sleep
    ttk::frame $ws

    set varName [namespace current]::tmp(sleeplogout)
    ttk::checkbutton $ws.clout -text [mc "Logout on sleep"]: -variable $varName \
      -command [namespace code [list SetEntryState [list $ws.emsg $ws.clin] $varName]]
    ttk::label $ws.lmsg -text [mc "Message"]:
    ttk::entry $ws.emsg -font CociSmallFont -width 32  \
      -textvariable [namespace current]::tmp(sleeploutmsg)
    ttk::checkbutton $ws.clin -text [mc "Relogin on wakeup"] \
      -variable [namespace current]::tmp(sleeplogin)

    SetEntryState [list $ws.emsg $ws.clin] $varName

    grid  $ws.clout  -         $ws.emsg
    grid  x          $ws.clin  -
    grid $ws.clout -sticky w
    grid $ws.clin -sticky w
    grid $ws.emsg -sticky ew    
    
    grid columnconfigure $ws 0 -minsize 32
    grid columnconfigure $ws 1 -weight 1
    grid columnconfigure $ws 2 -weight 1

    pack  $ws  -side top -fill x
}

proc ::Carbon::SetEntryState {winL varName} {
    upvar #0 $varName var
    if {$var} {
	foreach w $winL {
	    $w state {!disabled}
	}
    } else {
	foreach w $winL {
	    $w state {disabled}
	}
    }
}

proc ::Carbon::SavePrefsHook {} {
    global jprefs
    variable tmp	
    array set jprefs [array get tmp]
}

proc ::Carbon::CancelPrefsHook {} {
    global jprefs
    variable tmp
	
    foreach key [array names tmp] {
	if {![string equal $jprefs($key) $tmp($key)]} {
	    ::Preferences::HasChanged
	    return
	}
    }    
}

proc ::Carbon::UserDefaultsHook {} {
    global jprefs
    variable tmp
	
    foreach key [array names tmp] {
	set tmp($key) $jprefs($key)
    }
}

proc ::Carbon::DestroyPrefsHook {} {
    variable tmp
    
    unset -nocomplain tmp
}

#-------------------------------------------------------------------------------