OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::GUI::Config::SnapshotDB Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.hpp>

Collaboration diagram for Slic3r::GUI::Config::SnapshotDB:

Public Types

typedef std::vector< Snapshot >::const_iterator const_iterator

Public Member Functions

size_t load_db ()
void update_slic3r_versions (std::vector< Index > &index_db)
const Snapshot & take_snapshot (const AppConfig &app_config, Snapshot::Reason reason, const std::string &comment="")
const Snapshot & restore_snapshot (const std::string &id, AppConfig &app_config)
void restore_snapshot (const Snapshot &snapshot, AppConfig &app_config)
bool is_on_snapshot (AppConfig &app_config) const
const_iterator snapshot_with_vendor_preset (const std::string &vendor_name, const Semver &config_version)
const_iterator begin () const
const_iterator end () const
const_iterator snapshot (const std::string &id) const
const std::vector< Snapshot > & snapshots () const

Static Public Member Functions

static SnapshotDB & singleton ()

Static Private Member Functions

static boost::filesystem::path create_db_dir ()

Private Attributes

std::vector< Snapshot > m_snapshots

Detailed Description

Member Typedef Documentation

◆ const_iterator

typedef std::vector<Snapshot>::const_iterator Slic3r::GUI::Config::SnapshotDB::const_iterator

Member Function Documentation

◆ begin()

const_iterator Slic3r::GUI::Config::SnapshotDB::begin ( ) const
inline
119{ return m_snapshots.begin(); }
std::vector< Snapshot > m_snapshots
Definition Snapshot.hpp:129

References m_snapshots.

◆ create_db_dir()

boost::filesystem::path Slic3r::GUI::Config::SnapshotDB::create_db_dir ( )
staticprivate
559{
560 boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
561 boost::filesystem::path snapshots_dir = data_dir / SLIC3R_SNAPSHOTS_DIR;
562 for (const boost::filesystem::path &path : { data_dir, snapshots_dir }) {
563 boost::filesystem::path subdir = path;
564 subdir.make_preferred();
565 if (! boost::filesystem::is_directory(subdir) &&
566 ! boost::filesystem::create_directory(subdir))
567 throw Slic3r::RuntimeError(std::string("Slic3r was unable to create a directory at ") + subdir.string());
568 }
569 return snapshots_dir;
570}
#define SLIC3R_SNAPSHOTS_DIR
Definition Snapshot.cpp:50
const std::string & data_dir()
Definition utils.cpp:365

References Slic3r::data_dir(), and SLIC3R_SNAPSHOTS_DIR.

Referenced by load_db(), restore_snapshot(), and take_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ end()

const_iterator Slic3r::GUI::Config::SnapshotDB::end ( ) const
inline
120{ return m_snapshots.end(); }

References m_snapshots.

Referenced by is_on_snapshot().

Here is the caller graph for this function:

◆ is_on_snapshot()

bool Slic3r::GUI::Config::SnapshotDB::is_on_snapshot ( AppConfig & app_config) const
515{
516 // Is the "on_snapshot" configuration value set?
517 std::string on_snapshot = app_config.get("on_snapshot");
518 if (on_snapshot.empty())
519 // No, we are not on a snapshot.
520 return false;
521 // Is the "on_snapshot" equal to the current configuration state?
522 auto it_snapshot = this->snapshot(on_snapshot);
523 if (it_snapshot != this->end() && it_snapshot->equal_to_active(app_config))
524 // Yes, we are on the snapshot.
525 return true;
526 // No, we are no more on a snapshot. Reset the state.
527 app_config.set("on_snapshot", "");
528 return false;
529}
const_iterator snapshot(const std::string &id) const
Definition Snapshot.cpp:550
const_iterator end() const
Definition Snapshot.hpp:120

References end(), Slic3r::AppConfig::get(), Slic3r::AppConfig::set(), and snapshot().

Here is the call graph for this function:

◆ load_db()

