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
This commit is contained in:
Alexander Lohnau 2023-07-31 06:08:37 +02:00 committed by Albert Vaca Cintora
parent b356c742c2
commit 83888412a9
6 changed files with 12 additions and 14 deletions

View file

@ -104,7 +104,7 @@ void LanLinkProvider::onStart()
QAbstractSocket::SocketError sockErr = m_udpSocket.error(); QAbstractSocket::SocketError sockErr = m_udpSocket.error();
// Refer to https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum to decode socket error number // Refer to https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum to decode socket error number
QString errorMessage = QString::fromLatin1(QMetaEnum::fromType<QAbstractSocket::SocketError>().valueToKey(sockErr)); QString errorMessage = QString::fromLatin1(QMetaEnum::fromType<QAbstractSocket::SocketError>().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); Q_ASSERT(success);

View file

@ -84,8 +84,8 @@ void DBusInstancePrivate::launchDBusDaemon()
kdeconnectDBusConfiguration = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dbus-1/session.conf")); kdeconnectDBusConfiguration = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("dbus-1/session.conf"));
} else { } else {
// macOS Debug env // macOS Debug env
dbusDaemonExecutable = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../bin/dbus-daemon"); dbusDaemonExecutable = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../bin/dbus-daemon");
kdeconnectDBusConfiguration = QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../share/dbus-1/session.conf"); kdeconnectDBusConfiguration = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../share/dbus-1/session.conf");
} }
m_dbusProcess->setProgram(dbusDaemonExecutable); m_dbusProcess->setProgram(dbusDaemonExecutable);
m_dbusProcess->setArguments({QStringLiteral("--print-address"), m_dbusProcess->setArguments({QStringLiteral("--print-address"),

View file

@ -446,13 +446,13 @@ QString Device::encryptionInfo() const
QString localChecksum = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex()); QString localChecksum = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex());
for (int i = 2; i < localChecksum.size(); i += 3) { 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); result += i18n("SHA256 fingerprint of your device certificate is: %1\n", localChecksum);
QString remoteChecksum = QString::fromLatin1(certificate().digest(digestAlgorithm).toHex()); QString remoteChecksum = QString::fromLatin1(certificate().digest(digestAlgorithm).toHex());
for (int i = 2; i < remoteChecksum.size(); i += 3) { 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); result += i18n("SHA256 fingerprint of remote device certificate is: %1\n", remoteChecksum);

View file

@ -127,10 +127,10 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
} }
// Start kdeconnectd, the daemon will not duplicate when there is already one // Start kdeconnectd, the daemon will not duplicate when there is already one
if (QFile::exists(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd"))) { if (QString daemon = QCoreApplication::applicationDirPath() + QLatin1String("/kdeconnectd"); QFile::exists(daemon)) {
kdeconnectd.setProgram(QCoreApplication::applicationDirPath() + QStringLiteral("/kdeconnectd")); kdeconnectd.setProgram(daemon);
} else if (QFile::exists(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd"))) { } else if (QString daemon = QLatin1String(qgetenv("craftRoot")) + QLatin1String("/../lib/libexec/kdeconnectd"); QFile::exists(daemon)) {
kdeconnectd.setProgram(QString::fromLatin1(qgetenv("craftRoot")) + QStringLiteral("/../lib/libexec/kdeconnectd")); kdeconnectd.setProgram(daemon);
} else { } else {
QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot find kdeconnectd"), QMessageBox::Abort, QMessageBox::Abort); QMessageBox::critical(nullptr, i18n("KDE Connect"), i18n("Cannot find kdeconnectd"), QMessageBox::Abort, QMessageBox::Abort);
return -1; return -1;

View file

@ -59,11 +59,9 @@ class QObject;
*/ */
#ifdef Q_OS_WIN #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 #else
Q_GLOBAL_STATIC_WITH_ARGS(QString, Q_GLOBAL_STATIC_WITH_ARGS(QString, vcardsLocation, (QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kpeoplevcard")))
vcardsLocation,
(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QString::fromLatin1("/kpeoplevcard")))
#endif #endif
#define VCARD_EXTENSION QStringLiteral(".vcf") #define VCARD_EXTENSION QStringLiteral(".vcf")

View file

@ -27,7 +27,7 @@ void FindMyPhonePlugin::ring()
QString FindMyPhonePlugin::dbusPath() const 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" #include "findmyphoneplugin.moc"