userform.go 1.78 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
	"github.com/visualfc/goqt/ui"
)

type Widget struct {
12
	*ui.QWidget
Loïck Bonniot's avatar
Loïck Bonniot committed
13 14
}

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

	// Events

	registerButton.OnClicked(func() {
		form.SetDisabled(true)
		feedbackLabel.SetText("Registration in progress...")
35 36
		filter := "Root Certificates (*.pem);;Any (*.*)"
		caFilename := ui.QFileDialogGetOpenFileNameWithParentCaptionDirFilterSelectedfilterOptions(form, "Select the CA file for the platform", home, filter, &filter, 0)
37
		caDest := home + config.CAFile
38
		_ = copyCA(caFilename, caDest)
Loïck Bonniot's avatar
Loïck Bonniot committed
39 40

		err := user.Register(
41 42 43
			caDest,
			home+config.CertFile,
			home+config.KeyFile,
Loïck Bonniot's avatar
Loïck Bonniot committed
44 45 46 47 48 49 50
			hostField.Text(),
			passwordField.Text(),
			"", "", "", emailField.Text(), 2048,
		)
		if err != nil {
			feedbackLabel.SetText(err.Error())
		} else {
51
			conf.Email = emailField.Text()
52
			conf.Platform = hostField.Text()
53
			onRegistered(passwordField.Text())
54
			config.Save(*conf)
Loïck Bonniot's avatar
Loïck Bonniot committed
55 56 57 58
		}
		form.SetDisabled(false)
	})

59
	return &Widget{QWidget: form}
Loïck Bonniot's avatar
Loïck Bonniot committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

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)
}