RosterPage.qml 4.61 KB
Newer Older
1
/*
2
 *  Kaidan - A user-friendly XMPP client for every device!
3
 *
Linus Jahn's avatar
Linus Jahn committed
4
 *  Copyright (C) 2016-2019 Kaidan developers and contributors
Linus Jahn's avatar
Linus Jahn committed
5
 *  (see the LICENSE file for a full list of copyright authors)
6
7
8
9
10
11
 *
 *  Kaidan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
Linus Jahn's avatar
Linus Jahn committed
12
13
14
15
16
17
18
19
20
21
 *  In addition, as a special exception, the author of Kaidan gives
 *  permission to link the code of its release with the OpenSSL
 *  project's "OpenSSL" library (or with modified versions of it that
 *  use the same license as the "OpenSSL" library), and distribute the
 *  linked executables. You must obey the GNU General Public License in
 *  all respects for all of the code used other than "OpenSSL". If you
 *  modify this file, you may extend this exception to your version of
 *  the file, but you are not obligated to do so.  If you do not wish to
 *  do so, delete this exception statement from your version.
 *
22
23
24
25
26
27
 *  Kaidan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
Linus Jahn's avatar
Linus Jahn committed
28
 *  along with Kaidan.  If not, see <http://www.gnu.org/licenses/>.
29
30
 */

31
import QtQuick 2.7
32
import QtQuick.Layouts 1.3
Robert Maerkisch's avatar
Robert Maerkisch committed
33
import QtQuick.Controls 2.3 as Controls
JBB's avatar
JBB committed
34
import org.kde.kirigami 2.0 as Kirigami
35
import im.kaidan.kaidan 1.0
36
import "elements"
Marzanna's avatar
Marzanna committed
37

Linus Jahn's avatar
Linus Jahn committed
38
Kirigami.ScrollablePage {
39
40
41
42
43
	title: {
		kaidan.connectionState === Enums.StateConnecting ? qsTr("Connecting…") :
		kaidan.connectionState === Enums.StateDisconnected ? qsTr("Offline") :
		qsTr("Contacts")
	}
Robert Maerkisch's avatar
Robert Maerkisch committed
44
45
46
47
	leftPadding: 0
	topPadding: 0
	rightPadding: 0
	bottomPadding: 0
Linus Jahn's avatar
Linus Jahn committed
48

49
50
	RosterAddContactSheet {
		id: addContactSheet
51
		jid: ""
52
53
	}

54
55
56
57
	mainAction: Kirigami.Action {
		text: qsTr("Add new contact")
		iconName: "contact-new"
		onTriggered: {
58
59
60
61
			if (addContactSheet.sheetOpen)
				addContactSheet.close()
			else
				addContactSheet.open()
62
63
64
		}
	}

Robert Maerkisch's avatar
Robert Maerkisch committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
	rightAction: Kirigami.Action {
		text: qsTr("Search contacts")
		iconName: "search"
		onTriggered: {
			if (searchField.visible) {
				searchField.visible = false
			} else {
				searchField.visible = true
			}
		}
	}

	header: Item {
		height: searchField.visible ? searchField.height : 0
		Controls.TextField {
			id: searchField
			width: parent.width
			visible: false
			focus: visible
			placeholderText: qsTr("Search…")
			onVisibleChanged: text = ""
		}
	}

Marzanna's avatar
Marzanna committed
89
	ListView {
Linus Jahn's avatar
Linus Jahn committed
90
		verticalLayoutDirection: ListView.TopToBottom
91
		model: kaidan.rosterModel
92
		delegate: RosterListItem {
93
			id: rosterItem
Robert Maerkisch's avatar
Robert Maerkisch committed
94
95
96
97
98
			visible: {
				model.jid.toLowerCase().includes(searchField.displayText.toLowerCase()) ||
						model.name.toLowerCase().includes(searchField.displayText.toLowerCase())
			}
			height: visible ? 65 : 0
99
			name: model.name ? model.name : model.jid
100
			jid: model.jid
101
			lastMessage: model.lastMessage
102
103
			presenceType: kaidan.presenceCache.getPresenceType(model.jid)
			statusMsg: kaidan.presenceCache.getStatusText(model.jid)
104
			unreadMessages: model.unreadMessages
105
			avatarImagePath: kaidan.avatarStorage.getAvatarUrl(model.jid)
106
			backgroundColor: {
107
108
				if (!Kirigami.Settings.isMobile &&
						kaidan.messageModel.chatPartner === model.jid) {
109
110
111
112
					Kirigami.Theme.highlightColor
				} else {
					Kirigami.Theme.backgroundColor
				}
113
			}
114
			onClicked: {
Robert Maerkisch's avatar
Robert Maerkisch committed
115
				searchField.visible = false
116
117
118
119
120
				// We need to cache the chatName, because changing the chatPartner in the
				// message model will in some cases also update the roster model. That
				// will then remove this item and readd an updated version of it, so
				// model.* won't work anymore after this.
				var chatName = model.name ? model.name : model.jid
121
				kaidan.messageModel.chatPartner = model.jid
122
				pageStack.push(chatPage, {
123
					"chatName": chatName
124
				})
125
			}
126
127
128

			function newPresenceArrived(jid) {
				if (jid === model.jid) {
Robert Maerkisch's avatar
Robert Maerkisch committed
129
130
					rosterItem.presenceType = kaidan.presenceCache.getPresenceType(model.jid)
					rosterItem.statusMsg = kaidan.presenceCache.getStatusText(model.jid)
131
132
133
				}
			}

134
135
136
137
138
139
			function xmppUriReceived(uri) {
				// 'xmpp:' has length of 5
				addContactSheet.jid = uri.substr(5)
				addContactSheet.open()
			}

140
141
			Component.onCompleted: {
				kaidan.presenceCache.presenceChanged.connect(newPresenceArrived)
142
				kaidan.xmppUriReceived.connect(xmppUriReceived)
143
144
145
			}
			Component.onDestruction: {
				kaidan.presenceCache.presenceChanged.disconnect(newPresenceArrived)
146
				kaidan.xmppUriReceived.disconnect(xmppUriReceived)
147
			}
Marzanna's avatar
Marzanna committed
148
149
150
		}
	}
}