]>
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.h>
23 #include <kdirlister.h>
24 #include <kfileitem.h>
25 #include <kio/netaccess.h>
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),
41 FileNameSearchProtocol::~FileNameSearchProtocol()
47 void FileNameSearchProtocol::listDir(const KUrl
& url
)
52 const QString search
= url
.queryItem("search");
53 if (!search
.isEmpty()) {
54 m_regExp
= new QRegExp(search
, Qt::CaseInsensitive
, QRegExp::Wildcard
);
57 m_checkContent
= false;
58 const QString checkContent
= url
.queryItem("checkContent");
59 if (checkContent
== QLatin1String("yes")) {
60 m_checkContent
= true;
63 const QString urlString
= url
.queryItem("url");
64 searchDirectory(KUrl(urlString
));
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
= contentContainsPattern(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::contentContainsPattern(const KUrl
& fileName
) const
113 Q_ASSERT(m_regExp
!= 0);
116 KTemporaryFile tempFile
;
118 if (fileName
.isLocalFile()) {
119 path
= fileName
.path();
120 } else if (tempFile
.open()) {
121 KIO::Job
* getJob
= KIO::file_copy(fileName
,
124 KIO::Overwrite
| KIO::HideProgressInfo
);
125 if (!KIO::NetAccess::synchronousRun(getJob
, 0)) {
126 // The non-local file could not be downloaded
129 path
= tempFile
.fileName();
131 // No temporary file could be created for downloading non-local files
136 if (!file
.open(QIODevice::ReadOnly
| QIODevice::Text
)) {
140 QTextStream
in(&file
);
141 while (!in
.atEnd()) {
142 const QString line
= in
.readLine();
143 if (line
.contains(*m_regExp
)) {
151 extern "C" int KDE_EXPORT
kdemain( int argc
, char **argv
)
153 KComponentData
instance("kio_search");
154 QCoreApplication
app(argc
, argv
);
157 fprintf(stderr
, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
161 FileNameSearchProtocol
slave(argv
[2], argv
[3]);
162 slave
.dispatchLoop();