From 1fc05bbf00c8c5574f4ba06b9ec735a9954c9869 Mon Sep 17 00:00:00 2001
From: Vojtech Kral <vojtech@kral.hk>
Date: Sun, 11 Aug 2019 18:57:33 +0200
Subject: [PATCH] ConfigWizard: Fix: Snapshot not being taken on user-requested
 Wizard run

If the user launched Wizard from the menu and checked the reset
checkbox, snapshot was not taken in case no new bundles were
to be installed from resources (ie. most of the time).

Snapshot is now taken as appropriate.
---
 src/slic3r/GUI/ConfigWizard.cpp    | 33 +++++++++++++++++++++++++-----
 src/slic3r/Utils/PresetUpdater.cpp | 10 +++++++++
 2 files changed, 38 insertions(+), 5 deletions(-)

diff --git a/src/slic3r/GUI/ConfigWizard.cpp b/src/slic3r/GUI/ConfigWizard.cpp
index 6a70cb9fd..0115ff5f9 100644
--- a/src/slic3r/GUI/ConfigWizard.cpp
+++ b/src/slic3r/GUI/ConfigWizard.cpp
@@ -25,6 +25,7 @@
 #include "PresetBundle.hpp"
 #include "GUI.hpp"
 #include "GUI_Utils.hpp"
+#include "slic3r/Config/Snapshot.hpp"
 #include "slic3r/Utils/PresetUpdater.hpp"
 
 
@@ -32,6 +33,10 @@ namespace Slic3r {
 namespace GUI {
 
 
+using Config::Snapshot;
+using Config::SnapshotDB;
+
+
 // Printer model picker GUI control
 
 struct PrinterPickerEvent : public wxEvent
@@ -1025,15 +1030,33 @@ void ConfigWizard::priv::apply_config(AppConfig *app_config, PresetBundle *prese
 
     // Decide whether to create snapshot based on run_reason and the reset profile checkbox
     bool snapshot = true;
+    Snapshot::Reason snapshot_reason = Snapshot::SNAPSHOT_UPGRADE;
     switch (run_reason) {
-        case ConfigWizard::RR_DATA_EMPTY:    snapshot = false; break;
-        case ConfigWizard::RR_DATA_LEGACY:   snapshot = true; break;
-        case ConfigWizard::RR_DATA_INCOMPAT: snapshot = false; break;      // In this case snapshot is done by PresetUpdater with the appropriate reason
-        case ConfigWizard::RR_USER:          snapshot = page_welcome->reset_user_profile(); break;
+        case ConfigWizard::RR_DATA_EMPTY:
+            snapshot = false;
+            break;
+        case ConfigWizard::RR_DATA_LEGACY:
+            snapshot = true;
+            break;
+        case ConfigWizard::RR_DATA_INCOMPAT:
+            // In this case snapshot has already been taken by
+            // PresetUpdater with the appropriate reason
+            snapshot = false;
+            break;
+        case ConfigWizard::RR_USER:
+            snapshot = page_welcome->reset_user_profile();
+            snapshot_reason = Snapshot::SNAPSHOT_USER;
+            break;
     }
+
+    if (snapshot) {
+        SnapshotDB::singleton().take_snapshot(*app_config, snapshot_reason);
+    }
+
     if (install_bundles.size() > 0) {
         // Install bundles from resources.
-        updater->install_bundles_rsrc(std::move(install_bundles), snapshot);
+        // Don't create snapshot - we've already done that above if applicable.
+        updater->install_bundles_rsrc(std::move(install_bundles), false);
     } else {
         BOOST_LOG_TRIVIAL(info) << "No bundles need to be installed from resources";
     }
diff --git a/src/slic3r/Utils/PresetUpdater.cpp b/src/slic3r/Utils/PresetUpdater.cpp
index 589db36dc..d55063c7b 100644
--- a/src/slic3r/Utils/PresetUpdater.cpp
+++ b/src/slic3r/Utils/PresetUpdater.cpp
@@ -35,6 +35,10 @@ using Slic3r::GUI::Config::Snapshot;
 using Slic3r::GUI::Config::SnapshotDB;
 
 
+
+// FIXME: Incompat bundle resolution doesn't deal with inherited user presets
+
+
 namespace Slic3r {
 
 
@@ -624,11 +628,17 @@ PresetUpdater::UpdateResult PresetUpdater::config_update() const
 		const auto res = dlg.ShowModal();
 		if (res == wxID_REPLACE) {
 			BOOST_LOG_TRIVIAL(info) << "User wants to re-configure...";
+
+			// This effectively removes the incompatible bundles:
+			// (snapshot is taken beforehand)
 			p->perform_updates(std::move(updates));
+
 			GUI::ConfigWizard wizard(nullptr, GUI::ConfigWizard::RR_DATA_INCOMPAT);
+
 			if (! wizard.run(GUI::wxGetApp().preset_bundle, this)) {
 				return R_INCOMPAT_EXIT;
 			}
+
 			GUI::wxGetApp().load_current_presets();
 			return R_INCOMPAT_CONFIGURED;
 		} else {