/
GNPreferencesWindow.rb
183 lines (147 loc) · 3.95 KB
/
GNPreferencesWindow.rb
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
#
# GNPreferencesWindow.rb
# Gmail Notifr
#
# Created by james on 10/4/08.
# Copyright (c) 2008 ashchan.com. All rights reserved.
#
require 'osx/cocoa'
class GNPreferencesWindow < OSX::NSWindow
include OSX
TAG_USERNAME = 0
TAG_PASSWORD = 1
ib_outlet :applicationContrller
ib_outlet :username
ib_outlet :password
ib_outlet :removeButton
ib_outlet :hint
ib_outlet :interval
ib_outlet :autoLaunch
ib_outlet :growl
ib_outlet :soundList
ib_outlet :userList
ib_action :save
ib_action :soundSelect
ib_action :addUser
ib_action :removeUser
def awakeFromNib
@soundList.removeAllItems
GNPreferences.sounds.each { |s| @soundList.addItemWithTitle(s) }
load_defaults
self.setDelegate(self)
@userList.dataSource = self
@userList.delegate = self
@username.delegate = self
@password.delegate = self
reload_ui
end
def windowWillClose(notification)
#cancel changes
load_defaults
reload_ui
end
def save
self.orderOut(nil)
interval = @interval.integerValue
authChanged = false
resetTimer = false
if @preferences.interval != interval
@preferences.interval = interval
resetTimer = true
end
authChanged = @preferences.merge_accounts_change
@preferences.autoLaunch = @autoLaunch.state == NSOnState ? true : false
@preferences.growl = @growl.state == NSOnState ? true : false
@preferences.sound = @soundList.titleOfSelectedItem
@preferences.writeBack
reload_ui
@applicationContrller.setTimer if resetTimer
@applicationContrller.checkMail if authChanged
self.close
end
def load_defaults
@preferences = GNPreferences.alloc.init
end
def reload_ui
@interval.setTitleWithMnemonic(@preferences.interval.to_s)
@autoLaunch.setState(@preferences.autoLaunch ? NSOnState : NSOffState)
@growl.setState(@preferences.growl ? NSOnState : NSOffState)
@soundList.selectItemWithTitle(@preferences.sound)
@userList.reloadData
refresh_account_fields
show_username_and_password
end
def soundSelect(sender)
if sound = NSSound.soundNamed(@soundList.titleOfSelectedItem)
sound.play
end
end
def addUser(sender)
account = GNAccount.alloc.init
account.username = "username"
account.password = ""
@preferences.accounts.addObject(account)
@userList.reloadData
@userList.selectRow_byExtendingSelection(accounts.size - 1, false)
refresh_account_fields
@username.selectText(self)
end
def removeUser(sender)
selected_account = current_account
if selected_account
selected_account.destroy
@userList.reloadData
end
refresh_account_fields
show_username_and_password
end
def numberOfRowsInTableView(sender)
accounts.size
end
def tableView_objectValueForTableColumn_row(tableView, tableColumn, row)
account = accounts[row]
account ? account.username : ""
end
def tableView_setObjectValue_forTableColumn_row(tableView, object, tableColumn, row)
end
def tableViewSelectionDidChange(notification)
show_username_and_password
end
def controlTextDidChange(notification)
selected_account = current_account
if selected_account
field = notification.object
if field.tag == TAG_USERNAME
selected_account.username = @username.stringValue
@userList.reloadData
elsif field.tag == TAG_PASSWORD
selected_account.password = @password.stringValue
else
NSLog("username/password input not captured")
end
end
end
private
def accounts
@preferences.accounts.reject { |a| a.deleted? }
end
def current_account
accounts[@userList.selectedRow]
end
def show_username_and_password
selected_account = current_account
if selected_account
@username.setTitleWithMnemonic(selected_account.username)
@password.setTitleWithMnemonic(selected_account.password)
end
end
def refresh_account_fields
enabled = accounts.size > 0
@username.enabled = @password.enabled = @removeButton.enabled = enabled
@hint.hidden = enabled
unless enabled
@username.setTitleWithMnemonic("")
@password.setTitleWithMnemonic("")
end
end
end