1 #include "PresetUpdater.hpp"
2 
3 #include <algorithm>
4 #include <thread>
5 #include <unordered_map>
6 #include <ostream>
7 #include <utility>
8 #include <stdexcept>
9 #include <boost/format.hpp>
10 #include <boost/algorithm/string.hpp>
11 #include <boost/filesystem.hpp>
12 #include <boost/filesystem/fstream.hpp>
13 #include <boost/lexical_cast.hpp>
14 #include <boost/log/trivial.hpp>
15 
16 #include <wx/app.h>
17 #include <wx/msgdlg.h>
18 
19 #include "libslic3r/libslic3r.h"
20 #include "libslic3r/format.hpp"
21 #include "libslic3r/Utils.hpp"
22 #include "libslic3r/PresetBundle.hpp"
23 #include "slic3r/GUI/GUI.hpp"
24 #include "slic3r/GUI/I18N.hpp"
25 #include "slic3r/GUI/UpdateDialogs.hpp"
26 #include "slic3r/GUI/ConfigWizard.hpp"
27 #include "slic3r/GUI/GUI_App.hpp"
28 #include "slic3r/GUI/Plater.hpp"
29 #include "slic3r/GUI/format.hpp"
30 #include "slic3r/GUI/NotificationManager.hpp"
31 #include "slic3r/Utils/Http.hpp"
32 #include "slic3r/Config/Version.hpp"
33 #include "slic3r/Config/Snapshot.hpp"
34 
35 namespace fs = boost::filesystem;
36 using Slic3r::GUI::Config::Index;
37 using Slic3r::GUI::Config::Version;
38 using Slic3r::GUI::Config::Snapshot;
39 using Slic3r::GUI::Config::SnapshotDB;
40 
41 
42 
43 // FIXME: Incompat bundle resolution doesn't deal with inherited user presets
44 
45 
46 namespace Slic3r {
47 
48 
49 enum {
50 	SLIC3R_VERSION_BODY_MAX = 256,
51 };
52 
53 static const char *INDEX_FILENAME = "index.idx";
54 static const char *TMP_EXTENSION = ".download";
55 
56 
copy_file_fix(const fs::path & source,const fs::path & target)57 void copy_file_fix(const fs::path &source, const fs::path &target)
58 {
59 	BOOST_LOG_TRIVIAL(debug) << format("PresetUpdater: Copying %1% -> %2%", source, target);
60 	std::string error_message;
61 	CopyFileResult cfr = copy_file(source.string(), target.string(), error_message, false);
62 	if (cfr != CopyFileResult::SUCCESS) {
63 		BOOST_LOG_TRIVIAL(error) << "Copying failed(" << cfr << "): " << error_message;
64 		throw Slic3r::CriticalException(GUI::format(
65 				_L("Copying of file %1% to %2% failed: %3%"),
66 				source, target, error_message));
67 	}
68 	// Permissions should be copied from the source file by copy_file(). We are not sure about the source
69 	// permissions, let's rewrite them with 644.
70 	static constexpr const auto perms = fs::owner_read | fs::owner_write | fs::group_read | fs::others_read;
71 	fs::permissions(target, perms);
72 }
73 
74 struct Update
75 {
76 	fs::path source;
77 	fs::path target;
78 
79 	Version version;
80 	std::string vendor;
81 	std::string changelog_url;
82 
83 	bool forced_update;
84 
UpdateSlic3r::Update85 	Update() {}
UpdateSlic3r::Update86 	Update(fs::path &&source, fs::path &&target, const Version &version, std::string vendor, std::string changelog_url, bool forced = false)
87 		: source(std::move(source))
88 		, target(std::move(target))
89 		, version(version)
90 		, vendor(std::move(vendor))
91 		, changelog_url(std::move(changelog_url))
92 		, forced_update(forced)
93 	{}
94 
installSlic3r::Update95 	void install() const
96 	{
97 		copy_file_fix(source, target);
98 	}
99 
operator <<(std::ostream & os,const Update & self)100 	friend std::ostream& operator<<(std::ostream& os, const Update &self)
101 	{
102 		os << "Update(" << self.source.string() << " -> " << self.target.string() << ')';
103 		return os;
104 	}
105 };
106 
107 struct Incompat
108 {
109 	fs::path bundle;
110 	Version version;
111 	std::string vendor;
112 
IncompatSlic3r::Incompat113 	Incompat(fs::path &&bundle, const Version &version, std::string vendor)
114 		: bundle(std::move(bundle))
115 		, version(version)
116 		, vendor(std::move(vendor))
117 	{}
118 
removeSlic3r::Incompat119 	void remove() {
120 		// Remove the bundle file
121 		fs::remove(bundle);
122 
123 		// Look for an installed index and remove it too if any
124 		const fs::path installed_idx = bundle.replace_extension("idx");
125 		if (fs::exists(installed_idx)) {
126 			fs::remove(installed_idx);
127 		}
128 	}
129 
operator <<(std::ostream & os,const Incompat & self)130 	friend std::ostream& operator<<(std::ostream& os , const Incompat &self) {
131 		os << "Incompat(" << self.bundle.string() << ')';
132 		return os;
133 	}
134 };
135 
136 struct Updates
137 {
138 	std::vector<Incompat> incompats;
139 	std::vector<Update> updates;
140 };
141 
142 
143 wxDEFINE_EVENT(EVT_SLIC3R_VERSION_ONLINE, wxCommandEvent);
144 
145 
146 struct PresetUpdater::priv
147 {
148 	std::vector<Index> index_db;
149 
150 	bool enabled_version_check;
151 	bool enabled_config_update;
152 	std::string version_check_url;
153 
154 	fs::path cache_path;
155 	fs::path rsrc_path;
156 	fs::path vendor_path;
157 
158 	bool cancel;
159 	std::thread thread;
160 
161 	bool has_waiting_updates { false };
162 	Updates waiting_updates;
163 
164 	priv();
165 
166 	void set_download_prefs(AppConfig *app_config);
167 	bool get_file(const std::string &url, const fs::path &target_path) const;
168 	void prune_tmps() const;
169 	void sync_version() const;
170 	void sync_config(const VendorMap vendors);
171 
172 	void check_install_indices() const;
173 	Updates get_config_updates(const Semver& old_slic3r_version) const;
174 	bool perform_updates(Updates &&updates, bool snapshot = true) const;
175 	void set_waiting_updates(Updates u);
176 };
177 
priv()178 PresetUpdater::priv::priv()
179 	: cache_path(fs::path(Slic3r::data_dir()) / "cache")
180 	, rsrc_path(fs::path(resources_dir()) / "profiles")
181 	, vendor_path(fs::path(Slic3r::data_dir()) / "vendor")
182 	, cancel(false)
183 {
184 	set_download_prefs(GUI::wxGetApp().app_config);
185 	// Install indicies from resources. Only installs those that are either missing or older than in resources.
186 	check_install_indices();
187 	// Load indices from the cache directory.
188 	index_db = Index::load_db();
189 }
190 
191 // Pull relevant preferences from AppConfig
set_download_prefs(AppConfig * app_config)192 void PresetUpdater::priv::set_download_prefs(AppConfig *app_config)
193 {
194 	enabled_version_check = app_config->get("version_check") == "1";
195 	version_check_url = app_config->version_check_url();
196 	enabled_config_update = app_config->get("preset_update") == "1" && !app_config->legacy_datadir();
197 }
198 
199 // Downloads a file (http get operation). Cancels if the Updater is being destroyed.
get_file(const std::string & url,const fs::path & target_path) const200 bool PresetUpdater::priv::get_file(const std::string &url, const fs::path &target_path) const
201 {
202 	bool res = false;
203 	fs::path tmp_path = target_path;
204 	tmp_path += format(".%1%%2%", get_current_pid(), TMP_EXTENSION);
205 
206 	BOOST_LOG_TRIVIAL(info) << format("Get: `%1%`\n\t-> `%2%`\n\tvia tmp path `%3%`",
207 		url,
208 		target_path.string(),
209 		tmp_path.string());
210 
211 	Http::get(url)
212 		.on_progress([this](Http::Progress, bool &cancel) {
213 			if (cancel) { cancel = true; }
214 		})
215 		.on_error([&](std::string body, std::string error, unsigned http_status) {
216 			(void)body;
217 			BOOST_LOG_TRIVIAL(error) << format("Error getting: `%1%`: HTTP %2%, %3%",
218 				url,
219 				http_status,
220 				error);
221 		})
222 		.on_complete([&](std::string body, unsigned /* http_status */) {
223 			fs::fstream file(tmp_path, std::ios::out | std::ios::binary | std::ios::trunc);
224 			file.write(body.c_str(), body.size());
225 			file.close();
226 			fs::rename(tmp_path, target_path);
227 			res = true;
228 		})
229 		.perform_sync();
230 
231 	return res;
232 }
233 
234 // Remove leftover paritally downloaded files, if any.
prune_tmps() const235 void PresetUpdater::priv::prune_tmps() const
236 {
237     for (auto &dir_entry : boost::filesystem::directory_iterator(cache_path))
238 		if (is_plain_file(dir_entry) && dir_entry.path().extension() == TMP_EXTENSION) {
239 			BOOST_LOG_TRIVIAL(debug) << "Cache prune: " << dir_entry.path().string();
240 			fs::remove(dir_entry.path());
241 		}
242 }
243 
244 // Get Slic3rPE version available online, save in AppConfig.
sync_version() const245 void PresetUpdater::priv::sync_version() const
246 {
247 	if (! enabled_version_check) { return; }
248 
249 	BOOST_LOG_TRIVIAL(info) << format("Downloading %1% online version from: `%2%`", SLIC3R_APP_NAME, version_check_url);
250 
251 	Http::get(version_check_url)
252 		.size_limit(SLIC3R_VERSION_BODY_MAX)
253 		.on_progress([this](Http::Progress, bool &cancel) {
254 			cancel = this->cancel;
255 		})
256 		.on_error([&](std::string body, std::string error, unsigned http_status) {
257 			(void)body;
258 			BOOST_LOG_TRIVIAL(error) << format("Error getting: `%1%`: HTTP %2%, %3%",
259 				version_check_url,
260 				http_status,
261 				error);
262 		})
263 		.on_complete([&](std::string body, unsigned /* http_status */) {
264 			boost::trim(body);
265 			const auto nl_pos = body.find_first_of("\n\r");
266 			if (nl_pos != std::string::npos) {
267 				body.resize(nl_pos);
268 			}
269 
270 			if (! Semver::parse(body)) {
271 				BOOST_LOG_TRIVIAL(warning) << format("Received invalid contents from `%1%`: Not a correct semver: `%2%`", SLIC3R_APP_NAME, body);
272 				return;
273 			}
274 
275 			BOOST_LOG_TRIVIAL(info) << format("Got %1% online version: `%2%`. Sending to GUI thread...", SLIC3R_APP_NAME, body);
276 
277 			wxCommandEvent* evt = new wxCommandEvent(EVT_SLIC3R_VERSION_ONLINE);
278 			evt->SetString(GUI::from_u8(body));
279 			GUI::wxGetApp().QueueEvent(evt);
280 		})
281 		.perform_sync();
282 }
283 
284 // Download vendor indices. Also download new bundles if an index indicates there's a new one available.
285 // Both are saved in cache.
sync_config(const VendorMap vendors)286 void PresetUpdater::priv::sync_config(const VendorMap vendors)
287 {
288 	BOOST_LOG_TRIVIAL(info) << "Syncing configuration cache";
289 
290 	if (!enabled_config_update) { return; }
291 
292 	// Donwload vendor preset bundles
293 	// Over all indices from the cache directory:
294 	for (auto &index : index_db) {
295 		if (cancel) { return; }
296 
297 		const auto vendor_it = vendors.find(index.vendor());
298 		if (vendor_it == vendors.end()) {
299 			BOOST_LOG_TRIVIAL(warning) << "No such vendor: " << index.vendor();
300 			continue;
301 		}
302 
303 		const VendorProfile &vendor = vendor_it->second;
304 		if (vendor.config_update_url.empty()) {
305 			BOOST_LOG_TRIVIAL(info) << "Vendor has no config_update_url: " << vendor.name;
306 			continue;
307 		}
308 
309 		// Download a fresh index
310 		BOOST_LOG_TRIVIAL(info) << "Downloading index for vendor: " << vendor.name;
311 		const auto idx_url = vendor.config_update_url + "/" + INDEX_FILENAME;
312 		const std::string idx_path = (cache_path / (vendor.id + ".idx")).string();
313 		const std::string idx_path_temp = idx_path + "-update";
314 		//check if idx_url is leading to our site
315 		if (! boost::starts_with(idx_url, "http://files.prusa3d.com/wp-content/uploads/repository/") &&
316 		    ! boost::starts_with(idx_url, "https://files.prusa3d.com/wp-content/uploads/repository/"))
317 		{
318 			BOOST_LOG_TRIVIAL(warning) << "unsafe url path for vendor \"" << vendor.name << "\" rejected: " << idx_url;
319 			continue;
320 		}
321 		if (!get_file(idx_url, idx_path_temp)) { continue; }
322 		if (cancel) { return; }
323 
324 		// Load the fresh index up
325 		{
326 			Index new_index;
327 			try {
328 				new_index.load(idx_path_temp);
329 			} catch (const std::exception & /* err */) {
330 				BOOST_LOG_TRIVIAL(error) << format("Could not load downloaded index %1% for vendor %2%: invalid index?", idx_path_temp, vendor.name);
331 				continue;
332 			}
333 			if (new_index.version() < index.version()) {
334 				BOOST_LOG_TRIVIAL(warning) << format("The downloaded index %1% for vendor %2% is older than the active one. Ignoring the downloaded index.", idx_path_temp, vendor.name);
335 				continue;
336 			}
337 			Slic3r::rename_file(idx_path_temp, idx_path);
338 			//if we rename path we need to change it in Index object too or create the object again
339 			//index = std::move(new_index);
340 			try {
341 				index.load(idx_path);
342 			}
343 			catch (const std::exception& /* err */) {
344 				BOOST_LOG_TRIVIAL(error) << format("Could not load downloaded index %1% for vendor %2%: invalid index?", idx_path, vendor.name);
345 				continue;
346 			}
347 			if (cancel)
348 				return;
349 		}
350 
351 		// See if a there's a new version to download
352 		const auto recommended_it = index.recommended();
353 		if (recommended_it == index.end()) {
354 			BOOST_LOG_TRIVIAL(error) << format("No recommended version for vendor: %1%, invalid index?", vendor.name);
355 			continue;
356 		}
357 
358 		const auto recommended = recommended_it->config_version;
359 
360 		BOOST_LOG_TRIVIAL(debug) << format("Got index for vendor: %1%: current version: %2%, recommended version: %3%",
361 			vendor.name,
362 			vendor.config_version.to_string(),
363 			recommended.to_string());
364 
365 		if (vendor.config_version >= recommended) { continue; }
366 
367 		// Download a fresh bundle
368 		BOOST_LOG_TRIVIAL(info) << "Downloading new bundle for vendor: " << vendor.name;
369 		const auto bundle_url = format("%1%/%2%.ini", vendor.config_update_url, recommended.to_string());
370 		const auto bundle_path = cache_path / (vendor.id + ".ini");
371 		if (! get_file(bundle_url, bundle_path)) { continue; }
372 		if (cancel) { return; }
373 	}
374 }
375 
376 // Install indicies from resources. Only installs those that are either missing or older than in resources.
check_install_indices() const377 void PresetUpdater::priv::check_install_indices() const
378 {
379 	BOOST_LOG_TRIVIAL(info) << "Checking if indices need to be installed from resources...";
380 
381     for (auto &dir_entry : boost::filesystem::directory_iterator(rsrc_path))
382 		if (is_idx_file(dir_entry)) {
383 			const auto &path = dir_entry.path();
384 			const auto path_in_cache = cache_path / path.filename();
385 
386 			if (! fs::exists(path_in_cache)) {
387 				BOOST_LOG_TRIVIAL(info) << "Install index from resources: " << path.filename();
388 				copy_file_fix(path, path_in_cache);
389 			} else {
390 				Index idx_rsrc, idx_cache;
391 				idx_rsrc.load(path);
392 				idx_cache.load(path_in_cache);
393 
394 				if (idx_cache.version() < idx_rsrc.version()) {
395 					BOOST_LOG_TRIVIAL(info) << "Update index from resources: " << path.filename();
396 					copy_file_fix(path, path_in_cache);
397 				}
398 			}
399 		}
400 }
401 
402 // Generates a list of bundle updates that are to be performed.
403 // Version of slic3r that was running the last time and which was read out from PrusaSlicer.ini is provided
404 // as a parameter.
get_config_updates(const Semver & old_slic3r_version) const405 Updates PresetUpdater::priv::get_config_updates(const Semver &old_slic3r_version) const
406 {
407 	Updates updates;
408 
409 	BOOST_LOG_TRIVIAL(info) << "Checking for cached configuration updates...";
410 
411 	// Over all indices from the cache directory:
412 	for (const auto idx : index_db) {
413 		auto bundle_path = vendor_path / (idx.vendor() + ".ini");
414 		auto bundle_path_idx = vendor_path / idx.path().filename();
415 
416 		if (! fs::exists(bundle_path)) {
417 			BOOST_LOG_TRIVIAL(info) << format("Confing bundle not installed for vendor %1%, skipping: ", idx.vendor());
418 			continue;
419 		}
420 
421 		// Perform a basic load and check the version of the installed preset bundle.
422 		auto vp = VendorProfile::from_ini(bundle_path, false);
423 
424 		// Getting a recommended version from the latest index, wich may have been downloaded
425 		// from the internet, or installed / updated from the installation resources.
426 		auto recommended = idx.recommended();
427 		if (recommended == idx.end()) {
428 			BOOST_LOG_TRIVIAL(error) << format("No recommended version for vendor: %1%, invalid index? Giving up.", idx.vendor());
429 			// XXX: what should be done here?
430 			continue;
431 		}
432 
433 		const auto ver_current = idx.find(vp.config_version);
434 		const bool ver_current_found = ver_current != idx.end();
435 
436 		BOOST_LOG_TRIVIAL(debug) << format("Vendor: %1%, version installed: %2%%3%, version cached: %4%",
437 			vp.name,
438 			vp.config_version.to_string(),
439 			(ver_current_found ? "" : " (not found in index!)"),
440 			recommended->config_version.to_string());
441 
442 		if (! ver_current_found) {
443 			// Any published config shall be always found in the latest config index.
444 			auto message = format("Preset bundle `%1%` version not found in index: %2%", idx.vendor(), vp.config_version.to_string());
445 			BOOST_LOG_TRIVIAL(error) << message;
446 			GUI::show_error(nullptr, message);
447 			continue;
448 		}
449 
450 		bool current_not_supported = false; //if slcr is incompatible but situation is not downgrade, we do forced updated and this bool is information to do it
451 
452 		if (ver_current_found && !ver_current->is_current_slic3r_supported()){
453 			if(ver_current->is_current_slic3r_downgrade()) {
454 				// "Reconfigure" situation.
455 				BOOST_LOG_TRIVIAL(warning) << "Current Slic3r incompatible with installed bundle: " << bundle_path.string();
456 				updates.incompats.emplace_back(std::move(bundle_path), *ver_current, vp.name);
457 				continue;
458 			}
459 		current_not_supported = true;
460 		}
461 
462 		if (recommended->config_version < vp.config_version) {
463 			BOOST_LOG_TRIVIAL(warning) << format("Recommended config version for the currently running PrusaSlicer is older than the currently installed config for vendor %1%. This should not happen.", idx.vendor());
464 			continue;
465 		}
466 
467 		if (recommended->config_version == vp.config_version) {
468 			// The recommended config bundle is already installed.
469 			continue;
470 		}
471 
472 		// Config bundle update situation. The recommended config bundle version for this PrusaSlicer version from the index from the cache is newer
473 		// than the version of the currently installed config bundle.
474 
475 		// The config index inside the cache directory (given by idx.path()) is one of the following:
476 		// 1) The last config index downloaded by any previously running PrusaSlicer instance
477 		// 2) The last config index installed by any previously running PrusaSlicer instance (older or newer) from its resources.
478 		// 3) The last config index installed by the currently running PrusaSlicer instance from its resources.
479 		// The config index is always the newest one (given by its newest config bundle referenced), and older config indices shall fully contain
480 		// the content of the older config indices.
481 
482 		// Config bundle inside the cache directory.
483 		fs::path path_in_cache 		= cache_path / (idx.vendor() + ".ini");
484 		// Config bundle inside the resources directory.
485 		fs::path path_in_rsrc 		= rsrc_path  / (idx.vendor() + ".ini");
486 		// Config index inside the resources directory.
487 		fs::path path_idx_in_rsrc 	= rsrc_path  / (idx.vendor() + ".idx");
488 
489 		// Search for a valid config bundle in the cache directory.
490 		bool 		found = false;
491 		Update    	new_update;
492 		fs::path 	bundle_path_idx_to_install;
493 		if (fs::exists(path_in_cache)) {
494 			try {
495 				VendorProfile new_vp = VendorProfile::from_ini(path_in_cache, false);
496 				if (new_vp.config_version == recommended->config_version) {
497 					// The config bundle from the cache directory matches the recommended version of the index from the cache directory.
498 					// This is the newest known recommended config. Use it.
499 					new_update = Update(std::move(path_in_cache), std::move(bundle_path), *recommended, vp.name, vp.changelog_url, current_not_supported);
500 					// and install the config index from the cache into vendor's directory.
501 					bundle_path_idx_to_install = idx.path();
502 					found = true;
503 				}
504 			} catch (const std::exception &ex) {
505 				BOOST_LOG_TRIVIAL(info) << format("Failed to load the config bundle `%1%`: %2%", path_in_cache.string(), ex.what());
506 			}
507 		}
508 
509 		// Keep the rsrc_idx outside of the next block, as we will reference the "recommended" version by an iterator.
510 		Index rsrc_idx;
511 		if (! found && fs::exists(path_in_rsrc) && fs::exists(path_idx_in_rsrc)) {
512 			// Trying the config bundle from resources (from the installation).
513 			// In that case, the recommended version number has to be compared against the recommended version reported by the config index from resources as well,
514 			// as the config index in the cache directory may already be newer, recommending a newer config bundle than available in cache or resources.
515 			VendorProfile rsrc_vp;
516 			try {
517 				rsrc_vp = VendorProfile::from_ini(path_in_rsrc, false);
518 			} catch (const std::exception &ex) {
519 				BOOST_LOG_TRIVIAL(info) << format("Cannot load the config bundle at `%1%`: %2%", path_in_rsrc.string(), ex.what());
520 			}
521 			if (rsrc_vp.valid()) {
522 				try {
523 					rsrc_idx.load(path_idx_in_rsrc);
524 				} catch (const std::exception &ex) {
525 					BOOST_LOG_TRIVIAL(info) << format("Cannot load the config index at `%1%`: %2%", path_idx_in_rsrc.string(), ex.what());
526 				}
527 				recommended = rsrc_idx.recommended();
528 				if (recommended != rsrc_idx.end() && recommended->config_version == rsrc_vp.config_version && recommended->config_version > vp.config_version) {
529 					new_update = Update(std::move(path_in_rsrc), std::move(bundle_path), *recommended, vp.name, vp.changelog_url, current_not_supported);
530 					bundle_path_idx_to_install = path_idx_in_rsrc;
531 					found = true;
532 				} else {
533 					BOOST_LOG_TRIVIAL(warning) << format("The recommended config version for vendor `%1%` in resources does not match the recommended\n"
534 			                                             " config version for this version of PrusaSlicer. Corrupted installation?", idx.vendor());
535 				}
536 			}
537 		}
538 
539 		if (found) {
540 			// Load 'installed' idx, if any.
541 			// 'Installed' indices are kept alongside the bundle in the `vendor` subdir
542 			// for bookkeeping to remember a cancelled update and not offer it again.
543 			if (fs::exists(bundle_path_idx)) {
544 				Index existing_idx;
545 				try {
546 					existing_idx.load(bundle_path_idx);
547 					// Find a recommended config bundle version for the slic3r version last executed. This makes sure that a config bundle update will not be missed
548 					// when upgrading an application. On the other side, the user will be bugged every time he will switch between slic3r versions.
549                     /*const auto existing_recommended = existing_idx.recommended(old_slic3r_version);
550                     if (existing_recommended != existing_idx.end() && recommended->config_version == existing_recommended->config_version) {
551 						// The user has already seen (and presumably rejected) this update
552 						BOOST_LOG_TRIVIAL(info) << format("Downloaded index for `%1%` is the same as installed one, not offering an update.",idx.vendor());
553 						continue;
554 					}*/
555 				} catch (const std::exception &err) {
556 					BOOST_LOG_TRIVIAL(error) << format("Cannot load the installed index at `%1%`: %2%", bundle_path_idx, err.what());
557 				}
558 			}
559 
560 			// Check if the update is already present in a snapshot
561 			if(!current_not_supported)
562 			{
563 				const auto recommended_snap = SnapshotDB::singleton().snapshot_with_vendor_preset(vp.name, recommended->config_version);
564 				if (recommended_snap != SnapshotDB::singleton().end()) {
565 					BOOST_LOG_TRIVIAL(info) << format("Bundle update %1% %2% already found in snapshot %3%, skipping...",
566 						vp.name,
567 						recommended->config_version.to_string(),
568 						recommended_snap->id);
569 					continue;
570 				}
571 			}
572 
573 			updates.updates.emplace_back(std::move(new_update));
574 			// 'Install' the index in the vendor directory. This is used to memoize
575 			// offered updates and to not offer the same update again if it was cancelled by the user.
576 			copy_file_fix(bundle_path_idx_to_install, bundle_path_idx);
577 		} else {
578 			BOOST_LOG_TRIVIAL(warning) << format("Index for vendor %1% indicates update (%2%) but the new bundle was found neither in cache nor resources",
579 				idx.vendor(),
580 				recommended->config_version.to_string());
581 		}
582 	}
583 
584 	return updates;
585 }
586 
perform_updates(Updates && updates,bool snapshot) const587 bool PresetUpdater::priv::perform_updates(Updates &&updates, bool snapshot) const
588 {
589 	if (updates.incompats.size() > 0) {
590 		if (snapshot) {
591 			BOOST_LOG_TRIVIAL(info) << "Taking a snapshot...";
592 			if (! GUI::Config::take_config_snapshot_cancel_on_error(*GUI::wxGetApp().app_config, Snapshot::SNAPSHOT_DOWNGRADE, "",
593 				_u8L("Continue and install configuration updates?")))
594 				return false;
595 		}
596 
597 		BOOST_LOG_TRIVIAL(info) << format("Deleting %1% incompatible bundles", updates.incompats.size());
598 
599 		for (auto &incompat : updates.incompats) {
600 			BOOST_LOG_TRIVIAL(info) << '\t' << incompat;
601 			incompat.remove();
602 		}
603 
604 
605 	} else if (updates.updates.size() > 0) {
606 
607 		if (snapshot) {
608 			BOOST_LOG_TRIVIAL(info) << "Taking a snapshot...";
609 			if (! GUI::Config::take_config_snapshot_cancel_on_error(*GUI::wxGetApp().app_config, Snapshot::SNAPSHOT_UPGRADE, "",
610 				_u8L("Continue and install configuration updates?")))
611 				return false;
612 		}
613 
614 		BOOST_LOG_TRIVIAL(info) << format("Performing %1% updates", updates.updates.size());
615 
616 		for (const auto &update : updates.updates) {
617 			BOOST_LOG_TRIVIAL(info) << '\t' << update;
618 
619 			update.install();
620 
621 			PresetBundle bundle;
622 			// Throw when parsing invalid configuration. Only valid configuration is supposed to be provided over the air.
623 			bundle.load_configbundle(update.source.string(), PresetBundle::LoadConfigBundleAttribute::LoadSystem, ForwardCompatibilitySubstitutionRule::Disable);
624 
625 			BOOST_LOG_TRIVIAL(info) << format("Deleting %1% conflicting presets", bundle.prints.size() + bundle.filaments.size() + bundle.printers.size());
626 
627 			auto preset_remover = [](const Preset &preset) {
628 				BOOST_LOG_TRIVIAL(info) << '\t' << preset.file;
629 				fs::remove(preset.file);
630 			};
631 
632 			for (const auto &preset : bundle.prints)    { preset_remover(preset); }
633 			for (const auto &preset : bundle.filaments) { preset_remover(preset); }
634 			for (const auto &preset : bundle.printers)  { preset_remover(preset); }
635 
636 			// Also apply the `obsolete_presets` property, removing obsolete ini files
637 
638 			BOOST_LOG_TRIVIAL(info) << format("Deleting %1% obsolete presets",
639 				bundle.obsolete_presets.prints.size() + bundle.obsolete_presets.filaments.size() + bundle.obsolete_presets.printers.size());
640 
641 			auto obsolete_remover = [](const char *subdir, const std::string &preset) {
642 				auto path = fs::path(Slic3r::data_dir()) / subdir / preset;
643 				path += ".ini";
644 				BOOST_LOG_TRIVIAL(info) << '\t' << path.string();
645 				fs::remove(path);
646 			};
647 
648 			for (const auto &name : bundle.obsolete_presets.prints)    { obsolete_remover("print", name); }
649 			for (const auto &name : bundle.obsolete_presets.filaments) { obsolete_remover("filament", name); }
650 			for (const auto &name : bundle.obsolete_presets.sla_prints) { obsolete_remover("sla_print", name); }
651 			for (const auto &name : bundle.obsolete_presets.sla_materials/*filaments*/) { obsolete_remover("sla_material", name); }
652 			for (const auto &name : bundle.obsolete_presets.printers)  { obsolete_remover("printer", name); }
653 		}
654 	}
655 
656 	return true;
657 }
658 
set_waiting_updates(Updates u)659 void PresetUpdater::priv::set_waiting_updates(Updates u)
660 {
661 	waiting_updates = u;
662 	has_waiting_updates = true;
663 }
664 
PresetUpdater()665 PresetUpdater::PresetUpdater() :
666 	p(new priv())
667 {}
668 
669 
670 // Public
671 
~PresetUpdater()672 PresetUpdater::~PresetUpdater()
673 {
674 	if (p && p->thread.joinable()) {
675 		// This will stop transfers being done by the thread, if any.
676 		// Cancelling takes some time, but should complete soon enough.
677 		p->cancel = true;
678 		p->thread.join();
679 	}
680 }
681 
sync(PresetBundle * preset_bundle)682 void PresetUpdater::sync(PresetBundle *preset_bundle)
683 {
684 	p->set_download_prefs(GUI::wxGetApp().app_config);
685 	if (!p->enabled_version_check && !p->enabled_config_update) { return; }
686 
687 	// Copy the whole vendors data for use in the background thread
688 	// Unfortunatelly as of C++11, it needs to be copied again
689 	// into the closure (but perhaps the compiler can elide this).
690 	VendorMap vendors = preset_bundle->vendors;
691 
692 	p->thread = std::move(std::thread([this, vendors]() {
693 		this->p->prune_tmps();
694 		this->p->sync_version();
695 		this->p->sync_config(std::move(vendors));
696 	}));
697 }
698 
slic3r_update_notify()699 void PresetUpdater::slic3r_update_notify()
700 {
701 	if (! p->enabled_version_check) { return; }
702 
703 	auto* app_config = GUI::wxGetApp().app_config;
704 	const auto ver_online_str = app_config->get("version_online");
705 	const auto ver_online = Semver::parse(ver_online_str);
706 	const auto ver_online_seen = Semver::parse(app_config->get("version_online_seen"));
707 
708 	if (ver_online) {
709 		// Only display the notification if the version available online is newer AND if we haven't seen it before
710 		if (*ver_online > Slic3r::SEMVER && (! ver_online_seen || *ver_online_seen < *ver_online)) {
711 			GUI::MsgUpdateSlic3r notification(Slic3r::SEMVER, *ver_online);
712 			notification.ShowModal();
713 			if (notification.disable_version_check()) {
714 				app_config->set("version_check", "0");
715 				p->enabled_version_check = false;
716 			}
717 		}
718 
719 		app_config->set("version_online_seen", ver_online_str);
720 	}
721 }
722 
reload_configs_update_gui()723 static void reload_configs_update_gui()
724 {
725 	// Reload global configuration
726 	auto* app_config = GUI::wxGetApp().app_config;
727 	// System profiles should not trigger any substitutions, user profiles may trigger substitutions, but these substitutions
728 	// were already presented to the user on application start up. Just do substitutions now and keep quiet about it.
729 	// However throw on substitutions in system profiles, those shall never happen with system profiles installed over the air.
730 	GUI::wxGetApp().preset_bundle->load_presets(*app_config, ForwardCompatibilitySubstitutionRule::EnableSilentDisableSystem);
731 	GUI::wxGetApp().load_current_presets();
732 	GUI::wxGetApp().plater()->set_bed_shape();
733 	GUI::wxGetApp().update_wizard_from_config();
734 }
735 
config_update(const Semver & old_slic3r_version,UpdateParams params) const736 PresetUpdater::UpdateResult PresetUpdater::config_update(const Semver& old_slic3r_version, UpdateParams params) const
737 {
738  	if (! p->enabled_config_update) { return R_NOOP; }
739 
740 	auto updates = p->get_config_updates(old_slic3r_version);
741 	if (updates.incompats.size() > 0) {
742 		BOOST_LOG_TRIVIAL(info) << format("%1% bundles incompatible. Asking for action...", updates.incompats.size());
743 
744 		std::unordered_map<std::string, wxString> incompats_map;
745 		for (const auto &incompat : updates.incompats) {
746 			const auto min_slic3r = incompat.version.min_slic3r_version;
747 			const auto max_slic3r = incompat.version.max_slic3r_version;
748 			wxString restrictions;
749 			if (min_slic3r != Semver::zero() && max_slic3r != Semver::inf()) {
750                 restrictions = GUI::format_wxstr(_L("requires min. %s and max. %s"),
751                     min_slic3r.to_string(),
752                     max_slic3r.to_string());
753 			} else if (min_slic3r != Semver::zero()) {
754 				restrictions = GUI::format_wxstr(_L("requires min. %s"), min_slic3r.to_string());
755 				BOOST_LOG_TRIVIAL(debug) << "Bundle is not downgrade, user will now have to do whole wizard. This should not happen.";
756 			} else {
757                 restrictions = GUI::format_wxstr(_L("requires max. %s"), max_slic3r.to_string());
758 			}
759 
760 			incompats_map.emplace(std::make_pair(incompat.vendor, std::move(restrictions)));
761 		}
762 
763 		GUI::MsgDataIncompatible dlg(std::move(incompats_map));
764 		const auto res = dlg.ShowModal();
765 		if (res == wxID_REPLACE) {
766 			BOOST_LOG_TRIVIAL(info) << "User wants to re-configure...";
767 
768 			// This effectively removes the incompatible bundles:
769 			// (snapshot is taken beforehand)
770 			if (! p->perform_updates(std::move(updates)) ||
771 				! GUI::wxGetApp().run_wizard(GUI::ConfigWizard::RR_DATA_INCOMPAT))
772 				return R_INCOMPAT_EXIT;
773 
774 			return R_INCOMPAT_CONFIGURED;
775 		}
776 		else {
777 			BOOST_LOG_TRIVIAL(info) << "User wants to exit Slic3r, bye...";
778 			return R_INCOMPAT_EXIT;
779 		}
780 
781 	} else if (updates.updates.size() > 0) {
782 
783 		bool incompatible_version = false;
784 		for (const auto& update : updates.updates) {
785 			incompatible_version = (update.forced_update ? true : incompatible_version);
786 			//td::cout << update.forced_update << std::endl;
787 			//BOOST_LOG_TRIVIAL(info) << format("Update requires higher version.");
788 		}
789 
790 		//forced update
791 		if (incompatible_version)
792 		{
793 			BOOST_LOG_TRIVIAL(info) << format("Update of %1% bundles available. At least one requires higher version of Slicer.", updates.updates.size());
794 
795 			std::vector<GUI::MsgUpdateForced::Update> updates_msg;
796 			for (const auto& update : updates.updates) {
797 				std::string changelog_url = update.version.config_version.prerelease() == nullptr ? update.changelog_url : std::string();
798 				updates_msg.emplace_back(update.vendor, update.version.config_version, update.version.comment, std::move(changelog_url));
799 			}
800 
801 			GUI::MsgUpdateForced dlg(updates_msg);
802 
803 			const auto res = dlg.ShowModal();
804 			if (res == wxID_OK) {
805 				BOOST_LOG_TRIVIAL(info) << "User wants to update...";
806 				if (! p->perform_updates(std::move(updates)))
807 					return R_INCOMPAT_EXIT;
808 				reload_configs_update_gui();
809 				return R_UPDATE_INSTALLED;
810 			}
811 			else {
812 				BOOST_LOG_TRIVIAL(info) << "User wants to exit Slic3r, bye...";
813 				return R_INCOMPAT_EXIT;
814 			}
815 		}
816 
817 		// regular update
818 		if (params == UpdateParams::SHOW_NOTIFICATION) {
819 			p->set_waiting_updates(updates);
820 			GUI::wxGetApp().plater()->get_notification_manager()->push_notification(GUI::NotificationType::PresetUpdateAvailable);
821 		}
822 		else {
823 			BOOST_LOG_TRIVIAL(info) << format("Update of %1% bundles available. Asking for confirmation ...", p->waiting_updates.updates.size());
824 
825 			std::vector<GUI::MsgUpdateConfig::Update> updates_msg;
826 			for (const auto& update : updates.updates) {
827 				std::string changelog_url = update.version.config_version.prerelease() == nullptr ? update.changelog_url : std::string();
828 				updates_msg.emplace_back(update.vendor, update.version.config_version, update.version.comment, std::move(changelog_url));
829 			}
830 
831 			GUI::MsgUpdateConfig dlg(updates_msg, params == UpdateParams::FORCED_BEFORE_WIZARD);
832 
833 			const auto res = dlg.ShowModal();
834 			if (res == wxID_OK) {
835 				BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update";
836 				if (! p->perform_updates(std::move(updates)))
837 					return R_ALL_CANCELED;
838 				reload_configs_update_gui();
839 				return R_UPDATE_INSTALLED;
840 			}
841 			else {
842 				BOOST_LOG_TRIVIAL(info) << "User refused the update";
843 				if (params == UpdateParams::FORCED_BEFORE_WIZARD && res == wxID_CANCEL)
844 					return R_ALL_CANCELED;
845 				return R_UPDATE_REJECT;
846 			}
847 		}
848 
849 		// MsgUpdateConfig will show after the notificaation is clicked
850 	} else {
851 		BOOST_LOG_TRIVIAL(info) << "No configuration updates available.";
852 	}
853 
854 	return R_NOOP;
855 }
856 
install_bundles_rsrc(std::vector<std::string> bundles,bool snapshot) const857 bool PresetUpdater::install_bundles_rsrc(std::vector<std::string> bundles, bool snapshot) const
858 {
859 	Updates updates;
860 
861 	BOOST_LOG_TRIVIAL(info) << format("Installing %1% bundles from resources ...", bundles.size());
862 
863 	for (const auto &bundle : bundles) {
864 		auto path_in_rsrc = (p->rsrc_path / bundle).replace_extension(".ini");
865 		auto path_in_vendors = (p->vendor_path / bundle).replace_extension(".ini");
866 		updates.updates.emplace_back(std::move(path_in_rsrc), std::move(path_in_vendors), Version(), "", "");
867 	}
868 
869 	return p->perform_updates(std::move(updates), snapshot);
870 }
871 
on_update_notification_confirm()872 void PresetUpdater::on_update_notification_confirm()
873 {
874 	if (!p->has_waiting_updates)
875 		return;
876 	BOOST_LOG_TRIVIAL(info) << format("Update of %1% bundles available. Asking for confirmation ...", p->waiting_updates.updates.size());
877 
878 	std::vector<GUI::MsgUpdateConfig::Update> updates_msg;
879 	for (const auto& update : p->waiting_updates.updates) {
880 		std::string changelog_url = update.version.config_version.prerelease() == nullptr ? update.changelog_url : std::string();
881 		updates_msg.emplace_back(update.vendor, update.version.config_version, update.version.comment, std::move(changelog_url));
882 	}
883 
884 	GUI::MsgUpdateConfig dlg(updates_msg);
885 
886 	const auto res = dlg.ShowModal();
887 	if (res == wxID_OK) {
888 		BOOST_LOG_TRIVIAL(debug) << "User agreed to perform the update";
889 		if (p->perform_updates(std::move(p->waiting_updates))) {
890 			reload_configs_update_gui();
891 			p->has_waiting_updates = false;
892 		}
893 	}
894 	else {
895 		BOOST_LOG_TRIVIAL(info) << "User refused the update";
896 	}
897 }
898 
899 }
900