forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
197 lines (157 loc) · 5.29 KB
/
settings.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package settings
import (
"fmt"
"os"
"github.com/twstrike/coyim/gui/settings/definitions"
"github.com/twstrike/gotk3adapter/glibi"
)
var g glibi.Glib
// InitSettings should be called before using settings
func InitSettings(gx glibi.Glib) {
g = gx
}
var cachedSchema glibi.SettingsSchemaSource
func getSchemaSource() glibi.SettingsSchemaSource {
if cachedSchema == nil {
dir := definitions.SchemaInTempDir()
defer os.Remove(dir)
cachedSchema = g.SettingsSchemaSourceNewFromDirectory(dir, nil, true)
}
return cachedSchema
}
func getSchema() glibi.SettingsSchema {
return getSchemaSource().Lookup("im.coy.coyim.MainSettings", false)
}
func getSettingsFor(s string) glibi.Settings {
return g.SettingsNewFull(getSchema(), nil, fmt.Sprintf("/im/coy/coyim/%s/", s))
}
func getDefaultSettings() glibi.Settings {
return g.SettingsNewFull(getSchema(), nil, "/im/coy/coyim/")
}
// Settings allow access to our configured settings
type Settings struct {
def, spec glibi.Settings
}
// For will return a valid settings instance for the given ident, or the empty string
func For(ident string) *Settings {
s := &Settings{}
s.def = getDefaultSettings()
if ident != "" {
s.spec = getSettingsFor(ident)
}
return s
}
func hasSetStr(k string) string {
return fmt.Sprintf("has-set-%s", k)
}
func hasSet(s glibi.Settings, k string) bool {
return s.GetBoolean(hasSetStr(k))
}
func (s *Settings) settingsForGet(name string) glibi.Settings {
if s.spec != nil && hasSet(s.spec, name) {
return s.spec
}
return s.def
}
func (s *Settings) settingsForSet() glibi.Settings {
if s.spec != nil {
return s.spec
}
return s.def
}
func (s *Settings) getBooleanSetting(name string) bool {
return s.settingsForGet(name).GetBoolean(name)
}
func (s *Settings) setBooleanSetting(name string, val bool) {
sets := s.settingsForSet()
sets.SetBoolean(hasSetStr(name), true)
sets.SetBoolean(name, val)
}
func (s *Settings) getStringSetting(name string) string {
return s.settingsForGet(name).GetString(name)
}
func (s *Settings) setStringSetting(name string, val string) {
sets := s.settingsForSet()
sets.SetBoolean(hasSetStr(name), true)
sets.SetString(name, val)
}
// GetSingleWindow returns the single-window setting
func (s *Settings) GetSingleWindow() bool {
return s.getBooleanSetting("single-window")
}
// SetSingleWindow sets the single-window setting
func (s *Settings) SetSingleWindow(val bool) {
s.setBooleanSetting("single-window", val)
}
// GetNotificationUrgency returns the notification-urgency setting
func (s *Settings) GetNotificationUrgency() bool {
return s.getBooleanSetting("notification-urgency")
}
// SetNotificationUrgency sets the notification-urgency setting
func (s *Settings) SetNotificationUrgency(val bool) {
s.setBooleanSetting("notification-urgency", val)
}
// GetNotificationExpires returns the notification-expires setting
func (s *Settings) GetNotificationExpires() bool {
return s.getBooleanSetting("notification-expires")
}
// SetNotificationExpires sets the notification-expires setting
func (s *Settings) SetNotificationExpires(val bool) {
s.setBooleanSetting("notification-expires", val)
}
// GetNotificationStyle returns the notification-style setting
func (s *Settings) GetNotificationStyle() string {
return s.getStringSetting("notification-style")
}
// SetNotificationStyle sets the notification-style setting
func (s *Settings) SetNotificationStyle(val string) {
s.setStringSetting("notification-style", val)
}
// GetShiftEnterForSend returns the shift-enter-for-send setting
func (s *Settings) GetShiftEnterForSend() bool {
return s.getBooleanSetting("shift-enter-for-send")
}
// SetShiftEnterForSend sets the shift-enter-for-send setting
func (s *Settings) SetShiftEnterForSend(val bool) {
s.setBooleanSetting("shift-enter-for-send", val)
}
// GetEmacsKeyBindings returns the emacs-keyboard setting
func (s *Settings) GetEmacsKeyBindings() bool {
return s.getBooleanSetting("emacs-keyboard")
}
// SetEmacsKeyBindings sets the emacs-keyboard setting
func (s *Settings) SetEmacsKeyBindings(val bool) {
s.setBooleanSetting("emacs-keyboard", val)
}
// GetShowEmptyGroups returns the show-empty-groups setting
func (s *Settings) GetShowEmptyGroups() bool {
return s.getBooleanSetting("show-empty-groups")
}
// SetShowEmptyGroups sets the show-empty-groups setting
func (s *Settings) SetShowEmptyGroups(val bool) {
s.setBooleanSetting("show-empty-groups", val)
}
// GetShowAdvancedOptions returns the show-advanced-options setting
func (s *Settings) GetShowAdvancedOptions() bool {
return s.getBooleanSetting("show-advanced-options")
}
// SetShowAdvancedOptions sets the show-advanced-options setting
func (s *Settings) SetShowAdvancedOptions(val bool) {
s.setBooleanSetting("show-advanced-options", val)
}
// GetConnectAutomatically returns the connect-automatically setting
func (s *Settings) GetConnectAutomatically() bool {
return s.getBooleanSetting("connect-automatically")
}
// SetConnectAutomatically sets the connect-automatically setting
func (s *Settings) SetConnectAutomatically(val bool) {
s.setBooleanSetting("connect-automatically", val)
}
// GetCollapsed returns the collapsed setting
func (s *Settings) GetCollapsed() string {
return s.getStringSetting("collapsed")
}
// SetCollapsed sets the collapsed setting
func (s *Settings) SetCollapsed(val string) {
s.setStringSetting("collapsed", val)
}