From e6f7af64a746702215144dbdd6541b4bacd60624 Mon Sep 17 00:00:00 2001
From: Vojtech Kral <vojtech@kral.hk>
Date: Thu, 14 Feb 2019 16:51:41 +0100
Subject: [PATCH] PresetUpdater: Fix a race condition

---
 src/slic3r/GUI/GUI_App.cpp         |  4 ++++
 src/slic3r/Utils/PresetUpdater.cpp | 12 +++++-------
 src/slic3r/Utils/PresetUpdater.hpp |  2 ++
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/slic3r/GUI/GUI_App.cpp b/src/slic3r/GUI/GUI_App.cpp
index b5d56bb54..8cd5ecfc4 100644
--- a/src/slic3r/GUI/GUI_App.cpp
+++ b/src/slic3r/GUI/GUI_App.cpp
@@ -127,6 +127,10 @@ bool GUI_App::OnInit()
     app_config->save();
 
     preset_updater = new PresetUpdater();
+    Bind(EVT_SLIC3R_VERSION_ONLINE, [this](const wxCommandEvent &evt) {
+        app_config->set("version_online", into_u8(evt.GetString()));
+        app_config->save();
+    });
 
     load_language();
 
diff --git a/src/slic3r/Utils/PresetUpdater.cpp b/src/slic3r/Utils/PresetUpdater.cpp
index 1fd6d3f4a..d3f54ed15 100644
--- a/src/slic3r/Utils/PresetUpdater.cpp
+++ b/src/slic3r/Utils/PresetUpdater.cpp
@@ -103,6 +103,8 @@ static Semver get_slic3r_version()
 	return *res;
 }
 
+wxDEFINE_EVENT(EVT_SLIC3R_VERSION_ONLINE, wxCommandEvent);
+
 
 struct PresetUpdater::priv
 {
@@ -225,13 +227,9 @@ void PresetUpdater::priv::sync_version() const
 			boost::trim(body);
 			BOOST_LOG_TRIVIAL(info) << boost::format("Got Slic3rPE online version: `%1%`. Sending to GUI thread...") % body;
 
-			// FIXME: race condition
-
-// 			wxCommandEvent* evt = new wxCommandEvent(version_online_event);
-// 			evt->SetString(body);
-// 			GUI::get_app()->QueueEvent(evt);
-	        GUI::wxGetApp().app_config->set("version_online", body);
-	        GUI::wxGetApp().app_config->save();
+			wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_VERSION_ONLINE);
+			evt->SetString(GUI::from_u8(body));
+			GUI::wxGetApp().QueueEvent(evt);
 		})
 		.perform_sync();
 }
diff --git a/src/slic3r/Utils/PresetUpdater.hpp b/src/slic3r/Utils/PresetUpdater.hpp
index 451e8b2cf..29ccaef8f 100644
--- a/src/slic3r/Utils/PresetUpdater.hpp
+++ b/src/slic3r/Utils/PresetUpdater.hpp
@@ -37,6 +37,8 @@ private:
 	std::unique_ptr<priv> p;
 };
 
+wxDECLARE_EVENT(EVT_SLIC3R_VERSION_ONLINE, wxCommandEvent);
+
 
 }
 #endif