From de795b8ab7d061b6ef25233ef5faf699643bb81b Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 9 Dec 2018 05:09:09 +0100 Subject: Improve subject guessing Use QRegularExpression instead of QRegExp, since the former is recommended. Escape the filename, so we don't have to worry about special chars and then match line by line. If there is a number at the end of the found subject, remove it. The former logic with QRegExp, indexOf and no escape wouldn't match files with whitespaces and special chars... --- filewidget.cpp | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/filewidget.cpp b/filewidget.cpp index 31dfa92..6294487 100644 --- a/filewidget.cpp +++ b/filewidget.cpp @@ -395,22 +395,27 @@ void FileWidget::guessSubject(){ QModelIndexList sel = mFileView->selectionModel()->selectedRows(NameColumn); if(!sel.isEmpty()){ QFileInfo fi(sel.first().data().toString()); - QString fRe = QString(".*%1.*").arg(fi.completeBaseName()); - const QRegExp fnRe(fRe); - int index = mDescript.indexOf(fnRe); - if(index > -1){ - QString subject(mDescript.at(index)); - if(subject.startsWith('"')){ - int nextQuot = subject.indexOf('"', 1); - if(nextQuot > -1){ - subject = subject.remove(0, nextQuot + 1).trimmed(); + QString fRe = QRegularExpression::escape(fi.completeBaseName()); + const QRegularExpression fnRe(fRe); + QRegularExpressionMatch m; + for(QString l : mDescript){ + m = fnRe.match(l); + if(m.hasMatch()){ + QString subject; + if(l.startsWith('"')){ + int nextQuot = l.indexOf('"', 1); + if(nextQuot > -1){ + subject = l.remove(0, nextQuot + 1).trimmed(); + } } + subject.replace(QRegularExpression("\\d+$"), ""); + subject = subject.trimmed(); + qApp->clipboard()->setText(subject); + emit statusMessage(subject); + return; } - qApp->clipboard()->setText(subject); - emit statusMessage(subject); - }else{ - emit statusMessage(tr("No subject found!")); } + emit statusMessage(tr("No subject found!")); } } -- cgit v1.2.3-70-g09d2