Verified Commit a12910dd authored by Linus Jahn's avatar Linus Jahn 🍙

chat: Make HTTP/HTTPS links highlighted and usable

This will highlight links starting with http:// or https:// and will
make them open externally when clicked. Unfortunately (in Material and
(KDE) Desktop style) the link color doesn't change when hovered.
parent 04d55c40
Pipeline #263 passed with stage
in 2 minutes and 31 seconds
......@@ -286,3 +286,27 @@ QString Kaidan::fileSizeFromUrl(QString url)
return QString::number(qRound(size / 1024.0 / 10.24) / 100.0).append(" MiB");
#endif
}
QString Kaidan::formatMessage(QString message)
{
// escape all special XML chars (as '<' and '>')
message = message.toHtmlEscaped();
return processMsgFormatting(message.split(" "));
}
QString Kaidan::processMsgFormatting(QStringList list, bool isFirst)
{
if (list.isEmpty())
return "";
// add space before word (if word is not the first)
QString prepend = isFirst ? "" : " ";
// link highlighting
if (list.first().startsWith("https://") || list.first().startsWith("http://"))
return prepend + QString("<a href='%1'>%1</a>").arg(list.first())
+ processMsgFormatting(list.mid(1), false);
return prepend + list.first() + processMsgFormatting(list.mid(1), false);
}
......@@ -47,10 +47,10 @@ using namespace Enums;
/**
* @class Kaidan Kaidan's Back-End Class
*
*
* @brief This class will initiate the complete back-end, including the @see Database
* connection, viewing models (@see MessageModel, @see RosterModel), etc.
*
*
* This class will run in the main thread, only the XMPP connection runs in another
* thread (@see ClientThread).
*/
......@@ -93,7 +93,7 @@ public:
*
* This will disconnect the client from the server. When disconnected,
* the connectionStateChanged signal will be emitted.
*
*
* @param openLogInPage If true, the newCredentialsNeeded signal will be
* emitted.
*/
......@@ -250,6 +250,13 @@ public:
*/
Q_INVOKABLE QString fileSizeFromUrl(QString url);
/**
* Styles/formats a message for displaying
*
* This currently only adds some link highlighting
*/
Q_INVOKABLE QString formatMessage(QString message);
signals:
void rosterModelChanged();
void messageModelChanged();
......@@ -404,6 +411,11 @@ public slots:
private:
void connectDatabases();
/**
* Highlights links in a list of words
*/
QString processMsgFormatting(QStringList words, bool isFirst = true);
ClientWorker *client;
ClientThread *cltThrd;
Database *database;
......
......@@ -126,11 +126,12 @@ RowLayout {
// message body
Controls.Label {
visible: messageBody !== ""
text: messageBody
textFormat: Text.PlainText
text: kaidan.formatMessage(messageBody)
textFormat: Text.StyledText
wrapMode: Text.Wrap
color: sentByMe ? Kirigami.Theme.buttonTextColor
: Kirigami.Theme.complementaryTextColor
onLinkActivated: Qt.openUrlExternally(link)
Layout.maximumWidth: mediaType === Enums.MessageImage && media.width !== 0
? media.width
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment