Skip to content

Commit

Permalink
windows: better use of registry, general UX improvements
Browse files Browse the repository at this point in the history
Former-commit-id: 17a04fb
  • Loading branch information
kercre123 committed Nov 14, 2023
1 parent 6c56239 commit efaad40
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 38 deletions.
61 changes: 52 additions & 9 deletions chipper/cmd/windows/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -22,7 +23,7 @@ import (
var mBoxTitle = "wire-pod"
var mBoxError = `There was an error starting wire-pod: `
var mBoxAlreadyRunning = "Wire-pod is already running. You must quit that instance before starting another one. Exiting."
var mBoxSuccess = `Wire-pod has started successfully! It is now running in the background. It can be stopped in the system tray.`
var mBoxSuccess = `Wire-pod has started successfully! It is now running in the background and can be managed in the system tray.`
var mBoxIcon = "./icons/start-up-full.png"

func getNeedsSetupMsg() string {
Expand All @@ -48,14 +49,33 @@ func main() {
os.Exit(1)
}
}
os.WriteFile(conf+"\\runningPID", []byte(strconv.Itoa(os.Getpid())), 0777)
os.WriteFile(filepath.Join(os.TempDir(), "/wirepodrunningPID"), []byte(strconv.Itoa(os.Getpid())), 0777)
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\wire-pod`, registry.WRITE|registry.READ)
if err != nil {
fmt.Println("Error reading from registry: " + err.Error())
return
}
defer k.Close()

keyPath := `Software\Microsoft\Windows\CurrentVersion\Uninstall\wire-pod`
k, err := registry.OpenKey(registry.LOCAL_MACHINE, keyPath, registry.QUERY_VALUE)
pidPre, _, err := k.GetIntegerValue("LastRunningPID")
if err == nil {
fmt.Println(int(pidPre))
_, err := os.FindProcess(int(pidPre))
if err == nil || errors.Is(err, os.ErrPermission) {
zenity.Error(
"Wire-pod is already running.",
zenity.ErrorIcon,
zenity.Title(mBoxTitle),
)
os.Exit(1)
}
}

err = k.SetQWordValue("LastRunningPID", uint64(os.Getpid()))
if err != nil {
ErrMsg(fmt.Errorf("error opening key from the registry: " + err.Error()))
fmt.Println("Error writing to registry: " + err.Error())
return
}

val, _, err := k.GetStringValue("InstallPath")
if err != nil {
ErrMsg(fmt.Errorf("error getting value from the registry: " + err.Error()))
Expand All @@ -69,10 +89,13 @@ func main() {
}

func ExitProgram(code int) {
conf, _ := os.UserConfigDir()
os.Remove(conf + "/runningPID")
k, err := registry.OpenKey(registry.CURRENT_USER, `Software\wire-pod`, registry.WRITE|registry.READ)
if err != nil {
fmt.Println("Error reading from registry: " + err.Error())
os.Exit(code)
}
k.DeleteValue("LastRunningPID")
os.Exit(code)

}

func onExit() {
Expand All @@ -99,6 +122,7 @@ func onReady() {
systray.SetTooltip("wire-pod is starting...")
mQuit := systray.AddMenuItem("Quit", "Quit wire-pod")
mBrowse := systray.AddMenuItem("Web interface", "Open web UI")
mConfig := systray.AddMenuItem("Config folder", "Open config folder in case you need to. The web UI should have everything you need.")

go func() {
for {
Expand All @@ -112,6 +136,9 @@ func onReady() {
ExitProgram(0)
case <-mBrowse.ClickedCh:
go openBrowser("http://" + botsetup.GetOutboundIP().String() + ":" + vars.WebPort)
case <-mConfig.ClickedCh:
conf, _ := os.UserConfigDir()
go openFileExplorer(filepath.Join(conf, vars.PodName))
}
}
}()
Expand Down Expand Up @@ -142,3 +169,19 @@ func openBrowser(url string) {
logger.Println(err)
}
}

func openFileExplorer(path string) error {
var cmd *exec.Cmd

switch runtime.GOOS {
case "windows":
cmd = exec.Command("explorer", path)
case "darwin":
cmd = exec.Command("open", path)
case "linux":
cmd = exec.Command("xdg-open", path)
default:
return fmt.Errorf("unsupported platform")
}
return cmd.Start()
}
12 changes: 10 additions & 2 deletions chipper/cmd/windows/win-initwirepod.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"time"

chipperpb "github.com/digital-dream-labs/api/go/chipperpb"
Expand Down Expand Up @@ -49,6 +50,7 @@ func NeedsSetupMsg() {
zenity.Icon(mBoxIcon),
zenity.Title(mBoxTitle),
zenity.ExtraButton("Open browser"),
zenity.OKLabel("OK"),
)
if err != nil {
if err == zenity.ErrExtraButton {
Expand Down Expand Up @@ -275,8 +277,14 @@ func StartChipper(fromInit bool) {
go httpServe(httpListenerTwo)
}

systray.SetTooltip("wire-pod is running.")
if fromInit {
systray.SetTooltip("wire-pod is running.\n" + "http://" + botsetup.GetOutboundIP().String() + ":" + vars.WebPort)
var discrete bool
if len(os.Args) > 1 {
if strings.Contains(os.Args[1], "-d") {
discrete = true
}
}
if fromInit && !discrete {
go zenity.Info(
mBoxSuccess,
zenity.Icon(mBoxIcon),
Expand Down
6 changes: 5 additions & 1 deletion chipper/cmd/wire-pod-installer/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import (
"os"
"path/filepath"
"strings"
"time"
)

func InstallWirePod(is InstallSettings) error {
UpdateInstallStatus("Stopping any wire-pod instances...")
StopWirePodIfRunning()

UpdateInstallStatus("Uninstalling any previous wire-pod instances (user data will be kept)...")
DeleteAnyOtherInstallation()

UpdateInstallStatus("Removing any wire-pod files (if they exist)...")
os.RemoveAll(is.Where)

Expand All @@ -31,7 +35,6 @@ func InstallWirePod(is InstallSettings) error {
var bytesRead int64 = 0

// Create a temporary file to store the download
UpdateInstallStatus("Creating temp file...")
tempFile, err := os.CreateTemp("", "wire-pod-*.zip")
if err != nil {
return fmt.Errorf("error creating a temp file: %s", err)
Expand Down Expand Up @@ -138,5 +141,6 @@ func InstallWirePod(is InstallSettings) error {
UpdateInstallStatus("Done!")

UpdateInstallBar(100)
time.Sleep(time.Second / 3)
return nil
}
32 changes: 23 additions & 9 deletions chipper/cmd/wire-pod-installer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func GetStatusChan() chan string {

func ExecuteDetached(program string) error {
cmd := exec.Command(program)
// Start the program in a new process group to make it detached
cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP}
return cmd.Start()
}
Expand Down Expand Up @@ -131,7 +130,10 @@ func DoInstall(myApp fyne.App, is InstallSettings) {
card.Refresh()
}
}()
InstallWirePod(is)
err := InstallWirePod(is)
if err != nil {
fmt.Println("error installing wire-pod: " + err.Error())
}
window.Hide()
PostInstall(myApp, is)
}
Expand All @@ -146,13 +148,16 @@ func GetPreferences(myApp fyne.App) {
is.RunAtStartup = checked
})

launchOnStartup.SetChecked(true)
is.RunAtStartup = true

installDir := widget.NewEntry()
installDir.SetText(DefaultInstallationDirectory)

selectDirButton := widget.NewButton("Select Directory", func() {
dlg := dialog.NewFolderOpen(func(uri fyne.ListableURI, err error) {
if uri != nil {
installDir.SetText(uri.Path())
installDir.SetText(filepath.Join(uri.Path(), "wire-pod"))
}
}, window)
dlg.Show()
Expand Down Expand Up @@ -191,9 +196,7 @@ func GetPreferences(myApp fyne.App) {
}

func StopWirePodIfRunning() {
confDir, _ := os.UserConfigDir()
podDir := confDir + "/wire-pod"
podPid, err := os.ReadFile(podDir + "/runningPID")
podPid, err := os.ReadFile(filepath.Join(os.TempDir(), "/wirepodrunningPID"))
if err == nil {
pid, _ := strconv.Atoi(string(podPid))
// doesn't work on unix, but should on Windows
Expand All @@ -205,12 +208,10 @@ func StopWirePodIfRunning() {
fmt.Println("Stopped")
}
}
CheckWirePodRunningViaRegistry()
}

func ValidateInstallDirectory(dir string) bool {
if dir == "C:\\Program Files" || dir == "C:\\Program Files\\" {
return false
}
var dirWithoutLast string
splitDir := strings.Split(dir, "\\")
dirWithoutLast = splitDir[0]
Expand All @@ -233,6 +234,19 @@ func main() {
fmt.Println("installer must be run as administrator")
os.Exit(0)
}
fmt.Println("Getting tag from GitHub")
tag, err := GetLatestReleaseTag("kercre123", "wire-pod")
if err != nil {
fmt.Println("Error getting: " + err.Error())
zenity.Error(
"Error getting latest GitHub tag from GitHub, exiting: "+err.Error(),
zenity.ErrorIcon,
zenity.Title("wire-pod installer"),
)
os.Exit(0)
}
GitHubTag = tag
fmt.Println(tag)
iconBytes, err := iconData.ReadFile("ico/pod.png")
if err != nil {
fmt.Println(err)
Expand Down
Loading

0 comments on commit efaad40

Please sign in to comment.