From 68bb06675567a952996af4c3ec9f30d8229fbe60 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 18 May 2020 14:12:50 +0200 Subject: [PATCH] [partition] Consolidate SwapChoice handling - pickOne() may be useful, given a set of swap choices; expose it - move type definitions to PartitionActions, where some of them come from. --- src/modules/partition/core/Config.cpp | 4 ++-- src/modules/partition/core/Config.h | 4 +--- .../partition/core/PartitionActions.cpp | 19 ++++++++++++++++++ src/modules/partition/core/PartitionActions.h | 10 ++++++++++ src/modules/partition/gui/ChoicePage.cpp | 20 +------------------ 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/modules/partition/core/Config.cpp b/src/modules/partition/core/Config.cpp index b8e68a137..d3b5a0310 100644 --- a/src/modules/partition/core/Config.cpp +++ b/src/modules/partition/core/Config.cpp @@ -28,7 +28,7 @@ Config::Config( QObject* parent ) { } -Config::SwapChoices +static PartitionActions::Choices::SwapChoiceSet getSwapChoices( const QVariantMap& configurationMap ) { // SWAP SETTINGS @@ -53,7 +53,7 @@ getSwapChoices( const QVariantMap& configurationMap ) } bool neverCreateSwap = CalamaresUtils::getBool( configurationMap, "neverCreateSwap", false ); - Config::SwapChoices choices; // Available swap choices + PartitionActions::Choices::SwapChoiceSet choices; // Available swap choices if ( configurationMap.contains( "userSwapChoices" ) ) { // We've already warned about overlapping settings with the diff --git a/src/modules/partition/core/Config.h b/src/modules/partition/core/Config.h index dcd8199d7..c18506f7b 100644 --- a/src/modules/partition/core/Config.h +++ b/src/modules/partition/core/Config.h @@ -36,10 +36,8 @@ public: void updateGlobalStorage() const; - using SwapChoices = QSet< PartitionActions::Choices::SwapChoice >; - private: - SwapChoices m_swapChoices; + PartitionActions::Choices::SwapChoiceSet m_swapChoices; qreal m_requiredStorageGiB = 0.0; // May duplicate setting in the welcome module }; diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index d0de2c0d4..3bf5d35d4 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -293,6 +293,25 @@ choiceToName( SwapChoice c ) return nameTable().find( c, ok ); } +SwapChoice +pickOne( const SwapChoiceSet& s ) +{ + if ( s.count() == 0 ) + { + return SwapChoice::NoSwap; + } + if ( s.count() == 1 ) + { + return *( s.begin() ); + } + if ( s.contains( SwapChoice::NoSwap ) ) + { + return SwapChoice::NoSwap; + } + // Here, count > 1 but NoSwap is not a member. + return *( s.begin() ); +} + } // namespace Choices } // namespace PartitionActions diff --git a/src/modules/partition/core/PartitionActions.h b/src/modules/partition/core/PartitionActions.h index ed7045822..558ccb4b9 100644 --- a/src/modules/partition/core/PartitionActions.h +++ b/src/modules/partition/core/PartitionActions.h @@ -19,6 +19,7 @@ #ifndef PARTITIONACTIONS_H #define PARTITIONACTIONS_H +#include #include class PartitionCoreModule; @@ -42,10 +43,19 @@ enum SwapChoice FullSwap, // ensureSuspendToDisk -- at least RAM size SwapFile // use a file (if supported) }; +using SwapChoiceSet = QSet< SwapChoice >; SwapChoice nameToChoice( QString name, bool& ok ); QString choiceToName( SwapChoice ); +/** @brief Given a set of swap choices, return a sensible value from it. + * + * "Sensible" here means: if there is one value, use it; otherwise, use + * NoSwap if there are no choices, or if NoSwap is one of the choices, in the set. + * If that's not possible, any value from the set. + */ +SwapChoice pickOne( const SwapChoiceSet& s ); + struct ReplacePartitionOptions { QString defaultFsType; // e.g. "ext4" or "btrfs" diff --git a/src/modules/partition/gui/ChoicePage.cpp b/src/modules/partition/gui/ChoicePage.cpp index 5c90ea7b0..b7d4c33ad 100644 --- a/src/modules/partition/gui/ChoicePage.cpp +++ b/src/modules/partition/gui/ChoicePage.cpp @@ -70,24 +70,6 @@ using CalamaresUtils::Partition::isPartitionFreeSpace; using CalamaresUtils::Partition::findPartitionByPath; using Calamares::PrettyRadioButton; -/** @brief Given a set of swap choices, return a sensible value from it. - * - * "Sensible" here means: if there is one value, use it; otherwise, use - * NoSwap if there are no choices, or if NoSwap is one of the choices, in the set. - * If that's not possible, any value from the set. - */ -SwapChoice pickOne( const SwapChoiceSet& s ) -{ - if ( s.count() == 0 ) - return SwapChoice::NoSwap; - if ( s.count() == 1 ) - return *( s.begin() ); - if ( s.contains( SwapChoice::NoSwap ) ) - return SwapChoice::NoSwap; - // Here, count > 1 but NoSwap is not a member. - return *( s.begin() ); -} - /** * @brief ChoicePage::ChoicePage is the default constructor. Called on startup as part of * the module loading code path. @@ -112,7 +94,7 @@ ChoicePage::ChoicePage( const SwapChoiceSet& swapChoices, QWidget* parent ) , m_lastSelectedDeviceIndex( -1 ) , m_enableEncryptionWidget( true ) , m_availableSwapChoices( swapChoices ) - , m_eraseSwapChoice( pickOne( swapChoices ) ) + , m_eraseSwapChoice( PartitionActions::Choices::pickOne( swapChoices ) ) , m_allowManualPartitioning( true ) { setupUi( this );