Make QCoreApplication::applicationName() default to argv[0]
authorDavid Faure <faure@kde.org>
Tue, 20 Mar 2012 17:01:20 +0000 (18:01 +0100)
committerQt by Nokia <qt-info@nokia.com>
Tue, 27 Mar 2012 22:51:32 +0000 (00:51 +0200)
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 <thiago.macieira@intel.com>
src/corelib/kernel/qcoreapplication.cpp
src/gui/util/qdesktopservices.cpp
tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp
tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp

index 967ed44..3d09d85 100644 (file)
@@ -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;
 }
 
index 1a66614..26aecad 100644 (file)
@@ -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<QStandardPaths::StandardLocation>(type));
 }
 
index a6eabbb..e9af294 100644 (file)
@@ -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();
index 84d723c..f4aefb2 100644 (file)
@@ -121,7 +121,8 @@ void tst_QCoreApplication::qAppName()
     int argc = 1;
     char *argv[] = { const_cast<char*>("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()