diff --git a/src/QmlUtils.cpp b/src/QmlUtils.cpp index 9aa4f97..84e5386 100644 --- a/src/QmlUtils.cpp +++ b/src/QmlUtils.cpp @@ -1,135 +1,204 @@ /* * Kaidan - A user-friendly XMPP client for every device! * * Copyright (C) 2016-2019 Kaidan developers and contributors * (see the LICENSE file for a full list of copyright authors) * * 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. * * 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. * * 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 * along with Kaidan. If not, see . */ #include "QmlUtils.h" // Qt #include #include #include #include #include #include #include #include #include #include // QXmpp #include "qxmpp-exts/QXmppColorGenerator.h" QmlUtils::QmlUtils(QObject *parent) : QObject(parent) { } +QString QmlUtils::presenceTypeToIcon(Enums::AvailabilityTypes type) +{ + switch (type) { + case AvailabilityTypes::PresOnline: + return "im-user-online"; + case AvailabilityTypes::PresChat: + return "im-user-online"; + case AvailabilityTypes::PresAway: + return "im-user-away"; + case AvailabilityTypes::PresDND: + return "im-kick-user"; + case AvailabilityTypes::PresXA: + return "im-user-away"; + case AvailabilityTypes::PresUnavailable: + return "im-user-offline"; + case AvailabilityTypes::PresError: + return "im-ban-kick-user"; + case AvailabilityTypes::PresInvisible: + return "im-invisible-user"; + } + Q_UNREACHABLE(); +} + +QString QmlUtils::presenceTypeToText(AvailabilityTypes type) +{ + switch (type) { + case AvailabilityTypes::PresOnline: + return tr("Available"); + case AvailabilityTypes::PresChat: + return tr("Free for chat"); + case AvailabilityTypes::PresAway: + return tr("Away"); + case AvailabilityTypes::PresDND: + return tr("Do not disturb"); + case AvailabilityTypes::PresXA: + return tr("Away for longer"); + case AvailabilityTypes::PresUnavailable: + return tr("Offline"); + case AvailabilityTypes::PresError: + return tr("Error"); + case AvailabilityTypes::PresInvisible: + return tr("Invisible"); + } + Q_UNREACHABLE(); +} + +QColor QmlUtils::presenceTypeToColor(AvailabilityTypes type) +{ + switch (type) { + case AvailabilityTypes::PresOnline: + return {"green"}; + case AvailabilityTypes::PresChat: + return {"darkgreen"}; + case AvailabilityTypes::PresAway: + return {"orange"}; + case AvailabilityTypes::PresDND: + return QColor::fromRgb(218, 68, 83); + case AvailabilityTypes::PresXA: + return {"orange"}; + case AvailabilityTypes::PresError: + return {"red"}; + case AvailabilityTypes::PresUnavailable: + return {"silver"}; + case AvailabilityTypes::PresInvisible: + return {"grey"}; + } + Q_UNREACHABLE(); +} + QString QmlUtils::getResourcePath(const QString &name) const { // We generally prefer to first search for files in application resources if (QFile::exists(":/" + name)) return QString("qrc:/" + name); // list of file paths where to search for the resource file QStringList pathList; // add relative path from binary (only works if installed) pathList << QCoreApplication::applicationDirPath() + QString("/../share/") + QString(APPLICATION_NAME); // get the standard app data locations for current platform pathList << QStandardPaths::standardLocations(QStandardPaths::AppDataLocation); #ifdef UBUNTU_TOUCH pathList << QString("./share/") + QString(APPLICATION_NAME); #endif #ifndef NDEBUG #ifdef DEBUG_SOURCE_PATH // add source directory (only for debug builds) pathList << QString(DEBUG_SOURCE_PATH) + QString("/data"); #endif #endif // search for file in directories for (int i = 0; i < pathList.size(); i++) { // open directory QDir directory(pathList.at(i)); // look up the file if (directory.exists(name)) { // found the file, return the path return QUrl::fromLocalFile(directory.absoluteFilePath(name)).toString(); } } // no file found qWarning() << "[main] Could NOT find media file:" << name; return QString(); } bool QmlUtils::isImageFile(const QUrl &fileUrl) const { QMimeType type = QMimeDatabase().mimeTypeForUrl(fileUrl); return type.inherits("image/jpeg") || type.inherits("image/png"); } void QmlUtils::copyToClipboard(const QString &text) const { QGuiApplication::clipboard()->setText(text); } QString QmlUtils::fileNameFromUrl(const QUrl &url) const { return QUrl(url).fileName(); } QString QmlUtils::fileSizeFromUrl(const QUrl &url) const { return QLocale::system().formattedDataSize( QFileInfo(QUrl(url).toLocalFile()).size()); } QString QmlUtils::formatMessage(const QString &message) const { // escape all special XML chars (like '<' and '>') // and spilt into words for processing return processMsgFormatting(message.toHtmlEscaped().split(" ")); } QColor QmlUtils::getUserColor(const QString &nickName) const { QXmppColorGenerator::RGBColor color = QXmppColorGenerator::generateColor(nickName); return {color.red, color.green, color.blue}; } QString QmlUtils::processMsgFormatting(const QStringList &list, bool isFirst) const { if (list.isEmpty()) return QString(); // link highlighting if (list.first().startsWith("https://") || list.first().startsWith("http://")) return (isFirst ? QString() : " ") + QString("%1").arg(list.first()) + processMsgFormatting(list.mid(1), false); return (isFirst ? QString() : " ") + list.first() + processMsgFormatting(list.mid(1), false); } diff --git a/src/QmlUtils.h b/src/QmlUtils.h index cbc78a0..6269a26 100644 --- a/src/QmlUtils.h +++ b/src/QmlUtils.h @@ -1,118 +1,136 @@ /* * Kaidan - A user-friendly XMPP client for every device! * * Copyright (C) 2016-2019 Kaidan developers and contributors * (see the LICENSE file for a full list of copyright authors) * * 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. * * 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. * * 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 * along with Kaidan. If not, see . */ #ifndef UTILS_H #define UTILS_H #include #include "Globals.h" +#include "Enums.h" + +using namespace Enums; /** * @brief C++ utitlities to be used in QML * * The methods are not static, because they need to be used from QML and registered in * Qt. */ class QmlUtils : public QObject { Q_OBJECT public: QmlUtils(QObject *parent = nullptr); + /** + * Returns the icon belonging to the given presence status type + */ + Q_INVOKABLE static QString presenceTypeToIcon(Enums::AvailabilityTypes type); + + /** + * Returns the text belonging to the given presence status type + */ + Q_INVOKABLE static QString presenceTypeToText(Enums::AvailabilityTypes type); + + /** + * Returns the color belonging to the given presence status type + */ + Q_INVOKABLE static QColor presenceTypeToColor(Enums::AvailabilityTypes type); + public slots: /** * Returns a URL to a given resource file name * * This will check various paths which could contain the searched file. * If the file was found, it'll return a `file://` or a `qrc:/` url to * the file. */ QString getResourcePath(const QString &resourceName) const; /** * Returns a string of this build's Kaidan version */ QString getVersionString() const { return VERSION_STRING; } /** * Returns a string without new lines, unneeded spaces, etc. * * See QString::simplified for more information. */ QString removeNewLinesFromString(const QString &input) const { return input.simplified(); } /** * Checks whether a file is an image and could be displayed as such. * @param fileUrl URL to the possible image file */ bool isImageFile(const QUrl &fileUrl) const; /** * Copy text to the clipboard */ void copyToClipboard(const QString &text) const; /** * Returns the file name from a URL */ QString fileNameFromUrl(const QUrl &url) const; /** * Returns the file size from a URL */ QString fileSizeFromUrl(const QUrl &url) const; /** * Styles/formats a message for displaying * * This currently only adds some link highlighting */ QString formatMessage(const QString &message) const; /** * Returns a consistent user color generated from the nickname. */ QColor getUserColor(const QString &nickName) const; private: /** * Highlights links in a list of words */ QString processMsgFormatting(const QStringList &words, bool isFirst = true) const; }; #endif // UTILS_H diff --git a/src/qml/elements/RosterListItem.qml b/src/qml/elements/RosterListItem.qml index d3909d2..b8be952 100644 --- a/src/qml/elements/RosterListItem.qml +++ b/src/qml/elements/RosterListItem.qml @@ -1,214 +1,194 @@ /* * Kaidan - A user-friendly XMPP client for every device! * * Copyright (C) 2016-2019 Kaidan developers and contributors * (see the LICENSE file for a full list of copyright authors) * * 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. * * 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. * * 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 * along with Kaidan. If not, see . */ import QtQuick 2.3 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.0 as Controls import QtGraphicalEffects 1.0 import org.kde.kirigami 2.0 as Kirigami import im.kaidan.kaidan 1.0 Kirigami.SwipeListItem { id: listItem property string name property string jid property string lastMessage property int unreadMessages property string avatarImagePath property int presenceType property string statusMsg topPadding: 0 leftPadding: 0 bottomPadding: 0 height: Kirigami.Units.gridUnit * 3.5 RowLayout { spacing: Kirigami.Units.gridUnit * 0.5 // left border: presence Rectangle { id: presenceIndicator visible: presenceType !== Enums.PresInvisible width: Kirigami.Units.gridUnit * 0.3 height: parent.height - color: presenceTypeToColor(presenceType) + color: kaidan.utils.presenceTypeToColor(presenceType) } // left: avatar Item { id: avatarSpace Layout.preferredHeight: parent.height - Kirigami.Units.gridUnit * 0.8 Layout.preferredWidth: parent.height - Kirigami.Units.gridUnit * 0.8 Controls.ToolTip { visible: hovered && !Kirigami.Settings.isMobile delay: Qt.styleHints.mousePressAndHoldInterval text: generateToolTipText(listItem.name, listItem.jid, listItem.presenceType, listItem.statusMsg) } RoundImage { id: avatar visible: avatarImagePath !== "" anchors.fill: parent source: avatarImagePath width: height fillMode: Image.PreserveAspectFit mipmap: true } TextAvatar { visible: avatarImagePath == "" anchors.fill: parent width: height id: textAvatar name: listItem.name } } // right side ColumnLayout { spacing: Kirigami.Units.smallSpacing Layout.fillWidth: true // contact name 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 } } // bottom RowLayout { Layout.fillWidth: true Controls.Label { Layout.fillWidth: true elide: Text.ElideRight maximumLineCount: 1 text: { presenceType === Enums.PresError ? // error presence type qsTr("Error: Please check the JID.") : kaidan.utils.removeNewLinesFromString(lastMessage) } textFormat: Text.PlainText font.pixelSize: Kirigami.Units.gridUnit * 0.8 } } } // unread message counter MessageCounter { id: counter visible: unreadMessages > 0 counter: unreadMessages muted: kaidan.notificationsMuted(jid) Layout.preferredHeight: Kirigami.Units.gridUnit * 1.25 Layout.preferredWidth: Kirigami.Units.gridUnit * 1.25 } } - /** - * Returns the color belonging to the given presence status type - */ - function presenceTypeToColor(type) { - 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" : - "silver" // unavailable (offline) - } - /** * 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 = "

" + name + "

" // in small: JID (only if differs name) if (name !== jid) { string += "
" + jid + "
" } // presence status type - string += "" - 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 + string += "" + string += kaidan.utils.presenceTypeToText(statusType) string += "" // presence status message if (statusMsg !== "") { string += ": " + statusMsg } return string } 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) } }