forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registration.go
145 lines (114 loc) · 3.14 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
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
package gui
import (
"errors"
"log"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/gtki"
"github.com/twstrike/coyim/config"
"github.com/twstrike/coyim/tls"
"github.com/twstrike/coyim/xmpp/data"
"github.com/twstrike/coyim/xmpp/interfaces"
)
var (
errRegistrationAborted = errors.New("registration cancelled")
)
type registrationForm struct {
parent gtki.Window
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 {
wait := make(chan error)
doInUIThread(func() {
f.addFields(fields)
builder := newBuilder("RegistrationForm")
obj := builder.getObj("dialog")
dialog := obj.(gtki.Dialog)
dialog.SetTitle(title)
obj = builder.getObj("instructions")
label := obj.(gtki.Label)
label.SetText(instructions)
label.SetSelectable(true)
obj = builder.getObj("grid")
grid := obj.(gtki.Grid)
for i, field := range f.fields {
grid.Attach(field.label, 0, i+1, 1, 1)
grid.Attach(field.widget, 1, i+1, 1, 1)
}
grid.ShowAll()
dialog.SetTransientFor(f.parent)
resp := gtki.ResponseType(dialog.Run())
switch resp {
case gtki.RESPONSE_APPLY:
wait <- f.accepted()
default:
wait <- errRegistrationAborted
}
dialog.Destroy()
})
return <-wait
}
func requestAndRenderRegistrationForm(server string, formHandler data.FormCallback, saveFn func(), errorFn func(error), df interfaces.DialerFactory, verifier tls.Verifier) {
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
_, err := policy.RegisterAccount(formHandler, conf, verifier)
if err != nil {
errorFn(err)
return
}
go saveFn()
}
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
}