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
This commit is contained in:
Gianluca Recchia 2019-01-25 04:45:05 +01:00 committed by Jonathan White
parent b21936f94d
commit 395a88a5ef
3 changed files with 29 additions and 7 deletions

View File

@ -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 <typename RandomAccessIterator, typename T>
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

View File

@ -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<QVariant> args;
args << QVariant::fromValue(static_cast<uint32_t>(winId()))
<< QVariant::fromValue(QDBusObjectPath("/MenuBar/1"));

View File

@ -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