-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_handler.go
More file actions
138 lines (122 loc) · 3.72 KB
/
event_handler.go
File metadata and controls
138 lines (122 loc) · 3.72 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"github.com/gen2brain/iup-go/iup"
"github.com/hugolgst/rich-go/client"
log "github.com/sirupsen/logrus"
lfm "github.com/twangodev/lfm-api"
"strings"
"time"
)
func promptUsername() int {
dlg := iup.Dialog(
iup.Vbox(
iup.Hbox(
iup.Fill(),
iup.Label("Username:"),
iup.Text().SetAttributes(`CONFIG="`+config.username+`", SIZE=75x`).SetHandle("username"),
iup.Fill(),
),
iup.Hbox(
iup.Fill(),
iup.Button("OK").SetAttribute("SIZE", "40x").
SetCallback("ACTION", iup.ActionFunc(func(ih iup.Ihandle) int {
rawUsername := iup.GetHandle("username").GetAttribute("VALUE")
trimmed := strings.TrimSpace(rawUsername)
if trimmed == "" {
log.Error("Username cannot be empty")
go iup.Message("Error", "Username is empty")
return iup.CLOSE
}
if strings.Contains(trimmed, " ") {
log.Error("Username contains spaces, skipping update and sending message")
go iup.Message("Invalid Username", "Username cannot contain spaces")
return iup.DEFAULT
}
config.username = trimmed
log.Info("Username updated to: ", trimmed)
go updatePresence(true)
return iup.CLOSE
})),
iup.Button("Cancel").SetAttribute("SIZE", "40x").SetCallback("ACTION", iup.ActionFunc(exit)),
iup.Fill(),
),
).SetAttributes(`MARGIN=4x5, GAP=5`),
)
iup.Popup(dlg, iup.CENTER, iup.CENTER)
return iup.DEFAULT
}
func safeSetActivity(activity client.Activity) {
logContext := generateLogContext("safeSetActivity").WithField("activity", activity)
defer func() {
if r := recover(); r != nil {
logContext.WithField("panic", r).Error("Panic while updating presence. Restart recommended")
}
}()
err := client.SetActivity(activity)
if err != nil {
logContext.Error("Presence failed to update")
} else {
logContext.Info("Presence updated")
updateRpcPreview(activity)
}
}
func updatePresenceReceiver(force bool) {
logContext := generateLogContext("updatePresenceReceiver").WithFields(log.Fields{
"force": force, "username": config.username, "state": config.state,
})
if !config.state {
logContext.Debug("State is disabled, skipping update")
return
}
if config.username == "" {
logContext.Warn("No username set, skipping presence update")
return
}
currentScrobble, err := lfm.GetActiveScrobble(config.username)
logContext.WithFields(log.Fields{
"scrobble": currentScrobble,
"active": currentScrobble.Active,
}).Debugln("Received scrobble")
logContext = logContext.WithField("currentScrobble", currentScrobble)
if err != nil {
logContext.Warnln("Failed to get active scrobble:", err)
return
}
if currentScrobble.Active {
if !loggedIn {
logContext.Info("New scrobble detected. Logging in")
login()
}
} else {
if loggedIn {
logContext.Info("No scrobble detected. Logging out")
logout()
updateRpcPreviewImage("") // Reset image to default
} else { // Retain logout state
logContext.Trace("No new scrobble detected")
}
return
}
if timestamp != currentScrobble.DataTimestamp || force { // Update old scrobble to match current scrobble
timestamp = currentScrobble.DataTimestamp
logContext.Debug("Updating presence")
} else {
return
}
baseActivity := createActivity(currentScrobble, false)
safeSetActivity(baseActivity)
buttonedActivity := createActivity(currentScrobble, true)
safeSetActivity(buttonedActivity)
}
func elapsedTimeReceiver() {
elapsed := time.Since(timestamp)
minutes := int(elapsed.Minutes())
seconds := int(elapsed.Seconds()) % 60
generateLogContext("elapsedTimeReceiver").WithFields(log.Fields{
"minutes": minutes, "seconds": seconds},
).Trace("Updating elapsed time")
if elapsed < time.Second {
return
}
go updateRpcPreviewElapsed(minutes, seconds)
}