some stuff and added a funny randomy segfault I cant trace because funny

This commit is contained in:
2024-06-26 17:18:10 +02:00
parent 5c9a49e634
commit a3cabf4e82
66 changed files with 137 additions and 77042 deletions

View File

@@ -12,7 +12,7 @@ FileController::FileController( ) {
std::chrono::milliseconds(1000));
#else
m_fmWorker = std::make_shared<FileMonitor>(
std::filesystem::path("/home/crylia/Dokumente"),
std::filesystem::path(std::getenv("HOME")),
std::chrono::milliseconds(1000));
#endif
@@ -20,11 +20,17 @@ FileController::FileController( ) {
connect(this, &FileController::operate, m_fmWorker.get( ),
&FileMonitor::start);
connect(m_fmWorker.get( ), &FileMonitor::fileHappened, this,
connect(m_fmWorker.get( ), &FileMonitor::changed, this,
&FileController::update);
connect(m_fmWorker.get( ), &FileMonitor::pathChanged, this,
&FileController::newPath);
connect(this, &FileController::updatePath, m_fmWorker.get( ), &FileMonitor::SetPath);
//Why this no workie
//connect(this, &FileController::updatePath, m_fmWorker.get( ), &FileMonitor::SetPath);
connect(this, &FileController::updatePath, this, [this](std::filesystem::path p) {
m_fmWorker.get( )->SetPath(p);
});
m_fsThread.start( );
@@ -36,11 +42,8 @@ FileController::~FileController( ) {
m_fsThread.wait( );
}
void FileController::newPath(
std::unordered_map<std::filesystem::path, std::filesystem::file_time_type>
path) {
std::cout << "bruh" << std::endl;
emit pathChanged(path);
void FileController::newPath(const std::filesystem::path p) {
emit pathChanged(p);
}
void FileController::update(const std::filesystem::path path,

View File

@@ -2,26 +2,35 @@
#include <QThread>
#include <memory>
#include <unordered_map>
#include <filesystem>
#include "../../Core/FileMonitor/FileMonitor.hpp"
class FileController : public QObject {
Q_OBJECT
public:
// Static method to access the singleton instance
static FileController* instance( ) {
static FileController instance;
return &instance;
}
// Deleting the copy constructor and assignment operator to prevent copying
FileController(const FileController&) = delete;
FileController& operator=(const FileController&) = delete;
private:
// Private constructor to prevent instantiation
FileController( );
~FileController( );
private:
QThread m_fsThread;
std::shared_ptr<FileMonitor> m_fmWorker;
private slots:
void update(const std::filesystem::path path, const FileEvent);
void newPath(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
void update(const std::filesystem::path path, const FileEvent event);
void newPath(const std::filesystem::path p);
signals:
// Start signal to start the function in the thread
@@ -29,11 +38,8 @@ signals:
// Stop signal to stop the function in the thread
void pause( );
// Signal to update the path inside the thread
void updatePath(const std::filesystem::path&);
void updatePath(const std::filesystem::path& p);
void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
void contentChanged(std::filesystem::path, FileEvent event);
void pathChanged(const std::filesystem::path p);
void contentChanged(std::filesystem::path path, FileEvent event);
};

View File

@@ -9,15 +9,29 @@ FileMonitor::FileMonitor(std::filesystem::path path,
void FileMonitor::SetPath(std::filesystem::path newPath) {
m_path = newPath;
std::cout << m_path.string( ) << std::endl;
emit pathChanged(m_path);
initPathsMap( );
emit pathChanged(m_paths);
}
void FileMonitor::initPathsMap( ) {
m_paths.clear( );
for (auto& file : std::filesystem::directory_iterator(m_path)) {
/* for (auto& file : std::filesystem::directory_iterator(m_path)) {
m_paths[file.path( )] = std::filesystem::last_write_time(file);
} */
}
bool FileMonitor::is_hidden(const std::filesystem::path& p) {
#ifdef _WIN32
DWORD attrs = GetFileAttributes(p.c_str( ));
if (attrs == INVALID_FILE_ATTRIBUTES) {
throw std::runtime_error("Error getting file attributes");
}
return (attrs & FILE_ATTRIBUTE_HIDDEN);
#elif __unix__
return p.filename( ).string( ).front( ) == '.';
#endif
return false;
}
void FileMonitor::start( ) {
@@ -27,25 +41,37 @@ void FileMonitor::start( ) {
auto pbegin = m_paths.begin( );
while (pbegin != m_paths.end( )) {
if (!std::filesystem::exists(pbegin->first)) {
emit fileHappened(pbegin->first, FileEvent::erased);
emit changed(pbegin->first, FileEvent::erased);
pbegin = m_paths.erase(pbegin);
} else
pbegin++;
}
try {
for (auto& file : std::filesystem::directory_iterator(m_path)) {
std::filesystem::file_time_type curr_file_last_write;
if (!file.is_symlink( )) {
curr_file_last_write = std::filesystem::last_write_time(file);
} else {
curr_file_last_write = std::filesystem::file_time_type(std::chrono::nanoseconds(0));
}
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 fileHappened(file.path( ), FileEvent::created);
} else {
if (m_paths[file.path( )] != curr_file_last_write) {
m_paths[file.path( )] = curr_file_last_write;
emit fileHappened(file.path( ), FileEvent::modified);
if (!is_hidden(file)) {
if (!contains(file.path( ))) {
m_paths[file.path( )] = curr_file_last_write;
emit changed(file.path( ), FileEvent::created);
} else {
if (m_paths[file.path( )] != curr_file_last_write) {
m_paths[file.path( )] = curr_file_last_write;
emit changed(file.path( ), FileEvent::modified);
}
}
}
}
}
catch (std::filesystem::filesystem_error err) {
std::cout << err.what( ) << std::endl;
}
}
}

View File

@@ -5,6 +5,12 @@
#include <filesystem>
#include <unordered_map>
#ifdef _WIN32
#include <windows.h>
#elif __unix__
#include <sys/stat.h>
#endif
enum class FileEvent { created, modified, erased };
class FileMonitor : public QObject {
@@ -32,9 +38,9 @@ private:
void initPathsMap( );
bool is_hidden(const std::filesystem::path& p);
signals:
void fileHappened(const std::filesystem::path path, const FileEvent);
void pathChanged(const std::unordered_map<std::filesystem::path,
std::filesystem::file_time_type>
paths);
void changed(const std::filesystem::path path, const FileEvent);
void pathChanged(const std::filesystem::path p);
};

View File

@@ -2,35 +2,30 @@
#include <iostream>
GridItemView::GridItemView(QWidget* parent)
: QFrame(parent), fileController(std::make_shared<FileController>( )) {
: QFrame(parent), fileController(FileController::instance( )) {
QGridLayout* mainLayout = new QGridLayout( );
this->setLayout(mainLayout);
connect(fileController.get( ), &FileController::pathChanged, this,
[this, mainLayout](const std::unordered_map<std::filesystem::path, std::filesystem::file_time_type> paths) {
/* int r{ 0 }, c{ 0 };
for (auto path : paths) {
auto w = new GridItem(path.first);
gridMap[path.first] = w;
mainLayout->addWidget(w, r, c);
w->show( );
++r;
if ((r % 8) == 0) {
++c;
r = 0;
connect(fileController, &FileController::pathChanged, this,
[this, mainLayout](const std::filesystem::path path) {
while (mainLayout->count( )) {
QWidget* widget = mainLayout->itemAt(0)->widget( );
if (widget) {
mainLayout->removeWidget(widget);
delete widget;
}
} */
}
});
fileController.get( )->updatePath("/home/crylia/Dokumente");
connect(fileController.get( ), &FileController::contentChanged, this, [this, mainLayout](std::filesystem::path path, FileEvent event) {
connect(fileController, &FileController::contentChanged, this, [this, mainLayout](std::filesystem::path path, FileEvent event) {
if (event == FileEvent::created) {
auto w = new GridItem(path);
gridMap[path] = w;
connect(w, &GridItem::clicked, this, [this, path]( ) {
emit fileController->updatePath(path);
});
int pos = gridMap.size( ) - 1;
mainLayout->addWidget(w, pos / 8, pos % 8);
w->show( );
@@ -39,7 +34,6 @@ GridItemView::GridItemView(QWidget* parent)
delete gridMap[path];
gridMap.erase(path);
// Re-layout the remaining widgets
int pos = 0;
for (const auto& [key, widget] : gridMap) {
mainLayout->addWidget(widget, pos / 8, pos % 8);
@@ -47,7 +41,6 @@ GridItemView::GridItemView(QWidget* parent)
}
} else if (event == FileEvent::modified) {
std::cout << "modified" << std::endl;
// Do nothing ig? In theory nothing has to update unless we show the time
}
});

View File

@@ -14,7 +14,7 @@ private:
QList<GridItem> m_gridItemList;
std::shared_ptr<FileController> fileController;
FileController* fileController;
//Grid map to keep track of the widgets, and delete them easily as QT doesnt
//provide an easy way to find children by a property

View File

@@ -14,6 +14,4 @@ class MainWidget : public QWidget {
public:
MainWidget(QWidget* parent = nullptr);
~MainWidget( );
private:
};

View File

@@ -3,12 +3,31 @@
Path::Path(QWidget* parent)
:QWidget(parent),
m_fileController(FileController::instance( )),
#ifdef _WIN32
pathLabel_m(new QLabel(std::getenv("USERPROFILE"))) {
m_pathLabel(new QLabel(std::getenv("USERPROFILE"))) {
#else
pathLabel_m(new QLabel(std::getenv("HOME"))) {
m_pathLabel(new QLabel(std::getenv("HOME"))) {
#endif
auto layout = new QHBoxLayout(this);
m_pathLabel->setStyleSheet(R"(
background-color: #313131;
border: 2px solid #414141;
border-radius: 6px;
color: #D8D8D8;
font-size: 20px;
)");
setFixedHeight(40);
connect(m_fileController, &FileController::pathChanged, this,
[this](const std::filesystem::path path) {
setPath(QString::fromStdString(path.string( )));
});
layout->addWidget(m_pathLabel);
}
Path::~Path( ) { }

View File

@@ -2,22 +2,28 @@
#include <QWidget>
#include <QLabel>
#include <QHBoxLayout>
#include "../../Controller/FileController/FileController.hpp"
class Path : public QWidget {
Q_OBJECT
private:
QLabel* pathLabel_m;
QString path_m;
QLabel* m_pathLabel;
QString m_path;
FileController* m_fileController;
public:
Path(QWidget* parent = nullptr);
~Path( );
QString path( ) { return path_m; }
QString path( ) { return m_path; }
void setPath(QString path) {
if (path == this->path_m) { return; }
if (path == this->m_path) { return; }
path_m = path;
pathLabel_m->setText(path_m);
m_path = path;
m_pathLabel->setText(m_path);
}
};

View File

@@ -78,3 +78,10 @@ void GridItem::paintEvent(QPaintEvent* event) {
style( )->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
QWidget::paintEvent(event);
}
void GridItem::mousePressEvent(QMouseEvent* event) {
if (event->button( ) == Qt::LeftButton) {
emit clicked( );
}
QWidget::mousePressEvent(event);
}

View File

@@ -8,6 +8,7 @@
#include <QMimeDatabase>
#include <QApplication>
#include <filesystem>
#include <QMouseEvent>
class GridItem : public QWidget {
Q_OBJECT
@@ -35,4 +36,8 @@ public:
protected:
void paintEvent(QPaintEvent* event)override;
void mousePressEvent(QMouseEvent* event) override;
signals:
void clicked( );
};