Commit 66229118 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

only open file once for mime-type & search

parent c29012fd
......@@ -53,6 +53,7 @@ void SearchDiskFiles::startSearch(const QStringList &files, const QRegularExpres
void SearchDiskFiles::run()
{
const QMimeDatabase db;
for (const QString &fileName : qAsConst(m_files)) {
if (m_cancelSearch) {
break;
......@@ -63,18 +64,24 @@ void SearchDiskFiles::run()
emit searching(fileName);
}
// open file early, this allows mime-type detection & search to use same io device
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
continue;
}
// exclude binary files?
if (!m_includeBinaryFiles) {
const auto mimeType = QMimeDatabase().mimeTypeForFile(fileName);
const auto mimeType = db.mimeTypeForFileNameAndData(fileName, &file);
if (!mimeType.inherits(QStringLiteral("text/plain"))) {
continue;
}
}
if (m_regExp.pattern().contains(QLatin1String("\\n"))) {
searchMultiLineRegExp(fileName);
searchMultiLineRegExp(file);
} else {
searchSingleLineRegExp(fileName);
searchSingleLineRegExp(file);
}
}
......@@ -101,13 +108,8 @@ bool SearchDiskFiles::searching()
return !m_cancelSearch;
}
void SearchDiskFiles::searchSingleLineRegExp(const QString &fileName)
void SearchDiskFiles::searchSingleLineRegExp(QFile &file)
{
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
return;
}
QTextStream stream(&file);
QString line;
int i = 0;
......@@ -142,23 +144,18 @@ void SearchDiskFiles::searchSingleLineRegExp(const QString &fileName)
}
// emit all matches batched
const QUrl fileUrl = QUrl::fromUserInput(fileName);
const QUrl fileUrl = QUrl::fromUserInput(file.fileName());
emit matchesFound(fileUrl, matches);
}
void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
void SearchDiskFiles::searchMultiLineRegExp(QFile &file)
{
QFile file(fileName);
int column = 0;
int line = 0;
static QString fullDoc;
static QVector<int> lineStart;
QRegularExpression tmpRegExp = m_regExp;
if (!file.open(QFile::ReadOnly)) {
return;
}
QTextStream stream(&file);
fullDoc = stream.readAll();
fullDoc.remove(QLatin1Char('\r'));
......@@ -217,6 +214,6 @@ void SearchDiskFiles::searchMultiLineRegExp(const QString &fileName)
}
// emit all matches batched
const QUrl fileUrl = QUrl::fromUserInput(fileName);
const QUrl fileUrl = QUrl::fromUserInput(file.fileName());
emit matchesFound(fileUrl, matches);
}
......@@ -43,8 +43,8 @@ public:
bool searching();
private:
void searchSingleLineRegExp(const QString &fileName);
void searchMultiLineRegExp(const QString &fileName);
void searchSingleLineRegExp(QFile &file);
void searchMultiLineRegExp(QFile &file);
public Q_SLOTS:
void cancelSearch();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment