From 395a88a5efa600d31d5ee9d11a86e25a9f832d8c Mon Sep 17 00:00:00 2001 From: Gianluca Recchia Date: Fri, 25 Jan 2019 04:45:05 +0100 Subject: [PATCH] Prevent post-compilation Qt downgrades (#2576) * Abort the app if a Qt downgrade is detected The app will now exit immediately if it was compiled with a Qt version higher than the one present on the machine. * Add function for checking the Qt version at runtime * Re-register global D-Bus menu only if DE is Unity --- src/core/Tools.h | 20 ++++++++++++++++++-- src/gui/MainWindow.cpp | 14 +++++++++----- src/main.cpp | 2 ++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/core/Tools.h b/src/core/Tools.h index f6d0daaa8..6d190a466 100644 --- a/src/core/Tools.h +++ b/src/core/Tools.h @@ -41,8 +41,10 @@ namespace Tools void sleep(int ms); void wait(int ms); QString uuidToHex(const QUuid& uuid); - QRegularExpression convertToRegex(const QString& string, bool useWildcards = false, - bool exactMatch = false, bool caseSensitive = false); + QRegularExpression convertToRegex(const QString& string, + bool useWildcards = false, + bool exactMatch = false, + bool caseSensitive = false); template RandomAccessIterator binaryFind(RandomAccessIterator begin, RandomAccessIterator end, const T& value) @@ -83,6 +85,20 @@ namespace Tools void clear(); QByteArray content() const; }; + + inline int qtRuntimeVersion() + { + // Cache the result since the Qt version can't change during + // the execution, computing it once will be enough + const static int version = []() { + const auto sq = QString::fromLatin1(qVersion()); + return (sq.section(QChar::fromLatin1('.'), 0, 0).toInt() << 16) + + (sq.section(QChar::fromLatin1('.'), 1, 1).toInt() << 8) + + (sq.section(QChar::fromLatin1('.'), 2, 2).toInt()); + }(); + + return version; + } } // namespace Tools #endif // KEEPASSX_TOOLS_H diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 7147808d7..8e957c926 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -33,6 +33,7 @@ #include "core/FilePath.h" #include "core/InactivityTimer.h" #include "core/Metadata.h" +#include "core/Tools.h" #include "gui/AboutDialog.h" #include "gui/DatabaseWidget.h" #include "gui/SearchWidget.h" @@ -1017,11 +1018,14 @@ void MainWindow::toggleWindow() // see https://github.com/keepassxreboot/keepassxc/issues/271 // and https://bugreports.qt.io/browse/QTBUG-58723 // check for !isVisible(), because isNativeMenuBar() does not work with appmenu-qt5 - if (!m_ui->menubar->isVisible()) { - QDBusMessage msg = QDBusMessage::createMethodCall("com.canonical.AppMenu.Registrar", - "/com/canonical/AppMenu/Registrar", - "com.canonical.AppMenu.Registrar", - "RegisterWindow"); + const static auto isDesktopSessionUnity = qgetenv("XDG_CURRENT_DESKTOP") == "Unity"; + + if (isDesktopSessionUnity && Tools::qtRuntimeVersion() < QT_VERSION_CHECK(5, 9, 0) + && !m_ui->menubar->isVisible()) { + QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.canonical.AppMenu.Registrar"), + QStringLiteral("/com/canonical/AppMenu/Registrar"), + QStringLiteral("com.canonical.AppMenu.Registrar"), + QStringLiteral("RegisterWindow")); QList args; args << QVariant::fromValue(static_cast(winId())) << QVariant::fromValue(QDBusObjectPath("/MenuBar/1")); diff --git a/src/main.cpp b/src/main.cpp index 81192f977..548bee79b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,6 +46,8 @@ Q_IMPORT_PLUGIN(QXcbIntegrationPlugin) int main(int argc, char** argv) { + QT_REQUIRE_VERSION(argc, argv, QT_VERSION_STR) + #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif