diff --git a/src/plugins/mcusupport/mcupackage.cpp b/src/plugins/mcusupport/mcupackage.cpp index 15b6b589859..2ff913c680e 100644 --- a/src/plugins/mcusupport/mcupackage.cpp +++ b/src/plugins/mcusupport/mcupackage.cpp @@ -42,12 +42,12 @@ McuPackage::McuPackage(const SettingsHandler::Ptr &settingsHandler, const QString &downloadUrl, const McuPackageVersionDetector *versionDetector, const bool addToSystemPath, - const Utils::PathChooser::Kind &valueType) + const Utils::PathChooser::Kind &valueType, + const bool useNewestVersionKey) : settingsHandler(settingsHandler) , m_label(label) - , m_defaultPath(settingsHandler->getPath(settingsKey, QSettings::SystemScope, defaultPath)) , m_detectionPaths(detectionPaths) - , m_settingsKey(settingsKey) + , m_settingsKey(settingsHandler->getVersionedKey(settingsKey, QSettings::SystemScope, versions, useNewestVersionKey)) , m_versionDetector(versionDetector) , m_versions(versions) , m_cmakeVariableName(cmakeVarName) @@ -56,7 +56,8 @@ McuPackage::McuPackage(const SettingsHandler::Ptr &settingsHandler, , m_addToSystemPath(addToSystemPath) , m_valueType(valueType) { - m_path = this->settingsHandler->getPath(settingsKey, QSettings::UserScope, m_defaultPath); + m_defaultPath = settingsHandler->getPath(m_settingsKey, QSettings::SystemScope, defaultPath); + m_path = settingsHandler->getPath(m_settingsKey, QSettings::UserScope, m_defaultPath); if (m_path.isEmpty()) { m_path = FilePath::fromUserInput(qtcEnvironmentVariable(m_environmentVariableName)); } diff --git a/src/plugins/mcusupport/mcupackage.h b/src/plugins/mcusupport/mcupackage.h index 2efeb5e127f..a74c08bcf2d 100644 --- a/src/plugins/mcusupport/mcupackage.h +++ b/src/plugins/mcusupport/mcupackage.h @@ -40,7 +40,8 @@ public: const McuPackageVersionDetector *versionDetector = nullptr, const bool addToPath = false, const Utils::PathChooser::Kind &valueType - = Utils::PathChooser::Kind::ExistingDirectory); + = Utils::PathChooser::Kind::ExistingDirectory, + const bool useNewestVersionKey = false); ~McuPackage() override = default; diff --git a/src/plugins/mcusupport/mcusupportsdk.cpp b/src/plugins/mcusupport/mcusupportsdk.cpp index 689ab875b42..18d62f41e91 100644 --- a/src/plugins/mcusupport/mcusupportsdk.cpp +++ b/src/plugins/mcusupport/mcusupportsdk.cpp @@ -54,7 +54,13 @@ McuPackagePtr createQtForMCUsPackage(const SettingsHandler::Ptr &settingsHandler .withExecutableSuffix()}, // detectionPaths Constants::SETTINGS_KEY_PACKAGE_QT_FOR_MCUS_SDK, // settingsKey Legacy::Constants::QUL_CMAKE_VAR, - Legacy::Constants::QUL_ENV_VAR)}; + Legacy::Constants::QUL_ENV_VAR, + {}, // versions + {}, // downloadUrl + nullptr, // versionDetector + false, // addToPath + Utils::PathChooser::Kind::ExistingDirectory, // valueType + true)}; // useNewestVersionKey } namespace Legacy { diff --git a/src/plugins/mcusupport/settingshandler.cpp b/src/plugins/mcusupport/settingshandler.cpp index 2bae687cfc8..1ecec030b45 100644 --- a/src/plugins/mcusupport/settingshandler.cpp +++ b/src/plugins/mcusupport/settingshandler.cpp @@ -10,6 +10,9 @@ #include #include +#include +#include + using namespace Utils; namespace McuSupport::Internal { @@ -27,6 +30,68 @@ static FilePath packagePathFromSettings(const Key &settingsKey, return FilePath::fromUserInput(path); } +static Key getKeyForNewestVersion(const Key &plainKey, + QtcSettings &settings) +{ + const Key baseKey = Key(Constants::SETTINGS_KEY_PACKAGE_PREFIX + plainKey); + + // Versioned keys have their version string after the last underscore character + // Only version strings on the format x[.y.z] are considered. + settings.beginGroup(Constants::SETTINGS_GROUP); + const QRegularExpression re(QString("%1_\\d+(\\.\\d+){0,2}$").arg(stringFromKey(baseKey))); + const QStringList matchingKeys = stringsFromKeys(settings.childKeys()).filter(re); + settings.endGroup(); + + if (matchingKeys.isEmpty()) { + return plainKey; + } + QVersionNumber newestVersion; + for (const auto &k: matchingKeys) { + const QString currentVersionStr = k.mid(k.lastIndexOf("_") + 1); + const auto currentVersion = QVersionNumber::fromString(currentVersionStr); + if (newestVersion.isNull() || newestVersion < currentVersion) { + newestVersion = currentVersion; + } + } + const QString newestVersionStr = QString("_%1").arg(newestVersion.toString()); + return Key(plainKey + newestVersionStr.toLocal8Bit()); +} + + +static Key getVersionedKeyFromSettings(const Key &plainKey, + QtcSettings &settings, + const QStringList &versions, + bool allowNewerVersions = false) +{ + const Key keyBase = Key(Constants::SETTINGS_GROUP) + '/' + + Constants::SETTINGS_KEY_PACKAGE_PREFIX; + + // Always prefer one of the versions listed in the kit + for (const auto &versionString: versions) { + const Key versionedKey = plainKey + QString("_%1").arg(versionString).toLocal8Bit(); + + if (settings.contains(keyBase + versionedKey)) { + return versionedKey; + } + } + + // Maybe find the newest version listed in the settings + if (allowNewerVersions) { + return getKeyForNewestVersion(plainKey, settings); + } + + // Fall back to the plain key if no versioned key is found + return plainKey; +} + +Key SettingsHandler::getVersionedKey(const Key &plainKey, + QSettings::Scope scope, + const QStringList &versions, + bool allowNewer) const +{ + return getVersionedKeyFromSettings(plainKey, *Core::ICore::settings(scope), versions, allowNewer); +} + FilePath SettingsHandler::getPath(const Key &settingsKey, QSettings::Scope scope, const FilePath &defaultPath) const diff --git a/src/plugins/mcusupport/settingshandler.h b/src/plugins/mcusupport/settingshandler.h index c50dcde1e3c..c5ed62c6428 100644 --- a/src/plugins/mcusupport/settingshandler.h +++ b/src/plugins/mcusupport/settingshandler.h @@ -18,6 +18,10 @@ public: virtual Utils::FilePath getPath(const Utils::Key &settingsKey, QSettings::Scope scope, const Utils::FilePath &m_defaultPath) const; + Utils::Key getVersionedKey(const Utils::Key &plainKey, + QSettings::Scope scope, + const QStringList &versions, + bool allowNewer) const; virtual bool write(const Utils::Key &settingsKey, const Utils::FilePath &path,