-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccounts.go
242 lines (195 loc) · 5.87 KB
/
accounts.go
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
package main
import (
"reflect"
"regexp"
"strings"
"github.com/andlabs/ui"
// "github.com/agl/xmpp"
)
type DisplayAccount struct {
Enabled bool
Name string
}
func listAccounts() {
// Import the configuration
config := importConfig();
// Assemble table of accounts
accounts := make([]DisplayAccount, len(config.Accounts))
// Pull accounts from config file
for i, account := range config.Accounts {
enabled := account.Enabled
acctName := strings.Join([]string{account.Name, account.Domain}, "@")
accounts[i] = DisplayAccount{enabled, acctName}
}
// Populate table
accountTable := ui.NewTable(reflect.TypeOf(accounts[0]))
accountTable.Lock()
e := accountTable.Data().(*[]DisplayAccount)
*e = accounts
accountTable.Unlock()
// Assemble button stack
addButton := ui.NewButton("Add")
modifyButton := ui.NewButton("Modify")
deleteButton := ui.NewButton("Delete")
// Set up channel for sending data between windows
done := make(chan Account)
// When "Delete" is clicked, remove selected account
deleteButton.OnClicked(func() {
sel := accountTable.Selected()
cfgAccts := config.Accounts
// Delete the account from the config file
config.Accounts = append(cfgAccts[:sel], cfgAccts[sel+1:]...)
config.Save()
// Delete the account from the table by building a new slice omitting it
accountTable.Lock()
tblAccts := accountTable.Data().(*[]DisplayAccount)
aa := *tblAccts
*tblAccts = append(aa[:sel], aa[sel+1:]...)
accountTable.Unlock()
})
// When "Modify" is clicked, present selected account for editing
modifyButton.OnClicked(func() {
sel := accountTable.Selected()
cfgAccts := config.Accounts
if sel < 0 {
return
}
// Retrieve selected account
acct := cfgAccts[sel]
// Open window to edit selected account
editAccount(acct, done)
go func() {
// Get edited account
editedAcct := <- done
// Update the account
config.Accounts[sel] = editedAcct
config.Save()
// Update the accounts list
accounts := make([]DisplayAccount, len(config.Accounts))
for i, account := range config.Accounts {
acctName := strings.Join([]string{account.Name, account.Domain}, "@")
accounts[i] = DisplayAccount{account.Enabled, acctName}
}
// Update accountTable
accountTable.Lock()
e := accountTable.Data().(*[]DisplayAccount)
*e = accounts
accountTable.Unlock()
}()
})
// When "Add" is clicked, open window to enter new account info
addButton.OnClicked(func() {
// Create an empty Account to pass to editAccount
var account Account;
// Open the edit window and wait for a result
editAccount(account, done)
// Update accounts list concurrently once we hear back from the edit window
go func() {
acct := <-done
// Add new account to config.Accounts
config.Accounts = append(config.Accounts, acct)
config.Save()
// Update the accounts list
accounts := make([]DisplayAccount, len(config.Accounts))
for i, account := range config.Accounts {
acctName := strings.Join([]string{account.Name, account.Domain}, "@")
accounts[i] = DisplayAccount{account.Enabled, acctName}
}
accountTable.Lock()
e := accountTable.Data().(*[]DisplayAccount)
*e = accounts
accountTable.Unlock()
}()
})
// Put the interface together
buttons := ui.NewHorizontalStack(
addButton,
modifyButton,
deleteButton)
accountsInterface := ui.NewVerticalStack(
accountTable,
buttons)
accountsInterface.SetStretchy(0)
// Build/display the window
acctWin := ui.NewWindow("Accounts", 400, 500, accountsInterface)
acctWin.OnClosing(func() bool {
ui.Stop()
return true
})
acctWin.Show()
}
func editAccount(account Account, done chan Account) {
// Initialize fields
enabled := ui.NewCheckbox("Enabled")
username := ui.NewTextField()
domain := ui.NewTextField()
password := ui.NewPasswordField()
// If this is an existing account, populate the fields from the config file
if len(account.Name) > 0 {
enabled.SetChecked(account.Enabled)
username.SetText(account.Name)
domain.SetText(account.Domain)
password.SetText(account.Password)
} else {
enabled.SetChecked(false)
username.SetText("e.g. 'esnowden'")
domain.SetText("e.g. 'ccc.de'")
password.SetText("password1234")
}
// Assemble input fields
textFields := ui.NewVerticalStack(
ui.NewStandaloneLabel("Enabled"),
enabled,
ui.NewStandaloneLabel("Username"),
username,
ui.NewStandaloneLabel("Domain"),
domain,
ui.NewStandaloneLabel("Password"),
password)
// Assemble button stack
saveButton := ui.NewButton("Save")
cancelButton := ui.NewButton("Cancel")
buttons := ui.NewHorizontalStack(
saveButton,
cancelButton)
editAccountInterface := ui.NewVerticalStack(
textFields,
buttons)
editAccountInterface.SetStretchy(0)
// Build and display window
editWin := ui.NewWindow("Edit Account", 400, 400, editAccountInterface)
editWin.OnClosing(func() bool {
return true
})
//Save a new account
saveButton.OnClicked(func() {
//Validate the data (TODO: use the fields' methods for this)
validUsername := regexp.MustCompile("[^\x00-\x20\x22\x26\x27\x2F\x3A\x3C\x3E\x40\x7F]+")
// validDomain := regexp.MustCompile("(\w{2,}\.\w{2,3}\.\w{2,3}|\w{2,}\.\w{2,3})$")
if strings.Trim(username.Text(), " ") == "" {
username.Invalid("You must enter a username")
} else if validUsername.MatchString(username.Text()) == false {
username.Invalid("Username contains invalid characters")
// } else if validDomain.MatchString(domain.Text()) == false {
// domain.Invalid("Domain contains invalid characters")
} else {
//Instantiate a new account and grow config.Accounts to add it
acct := Account{Enabled: enabled.Checked(), Name: username.Text(), Domain: domain.Text(), Password: password.Text()}
editWin.Hide()
done <- acct
}
})
cancelButton.OnClicked(func() {
editWin.Hide()
})
editWin.Show()
}
/*
func main() {
go ui.Do(listAccounts)
err := ui.Go()
if err != nil {
panic(err)
}
}
*/