From 695ce815d933f28c6b6e3ebbc4efb11eeec7f015 Mon Sep 17 00:00:00 2001 From: Belac Darkstorm Date: Sun, 9 Apr 2017 01:26:31 -0500 Subject: [PATCH] Nearly done with download :) --- appimg/Proc.go | 20 ++++---- appimg/Tag.go | 14 +++--- appimg/appimg.go | 13 ++++++ appimg/download.go | 65 ++++++++++++++++++++++++++ appimg/installui.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 + ui.go | 15 +++++- 7 files changed, 221 insertions(+), 19 deletions(-) create mode 100644 appimg/appimg.go create mode 100644 appimg/download.go create mode 100644 appimg/installui.go diff --git a/appimg/Proc.go b/appimg/Proc.go index d24f28a..d667679 100644 --- a/appimg/Proc.go +++ b/appimg/Proc.go @@ -1,25 +1,25 @@ -//Package appimg is to download and update AppImages for LinuxPA. -//Converted from github.com/CalebQ42/bbConvert package appimg import "reflect" -//Convert converts the input string. Only returns tags -func Convert(in string) (out []Tag) { +func convert(in string) (out []tag) { for i := 0; i < len(in); i++ { v := in[i] if v == '<' { for j := i; j < len(in); j++ { val := in[j] if val == '>' { - var tmp Tag + var tmp tag tmp.process(in[i+1 : j+1]) - if !tmp.end { + if !tmp.end && tmp.typ == "a" { tmp.index[0] = i tmp.index[1] = j 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) + str := in[tmp.index[1]:nd.index[0]] + in = in[:i] + str + in[nd.index[1]+1:] } } break @@ -30,13 +30,13 @@ func Convert(in string) (out []Tag) { return } -func fndend(fnt Tag, area string) Tag { +func fndend(fnt tag, area string) tag { var count int for i, v := range area { if v == '<' { for j, val := range area[i:] { if val == '>' { - var tmp Tag + var tmp tag tmp.process(area[i+1 : i+j+1]) if tmp.typ == fnt.typ { if tmp.end { @@ -56,5 +56,5 @@ func fndend(fnt Tag, area string) Tag { } } } - return Tag{} + return tag{} } diff --git a/appimg/Tag.go b/appimg/Tag.go index 7542e01..7d0a066 100644 --- a/appimg/Tag.go +++ b/appimg/Tag.go @@ -2,28 +2,26 @@ package appimg import "strings" -//Tag is a bbCode tag that is properly processed. -type Tag struct { +type tag struct { typ string end bool params map[string]string index [2]int + Meat string } -//Value returns the value of a parameter of the bbCode tag. Starting parameters -//is under "starting". Parameter names are always in lowercase -func (t *Tag) Value(param string) string { +func (t *tag) value(param string) string { return t.params[param] } -func (t *Tag) setValue(param, value string) { +func (t *tag) setValue(param, value string) { if t.params == nil { t.params = make(map[string]string) } 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, "/") { t.end = true t.typ = strings.ToLower(strings.TrimPrefix(bbtag[:len(bbtag)-1], "/")) @@ -68,7 +66,7 @@ func (t *Tag) process(bbtag string) { t.processFurther(bbtag) } -func (t *Tag) processFurther(further string) { +func (t *tag) processFurther(further string) { further = strings.TrimSpace(further) for i := 0; i < len(further); i++ { switch further[i] { diff --git a/appimg/appimg.go b/appimg/appimg.go new file mode 100644 index 0000000..b02e529 --- /dev/null +++ b/appimg/appimg.go @@ -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 +} diff --git a/appimg/download.go b/appimg/download.go new file mode 100644 index 0000000..39fd36b --- /dev/null +++ b/appimg/download.go @@ -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) +} diff --git a/appimg/installui.go b/appimg/installui.go new file mode 100644 index 0000000..3ccb448 --- /dev/null +++ b/appimg/installui.go @@ -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) +} diff --git a/main.go b/main.go index ff44155..85ebbb5 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/gob" "fmt" "os" + "runtime" "github.com/gotk3/gotk3/gtk" ) @@ -24,6 +25,7 @@ var ( ) func main() { + runtime.GOMAXPROCS(4) os.MkdirAll("PortableApps/LinuxPACom", 0777) master = make(map[string][]app) linmaster = make(map[string][]app) diff --git a/ui.go b/ui.go index 75a3c2e..7bf3ab4 100644 --- a/ui.go +++ b/ui.go @@ -3,6 +3,7 @@ package main import ( "fmt" + "github.com/CalebQ42/LinuxPA/appimg" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" ) @@ -18,8 +19,11 @@ func ui(win *gtk.Window) { settings.Connect("clicked", func() { 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.PackEnd(dnl) win.SetTitlebar(header) topLvl, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) 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) {