Skip to content

Commit

Permalink
better ui for window
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Oct 18, 2018
1 parent f9dfa6b commit 48933b5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 53 deletions.
1 change: 1 addition & 0 deletions src/croc/croc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Croc struct {
// special for window
WindowRecipientPrompt bool
WindowRecipientAccept bool
WindowReceivingString string
}

// Init will initiate with the default parameters
Expand Down
10 changes: 8 additions & 2 deletions src/croc/recipient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions src/croc/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/win/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
GO111MODULE=off qtdeploy ${LDFLAGS} --tags='wincroc' --debug --docker build windows_64_static
99 changes: 52 additions & 47 deletions src/win/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -146,42 +132,61 @@ func main() {
dialog(fmt.Sprintf("No folder selected"))
return
}

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
}

cwd, _ := os.Getwd()
os.Chdir(fn)
defer os.Chdir(cwd)

cr := croc.Init(true)
done := make(chan bool)
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
}()

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

// 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(receiveButton)

Expand Down

0 comments on commit 48933b5

Please sign in to comment.