aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: ad0d7c84e0aaf63547b80463760024ea4e0588fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/delucks/go-subsonic"
	"github.com/rivo/tview"
)

var (
	url      = ""
	username = ""
	password = ""

	sub *subsonic.Client = nil

	// GUI
	app   *tview.Application
	pages *tview.Pages
)

func main() {
	loadConfig()

	app = tview.NewApplication()

	pages = tview.NewPages()

	showConfig := sub == nil
	pages.AddPage("page-config", configPage(), true, showConfig)

	if !showConfig {
		pages.AddPage("page-main", mainView(), true, true)
	}

	if err := app.SetRoot(pages, true).EnableMouse(true).SetFocus(pages).Run(); err != nil {
		fmt.Printf("Error running TermSonic: %v", err)
		os.Exit(1)
	}
}

func configPage() *tview.Form {
	form := tview.NewForm().
		AddInputField("Server URL", url, 40, nil, func(txt string) { url = txt }).
		AddInputField("Username", username, 20, nil, func(txt string) { username = txt }).
		AddPasswordField("Password", password, 20, '*', func(txt string) { password = txt }).
		AddButton("Test", func() {
			tmpSub := &subsonic.Client{
				Client:       http.DefaultClient,
				BaseUrl:      url,
				User:         username,
				ClientName:   "termsonic",
				PasswordAuth: true,
			}

			if err := tmpSub.Authenticate(password); err != nil {
				alert("Could not auth: %v", err)
			} else {
				sub = tmpSub

				alert("Success.")
			}
		}).
		AddButton("Save", nil)
	return form
}

func mainView() *tview.Grid {
	grid := tview.NewGrid().
		SetRows(2, 0).
		SetColumns(30, 0).
		SetBorders(true)

	grid.AddItem(tview.NewTextView().SetText("Artist & Album list"), 1, 0, 1, 1, 0, 0, true)
	grid.AddItem(tview.NewTextView().SetText("Song list!"), 1, 1, 1, 2, 0, 0, false)

	return grid
}

func alert(format string, params ...interface{}) {
	modal := tview.NewModal().
		SetText(fmt.Sprintf(format, params...)).
		AddButtons([]string{"OK"}).
		SetDoneFunc(func(_ int, _ string) {
			pages.RemovePage("alert")
		})

	pages.AddPage("alert", modal, true, true)
}

func loadConfig() {
	url = "http://music.nuc.local"
	username = "admin"
	password = "admin"

	sub = &subsonic.Client{
		Client:       http.DefaultClient,
		User:         username,
		BaseUrl:      url,
		ClientName:   "termsonic",
		PasswordAuth: true,
	}

	if err := sub.Authenticate(password); err != nil {
		sub = nil
	}
}

func saveConfig() {
}