some changes on the filemonitor signals

This commit is contained in:
2024-06-12 15:41:12 +02:00
parent 09987eed01
commit df5e9bdfa9
5 changed files with 105 additions and 76 deletions

View File

@@ -1,38 +1,49 @@
#include "FileController.hpp" #include "FileController.hpp"
#include <chrono> #include <chrono>
#include <memory> #include <filesystem>
#include <iostream> #include <iostream>
#include <memory>
#include <unordered_map>
FileController::FileController( ) { FileController::FileController() {
#ifdef _WIN32 #ifdef _WIN32
m_fmWorker = std::make_shared<FileMonitor>(std::filesystem::path(std::getenv("USERPROFILE")), m_fmWorker = std::make_shared<FileMonitor>(
std::chrono::milliseconds(1000)); std::filesystem::path(std::getenv("USERPROFILE")),
std::chrono::milliseconds(1000));
#else #else
m_fmWorker = std::make_shared<FileMonitor>(std::filesystem::path("/home/crylia/Dokumente"), m_fmWorker = std::make_shared<FileMonitor>(
std::chrono::milliseconds(1000)); std::filesystem::path("/home/crylia/Dokumente"),
std::chrono::milliseconds(1000));
#endif #endif
m_fmWorker->moveToThread(&m_fsThread); m_fmWorker->moveToThread(&m_fsThread);
connect(this, &FileController::operate, m_fmWorker.get( ), &FileMonitor::start); connect(this, &FileController::operate, m_fmWorker.get(),
connect(m_fmWorker.get( ), &FileMonitor::update, this, &FileController::update); &FileMonitor::start);
connect(m_fmWorker.get( ), &FileMonitor::newPathEntered, this, &FileController::emitNewPath); connect(m_fmWorker.get(), &FileMonitor::fileCreated, this,
&FileController::update);
connect(m_fmWorker.get(), &FileMonitor::pathChanged, this,
&FileController::newPath);
m_fsThread.start( ); m_fsThread.start();
emit operate( ); emit operate();
} }
FileController::~FileController( ) { FileController::~FileController() {
m_fsThread.quit( ); m_fsThread.quit();
m_fsThread.wait( ); m_fsThread.wait();
} }
void FileController::update(const std::filesystem::path path, const FileEvent event) { void FileController::newPath(
std::cout << path.string( ) << std::endl; std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
} path) {
void FileController::emitNewPath(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) {
std::cout << "test" << std::endl; std::cout << "test" << std::endl;
emit newPathEntered(paths); emit pathChanged(path);
}
void FileController::update(const std::filesystem::path path,
const FileEvent event) {
std::cout << "bruh" << std::endl;
std::cout << path.string() << std::endl;
} }

View File

