From b518ef7dfe069feab18b9d8e9a6ef9b7c057b742 Mon Sep 17 00:00:00 2001
From: Adriaan de Groot <groot@kde.org>
Date: Mon, 28 Sep 2020 15:32:47 +0200
Subject: [PATCH] [partition] Select initial swap choice

---
 src/modules/partition/core/Config.cpp       |  7 +++++++
 src/modules/partition/core/Config.h         | 12 +++++++++---
 src/modules/partition/gui/ChoicePage.cpp    |  2 +-
 src/modules/partition/partition.conf        | 11 +++++++++--
 src/modules/partition/partition.schema.yaml |  1 +
 5 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/modules/partition/core/Config.cpp b/src/modules/partition/core/Config.cpp
index 686d76716..8907a978a 100644
--- a/src/modules/partition/core/Config.cpp
+++ b/src/modules/partition/core/Config.cpp
@@ -116,6 +116,13 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
     bool nameFound = false;  // In the name table (ignored, falls back to first entry in table)
     m_initialInstallChoice = PartitionActions::Choices::installChoiceNames().find(
         CalamaresUtils::getString( configurationMap, "initialPartitioningChoice" ), nameFound );
+    m_initialSwapChoice = PartitionActions::Choices::swapChoiceNames().find(
+        CalamaresUtils::getString( configurationMap, "initialSwapChoice" ), nameFound );
+    if ( !m_swapChoices.contains( m_initialSwapChoice ) )
+    {
+        cWarning() << "Configuration for *initialSwapChoice* is not one of the *userSwapChoices*";
+        m_initialSwapChoice = PartitionActions::Choices::pickOne( m_swapChoices );
+    }
 }
 
 void
diff --git a/src/modules/partition/core/Config.h b/src/modules/partition/core/Config.h
index f2ba9cf58..d338dd57b 100644
--- a/src/modules/partition/core/Config.h
+++ b/src/modules/partition/core/Config.h
@@ -28,14 +28,20 @@ public:
 
     PartitionActions::Choices::SwapChoiceSet swapChoices() const { return m_swapChoices; }
 
-    /**
-     * @brief What kind of installation (partitioning) is requested **initially**?
+    /** @brief What kind of installation (partitioning) is requested **initially**?
      *
-     * @return the partitioning choice (may by @c NoChoice)
+     * @return the partitioning choice (may be @c NoChoice)
      */
     PartitionActions::Choices::InstallChoice initialInstallChoice() const { return m_initialInstallChoice; }
 
+    /** @brief What kind of swap selection is requested **initially**?
+     *
+     * @return The swap choice (may be @c NoSwap )
+     */
+    PartitionActions::Choices::SwapChoice initialSwapChoice() const { return m_initialSwapChoice; }
+
 private:
+    PartitionActions::Choices::SwapChoice m_initialSwapChoice;
     PartitionActions::Choices::SwapChoiceSet m_swapChoices;
     PartitionActions::Choices::InstallChoice m_initialInstallChoice = PartitionActions::Choices::NoChoice;
     qreal m_requiredStorageGiB = 0.0;  // May duplicate setting in the welcome module
diff --git a/src/modules/partition/gui/ChoicePage.cpp b/src/modules/partition/gui/ChoicePage.cpp
index 2e965ad93..cb263f32e 100644
--- a/src/modules/partition/gui/ChoicePage.cpp
+++ b/src/modules/partition/gui/ChoicePage.cpp
@@ -86,7 +86,7 @@ ChoicePage::ChoicePage( Config* config, QWidget* parent )
     , m_lastSelectedDeviceIndex( -1 )
     , m_enableEncryptionWidget( true )
     , m_availableSwapChoices( config->swapChoices() )
-    , m_eraseSwapChoice( PartitionActions::Choices::pickOne( m_availableSwapChoices ) )
+    , m_eraseSwapChoice( config->initialSwapChoice() )
     , m_allowManualPartitioning( true )
 {
     setupUi( this );
diff --git a/src/modules/partition/partition.conf b/src/modules/partition/partition.conf
index 007d7b36e..276ee3458 100644
--- a/src/modules/partition/partition.conf
+++ b/src/modules/partition/partition.conf
@@ -74,8 +74,15 @@ alwaysShowPartitionLabels: true
 #
 # The default is "none"
 #
-# TODO: this isn't implemented
-# initialPartitioningChoice: none
+initialPartitioningChoice: none
+#
+# Similarly, some of the installation choices may offer a choice of swap;
+# the available choices depend on *userSwapChoices*, above, and this
+# setting can be used to pick a specific one.
+#
+# The default is "none" (no swap) if that is one of the enabled options, otherwise
+# one of the items from the options.
+initialSwapChoice: none
 
 # Default filesystem type, used when a "new" partition is made.
 #
diff --git a/src/modules/partition/partition.schema.yaml b/src/modules/partition/partition.schema.yaml
index 770b8a645..16cc08319 100644
--- a/src/modules/partition/partition.schema.yaml
+++ b/src/modules/partition/partition.schema.yaml
@@ -22,6 +22,7 @@ properties:
     allowManualPartitioning: { type: boolean, default: true }
     partitionLayout: { type: array }  # TODO: specify items
     initialPartitioningChoice: { type: string, enum: [ none, erase, replace, alongside, manual ] }
+    initialSwapChoice: { type: string, enum: [ none, small, suspend, reuse, file ] }
 
     requiredStorage: { type: number }
 required: