-
Notifications
You must be signed in to change notification settings - Fork 80
/
muc_chat_services_component.go
196 lines (161 loc) · 4.84 KB
/
muc_chat_services_component.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
package gui
import (
"github.com/coyim/coyim/xmpp/jid"
"github.com/coyim/gotk3adapter/glibi"
"github.com/coyim/gotk3adapter/gtki"
)
const (
chatServicesModelIDColumn int = iota
chatServicesModelTextColumn
)
type chatServicesComponent struct {
currentAccount *account
services map[int]string
servicesList gtki.ComboBoxText
servicesListModel gtki.ListStore
serviceEntry gtki.Entry
previousUpdateChannel chan bool
}
func (u *gtkUI) createChatServicesComponent(list gtki.ComboBoxText, entry gtki.Entry, onServiceChanged func()) *chatServicesComponent {
c := &chatServicesComponent{
serviceEntry: entry,
services: make(map[int]string),
}
var err error
// The following creates a list store model with two columns
// one for the "ID" and the another for the "text"
c.servicesListModel, err = g.gtk.ListStoreNew(glibi.TYPE_STRING, glibi.TYPE_STRING)
if err != nil {
panic(err)
}
onServiceChangedFinal := onServiceChanged
onServiceChanged = func() {
if onServiceChangedFinal != nil {
onServiceChangedFinal()
}
}
c.servicesList = list
c.servicesList.Connect("changed", onServiceChanged)
c.servicesList.SetModel(c.servicesListModel)
c.servicesList.SetIDColumn(chatServicesModelIDColumn)
c.servicesList.SetEntryTextColumn(chatServicesModelTextColumn)
return c
}
func (c *chatServicesComponent) updateServicesBasedOnAccount(ca *account) {
if c.currentAccount == nil || c.currentAccount.Account() != ca.Account() {
c.currentAccount = ca
if c.previousUpdateChannel != nil {
c.previousUpdateChannel <- true
}
c.previousUpdateChannel = make(chan bool)
csc, ec, endEarly := ca.session.GetChatServices(jid.ParseDomain(ca.Account()))
go c.updateChatServices(ca, csc, ec, endEarly)
}
}
func (c *chatServicesComponent) updateChatServices(ca *account, csc <-chan jid.Domain, ec <-chan error, endEarly func()) {
doInUIThread(c.removeAll)
defer func() {
c.previousUpdateChannel = nil
}()
for {
select {
case <-c.previousUpdateChannel:
doInUIThread(c.removeAll)
endEarly()
return
case err := <-ec:
if err != nil {
ca.log.WithError(err).Error("Something went wrong trying to get the available chat services")
}
return
case cs, ok := <-csc:
if !ok {
return
}
doInUIThread(func() {
ix := c.addService(cs)
if c.currentServiceValue() == "" {
c.setActive(ix)
}
})
}
}
}
// currentServiceValue MUST be called from the UI thread
func (c *chatServicesComponent) currentServiceValue() string {
if currentValue, ok := c.services[c.servicesList.GetActive()]; ok {
return currentValue
}
return getEntryText(c.serviceEntry)
}
// currentService MUST be called from the UI thread
func (c *chatServicesComponent) currentService() jid.Domain {
return jid.ParseDomain(c.currentServiceValue())
}
// setCurrentService MUST be called from the UI thread
func (c *chatServicesComponent) setCurrentService(s jid.Domain) {
ix := c.serviceIndex(s)
if ix == -1 {
ix = c.addService(s)
}
c.setActive(ix)
}
// setActive MUST be called from the UI thread
func (c *chatServicesComponent) setActive(ix int) {
if len(c.services) > 0 && ix < len(c.services) {
c.servicesList.SetActive(ix)
}
}
// addService MUST be called from the UI thread
func (c *chatServicesComponent) addService(s jid.Domain) int {
if ix := c.serviceIndex(s); ix != -1 {
return ix
}
iter := c.servicesListModel.Append()
_ = c.servicesListModel.SetValue(iter, chatServicesModelIDColumn, s.String())
_ = c.servicesListModel.SetValue(iter, chatServicesModelTextColumn, s.String())
ix := len(c.services)
c.services[ix] = s.String()
return ix
}
// removeAll MUST be called from the UI thread
func (c *chatServicesComponent) removeAll() {
c.services = make(map[int]string)
c.servicesListModel.Clear()
}
// clearText MUST be called from the UI thread
func (c *chatServicesComponent) clearText() {
setEntryText(c.serviceEntry, "")
}
// enableServiceInput MUST be called from the UI thread
func (c *chatServicesComponent) enableServiceInput() {
c.servicesList.SetSensitive(true)
}
// disableServiceInput MUST be called from the UI thread
func (c *chatServicesComponent) disableServiceInput() {
c.servicesList.SetSensitive(false)
}
// resetToDefault MUST be called from the UI thread
func (c *chatServicesComponent) resetToDefault() {
c.clearText()
if len(c.services) > 0 {
c.setActive(0)
}
}
// serviceIndex IS SAFE to be called outside the ui thread
func (c *chatServicesComponent) serviceIndex(s jid.Domain) int {
for ix, ss := range c.services {
if ss == s.String() {
return ix
}
}
return -1
}
// hasServiceValue MUST be called from the UI thread
func (c *chatServicesComponent) hasServiceValue() bool {
return c.currentServiceValue() != ""
}
// hasService IS SAFE to be called outside the ui thread
func (c *chatServicesComponent) hasService(s jid.Domain) bool {
return c.serviceIndex(s) != -1
}