From a55c82483a2962333689cd9e2545b8c2fd93e633 Mon Sep 17 00:00:00 2001 From: Belac Darkstorm Date: Fri, 14 Apr 2017 03:36:50 -0500 Subject: [PATCH] Better download list parsing --- appimg/appimg.go | 6 +++-- appimg/download.go | 11 +++++---- appimg/installui.go | 48 ++++++++++++++++++++++++++++++------- appimg/removeLetters.go | 12 ++++++++++ appimg/version.go | 33 +++++++++++++++++++++++++ main.go | 53 ++++++++++++++++++++++++++++------------- settings.go | 28 ++++++++++++++++++++-- ui.go | 2 +- 8 files changed, 158 insertions(+), 35 deletions(-) create mode 100644 appimg/removeLetters.go create mode 100644 appimg/version.go diff --git a/appimg/appimg.go b/appimg/appimg.go index 60f58be..8ef59be 100644 --- a/appimg/appimg.go +++ b/appimg/appimg.go @@ -1,11 +1,13 @@ package appimg type appimg struct { - name string + full string + name string + version string } func newApp(name string) appimg { var out appimg - out.name = name + out.full = name return out } diff --git a/appimg/download.go b/appimg/download.go index 1d87c33..33f7291 100644 --- a/appimg/download.go +++ b/appimg/download.go @@ -19,7 +19,7 @@ func downloadApp(parent *gtk.Window, ap appimg) { }) spn, _ := gtk.SpinnerNew() spn.Start() - lbl, _ := gtk.LabelNew("Downloading " + ap.name + "...") + lbl, _ := gtk.LabelNew("Downloading " + ap.full + "...") box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5) box.SetMarginStart(10) box.SetMarginEnd(10) @@ -39,13 +39,13 @@ func downloadApp(parent *gtk.Window, ap appimg) { return nil }, } - resp, err := check.Get(urlBase + ap.name) + resp, err := check.Get(urlBase + ap.full) if err != nil { fmt.Println(err) return } defer resp.Body.Close() - name := strings.Split(ap.name, "-")[0] + name := strings.Split(ap.full, "-")[0] var foldName string if _, err = os.Open("PortableApps/" + name + "Portable"); err == nil { foldName = "PortableApps/" + name + "Portable" @@ -55,12 +55,13 @@ func downloadApp(parent *gtk.Window, ap appimg) { os.Mkdir("PortableApps/"+name+"Portable", 0777) foldName = "PortableApps/" + name } - fil, err := os.Create(foldName + "/" + ap.name) + os.Remove(foldName + "/" + ap.full) + fil, err := os.Create(foldName + "/" + ap.full) if err != nil { fmt.Println(err) return } io.Copy(fil, resp.Body) - _ = fil.Chmod(0777) + fil.Chmod(0777) }(win, ap) } diff --git a/appimg/installui.go b/appimg/installui.go index 0c68f30..69bbfae 100644 --- a/appimg/installui.go +++ b/appimg/installui.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "sort" "strings" "github.com/gotk3/gotk3/glib" @@ -16,7 +17,7 @@ const ( ) //ShowUI shows the list of possible AppImages to be downloaded in a gtk.Window -func ShowUI(clsFunc func()) { +func ShowUI(newestVersionOnly bool, clsFunc func()) { win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL) win.Connect("destroy", func() { clsFunc() @@ -45,13 +46,44 @@ func ShowUI(clsFunc func()) { win.Show() getList(win, apch) go func(win *gtk.Window, apch chan appimg, list *gtk.ListBox) { - for i := range apch { - glib.IdleAdd(func(list *gtk.ListBox, i appimg) { - lbl, _ := gtk.LabelNew(i.name) - list.Add(lbl) - apps = append(apps, i) - lbl.Show() - }, list, i) + if newestVersionOnly { + imgs := make([]appimg, 0) + a := make(map[string][]appimg) + names := make([]string, 0) + for i := range apch { + imgs = append(imgs, i) + } + for i, v := range imgs { + sp := strings.Split(v.full, "-") + if len(sp) >= 2 { + vers := sp[1] + removeLetters(vers) + imgs[i].version = vers + imgs[i].name = sp[0] + if _, ok := a[imgs[i].name]; !ok { + names = append(names, imgs[i].name) + } + a[imgs[i].name] = append(a[imgs[i].name], imgs[i]) + } + } + sort.Strings(names) + for _, name := range names { + glib.IdleAdd(func(name string, list *gtk.ListBox, i appimg) { + lbl, _ := gtk.LabelNew(name) + list.Add(lbl) + apps = append(apps, i) + lbl.Show() + }, name, list, a[name][compareVersions(a[name])]) + } + } else { + for i := range apch { + glib.IdleAdd(func(list *gtk.ListBox, i appimg) { + lbl, _ := gtk.LabelNew(i.full) + list.Add(lbl) + apps = append(apps, i) + lbl.Show() + }, list, i) + } } }(win, apch, appList) } diff --git a/appimg/removeLetters.go b/appimg/removeLetters.go new file mode 100644 index 0000000..4a3b566 --- /dev/null +++ b/appimg/removeLetters.go @@ -0,0 +1,12 @@ +package appimg + +import "strings" + +func removeLetters(vers string) string { + vers = strings.ToLower(vers) + letters := []string{"abcdefghijklmnopqrstuvwxyz"} + for _, v := range letters { + vers = strings.Replace(vers, v, "", -1) + } + return vers +} diff --git a/appimg/version.go b/appimg/version.go new file mode 100644 index 0000000..6ac873d --- /dev/null +++ b/appimg/version.go @@ -0,0 +1,33 @@ +package appimg + +import ( + "strconv" + "strings" +) + +func compareVersions(imgs []appimg) int { + for i := range imgs { + imgs[i].version = removeLetters(imgs[i].version) + } + highest := 0 + higharr := strings.Split(imgs[0].version, ".") + for i := 0; i < len(imgs); i++ { + if i != highest { + varr := strings.Split(imgs[i].version, ".") + if len(higharr) < len(varr) { + for j := 0; j < len(higharr); j++ { + h, _ := strconv.Atoi(higharr[j]) + c, _ := strconv.Atoi(varr[j]) + if h > c { + break + } else if c > h { + highest = i + higharr = varr + break + } + } + } + } + } + return highest +} diff --git a/main.go b/main.go index e24a3e3..267985c 100644 --- a/main.go +++ b/main.go @@ -9,19 +9,20 @@ import ( ) const ( - version = "2.1.0.5" + version = "2.1.1.0" defIni = "" ) var ( - master map[string][]app - linmaster map[string][]app - cats []string - lin []string - wine bool - comEnbld bool - wineAvail bool - portableHide bool + master map[string][]app + linmaster map[string][]app + cats []string + lin []string + wine bool + comEnbld bool + wineAvail bool + portableHide bool + versionNewest = true ) func main() { @@ -53,16 +54,24 @@ func uiStart() { } func savePrefs() { - fil, err := os.Open("PortableApps/LinuxPACom/Prefs.gob") - if os.IsNotExist(err) { - fil, err = os.Create("PortableApps/LinuxPACom/Prefs.gob") - } + os.Remove("PortableApps/LinuxPACom/Prefs.gob") + fil, err := os.Create("PortableApps/LinuxPACom/Prefs.gob") if err != nil { return } enc := gob.NewEncoder(fil) - enc.Encode(wine) - enc.Encode(portableHide) + err = enc.Encode(wine) + if err != nil { + return + } + err = enc.Encode(portableHide) + if err != nil { + return + } + err = enc.Encode(versionNewest) + if err != nil { + return + } } func loadPrefs() { @@ -71,8 +80,18 @@ func loadPrefs() { return } dec := gob.NewDecoder(fil) - dec.Decode(&wine) - dec.Decode(&portableHide) + err = dec.Decode(&wine) + if err != nil { + return + } + err = dec.Decode(&portableHide) + if err != nil { + return + } + err = dec.Decode(&versionNewest) + if err != nil { + return + } } func contains(arr []string, str string) bool { diff --git a/settings.go b/settings.go index d248a2f..add75dc 100644 --- a/settings.go +++ b/settings.go @@ -3,7 +3,9 @@ package main import ( "io/ioutil" "os" + "os/exec" + "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" ) @@ -26,6 +28,7 @@ func settingsUI(parent *gtk.Window, onExit func()) { gnrl.SetMarginTop(10) gnrl.SetMarginBottom(10) dlWine, _ := gtk.ButtonNewWithLabel("Download Wine") + wineCheck, _ := gtk.CheckButtonNewWithLabel("Show Windows apps (Wine)") wineLbl, _ := gtk.LabelNew("PortableApps/LinuxPACom/Wine present") dlWine.Connect("clicked", func() { cb := make(chan bool) @@ -36,6 +39,22 @@ func settingsUI(parent *gtk.Window, onExit func()) { setupTxt(comBuf) wineLbl.Show() } + if _, err := os.Open("PortableApps/LinuxPACom/Wine"); os.IsNotExist(err) { + if _, errd := exec.LookPath("wine"); errd == nil { + wineAvail = true + } + } else if err == nil { + wineAvail = true + } + glib.IdleAdd(func() { + if !wineAvail { + wineCheck.SetSensitive(false) + wineCheck.SetTooltipText("Download wine to run windows apps") + } else { + wineCheck.SetSensitive(true) + wineCheck.SetTooltipText("") + } + }) }() }) if !comEnbld { @@ -51,7 +70,7 @@ func settingsUI(parent *gtk.Window, onExit func()) { lin = make([]string, 0) setup() }) - wineCheck, _ := gtk.CheckButtonNewWithLabel("Show Windows apps (Wine)") + pthdCheck.SetActive(portableHide) if !wineAvail { wineCheck.SetSensitive(false) wineCheck.SetTooltipText("Download wine to run windows apps") @@ -60,11 +79,16 @@ func settingsUI(parent *gtk.Window, onExit func()) { wineCheck.Connect("toggled", func() { wine = wineCheck.GetActive() }) - pthdCheck.SetActive(portableHide) + versCheck, _ := gtk.CheckButtonNewWithLabel("Only show newest app version in downloads (A bit iffy ATM)") + versCheck.SetActive(versionNewest) + versCheck.Connect("toggled", func() { + versionNewest = versCheck.GetActive() + }) gnrl.Add(wineLbl) gnrl.Add(dlWine) gnrl.Add(pthdCheck) gnrl.Add(wineCheck) + gnrl.Add(versCheck) ntbk.AppendPage(gnrl, getLabel("General")) com, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5) com.SetMarginStart(10) diff --git a/ui.go b/ui.go index 1b8882c..e698d0b 100644 --- a/ui.go +++ b/ui.go @@ -140,7 +140,7 @@ func ui(win *gtk.Window) { } }) dnl.Connect("clicked", func() { - appimg.ShowUI(func() { + appimg.ShowUI(versionNewest, func() { master = make(map[string][]app) linmaster = make(map[string][]app) cats = make([]string, 0)