From 0af12546ef794dd8fdf67b52e0e45d57f0a5a074 Mon Sep 17 00:00:00 2001
From: Adriaan de Groot <groot@kde.org>
Date: Wed, 11 Nov 2020 14:41:45 +0100
Subject: [PATCH] [plasmalnf] Migrate more settings to Config

---
 src/modules/plasmalnf/Config.cpp            | 42 +++++++++++++++++++
 src/modules/plasmalnf/Config.h              | 24 +++++++++++
 src/modules/plasmalnf/PlasmaLnfViewStep.cpp | 45 ++-------------------
 src/modules/plasmalnf/PlasmaLnfViewStep.h   |  5 ---
 4 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/src/modules/plasmalnf/Config.cpp b/src/modules/plasmalnf/Config.cpp
index cefbd1e64..c813959e3 100644
--- a/src/modules/plasmalnf/Config.cpp
+++ b/src/modules/plasmalnf/Config.cpp
@@ -9,6 +9,7 @@
 
 #include "Config.h"
 
+#include "utils/CalamaresUtilsSystem.h"
 #include "utils/Logger.h"
 #include "utils/Variant.h"
 
@@ -26,4 +27,45 @@ Config::setConfigurationMap( const QVariantMap& configurationMap )
     {
         cWarning() << "no lnftool given for plasmalnf module.";
     }
+
+    m_liveUser = CalamaresUtils::getString( configurationMap, "liveuser" );
+}
+
+void
+Config::setTheme( const QString& id )
+{
+    if ( m_themeId == id )
+    {
+        return;
+    }
+
+    m_themeId = id;
+    if ( lnfToolPath().isEmpty() )
+    {
+        cWarning() << "no lnftool given for plasmalnf module.";
+    }
+    else
+    {
+        QStringList command;
+        if ( !m_liveUser.isEmpty() )
+        {
+            command << "sudo"
+                    << "-E"
+                    << "-H"
+                    << "-u" << m_liveUser;
+        }
+        command << lnfToolPath() << "--resetLayout"
+                << "--apply" << id;
+        auto r = CalamaresUtils::System::instance()->runCommand( command, std::chrono::seconds( 10 ) );
+
+        if ( r.getExitCode() )
+        {
+            cWarning() << r.explainProcess( command, std::chrono::seconds( 10 ) );
+        }
+        else
+        {
+            cDebug() << "Plasma look-and-feel applied" << id;
+        }
+    }
+    emit themeChanged( id );
 }
diff --git a/src/modules/plasmalnf/Config.h b/src/modules/plasmalnf/Config.h
index 759d9677f..16980abc0 100644
--- a/src/modules/plasmalnf/Config.h
+++ b/src/modules/plasmalnf/Config.h
@@ -16,16 +16,40 @@ class Config : public QObject
 {
     Q_OBJECT
 
+    Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
+
 public:
     Config( QObject* parent = nullptr );
     virtual ~Config() override = default;
 
     void setConfigurationMap( const QVariantMap& );
 
+    /** @brief Full path to the lookandfeeltool (if it exists)
+     *
+     * This can be configured, or defaults to `lookandfeeltool` to find
+     * it in $PATH.
+     */
     QString lnfToolPath() const { return m_lnfPath; }
+    /** @brief For OEM mode, the name of the (current) live user
+     *
+     */
+    QString liveUser() const { return m_liveUser; }
+
+    /** @brief The id (in reverse-DNS notation) of the current theme.
+     */
+    QString theme() const { return m_themeId; }
+
+public slots:
+    void setTheme( const QString& id );
+
+signals:
+    void themeChanged( const QString& id );
 
 private:
     QString m_lnfPath;  // Path to the lnf tool
+    QString m_liveUser;  // Name of the live user (for OEM mode)
+
+    QString m_themeId;  // Id of selected theme
 };
 
 #endif
