~f4814n/frost

8db62c04508c238d307cee6c40f2a8891ee6ea56 — Fabian Geiselhart 11 months ago 7c0d757
Create linux Platform
6 files changed, 85 insertions(+), 100 deletions(-)

D appdirs/appdirs.go
D appdirs/appdirs_darwin.go
D appdirs/appdirs_linux.go
D appdirs/appdirs_windows.go
M main.go
A platform_linux.go
D appdirs/appdirs.go => appdirs/appdirs.go +0 -15
@@ 1,15 0,0 @@
// +build !linux,!windows,!darwin

package appdirs

func UserDataDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

func UserCacheDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

func UserConfigDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

D appdirs/appdirs_darwin.go => appdirs/appdirs_darwin.go +0 -0
D appdirs/appdirs_linux.go => appdirs/appdirs_linux.go +0 -32
@@ 1,32 0,0 @@
package appdirs

import (
	"os"
	"fmt"
)

func getenv(key, def string) (ret string) {
	if val, ok := os.LookupEnv(key); ok {
		ret = val
	} else {
		ret = def
	}

	if ret[0] == '~' {
		return os.Getenv("HOME") + ret[1:]
	}

	return ret
}

func UserDataDir(appname, appauthor string) string {
	return fmt.Sprintf("%s/%s", getenv("XDG_DATA_HOME", "~/.local/share"), appname)
}

func UserCacheDir(appname, appauthor string) string {
	return fmt.Sprintf("%s/%s", getenv("XDG_CACHE_HOME", "~/.cache"), appname)
}

func UserConfigDir(appname, appauthor string) string {
	return fmt.Sprintf("%s/%s", getenv("XDG_CONFIG_HOME", "~/.config"), appname)
}

D appdirs/appdirs_windows.go => appdirs/appdirs_windows.go +0 -13
@@ 1,13 0,0 @@
package appdirs

func UserDataDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

func UserCacheDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

func UserConfigDir(appname, appauthor string) string {
	panic("Unsupported platform")
}

M main.go => main.go +21 -40
@@ 1,8 1,7 @@
package main

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

	"gioui.org/app"


@@ 13,15 12,9 @@ import (
	"gioui.org/unit"
	"gioui.org/widget/material"
	"git.sr.ht/~f4814n/frost/matrix"
	backend "git.sr.ht/~f4814n/frost/matrix/backend/sqlite"
	"git.sr.ht/~f4814n/frost/appdirs"
	log "github.com/sirupsen/logrus"
)

var (
	configFilePath = appdirs.UserDataDir("frost", "f4814n") + "/config.json"
)

var theme *material.Theme

type Gtx = layout.Context


@@ 49,20 42,23 @@ type App struct {
	rx, tx chan Event
	page   Page
	config AppConfig
	platform Platform
}

func newApp() *App {
	platform := InitPlatform()
	return &App{
		rx: make(chan Event, 10),
		tx: make(chan Event, 10),
		cli: matrix.NewClient(matrix.ClientOpts{
			HTTPClient: http.DefaultClient,
			Backend:    backend.New(appdirs.UserDataDir("frost", "f4814n")),
			Backend: platform.NewBackend(),
		}),
		window: app.NewWindow(
			app.Size(unit.Dp(400), unit.Dp(800)),
			app.Title("Frost"),
		),
		platform: platform,
	}
}



@@ 73,7 69,11 @@ func (a *App) run() error {
			switch e := e.(type) {
			case system.StageEvent:
				theme = material.NewTheme(gofont.Collection())
				a.loadConfig()
				var err error
				a.config, err = a.platform.LoadConfig()
				if err != nil {
					log.WithError(err).Warn("platform: Failed to load config")
				}
				page := a.initialPage()
				a.page = page
				page.Start(a.tx, a.rx)


@@ 95,13 95,20 @@ func (a *App) run() error {
					log.Info("Logging in")
					err := a.cli.Login(e.Username, e.Password)
					if err != nil {
						log.Info("Failed to log in")
						log.WithError(err).Info("Failed to log in")
						a.tx <- LoginErrorEvent{Err: err}
						return
					}
					log.Info("Successfully authenticated")
					a.config.Session = &SessionConfig{MxID: a.cli.MxID(), DeviceID: a.cli.DeviceID()}
					a.flushConfig()

					a.config.Session = &SessionConfig{
						MxID: a.cli.MxID(),
						DeviceID: a.cli.DeviceID(),
					}
					err = a.platform.FlushConfig(a.config)
					if err != nil {
						log.WithError(err).Warn("platform: Failed to flush config")
					}

					a.page.Stop()
					a.page = NewOverviewPage(a.cli)


@@ 110,38 117,12 @@ func (a *App) run() error {
			case InvalidationEvent:
				a.window.Invalidate()
			default:
				log.Warnf("%#v", e)
				log.WithField("event", fmt.Sprintf("%#v", e)).Warn("Unhandled event")
			}
		}
	}
}

func (a *App) loadConfig() {
	f, err := os.Open(configFilePath)
	if err != nil {
		log.WithField("error", err).Error("Failed to open config file")
		return
	}

	err = json.NewDecoder(f).Decode(&a.config)
	if err != nil {
		// XXX Better error communication
		log.WithField("error", err).Error("Failed to parse config file")
	}
}

func (a *App) flushConfig() {
	f, err := os.Create(configFilePath)
	if err != nil {
		log.WithField("error", err).Error("Failed to flush config file")
	}

	err = json.NewEncoder(f).Encode(a.config)
	if err != nil {
		log.WithField("error", err).Error("Failed to encode configuration")
	}
}

func (a *App) initialPage() Page {
	if a.config.Session != nil {
		err := a.cli.LoadToken(a.config.Session.MxID, a.config.Session.DeviceID)

A platform_linux.go => platform_linux.go +64 -0
@@ 0,0 1,64 @@
package main

import (
	"os"
	"fmt"
	"encoding/json"
	"git.sr.ht/~f4814n/frost/matrix"
	backend "git.sr.ht/~f4814n/frost/matrix/backend/sqlite"
)

var (
	dataDir = fmt.Sprintf("%s/frost", getenv("XDG_DATA_HOME", "~/.local/share"))
	configFilePath = dataDir + "/config.json"
)

type Platform struct {
}

func InitPlatform() Platform {
	// Create b.Path directory
	if _, err := os.Stat(dataDir); os.IsNotExist(err) {
		os.Mkdir(dataDir, os.ModePerm) // XXX Correct permissions
	}

	return Platform{}
}

func (p Platform) LoadConfig() (cfg AppConfig, err error) {
	f, err := os.Open(configFilePath)
	if err != nil {
		return
	}

	err = json.NewDecoder(f).Decode(&cfg)
	return
}

func (p Platform) FlushConfig(cfg AppConfig) (err error) {
	f, err := os.Create(configFilePath)
	if err != nil {
		return
	}

	err = json.NewEncoder(f).Encode(cfg)
	return
}

func (p Platform) NewBackend() matrix.Backend {
	return backend.New(dataDir)
}

func getenv(key, fallback string) (res string) {
	if val, ok := os.LookupEnv(key); ok {
		res = val
	} else {
		res = fallback
	}

	if res[0] == '~' {
		res = os.Getenv("HOME") + res[1:]
	}

	return
}