diff options
author | xengineering <me@xengineering.eu> | 2023-05-20 19:19:19 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-20 19:19:45 +0200 |
commit | 6713c3cc833906802acf3ef89e5ac3dd7f69e128 (patch) | |
tree | d1bb8ff102f4aaeab623c69982098ccc434df81d | |
parent | 7b33036f9b4ea4ed91f60481a9eed30b8188167c (diff) | |
download | limox-6713c3cc833906802acf3ef89e5ac3dd7f69e128.tar limox-6713c3cc833906802acf3ef89e5ac3dd7f69e128.tar.zst limox-6713c3cc833906802acf3ef89e5ac3dd7f69e128.zip |
Add persistence for last used JID
-rw-r--r-- | limox.go | 10 | ||||
-rw-r--r-- | persistence.go | 55 |
2 files changed, 63 insertions, 2 deletions
@@ -34,7 +34,7 @@ type Limox struct { } func NewLimox() Limox { - return Limox{ + l := Limox{ Window: app.NewWindow( app.Title("LimoX"), app.Size(unit.Dp(400), unit.Dp(600)), @@ -44,6 +44,10 @@ func NewLimox() Limox { XmppConn: make(chan xmpp.Event), State: Disconnected, } + + l.JidEditor.SetText(getLastJid()) + + return l } func (l *Limox) run() error { @@ -77,7 +81,9 @@ func (l *Limox) buttonCallback() { switch l.State { case Disconnected: log.Println("Starting connection establishment ...") - go xmpp.Run(l.XmppConn, l.JidEditor.Text(), l.PwdEditor.Text()) + jid := l.JidEditor.Text() + setLastJid(jid) + go xmpp.Run(l.XmppConn, jid, l.PwdEditor.Text()) l.State = Connecting case Connecting: log.Println("Aborted connection establishment") diff --git a/persistence.go b/persistence.go new file mode 100644 index 0000000..5199fc5 --- /dev/null +++ b/persistence.go @@ -0,0 +1,55 @@ +package main + +import ( + "io/ioutil" + "os" + "path/filepath" +) + +const ( + RelDataPath = `/.config/limox` + RelJidPath = `/last_jid.txt` +) + +func setLastJid(j string) { + data, err := assertDatadir() + if err != nil { + return + } + + path := filepath.Join(data, RelJidPath) + + _ = ioutil.WriteFile(path, []byte(j), 0644) +} + +func getLastJid() string { + data, err := assertDatadir() + if err != nil { + return "" + } + + path := filepath.Join(data, RelJidPath) + + content, err := ioutil.ReadFile(path) + if err != nil { + return "" + } + + return string(content) +} + +func assertDatadir() (string, error) { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + + path := filepath.Join(home, RelDataPath) + + err = os.MkdirAll(path, 0755) + if err != nil { + return "", err + } + + return path, nil +} |