diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 3587b57b..27915115 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -1,276 +1,276 @@ /* Copyright (c) 2017-2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "utils.h" #include "ruqola_debug.h" #include #include #include #include #include #include QUrl Utils::generateServerUrl(const QString &url) { if (url.isEmpty()) { return {}; } QString serverUrl = url; if (serverUrl.startsWith(QLatin1String("https://"))) { serverUrl.replace(QLatin1String("https://"), QLatin1String("wss://")); } else if (serverUrl.startsWith(QLatin1String("http://"))) { serverUrl.replace(QLatin1String("http://"), QLatin1String("ws://")); } else { serverUrl = QLatin1String("wss://") + serverUrl; } return QUrl(serverUrl + QStringLiteral("/websocket")); } QString Utils::extractRoomUserFromUrl(QString url) { url.remove(QStringLiteral("ruqola:/user/")); url.remove(QStringLiteral("ruqola:/room/")); return url; } QString Utils::markdownToRichText(const QString &markDown) { if (markDown.isEmpty()) { return QString(); } //qCDebug(RUQOLA_LOG) << "BEFORE markdownToRichText "<...)" style urls str = Utils::convertTextWithUrl(str); //Bug 391520 I don't remember why I removed
need to investigate //str.remove(QStringLiteral("
")); //qCDebug(RUQOLA_LOG) << "markdownToRichText "<@%1") - .arg(word, userMentionForegroundColor, userMentionBackgroundColor)); + .arg(word.toString(), userMentionForegroundColor, userMentionBackgroundColor)); } else { newStr.replace(QLatin1Char('@') + word, QStringLiteral("@%1").arg(word)); } } static const QRegularExpression regularExpressionRoom(QStringLiteral("(^|\\s+)#([\\w._-]+)")); QRegularExpressionMatchIterator roomIterator = regularExpressionRoom.globalMatch(newStr); while (roomIterator.hasNext()) { const QRegularExpressionMatch match = roomIterator.next(); const QStringRef word = match.capturedRef(2); newStr.replace(QLatin1Char('#') + word, QStringLiteral("#%1").arg(word)); } /// match unescaped `...` regions, i.e. properly match `...\`...` /// and make the inner region non-greedy, to have two code blocks for /// lines like this: `foo` asdf `bar` static const QRegularExpression regularExpressionCode(QStringLiteral("((?\\1")); return newStr; } QString Utils::formatQuotedRichText(const QString &richText) { // Qt's support for borders is limited to tables, so we have to jump through some hoops... KColorScheme scheme; const auto backgroundColor = scheme.background(KColorScheme::AlternateBackground).color().name(); const auto borderColor = scheme.background(KColorScheme::LinkBackground).color().name(); - return QLatin1String("
").arg(backgroundColor, borderColor) + return QStringLiteral("
").arg(backgroundColor, borderColor) + richText - + QLatin1String("
"); + + QStringLiteral("
"); } QString Utils::presenceStatusToString(User::PresenceStatus status) { switch (status) { case User::PresenceStatus::PresenceOnline: return QStringLiteral("online"); case User::PresenceStatus::PresenceBusy: return QStringLiteral("busy"); case User::PresenceStatus::PresenceAway: return QStringLiteral("away"); case User::PresenceStatus::PresenceOffline: return QStringLiteral("offline"); case User::PresenceStatus::Unknown: return {}; } return {}; } QString Utils::iconFromStatus(const QString &status) { if (status == QLatin1String("online")) { return QStringLiteral("user-online"); } else if (status == QLatin1String("busy")) { return QStringLiteral("user-busy"); } else if (status == QLatin1String("away")) { return QStringLiteral("user-away"); } else if (status == QLatin1String("offline")) { return QStringLiteral("user-offline"); } else { qCDebug(RUQOLA_LOG) << "Problem with status " << status; return QStringLiteral("unknown"); } qCDebug(RUQOLA_LOG) << " QString User::iconFromStatus() const" << status; return {}; } User::PresenceStatus Utils::presenceStatusFromString(const QString &status) { if (status == QLatin1String("online")) { return User::PresenceStatus::PresenceOnline; } else if (status == QLatin1String("busy")) { return User::PresenceStatus::PresenceBusy; } else if (status == QLatin1String("away")) { return User::PresenceStatus::PresenceAway; } else if (status == QLatin1String("offline")) { return User::PresenceStatus::PresenceOffline; } else { qCDebug(RUQOLA_LOG) << "Problem with status " << status; return User::PresenceStatus::Unknown; } } void Utils::parseNotification(const QJsonArray &contents, QString &message, QString &title, QString &sender) { QJsonObject obj = contents.at(0).toObject(); message = obj[QStringLiteral("text")].toString(); title = obj[QStringLiteral("title")].toString(); obj = obj.value(QLatin1String("payload")).toObject(); if (!obj.isEmpty()) { obj = obj.value(QLatin1String("sender")).toObject(); if (!obj.isEmpty()) { sender = obj.value(QLatin1String("_id")).toString(); } else { qCDebug(RUQOLA_LOG) << "Problem with notification json: missing sender"; } } else { qCDebug(RUQOLA_LOG) << "Problem with notification json: missing payload"; } } QString Utils::userIdFromDirectChannel(const QString &rid, const QString &userId) { QString newUserId = rid; newUserId.remove(userId); return newUserId; } qint64 Utils::parseDate(const QString &key, const QJsonObject &o) { return o.value(key).toObject().value(QLatin1String("$date")).toDouble(-1); } qint64 Utils::parseIsoDate(const QString &key, const QJsonObject &o) { if (o.contains(key)) { return QDateTime::fromString(o.value(key).toString(), Qt::ISODate).toMSecsSinceEpoch(); } else { return -1; } } QString Utils::convertTextWithUrl(const QString &str) { static const QRegularExpression regularExpressionAHref(QStringLiteral("(.*)")); QString newStr; bool isRef = false; bool isUrl = false; // bool isHasNewRef = false; QString url; QString references; for (int i = 0; i < str.count(); ++i) { const QChar ref = str.at(i); if (ref == QLatin1Char('[')) { isRef = true; } else if (ref == QLatin1Char(']')) { isRef = false; if ((i == str.count() - 1) || (str.at(i+1) != QLatin1Char('('))) { newStr += QLatin1Char('[') + references + QLatin1Char(']'); } // } else if (ref == QLatin1Char('|')) { // isUrl = false; // isRef = true; // isHasNewRef = true; // } else if (ref == QLatin1Char('<')) { // isUrl = true; // } else if (ref == QLatin1Char('>') && isHasNewRef) { // isUrl = false; // isRef = false; // isHasNewRef = false; // if (url.startsWith(QLatin1Char('<'))) { // newStr += url.replace(regularExpressionAHref, QStringLiteral("%1").arg(references)); // } else { // newStr += QStringLiteral("%2").arg(url, references); // } // references.clear(); // url.clear(); } else if (ref == QLatin1Char('(') && !references.isEmpty()) { isUrl = true; } else if (ref == QLatin1Char(')') && !references.isEmpty()) { isUrl = false; // detect whether the string already contains HTML tags if (url.startsWith(QLatin1Char('<'))) { newStr += url.replace(regularExpressionAHref, QStringLiteral("%1").arg(references)); } else { newStr += QStringLiteral("%2").arg(url, references); } references.clear(); url.clear(); } else { if (isRef) { references += ref; } else if (isUrl) { url += ref; } else { newStr += ref; } } } return newStr; } diff --git a/src/rocketchatrestapi-qt5/commands/runcommandjob.cpp b/src/rocketchatrestapi-qt5/commands/runcommandjob.cpp index 450a50f9..7480e6bc 100644 --- a/src/rocketchatrestapi-qt5/commands/runcommandjob.cpp +++ b/src/rocketchatrestapi-qt5/commands/runcommandjob.cpp @@ -1,144 +1,148 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "runcommandjob.h" #include "restapimethod.h" #include "rocketchatqtrestapi_debug.h" #include #include #include #include using namespace RocketChatRestApi; RunCommandJob::RunCommandJob(QObject *parent) : RestApiAbstractJob(parent) { } RunCommandJob::~RunCommandJob() { } bool RunCommandJob::start() { if (!canStart()) { deleteLater(); return false; } QNetworkReply *reply = submitPostRequest(json()); connect(reply, &QNetworkReply::finished, this, &RunCommandJob::slotRunCommand); return true; } void RunCommandJob::slotRunCommand() { auto *reply = qobject_cast(sender()); if (reply) { const QByteArray data = reply->readAll(); const QJsonDocument replyJson = QJsonDocument::fromJson(data); const QJsonObject replyObject = replyJson.object(); if (replyObject[QStringLiteral("success")].toBool()) { addLoggerInfo(QByteArrayLiteral("RunCommandJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); Q_EMIT runCommandDone(); } else { emitFailedMessage(replyObject, reply); addLoggerWarning(QByteArrayLiteral("RunCommandJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); } reply->deleteLater(); } deleteLater(); } RunCommandJob::RunCommandInfo RunCommandJob::runCommandInfo() const { return mRunCommandInfo; } void RunCommandJob::setRunCommandInfo(const RunCommandInfo &runCommandInfo) { mRunCommandInfo = runCommandInfo; } RunCommandJob::RunCommandInfo RunCommandJob::parseString(const QString &str, const QString &roomId) { RunCommandJob::RunCommandInfo info; if (str.length() > 1) { QString newStr = str.mid(1); +#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) + QStringList lst = newStr.split(QLatin1Char(' '), QString::SkipEmptyParts); +#else QStringList lst = newStr.split(QLatin1Char(' '), Qt::SkipEmptyParts); +#endif const int numberElement = lst.count(); info.commandName = lst.takeAt(0); info.roomId = roomId; if (numberElement == 2) { info.params = lst.join(QLatin1Char(' ')); } } return info; } bool RunCommandJob::requireHttpAuthentication() const { return true; } bool RunCommandJob::canStart() const { if (!RestApiAbstractJob::canStart()) { return false; } if (!mRunCommandInfo.isValid()) { qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RunCommandJob: RoomId and CommandName are empty"; return false; } return true; } QNetworkRequest RunCommandJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::CommandsRun); QNetworkRequest request(url); addAuthRawHeader(request); addRequestAttribute(request); return request; } QJsonDocument RunCommandJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("command")] = mRunCommandInfo.commandName; jsonObj[QLatin1String("roomId")] = mRunCommandInfo.roomId; if (!mRunCommandInfo.threadMessageId.isEmpty()) { jsonObj[QLatin1String("tmid")] = mRunCommandInfo.threadMessageId; } if (!mRunCommandInfo.triggerId.isEmpty()) { jsonObj[QLatin1String("triggerId")] = mRunCommandInfo.triggerId; } if (!mRunCommandInfo.params.isEmpty()) { jsonObj[QLatin1String("params")] = mRunCommandInfo.params; } const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } bool RunCommandJob::RunCommandInfo::isValid() const { return !commandName.isEmpty() && !roomId.isEmpty(); } diff --git a/src/widgets/dialogs/autotranslateconfigurewidget.cpp b/src/widgets/dialogs/autotranslateconfigurewidget.cpp index 1b899d92..bbbcb95f 100644 --- a/src/widgets/dialogs/autotranslateconfigurewidget.cpp +++ b/src/widgets/dialogs/autotranslateconfigurewidget.cpp @@ -1,100 +1,101 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "autotranslateconfigurewidget.h" #include "model/autotranslatelanguagesmodel.h" #include "ruqola.h" #include "rocketchataccount.h" #include "roomwrapper.h" #include #include #include #include #include #include AutoTranslateConfigureWidget::AutoTranslateConfigureWidget(QWidget *parent) : QWidget(parent) { QVBoxLayout *mainLayout = new QVBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainLayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mAutoTranslate = new QCheckBox(i18n("Auto-Translate"), this); mAutoTranslate->setObjectName(QStringLiteral("mAutoTranslate")); mainLayout->addWidget(mAutoTranslate); connect(mAutoTranslate, &QCheckBox::clicked, this, &AutoTranslateConfigureWidget::slotChangeAutoTranslate); QHBoxLayout *horizontalLayout = new QHBoxLayout; horizontalLayout->setObjectName(QStringLiteral("horizontalLayout")); horizontalLayout->setContentsMargins(0, 0, 0, 0); mainLayout->addLayout(horizontalLayout); QLabel *label = new QLabel(i18n("Language:"), this); label->setObjectName(QStringLiteral("label")); horizontalLayout->addWidget(label); mLanguage = new QComboBox(this); mLanguage->setObjectName(QStringLiteral("mLanguage")); mLanguage->setModel(Ruqola::self()->rocketChatAccount()->autoTranslateLanguagesModel()); - connect(mLanguage, &QComboBox::activated, this, &AutoTranslateConfigureWidget::slotLanguageChanged); + connect(mLanguage, QOverload::of(&QComboBox::activated), + this, &AutoTranslateConfigureWidget::slotLanguageChanged); horizontalLayout->addWidget(mLanguage); mainLayout->addStretch(1); } AutoTranslateConfigureWidget::~AutoTranslateConfigureWidget() { } RoomWrapper *AutoTranslateConfigureWidget::roomWrapper() const { return mRoomWrapper; } void AutoTranslateConfigureWidget::slotLanguageChanged(int index) { Ruqola::self()->rocketChatAccount()->autoTranslateSaveLanguageSettings(mRoomWrapper->roomId(), Ruqola::self()->rocketChatAccount()->autoTranslateLanguagesModel()->selectedLanguage(index)); } void AutoTranslateConfigureWidget::slotChangeAutoTranslate(bool status) { Ruqola::self()->rocketChatAccount()->autoTranslateSaveAutoTranslateSettings(mRoomWrapper->roomId(), status); } void AutoTranslateConfigureWidget::slotAutoTranslateChanged() { mAutoTranslate->setChecked(mRoomWrapper->autoTranslate()); } void AutoTranslateConfigureWidget::slotAutoTranslateLanguageChanged() { mLanguage->setCurrentIndex(Ruqola::self()->rocketChatAccount()->autoTranslateLanguagesModel()->currentLanguage(mRoomWrapper->autoTranslateLanguage())); } void AutoTranslateConfigureWidget::setRoomWrapper(RoomWrapper *roomWrapper) { mRoomWrapper = roomWrapper; connect(mRoomWrapper, &RoomWrapper::autoTranslateChanged, this, &AutoTranslateConfigureWidget::slotAutoTranslateChanged); connect(mRoomWrapper, &RoomWrapper::autoTranslateLanguageChanged, this, &AutoTranslateConfigureWidget::slotAutoTranslateLanguageChanged); slotAutoTranslateChanged(); slotAutoTranslateLanguageChanged(); }