Skip to content

Commit

Permalink
Better download list parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebQ42 committed Apr 14, 2017
1 parent 9e9bb82 commit a55c824
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 35 deletions.
6 changes: 4 additions & 2 deletions appimg/appimg.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 6 additions & 5 deletions appimg/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"
Expand All @@ -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)
}
48 changes: 40 additions & 8 deletions appimg/installui.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"

"github.com/gotk3/gotk3/glib"
Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions appimg/removeLetters.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions appimg/version.go
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 36 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
28 changes: 26 additions & 2 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"io/ioutil"
"os"
"os/exec"

"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a55c824

Please sign in to comment.