summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorRuben Pollan <meskio@sindominio.net>2018-02-08 21:41:31 +0100
committerRuben Pollan <meskio@sindominio.net>2018-02-08 21:41:31 +0100
commita846cb2a424fd17d43e3edf885cca7d79820fa9f (patch)
tree8b22a77ee70f87516fb11712b3597e238a2c7943 /config.go
parentb32bcc834620a5c30ab4d7f7b8dde9ffdafd348f (diff)
[feat] use a config file to check when to produce the next notification
Diffstat (limited to 'config.go')
-rw-r--r--config.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..0a769aa
--- /dev/null
+++ b/config.go
@@ -0,0 +1,74 @@
+package main
+
+import (
+ "encoding/json"
+ "log"
+ "os"
+ "path"
+ "time"
+
+ "0xacab.org/leap/bitmask-systray/bitmask"
+)
+
+const (
+ oneDay = time.Hour * 24
+ oneMonth = oneDay * 30
+)
+
+var (
+ configPath = path.Join(bitmask.ConfigPath, "systray.json")
+)
+
+type systrayConfig struct {
+ LastNotification time.Time
+ Donated time.Time
+}
+
+func parseConfig() (*systrayConfig, error) {
+ var conf systrayConfig
+
+ f, err := os.Open(configPath)
+ if os.IsNotExist(err) {
+ return &conf, nil
+ }
+ if err != nil {
+ return nil, err
+ }
+ defer f.Close()
+
+ dec := json.NewDecoder(f)
+ err = dec.Decode(&conf)
+ return &conf, err
+}
+
+func (c *systrayConfig) hasDonated() bool {
+ log.Println("has donated ", c.Donated.Add(oneMonth))
+ return c.Donated.Add(oneMonth).After(time.Now())
+}
+
+func (c *systrayConfig) needsNotification() bool {
+ log.Println("needs ", c.LastNotification.Add(oneDay))
+ log.Println(!c.hasDonated() && c.LastNotification.Add(oneDay).Before(time.Now()))
+ return !c.hasDonated() && c.LastNotification.Add(oneDay).Before(time.Now())
+}
+
+func (c *systrayConfig) setNotification() error {
+ c.LastNotification = time.Now()
+ return c.save()
+}
+
+func (c *systrayConfig) setDonated() error {
+ c.Donated = time.Now()
+ return c.save()
+}
+
+func (c *systrayConfig) save() error {
+ f, err := os.Create(configPath)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+
+ enc := json.NewEncoder(f)
+ return enc.Encode(c)
+}