From 4d5aed1831fa663d0f2604d80f62b190511d8e6b Mon Sep 17 00:00:00 2001 From: hama Date: Mon, 20 Apr 2015 00:05:02 +0200 Subject: [PATCH] TextFinder integrated --- appl/refind/about.ui | 74 +++++++++++++++++++++++++++++ appl/refind/aboutdialog.cpp | 19 ++++++++ appl/refind/aboutdialog.hpp | 29 ++++++++++++ appl/refind/aboutdialog.ui | 83 ++++++++++++++++++++++++++++++++ appl/refind/filefinder.cpp | 41 ++++++++++++---- appl/refind/filefinder.hpp | 11 +++-- appl/refind/mainwindow.cpp | 36 +++++++++++++- appl/refind/mainwindow.hpp | 7 ++- appl/refind/mainwindow.ui | 58 ++++++++++++++++------- appl/refind/refind.pro | 9 ++-- appl/refind/textfinder.cpp | 94 +++++++++++++++++++++++++++++-------- appl/refind/textfinder.hpp | 16 +++++-- 12 files changed, 421 insertions(+), 56 deletions(-) create mode 100644 appl/refind/about.ui create mode 100644 appl/refind/aboutdialog.cpp create mode 100644 appl/refind/aboutdialog.hpp create mode 100644 appl/refind/aboutdialog.ui diff --git a/appl/refind/about.ui b/appl/refind/about.ui new file mode 100644 index 0000000..a6d80c5 --- /dev/null +++ b/appl/refind/about.ui @@ -0,0 +1,74 @@ + + + AboutDialog + + + + 0 + 0 + 400 + 225 + + + + About + + + + + 20 + 20 + 351 + 141 + + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">ReFind</span> for searching files in a directory tree</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is a program of the project &quot;<span style=" font-weight:600;">Re</span>al <span style=" font-weight:600;">Pub</span>lic <span style=" font-weight:600;">Lib</span>rary&quot;.</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Sources are public domain and available under https://github.com/republib</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Implemented in QT (C++) 5.x</p></body></html> + + + + + + 280 + 180 + 93 + 27 + + + + &OK + + + + + &OK + + + + + + + action_OK + triggered() + AboutDialog + close() + + + -1 + -1 + + + 199 + 112 + + + + + diff --git a/appl/refind/aboutdialog.cpp b/appl/refind/aboutdialog.cpp new file mode 100644 index 0000000..43a6eb5 --- /dev/null +++ b/appl/refind/aboutdialog.cpp @@ -0,0 +1,19 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. + */ + +#include "aboutdialog.hpp" +#include "ui_aboutdialog.h" + +AboutDialog::AboutDialog(QWidget *parent) : + QDialog(parent), ui(new Ui::AboutDialog){ + ui->setupUi(this); +} + +AboutDialog::~AboutDialog(){ + delete ui; +} diff --git a/appl/refind/aboutdialog.hpp b/appl/refind/aboutdialog.hpp new file mode 100644 index 0000000..b15b499 --- /dev/null +++ b/appl/refind/aboutdialog.hpp @@ -0,0 +1,29 @@ +/* + * Licence: + * You can use and modify this file without any restriction. + * There is no warranty. + * You also can use the licence from http://www.wtfpl.net/. + * The original sources can be found on https://github.com/republib. + */ + +#ifndef ABOUTDIALOG_HPP +#define ABOUTDIALOG_HPP + +#include + +namespace Ui { +class AboutDialog; +} + +class AboutDialog: public QDialog { + Q_OBJECT + +public: + explicit AboutDialog(QWidget *parent = 0); + ~AboutDialog(); + +private: + Ui::AboutDialog *ui; +}; + +#endif // ABOUTDIALOG_HPP diff --git a/appl/refind/aboutdialog.ui b/appl/refind/aboutdialog.ui new file mode 100644 index 0000000..fd6f146 --- /dev/null +++ b/appl/refind/aboutdialog.ui @@ -0,0 +1,83 @@ + + + AboutDialog + + + + 0 + 0 + 400 + 218 + + + + Dialog + + + + + 20 + 20 + 351 + 141 + + + + true + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-weight:600;">ReFind</span> for searching files in a directory tree</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">This is a program of the project &quot;<span style=" font-weight:600;">Re</span>al <span style=" font-weight:600;">Pub</span>lic <span style=" font-weight:600;">Lib</span>rary&quot;.</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Sources are public domain and available under https://github.com/republib</p> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;">Implemented in QT (C++) 5.x</p></body></html> + + + + + + 150 + 170 + 93 + 27 + + + + &OK + + + + + &Ok + + + Close the dialog + + + Ctrl+X + + + + + + + actionOk + triggered() + AboutDialog + close() + + + -1 + -1 + + + 199 + 108 + + + + + diff --git a/appl/refind/filefinder.cpp b/appl/refind/filefinder.cpp index b934212..777a6a3 100644 --- a/appl/refind/filefinder.cpp +++ b/appl/refind/filefinder.cpp @@ -10,6 +10,7 @@ #include "base/rebase.hpp" #include "mainwindow.hpp" #include "filefinder.hpp" +#include "textfinder.hpp" /** * Constructor. @@ -29,10 +30,13 @@ FileFinder::FileFinder() : m_countFiles(0), m_countDirs(0), m_bytes(0), - m_excludedDirs(){ + m_excludedDirs(), + m_textFinder(NULL){ m_youngerThan.setMSecsSinceEpoch(0); m_olderThan.setMSecsSinceEpoch(0); } +FileFinder::~FileFinder(){ +} /** * Returns the sum of sizes of the found files. @@ -109,6 +113,15 @@ QString typeOf(QFileInfo& info){ return rc; } +/** + * Sets the text finder parameter template. + * + * @param textFinder the text finder instance containing the search parameter + */ +void FileFinder::setTextFinder(TextFinder* textFinder){ + m_textFinder = textFinder; +} + /** * Fills the table with the data of the filtered files of a given directory. * @@ -127,6 +140,7 @@ void FileFinder::fillTable(const QString& path, int depth, QTableWidget* table){ QList ::iterator it; QString relativePath = path.mid(1 + m_baseDir.length()); QString node, ext; + for (it = entries.begin(); it != entries.end(); ++it){ node = it->fileName(); if (node == "." || node == "..") @@ -207,6 +221,12 @@ bool FileFinder::isValid(const QFileInfo& file){ if (rc) rc = m_olderThan.toMSecsSinceEpoch() == 0 || date <= m_olderThan; } + if (rc && m_textFinder != NULL){ + QString full = file.absoluteFilePath(); + TextFinder textFinder(full, file.size()); + textFinder.getSearchParameter(*m_textFinder); + rc = textFinder.contains(); + } return rc; } @@ -245,15 +265,6 @@ void FileFinder::setFiletypes(const QDir::Filters& filetypes){ void FileFinder::setMaxDepth(int maxDepth){ m_maxDepth = maxDepth; } -/** - * Sets the minimum subdirectory depth. - * - * @param minDepth 0: search in the base directory too
- * otherwise: the minimum depth - */ -void FileFinder::setMinDepth(int minDepth){ - m_minDepth = minDepth; -} /** * Sets the maximum size. @@ -264,6 +275,16 @@ void FileFinder::setMaxSize(const int64_t& maxSize){ m_maxSize = maxSize; } +/** + * Sets the minimum subdirectory depth. + * + * @param minDepth 0: search in the base directory too
+ * otherwise: the minimum depth + */ +void FileFinder::setMinDepth(int minDepth){ + m_minDepth = minDepth; +} + /** * Sets the minimum size. * diff --git a/appl/refind/filefinder.hpp b/appl/refind/filefinder.hpp index 98d1410..9f9941d 100644 --- a/appl/refind/filefinder.hpp +++ b/appl/refind/filefinder.hpp @@ -13,9 +13,11 @@ #include #include +class TextFinder; class FileFinder { public: FileFinder(); + ~FileFinder(); public: int64_t bytes() const; void clear(); @@ -24,13 +26,14 @@ public: void fillTable(const QString& path, int depth, QTableWidget* table); void setBaseDir(const QString& baseDir); void setFiletypes(const QDir::Filters& filetypes); + void setExcludedDirs(const QStringList& excludedDirs); void setMaxDepth(int maxDepth); + void setMaxSize(const int64_t& maxSize); void setMinDepth(int minDepth); - void setExcludedDirs(const QStringList& excludedDirs); + void setMinSize(const int64_t& minSize); void setOlderThan(const QDateTime& olderThan); void setPatterns(const QStringList& patterns); - void setMaxSize(const int64_t& maxSize); - void setMinSize(const int64_t& minSize); + void setTextFinder(TextFinder* textFinder); void setYoungerThan(const QDateTime& youngerThan); private: @@ -52,6 +55,8 @@ private: int m_countDirs; int64_t m_bytes; QStringList m_excludedDirs; + // Only used to hold the search parameters: + TextFinder* m_textFinder; }; #endif // FILEFINDER_HPP diff --git a/appl/refind/mainwindow.cpp b/appl/refind/mainwindow.cpp index 6ee4f2b..fa5075a 100644 --- a/appl/refind/mainwindow.cpp +++ b/appl/refind/mainwindow.cpp @@ -12,9 +12,11 @@ #include #include #include "base/rebase.hpp" +#include "textfinder.hpp" #include "mainwindow.hpp" #include "ui_mainwindow.h" #include "filefinder.hpp" +#include "aboutdialog.hpp" /** * @brief Constructor. @@ -26,17 +28,23 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : ui(new Ui::MainWindow), m_statusMessage(NULL), m_stdLabelBackgroundRole(NULL), - m_errors(0){ + m_errors(0), + m_textFinder(){ ui->setupUi(this); m_statusMessage = new QLabel(tr("Willkommen bei refind")); ui->comboBoxDirectory->setCurrentText( startDir.isEmpty() ? QDir::currentPath() : startDir); statusBar()->addWidget(m_statusMessage); + connect(ui->actionSearch, SIGNAL(triggered()), this, SLOT(search())); connect(ui->pushButtonSearch, SIGNAL(clicked()), this, SLOT(search())); connect(ui->pushButtonSearch2, SIGNAL(clicked()), this, SLOT(search())); + connect(ui->actionUp, SIGNAL(triggered()), this, SLOT(up())); connect(ui->pushButtonUp, SIGNAL(clicked()), this, SLOT(up())); + connect(ui->actionSelectDirectory, SIGNAL(triggered()), this, + SLOT(selectDirectory())); connect(ui->pushButtonDirectory, SIGNAL(clicked()), this, SLOT(selectDirectory())); + connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(about())); ui->tableWidget->setColumnWidth(TC_NODE, 200); ui->tableWidget->setColumnWidth(TC_EXT, 40); ui->tableWidget->setColumnWidth(TC_SIZE, 125); @@ -51,6 +59,16 @@ MainWindow::MainWindow(const QString& startDir, QWidget *parent) : MainWindow::~MainWindow(){ delete ui; } + +/** + * Starts the about dialog. + */ +void MainWindow::about(){ + AboutDialog dialog; + dialog.setModal(true); + dialog.show(); +} + /** * Returns the date given as formula in a combobox. * @@ -169,6 +187,19 @@ QDir::Filters MainWindow::buildFileTypes(){ return rc; } +/** + * Prepares the text search. + */ +void MainWindow::prepareTextFind(){ + m_textFinder.setSearchParameter(ui->comboBoxTextPattern->currentText(), + ui->checkBoxTextIgnoreCase->isChecked(), ui->checkBoxRegExpr, + !ui->checkBoxBinaryFiles); + QString error = m_textFinder.regExprError(); + if (!error.isEmpty()){ + guiError(ui->comboBoxTextPattern, error); + } +} + /** * Handles the "search" button. */ @@ -198,7 +229,10 @@ void MainWindow::search(){ else if (!value.isEmpty()) patterns = value.split(","); finder.setExcludedDirs(patterns); + prepareTextFind(); if (m_errors == 0){ + if (!ui->comboBoxTextPattern->currentText().isEmpty()) + finder.setTextFinder(&m_textFinder); clock_t start = clock(); finder.fillTable(path, 0, ui->tableWidget); QString msg; diff --git a/appl/refind/mainwindow.hpp b/appl/refind/mainwindow.hpp index 373ed7b..4869601 100644 --- a/appl/refind/mainwindow.hpp +++ b/appl/refind/mainwindow.hpp @@ -18,7 +18,9 @@ #include #include #include - +#if ! defined TEXTFINDER_HPP +#include "textfinder.hpp" +#endif namespace Ui { class MainWindow; } @@ -35,6 +37,7 @@ public: ~MainWindow(); private slots: + void about(); void search(); void up(); void selectDirectory(); @@ -46,6 +49,7 @@ private: int64_t comboSize(QComboBox* combo); QString comboText(QComboBox* combo); void guiError(QWidget* widget, const QString& message); + void prepareTextFind(); void setInHistory(QComboBox* combo, const QString& value); void setStatusMessage(bool error, const QString& message); private: @@ -53,6 +57,7 @@ private: QLabel* m_statusMessage; QPalette::ColorRole* m_stdLabelBackgroundRole; int m_errors; + TextFinder m_textFinder; }; #endif // MAINWINDOW_HPP diff --git a/appl/refind/mainwindow.ui b/appl/refind/mainwindow.ui index 252bdec..0acc5a7 100644 --- a/appl/refind/mainwindow.ui +++ b/appl/refind/mainwindow.ui @@ -289,7 +289,7 @@ Execute the search - Search + &Search Ctrl+F @@ -589,7 +589,7 @@ - Search + &Search @@ -651,27 +651,36 @@ 0 0 1030 - 26 + 23 - File + &File + - Help + &Help - + + + + + &Navigate + + + + @@ -685,7 +694,7 @@ - Exit + E&xit Exits the program @@ -696,7 +705,7 @@ - Search + &Search Search the files with the given properties @@ -707,23 +716,40 @@ - About + &About - - Informs about the author + + Ctrl+A + + + + + &Export - Space + Ctrl+E - + - About + &Up + + + Change to the parent directory + + + Ctrl+U - + - Export + &Select directory + + + Select directory with a dialog + + + Ctrl+D diff --git a/appl/refind/refind.pro b/appl/refind/refind.pro index f0d0ab5..1b3fe1a 100644 --- a/appl/refind/refind.pro +++ b/appl/refind/refind.pro @@ -19,14 +19,17 @@ SOURCES += main.cpp\ ../../base/ReQStringUtil.cpp \ ../../base/ReLogger.cpp \ filefinder.cpp \ - textfinder.cpp + textfinder.cpp \ + aboutdialog.cpp HEADERS += mainwindow.hpp \ ../../base/rebase.hpp \ filefinder.hpp \ ../../base/ReQStringUtil.hpp \ - textfinder.hpp + textfinder.hpp \ + aboutdialog.hpp -FORMS += mainwindow.ui +FORMS += mainwindow.ui \ + aboutdialog.ui diff --git a/appl/refind/textfinder.cpp b/appl/refind/textfinder.cpp index abaf7da..e655791 100644 --- a/appl/refind/textfinder.cpp +++ b/appl/refind/textfinder.cpp @@ -11,18 +11,31 @@ /** * Constructor. - * - * @param fullName path and name of the file - * @param length length of the file - * @param ignoreBinary true: binary files will be ignored */ -TextFinder::TextFinder(const QString& fullName, int64_t length, - bool ignoreBinary) : - m_ignoreBinary(ignoreBinary), +TextFinder::TextFinder() : + m_ignoreBinary(false), + m_filename(), + m_length(0), + m_file(), + m_valid(false), + m_regExpr(NULL), + m_isRegExpr(false), + m_ignoreCase(false), + m_ownsRegExpr(false), + m_text(){ +} + +TextFinder::TextFinder(const QString& fullName, int64_t length) : + m_ignoreBinary(false), m_filename(fullName), m_length(length), m_file(fullName), - m_valid(false){ + m_valid(false), + m_regExpr(NULL), + m_isRegExpr(false), + m_ignoreCase(false), + m_ownsRegExpr(false), + m_text(){ m_valid = m_file.open(QIODevice::ReadOnly); } @@ -40,23 +53,17 @@ TextFinder::~TextFinder(){ * @param isRegular true: the pattern is a regular expression * @return true: the patter was found */ -bool TextFinder::contains(const QString& pattern, bool ignoreCase, - bool isRegular){ +bool TextFinder::contains(){ bool rc = false; if (!m_ignoreBinary || !isBinary()){ m_file.seek(0); QTextStream stream(&m_file); QString line; - if (isRegular){ - QRegularExpression::PatternOption option = - ignoreCase ? - QRegularExpression::CaseInsensitiveOption : - QRegularExpression::NoPatternOption; - QRegularExpression expr(pattern, option); + if (m_regExpr != NULL){ QRegularExpressionMatch match; while (!stream.atEnd()){ line = stream.readLine(); - match = expr.match(line); + match = m_regExpr->match(line); if (match.hasMatch()){ rc = true; break; @@ -64,10 +71,10 @@ bool TextFinder::contains(const QString& pattern, bool ignoreCase, } }else{ Qt::CaseSensitivity mode = - ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive; + m_ignoreCase ? Qt::CaseInsensitive : Qt::CaseSensitive; while (!stream.atEnd()){ line = stream.readLine(); - if (line.indexOf(pattern, 0, mode) >= 0){ + if (line.indexOf(m_text, 0, mode) >= 0){ rc = true; break; } @@ -77,6 +84,18 @@ bool TextFinder::contains(const QString& pattern, bool ignoreCase, return rc; } +/** + * Gets the search specific parameters from another instance. + * + * @param source the source of the parameter + */ +void TextFinder::getSearchParameter(const TextFinder& source){ + m_ignoreBinary = source.m_ignoreBinary; + m_regExpr = source.m_regExpr; + m_isRegExpr = source.m_isRegExpr; + m_ignoreCase = source.m_ignoreCase; +} + /** * Tests whether the file is a binary file. * @@ -185,3 +204,40 @@ bool TextFinder::isValid() const{ return m_valid; } +/** + * Set the search parameters. + * + * @param text the text to search + * @param ignoreCase true: the search is case insensitive + * @param isRegExpr true: the search uses a regular expression + * @param ignoreBinary true: binary files will not processed + */ +void TextFinder::setSearchParameter(const QString& text, bool ignoreCase, + bool isRegExpr, bool ignoreBinary){ + m_text = text; + m_ignoreCase = ignoreCase; + m_isRegExpr = isRegExpr; + m_ignoreBinary = ignoreBinary; + if (isRegExpr && !text.isEmpty()){ + QRegularExpression::PatternOption option = + ignoreCase ? + QRegularExpression::CaseInsensitiveOption : + QRegularExpression::NoPatternOption; + m_ownsRegExpr = true; + m_regExpr = new QRegularExpression(text, option); + } +} + +/** + * Returns the error text of a wrong regular expression. + * + * @return "": the regular expression is correct. + * otherwise: the error message + */ +QString TextFinder::regExprError(){ + QString rc; + if (m_regExpr != NULL && m_regExpr->isValid()) + rc = m_regExpr->errorString(); + return rc; +} + diff --git a/appl/refind/textfinder.hpp b/appl/refind/textfinder.hpp index 42748de..a9590e8 100644 --- a/appl/refind/textfinder.hpp +++ b/appl/refind/textfinder.hpp @@ -9,23 +9,33 @@ #ifndef TEXTFINDER_HPP #define TEXTFINDER_HPP +#include class TextFinder { public: - TextFinder(const QString& fullName, int64_t length, bool ignoreBinary); + TextFinder(); + TextFinder(const QString& fullName, int64_t length); ~TextFinder(); public: - bool contains(const QString& pattern, bool ignoreCase, bool isRegular); + void getSearchParameter(const TextFinder& source); + bool contains(); bool isBinary(); bool isText(const QByteArray& data, bool* trueAscii = NULL); bool isUTF8(const QByteArray& data, bool* trueAscii) const; bool isValid() const; - + void setSearchParameter(const QString& text, bool ignoreCase, bool isRegExpr, + bool ignoreBinary); + QString regExprError(); private: bool m_ignoreBinary; QString m_filename; int64_t m_length; QFile m_file; bool m_valid; + QRegularExpression* m_regExpr; + bool m_isRegExpr; + bool m_ignoreCase; + bool m_ownsRegExpr; + QString m_text; }; #endif // TEXTFINDER_HPP -- 2.39.5