diff options
-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 +} |