From 48933b5ecc58f0205c1615952425f477d6c99ee9 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Wed, 17 Oct 2018 21:38:29 -0700 Subject: [PATCH] better ui for window --- src/croc/croc.go | 1 + src/croc/recipient.go | 10 +++- src/croc/sender.go | 3 +- src/win/Makefile | 4 +- src/win/main.go | 105 ++++++++++++++++++++++-------------------- 5 files changed, 67 insertions(+), 56 deletions(-) diff --git a/src/croc/croc.go b/src/croc/croc.go index 60f50c4..0382eb4 100644 --- a/src/croc/croc.go +++ b/src/croc/croc.go @@ -61,6 +61,7 @@ type Croc struct { // special for window WindowRecipientPrompt bool WindowRecipientAccept bool + WindowReceivingString string } // Init will initiate with the default parameters diff --git a/src/croc/recipient.go b/src/croc/recipient.go index a37b0af..34feecb 100644 --- a/src/croc/recipient.go +++ b/src/croc/recipient.go @@ -71,6 +71,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond) spin.Writer = os.Stderr spin.Suffix = " performing PAKE..." + cr.StateString = "Performing PAKE..." spin.Start() defer spin.Stop() @@ -159,6 +160,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, c.WriteMessage(websocket.BinaryMessage, []byte("ready")) case 3: spin.Stop() + cr.StateString = "Recieving file info..." // unmarshal the file info log.Debugf("[%d] recieve file info", step) @@ -207,12 +209,15 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, if cr.FileInfo.IsDir { fileOrFolder = "folder" } - fmt.Fprintf(os.Stderr, "\r%s %s (%s) into: %s\n", + cr.WindowReceivingString = fmt.Sprintf("%s %s (%s) into: %s", overwritingOrReceiving, fileOrFolder, humanize.Bytes(uint64(cr.FileInfo.Size)), cr.FileInfo.Name, ) + fmt.Fprintf(os.Stderr, "\r%s%s\n", + cr.WindowReceivingString, + ) if !noPrompt { if "y" != utils.GetInput("ok? (y/N): ") { fmt.Fprintf(os.Stderr, "cancelling request") @@ -273,8 +278,8 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, } else { blockSize = models.TCP_BUFFER_SIZE / 2 } - // start the ui for pgoress + cr.StateString = "Recieving file..." bytesWritten := 0 fmt.Fprintf(os.Stderr, "\nReceiving (<-%s)...\n", otherIP) cr.Bar = progressbar.NewOptions( @@ -495,6 +500,7 @@ func (cr *Croc) receive(forceSend int, serverAddress string, tcpPorts []string, } fmt.Fprintf(os.Stderr, "\nReceived %s written to %s (%2.1f %s)\n", folderOrFile, cr.FileInfo.Name, transferRate, transferType) os.Remove(progressFile) + cr.StateString = fmt.Sprintf("Received %s written to %s (%2.1f %s)", folderOrFile, cr.FileInfo.Name, transferRate, transferType) } return err } else { diff --git a/src/croc/sender.go b/src/croc/sender.go index b49eb9a..997270d 100644 --- a/src/croc/sender.go +++ b/src/croc/sender.go @@ -39,8 +39,6 @@ func (cr *Croc) startSender(forceSend int, serverAddress string, tcpPorts []stri fmt.Fprintf(os.Stderr, "\n"+err.Error()) } cr.StateString = err.Error() - } else { - cr.StateString = "File transfer completed." } done <- struct{}{} @@ -455,6 +453,7 @@ func (cr *Croc) send(forceSend int, serverAddress string, tcpPorts []string, isL transferType = "kB/s" } fmt.Fprintf(os.Stderr, "\nTransfer complete (%2.1f %s)", transferRate, transferType) + cr.StateString = fmt.Sprintf("Transfer complete (%2.1f %s)", transferRate, transferType) return nil } else { fmt.Fprintf(os.Stderr, "\nTransfer corrupted") diff --git a/src/win/Makefile b/src/win/Makefile index 61a862d..aecd6b0 100644 --- a/src/win/Makefile +++ b/src/win/Makefile @@ -1,4 +1,4 @@ -VERSION=$(shell git describe --abbrev=0) +VERSION=$(shell git describe --tags --abbrev=0) LDFLAGS=-ldflags "-X main.Version=${VERSION}" .PHONY: linux @@ -10,4 +10,4 @@ fast: GO111MODULE=off qtdeploy ${LDFLAGS} --fast --tags='wincroc' --debug build desktop windows: - GO111MODULE=off qtdeploy ${LDFLAGS} --tags='wincroc' --debug --docker build windows_64_static \ No newline at end of file + GO111MODULE=off qtdeploy ${LDFLAGS} --tags='wincroc' --debug --docker build windows_64_static diff --git a/src/win/main.go b/src/win/main.go index cc266f3..21667f3 100644 --- a/src/win/main.go +++ b/src/win/main.go @@ -50,7 +50,7 @@ func main() { labels[0].SetText("secure data transfer") labels[1].SetText("Click 'Send' or 'Receive' to start") - button := widgets.NewQPushButton2("Send file", nil) + button := widgets.NewQPushButton2("Send", nil) button.ConnectClicked(func(bool) { if isWorking { dialog("Can only do one send or receive at a time") @@ -105,16 +105,6 @@ func main() { done <- true isWorking = false }() - - // for i, label := range labels { - // go func(i int, label *CustomLabel) { - // var tick int - // for range time.NewTicker(time.Duration((i+1)*25) * time.Millisecond).C { - // tick++ - // label.SetText(fmt.Sprintf("%v %v", tick, time.Now().UTC().Format("15:04:05.0000"))) - // } - // }(i, label) - // } }) widget.Layout().AddWidget(button) @@ -124,17 +114,13 @@ func main() { dialog("Can only do one send or receive at a time") return } + isWorking = true defer func() { isWorking = false }() - var codePhrase = widgets.QInputDialog_GetText(nil, "Enter code phrase", "", - widgets.QLineEdit__Normal, "", true, core.Qt__Dialog, core.Qt__ImhNone) - if len(codePhrase) < 3 { - dialog(fmt.Sprintf("Invalid codephrase: '%s'", codePhrase)) - return - } + // determine the folder to save the file var folderDialog = widgets.NewQFileDialog2(nil, "Open folder to receive file...", "", "") folderDialog.SetAcceptMode(widgets.QFileDialog__AcceptOpen) folderDialog.SetFileMode(widgets.QFileDialog__DirectoryOnly) @@ -146,42 +132,61 @@ func main() { dialog(fmt.Sprintf("No folder selected")) return } - cwd, _ := os.Getwd() - os.Chdir(fn) - defer os.Chdir(cwd) - cr := croc.Init(true) - done := make(chan bool) - go func() { - cr.Receive(codePhrase) - done <- true - }() - - for { - select { - case _ = <-done: - break - } - labels[0].SetText(cr.StateString) - if cr.FileInfo.SentName != "" { - labels[0].SetText(fmt.Sprintf("%s", cr.FileInfo.SentName)) - } - if cr.Bar != nil { - barState := cr.Bar.State() - labels[1].SetText(fmt.Sprintf("%2.1f", barState.CurrentPercent)) - } - time.Sleep(100 * time.Millisecond) + var codePhrase = widgets.QInputDialog_GetText(nil, "croc", "Enter code phrase:", + widgets.QLineEdit__Normal, "", true, core.Qt__Dialog, core.Qt__ImhNone) + if len(codePhrase) < 3 { + dialog(fmt.Sprintf("Invalid codephrase: '%s'", codePhrase)) + return } - // for i, label := range labels { - // go func(i int, label *CustomLabel) { - // var tick int - // for range time.NewTicker(time.Duration((i+1)*25) * time.Millisecond).C { - // tick++ - // label.SetText(fmt.Sprintf("%v %v", tick, time.Now().UTC().Format("15:04:05.0000"))) - // } - // }(i, label) - // } + cwd, _ := os.Getwd() + + go func() { + os.Chdir(fn) + defer os.Chdir(cwd) + + cr := croc.Init(true) + cr.WindowRecipientPrompt = true + + done := make(chan bool) + + go func(done chan bool) { + for { + if cr.WindowReceivingString != "" { + var question = widgets.QMessageBox_Question(nil, "croc", fmt.Sprintf("%s?", cr.WindowReceivingString), widgets.QMessageBox__Yes|widgets.QMessageBox__No, 0) + if question == widgets.QMessageBox__Yes { + cr.WindowRecipientAccept = true + labels[0].UpdateTextFromGoroutine(cr.WindowReceivingString) + } else { + cr.WindowRecipientAccept = false + labels[2].UpdateTextFromGoroutine("canceled") + return + } + cr.WindowRecipientPrompt = false + cr.WindowReceivingString = "" + } + + if cr.Bar != nil { + barState := cr.Bar.State() + labels[1].UpdateTextFromGoroutine(fmt.Sprintf("%2.1f%% [%2.0f:%2.0f]", barState.CurrentPercent*100, barState.SecondsSince, barState.SecondsLeft)) + } + labels[2].UpdateTextFromGoroutine(cr.StateString) + time.Sleep(100 * time.Millisecond) + select { + case _ = <-done: + labels[2].UpdateTextFromGoroutine(cr.StateString) + return + default: + continue + } + } + }(done) + + cr.Receive(codePhrase) + done <- true + isWorking = false + }() }) widget.Layout().AddWidget(receiveButton)