Allow to use wildcards when searching filenames or simple text files with the filenamesearchprotocol.

svn path=/trunk/KDE/kdebase/apps/; revision=1183944
This commit is contained in:
Peter Penz 2010-10-08 20:07:13 +00:00
parent eade3a4a3d
commit 396706a741
2 changed files with 14 additions and 6 deletions

View file

@ -26,24 +26,29 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QEventLoop> #include <QEventLoop>
#include <QRegExp>
FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) : FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) :
SlaveBase("search", pool, app), SlaveBase("search", pool, app),
m_checkContent(false), m_checkContent(false),
m_searchPattern() m_regExp(0)
{ {
} }
FileNameSearchProtocol::~FileNameSearchProtocol() FileNameSearchProtocol::~FileNameSearchProtocol()
{ {
delete m_regExp;
m_regExp = 0;
} }
void FileNameSearchProtocol::listDir(const KUrl& url) void FileNameSearchProtocol::listDir(const KUrl& url)
{ {
delete m_regExp;
m_regExp = 0;
const QStringList searchValues = url.allQueryItemValues("search"); const QStringList searchValues = url.allQueryItemValues("search");
m_searchPattern.clear();
if (!searchValues.isEmpty()) { if (!searchValues.isEmpty()) {
m_searchPattern = searchValues.first(); m_regExp = new QRegExp(searchValues.first(), Qt::CaseInsensitive, QRegExp::Wildcard);
} }
m_checkContent = false; m_checkContent = false;
@ -79,7 +84,7 @@ void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
const KFileItemList items = dirLister->items(); const KFileItemList items = dirLister->items();
foreach (const KFileItem& item, items) { foreach (const KFileItem& item, items) {
bool addItem = false; bool addItem = false;
if (m_searchPattern.isEmpty() || item.name().contains(m_searchPattern, Qt::CaseInsensitive)) { if ((m_regExp == 0) || item.name().contains(*m_regExp)) {
addItem = true; addItem = true;
} else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) { } else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) {
addItem = containsPattern(item.url()); addItem = containsPattern(item.url());
@ -105,6 +110,8 @@ void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
bool FileNameSearchProtocol::containsPattern(const KUrl& fileName) const bool FileNameSearchProtocol::containsPattern(const KUrl& fileName) const
{ {
Q_ASSERT(m_regExp != 0);
QFile file(fileName.path()); QFile file(fileName.path());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false; return false;
@ -113,7 +120,7 @@ bool FileNameSearchProtocol::containsPattern(const KUrl& fileName) const
QTextStream in(&file); QTextStream in(&file);
while (!in.atEnd()) { while (!in.atEnd()) {
const QString line = in.readLine(); const QString line = in.readLine();
if (line.contains(m_searchPattern)) { if (line.contains(*m_regExp)) {
return true; return true;
} }
} }

View file

@ -23,6 +23,7 @@
#include <kio/slavebase.h> #include <kio/slavebase.h>
class KUrl; class KUrl;
class QRegExp;
/** /**
* @brief Lists files where the filename matches do a given query. * @brief Lists files where the filename matches do a given query.
@ -49,7 +50,7 @@ private:
bool containsPattern(const KUrl& fileName) const; bool containsPattern(const KUrl& fileName) const;
bool m_checkContent; bool m_checkContent;
QString m_searchPattern; QRegExp* m_regExp;
}; };
#endif #endif