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 <chrono>
#include <memory>
#include <filesystem>
#include <iostream>
#include <memory>
#include <unordered_map>
FileController::FileController( ) {
FileController::FileController() {
#ifdef _WIN32
m_fmWorker = std::make_shared<FileMonitor>(std::filesystem::path(std::getenv("USERPROFILE")),
std::chrono::milliseconds(1000));
m_fmWorker = std::make_shared<FileMonitor>(
std::filesystem::path(std::getenv("USERPROFILE")),
std::chrono::milliseconds(1000));
#else
m_fmWorker = std::make_shared<FileMonitor>(std::filesystem::path("/home/crylia/Dokumente"),
std::chrono::milliseconds(1000));
m_fmWorker = std::make_shared<FileMonitor>(
std::filesystem::path("/home/crylia/Dokumente"),
std::chrono::milliseconds(1000));
#endif
m_fmWorker->moveToThread(&m_fsThread);
connect(this, &FileController::operate, m_fmWorker.get( ), &FileMonitor::start);
connect(m_fmWorker.get( ), &FileMonitor::update, this, &FileController::update);
connect(m_fmWorker.get( ), &FileMonitor::newPathEntered, this, &FileController::emitNewPath);
connect(this, &FileController::operate, m_fmWorker.get(),
&FileMonitor::start);
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( ) {
m_fsThread.quit( );
m_fsThread.wait( );
FileController::~FileController() {
m_fsThread.quit();
m_fsThread.wait();
}
void FileController::update(const std::filesystem::path path, const FileEvent event) {
std::cout << path.string( ) << std::endl;
}
void FileController::emitNewPath(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) {
void FileController::newPath(
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
path) {
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 {
Q_OBJECT
public:
FileController( );
~FileController( );
FileController();
~FileController();
private:
QThread m_fsThread;
@@ -19,15 +19,19 @@ private:
private slots:
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:
// Start signal to start the function in the thread
void operate( );
void operate();
// Stop signal to stop the function in the thread
void pause( );
void pause();
// 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 <filesystem>
#include <thread>
#include <iostream>
#include <thread>
FileMonitor::FileMonitor(std::filesystem::path path,
std::chrono::duration<int, std::milli> delay)
: m_path(path), m_delay(delay) {
std::chrono::duration<int, std::milli> delay)
: m_path(path), m_delay(delay) {
for (auto& file :
std::filesystem::directory_iterator(m_path)) {
m_paths[file.path( )] = std::filesystem::last_write_time(file);
std::cout << file.path( ).string( ) << std::endl;
}
emit newPathEntered(m_paths);
initPathsMap();
emit pathChanged(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) {
std::this_thread::sleep_for(m_delay);
auto pbegin = m_paths.begin( );
while (pbegin != m_paths.end( )) {
auto pbegin = m_paths.begin();
while (pbegin != m_paths.end()) {
if (!std::filesystem::exists(pbegin->first)) {
emit update(pbegin->first, FileEvent::erased);
emit fileDeleted(pbegin->first, FileEvent::erased);
pbegin = m_paths.erase(pbegin);
} else
pbegin++;
}
for (auto& file :
std::filesystem::directory_iterator(m_path)) {
for (auto &file : std::filesystem::directory_iterator(m_path)) {
auto curr_file_last_write = std::filesystem::last_write_time(file);
if (!contains(file.path( ))) {
m_paths[file.path( )] =
curr_file_last_write;
emit update(file.path( ), FileEvent::created);
if (!contains(file.path())) {
m_paths[file.path()] = curr_file_last_write;
emit fileCreated(file.path(), FileEvent::created);
} else {
if (m_paths[file.path( )] !=
curr_file_last_write) {
m_paths[file.path( )] =
curr_file_last_write;
emit update(file.path( ), FileEvent::modified);
if (m_paths[file.path()] != curr_file_last_write) {
m_paths[file.path()] = curr_file_last_write;
emit fileModified(file.path(), FileEvent::modified);
}
}
}
}
}
void FileMonitor::stop( ) { m_running = false; }
void FileMonitor::stop() { m_running = false; }
bool FileMonitor::contains(const std::filesystem::path& key) {
return m_paths.find(key) != m_paths.end( );
bool FileMonitor::contains(const std::filesystem::path &key) {
return m_paths.find(key) != m_paths.end();
}

View File

@@ -12,23 +12,31 @@ class FileMonitor : public QObject {
public:
std::chrono::duration<int, std::milli> m_delay;
FileMonitor(std::filesystem::path path, std::chrono::duration<int, std::milli> delay);
~FileMonitor( ) = default;
FileMonitor(std::filesystem::path path,
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:
void start( );
void stop( );
void start();
void stop();
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;
std::filesystem::path m_path;
bool contains(const std::filesystem::path& key);
bool contains(const std::filesystem::path &key);
void initPathsMap();
signals:
void update(const std::filesystem::path path, const FileEvent);
void newPathEntered(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths);
void fileDeleted(const std::filesystem::path path, const FileEvent);
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 <iostream>
GridItemView::GridItemView(QWidget* parent)
: QFrame(parent),
fileController(std::make_shared<FileController>( )) {
GridItemView::GridItemView(QWidget *parent)
: QFrame(parent), 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) {
std::cout << "path.first" << std::endl;
for (auto path : paths) {
mainLayout->addWidget(new GridItem(path.first, this));
}
});
connect(fileController.get(), &FileController::pathChanged, this,
[this,
mainLayout](const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths) {
std::cout << "path.first" << std::endl;
for (auto path : paths) {
mainLayout->addWidget(new GridItem(path.first, this));
}
});
}
GridItemView::~GridItemView( ) { }
GridItemView::~GridItemView() {}