userform.go 1.91 KB
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1
2
3
4
5
6
package userform

import (
	"io/ioutil"

	"dfss/dfssc/user"
7
	"dfss/gui/config"
Loïck Bonniot's avatar
Loïck Bonniot committed
8
9
10
11
12
13
14
	"github.com/visualfc/goqt/ui"
)

type Widget struct {
	W *ui.QWidget
}

15
func NewWidget(conf *config.Config, onRegistered func(pw string)) *Widget {
Loïck Bonniot's avatar
Loïck Bonniot committed
16
17
18
19
20
21
22
23
24
25
26
27
	file := ui.NewFileWithName(":/userform/userform.ui")
	loader := ui.NewUiLoader()
	form := loader.Load(file)

	emailField := ui.NewLineEditFromDriver(form.FindChild("emailField"))
	hostField := ui.NewLineEditFromDriver(form.FindChild("hostField"))
	passwordField := ui.NewLineEditFromDriver(form.FindChild("passwordField"))
	passwordField.SetEchoMode(ui.QLineEdit_Password)

	feedbackLabel := ui.NewLabelFromDriver(form.FindChild("feedbackLabel"))
	registerButton := ui.NewPushButtonFromDriver(form.FindChild("registerButton"))

28
	home := config.GetHomeDir()
Loïck Bonniot's avatar
Loïck Bonniot committed
29
30
31
32
33
34
35
36
37
38
39
40
	fileDialog := ui.NewFileDialogWithParentCaptionDirectoryFilter(nil, "Select the CA file for the platform", home, "Root Certificates (*.pem);;Any (*.*)")

	// Events

	registerButton.OnClicked(func() {
		form.SetDisabled(true)
		feedbackLabel.SetText("Registration in progress...")
		fileDialog.Open()
	})

	fileDialog.OnFileSelected(func(ca string) {
		fileDialog.Hide()
41
		caDest := home + config.CAFile
Loïck Bonniot's avatar
Loïck Bonniot committed
42
43
44
		_ = copyCA(ca, caDest)

		err := user.Register(
45
46
47
			caDest,
			home+config.CertFile,
			home+config.KeyFile,
Loïck Bonniot's avatar
Loïck Bonniot committed
48
49
50
51
52
53
54
			hostField.Text(),
			passwordField.Text(),
			"", "", "", emailField.Text(), 2048,
		)
		if err != nil {
			feedbackLabel.SetText(err.Error())
		} else {
55
			conf.Email = emailField.Text()
56
			conf.Platform = hostField.Text()
57
			onRegistered(passwordField.Text())
58
			config.Save(*conf)
Loïck Bonniot's avatar
Loïck Bonniot committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
		}
		form.SetDisabled(false)
	})

	fileDialog.OnRejected(func() {
		form.SetDisabled(false)
		feedbackLabel.SetText("Registration aborted.")
	})

	return &Widget{W: form}
}

func copyCA(from string, to string) error {
	if from == to {
		return nil
	}

	file, err := ioutil.ReadFile(from)
	if err != nil {
		return err
	}

	return ioutil.WriteFile(to, file, 0600)
}