RosterListItem.qml 5.59 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 52 53 54 55

	topPadding: Kirigami.Units.smallSpacing * 1.5
	height: Kirigami.Units.gridUnit * 3.5

	RowLayout {
		spacing: Kirigami.Units.gridUnit * 0.5

		// left side: Avatar
56 57
		Item {
			id: avatarSpace
58 59
			Layout.preferredHeight: parent.height
			Layout.preferredWidth: parent.height
60 61 62 63 64 65 66 67

			RoundImage {
				id: avatar
				anchors.fill: parent
				source: avatarImagePath
				width: height
				fillMode: Image.PreserveAspectFit
				mipmap: true
68 69

				Controls.ToolTip {
70
					visible: hovered && !Kirigami.Settings.isMobile
71 72 73 74
					delay: Qt.styleHints.mousePressAndHoldInterval
					text: generateToolTipText(listItem.name, listItem.jid,
					                          listItem.presenceType, listItem.statusMsg)
				}
75 76 77 78 79 80 81 82 83 84 85
			}

			Rectangle {
				id: presenceIndicator
				visible: presenceType !== 8 // invisible when presence is invalid
				anchors.right: avatarSpace.right
				anchors.bottom: avatarSpace.bottom

				width: Kirigami.Units.gridUnit
				height: Kirigami.Units.gridUnit

86
				color: presenceTypeToColor(presenceType)
87 88 89 90 91 92 93 94 95 96 97
				radius: Math.min(width, height) * 0.5
				// shadow
				layer.enabled: presenceIndicator.visible
				layer.effect: DropShadow {
					verticalOffset: Kirigami.Units.gridUnit * 0.08
					horizontalOffset: Kirigami.Units.gridUnit * 0.08
					color: Kirigami.Theme.disabledTextColor
					samples: 10
					spread: 0.1
				}
			}
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 126 127 128
						qsTr("Error: Please check the JID.") :
						kaidan.removeNewLinesFromString(lastMessage)
					}
					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 137 138 139 140 141 142
			}
		}

		// unread message counter
		MessageCounter {
			visible: unreadMessages > 0
			counter: unreadMessages

			Layout.preferredHeight: Kirigami.Units.gridUnit * 1.25
			Layout.preferredWidth: Kirigami.Units.gridUnit * 1.25
		}
	}
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
}