File: edit_contact.go

package info (click to toggle)
coyim 0.3.7-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,064 kB
  • ctags: 4,528
  • sloc: xml: 5,120; sh: 328; python: 286; makefile: 235; ruby: 51
file content (133 lines) | stat: -rw-r--r-- 3,358 bytes parent folder | download
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
package gui

import (
	"log"

	"github.com/twstrike/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
	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,
		"btn-save", &ecd.save,
	)
}

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 (r *roster) openEditContactDialog(jid string, acc *account) {
	assertInUIThread()
	peer, ok := r.ui.accountManager.getPeer(acc, jid)
	if !ok {
		log.Printf("Couldn't find peer %s in account %v", jid, acc)
		return
	}

	ecd := &editContactDialog{}
	ecd.init()
	conf := acc.session.GetConfig()

	ecd.accountName.SetText(conf.Account)
	ecd.contactJID.SetText(jid)

	//nickNameEntry.SetText(peer.Name)
	if peer, ok := r.ui.getPeer(acc, jid); ok {
		ecd.nickname.SetText(peer.Nickname)
	}

	shouldEncryptTo := conf.ShouldEncryptTo(jid)
	ecd.requireEncryption.SetActive(shouldEncryptTo)

	ecd.initCurrentGroups(sortedGroupNames(peer.Groups))
	ecd.initAllGroups(r.getGroupNamesFor(acc))

	ecd.existingGroups.Add(ecd.addGroup)
	ecd.removeGroup.SetSensitive(false)

	ecd.builder.ConnectSignals(map[string]interface{}{
		"on-add-new-group": func() {
			r.addGroupDialog(ecd.currentGroups)
		},
		"on-group-selection-changed": func() {
			ts, _ := ecd.currentGroupsView.GetSelection()
			_, _, ok := ts.GetSelected()
			ecd.removeGroup.SetSensitive(ok)
		},
		"on-remove-group": func() {
			ts, _ := ecd.currentGroupsView.GetSelection()
			if _, iter, ok := ts.GetSelected(); ok {
				ecd.currentGroups.Remove(iter)
			}
		},
		"on-cancel": ecd.dialog.Destroy,
		"on-save": func() {
			assertInUIThread()
			defer ecd.dialog.Destroy()

			groups := toArray(ecd.currentGroups)
			nickname, _ := ecd.nickname.GetText()

			err := r.updatePeer(acc, jid, 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()
}