From e5d549552614f89dd73b29fc3ee4710f65bb1e57 Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 20 Mar 2012 18:01:20 +0100 Subject: [PATCH] Make QCoreApplication::applicationName() default to argv[0] This makes it more useful in all the Qt apps that don't set it, given that it's used internally by QTemporaryFile, QTemporaryDir, QStandardPaths, QDBus, QAccessibleApplication, etc. Qt4 compatibility in the deprecated QDesktopServices is preserved, no fallback there. Change-Id: I584463507cf917a3720793c6bd45d07c60f8356c Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcoreapplication.cpp | 13 ++++++++++- src/gui/util/qdesktopservices.cpp | 27 ++++++++++++++-------- .../io/qstandardpaths/tst_qstandardpaths.cpp | 14 ++++------- .../qcoreapplication/tst_qcoreapplication.cpp | 3 ++- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 967ed44..3d09d85 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1943,7 +1943,9 @@ QString QCoreApplication::organizationDomain() using the empty constructor. This saves having to repeat this information each time a QSettings object is created. - \sa organizationName organizationDomain applicationVersion + If not set, the application name defaults to the executable name (since 5.0). + + \sa organizationName organizationDomain applicationVersion applicationFilePath */ void QCoreApplication::setApplicationName(const QString &application) { @@ -1952,6 +1954,15 @@ void QCoreApplication::setApplicationName(const QString &application) QString QCoreApplication::applicationName() { + QString appname = coreappdata()->application; + if (appname.isEmpty() && QCoreApplication::self) + appname = QCoreApplication::self->d_func()->appName(); + return appname; +} + +// Exported for QDesktopServices (Qt4 behavior compatibility) +Q_CORE_EXPORT QString qt_applicationName_noFallback() +{ return coreappdata()->application; } diff --git a/src/gui/util/qdesktopservices.cpp b/src/gui/util/qdesktopservices.cpp index 1a66614..26aecad 100644 --- a/src/gui/util/qdesktopservices.cpp +++ b/src/gui/util/qdesktopservices.cpp @@ -287,17 +287,26 @@ void QDesktopServices::unsetUrlHandler(const QString &scheme) QString QDesktopServices::storageLocationImpl(StandardLocation type) { -#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC) if (type == DataLocation) { - QString xdgDataHome = QLatin1String(qgetenv("XDG_DATA_HOME")); - if (xdgDataHome.isEmpty()) - xdgDataHome = QDir::homePath() + QLatin1String("/.local/share"); - xdgDataHome += QLatin1String("/data/") - + QCoreApplication::organizationName() + QLatin1Char('/') - + QCoreApplication::applicationName(); - return xdgDataHome; - } + // Preserve Qt 4 compatibility: + // * QCoreApplication::applicationName() must default to empty + // * Unix data location is under the "data/" subdirectory + extern Q_CORE_EXPORT QString qt_applicationName_noFallback(); + const QString compatAppName = qt_applicationName_noFallback(); + const QString baseDir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); +#if defined(Q_OS_WIN) || defined(Q_OS_MAC) + QString result = baseDir; + if (!QCoreApplication::organizationName().isEmpty()) + result += QLatin1Char('/') + QCoreApplication::organizationName(); + if (!compatAppName.isEmpty()) + result += QLatin1Char('/') + compatAppName; + return result; +#elif defined(Q_OS_UNIX) + return baseDir + QLatin1String("/data/") + + QCoreApplication::organizationName() + QLatin1Char('/') + + compatAppName; #endif + } return QStandardPaths::writableLocation(static_cast(type)); } diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp index a6eabbb..e9af294 100644 --- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp +++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp @@ -186,18 +186,12 @@ void tst_qstandardpaths::testDataLocation() { // On all platforms, DataLocation should be GenericDataLocation / organization name / app name // This allows one app to access the data of another app. - { - const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); - const QString app = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - QCOMPARE(base, app); - } + const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); + QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::DataLocation), base + "/tst_qstandardpaths"); QCoreApplication::instance()->setOrganizationName("Qt"); + QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::DataLocation), base + "/Qt/tst_qstandardpaths"); QCoreApplication::instance()->setApplicationName("QtTest"); - { - const QString base = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); - const QString app = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - QCOMPARE(app, base + "/Qt/QtTest"); - } + QCOMPARE(QStandardPaths::writableLocation(QStandardPaths::DataLocation), base + "/Qt/QtTest"); #ifdef Q_XDG_PLATFORM setDefaultLocations(); diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp index 84d723c..f4aefb2 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp @@ -121,7 +121,8 @@ void tst_QCoreApplication::qAppName() int argc = 1; char *argv[] = { const_cast("tst_qcoreapplication") }; QCoreApplication app(argc, argv); - QVERIFY(!::qAppName().isEmpty()); + QCOMPARE(::qAppName(), QString::fromLatin1("tst_qcoreapplication")); + QCOMPARE(QCoreApplication::applicationName(), QString::fromLatin1("tst_qcoreapplication")); } void tst_QCoreApplication::argc() -- 2.7.4