@@ -8,8 +8,8 @@
class FileController : public QObject { class FileController : public QObject {
Q_OBJECT Q_OBJECT
public: public:
FileController( ); FileController();
~FileController( ); ~FileController();
private: private:
QThread m_fsThread; QThread m_fsThread;
@@ -19,15 +19,19 @@ private:
private slots: private slots:
void update(const std::filesystem::path path, const FileEvent); void update(const std::filesystem::path path, const FileEvent);
void emitNewPath(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths); void newPath(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
signals: signals:
// Start signal to start the function in the thread // Start signal to start the function in the thread
void operate( ); void operate();
// Stop signal to stop the function in the thread // Stop signal to stop the function in the thread
void pause( ); void pause();
// Signal to update the path inside the thread // Signal to update the path inside the thread
void updatePath(const std::filesystem::path&); void updatePath(const std::filesystem::path &);
void newPathEntered(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths); void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
}; };

View File

@@ -1,56 +1,60 @@
#include "FileMonitor.hpp" #include "FileMonitor.hpp"
#include <filesystem> #include <filesystem>
#include <thread>
#include <iostream> #include <iostream>
#include <thread>
FileMonitor::FileMonitor(std::filesystem::path path, FileMonitor::FileMonitor(std::filesystem::path path,
std::chrono::duration<int, std::milli> delay) std::chrono::duration<int, std::milli> delay)
: m_path(path), m_delay(delay) { : m_path(path), m_delay(delay) {
for (auto& file : initPathsMap();
std::filesystem::directory_iterator(m_path)) { emit pathChanged(m_paths);
m_paths[file.path( )] = std::filesystem::last_write_time(file);
std::cout << file.path( ).string( ) << std::endl;
}
emit newPathEntered(m_paths);
} }
void FileMonitor::start( ) { void FileMonitor::SetPath(std::filesystem::path newPath) {
m_path = newPath;
initPathsMap();
emit pathChanged(m_paths);
}
void FileMonitor::initPathsMap() {
m_paths.clear();
for (auto &file : std::filesystem::directory_iterator(m_path)) {
m_paths[file.path()] = std::filesystem::last_write_time(file);
}
}
void FileMonitor::start() {
while (m_running) { while (m_running) {
std::this_thread::sleep_for(m_delay); std::this_thread::sleep_for(m_delay);
auto pbegin = m_paths.begin( ); auto pbegin = m_paths.begin();
while (pbegin != m_paths.end( )) { while (pbegin != m_paths.end()) {
if (!std::filesystem::exists(pbegin->first)) { if (!std::filesystem::exists(pbegin->first)) {
emit update(pbegin->first, FileEvent::erased); emit fileDeleted(pbegin->first, FileEvent::erased);
pbegin = m_paths.erase(pbegin); pbegin = m_paths.erase(pbegin);
} else } else
pbegin++; pbegin++;
} }
for (auto& file : for (auto &file : std::filesystem::directory_iterator(m_path)) {
std::filesystem::directory_iterator(m_path)) {
auto curr_file_last_write = std::filesystem::last_write_time(file); auto curr_file_last_write = std::filesystem::last_write_time(file);
if (!contains(file.path( ))) { if (!contains(file.path())) {
m_paths[file.path( )] = m_paths[file.path()] = curr_file_last_write;
curr_file_last_write; emit fileCreated(file.path(), FileEvent::created);
emit update(file.path( ), FileEvent::created);
} else { } else {
if (m_paths[file.path( )] != if (m_paths[file.path()] != curr_file_last_write) {
curr_file_last_write) { m_paths[file.path()] = curr_file_last_write;
m_paths[file.path( )] = emit fileModified(file.path(), FileEvent::modified);
curr_file_last_write;
emit update(file.path( ), FileEvent::modified);
} }
} }
} }
} }
} }
void FileMonitor::stop( ) { m_running = false; } void FileMonitor::stop() { m_running = false; }
bool FileMonitor::contains(const std::filesystem::path& key) { bool FileMonitor::contains(const std::filesystem::path &key) {
return m_paths.find(key) != m_paths.end( ); return m_paths.find(key) != m_paths.end();
} }

View File

@@ -12,23 +12,31 @@ class FileMonitor : public QObject {
public: public:
std::chrono::duration<int, std::milli> m_delay; std::chrono::duration<int, std::milli> m_delay;
FileMonitor(std::filesystem::path path, std::chrono::duration<int, std::milli> delay); FileMonitor(std::filesystem::path path,
~FileMonitor( ) = default; std::chrono::duration<int, std::milli> delay);
~FileMonitor() = default;
void SetPath(std::filesystem::path path) { m_path = path; } void SetPath(std::filesystem::path path);
public slots: public slots:
void start( ); void start();
void stop( ); void stop();
private: private:
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> m_paths; std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
m_paths;
bool m_running; bool m_running;
std::filesystem::path m_path; std::filesystem::path m_path;
bool contains(const std::filesystem::path& key); bool contains(const std::filesystem::path &key);
void initPathsMap();
signals: signals:
void update(const std::filesystem::path path, const FileEvent); void fileDeleted(const std::filesystem::path path, const FileEvent);
void newPathEntered(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths); void fileCreated(const std::filesystem::path path, const FileEvent);
void fileModified(const std::filesystem::path path, const FileEvent);
void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
}; };

View File

@@ -1,20 +1,22 @@
#include "GridItemView.hpp" #include "GridItemView.hpp"
#include <iostream> #include <iostream>
GridItemView::GridItemView(QWidget* parent) GridItemView::GridItemView(QWidget *parent)
: QFrame(parent), : QFrame(parent), fileController(std::make_shared<FileController>()) {
fileController(std::make_shared<FileController>( )) {
QGridLayout* mainLayout = new QGridLayout( ); QGridLayout *mainLayout = new QGridLayout();
connect(fileController.get( ), &FileController::newPathEntered, this, [this, mainLayout](const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) { connect(fileController.get(), &FileController::pathChanged, this,
std::cout << "path.first" << std::endl; [this,
mainLayout](const std::unordered_map<std::filesystem::path,
for (auto path : paths) { std::filesystem::file_time_type>
mainLayout->addWidget(new GridItem(path.first, this)); paths) {
} std::cout << "path.first" << std::endl;
});
for (auto path : paths) {
mainLayout->addWidget(new GridItem(path.first, this));
}
});
} }
GridItemView::~GridItemView( ) { } GridItemView::~GridItemView() {}