This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
contactlistmodel.go
170 lines (140 loc) · 3.68 KB
/
contactlistmodel.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
package main
import (
"fmt"
"github.com/therecipe/qt/core"
)
// Model Roles
const (
RoleNickname = int(core.Qt__UserRole) + iota
RoleAvatar
RoleMessage
RoleStatus
RoleTimestamp
RoleOutbound
RoleMessageID
RoleKeyExchanged
)
// Contact holds the data for a contact
type Contact struct {
core.QObject
Nickname string
Avatar string
KeyExchanged bool
}
// ContactListModel holds a collection of contacts
type ContactListModel struct {
core.QAbstractListModel
_ func() `constructor:"init"`
_ map[int]*core.QByteArray `property:"roles"`
_ []*Contact `property:"contacts"`
_ func(*Contact) `slot:"addContact"`
_ func(*Contact) `slot:"appendContact"`
_ func(int) `slot:"removeContact"`
_ func() `slot:"clear"`
}
func (m *ContactListModel) init() {
m.SetRoles(map[int]*core.QByteArray{
RoleNickname: core.NewQByteArray2("nickname", -1),
RoleAvatar: core.NewQByteArray2("avatar", -1),
RoleKeyExchanged: core.NewQByteArray2("keyexchanged", -1),
})
m.ConnectData(m.data)
m.ConnectRowCount(m.rowCount)
m.ConnectColumnCount(m.columnCount)
m.ConnectRoleNames(m.roleNames)
m.ConnectAddContact(m.addContact)
m.ConnectAppendContact(m.appendContact)
m.ConnectRemoveContact(m.removeContact)
m.ConnectClear(m.clear)
}
func (m *ContactListModel) data(index *core.QModelIndex, role int) *core.QVariant {
if !index.IsValid() {
return core.NewQVariant()
}
if index.Row() >= len(m.Contacts()) {
return core.NewQVariant()
}
var p = m.Contacts()[len(m.Contacts())-1-index.Row()]
if p == nil {
return core.NewQVariant()
}
switch role {
case RoleNickname:
{
return core.NewQVariant1(p.Nickname)
}
case RoleAvatar:
{
if p.Avatar == "" {
return core.NewQVariant1("qrc:/qml/images/katzenpost_logo.png")
} else {
return core.NewQVariant1(p.Avatar)
}
}
case RoleKeyExchanged:
{
return core.NewQVariant1(p.KeyExchanged)
}
default:
{
return core.NewQVariant()
}
}
}
func (m *ContactListModel) rowCount(parent *core.QModelIndex) int {
return len(m.Contacts())
}
func (m *ContactListModel) columnCount(parent *core.QModelIndex) int {
return 1
}
func (m *ContactListModel) roleNames() map[int]*core.QByteArray {
return m.Roles()
}
func (m *ContactListModel) clear() {
m.BeginResetModel()
m.SetContacts([]*Contact{})
m.EndResetModel()
}
func (m *ContactListModel) addContact(p *Contact) {
m.BeginInsertRows(core.NewQModelIndex(), 0, 0)
m.SetContacts(append(m.Contacts(), p))
m.EndInsertRows()
}
func (m *ContactListModel) appendContact(p *Contact) {
m.BeginInsertRows(core.NewQModelIndex(), len(m.Contacts()), len(m.Contacts()))
m.SetContacts(append([]*Contact{p}, m.Contacts()...))
m.EndInsertRows()
}
func (m *ContactListModel) removeContact(row int) {
trow := len(m.Contacts()) - 1 - row
m.BeginRemoveRows(core.NewQModelIndex(), row, row)
m.SetContacts(append(m.Contacts()[:trow], m.Contacts()[trow+1:]...))
m.EndRemoveRows()
}
func (m *ContactListModel) updateContactStatus(nickname string, keyExchanged bool) {
fmt.Println("Updating contact status:", nickname, keyExchanged)
for _, v := range m.Contacts() {
if v.Nickname == nickname {
v.KeyExchanged = keyExchanged
var fIndex = m.Index(0, 0, core.NewQModelIndex())
var lIndex = m.Index(len(m.Contacts())-1, 0, core.NewQModelIndex())
m.DataChanged(fIndex, lIndex, []int{RoleKeyExchanged})
if accountBridge.Recipient() == nickname {
accountBridge.SetKeyExchanged(true)
}
return
}
}
}
func (m *ContactListModel) getContact(nickname string) *Contact {
for _, v := range m.Contacts() {
if v.Nickname == nickname {
return v
}
}
return nil
}
func init() {
ContactListModel_QRegisterMetaType()
Contact_QRegisterMetaType()
}