-
Notifications
You must be signed in to change notification settings - Fork 80
/
conversation_view_owner.go
385 lines (308 loc) · 10.7 KB
/
conversation_view_owner.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package gui
import (
"fmt"
rosters "github.com/coyim/coyim/roster"
"github.com/coyim/coyim/xmpp/jid"
"github.com/coyim/gotk3adapter/gtki"
)
type conversationViewFactory interface {
OpenConversationView(userInitiated bool) conversationView
IfConversationView(whenExists func(conversationView), whenDoesntExist func())
}
type ourConversationViewFactory struct {
account *account
peer jid.Any
ui *gtkUI
ul *unifiedLayout
targeted bool
}
func (u *gtkUI) NewConversationViewFactory(account *account, peer jid.Any, targeted bool) conversationViewFactory {
return &ourConversationViewFactory{
ui: u,
ul: u.unified,
account: account,
peer: peer,
targeted: targeted,
}
}
func (cvf *ourConversationViewFactory) OpenConversationView(userInitiated bool) conversationView {
// fmt.Printf("OpenConversationView(peer=%s, user=%v, targeted=%v)\n", cvf.peer, userInitiated, cvf.targeted)
c, ok := cvf.getConversationViewSafely()
if !ok {
c = cvf.createConversationView(nil)
}
c.show(userInitiated)
return c
}
func (cvf *ourConversationViewFactory) IfConversationView(whenExists func(conversationView), whenDoesntExist func()) {
// fmt.Printf("IfConversationView(peer=%s)\n", cvf.peer)
c, ok := cvf.getConversationViewSafely()
// fmt.Printf(" IfConversationView(peer=%s) ok=%v\n", cvf.peer, ok)
if ok {
whenExists(c)
} else {
whenDoesntExist()
}
}
func (cvf *ourConversationViewFactory) createConversationView(existing *conversationPane) conversationView {
// fmt.Printf("createConversationView(peer=%s, targeted=%v)\n", cvf.peer, cvf.targeted)
var cv conversationView
if cvf.ui.settings.GetSingleWindow() {
cv = cvf.createUnifiedConversationView(existing)
} else {
cv = cvf.createWindowedConversationView(existing)
}
cvf.setConversationView(cv)
return cv
}
func (cvf *ourConversationViewFactory) potentialTarget() string {
p := cvf.peer.PotentialResource().String()
if cvf.targeted && p != "" {
return fmt.Sprintf(" (%s)", p)
}
return ""
}
func windowConversationTitle(ui *gtkUI, peer jid.Any, account *account, potentialTarget string) string {
// TODO: Can we put the security rating here, maybe?
otherName := peer.String()
if p, ok := ui.accountManager.getPeer(account, peer.NoResource()); ok {
otherName = p.NameForPresentation()
}
return fmt.Sprintf("%s%s (%s)", otherName, potentialTarget, account.session.DisplayName())
}
func (cvf *ourConversationViewFactory) recreateWindowOn(conv *conversationWindow) {
cp := conv.conversationPane
builder := newBuilder("Conversation")
win := builder.getObj("conversation").(gtki.Window)
win.SetApplication(cvf.ui.app)
win.SetTitle(windowConversationTitle(cvf.ui, cvf.peer, cvf.account, cvf.potentialTarget()))
winBox := builder.getObj("box").(gtki.Box)
cp.menubar.Show()
winBox.PackStart(cp.widget, true, true, 0)
conv.win = win
cp.connectEnterHandler(conv.win)
cvf.setWindowEvents(conv, winBox)
cvf.ui.connectShortcutsChildWindow(conv.win)
cvf.ui.connectShortcutsConversationWindow(conv)
cp.transientParent = win
// This 115 is apparently for the letter "s"
win.AddMnemonic(uint(115), cp.encryptedLabel)
}
func (cvf *ourConversationViewFactory) setWindowEvents(conv *conversationWindow, winBox gtki.Box) {
cp := conv.conversationPane
inEventHandler := false
_, _ = conv.win.Connect("set-focus", func() {
if !inEventHandler {
inEventHandler = true
conv.entry.GrabFocus()
inEventHandler = false
}
})
_, _ = conv.win.Connect("focus-in-event", func() {
conv.unsetUrgent()
})
_, _ = conv.win.Connect("delete-event", func() {
winBox.Remove(cp.widget)
conv.win = nil
})
_, _ = conv.win.Connect("notify::is-active", func() {
if conv.win.IsActive() {
inEventHandler = true
conv.entry.GrabFocus()
inEventHandler = false
}
})
_, _ = conv.win.Connect("hide", func() {
conv.onHide()
})
_, _ = conv.win.Connect("show", func() {
conv.onShow()
})
}
func (cvf *ourConversationViewFactory) createWindowedConversationView(existing *conversationPane) *conversationWindow {
// fmt.Printf("createWindowedConversationView(peer=%s, targeted=%v)\n", cvf.peer, cvf.targeted)
builder := newBuilder("Conversation")
win := builder.getObj("conversation").(gtki.Window)
win.SetApplication(cvf.ui.app)
win.SetTitle(windowConversationTitle(cvf.ui, cvf.peer, cvf.account, cvf.potentialTarget()))
winBox := builder.getObj("box").(gtki.Box)
cp := cvf.createConversationPane(win)
if existing != nil {
b, _ := existing.history.GetBuffer()
cp.history.SetBuffer(b)
}
cp.menubar.Show()
winBox.PackStart(cp.widget, true, true, 0)
conv := &conversationWindow{
conversationPane: cp,
win: win,
}
cp.connectEnterHandler(conv.win)
cp.afterNewMessage = conv.potentiallySetUrgent
cvf.setWindowEvents(conv, winBox)
cvf.ui.connectShortcutsChildWindow(conv.win)
cvf.ui.connectShortcutsConversationWindow(conv)
conv.parentWin = cvf.ui.window
return conv
}
func (cvf *ourConversationViewFactory) createUnifiedConversationView(existing *conversationPane) conversationView {
// fmt.Printf("createUnifiedConversationView(peer=%s, targeted=%v)\n", cvf.peer, cvf.targeted)
cp := cvf.createConversationPane(cvf.ui.window)
if existing != nil {
b, _ := existing.history.GetBuffer()
cp.history.SetBuffer(b)
}
cp.connectEnterHandler(nil)
idx := cvf.ul.notebook.AppendPage(cp.widget, nil)
if idx < 0 {
panic("Failed to append page to notebook")
}
csi := &conversationStackItem{
conversationPane: cp,
pageIndex: idx,
layout: cvf.ul,
}
// csi.entry.SetHasFrame(true)
csi.entryScroll.SetMarginTop(5)
csi.entryScroll.SetMarginBottom(5)
tabLabel := csi.shortName()
resource := cvf.peer.PotentialResource().String()
if resource != "" {
tabLabel = tabLabel + " [at] " + resource
}
cvf.ul.notebook.SetTabLabelText(cp.widget, tabLabel)
cvf.ul.itemMap[idx] = csi
buffer, _ := csi.history.GetBuffer()
_, _ = buffer.Connect("changed", func() {
cvf.ul.onConversationChanged(csi)
})
return csi
}
func (cvf *ourConversationViewFactory) createConversationPane(win gtki.Window) *conversationPane {
// fmt.Printf("createConversationPane(peer=%s, targeted=%v)\n", cvf.peer, cvf.targeted)
builder := newBuilder("ConversationPane")
var target jid.Any = cvf.peer.NoResource()
if cvf.targeted {
target = cvf.peer.(jid.WithResource)
}
cp := &conversationPane{
isTargeted: cvf.targeted,
target: target,
otrLock: nil,
account: cvf.account,
fileTransferNotif: builder.fileTransferNotifInit(),
securityWarningNotif: builder.securityWarningNotifInit(),
transientParent: win,
shiftEnterSends: cvf.ui.settings.GetShiftEnterForSend(),
afterNewMessage: func() {},
delayed: make(map[int]sentMessage),
currentPeer: func() (*rosters.Peer, bool) {
p, ok := cvf.ui.getPeer(cvf.account, cvf.peer.NoResource())
if !ok {
cvf.ui.log.WithField("peer", cvf.peer.NoResource().String()).Warn("Failure to look up peer from account")
}
return p, ok
},
}
panicOnDevError(builder.bindObjects(cp))
builder.ConnectSignals(map[string]interface{}{
"on_start_otr": cp.onStartOtrSignal,
"on_end_otr": cp.onEndOtrSignal,
"on_verify_fp": cp.onVerifyFpSignal,
"on_connect": cp.onConnect,
"on_disconnect": cp.onDisconnect,
"on_destroy_file_transfer": cp.onDestroyFileTransferNotif,
"on_send_file_to_contact": func() {
cvf.account.sendFileTo(cp.currentPeerForSending(), cvf.ui, cp)
},
"on_send_dir_to_contact": func() {
cvf.account.sendDirectoryTo(cp.currentPeerForSending(), cvf.ui, cp)
},
})
// This 115 is apparently for the letter "s"
win.AddMnemonic(uint(115), cp.encryptedLabel)
_ = cp.entryScroll.SetProperty("height-request", cp.calculateHeight(1))
prov := providerWithCSS("scrolledwindow { border-top: 2px solid #d3d3d3; } ")
updateWithStyle(cp.entryScroll, prov)
cp.history.SetBuffer(cvf.ui.getTags().createTextBuffer())
_, _ = cp.history.Connect("size-allocate", func() {
scrollToBottom(cp.scrollHistory)
})
cp.pending.SetBuffer(cvf.ui.getTags().createTextBuffer())
_, _ = cp.entry.Connect("key-release-event", cp.doPotentialEntryResize)
cvf.ui.displaySettings.control(cp.history)
cvf.ui.displaySettings.shadeBackground(cp.pending)
cvf.ui.displaySettings.control(cp.entry)
cvf.ui.keyboardSettings.control(cp.entry)
cvf.ui.keyboardSettings.update()
cp.verifier = newVerifier(cvf.ui, cp)
cp.encryptionStatus = &encryptionStatus{}
return cp
}
func (cvf *ourConversationViewFactory) setConversationView(c conversationView) {
// fmt.Printf("setConversationView(peer=%s)\n", cvf.peer)
defer cvf.account.executeDelayed(cvf.ui, cvf.peer, cvf.targeted)
cvf.account.Lock()
defer cvf.account.Unlock()
if cold, ok := cvf.account.c[c.getTarget().String()]; ok {
cold.destroy()
}
// fmt.Printf("setConversationView(target=%s)\n", c.getTarget())
cvf.account.c[c.getTarget().String()] = c
}
func (cvf *ourConversationViewFactory) isWindowingStyleConsistent(c conversationView) bool {
unifiedLayout := cvf.ui.settings.GetSingleWindow()
_, windowUnifiedLayout := c.(*conversationStackItem)
return unifiedLayout == windowUnifiedLayout
}
func (cvf *ourConversationViewFactory) getConversationViewSafely() (conversationView, bool) {
// fmt.Printf("getConversationViewSafely(peer=%s)\n", cvf.peer)
c, ok := cvf.basicGetConversationView()
// fmt.Printf(" getConversationViewSafely(peer=%s) ok=%v\n", cvf.peer, ok)
if !ok {
return nil, false
}
if val, okWin := c.(*conversationWindow); okWin && val.win == nil {
cvf.recreateWindowOn(val)
}
if cvf.isWindowingStyleConsistent(c) {
return c, true
}
defer c.destroy()
var pane *conversationPane
switch v := c.(type) {
case *conversationWindow:
pane = v.conversationPane
case *conversationStackItem:
pane = v.conversationPane
}
return cvf.createConversationView(pane), true
}
func (cvf *ourConversationViewFactory) countPeerWindows(peer jid.Any) int {
c := 0
for k := range cvf.account.c {
if samePeer(peer, k) {
c++
}
}
return c
}
func (cvf *ourConversationViewFactory) basicGetConversationView() (conversationView, bool) {
// fmt.Printf("basicGetConversationView(peer=%s)\n", cvf.peer)
cvf.account.RLock()
defer cvf.account.RUnlock()
pw, pwo := jid.WithAndWithout(cvf.peer)
// fmt.Printf(" basicGetConversationView(peer=%s) with=%v without=%v\n", cvf.peer, pw, pwo)
if pw != nil {
if c, ok := cvf.account.c[pw.String()]; ok {
// This check is not strictly necessary - something should go VERY wrong if it triggers
if !c.isOtrLocked() || c.isOtrLockedTo(cvf.peer) {
return c, true
}
}
}
if c, ok := cvf.account.c[pwo.String()]; ok && !cvf.targeted && (!c.isOtrLocked() || c.isOtrLockedTo(cvf.peer) || cvf.countPeerWindows(pwo) == 1) {
return c, true
}
return nil, false
}