Commit d93fd42f authored by Loïck Bonniot's avatar Loïck Bonniot

[gui] Add logic for new contract form

parent bae90ef4
Pipeline #578 passed with stage
package config
import (
"errors"
"io/ioutil"
"dfss/auth"
"github.com/visualfc/goqt/ui"
)
// PasswordDialog checks the current private key for any passphrase.
// If the key is protected, it spawns an inputDialog window to ask the user's passphrase,
// and then calls the callback function with the result.
//
// The callback is always called, even when an error occurs.
func PasswordDialog(callback func(err error, pwd string)) {
// Try to get private key
path := GetHomeDir() + KeyFile
data, err := ioutil.ReadFile(path)
if err != nil {
callback(err, "")
return
}
if !auth.IsPEMEncrypted(data) {
callback(nil, "")
return
}
dialog := ui.NewInputDialog()
dialog.SetWindowTitle("Encrypted private key")
dialog.SetLabelText("Please type your password to proceed:")
dialog.SetTextEchoMode(ui.QLineEdit_Password)
dialog.OnRejected(func() {
callback(errors.New("user rejected"), "")
})
dialog.OnAccepted(func() {
pwd := dialog.TextValue()
if pwd == "" {
pwd = " " // doing this to force the "wrong password" error msg
}
callback(nil, pwd)
})
dialog.Open()
}
package contractform
import (
"strings"
"dfss/dfssc/sign"
"dfss/gui/config"
"github.com/visualfc/goqt/ui"
)
type Widget struct {
*ui.QWidget
signers *ui.QPlainTextEdit
}
func NewWidget(conf *config.Config) *Widget {
......@@ -14,5 +19,70 @@ func NewWidget(conf *config.Config) *Widget {
loader := ui.NewUiLoader()
form := loader.Load(file)
return &Widget{QWidget: form}
fileField := ui.NewLineEditFromDriver(form.FindChild("fileField"))
commentField := ui.NewPlainTextEditFromDriver(form.FindChild("commentField"))
signersField := ui.NewPlainTextEditFromDriver(form.FindChild("signersField"))
fileButton := ui.NewPushButtonFromDriver(form.FindChild("fileButton"))
createButton := ui.NewPushButtonFromDriver(form.FindChild("createButton"))
feedbackLabel := ui.NewLabelFromDriver(form.FindChild("feedbackLabel"))
w := &Widget{
QWidget: form,
signers: signersField,
}
signersField.SetPlainText(conf.Email + "\n")
fileButton.OnClicked(func() {
filter := "Any (*.*)"
filename := ui.QFileDialogGetOpenFileNameWithParentCaptionDirFilterSelectedfilterOptions(form, "Select contract file", config.GetHomeDir(), filter, &filter, 0)
fileField.SetText(filename)
})
createButton.OnClicked(func() {
form.SetDisabled(true)
feedbackLabel.SetText("Please wait...")
config.PasswordDialog(func(err error, pwd string) {
if err != nil {
form.SetDisabled(false)
feedbackLabel.SetText("Aborted.")
return // wrong key or rejection, aborting
}
home := config.GetHomeDir()
err = sign.SendNewContract(
home+config.CAFile,
home+config.CertFile,
home+config.KeyFile,
conf.Platform,
pwd,
fileField.Text(),
commentField.ToPlainText(),
w.SignersList(),
)
if err != nil {
feedbackLabel.SetText(err.Error())
} else {
feedbackLabel.SetText("Contract successfully sent to signers!")
fileField.SetText("")
}
form.SetDisabled(false)
})
})
return w
}
func (w *Widget) SignersList() (list []string) {
rawList := strings.Split(w.signers.ToPlainText(), "\n")
for _, e := range rawList {
clean := strings.TrimSpace(e)
if clean != "" {
list = append(list, clean)
}
}
return
}
......@@ -68,6 +68,9 @@
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="fileField">
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
......@@ -88,7 +91,7 @@
<item row="1" column="0">
<widget class="QLabel" name="signersLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Signers&lt;br/&gt;&lt;/span&gt;&lt;span style=&quot; font-size:8pt; font-style:italic;&quot;&gt;Space-separated&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;Signers&lt;br/&gt;&lt;/span&gt;&lt;span style=&quot; font-size:8pt; font-style:italic;&quot;&gt;One per line&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
......@@ -115,7 +118,7 @@
</property>
</widget>
</item>
<item row="4" column="1">
<item row="3" column="1">
<widget class="QLabel" name="feedbackLabel">
<property name="text">
<string/>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment