From 5945e9584dfdadbe9c9a2777a35fee4ea1cc257a Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 4 Dec 2018 11:15:38 +0100 Subject: [PATCH] [partition] Refactor name-to-enum and back for swap choices --- .../partition/core/PartitionActions.cpp | 43 +++++++++++++++++++ src/modules/partition/core/PartitionActions.h | 3 ++ .../partition/gui/PartitionViewStep.cpp | 27 +----------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/modules/partition/core/PartitionActions.cpp b/src/modules/partition/core/PartitionActions.cpp index a4f2baa17..8b0303277 100644 --- a/src/modules/partition/core/PartitionActions.cpp +++ b/src/modules/partition/core/PartitionActions.cpp @@ -303,4 +303,47 @@ doReplacePartition( PartitionCoreModule* core, core->dumpQueue(); } +namespace Choices +{ + +SwapChoice +nameToChoice( QString name, bool& ok ) +{ + ok = false; + name = name.toLower(); + + // Each return here first sets ok to true, returns enum value + if ( name == QStringLiteral( "none" ) ) + return( ok=true, SwapChoice::NoSwap ); + else if ( name == QStringLiteral( "small" ) ) + return( ok=true, SwapChoice::SmallSwap); + else if ( name == QStringLiteral( "suspend" ) ) + return( ok=true, SwapChoice::FullSwap ); + else if ( name == QStringLiteral( "reuse" ) ) + return( ok=true, SwapChoice::ReuseSwap ); + else if ( name == QStringLiteral( "file" ) ) + return( ok=true, SwapChoice::SwapFile ); + + ok = false; + return SwapChoice::NoSwap; } + +QString +choiceToName( SwapChoice c ) +{ + switch ( c ) + { + case SwapChoice::NoSwap: return QStringLiteral( "none" ); + case SwapChoice::SmallSwap: return QStringLiteral( "small" ); + case SwapChoice::FullSwap: return QStringLiteral( "suspend" ); + case SwapChoice::ReuseSwap: return QStringLiteral( "reuse" ); + case SwapChoice::SwapFile: return QStringLiteral( "file" ); + } + + cWarning() << "Unknown SwapChoice" << c << "treated as none"; + return QStringLiteral( "none" ); +} + +} // namespace Choices + +} // namespace PartitionActions diff --git a/src/modules/partition/core/PartitionActions.h b/src/modules/partition/core/PartitionActions.h index 5acf444fa..d17852b63 100644 --- a/src/modules/partition/core/PartitionActions.h +++ b/src/modules/partition/core/PartitionActions.h @@ -43,6 +43,9 @@ namespace Choices SwapFile // use a file (if supported) }; + SwapChoice nameToChoice( QString name, bool& ok ); + QString choiceToName( SwapChoice ); + struct ReplacePartitionOptions { QString defaultFsType; // e.g. "ext4" or "btrfs" diff --git a/src/modules/partition/gui/PartitionViewStep.cpp b/src/modules/partition/gui/PartitionViewStep.cpp index 0152f8bec..3ec5dcddc 100644 --- a/src/modules/partition/gui/PartitionViewStep.cpp +++ b/src/modules/partition/gui/PartitionViewStep.cpp @@ -471,31 +471,6 @@ PartitionViewStep::onLeave() } -static PartitionActions::Choices::SwapChoice -nameToChoice( QString name, bool& ok ) -{ - ok = false; - name = name.toLower(); - - using namespace PartitionActions::Choices; - - // Each return here first sets ok to true, returns enum value - if ( name == QStringLiteral( "none" ) ) - return( ok=true, SwapChoice::NoSwap ); - else if ( name == QStringLiteral( "small" ) ) - return( ok=true, SwapChoice::SmallSwap); - else if ( name == QStringLiteral( "suspend" ) ) - return( ok=true, SwapChoice::FullSwap ); - else if ( name == QStringLiteral( "reuse" ) ) - return( ok=true, SwapChoice::ReuseSwap ); - else if ( name == QStringLiteral( "file" ) ) - return( ok=true, SwapChoice::SwapFile ); - - ok = false; - return SwapChoice::NoSwap; -} - - void PartitionViewStep::setConfigurationMap( const QVariantMap& configurationMap ) { @@ -533,7 +508,7 @@ PartitionViewStep::setConfigurationMap( const QVariantMap& configurationMap ) for ( const auto& item : l ) { bool ok = false; - auto v = nameToChoice( item, ok ); + auto v = PartitionActions::Choices::nameToChoice( item, ok ); if ( ok ) choices.insert( v ); }