]>
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 <kdemacros.h>
29 #include <ktemporaryfile.h>
31 #include <QCoreApplication>
35 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray
&pool
, const QByteArray
&app
) :
36 SlaveBase("search", pool
, app
),
37 m_checkContent(false),
43 FileNameSearchProtocol::~FileNameSearchProtocol()
48 void FileNameSearchProtocol::listDir(const QUrl
& url
)
52 const QString search
= url
.queryItemValue("search");
53 if (!search
.isEmpty()) {
54 m_regExp
= new QRegExp(search
, Qt::CaseInsensitive
, QRegExp::Wildcard
);
57 m_checkContent
= false;
58 const QString checkContent
= url
.queryItemValue("checkContent");
59 if (checkContent
== QLatin1String("yes")) {
60 m_checkContent
= true;
63 const QString urlString
= url
.queryItemValue("url");
64 searchDirectory(KUrl(urlString
));
70 void FileNameSearchProtocol::searchDirectory(const KUrl
& directory
)
72 if (directory
.path() == QLatin1String("/proc")) {
73 // Don't try to iterate the /proc directory of Linux
77 // Get all items of the directory
78 KDirLister
*dirLister
= new KDirLister();
79 dirLister
->setDelayedMimeTypes(false);
80 dirLister
->setAutoErrorHandlingEnabled(false, 0);
81 dirLister
->openUrl(directory
);
84 QObject::connect(dirLister
, static_cast<void(KDirLister::*)()>(&KDirLister::canceled
), &eventLoop
, &QEventLoop::quit
);
85 QObject::connect(dirLister
, static_cast<void(KDirLister::*)()>(&KDirLister::completed
), &eventLoop
, &QEventLoop::quit
);
88 // Visualize all items that match the search pattern
89 QList
<KUrl
> pendingDirs
;
90 const KFileItemList items
= dirLister
->items();
91 foreach (const KFileItem
& item
, items
) {
93 if (!m_regExp
|| item
.name().contains(*m_regExp
)) {
95 } else if (m_checkContent
&& item
.determineMimeType().inherits(QLatin1String("text/plain"))) {
96 addItem
= contentContainsPattern(item
.url());
100 KIO::UDSEntry entry
= item
.entry();
101 entry
.insert(KIO::UDSEntry::UDS_URL
, item
.url().url() );
102 listEntry(entry
,false);
107 // Assure that no endless searching is done in directories that
108 // have already been iterated.
109 const KUrl
linkDest(item
.url(), item
.linkDest());
110 if (!m_iteratedDirs
.contains(linkDest
.path())) {
111 pendingDirs
.append(linkDest
);
114 pendingDirs
.append(item
.url());
118 listEntry(KIO::UDSEntry(), true);
120 m_iteratedDirs
.insert(directory
.path());
125 // Recursively iterate all sub directories
126 foreach (const KUrl
& pendingDir
, pendingDirs
) {
127 searchDirectory(pendingDir
);
131 bool FileNameSearchProtocol::contentContainsPattern(const KUrl
& fileName
) const
136 KTemporaryFile tempFile
;
138 if (fileName
.isLocalFile()) {
139 path
= fileName
.path();
140 } else if (tempFile
.open()) {
141 KIO::Job
* getJob
= KIO::file_copy(fileName
,
144 KIO::Overwrite
| KIO::HideProgressInfo
);
145 if (!KIO::NetAccess::synchronousRun(getJob
, 0)) {
146 // The non-local file could not be downloaded
149 path
= tempFile
.fileName();
151 // No temporary file could be created for downloading non-local files
156 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
160 QTextStream
in(&file
);
161 while (!in
.atEnd()) {
162 const QString line
= in
.readLine();
163 if (line
.contains(*m_regExp
)) {
171 void FileNameSearchProtocol::cleanup()
175 m_iteratedDirs
.clear();
178 extern "C" int KDE_EXPORT
kdemain( int argc
, char **argv
)
180 KComponentData
instance("kio_search");
181 QCoreApplication
app(argc
, argv
);
184 fprintf(stderr
, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
188 FileNameSearchProtocol
slave(argv
[2], argv
[3]);
189 slave
.dispatchLoop();