RosterListItem.qml 6.08 KB
Newer Older
1
2
3
/*
 *  Kaidan - A user-friendly XMPP client for every device!
 *
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.3
32
33
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0 as Controls
34
import QtGraphicalEffects 1.0
35
import org.kde.kirigami 2.0 as Kirigami
36
import im.kaidan.kaidan 1.0
37
38

Kirigami.SwipeListItem {
39
40
	id: listItem

41
	property string name
42
	property string jid
43
44
45
46
	property string lastMessage
	property int unreadMessages
	property string avatarImagePath
	property int presenceType
47
	property string statusMsg
48

49
50
51
	topPadding: 0
	leftPadding: 0
	bottomPadding: 0
52
53
54
55
56
	height: Kirigami.Units.gridUnit * 3.5

	RowLayout {
		spacing: Kirigami.Units.gridUnit * 0.5

57
58
59
60
61
		// left border: presence
		Rectangle {
			id: presenceIndicator
			visible: presenceType !== Enums.PresInvisible

62
			width: Kirigami.Units.gridUnit * 0.3
63
64
65
66
67
68
			height: parent.height

			color: presenceTypeToColor(presenceType)
		}

		// left: avatar
69
70
		Item {
			id: avatarSpace
71
72
			Layout.preferredHeight: parent.height - Kirigami.Units.gridUnit * 0.8
			Layout.preferredWidth: parent.height - Kirigami.Units.gridUnit * 0.8
73

74
75
76
77
78
79
80
			Controls.ToolTip {
				visible: hovered && !Kirigami.Settings.isMobile
				delay: Qt.styleHints.mousePressAndHoldInterval
				text: generateToolTipText(listItem.name, listItem.jid,
				                          listItem.presenceType, listItem.statusMsg)
			}

81
82
			RoundImage {
				id: avatar
83
				visible: avatarImagePath !== ""
84
85
86
87
88
				anchors.fill: parent
				source: avatarImagePath
				width: height
				fillMode: Image.PreserveAspectFit
				mipmap: true
89
			}
90

91
92
93
94
95
96
			TextAvatar {
				visible: avatarImagePath == ""
				anchors.fill: parent
				width: height
				id: textAvatar
				name: listItem.name
97
			}
98
		}
99

100
101
		// right side
		ColumnLayout {
102
			spacing: Kirigami.Units.smallSpacing
103
			Layout.fillWidth: true
104
105

			// contact name
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
			RowLayout {
				Kirigami.Heading {
					text: name
					textFormat: Text.PlainText
					elide: Text.ElideRight
					maximumLineCount: 1
					level: 3
					Layout.fillWidth: true
					Layout.maximumHeight: Kirigami.Units.gridUnit * 1.5
				}
				// muted-icon
				Kirigami.Icon {
					id: muteIcon
					source: "audio-volume-muted-symbolic"
					width: 16
					height: 16
					visible: kaidan.notificationsMuted(jid)
				}
				Item {
					Layout.fillWidth: true
				}
127
128
			}
			// bottom
129
			RowLayout {
130
				Layout.fillWidth: true
131
132
133
134
135
136

				Controls.Label {
					Layout.fillWidth: true
					elide: Text.ElideRight
					maximumLineCount: 1
					text: {
137
						presenceType === Enums.PresError ? // error presence type
138
						qsTr("Error: Please check the JID.") :
139
						kaidan.utils.removeNewLinesFromString(lastMessage)
140
141
					}
					textFormat: Text.PlainText
Jonah Brüchert's avatar
Jonah Brüchert committed
142
					font.pixelSize: Kirigami.Units.gridUnit * 0.8
143
				}
144
			}
145
		}
146

147
148
		// unread message counter
		MessageCounter {
149
			id: counter
150
151
			visible: unreadMessages > 0
			counter: unreadMessages
152
			muted: kaidan.notificationsMuted(jid)
153

154
155
			Layout.preferredHeight: Kirigami.Units.gridUnit * 1.25
			Layout.preferredWidth: Kirigami.Units.gridUnit * 1.25
156
157
		}
	}
158
159

	/**
Melvin Keskin's avatar
Melvin Keskin committed
160
	 * Returns the color belonging to the given presence status type
161
162
	 */
	function presenceTypeToColor(type) {
163
164
165
166
167
168
		return type === Enums.PresOnline ? "green" :
			type === Enums.PresChat ? "darkgreen" :
			type === Enums.PresAway ? "orange" :
			type === Enums.PresDND ? "orange" :
			type === Enums.PresXA ? "orange" :
			type === Enums.PresError ? "red" :
169
			"silver" // unavailable (offline)
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
	}

	/**
	 * Generates a styled text telling some basic information about the contact,
	 * is used for a tooltip
	 */
	function generateToolTipText(name, jid, statusType, statusMsg) {
		// header (contact name)
		var string = "<h3>" + name + "</h3>"
		// in small: JID (only if differs name)
		if (name !== jid) {
			string += "<h5><i>" + jid + "</i></h5>"
		}

		// presence status type
		string += "<font color='" + presenceTypeToColor(statusType) + "'>"
186
187
188
189
190
191
192
193
		string += statusType === Enums.PresOnline ? qsTr("Available") :
		          statusType === Enums.PresChat ? qsTr("Free for chat") :
		          statusType === Enums.PresAway ? qsTr("Away") :
		          statusType === Enums.PresDND ? qsTr("Do not disturb") :
		          statusType === Enums.PresXA ? qsTr("Away for longer") :
		          statusType === Enums.PresUnavailable ? qsTr("Offline") :
		          statusType === Enums.PresError ? qsTr("Error") :
		          "Invalid" // should never be displayed
194
195
196
197
198
199
200
201
		string += "</font>"

		// presence status message
		if (statusMsg !== "") {
			string += ": " + statusMsg
		}
		return string
	}
202
203
204
205
206
207
208
209
210
211
212
213

	function handleNotificationsMuted(mutedContact) {
		counter.muted = kaidan.notificationsMuted(jid)
		muteIcon.visible = kaidan.notificationsMuted(jid)
	}

	Component.onCompleted: {
		kaidan.notificationsMutedChanged.connect(handleNotificationsMuted)
	}
	Component.onDestruction: {
		kaidan.notificationsMutedChanged.disconnect(handleNotificationsMuted)
	}
214
}