mirror of
https://github.com/SoftFever/OrcaSlicer.git
synced 2025-07-25 07:34:03 -06:00
Configuration update application at startup
This commit is contained in:
parent
b030791384
commit
b49b59cbb2
14 changed files with 288 additions and 54 deletions
|
@ -1,21 +1,32 @@
|
|||
#include "PresetUpdater.hpp"
|
||||
|
||||
#include <iostream> // XXX
|
||||
#include <algorithm>
|
||||
#include <thread>
|
||||
#include <stack>
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/filesystem/fstream.hpp>
|
||||
|
||||
#include <wx/app.h>
|
||||
#include <wx/event.h>
|
||||
#include <wx/msgdlg.h>
|
||||
|
||||
#include "libslic3r/Utils.hpp"
|
||||
#include "slic3r/GUI/GUI.hpp"
|
||||
#include "slic3r/GUI/PresetBundle.hpp"
|
||||
#include "slic3r/Utils/Http.hpp"
|
||||
#include "slic3r/Utils/Semver.hpp"
|
||||
#include "slic3r/Config/Version.hpp"
|
||||
#include "slic3r/Config/Snapshot.hpp"
|
||||
|
||||
namespace fs = boost::filesystem;
|
||||
using Slic3r::GUI::Config::Index;
|
||||
using Slic3r::GUI::Config::Version;
|
||||
using Slic3r::GUI::Config::Snapshot;
|
||||
using Slic3r::GUI::Config::SnapshotDB;
|
||||
|
||||
// XXX: Prevent incomplete file downloads: download a tmp file, then move
|
||||
// Delete incomplete ones on startup.
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
|
@ -27,26 +38,55 @@ enum {
|
|||
SLIC3R_VERSION_BODY_MAX = 256,
|
||||
};
|
||||
|
||||
|
||||
struct Update
|
||||
{
|
||||
fs::path source;
|
||||
fs::path target;
|
||||
Version version;
|
||||
|
||||
Update(const fs::path &source, fs::path &&target, const Version &version) :
|
||||
source(source),
|
||||
target(std::move(target)),
|
||||
version(version)
|
||||
{}
|
||||
|
||||
std::string name() { return source.stem().string(); }
|
||||
};
|
||||
|
||||
typedef std::vector<Update> Updates;
|
||||
|
||||
|
||||
struct PresetUpdater::priv
|
||||
{
|
||||
int version_online_event;
|
||||
AppConfig *app_config;
|
||||
bool version_check;
|
||||
bool preset_update;
|
||||
|
||||
fs::path cache_path;
|
||||
fs::path rsrc_path;
|
||||
fs::path vendor_path;
|
||||
|
||||
bool cancel;
|
||||
std::thread thread;
|
||||
|
||||
priv(int event);
|
||||
priv(int event, AppConfig *app_config);
|
||||
|
||||
void download(const std::set<VendorProfile> vendors) const;
|
||||
|
||||
void check_install_indices() const;
|
||||
Updates config_update() const;
|
||||
};
|
||||
|
||||
PresetUpdater::priv::priv(int event) :
|
||||
PresetUpdater::priv::priv(int event, AppConfig *app_config) :
|
||||
version_online_event(event),
|
||||
app_config(app_config),
|
||||
version_check(false),
|
||||
preset_update(false),
|
||||
cache_path(fs::path(Slic3r::data_dir()) / "cache"),
|
||||
rsrc_path(fs::path(resources_dir()) / "profiles"),
|
||||
vendor_path(fs::path(Slic3r::data_dir()) / "vendor"),
|
||||
cancel(false)
|
||||
{}
|
||||
|
||||
|
@ -91,11 +131,119 @@ void PresetUpdater::priv::download(const std::set<VendorProfile> vendors) const
|
|||
}
|
||||
}
|
||||
|
||||
void PresetUpdater::priv::check_install_indices() const
|
||||
{
|
||||
for (fs::directory_iterator it(rsrc_path); it != fs::directory_iterator(); ++it) {
|
||||
const auto &path = it->path();
|
||||
if (path.extension() == ".idx") {
|
||||
const auto path_in_cache = cache_path / path.filename();
|
||||
|
||||
// TODO: compare versions
|
||||
if (! fs::exists(path_in_cache)) {
|
||||
fs::copy_file(path, path_in_cache, fs::copy_option::overwrite_if_exists);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Updates PresetUpdater::priv::config_update() const
|
||||
{
|
||||
priv::check_install_indices();
|
||||
const auto index_db = Index::load_db(); // TODO: Keep in Snapshots singleton?
|
||||
|
||||
Updates updates;
|
||||
|
||||
for (const auto idx : index_db) {
|
||||
const auto bundle_path = vendor_path / (idx.vendor() + ".ini");
|
||||
|
||||
// If the bundle doesn't exist at all, update from resources
|
||||
// if (! fs::exists(bundle_path)) {
|
||||
// auto path_in_rsrc = rsrc_path / (idx.vendor() + ".ini");
|
||||
|
||||
// // Otherwise load it and check for chached updates
|
||||
// const auto rsrc_vp = VendorProfile::from_ini(path_in_rsrc, false);
|
||||
|
||||
// const auto rsrc_ver = idx.find(rsrc_vp.config_version);
|
||||
// if (rsrc_ver == idx.end()) {
|
||||
// // TODO: throw
|
||||
// }
|
||||
|
||||
// if (fs::exists(path_in_rsrc)) {
|
||||
// updates.emplace_back(bundle_path, std::move(path_in_rsrc), *rsrc_ver);
|
||||
// } else {
|
||||
// // XXX: ???
|
||||
// }
|
||||
|
||||
// continue;
|
||||
// }
|
||||
|
||||
if (! fs::exists(bundle_path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Perform a basic load and check the version
|
||||
const auto vp = VendorProfile::from_ini(bundle_path, false);
|
||||
|
||||
const auto ver_current = idx.find(vp.config_version);
|
||||
if (ver_current == idx.end()) {
|
||||
// TODO: throw
|
||||
}
|
||||
|
||||
const auto recommended = idx.recommended();
|
||||
if (recommended == idx.end()) {
|
||||
// TODO: throw
|
||||
}
|
||||
|
||||
if (! ver_current->is_current_slic3r_supported()) {
|
||||
|
||||
// TODO: Downgrade situation
|
||||
|
||||
} else if (recommended->config_version > ver_current->config_version) {
|
||||
// Config bundle update situation
|
||||
|
||||
auto path_in_cache = cache_path / (idx.vendor() + ".ini");
|
||||
const auto cached_vp = VendorProfile::from_ini(path_in_cache, false);
|
||||
if (cached_vp.config_version == recommended->config_version) {
|
||||
updates.emplace_back(bundle_path, std::move(path_in_cache), *ver_current);
|
||||
} else {
|
||||
// XXX: ???
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for bundles that don't have an index
|
||||
// for (fs::directory_iterator it(rsrc_path); it != fs::directory_iterator(); ++it) {
|
||||
// if (it->path().extension() == ".ini") {
|
||||
// const auto &path = it->path();
|
||||
// const auto vendor_id = path.stem().string();
|
||||
|
||||
// const auto needle = std::find_if(index_db.begin(), index_db.end(), [&vendor_id](const Index &idx) {
|
||||
// return idx.vendor() == vendor_id;
|
||||
// });
|
||||
// if (needle != index_db.end()) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// auto vp = VendorProfile::from_ini(path, false);
|
||||
// auto path_in_data = vendor_path / path.filename();
|
||||
|
||||
// if (! fs::exists(path_in_data)) {
|
||||
// Version version;
|
||||
// version.config_version = vp.config_version;
|
||||
// updates.emplace_back(path, std::move(path_in_data), version);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
return updates;
|
||||
}
|
||||
|
||||
|
||||
PresetUpdater::PresetUpdater(int version_online_event, AppConfig *app_config) :
|
||||
p(new priv(version_online_event))
|
||||
p(new priv(version_online_event, app_config))
|
||||
{
|
||||
p->preset_update = app_config->get("preset_update") == "1";
|
||||
// preset_update implies version_check:
|
||||
// preset_update implies version_check: // XXX: not any more
|
||||
p->version_check = p->preset_update || app_config->get("version_check") == "1";
|
||||
}
|
||||
|
||||
|
@ -123,19 +271,49 @@ void PresetUpdater::download(PresetBundle *preset_bundle)
|
|||
}));
|
||||
}
|
||||
|
||||
void PresetUpdater::init_vendors()
|
||||
void PresetUpdater::config_update()
|
||||
{
|
||||
const auto vendors_rources = fs::path(resources_dir()) / "profiles";
|
||||
const auto vendors_data = fs::path(Slic3r::data_dir()) / "vendor";
|
||||
const auto updates = p->config_update();
|
||||
if (updates.size() > 0) {
|
||||
const auto msg = _(L("Configuration update is available. Would you like to install it?"));
|
||||
|
||||
for (fs::directory_iterator it(vendors_rources); it != fs::directory_iterator(); ++it) {
|
||||
if (it->path().extension() == ".ini") {
|
||||
auto vp = VendorProfile::from_ini(it->path(), false);
|
||||
const auto path_in_data = vendors_data / it->path().filename();
|
||||
auto ext_msg = _(L(
|
||||
"Note that a full configuration snapshot will be created first. It can then be restored at any time "
|
||||
"should there be a problem with the new version.\n\n"
|
||||
"Updated configuration bundles:\n"
|
||||
));
|
||||
|
||||
if (! fs::exists(path_in_data) || VendorProfile::from_ini(path_in_data, false).config_version < vp.config_version) {
|
||||
// FIXME: update vendor bundle properly when snapshotting is ready
|
||||
PresetBundle::install_vendor_configbundle(it->path());
|
||||
for (const auto &update : updates) {
|
||||
ext_msg += update.target.stem().string() + " " + update.version.config_version.to_string();
|
||||
if (! update.version.comment.empty()) {
|
||||
ext_msg += std::string(" (") + update.version.comment + ")";
|
||||
}
|
||||
ext_msg += "\n";
|
||||
}
|
||||
|
||||
wxMessageDialog dlg(NULL, msg, _(L("Configuration update")), wxYES_NO|wxCENTRE);
|
||||
dlg.SetExtendedMessage(ext_msg);
|
||||
const auto res = dlg.ShowModal();
|
||||
std::cerr << "After modal" << std::endl;
|
||||
if (res == wxID_YES) {
|
||||
// User gave clearance, updates are go
|
||||
|
||||
// TODO: Comment?
|
||||
SnapshotDB::singleton().take_snapshot(*p->app_config, Snapshot::SNAPSHOT_UPGRADE, "");
|
||||
|
||||
for (const auto &update : updates) {
|
||||
fs::copy_file(update.source, update.target, fs::copy_option::overwrite_if_exists);
|
||||
|
||||
PresetBundle bundle;
|
||||
bundle.load_configbundle(update.target.string(), PresetBundle::LOAD_CFGBNDLE_SYSTEM);
|
||||
|
||||
auto preset_remover = [](const Preset &preset) {
|
||||
fs::remove(preset.file);
|
||||
};
|
||||
|
||||
for (const auto &preset : bundle.prints) { preset_remover(preset); }
|
||||
for (const auto &preset : bundle.filaments) { preset_remover(preset); }
|
||||
for (const auto &preset : bundle.printers) { preset_remover(preset); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue