forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.go
115 lines (91 loc) · 2.5 KB
/
registration.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
package gui
import (
"errors"
"log"
"github.com/twstrike/coyim/config"
"github.com/twstrike/coyim/tls"
"github.com/twstrike/coyim/xmpp/data"
"github.com/twstrike/coyim/xmpp/interfaces"
"github.com/twstrike/gotk3adapter/gtki"
)
var (
errRegistrationAborted = errors.New("registration cancelled")
)
type registrationForm struct {
grid gtki.Grid
server string
conf *config.Account
fields []formField
}
func (f *registrationForm) accepted() error {
conf, err := config.NewAccount()
if err != nil {
return err
}
//Find the fields we need to copy from the form to the account
for _, field := range f.fields {
ff := field.field.(*data.TextFormField)
w := field.widget.(gtki.Entry)
ff.Result, _ = w.GetText()
switch ff.Label {
case "User", "Username":
conf.Account = ff.Result + "@" + f.server
case "Password":
conf.Password = ff.Result
default:
log.Println("Field", ff.Label)
}
}
f.conf = conf
return nil
}
func (f *registrationForm) addFields(fields []interface{}) {
f.fields = buildWidgetsForFields(fields)
}
func (f *registrationForm) renderForm(title, instructions string, fields []interface{}) error {
doInUIThread(func() {
f.addFields(fields)
for i, field := range f.fields {
f.grid.Attach(field.label, 0, i+1, 1, 1)
f.grid.Attach(field.widget, 1, i+1, 1, 1)
}
f.grid.ShowAll()
})
return nil
}
func requestAndRenderRegistrationForm(server string, formHandler data.FormCallback, df interfaces.DialerFactory, verifier tls.Verifier) error {
policy := config.ConnectionPolicy{DialerFactory: df}
//TODO: this would not be necessary if RegisterAccount did not use it
//TODO: we should give the choice of using Tor to the user
conf := &config.Account{
Account: "@" + server,
Proxies: []string{"tor-auto://"},
}
//TODO: this should receive only a JID domainpart
conn, err := policy.RegisterAccount(formHandler, conf, verifier)
defer conn.Close()
return err
}
type formField struct {
field interface{}
label gtki.Label
widget gtki.Widget
}
func buildWidgetsForFields(fields []interface{}) []formField {
ret := make([]formField, 0, len(fields))
for _, f := range fields {
switch field := f.(type) {
case *data.TextFormField:
//TODO: notify if it is required
l, _ := g.gtk.LabelNew(field.Label)
l.SetSelectable(true)
w, _ := g.gtk.EntryNew()
w.SetText(field.Default)
w.SetVisibility(!field.Private)
ret = append(ret, formField{field, l, w})
default:
log.Println("Missing to implement form field:", field)
}
}
return ret
}