Skip to content

Commit

Permalink
pool/project manager: check pool item mtimes and update pool if required
Browse files Browse the repository at this point in the history
  • Loading branch information
carrotIndustries committed Apr 16, 2022
1 parent 82ee006 commit 1b691d1
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/pool-prj-mgr/pool-prj-mgr-app_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "blocks/blocks_schematic.hpp"
#include "project/project.hpp"
#include <filesystem>
#include "util/fs_util.hpp"
#include "widgets/sqlite_shell.hpp"

#ifdef G_OS_WIN32
Expand Down Expand Up @@ -239,6 +240,8 @@ PoolProjectManagerAppWindow::PoolProjectManagerAppWindow(BaseObjectType *cobject
part_browser_window->pool_updated(path);
},
*this));

check_mtimes_dispatcher.connect([this] { pool_update(); });
}


Expand Down Expand Up @@ -931,6 +934,8 @@ bool PoolProjectManagerAppWindow::really_close_pool_or_project()
set_view_mode(ViewMode::OPEN);
}
output_window->clear_all();
if (check_mtimes_thread.joinable())
check_mtimes_thread.join();
return true;
}

Expand Down Expand Up @@ -1174,6 +1179,49 @@ gboolean PoolProjectManagerAppWindow::part_browser_key_pressed(GtkEventControlle
}
#endif

void PoolProjectManagerAppWindow::check_mtimes(const std::string &path)
{
try {
Pool my_pool(path);
int64_t last_update_time = 0;
{
SQLite::Query q(my_pool.db, "SELECT time FROM last_updated");
if (q.step())
last_update_time = q.get<sqlite3_int64>(0);
else
return;
}

for (auto &[bp, uu] : my_pool.get_actually_included_pools(true)) {
PoolInfo pool_info(bp);
for (const auto &[type, name] : IPool::type_names) {
const auto p = fs::u8path(bp) / name;
if (!fs::is_directory(p))
continue;
for (const auto &ent : fs::recursive_directory_iterator(p)) {
if (!ent.is_regular_file())
continue;
if (endswith(ent.path().filename().u8string(), ".json")) {
const auto mtime = ent.last_write_time().time_since_epoch().count();
if (mtime > last_update_time) {
Logger::log_info("pool " + path + " got updated", Logger::Domain::POOL_UPDATE,
"beacause " + ent.path().u8string() + " has been modified");
check_mtimes_dispatcher.emit();
return;
}
}
}
}
}
}
catch (const std::exception &e) {
Logger::log_critical("exception thrown in check_mtimes", Logger::Domain::POOL_UPDATE, e.what());
}
catch (...) {
Logger::log_critical("unknown exception thrown in check_mtimes", Logger::Domain::POOL_UPDATE);
}
}


void PoolProjectManagerAppWindow::check_pool_update(const std::string &base_path)
{
Expand All @@ -1183,6 +1231,9 @@ void PoolProjectManagerAppWindow::check_pool_update(const std::string &base_path
else if (check_pool_update_inc(base_path)) {
pool_update();
}
else {
check_mtimes_thread = std::thread(&PoolProjectManagerAppWindow::check_mtimes, this, base_path);
}
}

void PoolProjectManagerAppWindow::open_file_view(const Glib::RefPtr<Gio::File> &file)
Expand Down
4 changes: 4 additions & 0 deletions src/pool-prj-mgr/pool-prj-mgr-app_win.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "prj-mgr/prj-mgr_views.hpp"
#include "pool-mgr/view_create_pool.hpp"
#include "common/common.hpp"
#include <thread>

namespace horizon {
using json = nlohmann::json;
Expand Down Expand Up @@ -192,6 +193,9 @@ class PoolProjectManagerAppWindow : public Gtk::ApplicationWindow {
bool check_schema_update(const std::string &base_path);

void check_pool_update(const std::string &base_path);
void check_mtimes(const std::string &base_path);
std::thread check_mtimes_thread;
Glib::Dispatcher check_mtimes_dispatcher;

bool check_autosave(PoolProjectManagerProcess::Type type, const std::vector<std::string> &filenames);

Expand Down

0 comments on commit 1b691d1

Please sign in to comment.