size_t Slic3r::GUI::Config::SnapshotDB::load_db ( )
341{
342 boost::filesystem::path snapshots_dir = SnapshotDB::create_db_dir();
343
344 m_snapshots.clear();
345
346 // Walk over the snapshot directories and load their index.
347 std::string errors_cummulative;
348 for (auto &dir_entry : boost::filesystem::directory_iterator(snapshots_dir))
349 if (boost::filesystem::is_directory(dir_entry.status())) {
350 // Try to read "snapshot.ini".
351 boost::filesystem::path path_ini = dir_entry.path() / SLIC3R_SNAPSHOT_FILE;
352 Snapshot snapshot;
353 try {
354 snapshot.load_ini(path_ini.string());
355 } catch (const std::runtime_error &err) {
356 errors_cummulative += err.what();
357 errors_cummulative += "\n";
358 continue;
359 }
360 // Check that the name of the snapshot directory matches the snapshot id stored in the snapshot.ini file.
361 if (dir_entry.path().filename().string() != snapshot.id) {
362 errors_cummulative += std::string("Snapshot ID ") + snapshot.id + " does not match the snapshot directory " + dir_entry.path().filename().string() + "\n";
363 continue;
364 }
365 m_snapshots.emplace_back(std::move(snapshot));
366 }
367 // Sort the snapshots by their date/time.
368 std::sort(m_snapshots.begin(), m_snapshots.end(), [](const Snapshot &s1, const Snapshot &s2) { return s1.time_captured < s2.time_captured; });
369 if (! errors_cummulative.empty())
370 throw Slic3r::RuntimeError(errors_cummulative);
371 return m_snapshots.size();
372}
#define SLIC3R_SNAPSHOT_FILE
Definition Snapshot.cpp:51
static boost::filesystem::path create_db_dir()
Definition Snapshot.cpp:558

References create_db_dir(), m_snapshots, SLIC3R_SNAPSHOT_FILE, and snapshot().

Referenced by singleton().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ restore_snapshot() [1/2]

void Slic3r::GUI::Config::SnapshotDB::restore_snapshot ( const Snapshot & snapshot,
AppConfig & app_config )
496{
497 boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
498 boost::filesystem::path snapshot_db_dir = SnapshotDB::create_db_dir();
499 boost::filesystem::path snapshot_dir = snapshot_db_dir / snapshot.id;
500 // Remove existing ini files and restore the ini files from the snapshot.
501 for (const char *subdir : snapshot_subdirs) {
502 boost::filesystem::path src = snapshot_dir / subdir;
503 boost::filesystem::path dst = data_dir / subdir;
505 if (boost::filesystem::is_directory(src))
507 }
508 // Update AppConfig with the selections of the print / sla_print / filament / sla_material / printer profiles
509 // and about the installed printer types and variants.
510 snapshot.export_selections(app_config);
511 snapshot.export_vendor_configs(app_config);
512}
static constexpr auto snapshot_subdirs
Definition Snapshot.cpp:267
static void delete_existing_ini_files(const boost::filesystem::path &path)
Definition Snapshot.cpp:405
static void copy_config_dir_single_level(const boost::filesystem::path &path_src, const boost::filesystem::path &path_dst)
Definition Snapshot.cpp:390

References Slic3r::GUI::Config::copy_config_dir_single_level(), create_db_dir(), Slic3r::data_dir(), Slic3r::GUI::Config::delete_existing_ini_files(), snapshot(), and Slic3r::GUI::Config::snapshot_subdirs.

Here is the call graph for this function:

◆ restore_snapshot() [2/2]

const Snapshot & Slic3r::GUI::Config::SnapshotDB::restore_snapshot ( const std::string & id,
AppConfig & app_config )
486{
487 for (const Snapshot &snapshot : m_snapshots)
488 if (snapshot.id == id) {
489 this->restore_snapshot(snapshot, app_config);
490 return snapshot;
491 }
492 throw Slic3r::RuntimeError(std::string("Snapshot with id " + id + " was not found."));
493}
const Snapshot & restore_snapshot(const std::string &id, AppConfig &app_config)
Definition Snapshot.cpp:485

References m_snapshots, restore_snapshot(), and snapshot().

Referenced by restore_snapshot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ singleton()

SnapshotDB & Slic3r::GUI::Config::SnapshotDB::singleton ( )
static
573{
574 static SnapshotDB instance;
575 static bool loaded = false;
576 if (! loaded) {
577 try {
578 loaded = true;
579 // Load the snapshot database.
580 instance.load_db();
581 // Load the vendor specific configuration indices.
582 std::vector<Index> index_db = Index::load_db();
583 // Update the min / max slic3r versions compatible with the configurations stored inside the snapshots
584 // based on the min / max slic3r versions defined by the vendor specific config indices.
585 instance.update_slic3r_versions(index_db);
586 } catch (std::exception & /* ex */) {
587 }
588 }
589 return instance;
590}
static std::vector< Index > load_db()
Definition Version.cpp:319

