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 "FileController.hpp"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
FileController::FileController() {
|
FileController::FileController( ) {
|
||||||
m_fmWorker = std::make_unique<FileMonitor>(QString("/home/crylia/"),
|
m_fmWorker = std::make_shared<FileMonitor>(QString("/home/crylia/Dokumente"),
|
||||||
std::chrono::milliseconds(1000));
|
std::chrono::milliseconds(1000));
|
||||||
m_fmWorker->moveToThread(&m_fsThread);
|
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() {
|
FileController::~FileController( ) {
|
||||||
m_fsThread.quit();
|
m_fsThread.quit( );
|
||||||
m_fsThread.wait();
|
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 {
|
class FileController : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
FileController();
|
FileController( );
|
||||||
~FileController();
|
~FileController( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QThread m_fsThread;
|
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&);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,53 +3,53 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
FileMonitor::FileMonitor(QString path,
|
FileMonitor::FileMonitor(QString 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 :
|
for (auto& file :
|
||||||
std::filesystem::recursive_directory_iterator(m_path.toStdString())) {
|
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||||
m_paths[QString::fromStdString(file.path().string())] =
|
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||||
std::filesystem::last_write_time(file);
|
std::filesystem::last_write_time(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileMonitor::start(const QString path) {
|
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.toStdString())) {
|
if (!std::filesystem::exists(pbegin->first.toStdString( ))) {
|
||||||
emit update(pbegin->first, FileEvent::erased);
|
emit update(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::recursive_directory_iterator(m_path.toStdString())) {
|
std::filesystem::directory_iterator(m_path.toStdString( ))) {
|
||||||
auto curr_file_last_write = std::filesystem::last_write_time(file);
|
auto curr_file_last_write = std::filesystem::last_write_time(file);
|
||||||
|
|
||||||
if (!contains(QString::fromStdString(file.path().string()))) {
|
if (!contains(QString::fromStdString(file.path( ).string( )))) {
|
||||||
m_paths[QString::fromStdString(file.path().string())] =
|
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||||
curr_file_last_write;
|
curr_file_last_write;
|
||||||
emit update(QString::fromStdString(file.path().string()),
|
emit update(QString::fromStdString(file.path( ).string( )),
|
||||||
FileEvent::created);
|
FileEvent::created);
|
||||||
} else {
|
} else {
|
||||||
if (m_paths[QString::fromStdString(file.path().string())] !=
|
if (m_paths[QString::fromStdString(file.path( ).string( ))] !=
|
||||||
curr_file_last_write) {
|
curr_file_last_write) {
|
||||||
m_paths[QString::fromStdString(file.path().string())] =
|
m_paths[QString::fromStdString(file.path( ).string( ))] =
|
||||||
curr_file_last_write;
|
curr_file_last_write;
|
||||||
emit update(QString::fromStdString(file.path().string()),
|
emit update(QString::fromStdString(file.path( ).string( )),
|
||||||
FileEvent::modified);
|
FileEvent::modified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileMonitor::stop() { m_running = false; }
|
void FileMonitor::stop( ) { m_running = false; }
|
||||||
|
|
||||||
bool FileMonitor::contains(const QString &key) {
|
bool FileMonitor::contains(const QString& key) {
|
||||||
return m_paths.find(key) != m_paths.end();
|
return m_paths.find(key) != m_paths.end( );
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,20 +14,20 @@ public:
|
|||||||
std::chrono::duration<int, std::milli> m_delay;
|
std::chrono::duration<int, std::milli> m_delay;
|
||||||
|
|
||||||
FileMonitor(QString path, std::chrono::duration<int, std::milli> delay);
|
FileMonitor(QString path, std::chrono::duration<int, std::milli> delay);
|
||||||
~FileMonitor() = default;
|
~FileMonitor( ) = default;
|
||||||
|
|
||||||
void SetPath(QString path) { m_path = path; }
|
void SetPath(QString path) { m_path = path; }
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void start(const QString path);
|
void start( );
|
||||||
void stop();
|
void stop( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<QString, std::filesystem::file_time_type> m_paths;
|
std::unordered_map<QString, std::filesystem::file_time_type> m_paths;
|
||||||
bool m_running;
|
bool m_running;
|
||||||
QString m_path;
|
QString m_path;
|
||||||
|
|
||||||
bool contains(const QString &key);
|
bool contains(const QString& key);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void update(const QString path, const FileEvent);
|
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
|
#pragma once
|
||||||
|
|
||||||
#include <QLayout>
|
#include <QLayout>
|
||||||
|
#include <QLabel>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <qboxlayout.h>
|
|
||||||
#include <qtmetamacros.h>
|
#include "../../Controller/FileController/FileController.hpp"
|
||||||
|
|
||||||
class MainWidget : public QWidget {
|
class MainWidget : public QWidget {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
MainWidget() = default;
|
MainWidget(QWidget* parent = nullptr);
|
||||||
~MainWidget() = default;
|
~MainWidget( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
#include "MainWindow.hpp"
|
#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 <QMainWindow>
|
||||||
|
|
||||||
|
#include "../MainWidget/MainWidget.hpp"
|
||||||
|
|
||||||
class MainWindow : public QMainWindow {
|
class MainWindow : public QMainWindow {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
MainWindow(QWidget *parent = nullptr);
|
MainWindow(QWidget* parent = nullptr);
|
||||||
~MainWindow();
|
~MainWindow( );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
std::shared_ptr<MainWidget> m_mainWidget;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user