Make backend and frontend independent

This commit is contained in:
Xpl0itU 2023-08-29 20:55:43 +02:00
parent 08505c9eec
commit 99597834ce
8 changed files with 148 additions and 123 deletions

View file

@ -32,7 +32,7 @@ type MainWindow struct {
searchEntry *gtk.Entry
deleteEncryptedContentsCheckbox *gtk.CheckButton
addToQueueButton *gtk.Button
progressWindow wiiudownloader.ProgressWindow
progressWindow *ProgressWindow
lastSearchText string
titleQueue []wiiudownloader.TitleEntry
categoryButtons []*gtk.ToggleButton
@ -200,7 +200,7 @@ func (mw *MainWindow) ShowAll() {
mw.logger.Fatal("Unable to create menu item:", err)
}
decryptContentsMenuItem.Connect("activate", func() {
mw.progressWindow, err = wiiudownloader.CreateProgressWindow(mw.window)
mw.progressWindow, err = createProgressWindow(mw.window)
if err != nil {
return
}
@ -260,7 +260,7 @@ func (mw *MainWindow) ShowAll() {
wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), titleID, titleKey, titleVersion)
cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, &mw.progressWindow, grab.NewClient())
cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, grab.NewClient())
if err != nil {
return
}
@ -347,7 +347,7 @@ func (mw *MainWindow) ShowAll() {
if len(mw.titleQueue) == 0 {
return
}
mw.progressWindow, err = wiiudownloader.CreateProgressWindow(mw.window)
mw.progressWindow, err = createProgressWindow(mw.window)
if err != nil {
return
}
@ -483,7 +483,7 @@ func (mw *MainWindow) onCategoryToggled(button *gtk.ToggleButton) {
}
func (mw *MainWindow) onDecryptContentsMenuItemClicked(selectedPath string) error {
err := wiiudownloader.DecryptContents(selectedPath, &mw.progressWindow, false)
err := wiiudownloader.DecryptContents(selectedPath, mw.progressWindow, false)
glib.IdleAdd(func() {
if mw.progressWindow.Window.IsVisible() {
@ -740,7 +740,7 @@ queueProcessingLoop:
errGroup.Go(func() error {
tidStr := fmt.Sprintf("%016x", title.TitleID)
titlePath := filepath.Join(selectedPath, fmt.Sprintf("%s [%s] [%s]", normalizeFilename(title.Name), wiiudownloader.GetFormattedKind(title.TitleID), tidStr))
if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, &mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil {
if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil {
return err
}

View file

@ -0,0 +1,112 @@
package main
import (
"fmt"
"github.com/cavaliergopher/grab/v3"
"github.com/dustin/go-humanize"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type ProgressWindow struct {
Window *gtk.Window
box *gtk.Box
gameLabel *gtk.Label
bar *gtk.ProgressBar
cancelButton *gtk.Button
cancelled bool
}
func (pw *ProgressWindow) SetGameTitle(title string) {
glib.IdleAdd(func() {
pw.gameLabel.SetText(title)
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) UpdateDownloadProgress(resp *grab.Response, filePath string) {
glib.IdleAdd(func() {
pw.bar.SetFraction(resp.Progress())
pw.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(resp.BytesComplete())), humanize.Bytes(uint64(resp.Size())), humanize.Bytes(uint64(resp.BytesPerSecond()))))
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) UpdateDecryptionProgress(progress float64) {
glib.IdleAdd(func() {
pw.bar.SetFraction(progress)
pw.bar.SetText(fmt.Sprintf("Decrypting (%.2f%%)", progress*100))
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) Cancelled() bool {
return pw.cancelled
}
func createProgressWindow(parent *gtk.ApplicationWindow) (*ProgressWindow, error) {
win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
return nil, err
}
win.SetTitle("WiiUDownloader - Downloading")
win.SetTransientFor(parent)
box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
if err != nil {
return nil, err
}
win.Add(box)
gameLabel, err := gtk.LabelNew("")
if err != nil {
return nil, err
}
box.PackStart(gameLabel, false, false, 0)
progressBar, err := gtk.ProgressBarNew()
if err != nil {
return nil, err
}
progressBar.SetShowText(true)
box.PackStart(progressBar, false, false, 0)
cancelButton, err := gtk.ButtonNewWithLabel("Cancel")
if err != nil {
return nil, err
}
bottomhBox, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
if err != nil {
return nil, err
}
bottomhBox.PackEnd(cancelButton, false, false, 0)
box.SetMarginBottom(5)
box.SetMarginEnd(5)
box.SetMarginStart(5)
box.SetMarginTop(5)
box.PackEnd(bottomhBox, false, false, 0)
progressWindow := ProgressWindow{
Window: win,
box: box,
gameLabel: gameLabel,
bar: progressBar,
cancelButton: cancelButton,
cancelled: false,
}
progressWindow.cancelButton.Connect("clicked", func() {
progressWindow.cancelled = true
})
return &progressWindow, nil
}