]> gitweb.hamatoma.de Git - reqt/commitdiff
TextFinder integrated
authorhama <hama@siduction.net>
Sun, 19 Apr 2015 22:05:02 +0000 (00:05 +0200)
committerhama <hama@siduction.net>
Sun, 19 Apr 2015 22:05:02 +0000 (00:05 +0200)
12 files changed:
appl/refind/about.ui [new file with mode: 0644]
appl/refind/aboutdialog.cpp [new file with mode: 0644]
appl/refind/aboutdialog.hpp [new file with mode: 0644]
appl/refind/aboutdialog.ui [new file with mode: 0644]
appl/refind/filefinder.cpp
appl/refind/filefinder.hpp
appl/refind/mainwindow.cpp
appl/refind/mainwindow.hpp
appl/refind/mainwindow.ui
appl/refind/refind.pro
appl/refind/textfinder.cpp
appl/refind/textfinder.hpp

diff --git a/appl/refind/about.ui b/appl/refind/about.ui
new file mode 100644 (file)
index 0000000..a6d80c5
--- /dev/null
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>AboutDialog</class>
+ <widget class="QDialog" name="AboutDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>225</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>About</string>
+  </property>
+  <widget class="QTextEdit" name="textEdit">
+   <property name="geometry">
+    <rect>
+     <x>20</x>
+     <y>20</y>
+     <width>351</width>
+     <height>141</height>
+    </rect>
+   </property>
+   <property name="html">
+    <string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
+&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
+p, li { white-space: pre-wrap; }
+&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;ReFind&lt;/span&gt; for searching files in a directory tree&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This is a program of the project &amp;quot;&lt;span style=&quot; font-weight:600;&quot;&gt;Re&lt;/span&gt;al &lt;span style=&quot; font-weight:600;&quot;&gt;Pub&lt;/span&gt;lic &lt;span style=&quot; font-weight:600;&quot;&gt;Lib&lt;/span&gt;rary&amp;quot;.&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Sources are public domain and available under https://github.com/republib&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Implemented in QT (C++) 5.x&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButton">
+   <property name="geometry">
+    <rect>
+     <x>280</x>
+     <y>180</y>
+     <width>93</width>
+     <height>27</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>&amp;OK</string>
+   </property>
+  </widget>
+  <action name="action_OK">
+   <property name="text">
+    <string>&amp;OK</string>
+   </property>
+  </action>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>action_OK</sender>
+   <signal>triggered()</signal>
+   <receiver>AboutDialog</receiver>
+   <slot>close()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>199</x>
+     <y>112</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
diff --git a/appl/refind/aboutdialog.cpp b/appl/refind/aboutdialog.cpp
new file mode 100644 (file)
index 0000000..43a6eb5
--- /dev/null
@@ -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 (file)
index 0000000..b15b499
--- /dev/null
@@ -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 <QDialog>
+
+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 (file)
index 0000000..fd6f146
--- /dev/null
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>AboutDialog</class>
+ <widget class="QDialog" name="AboutDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>218</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <widget class="QTextEdit" name="textEdit">
+   <property name="geometry">
+    <rect>
+     <x>20</x>
+     <y>20</y>
+     <width>351</width>
+     <height>141</height>
+    </rect>
+   </property>
+   <property name="readOnly">
+    <bool>true</bool>
+   </property>
+   <property name="html">
+    <string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
+&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
+p, li { white-space: pre-wrap; }
+&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;&quot;&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-weight:600;&quot;&gt;ReFind&lt;/span&gt; for searching files in a directory tree&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;This is a program of the project &amp;quot;&lt;span style=&quot; font-weight:600;&quot;&gt;Re&lt;/span&gt;al &lt;span style=&quot; font-weight:600;&quot;&gt;Pub&lt;/span&gt;lic &lt;span style=&quot; font-weight:600;&quot;&gt;Lib&lt;/span&gt;rary&amp;quot;.&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Sources are public domain and available under https://github.com/republib&lt;/p&gt;
+&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;Implemented in QT (C++) 5.x&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
+   </property>
+  </widget>
+  <widget class="QPushButton" name="pushButtonOK">
+   <property name="geometry">
+    <rect>
+     <x>150</x>
+     <y>170</y>
+     <width>93</width>
+     <height>27</height>
+    </rect>
+   </property>
+   <property name="text">
+    <string>&amp;OK</string>
+   </property>
+  </widget>
+  <action name="actionOk">
+   <property name="text">
+    <string>&amp;Ok</string>
+   </property>
+   <property name="toolTip">
+    <string>Close the dialog</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+X</string>
+   </property>
+  </action>
+ </widget>
+ <resources/>
+ <connections>
+  <connection>
+   <sender>actionOk</sender>
+   <signal>triggered()</signal>
+   <receiver>AboutDialog</receiver>
+   <slot>close()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>-1</x>
+     <y>-1</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>199</x>
+     <y>108</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
index b934212bc0ab77286df4a6c1cacb2b36d028b95c..777a6a3517304faf0fb2c6ca128ef978348ebe3c 100644 (file)
@@ -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 <QFileInfo>::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<br>
- *                  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<br>
+ *                  otherwise: the minimum depth
+ */
+void FileFinder::setMinDepth(int minDepth){
+   m_minDepth = minDepth;
+}
+
 /**
  * Sets the minimum size.
  *
index 98d14106038974802c12d6f167d05c3454598cd7..9f9941dac2b3b52f96abc2dac2bc1746d2a4f4da 100644 (file)
 #include <QDir>
 #include <QDateTime>
 
+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
index 6ee4f2ba87a617c87f9bcbdedf185ce9581a84eb..fa5075af1117ea9cc9a0d40a66ba7b2bc2667d13 100644 (file)
 #include <QDir>
 #include <QFileDialog>
 #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;
index 373ed7b88775a1133442bfcb2fae0d31fd9dce54..48696015f3fb0a1315f2ce37897dc52fae8fdfeb 100644 (file)
@@ -18,7 +18,9 @@
 #include <QComboBox>
 #include <QLabel>
 #include <QDir>
-
+#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
index 252bdec0da368db36a24db37254f3b1f49f3418d..0acc5a7986f615a0e7ceded1621c251723b1bf0d 100644 (file)
                <string>Execute the search</string>
               </property>
               <property name="text">
-               <string>Search</string>
+               <string>&amp;Search</string>
               </property>
               <property name="shortcut">
                <string>Ctrl+F</string>
            <item row="2" column="4" colspan="2">
             <widget class="QPushButton" name="pushButtonSearch2">
              <property name="text">
-              <string>Search</string>
+              <string>&amp;Search</string>
              </property>
             </widget>
            </item>
      <x>0</x>
      <y>0</y>
      <width>1030</width>
-     <height>26</height>
+     <height>23</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
     <property name="title">
-     <string>File</string>
+     <string>&amp;File</string>
     </property>
     <addaction name="actionSearch"/>
     <addaction name="separator"/>
     <addaction name="actionExport"/>
     <addaction name="separator"/>
+    <addaction name="separator"/>
     <addaction name="actionExit"/>
     <addaction name="separator"/>
    </widget>
    <widget class="QMenu" name="menuHelp">
     <property name="title">
-     <string>Help</string>
+     <string>&amp;Help</string>
     </property>
-    <addaction name="actionAbout_2"/>
+    <addaction name="actionAbout"/>
+   </widget>
+   <widget class="QMenu" name="menuNavigation">
+    <property name="title">
+     <string>&amp;Navigate</string>
+    </property>
+    <addaction name="actionUp"/>
+    <addaction name="actionSelectDirectory"/>
    </widget>
    <addaction name="menuFile"/>
+   <addaction name="menuNavigation"/>
    <addaction name="menuHelp"/>
   </widget>
   <widget class="QToolBar" name="mainToolBar">
   <widget class="QStatusBar" name="statusBar"/>
   <action name="actionExit">
    <property name="text">
-    <string>Exit</string>
+    <string>E&amp;xit</string>
    </property>
    <property name="toolTip">
     <string>Exits the program</string>
   </action>
   <action name="actionSearch">
    <property name="text">
-    <string>Search</string>
+    <string>&amp;Search</string>
    </property>
    <property name="toolTip">
     <string>Search the files with the given properties</string>
   </action>
   <action name="actionAbout">
    <property name="text">
-    <string>About</string>
+    <string>&amp;About</string>
    </property>
-   <property name="toolTip">
-    <string>Informs about the author</string>
+   <property name="shortcut">
+    <string>Ctrl+A</string>
+   </property>
+  </action>
+  <action name="actionExport">
+   <property name="text">
+    <string>&amp;Export</string>
    </property>
    <property name="shortcut">
-    <string>Space</string>
+    <string>Ctrl+E</string>
    </property>
   </action>
-  <action name="actionAbout_2">
+  <action name="actionUp">
    <property name="text">
-    <string>About</string>
+    <string>&amp;Up</string>
+   </property>
+   <property name="toolTip">
+    <string>Change to the parent directory</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+U</string>
    </property>
   </action>
-  <action name="actionExport">
+  <action name="actionSelectDirectory">
    <property name="text">
-    <string>Export</string>
+    <string>&amp;Select directory</string>
+   </property>
+   <property name="toolTip">
+    <string>Select directory with a dialog</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+D</string>
    </property>
   </action>
  </widget>
index f0d0ab51bf72c43e6544621b23fe7db32f503055..1b3fe1a59483110b4c4cb1c5e35469f843f1d8b9 100644 (file)
@@ -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
index abaf7dabb964bf07cb7edc84ef21b7aaa341adc3..e6557915237d4521f92b35ed1ac784d978bbea11 100644 (file)
 
 /**
  * Constructor.
- *
- * @param fullName  path and name of the file
- * @param length        length of the file
- * @param ignoreBinary  <code>true</code>: 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     <code>true</code>: the pattern is a regular expression
  * @return              <code>true</code>: 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    <code>true</code>: the search is case insensitive
+ * @param isRegExpr     <code>true</code>: the search uses a regular expression
+ * @param ignoreBinary  <code>true</code>: 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;
+}
+
index 42748de3e0cdcebfcabeb4c3f71b8bb3562f3e3b..a9590e89e30492ac711f3dd7aecf3692dc08a015 100644 (file)
@@ -9,23 +9,33 @@
 #ifndef TEXTFINDER_HPP
 #define TEXTFINDER_HPP
 
+#include <QRegularExpression>
 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