Nearly done with download :)

This commit is contained in:
Belac Darkstorm
2017-04-09 01:26:31 -05:00
parent 01556c1eb0
commit 695ce815d9
7 changed files with 221 additions and 19 deletions
+10 -10
View File
@@ -1,25 +1,25 @@
//Package appimg is to download and update AppImages for LinuxPA.
//Converted from github.com/CalebQ42/bbConvert
package appimg package appimg
import "reflect" import "reflect"
//Convert converts the input string. Only returns <a> tags func convert(in string) (out []tag) {
func Convert(in string) (out []Tag) {
for i := 0; i < len(in); i++ { for i := 0; i < len(in); i++ {
v := in[i] v := in[i]
if v == '<' { if v == '<' {
for j := i; j < len(in); j++ { for j := i; j < len(in); j++ {
val := in[j] val := in[j]
if val == '>' { if val == '>' {
var tmp Tag var tmp tag
tmp.process(in[i+1 : j+1]) tmp.process(in[i+1 : j+1])
if !tmp.end { if !tmp.end && tmp.typ == "a" {
tmp.index[0] = i tmp.index[0] = i
tmp.index[1] = j tmp.index[1] = j
nd := fndend(tmp, in[j+1:]) nd := fndend(tmp, in[j+1:])
if !reflect.DeepEqual(nd, Tag{}) && tmp.typ == "a" { if !reflect.DeepEqual(nd, tag{}) {
tmp.Meat = in[j+1 : nd.index[0]]
out = append(out, tmp) out = append(out, tmp)
str := in[tmp.index[1]:nd.index[0]]
in = in[:i] + str + in[nd.index[1]+1:]
} }
} }
break break
@@ -30,13 +30,13 @@ func Convert(in string) (out []Tag) {
return return
} }
func fndend(fnt Tag, area string) Tag { func fndend(fnt tag, area string) tag {
var count int var count int
for i, v := range area { for i, v := range area {
if v == '<' { if v == '<' {
for j, val := range area[i:] { for j, val := range area[i:] {
if val == '>' { if val == '>' {
var tmp Tag var tmp tag
tmp.process(area[i+1 : i+j+1]) tmp.process(area[i+1 : i+j+1])
if tmp.typ == fnt.typ { if tmp.typ == fnt.typ {
if tmp.end { if tmp.end {
@@ -56,5 +56,5 @@ func fndend(fnt Tag, area string) Tag {
} }
} }
} }
return Tag{} return tag{}
} }
+6 -8
View File
@@ -2,28 +2,26 @@ package appimg
import "strings" import "strings"
//Tag is a bbCode tag that is properly processed. type tag struct {
type Tag struct {
typ string typ string
end bool end bool
params map[string]string params map[string]string
index [2]int index [2]int
Meat string
} }
//Value returns the value of a parameter of the bbCode tag. Starting parameters func (t *tag) value(param string) string {
//is under "starting". Parameter names are always in lowercase
func (t *Tag) Value(param string) string {
return t.params[param] return t.params[param]
} }
func (t *Tag) setValue(param, value string) { func (t *tag) setValue(param, value string) {
if t.params == nil { if t.params == nil {
t.params = make(map[string]string) t.params = make(map[string]string)
} }
t.params[strings.TrimSpace(strings.ToLower(param))] = strings.TrimSpace(value) t.params[strings.TrimSpace(strings.ToLower(param))] = strings.TrimSpace(value)
} }
func (t *Tag) process(bbtag string) { func (t *tag) process(bbtag string) {
if strings.HasPrefix(bbtag, "/") { if strings.HasPrefix(bbtag, "/") {
t.end = true t.end = true
t.typ = strings.ToLower(strings.TrimPrefix(bbtag[:len(bbtag)-1], "/")) t.typ = strings.ToLower(strings.TrimPrefix(bbtag[:len(bbtag)-1], "/"))
@@ -68,7 +66,7 @@ func (t *Tag) process(bbtag string) {
t.processFurther(bbtag) t.processFurther(bbtag)
} }
func (t *Tag) processFurther(further string) { func (t *tag) processFurther(further string) {
further = strings.TrimSpace(further) further = strings.TrimSpace(further)
for i := 0; i < len(further); i++ { for i := 0; i < len(further); i++ {
switch further[i] { switch further[i] {
+13
View File
@@ -0,0 +1,13 @@
package appimg
type appimg struct {
url string
name string
}
func newApp(url, name string) appimg {
var out appimg
out.url = url
out.name = name
return out
}
+65
View File
@@ -0,0 +1,65 @@
package appimg
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/gotk3/gotk3/gtk"
)
func downloadApp(parent *gtk.Window, ap appimg) {
parent.SetSensitive(false)
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.SetTransientFor(parent)
win.Connect("destroy", func() {
parent.SetSensitive(true)
})
spn, _ := gtk.SpinnerNew()
spn.Start()
lbl, _ := gtk.LabelNew("Downloading " + ap.name + "...")
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
box.SetMarginStart(10)
box.SetMarginEnd(10)
box.SetMarginTop(10)
box.SetMarginBottom(10)
box.Add(spn)
box.Add(lbl)
win.Add(box)
win.ShowAll()
win.Show()
go func(win *gtk.Window, ap appimg) {
defer win.Close()
check := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := check.Get(urlBase + ap.url)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
name := strings.Split(ap.name, "-")[0]
var foldName string
if _, err = os.Open("PortableApps/" + name + "Portable"); err == nil {
foldName = "PortableApps/" + name + "Portable"
} else if _, err = os.Open("PortableApps/" + name); err == nil {
foldName = "PortableApps/" + name
} else {
os.Mkdir("PortableApps/"+name, 0777)
foldName = "PortableApps/" + name
}
fil, err := os.Create(foldName + "/" + ap.name)
if err != nil {
fmt.Println(err)
return
}
io.Copy(fil, resp.Body)
_ = fil.Chmod(0777)
}(win, ap)
}
+111
View File
@@ -0,0 +1,111 @@
//Package appimg is for downloading new AppImages for LinuxPA
package appimg
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
const (
urlBase = "https://dl.bintray.com/probono/AppImages/"
)
//ShowUI shows the list of possible AppImages to be downloaded in a gtk.Window
func ShowUI(clsFunc func()) {
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.Connect("destroy", func() {
clsFunc()
})
apps := make([]appimg, 0)
win.SetSizeRequest(400, 400)
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
appList, _ := gtk.ListBoxNew()
apch := make(chan appimg)
appList.SetHExpand(true)
appList.SetVExpand(true)
vScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0)
hScrollCat, _ := gtk.AdjustmentNew(0, 0, 0, 0, 0, 0)
lst, _ := gtk.ScrolledWindowNew(hScrollCat, vScrollCat)
lst.SetSizeRequest(170, 500)
lst.Add(appList)
box.Add(lst)
win.Add(box)
appList.Connect("row-activated", func() {
if appList.GetSelectedRow().GetIndex() >= 0 {
downloadApp(win, apps[appList.GetSelectedRow().GetIndex()])
}
})
win.ShowAll()
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)
}
}(win, apch, appList)
}
func getList(parent *gtk.Window, apch chan appimg) {
win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
win.SetTransientFor(parent)
win.SetDestroyWithParent(true)
win.Connect("destroy", func() {
parent.SetSensitive(true)
})
parent.SetSensitive(false)
spin, _ := gtk.SpinnerNew()
spin.Start()
txt, _ := gtk.LabelNew("Getting List...")
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
box.SetMarginBottom(10)
box.SetMarginEnd(10)
box.SetMarginStart(10)
box.SetMarginTop(10)
box.Add(spin)
box.Add(txt)
win.Add(box)
win.SetPosition(gtk.WIN_POS_CENTER_ON_PARENT)
win.ShowAll()
win.Show()
go func(win *gtk.Window, apch chan appimg) {
check := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := check.Get(urlBase)
if err != nil {
fmt.Println(err)
close(apch)
win.Close()
return
}
defer resp.Body.Close()
btys, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
close(apch)
win.Close()
return
}
tgs := convert(string(btys))
for _, v := range tgs {
if strings.HasSuffix(strings.ToLower(v.Meat), ".appimage") {
apch <- newApp(v.value("href"), v.Meat)
}
}
close(apch)
win.Close()
}(win, apch)
}
+2
View File
@@ -4,6 +4,7 @@ import (
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"os" "os"
"runtime"
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
) )
@@ -24,6 +25,7 @@ var (
) )
func main() { func main() {
runtime.GOMAXPROCS(4)
os.MkdirAll("PortableApps/LinuxPACom", 0777) os.MkdirAll("PortableApps/LinuxPACom", 0777)
master = make(map[string][]app) master = make(map[string][]app)
linmaster = make(map[string][]app) linmaster = make(map[string][]app)
+14 -1
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/CalebQ42/LinuxPA/appimg"
"github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
) )
@@ -18,8 +19,11 @@ func ui(win *gtk.Window) {
settings.Connect("clicked", func() { settings.Connect("clicked", func() {
settingsUI() settingsUI()
}) })
settings.SetTooltipText("Settings (Coming Soon!)") settings.SetTooltipText("Settings")
dnl, _ := gtk.ButtonNewFromIconName("emblem-downloads", gtk.ICON_SIZE_SMALL_TOOLBAR)
dnl.SetTooltipText("Download Apps")
header.PackStart(settings) header.PackStart(settings)
header.PackEnd(dnl)
win.SetTitlebar(header) win.SetTitlebar(header)
topLvl, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) topLvl, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
lrBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5) lrBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
@@ -118,6 +122,15 @@ func ui(win *gtk.Window) {
} }
} }
}) })
dnl.Connect("clicked", func() {
appimg.ShowUI(func() {
store.Clear()
ls = getCatRows()
for _, v := range ls {
catList.Add(v)
}
})
})
} }
func getCatRows() (out []*gtk.Label) { func getCatRows() (out []*gtk.Label) {