References Slic3r::GUI::Config::Index::load_db(), load_db(), and update_slic3r_versions().

Referenced by Slic3r::GUI::Config::take_config_snapshot_cancel_on_error(), and Slic3r::GUI::Config::take_config_snapshot_report_error().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ snapshot()

SnapshotDB::const_iterator Slic3r::GUI::Config::SnapshotDB::snapshot ( const std::string & id) const
551{
552 for (const_iterator it = m_snapshots.begin(); it != m_snapshots.end(); ++ it)
553 if (it->id == id)
554 return it;
555 return m_snapshots.end();
556}
std::vector< Snapshot >::const_iterator const_iterator
Definition Snapshot.hpp:100

References m_snapshots.

Referenced by is_on_snapshot(), load_db(), restore_snapshot(), restore_snapshot(), snapshot_with_vendor_preset(), take_snapshot(), and update_slic3r_versions().

Here is the caller graph for this function:

◆ snapshot_with_vendor_preset()

SnapshotDB::const_iterator Slic3r::GUI::Config::SnapshotDB::snapshot_with_vendor_preset ( const std::string & vendor_name,
const Semver & config_version )
532{
533 auto it_found = m_snapshots.end();
534 Snapshot::VendorConfig key;
535 key.name = vendor_name;
536 for (auto it = m_snapshots.begin(); it != m_snapshots.end(); ++ it) {
537 const Snapshot &snapshot = *it;
538 auto it_vendor_config = std::lower_bound(snapshot.vendor_configs.begin(), snapshot.vendor_configs.end(),
539 key, [](const Snapshot::VendorConfig &cfg1, const Snapshot::VendorConfig &cfg2) { return cfg1.name < cfg2.name; });
540 if (it_vendor_config != snapshot.vendor_configs.end() && it_vendor_config->name == vendor_name &&
541 config_version == it_vendor_config->version.config_version) {
542 // Vendor config found with the correct version.
543 // Save it, but continue searching, as we want the newest snapshot.
544 it_found = it;
545 }
546 }
547 return it_found;
548}

References m_snapshots, Slic3r::GUI::Config::Snapshot::VendorConfig::name, and snapshot().

Here is the call graph for this function:

◆ snapshots()

const std::vector< Snapshot > & Slic3r::GUI::Config::SnapshotDB::snapshots ( ) const
inline
122{ return m_snapshots; }

References m_snapshots.

◆ take_snapshot()

