1package main 2 3import ( 4 "flag" 5 "fmt" 6 fyne "fyne.io/fyne/v2" 7 "fyne.io/fyne/v2/app" 8 "fyne.io/fyne/v2/container" 9 "fyne.io/fyne/v2/dialog" 10 "fyne.io/fyne/v2/layout" 11 "fyne.io/fyne/v2/widget" 12 "github.com/cosmocode/golocal/i18n" 13 setup "github.com/cosmocode/golocal/setup" 14 "log" 15 "os" 16 "regexp" 17 "strings" 18) 19 20func main() { 21 flag.Usage = usage 22 flagInstall := flag.Bool("install", false, "Install the protocol handler") 23 flagUninstall := flag.Bool("uninstall", false, "Uninstall the protocol handler") 24 flag.Parse() 25 26 i18n.Initialize() 27 28 if *flagInstall { 29 install(nil) 30 } else if *flagUninstall { 31 uninstall(nil) 32 } else { 33 _, window := guiInit() 34 if len(os.Args) > 1 { 35 go run(os.Args[1], window) 36 } else { 37 go guiInstaller(window) 38 } 39 40 // start the main loop 41 window.ShowAndRun() 42 } 43} 44 45func guiInit() (fyne.App, fyne.Window) { 46 application := app.New() 47 w := application.NewWindow(fmt.Sprintf("%s handler", setup.PROTOCOL)) 48 w.Resize(fyne.NewSize(500, 200)) 49 w.CenterOnScreen() 50 return application, w 51} 52 53func guiInstaller(window fyne.Window) { 54 lblIntro := widget.NewLabel(i18n.T("intro", nil)) 55 //lblIntro.Wrapping = fyne.TextWrapWord // breaks window height, see fyne-io/fyne#4097 56 btnInstall := widget.NewButton(i18n.T("install", nil), func() { install(window) }) 57 btnUninstall := widget.NewButton(i18n.T("uninstall", nil), func() { uninstall(window) }) 58 59 window.SetContent( 60 container.New( 61 layout.NewVBoxLayout(), 62 lblIntro, 63 layout.NewSpacer(), 64 btnInstall, 65 btnUninstall, 66 ), 67 ) 68} 69 70func run(path string, window fyne.Window) { 71 // remove protocol 72 log.Println("Input URL:", path) 73 r, _ := regexp.Compile("^.*?://") 74 path = r.ReplaceAllString(path, "") 75 log.Println("Extracted Path:", path) 76 77 // drive letter detection 78 isLetter, _ := regexp.MatchString("^/[C-Z]//", path) 79 if isLetter { 80 path = strings.Replace(path, "//", ":\\", 1) 81 path = path[1:] 82 log.Println("Drive letter path:", path) 83 } 84 85 // local path 86 path = setup.PreparePath(path, isLetter) 87 log.Println("Opening Path: ", path) 88 window.SetContent(widget.NewLabel(path)) 89 90 err := setup.Run(path) 91 errHandler(err, "", window) 92 if err == nil { 93 window.Close() 94 } 95} 96 97func install(window fyne.Window) { 98 err := setup.Install() 99 errHandler(err, i18n.T("installed", nil), window) 100} 101 102func uninstall(window fyne.Window) { 103 err := setup.Uninstall() 104 errHandler(err, i18n.T("uninstalled", nil), window) 105} 106 107// Outputs either error or success message using the appropriate channel based on if 108// window is available or nil 109func errHandler(err error, success string, window fyne.Window) { 110 if err == nil { 111 if window == nil { 112 log.Println(success) 113 } else if success != "" { 114 dialog.ShowInformation(i18n.T("success", nil), success, window) 115 } 116 } else { 117 if window == nil { 118 log.Fatal(err) 119 } else { 120 dialog.ShowError(err, window) 121 } 122 } 123} 124 125func usage() { 126 fmt.Printf("Usage: %s %s://path \n", os.Args[0], setup.PROTOCOL) 127 fmt.Println(" Protocol handling. Will try to open the given path locally.") 128 fmt.Println() 129 130 fmt.Printf("Usage: %s [OPTION]\n", os.Args[0]) 131 fmt.Println(" Install or uninstall the protocol handler") 132 flag.PrintDefaults() 133} 134