diff --git a/src/Controller/FileController/FileController.cpp b/src/Controller/FileController/FileController.cpp index 53a1e64..2ea8f51 100644 --- a/src/Controller/FileController/FileController.cpp +++ b/src/Controller/FileController/FileController.cpp @@ -1,38 +1,49 @@ #include "FileController.hpp" #include -#include +#include #include +#include +#include -FileController::FileController( ) { +FileController::FileController() { #ifdef _WIN32 - m_fmWorker = std::make_shared(std::filesystem::path(std::getenv("USERPROFILE")), - std::chrono::milliseconds(1000)); + m_fmWorker = std::make_shared( + std::filesystem::path(std::getenv("USERPROFILE")), + std::chrono::milliseconds(1000)); #else - m_fmWorker = std::make_shared(std::filesystem::path("/home/crylia/Dokumente"), - std::chrono::milliseconds(1000)); + m_fmWorker = std::make_shared( + 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 paths) { +void FileController::newPath( + std::unordered_map + 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; } diff --git a/src/Controller/FileController/FileController.hpp b/src/Controller/FileController/FileController.hpp index 7314774..b92d877 100644 --- a/src/Controller/FileController/FileController.hpp +++ b/src/Controller/FileController/FileController.hpp @@ -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 paths); + void newPath(const std::unordered_map + 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 paths); + void pathChanged(const std::unordered_map + paths); }; diff --git a/src/Core/FileMonitor/FileMonitor.cpp b/src/Core/FileMonitor/FileMonitor.cpp index 0d35c99..f499b23 100644 --- a/src/Core/FileMonitor/FileMonitor.cpp +++ b/src/Core/FileMonitor/FileMonitor.cpp @@ -1,56 +1,60 @@ #include "FileMonitor.hpp" #include -#include #include +#include FileMonitor::FileMonitor(std::filesystem::path path, - std::chrono::duration delay) - : m_path(path), m_delay(delay) { + std::chrono::duration 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(); } diff --git a/src/Core/FileMonitor/FileMonitor.hpp b/src/Core/FileMonitor/FileMonitor.hpp index 0ff8f8b..570946c 100644 --- a/src/Core/FileMonitor/FileMonitor.hpp +++ b/src/Core/FileMonitor/FileMonitor.hpp @@ -12,23 +12,31 @@ class FileMonitor : public QObject { public: std::chrono::duration m_delay; - FileMonitor(std::filesystem::path path, std::chrono::duration delay); - ~FileMonitor( ) = default; + FileMonitor(std::filesystem::path path, + std::chrono::duration 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 m_paths; + std::unordered_map + 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 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 + paths); }; diff --git a/src/View/GridItemView/GridItemView.cpp b/src/View/GridItemView/GridItemView.cpp index 114d89b..1724777 100644 --- a/src/View/GridItemView/GridItemView.cpp +++ b/src/View/GridItemView/GridItemView.cpp @@ -1,20 +1,22 @@ #include "GridItemView.hpp" #include -GridItemView::GridItemView(QWidget* parent) - : QFrame(parent), - fileController(std::make_shared( )) { +GridItemView::GridItemView(QWidget *parent) + : QFrame(parent), fileController(std::make_shared()) { - QGridLayout* mainLayout = new QGridLayout( ); + QGridLayout *mainLayout = new QGridLayout(); - connect(fileController.get( ), &FileController::newPathEntered, this, [this, mainLayout](const std::unordered_map 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 + paths) { + std::cout << "path.first" << std::endl; + for (auto path : paths) { + mainLayout->addWidget(new GridItem(path.first, this)); + } + }); } -GridItemView::~GridItemView( ) { } +GridItemView::~GridItemView() {}