RosterListItem.qml 5.44 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 62 63 64 65 66 67 68
		// left border: presence
		Rectangle {
			id: presenceIndicator
			visible: presenceType !== Enums.PresInvisible

			width: Kirigami.Units.gridUnit * 0.2
			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 106 107

			// contact name
			Kirigami.Heading {
				text: name
108
				textFormat: Text.PlainText
109 110 111 112 113
				elide: Text.ElideRight
				maximumLineCount: 1
				level: 3
				Layout.fillWidth: true
				Layout.maximumHeight: Kirigami.Units.gridUnit * 1.5
114 115
			}
			// bottom
116
			RowLayout {
117
				Layout.fillWidth: true
118 119 120 121 122 123

				Controls.Label {
					Layout.fillWidth: true
					elide: Text.ElideRight
					maximumLineCount: 1
					text: {
124
						presenceType === Enums.PresError ? // error presence type
125
						qsTr("Error: Please check the JID.") :
126
						kaidan.utils.removeNewLinesFromString(lastMessage)
127 128
					}
					textFormat: Text.PlainText
Jonah Brüchert's avatar
Jonah Brüchert committed
129
					font.pixelSize: Kirigami.Units.gridUnit * 0.8
130
				}
131 132
			}

133 134 135 136
			// unread message counter
			MessageCounter {
				visible: unreadMessages > 0
				counter: unreadMessages
137

138 139 140
				Layout.preferredHeight: Kirigami.Units.gridUnit * 1.25
				Layout.preferredWidth: Kirigami.Units.gridUnit * 1.25
			}
141 142
		}
	}
143 144 145 146 147

	/**
	 * Returns the colour beloning to the given presence status type
	 */
	function presenceTypeToColor(type) {
148 149 150 151 152 153 154
		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" :
			"lightgrey" // unavailable (offline)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
	}

	/**
	 * 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) + "'>"
171 172 173 174 175 176 177 178
		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
179 180 181 182 183 184 185 186
		string += "</font>"

		// presence status message
		if (statusMsg !== "") {
			string += ": " + statusMsg
		}
		return string
	}
187
}