put the filemonitor into a seperate thread and connected an update signal
This commit is contained in:
@@ -1,16 +1,26 @@
|
||||
#include "FileController.hpp"
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
|
||||
FileController::FileController() {
|
||||
m_fmWorker = std::make_unique<FileMonitor>(QString("/home/crylia/"),
|
||||
FileController::FileController( ) {
|
||||
m_fmWorker = std::make_shared<FileMonitor>(QString("/home/crylia/Dokumente"),
|
||||
std::chrono::milliseconds(1000));
|
||||
m_fmWorker->moveToThread(&m_fsThread);
|
||||
|
||||
m_fmWorker->start(QString("/home/crylia/"));
|
||||
connect(this, &FileController::operate, m_fmWorker.get( ), &FileMonitor::start);
|
||||
connect(m_fmWorker.get( ), &FileMonitor::update, this, &FileController::update);
|
||||
|
||||
m_fsThread.start( );
|
||||
|
||||
emit operate( );
|
||||
}
|
||||
|
||||
FileController::~FileController() {
|
||||
m_fsThread.quit();
|
||||
m_fsThread.wait();
|
||||
FileController::~FileController( ) {
|
||||
m_fsThread.quit( );
|
||||
m_fsThread.wait( );
|
||||
}
|
||||
|
||||
void FileController::update(const QString path, const FileEvent event) {
|
||||
std::cout << path.toStdString( ) << std::endl;
|
||||
}
|
||||
|
||||
@@ -8,11 +8,23 @@
|
||||
class FileController : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
FileController();
|
||||
~FileController();
|
||||
FileController( );
|
||||
~FileController( );
|
||||
|
||||
private:
|
||||
QThread m_fsThread;
|
||||
|
||||
std::unique_ptr<FileMonitor> m_fmWorker;
|
||||
std::shared_ptr<FileMonitor> m_fmWorker;
|
||||
|
||||
private slots:
|
||||
|
||||
void update(const QString path, const FileEvent);
|
||||
|
||||
signals:
|
||||
// Start signal to start the function in the thread
|
||||
void operate( );
|
||||
// Stop signal to stop the function in the thread
|
||||
void pause( );
|
||||
// Signal to update the path inside the thread
|
||||
void updatePath(const QString&);
|
||||
};
|
||||
|
||||
@@ -6,41 +6,41 @@ FileMonitor::FileMonitor(QString path,
|
||||
std::chrono::duration<int, std::milli> delay)
|
||||
: m_path(path), m_delay(delay) {
|
||||
|
||||
for (auto &file :
|
||||
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||
m_paths[QString::fromStdString(file.path().string())] =
|
||||
for (auto& file :
|
||||
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||
std::filesystem::last_write_time(file);
|
||||
}
|
||||
}
|
||||
|
||||
void FileMonitor::start(const QString path) {
|
||||
void FileMonitor::start( ) {
|
||||
while (m_running) {
|
||||
std::this_thread::sleep_for(m_delay);
|
||||
|
||||
auto pbegin = m_paths.begin();
|
||||
while (pbegin != m_paths.end()) {
|
||||
if (!std::filesystem::exists(pbegin->first.toStdString())) {
|
||||
auto pbegin = m_paths.begin( );
|
||||
while (pbegin != m_paths.end( )) {
|
||||
if (!std::filesystem::exists(pbegin->first.toStdString( ))) {
|
||||
emit update(pbegin->first, FileEvent::erased);
|
||||
pbegin = m_paths.erase(pbegin);
|
||||
} else
|
||||
pbegin++;
|
||||
}
|
||||
|
||||
for (auto &file :
|
||||
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
||||
for (auto& file :
|
||||
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||
auto curr_file_last_write = std::filesystem::last_write_time(file);
|
||||
|
||||
if (!contains(QString::fromStdString(file.path().string()))) {
|
||||
m_paths[QString::fromStdString(file.path().string())] =
|
||||
if (!contains(QString::fromStdString(file.path( ).string( )))) {
|
||||
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||
curr_file_last_write;
|
||||
emit update(QString::fromStdString(file.path().string()),
|
||||
emit update(QString::fromStdString(file.path( ).string( )),
|
||||
FileEvent::created);
|
||||
} else {
|
||||
if (m_paths[QString::fromStdString(file.path().string())] !=
|
||||
if (m_paths[QString::fromStdString(file.path( ).string( ))] !=
|
||||
curr_file_last_write) {
|
||||
m_paths[QString::fromStdString(file.path().string())] =
|
||||
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||
curr_file_last_write;
|
||||
emit update(QString::fromStdString(file.path().string()),
|
||||
emit update(QString::fromStdString(file.path( ).string( )),
|
||||
FileEvent::modified);
|
||||
}
|
||||
}
|
||||
@@ -48,8 +48,8 @@ void FileMonitor::start(const QString path) {
|
||||
}
|
||||
}
|
||||
|
||||
void FileMonitor::stop() { m_running = false; }
|
||||
void FileMonitor::stop( ) { m_running = false; }
|
||||
|
||||
bool FileMonitor::contains(const QString &key) {
|
||||
return m_paths.find(key) != m_paths.end();
|
||||
bool FileMonitor::contains(const QString& key) {
|
||||
return m_paths.find(key) != m_paths.end( );
|
||||
}
|
||||
|
||||
@@ -14,20 +14,20 @@ public:
|
||||
std::chrono::duration<int, std::milli> m_delay;
|
||||
|
||||
FileMonitor(QString path, std::chrono::duration<int, std::milli> delay);
|
||||
~FileMonitor() = default;
|
||||
~FileMonitor( ) = default;
|
||||
|
||||
void SetPath(QString path) { m_path = path; }
|
||||
|
||||
public slots:
|
||||
void start(const QString path);
|
||||
void stop();
|
||||
void start( );
|
||||
void stop( );
|
||||
|
||||
private:
|
||||
std::unordered_map<QString, std::filesystem::file_time_type> m_paths;
|
||||
bool m_running;
|
||||
QString m_path;
|
||||
|
||||
bool contains(const QString &key);
|
||||
bool contains(const QString& key);
|
||||
|
||||
signals:
|
||||
void update(const QString path, const FileEvent);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "MainWidget.hpp"
|
||||
|
||||
MainWidget::MainWidget(QWidget* parent) {
|
||||
|
||||
auto path_mainContentLayout = std::make_shared<QHBoxLayout>(this);
|
||||
auto fileTree_fileGridLayout = std::make_shared<QVBoxLayout>(this);
|
||||
|
||||
path_mainContentLayout->addLayout(fileTree_fileGridLayout.get( ));
|
||||
|
||||
setLayout(path_mainContentLayout.get( ));
|
||||
|
||||
auto fmc = new FileController( );
|
||||
}
|
||||
|
||||
MainWidget::~MainWidget( ) { }
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <QLayout>
|
||||
#include <QLabel>
|
||||
#include <QWidget>
|
||||
#include <qboxlayout.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
#include "../../Controller/FileController/FileController.hpp"
|
||||
|
||||
class MainWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
MainWidget() = default;
|
||||
~MainWidget() = default;
|
||||
MainWidget(QWidget* parent = nullptr);
|
||||
~MainWidget( );
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
#include "MainWindow.hpp"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {}
|
||||
MainWindow::MainWindow(QWidget* parent)
|
||||
: QMainWindow(parent), m_mainWidget(std::make_shared<MainWidget>( )) {
|
||||
setWindowTitle("QutieFM");
|
||||
|
||||
MainWindow::~MainWindow() {}
|
||||
setObjectName("MainWindow");
|
||||
|
||||
this->setCentralWidget(m_mainWidget.get( ));
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow( ) { }
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
#include "../MainWidget/MainWidget.hpp"
|
||||
|
||||
class MainWindow : public QMainWindow {
|
||||
Q_OBJECT
|
||||
public:
|
||||
MainWindow(QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
MainWindow(QWidget* parent = nullptr);
|
||||
~MainWindow( );
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<MainWidget> m_mainWidget;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user