QGuiApplication: Remove .desktop suffix in setDesktopFileName

The desktop file name should not contain ".desktop" suffix, but some
applications still specify it anyway because of the ambiguity in the
documentation that was fixed in
0c5135a9df.

This change makes setDesktopFileName remove ".desktop" suffix so
desktopFileName always returns a desktop file name with correct format
and its users don't need to chop ".desktop".

Pick-to: 6.5
Change-Id: If5abccaf3bf976449cada8891fff887870e45b5f
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Vlad Zahorodnii 2023-05-16 22:01:03 +03:00
parent d68dbd8b7d
commit fb86c5a470
2 changed files with 12 additions and 2 deletions

View File

@ -19,6 +19,7 @@
#include <QtCore/QAbstractEventDispatcher> #include <QtCore/QAbstractEventDispatcher>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QStandardPaths>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtCore/private/qcoreapplication_p.h> #include <QtCore/private/qcoreapplication_p.h>
#include <QtCore/private/qabstracteventdispatcher_p.h> #include <QtCore/private/qabstracteventdispatcher_p.h>
@ -792,6 +793,15 @@ void QGuiApplication::setDesktopFileName(const QString &name)
if (!QGuiApplicationPrivate::desktopFileName) if (!QGuiApplicationPrivate::desktopFileName)
QGuiApplicationPrivate::desktopFileName = new QString; QGuiApplicationPrivate::desktopFileName = new QString;
*QGuiApplicationPrivate::desktopFileName = name; *QGuiApplicationPrivate::desktopFileName = name;
if (name.endsWith(QLatin1String(".desktop"))) { // ### Qt 7: remove
const QString filePath = QStandardPaths::locate(QStandardPaths::ApplicationsLocation, name);
if (!filePath.isEmpty()) {
qWarning("QGuiApplication::setDesktopFileName: the specified desktop file name "
"ends with .desktop. For compatibility reasons, the .desktop suffix will "
"be removed. Please specify a desktop file name without .desktop suffix");
(*QGuiApplicationPrivate::desktopFileName).chop(8);
}
}
} }
QString QGuiApplication::desktopFileName() QString QGuiApplication::desktopFileName()

View File

@ -122,8 +122,8 @@ void tst_QGuiApplication::desktopFileName()
QCOMPARE(QGuiApplication::desktopFileName(), QString()); QCOMPARE(QGuiApplication::desktopFileName(), QString());
QGuiApplication::setDesktopFileName("io.qt.QGuiApplication.desktop"); QGuiApplication::setDesktopFileName("io.qt.QGuiApplication");
QCOMPARE(QGuiApplication::desktopFileName(), QString::fromLatin1("io.qt.QGuiApplication.desktop")); QCOMPARE(QGuiApplication::desktopFileName(), QString::fromLatin1("io.qt.QGuiApplication"));
QGuiApplication::setDesktopFileName(QString()); QGuiApplication::setDesktopFileName(QString());
QCOMPARE(QGuiApplication::desktopFileName(), QString()); QCOMPARE(QGuiApplication::desktopFileName(), QString());