signform.go 3.76 KB
Newer Older
Loïck Bonniot's avatar
Loïck Bonniot committed
1 2 3
package signform

import (
Loïck Bonniot's avatar
Loïck Bonniot committed
4 5 6 7 8
	"encoding/json"
	"io/ioutil"

	"dfss/dfssc/sign"
	"dfss/dfssp/contract"
Loïck Bonniot's avatar
Loïck Bonniot committed
9 10 11 12
	"dfss/gui/config"
	"github.com/visualfc/goqt/ui"
)

Loïck Bonniot's avatar
Loïck Bonniot committed
13 14 15 16 17 18
type line struct {
	status              sign.SignerStatus
	info, mail          string
	cellA, cellB, cellC *ui.QTableWidgetItem
}

Loïck Bonniot's avatar
Loïck Bonniot committed
19 20
type Widget struct {
	*ui.QWidget
Loïck Bonniot's avatar
Loïck Bonniot committed
21 22 23 24 25 26 27 28 29

	manager                  *sign.SignatureManager
	contract                 *contract.JSON
	table                    *ui.QTableWidget
	progressBar              *ui.QProgressBar
	feedbackLabel            *ui.QLabel
	lines                    []line
	statusMax, statusCurrent int32
	feedback                 string
Loïck Bonniot's avatar
Loïck Bonniot committed
30 31
}

Loïck Bonniot's avatar
Loïck Bonniot committed
32 33
func NewWidget(conf *config.Config, filename, pwd string) *Widget {
	loadIcons()
Loïck Bonniot's avatar
Loïck Bonniot committed
34 35 36 37 38
	file := ui.NewFileWithName(":/signform/signform.ui")
	loader := ui.NewUiLoader()
	form := loader.Load(file)
	w := &Widget{QWidget: form}

Loïck Bonniot's avatar
Loïck Bonniot committed
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
	w.feedbackLabel = ui.NewLabelFromDriver(w.FindChild("mainLabel"))
	w.table = ui.NewTableWidgetFromDriver(w.FindChild("signersTable"))
	w.progressBar = ui.NewProgressBarFromDriver(w.FindChild("progressBar"))

	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil
	}

	w.contract = new(contract.JSON)
	err = json.Unmarshal(data, w.contract)
	if err != nil {
		return nil
	}

	home := config.GetHomeDir()
	m, err := sign.NewSignatureManager(
		home+config.CAFile,
		home+config.CertFile,
		home+config.KeyFile,
		conf.Platform,
		pwd,
		9005, // TODO change port
		w.contract,
	)
	if err != nil {
		// TODO
	}

	w.manager = m
	w.manager.OnSignerStatusUpdate = w.signerUpdated
	w.manager.OnProgressUpdate = func(current int, max int) {
		w.statusCurrent = int32(current)
		w.statusMax = int32(max)
	}

	w.initLines()
	w.signerUpdated(conf.Email, sign.StatusConnected, "It's you!")
	go func() {
		err = w.execute()
		if err != nil {
			w.feedback = err.Error()
		} else {
			w.feedback = "Contract signed successfully!"
		}
	}()

Loïck Bonniot's avatar
Loïck Bonniot committed
86 87
	return w
}
Loïck Bonniot's avatar
Loïck Bonniot committed
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

func (w *Widget) execute() error {
	w.feedback = "Connecting to peers..."
	err := w.manager.ConnectToPeers()
	if err != nil {
		return err
	}

	w.feedback = "Waiting for peers..."
	_, err = w.manager.SendReadySign()
	if err != nil {
		return err
	}

	w.feedback = "Signature in progress..."
	err = w.manager.Sign()
	if err != nil {
		return err
	}

	w.feedback = "Storing file..."
	return w.manager.PersistSignaturesToFile() // TODO choose destination
}

func (w *Widget) signerUpdated(mail string, status sign.SignerStatus, data string) {
	for i, s := range w.contract.Signers {
		if s.Email == mail {
			w.lines[i].status = status
			w.lines[i].info = data
			break
		}
	}
}

// Tick updates the whole screen, as we cannot directly update the screen on each callback.
// It is called by the screen coordinator via a timer.
func (w *Widget) Tick() {
	w.feedbackLabel.SetText(w.feedback)
	w.progressBar.SetMaximum(w.statusMax)
	w.progressBar.SetValue(w.statusCurrent)
	for _, l := range w.lines {
		l.cellA.SetIcon(icons[l.status])
		l.cellA.SetText(icons_labels[l.status])
		l.cellB.SetText(l.info)
	}
}

func (w *Widget) initLines() {
	w.table.SetRowCount(int32(len(w.contract.Signers)))
	for i, s := range w.contract.Signers {
		status := ui.NewTableWidgetItemWithIconTextType(icons[sign.StatusWaiting], icons_labels[sign.StatusWaiting], int32(ui.QTableWidgetItem_UserType))
		w.table.SetItem(int32(i), 0, status)

		info := ui.NewTableWidgetItemWithTextType("", int32(ui.QTableWidgetItem_UserType))
		w.table.SetItem(int32(i), 1, info)

		mail := ui.NewTableWidgetItemWithTextType(s.Email, int32(ui.QTableWidgetItem_UserType))
		w.table.SetItem(int32(i), 2, mail)

		// We must store items, otherwise GC will remove it and cause the application to crash.
		// Trust me, hard to debug...
		w.lines = append(w.lines, line{sign.StatusWaiting, "", s.Email, status, info, mail})
	}
}

func (w *Widget) Q() *ui.QWidget {
	return w.QWidget
}