Sonically/sonically.go

111 lines
2.4 KiB
Go

package main
import (
// "net/http"
"os"
"path/filepath"
"runtime"
"github.com/TheCreeper/go-notify"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
"github.com/hashicorp/go-retryablehttp"
// "github.com/diamondburned/gotk4/pkg/core/glib"
// "github.com/hashicorp/go-retryablehttp"
)
// Widely used, conditionally created objects
var ifDelete bool = true
var delDir string
var settingsPath string = (GetRunDir() + MainDir + SettingsDir + "/gob")
// var mainWaitGroup sync.WaitGroup
// This is where app starts
func main() {
app := gtk.NewApplication("eu.itmodulo.sonically", 0)
app.ConnectActivate(func() { activate(app) })
if code := app.Run(os.Args); code > 0 {
os.Exit(code)
}
}
// This is func that initates GUI, deserializez data
func activate(app *gtk.Application) {
AudioControlChan = make(chan bool) // Initate goroutine channel
MainWindow = gtk.NewApplicationWindow(app)
MainWindow.SetTitle("Sonically")
SetUpGui()
x := retryablehttp.NewClient()
x.RetryMax = 15
x.Logger = nil
y := retryablehttp.NewClient()
y.RetryMax = 20
y.Logger = nil
// All widgets put together
HttpSettings = x.StandardClient() // Default client for connection
MetadataClient = y.StandardClient() // Default client for connection
Init() // Set up account
MainWindow.ConnectCloseRequest(closeApp)
MainWindow.Resizable()
MainWindow.Show()
// Background Read User Library, to speedup later loading
if MainSettings.Bitrate != 0 {
go DownloadLib()
go DownloadPlaylist()
}
}
func closeApp() bool {
if CurrentCache != nil {
CurrentCache.Clean()
}
if MainSettings.DeleteOnClose {
delDir = GetRunDir() + MainDir + DataDir + "/"
d, err := os.Open(delDir)
if err != nil {
PanicOnError(err)
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
PanicOnError(err)
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(delDir, name))
if err != nil {
PanicOnError(err)
}
}
}
delDir = GetRunDir() + MainDir + InfDir + "/"
d, err := os.Open(delDir)
if err != nil {
PanicOnError(err)
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
PanicOnError(err)
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(delDir, name))
if err != nil {
PanicOnError(err)
}
}
notify.CloseNotification(MainNotification.ReplacesID)
runtime.GC()
return false
}