]>
cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
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>
25 #include <KIO/NetAccess>
28 #include <ktemporaryfile.h>
30 #include <QCoreApplication>
34 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray
&pool
, const QByteArray
&app
) :
35 SlaveBase("search", pool
, app
),
36 m_checkContent(false),
42 FileNameSearchProtocol::~FileNameSearchProtocol()
47 void FileNameSearchProtocol::listDir(const KUrl
& url
)
51 const QString search
= url
.queryItem("search");
52 if (!search
.isEmpty()) {
53 m_regExp
= new QRegExp(search
, Qt::CaseInsensitive
, QRegExp::Wildcard
);
56 m_checkContent
= false;
57 const QString checkContent
= url
.queryItem("checkContent");
58 if (checkContent
== QLatin1String("yes")) {
59 m_checkContent
= true;
62 const QString urlString
= url
.queryItem("url");
63 searchDirectory(KUrl(urlString
));
69 void FileNameSearchProtocol::searchDirectory(const KUrl
& directory
)
71 if (directory
.path() == QLatin1String("/proc")) {
72 // Don't try to iterate the /proc directory of Linux
76 // Get all items of the directory
77 KDirLister
*dirLister
= new KDirLister();
78 dirLister
->setDelayedMimeTypes(false);
79 dirLister
->setAutoErrorHandlingEnabled(false, 0);
80 dirLister
->openUrl(directory
);
83 QObject::connect(dirLister
, SIGNAL(canceled()), &eventLoop
, SLOT(quit()));
84 QObject::connect(dirLister
, SIGNAL(completed()), &eventLoop
, SLOT(quit()));
87 // Visualize all items that match the search pattern
88 QList
<KUrl
> pendingDirs
;
89 const KFileItemList items
= dirLister
->items();
90 foreach (const KFileItem
& item
, items
) {
92 if ((m_regExp
== 0) || item
.name().contains(*m_regExp
)) {
94 } else if (m_checkContent
&& item
.mimetype().startsWith(QLatin1String("text/"))) {
95 addItem
= contentContainsPattern(item
.url());
99 KIO::UDSEntry entry
= item
.entry();
100 entry
.insert(KIO::UDSEntry::UDS_URL
, item
.url().url() );
101 listEntry(entry
,false);
106 // Assure that no endless searching is done in directories that
107 // have already been iterated.
108 const KUrl
linkDest(item
.url(), item
.linkDest());
109 if (!m_iteratedDirs
.contains(linkDest
.path())) {
110 pendingDirs
.append(linkDest
);
113 pendingDirs
.append(item
.url());
117 listEntry(KIO::UDSEntry(), true);
119 m_iteratedDirs
.insert(directory
.path());
124 // Recursively iterate all sub directories
125 foreach (const KUrl
& pendingDir
, pendingDirs
) {
126 searchDirectory(pendingDir
);
130 bool FileNameSearchProtocol::contentContainsPattern(const KUrl
& fileName
) const
132 Q_ASSERT(m_regExp
!= 0);
135 KTemporaryFile tempFile
;
137 if (fileName
.isLocalFile()) {
138 path
= fileName
.path();
139 } else if (tempFile
.open()) {
140 KIO::Job
* getJob
= KIO::file_copy(fileName
,
143 KIO::Overwrite
| KIO::HideProgressInfo
);
144 if (!KIO::NetAccess::synchronousRun(getJob
, 0)) {
145 // The non-local file could not be downloaded
148 path
= tempFile
.fileName();
150 // No temporary file could be created for downloading non-local files
155 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
159 QTextStream
in(&file
);
160 while (!in
.atEnd()) {
161 const QString line
= in
.readLine();
162 if (line
.contains(*m_regExp
)) {
170 void FileNameSearchProtocol::cleanup()
174 m_iteratedDirs
.clear();
177 extern "C" int KDE_EXPORT
kdemain( int argc
, char **argv
)
179 KComponentData
instance("kio_search");
180 QCoreApplication
app(argc
, argv
);
183 fprintf(stderr
, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
187 FileNameSearchProtocol
slave(argv
[2], argv
[3]);
188 slave
.dispatchLoop();