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,22 +1,29 @@
#include "FileController.hpp"
#include <chrono>
#include <memory>
#include <filesystem>
#include <iostream>
#include <memory>
#include <unordered_map>
FileController::FileController() {
#ifdef _WIN32
m_fmWorker = std::make_shared<FileMonitor>(std::filesystem::path(std::getenv("USERPROFILE")),
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"),
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();
@@ -28,11 +35,15 @@ FileController::~FileController( ) {
m_fsThread.wait();
}
void FileController::update(const std::filesystem::path path, const FileEvent event) {
std::cout << path.string( ) << std::endl;
void FileController::newPath(
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
path) {
std::cout << "test" << std::endl;
emit pathChanged(path);
}
void FileController::emitNewPath(const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) {
std::cout << "test" << std::endl;
emit newPathEntered(paths);
void FileController::update(const std::filesystem::path path,
const FileEvent event) {
std::cout << "bruh" << std::endl;
std::cout << path.string() << std::endl;
}

View File

@@ -19,7 +19,9 @@ 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
@@ -29,5 +31,7 @@ signals:
// Signal to update the path inside the thread
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,19 +1,27 @@
#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) {
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;
initPathsMap();
emit pathChanged(m_paths);
}
emit newPathEntered(m_paths);
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() {
@@ -23,26 +31,22 @@ void FileMonitor::start( ) {
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);
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);
}
}
}

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(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();
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);
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

@@ -2,19 +2,21 @@
#include <iostream>
GridItemView::GridItemView(QWidget *parent)
: QFrame(parent),
fileController(std::make_shared<FileController>( )) {
: QFrame(parent), fileController(std::make_shared<FileController>()) {
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,
[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() {}