From 83888412a983968a15b9c0e278acfbffb0de2747 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Mon, 31 Jul 2023 06:08:37 +0200 Subject: [PATCH] Simplify and optimize some string usages - Using QLatin1String when concatinating strings is faster, because they are more lightweight. For the resulting string, we need to allocate new memory anyway - Use QLatin1String overloads where they are provided by Qt APIs - Just use const char* for log messages, the quoting of QStrings is not needed - Make sure to reuse string results when possible --- core/backends/lan/lanlinkprovider.cpp | 2 +- core/dbushelper.cpp | 4 ++-- core/device.cpp | 4 ++-- indicator/indicatorhelper_mac.cpp | 8 ++++---- plugins/contacts/contactsplugin.h | 6 ++---- plugins/findmyphone/findmyphoneplugin.cpp | 2 +- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 13c73af99..b09996007 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -104,7 +104,7 @@ void LanLinkProvider::onStart() QAbstractSocket::SocketError sockErr = m_udpSocket.error(); // Refer to https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum to decode socket error number QString errorMessage = QString::fromLatin1(QMetaEnum::fromType().valueToKey(sockErr)); - qCritical(KDECONNECT_CORE) << QLatin1String("Failed to bind UDP socket on port") << m_udpListenPort << QLatin1String("with error") << errorMessage; + qCritical(KDECONNECT_CORE) << "Failed to bind UDP socket on port" << m_udpListenPort << "with error" << errorMessage; } Q_ASSERT(success); diff --git a/core/dbushelper.cpp b/core/dbushelper.cpp index fddc5fd8c..e78bb1659 100644 --- a/core/dbushelper.cpp +++ b/core/dbushelper.cpp @@ -84,8 +84,8 @@ void DBusInstancePrivate::launchDBusDaemon() kdeconnectDBusConfiguration = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dbus-1/session.conf")); } else { // macOS Debug env - dbusDaemonExecutable = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../bin/dbus-daemon"); - kdeconnectDBusConfiguration = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../share/dbus-1/session.conf"); + dbusDaemonExecutable = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../bin/dbus-daemon"); + kdeconnectDBusConfiguration = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../share/dbus-1/session.conf"); } m_dbusProcess->setProgram(dbusDaemonExecutable); m_dbusProcess->setArguments({QStringLiteral("--print-address"), diff --git a/core/device.cpp b/core/device.cpp index 6d8644dac..a11ca659b 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -446,13 +446,13 @@ QString Device::encryptionInfo() const QString localChecksum = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex()); for (int i = 2; i < localChecksum.size(); i += 3) { - localChecksum.insert(i, QStringLiteral(":")); // Improve readability + localChecksum.insert(i, QLatin1Char(':')); // Improve readability } result += i18n("SHA256 fingerprint of your device certificate is: %1\n", localChecksum); QString remoteChecksum = QString::fromLatin1(certificate().digest(digestAlgorithm).toHex()); for (int i = 2; i < remoteChecksum.size(); i += 3) { - remoteChecksum.insert(i, QStringLiteral(":")); // Improve readability + remoteChecksum.insert(i, QLatin1Char(':')); // Improve readability } result += i18n("SHA256 fingerprint of remote device certificate is: %1\n", remoteChecksum); diff --git a/indicator/indicatorhelper_mac.cpp b/indicator/indicatorhelper_mac.cpp index ba046840a..eb8a604eb 100644 --- a/indicator/indicatorhelper_mac.cpp +++ b/indicator/indicatorhelper_mac.cpp @@ -127,10 +127,10 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd) } // Start kdeconnectd, the daemon will not duplicate when there is already one - if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) { - kdeconnectd.setProgram(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd")); - } else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) { - kdeconnectd.setProgram(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd")); + if (QString daemon = QCoreApplication::applicationDirPath() + QLatin1String("/kdeconnectd"); QFile::exists(daemon)) { + kdeconnectd.setProgram(daemon); + } else if (QString daemon = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../lib/libexec/kdeconnectd"); QFile::exists(daemon)) { + kdeconnectd.setProgram(daemon); } else { QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot find kdeconnectd"), QMessageBox::Abort, QMessageBox::Abort); return -1; diff --git a/plugins/contacts/contactsplugin.h b/plugins/contacts/contactsplugin.h index 07ccf5156..00cc95b80 100644 --- a/plugins/contacts/contactsplugin.h +++ b/plugins/contacts/contactsplugin.h @@ -59,11 +59,9 @@ class QObject; */ #ifdef Q_OS_WIN -Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QString::fromLatin1("/Contacts"))) +Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + QLatin1String("/Contacts"))) #else -Q_GLOBAL_STATIC_WITH_ARGS(QString, - vcardsLocation, - (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString::fromLatin1("/kpeoplevcard"))) +Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kpeoplevcard"))) #endif #define VCARD_EXTENSION QStringLiteral(".vcf") diff --git a/plugins/findmyphone/findmyphoneplugin.cpp b/plugins/findmyphone/findmyphoneplugin.cpp index 4756e7035..0a4be89f2 100644 --- a/plugins/findmyphone/findmyphoneplugin.cpp +++ b/plugins/findmyphone/findmyphoneplugin.cpp @@ -27,7 +27,7 @@ void FindMyPhonePlugin::ring() QString FindMyPhonePlugin::dbusPath() const { - return QString::fromLatin1("/modules/kdeconnect/devices/") + device()->id() + QString::fromLatin1("/findmyphone"); + return QLatin1String("/modules/kdeconnect/devices/") + device()->id() + QLatin1String("/findmyphone"); } #include "findmyphoneplugin.moc"