From d782e70569d13103807dfa156741045caf9dcb94 Mon Sep 17 00:00:00 2001 From: hama Date: Sun, 12 Apr 2015 13:03:53 +0200 Subject: [PATCH] refind: filetype filter, dir exclusion filter --- appl/refind/filefinder.cpp | 50 +++++++++++++++++++++++------ appl/refind/filefinder.hpp | 4 +++ appl/refind/main.cpp | 5 +-- appl/refind/mainwindow.cpp | 54 +++++++++++++++++++++++++++++-- appl/refind/mainwindow.hpp | 3 ++ appl/refind/mainwindow.ui | 65 +++++++++++++++++++++++++++++--------- 6 files changed, 153 insertions(+), 28 deletions(-) diff --git a/appl/refind/filefinder.cpp b/appl/refind/filefinder.cpp index 91b609e..1b89746 100644 --- a/appl/refind/filefinder.cpp +++ b/appl/refind/filefinder.cpp @@ -6,6 +6,7 @@ * The original sources can be found on https://github.com/republib. */ +#include #include "mainwindow.hpp" #include "filefinder.hpp" @@ -21,9 +22,11 @@ FileFinder::FileFinder() : m_maxDepth(512), m_baseDir(""), m_checkDates(false), - m_countDirs(0), m_countFiles(0), - m_bytes(0){ + m_countDirs(0), + m_bytes(0), + m_excludedDirs() +{ m_youngerThan.setMSecsSinceEpoch(0); m_olderThan.setMSecsSinceEpoch(0); } @@ -110,11 +113,11 @@ QString typeOf(QFileInfo& info){ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ QDir dir(path); QFileInfoList entries; - if (m_patterns.count() == 0) - entries = dir.entryInfoList(m_fileTypes | QDir::NoDotAndDotDot, - QDir::NoSort); - else - entries = dir.entryInfoList(m_patterns, m_fileTypes, QDir::NoSort); + if (m_patterns.count() > 0) + dir.setNameFilters(m_patterns); + QDir::Filters filters = m_fileTypes | QDir::NoDotAndDotDot; + dir.setFilter(filters); + dir.setFilter(m_fileTypes); QList ::iterator it; QString relativePath = path.mid(1 + m_baseDir.length()); QString node, ext; @@ -152,14 +155,34 @@ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ if (depth < m_maxDepth){ entries = dir.entryInfoList( QDir::NoSymLinks | QDir::NoDotAndDotDot | QDir::AllDirs, QDir::NoSort); + bool filtered = m_excludedDirs.length() > 0; for (it = entries.begin(); it != entries.end(); ++it){ QString node = it->fileName(); - fillTable(path + QDir::separator() + node, depth + 1, table); + if (! filtered || !isExcludedDir(node)) + fillTable(path + QDir::separator() + node, depth + 1, table); } } table->setRowCount(m_lines); } +/** + * Checks whether a filename matches the list of excluded directories. + * + * @param node filename to check + * @return true: the node is part of the excluded dirs + */ +bool FileFinder::isExcludedDir(const QString& node){ + bool rc = false; + QList ::iterator it; + for (it = m_excludedDirs.begin(); it != m_excludedDirs.end(); ++it){ + if (QString::compare(node, *it, Qt::CaseInsensitive) == 0){ + rc = true; + break; + } + } + return rc; +} + /** * Tests whether a file matches the filter conditions. * @@ -187,7 +210,16 @@ bool FileFinder::isValid(const QFileInfo& file){ * @param baseDir the directory where the search starts */ void FileFinder::setBaseDir(const QString& baseDir){ - m_baseDir = baseDir; + m_baseDir = baseDir; +} + +/** + * Sets the list of excluded directories. + * @param excludedDirs each entry of this list will not be entered for search + */ +void FileFinder::setExcludedDirs(const QStringList& excludedDirs) +{ + m_excludedDirs = excludedDirs; } /** diff --git a/appl/refind/filefinder.hpp b/appl/refind/filefinder.hpp index 20a558e..98d1410 100644 --- a/appl/refind/filefinder.hpp +++ b/appl/refind/filefinder.hpp @@ -26,12 +26,15 @@ public: void setFiletypes(const QDir::Filters& filetypes); void setMaxDepth(int maxDepth); void setMinDepth(int minDepth); + void setExcludedDirs(const QStringList& excludedDirs); void setOlderThan(const QDateTime& olderThan); void setPatterns(const QStringList& patterns); void setMaxSize(const int64_t& maxSize); void setMinSize(const int64_t& minSize); void setYoungerThan(const QDateTime& youngerThan); + private: + bool isExcludedDir(const QString& node); bool isValid(const QFileInfo& file); private: int m_lines; @@ -48,6 +51,7 @@ private: int m_countFiles; int m_countDirs; int64_t m_bytes; + QStringList m_excludedDirs; }; #endif // FILEFINDER_HPP diff --git a/appl/refind/main.cpp b/appl/refind/main.cpp index 1feb89c..ef96fdf 100644 --- a/appl/refind/main.cpp +++ b/appl/refind/main.cpp @@ -1,6 +1,6 @@ /* * main.cpp - * + * * License: Public Domain * You can use and modify this file without any restriction. * Do what you want. @@ -9,8 +9,9 @@ * The latest sources: https://github.com/republib */ -#include "mainwindow.hpp" #include +#include +#include "mainwindow.hpp" int main(int argc, char *argv[]){ QApplication a(argc, argv); diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index d6ebe3c..941cfb1 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -10,6 +10,7 @@ */ #include +#include #include "base/rebase.hpp" #include "mainwindow.hpp" #include "ui_mainwindow.h" @@ -33,6 +34,7 @@ MainWindow::MainWindow(QWidget *parent) : connect(ui->pushButtonSearch, SIGNAL(clicked()), this, SLOT(search())); connect(ui->pushButtonSearch2, SIGNAL(clicked()), this, SLOT(search())); connect(ui->pushButtonUp, SIGNAL(clicked()), this, SLOT(up())); + connect(ui->pushButtonDirectory, SIGNAL(clicked()), this, SLOT(selectDirectory())); ui->tableWidget->setColumnWidth(TC_NODE, 200); ui->tableWidget->setColumnWidth(TC_EXT, 40); ui->tableWidget->setColumnWidth(TC_SIZE, 125); @@ -111,7 +113,7 @@ int64_t MainWindow::comboSize(QComboBox* combo){ rc = parser.asInt64(-1); if (rc >= 0){ setInHistory(combo, value); - combo->setCurrentText(QString("").sprintf("%lld", rc)); + combo->setCurrentText(QString("").sprintf("%ld", rc)); }else guiError(combo, parser.errorMessage()); } @@ -136,6 +138,35 @@ QString MainWindow::comboText(QComboBox* combo){ return rc; } +/** + * Converts the checkbox states to a filetype mask. + * @return the filetypes selected by the checkboxes + */ +QDir::Filters MainWindow::buildFileTypes(){ + QDir::Filters rc = 0; + if (ui->checkBoxDirs->isChecked()) + rc |= QDir::Dirs; + if (ui->checkBoxFiles->isChecked()) + rc |= QDir::Files; + if (rc == 0) + rc |= QDir::Dirs | QDir::Files; + if (! ui->checkBoxLinks->isChecked()) + rc |= QDir::NoSymLinks; + if (ui->checkBoxHidden) + rc |= QDir::Hidden | QDir::System; + QDir::Filters mask = 0; + if (ui->checkBoxWritable->isChecked()) + mask |= QDir::Writable; + if (ui->checkBoxReadable->isChecked()) + mask |= QDir::Readable; + if (ui->checkBoxExecutable->isChecked()) + mask |= QDir::Executable; + if (mask == 0) + mask |= QDir::PermissionMask; + rc |= mask; + return rc; +} + /** * Handles the "search" button. */ @@ -150,11 +181,20 @@ void MainWindow::search(){ finder.setYoungerThan(comboDate(ui->comboBoxYounger)); finder.setMinDepth(comboInt(ui->comboBoxMinDepth, 0)); finder.setMaxDepth(comboInt(ui->comboBoxMaxDepth, -1)); + finder.setFiletypes(buildFileTypes()); QStringList patterns; QString value = ui->comboBoxFilePatterns->currentText(); if (!value.isEmpty()) - patterns = value.split(";"); + patterns = value.split(","); finder.setPatterns(patterns); + value = ui->comboBoxExcludedDirs->currentText(); + if (value.indexOf('/')>= 0 || value.indexOf('\\') >= 0) + guiError(ui->comboBoxExcludedDirs, tr("no path delimiter allowed")); + else if (value.indexOf('*')>= 0) + guiError(ui->comboBoxExcludedDirs, tr("no patterns allowed. Do not use '*")); + else if (!value.isEmpty()) + patterns = value.split(","); + finder.setExcludedDirs(patterns); if (m_errors == 0){ clock_t start = clock(); finder.fillTable(path, 0, ui->tableWidget); @@ -167,6 +207,16 @@ void MainWindow::search(){ setStatusMessage(false, msg); } } +/** + * Handles the push of the button "select directory". + */ +void MainWindow::selectDirectory(){ + QString dir = QFileDialog::getExistingDirectory(this, + tr("Select Directory"), ui->comboBoxDirectory->currentText(), + QFileDialog::ShowDirsOnly| QFileDialog::DontResolveSymlinks); + if (! dir.isEmpty()) + ui->comboBoxDirectory->setCurrentText(dir); +} /** * @brief Sets a text in a combobox uses as history. diff --git a/appl/refind/mainwindow.hpp b/appl/refind/mainwindow.hpp index 99def87..f742f69 100644 --- a/appl/refind/mainwindow.hpp +++ b/appl/refind/mainwindow.hpp @@ -34,7 +34,10 @@ public: private slots: void search(); void up(); + void selectDirectory(); + private: + QDir::Filters buildFileTypes(); QDateTime comboDate(QComboBox* combo); int comboInt(QComboBox* combo, int defaultValue); int64_t comboSize(QComboBox* combo); diff --git a/appl/refind/mainwindow.ui b/appl/refind/mainwindow.ui index 0727c6b..7ef477f 100644 --- a/appl/refind/mainwindow.ui +++ b/appl/refind/mainwindow.ui @@ -32,7 +32,7 @@ - 1 + 0 @@ -57,7 +57,7 @@ - + 75 @@ -79,7 +79,7 @@ - + 75 @@ -101,7 +101,7 @@ - + 75 @@ -120,7 +120,7 @@ - + 75 @@ -134,7 +134,30 @@ - Specials + Write + + + true + + + + + + + Read + + + true + + + + + + + Exec. + + + true @@ -197,6 +220,9 @@ + + <html><head/><body><p>a comma (',') separated list of filename patterns</p><p>Example: *.txt,*.odt</p></body></html> + true @@ -208,7 +234,7 @@ - 157 + 130 0 @@ -283,8 +309,11 @@ 16777215 + + change to parent directory + - Up + ⇑ @@ -302,6 +331,9 @@ 16777215 + + select directory by a dialog box + ... @@ -330,7 +362,7 @@ - + Files @@ -347,7 +379,7 @@ Size, Date, Depth, Excluded Dirs - + 10 @@ -522,6 +554,9 @@ + + <html><head/><body><p>a comma (',') separated list of directory names not entered for the search</p><p>Example: .git,.cache</p></body></html> + true @@ -675,11 +710,11 @@ pushButtonUp pushButtonDirectory comboBoxFilePatterns - checkBox_2 - checkBox - checkBox_4 - checkBox_3 - checkBox_5 + checkBoxDirs + checkBoxFiles + checkBoxLinks + checkBoxHidden + checkBoxWritable comboBoxTextPattern checkBoxTextIgnoreCase checkBoxRegExpr -- 2.39.5