1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "filenamesearchprotocol.h"
22 #include <kcomponentdata.h>
23 #include <kdirlister.h>
24 #include <kfileitem.h>
27 #include <QCoreApplication>
31 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray
&pool
, const QByteArray
&app
) :
32 SlaveBase("search", pool
, app
),
33 m_checkContent(false),
38 FileNameSearchProtocol::~FileNameSearchProtocol()
44 void FileNameSearchProtocol::listDir(const KUrl
& url
)
49 const QStringList searchValues
= url
.allQueryItemValues("search");
50 if (!searchValues
.isEmpty()) {
51 m_regExp
= new QRegExp(searchValues
.first(), Qt::CaseInsensitive
, QRegExp::Wildcard
);
54 m_checkContent
= false;
55 const QStringList checkContentValues
= url
.allQueryItemValues("checkContent");
56 if (!checkContentValues
.isEmpty() && (checkContentValues
.first() == QLatin1String("yes"))) {
57 m_checkContent
= true;
61 directory
.setProtocol("file");
62 directory
.setEncodedQuery(QByteArray());
64 searchDirectory(directory
);
69 void FileNameSearchProtocol::searchDirectory(const KUrl
& directory
)
71 // Get all items of the directory
72 KDirLister
*dirLister
= new KDirLister();
73 dirLister
->setDelayedMimeTypes(false);
74 dirLister
->setAutoErrorHandlingEnabled(false, 0);
75 dirLister
->openUrl(directory
);
78 QObject::connect(dirLister
, SIGNAL(canceled()), &eventLoop
, SLOT(quit()));
79 QObject::connect(dirLister
, SIGNAL(completed()), &eventLoop
, SLOT(quit()));
82 // Visualize all items that match the search pattern
83 QList
<KUrl
> pendingDirs
;
84 const KFileItemList items
= dirLister
->items();
85 foreach (const KFileItem
& item
, items
) {
87 if ((m_regExp
== 0) || item
.name().contains(*m_regExp
)) {
89 } else if (m_checkContent
&& item
.mimetype().startsWith(QLatin1String("text/"))) {
90 addItem
= containsPattern(item
.url());
94 KIO::UDSEntry entry
= item
.entry();
95 entry
.insert(KIO::UDSEntry::UDS_URL
, item
.url().url() );
96 listEntry(entry
,false);
100 pendingDirs
.append(item
.url());
103 listEntry(KIO::UDSEntry(), true);
105 // Recursively iterate all sub directories
106 foreach (const KUrl
& pendingDir
, pendingDirs
) {
107 searchDirectory(pendingDir
);
111 bool FileNameSearchProtocol::containsPattern(const KUrl
& fileName
) const
113 Q_ASSERT(m_regExp
!= 0);
115 QFile
file(fileName
.path());
116 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
120 QTextStream
in(&file
);
121 while (!in
.atEnd()) {
122 const QString line
= in
.readLine();
123 if (line
.contains(*m_regExp
)) {
131 extern "C" int KDE_EXPORT
kdemain( int argc
, char **argv
)
133 KComponentData
instance("kio_search");
134 QCoreApplication
app(argc
, argv
);
137 fprintf(stderr
, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
141 FileNameSearchProtocol
slave(argv
[2], argv
[3]);
142 slave
.dispatchLoop();