diff --git a/src/modules/plasmalnf/PlasmaLnfViewStep.cpp b/src/modules/plasmalnf/PlasmaLnfViewStep.cpp
index 1e894011b..7f8e19681 100644
--- a/src/modules/plasmalnf/PlasmaLnfViewStep.cpp
+++ b/src/modules/plasmalnf/PlasmaLnfViewStep.cpp
@@ -43,7 +43,7 @@ PlasmaLnfViewStep::PlasmaLnfViewStep( QObject* parent )
     , m_config( new Config( this ) )
     , m_widget( new PlasmaLnfPage )
 {
-    connect( m_widget, &PlasmaLnfPage::plasmaThemeSelected, this, &PlasmaLnfViewStep::themeSelected );
+    connect( m_widget, &PlasmaLnfPage::plasmaThemeSelected, m_config, &Config::setTheme );
     emit nextStatusChanged( false );
 }
 
@@ -111,11 +111,11 @@ PlasmaLnfViewStep::jobs() const
     Calamares::JobList l;
 
     cDebug() << "Creating Plasma LNF jobs ..";
-    if ( !m_themeId.isEmpty() )
+    if ( !m_config->theme().isEmpty() )
     {
         if ( !m_config->lnfToolPath().isEmpty() )
         {
-            l.append( Calamares::job_ptr( new PlasmaLnfJob( m_config->lnfToolPath(), m_themeId ) ) );
+            l.append( Calamares::job_ptr( new PlasmaLnfJob( m_config->lnfToolPath(), m_config->theme() ) ) );
         }
         else
         {
@@ -133,8 +133,6 @@ PlasmaLnfViewStep::setConfigurationMap( const QVariantMap& configurationMap )
 
     m_widget->setLnfPath( m_config->lnfToolPath() );
 
-    m_liveUser = CalamaresUtils::getString( configurationMap, "liveuser" );
-
     QString preselect = CalamaresUtils::getString( configurationMap, "preselect" );
     if ( preselect == QStringLiteral( "*" ) )
     {
@@ -176,40 +174,3 @@ PlasmaLnfViewStep::setConfigurationMap( const QVariantMap& configurationMap )
         m_widget->setEnabledThemesAll();  // All of them
     }
 }
-
-void
-PlasmaLnfViewStep::themeSelected( const QString& id )
-{
-    m_themeId = id;
-    if ( m_config->lnfToolPath().isEmpty() )
-    {
-        cWarning() << "no lnftool given for plasmalnf module.";
-        return;
-    }
-
-    QProcess lnftool;
-    if ( !m_liveUser.isEmpty() )
-        lnftool.start( "sudo", { "-E", "-H", "-u", m_liveUser, m_config->lnfToolPath(), "--resetLayout", "--apply", id } );
-    else
-        lnftool.start( m_config->lnfToolPath(), { "--resetLayout", "--apply", id } );
-
-    if ( !lnftool.waitForStarted( 1000 ) )
-    {
-        cWarning() << "could not start look-and-feel" << m_config->lnfToolPath();
-        return;
-    }
-    if ( !lnftool.waitForFinished() )
-    {
-        cWarning() << m_config->lnfToolPath() << "timed out.";
-        return;
-    }
-
-    if ( ( lnftool.exitCode() == 0 ) && ( lnftool.exitStatus() == QProcess::NormalExit ) )
-    {
-        cDebug() << "Plasma look-and-feel applied" << id;
-    }
-    else
-    {
-        cWarning() << "could not apply look-and-feel" << id;
-    }
-}
diff --git a/src/modules/plasmalnf/PlasmaLnfViewStep.h b/src/modules/plasmalnf/PlasmaLnfViewStep.h
index 1f97f91b2..48f03cdad 100644
--- a/src/modules/plasmalnf/PlasmaLnfViewStep.h
+++ b/src/modules/plasmalnf/PlasmaLnfViewStep.h
@@ -41,14 +41,9 @@ public:
 
     void setConfigurationMap( const QVariantMap& configurationMap ) override;
 
-public slots:
-    void themeSelected( const QString& id );
-
 private:
     Config* m_config;
     PlasmaLnfPage* m_widget;
-    QString m_themeId;  // Id of selected theme
-    QString m_liveUser;  // Name of the live user (for OEM mode)
 };
 
 CALAMARES_PLUGIN_FACTORY_DECLARATION( PlasmaLnfViewStepFactory )