From 2b928ef6f95c528449d6ec9650325f3ccaac5094 Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Mon, 19 Dec 2016 18:56:46 -0800 Subject: Provide sensible defaults for QCoreApplication::applicationVersion [ChangeLog][QtCore] QCoreApplication::applicationVersion now defaults to an appropriate platform-specific value. On Windows, it defaults to the PRODUCTVERSION parameter of the VERSIONINFO resource for classic desktop apps, and the version attribute of the application package manifest for Univeral Windows Platform apps. On Apple Platforms (macOS, iOS, tvOS, watchOS), it defaults to the CFBundleVersion property of the information property list (Info.plist) file. On Android, it defaults to the android:versionName attribute of the AndroidManifest.xml manifest element. On other platforms, the default remains an empty string. Task-number: QTBUG-57715 Change-Id: I26f83dd00737e06f4321cf962aa5fab8398104ec Reviewed-by: Thiago Macieira --- src/corelib/kernel/kernel.pri | 2 + src/corelib/kernel/qcoreapplication.cpp | 85 ++++++++++++++++++++++++++--- src/corelib/kernel/qcoreapplication_p.h | 5 +- src/corelib/kernel/qcoreapplication_win.cpp | 68 +++++++++++++++++++++++ 4 files changed, 149 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/kernel.pri b/src/corelib/kernel/kernel.pri index 3744b2b36e..461fbd7840 100644 --- a/src/corelib/kernel/kernel.pri +++ b/src/corelib/kernel/kernel.pri @@ -88,6 +88,8 @@ win32 { SOURCES += kernel/qeventdispatcher_win.cpp HEADERS += kernel/qeventdispatcher_win_p.h } + + !winrt: LIBS_PRIVATE += -lversion } winrt { diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 3796df5614..f025678b5d 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -95,6 +95,11 @@ #endif #endif // QT_NO_QOBJECT +#if defined(Q_OS_ANDROID) +# include +# include +#endif + #ifdef Q_OS_MAC # include "qcore_mac_p.h" #endif @@ -144,11 +149,13 @@ int QCoreApplicationPrivate::app_compile_version = 0x050000; //we don't know exa bool QCoreApplicationPrivate::setuidAllowed = false; #if !defined(Q_OS_WIN) -#ifdef Q_OS_MAC -QString QCoreApplicationPrivate::macMenuBarName() +#ifdef Q_OS_DARWIN +QString QCoreApplicationPrivate::infoDictionaryStringProperty(const QString &propertyName) { QString bundleName; - CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), CFSTR("CFBundleName")); + QCFString cfPropertyName = propertyName.toCFString(); + CFTypeRef string = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), + cfPropertyName); if (string) bundleName = QString::fromCFString(static_cast(string)); return bundleName; @@ -157,8 +164,8 @@ QString QCoreApplicationPrivate::macMenuBarName() QString QCoreApplicationPrivate::appName() const { QString applicationName; -#ifdef Q_OS_MAC - applicationName = macMenuBarName(); +#ifdef Q_OS_DARWIN + applicationName = infoDictionaryStringProperty(QStringLiteral("CFBundleName")); #endif if (applicationName.isEmpty() && argv[0]) { char *p = strrchr(argv[0], '/'); @@ -167,6 +174,34 @@ QString QCoreApplicationPrivate::appName() const return applicationName; } +QString QCoreApplicationPrivate::appVersion() const +{ + QString applicationVersion; +#ifndef QT_BOOTSTRAPPED +# ifdef Q_OS_DARWIN + applicationVersion = infoDictionaryStringProperty(QStringLiteral("CFBundleVersion")); +# elif defined(Q_OS_ANDROID) + QJNIObjectPrivate context(QtAndroidPrivate::context()); + if (context.isValid()) { + QJNIObjectPrivate pm = context.callObjectMethod( + "getPackageManager", "()Landroid/content/pm/PackageManager;"); + QJNIObjectPrivate pn = context.callObjectMethod("getPackageName"); + if (pm.isValid() && pn.isValid()) { + QJNIObjectPrivate packageInfo = pm.callObjectMethod( + "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;", + pn.object(), 0); + if (packageInfo.isValid()) { + QJNIObjectPrivate versionName = packageInfo.getObjectField( + "versionName", "Ljava/lang/String;"); + if (versionName.isValid()) + return versionName.toString(); + } + } + } +# endif +#endif + return applicationVersion; +} #endif QString *QCoreApplicationPrivate::cachedApplicationFilePath = 0; @@ -332,6 +367,7 @@ uint QCoreApplicationPrivate::attribs = struct QCoreApplicationData { QCoreApplicationData() Q_DECL_NOTHROW { applicationNameSet = false; + applicationVersionSet = false; } ~QCoreApplicationData() { #ifndef QT_NO_QOBJECT @@ -347,6 +383,7 @@ struct QCoreApplicationData { QString application; // application name, initially from argv[0], can then be modified. QString applicationVersion; bool applicationNameSet; // true if setApplicationName was called + bool applicationVersionSet; // true if setApplicationVersion was called #ifndef QT_NO_LIBRARY QScopedPointer app_libpaths; @@ -742,10 +779,13 @@ void QCoreApplicationPrivate::init() Q_ASSERT_X(!QCoreApplication::self, "QCoreApplication", "there should be only one application object"); QCoreApplication::self = q; - // Store app name (so it's still available after QCoreApplication is destroyed) + // Store app name/version (so they're still available after QCoreApplication is destroyed) if (!coreappdata()->applicationNameSet) coreappdata()->application = appName(); + if (!coreappdata()->applicationVersionSet) + coreappdata()->applicationVersion = appVersion(); + QLoggingRegistry::instance()->init(); #ifndef QT_NO_LIBRARY @@ -2403,6 +2443,29 @@ Q_CORE_EXPORT QString qt_applicationName_noFallback() \since 4.4 \brief the version of this application + If not set, the application version defaults to a platform-specific value + determined from the main application executable or package (since Qt 5.9): + + \table + \header + \li Platform + \li Source + \row + \li Windows (classic desktop) + \li PRODUCTVERSION parameter of the VERSIONINFO resource + \row + \li Universal Windows Platform + \li version attribute of the application package manifest + \row + \li macOS, iOS, tvOS, watchOS + \li CFBundleVersion property of the information property list + \row + \li Android + \li android:versionName property of the AndroidManifest.xml manifest element + \endtable + + On other platforms, the default is the empty string. + \sa applicationName, organizationName, organizationDomain */ /*! @@ -2413,9 +2476,13 @@ Q_CORE_EXPORT QString qt_applicationName_noFallback() */ void QCoreApplication::setApplicationVersion(const QString &version) { - if (coreappdata()->applicationVersion == version) + coreappdata()->applicationVersionSet = !version.isEmpty(); + QString newVersion = version; + if (newVersion.isEmpty() && QCoreApplication::self) + newVersion = QCoreApplication::self->d_func()->appVersion(); + if (coreappdata()->applicationVersion == newVersion) return; - coreappdata()->applicationVersion = version; + coreappdata()->applicationVersion = newVersion; #ifndef QT_NO_QOBJECT if (QCoreApplication::self) emit QCoreApplication::self->applicationVersionChanged(); @@ -2424,7 +2491,7 @@ void QCoreApplication::setApplicationVersion(const QString &version) QString QCoreApplication::applicationVersion() { - return coreappdata()->applicationVersion; + return coreappdata() ? coreappdata()->applicationVersion : QString(); } #ifndef QT_NO_LIBRARY diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h index 3601add098..c646786296 100644 --- a/src/corelib/kernel/qcoreapplication_p.h +++ b/src/corelib/kernel/qcoreapplication_p.h @@ -83,9 +83,10 @@ public: void init(); QString appName() const; + QString appVersion() const; -#ifdef Q_OS_MAC - static QString macMenuBarName(); +#ifdef Q_OS_DARWIN + static QString infoDictionaryStringProperty(const QString &propertyName); #endif static void initLocale(); diff --git a/src/corelib/kernel/qcoreapplication_win.cpp b/src/corelib/kernel/qcoreapplication_win.cpp index 67e509eeef..4057c03b84 100644 --- a/src/corelib/kernel/qcoreapplication_win.cpp +++ b/src/corelib/kernel/qcoreapplication_win.cpp @@ -51,6 +51,16 @@ #include #include +#ifdef Q_OS_WINRT +#include +#include +#include +#include +using namespace ABI::Windows::ApplicationModel; +using namespace Microsoft::WRL; +using namespace Microsoft::WRL::Wrappers; +#endif + QT_BEGIN_NAMESPACE int appCmdShow = 0; @@ -118,6 +128,64 @@ QString QCoreApplicationPrivate::appName() const return QFileInfo(qAppFileName()).baseName(); } +QString QCoreApplicationPrivate::appVersion() const +{ + QString applicationVersion; +#ifndef QT_BOOTSTRAPPED +# ifdef Q_OS_WINRT + HRESULT hr; + + ComPtr packageFactory; + hr = RoGetActivationFactory( + HString::MakeReference(RuntimeClass_Windows_ApplicationModel_Package).Get(), + IID_PPV_ARGS(&packageFactory)); + RETURN_IF_FAILED("Failed to create package instance", return QString()); + + ComPtr package; + packageFactory->get_Current(&package); + RETURN_IF_FAILED("Failed to get current application package", return QString()); + + ComPtr packageId; + package->get_Id(&packageId); + RETURN_IF_FAILED("Failed to get current application package ID", return QString()); + + PackageVersion version; + packageId->get_Version(&version); + RETURN_IF_FAILED("Failed to get current application package version", return QString()); + + applicationVersion = QStringLiteral("%1.%2.%3.%4") + .arg(version.Major) + .arg(version.Minor) + .arg(version.Build) + .arg(version.Revision); +# else + const QString appFileName = qAppFileName(); + QVarLengthArray buffer(appFileName.size() + 1); + buffer[appFileName.toWCharArray(buffer.data())] = 0; + + DWORD versionInfoSize = GetFileVersionInfoSize(buffer.data(), nullptr); + if (versionInfoSize) { + QVarLengthArray info(static_cast(versionInfoSize)); + if (GetFileVersionInfo(buffer.data(), 0, versionInfoSize, info.data())) { + UINT size; + DWORD *fi; + + if (VerQueryValue(info.data(), __TEXT("\\"), + reinterpret_cast(&fi), &size) && size) { + const VS_FIXEDFILEINFO *verInfo = reinterpret_cast(fi); + applicationVersion = QStringLiteral("%1.%2.%3.%4") + .arg(HIWORD(verInfo->dwProductVersionMS)) + .arg(LOWORD(verInfo->dwProductVersionMS)) + .arg(HIWORD(verInfo->dwProductVersionLS)) + .arg(LOWORD(verInfo->dwProductVersionLS)); + } + } + } +# endif +#endif + return applicationVersion; +} + #endif // !(defined(Q_OS_WINRT) && _MSC_VER < 1900) #ifndef Q_OS_WINRT -- cgit v1.2.3