-
Notifications
You must be signed in to change notification settings - Fork 80
/
edit_contact.go
206 lines (167 loc) · 5.32 KB
/
edit_contact.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
package gui
import (
"log"
"github.com/coyim/coyim/config"
"github.com/coyim/coyim/i18n"
"github.com/coyim/coyim/xmpp/jid"
"github.com/coyim/gotk3adapter/gtki"
)
type editContactDialog struct {
builder *builder
dialog gtki.Dialog
accountName gtki.Label
contactJID gtki.Label
nickname gtki.Entry
requireEncryption gtki.CheckButton
currentGroups gtki.ListStore
existingGroups gtki.Menu
addGroup gtki.MenuItem
removeGroup gtki.Button
currentGroupsView gtki.TreeView
fingerprintsInformation gtki.Label
fingerprintsGrid gtki.Grid
save gtki.Button
}
func (ecd *editContactDialog) init() {
ecd.builder = newBuilder("PeerDetails")
ecd.builder.getItems(
"dialog", &ecd.dialog,
"account-name", &ecd.accountName,
"jid", &ecd.contactJID,
"nickname", &ecd.nickname,
"require-encryption", &ecd.requireEncryption,
"current-groups", &ecd.currentGroups,
"groups-menu", &ecd.existingGroups,
"addGroup", &ecd.addGroup,
"remove-btn", &ecd.removeGroup,
"groups-view", &ecd.currentGroupsView,
"fingerprintsInformation", &ecd.fingerprintsInformation,
"fingerprintsGrid", &ecd.fingerprintsGrid,
"btn-save", &ecd.save,
)
}
func (ecd *editContactDialog) closeDialog() {
ecd.dialog.Destroy()
}
func (ecd *editContactDialog) enableRemoveButton() {
ts, _ := ecd.currentGroupsView.GetSelection()
_, _, ok := ts.GetSelected()
ecd.removeGroup.SetSensitive(ok)
}
func (ecd *editContactDialog) removeSelectedGroup() {
ts, _ := ecd.currentGroupsView.GetSelection()
if _, iter, ok := ts.GetSelected(); ok {
ecd.currentGroups.Remove(iter)
}
}
func (ecd *editContactDialog) openAddGroupDialog() {
groupList := ecd.currentGroups
builder := newBuilder("GroupDetails")
dialog := builder.getObj("dialog").(gtki.Dialog)
nameEntry := builder.getObj("group-name").(gtki.Entry)
defaultBtn := builder.getObj("btn-ok").(gtki.Button)
defaultBtn.GrabDefault()
dialog.SetTransientFor(ecd.dialog)
dialog.ShowAll()
response := dialog.Run()
defer dialog.Destroy()
if gtki.ResponseType(response) != gtki.RESPONSE_OK {
return
}
groupName, _ := nameEntry.GetText()
groupList.SetValue(groupList.Append(), 0, groupName)
}
func (ecd *editContactDialog) addCurrentGroup(g string) {
ecd.currentGroups.SetValue(ecd.currentGroups.Append(), 0, g)
}
func (ecd *editContactDialog) initAllGroups(allGroups []string) {
for _, gr := range allGroups {
menu, _ := g.gtk.MenuItemNewWithLabel(gr)
menu.SetVisible(true)
menu.Connect("activate", func(m gtki.MenuItem) {
ecd.addCurrentGroup(m.GetLabel())
})
ecd.existingGroups.Add(menu)
}
if len(allGroups) > 0 {
sep, _ := g.gtk.SeparatorMenuItemNew()
sep.SetVisible(true)
ecd.existingGroups.Add(sep)
}
}
func (ecd *editContactDialog) initCurrentGroups(groups []string) {
ecd.currentGroups.Clear()
for _, g := range groups {
ecd.addCurrentGroup(g)
}
}
func (ecd *editContactDialog) showFingerprintsForPeer(peer jid.WithoutResource, account *account) {
info := ecd.fingerprintsInformation
grid := ecd.fingerprintsGrid
fprs := []*config.Fingerprint{}
p, ok := account.session.GetConfig().GetPeer(peer.String())
if ok {
fprs = p.Fingerprints
}
if len(fprs) == 0 {
info.SetText(i18n.Localf("There are no known fingerprints for %s", peer))
} else {
info.SetText(i18n.Localf("These are the fingerprints known for %s:", peer))
}
for ix, fpr := range fprs {
flabel, _ := g.gtk.LabelNew(config.FormatFingerprint(fpr.Fingerprint))
flabel.SetSelectable(true)
trusted := i18n.Local("not trusted")
if fpr.Trusted {
trusted = i18n.Local("trusted")
}
ftrusted, _ := g.gtk.LabelNew(trusted)
ftrusted.SetSelectable(true)
grid.Attach(flabel, 0, ix, 1, 1)
grid.Attach(ftrusted, 1, ix, 1, 1)
}
}
func (r *roster) openEditContactDialog(peer jid.WithoutResource, acc *account) {
assertInUIThread()
p, ok := r.ui.accountManager.getPeer(acc, peer)
if !ok {
log.Printf("Couldn't find peer %s in account %v", peer, acc)
return
}
conf := acc.session.GetConfig()
ecd := &editContactDialog{}
ecd.init()
ecd.accountName.SetText(conf.Account)
ecd.contactJID.SetText(peer.String())
//nickNameEntry.SetText(peer.Name)
if p, ok := r.ui.getPeer(acc, peer); ok {
ecd.nickname.SetText(p.Nickname)
}
shouldEncryptTo := conf.ShouldEncryptTo(peer.String())
ecd.requireEncryption.SetActive(shouldEncryptTo)
ecd.initCurrentGroups(sortedGroupNames(p.Groups))
ecd.initAllGroups(r.getGroupNamesFor(acc))
ecd.existingGroups.Add(ecd.addGroup)
ecd.removeGroup.SetSensitive(false)
ecd.showFingerprintsForPeer(peer, acc)
//TODO: Move to editContactDialog
ecd.builder.ConnectSignals(map[string]interface{}{
"on-add-new-group": ecd.openAddGroupDialog,
"on-group-selection-changed": ecd.enableRemoveButton,
"on-remove-group": ecd.removeSelectedGroup,
"on-cancel": ecd.closeDialog,
"on-save": func() {
assertInUIThread()
defer ecd.dialog.Destroy()
groups := toArray(ecd.currentGroups)
nickname, _ := ecd.nickname.GetText()
err := r.updatePeer(acc, peer, nickname, groups, ecd.requireEncryption.GetActive() != shouldEncryptTo, ecd.requireEncryption.GetActive())
if err != nil {
log.Println(err)
}
},
})
ecd.save.GrabDefault()
ecd.dialog.SetTransientFor(r.ui.window)
ecd.dialog.ShowAll()
}