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>
30 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray
&pool
, const QByteArray
&app
) :
31 SlaveBase("search", pool
, app
),
32 m_checkContent(false),
37 FileNameSearchProtocol::~FileNameSearchProtocol()
41 void FileNameSearchProtocol::listDir(const KUrl
& url
)
43 const QStringList searchValues
= url
.allQueryItemValues("search");
44 m_searchPattern
.clear();
45 if (!searchValues
.isEmpty()) {
46 m_searchPattern
= searchValues
.first();
49 m_checkContent
= false;
50 const QStringList checkContentValues
= url
.allQueryItemValues("checkContent");
51 if (!checkContentValues
.isEmpty() && (checkContentValues
.first() == QLatin1String("yes"))) {
52 m_checkContent
= true;
56 directory
.setProtocol("file");
57 directory
.setEncodedQuery(QByteArray());
59 searchDirectory(directory
);
64 void FileNameSearchProtocol::searchDirectory(const KUrl
& directory
)
66 // Get all items of the directory
67 KDirLister
*dirLister
= new KDirLister();
68 dirLister
->setDelayedMimeTypes(false);
69 dirLister
->setAutoErrorHandlingEnabled(false, 0);
70 dirLister
->openUrl(directory
);
73 QObject::connect(dirLister
, SIGNAL(canceled()), &eventLoop
, SLOT(quit()));
74 QObject::connect(dirLister
, SIGNAL(completed()), &eventLoop
, SLOT(quit()));
77 // Visualize all items that match the search pattern
78 QList
<KUrl
> pendingDirs
;
79 const KFileItemList items
= dirLister
->items();
80 foreach (const KFileItem
& item
, items
) {
82 if (m_searchPattern
.isEmpty() || item
.name().contains(m_searchPattern
, Qt::CaseInsensitive
)) {
84 } else if (m_checkContent
&& item
.mimetype().startsWith(QLatin1String("text/"))) {
85 addItem
= containsPattern(item
.url());
89 KIO::UDSEntry entry
= item
.entry();
90 entry
.insert(KIO::UDSEntry::UDS_URL
, item
.url().url() );
91 listEntry(entry
,false);
95 pendingDirs
.append(item
.url());
98 listEntry(KIO::UDSEntry(), true);
100 // Recursively iterate all sub directories
101 foreach (const KUrl
& pendingDir
, pendingDirs
) {
102 searchDirectory(pendingDir
);
106 bool FileNameSearchProtocol::containsPattern(const KUrl
& fileName
) const
108 QFile
file(fileName
.path());
109 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
113 QTextStream
in(&file
);
114 while (!in
.atEnd()) {
115 const QString line
= in
.readLine();
116 if (line
.contains(m_searchPattern
)) {
124 extern "C" int KDE_EXPORT
kdemain( int argc
, char **argv
)
126 KComponentData
instance("kio_search");
127 QCoreApplication
app(argc
, argv
);
130 fprintf(stderr
, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
134 FileNameSearchProtocol
slave(argv
[2], argv
[3]);
135 slave
.dispatchLoop();