const Snapshot & Slic3r::GUI::Config::SnapshotDB::take_snapshot ( const AppConfig & app_config,
Snapshot::Reason reason,
const std::string & comment = "" )
418{
419 boost::filesystem::path data_dir = boost::filesystem::path(Slic3r::data_dir());
420 boost::filesystem::path snapshot_db_dir = SnapshotDB::create_db_dir();
421
422 // 1) Prepare the snapshot structure.
423 Snapshot snapshot;
424 // Snapshot header.
427 snapshot.slic3r_version_captured = Slic3r::SEMVER;
428 snapshot.comment = comment;
429 snapshot.reason = reason;
430 // Active presets at the time of the snapshot.
431 snapshot.print = app_config.get("presets", PRESET_PRINT_NAME);
432 snapshot.sla_print = app_config.get("presets", "sla_print");
433 snapshot.filaments.emplace_back(app_config.get("presets", PRESET_FILAMENT_NAME));
434 snapshot.sla_material = app_config.get("presets", "sla_material");
435 snapshot.printer = app_config.get("presets", PRESET_PRINTER_NAME);
436 snapshot.physical_printer = app_config.get("presets", "physical_printer");
437 for (unsigned i = 1; i < 1000; ++ i) {
438 char name[64];
439 sprintf(name, "filament_%u", i);
440 if (! app_config.has("presets", name))
441 break;
442 snapshot.filaments.emplace_back(app_config.get("presets", name));
443 }
444 // Vendor specific config bundles and installed printers.
445 for (const auto &vendor : app_config.vendors()) {
446 Snapshot::VendorConfig cfg;
447 cfg.name = vendor.first;
448 cfg.models_variants_installed = vendor.second;
449 for (auto it = cfg.models_variants_installed.begin(); it != cfg.models_variants_installed.end();)
450 if (it->second.empty())
451 cfg.models_variants_installed.erase(it ++);
452 else
453 ++ it;
454 // Orca: the version the vendor is installed at, read from its profile or —
455 // where the cache is the whole installation — from the cache's own stamp.
456 cfg.version.config_version = installed_vendor_version(cfg.name);
457 snapshot.vendor_configs.emplace_back(std::move(cfg));
458 }
459
460 boost::filesystem::path snapshot_dir = snapshot_db_dir / snapshot.id;
461
462 try {
463 boost::filesystem::create_directory(snapshot_dir);
464
465 // Backup the presets.
466 for (const char *subdir : snapshot_subdirs)
467 copy_config_dir_single_level(data_dir / subdir, snapshot_dir / subdir);
468 snapshot.save_ini((snapshot_dir / "snapshot.ini").string());
469 assert(m_snapshots.empty() || m_snapshots.back().time_captured <= snapshot.time_captured);
470 m_snapshots.emplace_back(std::move(snapshot));
471 } catch (...) {
472 if (boost::filesystem::is_directory(snapshot_dir)) {
473 try {
474 // Clean up partially copied snapshot.
475 boost::filesystem::remove_all(snapshot_dir);
476 } catch (...) {
477 BOOST_LOG_TRIVIAL(error) << "Failed taking snapshot and failed removing the snapshot directory " << snapshot_dir;
478 }
479 }
480 throw;
481 }
482 return m_snapshots.back();
483}
#define PRESET_PRINTER_NAME
Definition Preset.hpp:35
#define PRESET_PRINT_NAME
Definition Preset.hpp:34
#define PRESET_FILAMENT_NAME
Definition Preset.hpp:33
std::string iso_utc_timestamp()
Definition Time.hpp:56
time_t get_current_time_utc()
Definition Time.cpp:183
Semver installed_vendor_version(const std::string &vendor)
Definition utils.cpp:1983
Semver SEMVER
Definition Semver.cpp:6

References Slic3r::GUI::Config::Version::config_version, Slic3r::GUI::Config::copy_config_dir_single_level(), create_db_dir(), Slic3r::data_dir(), Slic3r::AppConfig::get(), Slic3r::Utils::get_current_time_utc(), Slic3r::AppConfig::has(), Slic3r::installed_vendor_version(), Slic3r::Utils::iso_utc_timestamp(), m_snapshots, Slic3r::GUI::Config::Snapshot::VendorConfig::models_variants_installed, Slic3r::GUI::Config::Snapshot::VendorConfig::name, PRESET_FILAMENT_NAME, PRESET_PRINT_NAME, PRESET_PRINTER_NAME, Slic3r::SEMVER, snapshot(), Slic3r::GUI::Config::snapshot_subdirs, Slic3r::AppConfig::vendors(), and Slic3r::GUI::Config::Snapshot::VendorConfig::version.

Referenced by Slic3r::GUI::Config::take_config_snapshot_cancel_on_error(), and Slic3r::GUI::Config::take_config_snapshot_report_error().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_slic3r_versions()

void Slic3r::GUI::Config::SnapshotDB::update_slic3r_versions ( std::vector< Index > & index_db)
375{
376 for (Snapshot &snapshot : m_snapshots) {
377 for (Snapshot::VendorConfig &vendor_config : snapshot.vendor_configs) {
378 auto it = std::find_if(index_db.begin(), index_db.end(), [&vendor_config](const Index &idx) { return idx.vendor() == vendor_config.name; });
379 if (it != index_db.end()) {
380 Index::const_iterator it_version = it->find(vendor_config.version.config_version);
381 if (it_version != it->end()) {
382 vendor_config.version.min_slic3r_version = it_version->min_slic3r_version;
383 vendor_config.version.max_slic3r_version = it_version->max_slic3r_version;
384 }
385 }
386 }
387 }
388}
std::vector< Version >::const_iterator const_iterator
Definition Version.hpp:57

References Slic3r::GUI::Config::Version::config_version, m_snapshots, Slic3r::GUI::Config::Version::max_slic3r_version, Slic3r::GUI::Config::Version::min_slic3r_version, snapshot(), and Slic3r::GUI::Config::Snapshot::VendorConfig::version.

Referenced by singleton().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_snapshots

std::vector<Snapshot> Slic3r::GUI::Config::SnapshotDB::m_snapshots
private

The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/Config/Snapshot.cpp