]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
Fix build when baloo is found
[dolphin.git] / src / search / filenamesearchprotocol.cpp
1 /***************************************************************************
2 * Copyright (C) 2010 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "filenamesearchprotocol.h"
21
22 #include <KComponentData>
23 #include <KDirLister>
24 #include <KFileItem>
25 #include <KIO/NetAccess>
26 #include <KIO/Job>
27 #include <KUrl>
28 #include <kdemacros.h>
29 #include <ktemporaryfile.h>
30
31 #include <QCoreApplication>
32 #include <QEventLoop>
33 #include <QRegExp>
34
35 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) :
36 SlaveBase("search", pool, app),
37 m_checkContent(false),
38 m_regExp(0),
39 m_iteratedDirs()
40 {
41 }
42
43 FileNameSearchProtocol::~FileNameSearchProtocol()
44 {
45 cleanup();
46 }
47
48 void FileNameSearchProtocol::listDir(const QUrl& url)
49 {
50 cleanup();
51
52 const QString search = url.queryItemValue("search");
53 if (!search.isEmpty()) {
54 m_regExp = new QRegExp(search, Qt::CaseInsensitive, QRegExp::Wildcard);
55 }
56
57 m_checkContent = false;
58 const QString checkContent = url.queryItemValue("checkContent");
59 if (checkContent == QLatin1String("yes")) {
60 m_checkContent = true;
61 }
62
63 const QString urlString = url.queryItemValue("url");
64 searchDirectory(KUrl(urlString));
65
66 cleanup();
67 finished();
68 }
69
70 void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
71 {
72 if (directory.path() == QLatin1String("/proc")) {
73 // Don't try to iterate the /proc directory of Linux
74 return;
75 }
76
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);
82
83 QEventLoop eventLoop;
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);
86 eventLoop.exec();
87
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) {
92 bool addItem = false;
93 if (!m_regExp || item.name().contains(*m_regExp)) {
94 addItem = true;
95 } else if (m_checkContent && item.determineMimeType().inherits(QLatin1String("text/plain"))) {
96 qDebug() << "### Checking" << item;
97 addItem = contentContainsPattern(item.url());
98 } else {
99 qDebug() << "### NOT Checking" << item;
100 }
101
102 if (addItem) {
103 KIO::UDSEntry entry = item.entry();
104 entry.insert(KIO::UDSEntry::UDS_URL, item.url().url() );
105 listEntry(entry,false);
106 }
107
108 if (item.isDir()) {
109 if (item.isLink()) {
110 // Assure that no endless searching is done in directories that
111 // have already been iterated.
112 const KUrl linkDest(item.url(), item.linkDest());
113 if (!m_iteratedDirs.contains(linkDest.path())) {
114 pendingDirs.append(linkDest);
115 }
116 } else {
117 pendingDirs.append(item.url());
118 }
119 }
120 }
121 listEntry(KIO::UDSEntry(), true);
122
123 m_iteratedDirs.insert(directory.path());
124
125 delete dirLister;
126 dirLister = 0;
127
128 // Recursively iterate all sub directories
129 foreach (const KUrl& pendingDir, pendingDirs) {
130 searchDirectory(pendingDir);
131 }
132 }
133
134 bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
135 {
136 Q_ASSERT(m_regExp);
137
138 QString path;
139 KTemporaryFile tempFile;
140
141 if (fileName.isLocalFile()) {
142 path = fileName.path();
143 } else if (tempFile.open()) {
144 KIO::Job* getJob = KIO::file_copy(fileName,
145 tempFile.fileName(),
146 -1,
147 KIO::Overwrite | KIO::HideProgressInfo);
148 if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
149 // The non-local file could not be downloaded
150 return false;
151 }
152 path = tempFile.fileName();
153 } else {
154 // No temporary file could be created for downloading non-local files
155 return false;
156 }
157
158 QFile file(path);
159 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
160 return false;
161 }
162
163 QTextStream in(&file);
164 while (!in.atEnd()) {
165 const QString line = in.readLine();
166 if (line.contains(*m_regExp)) {
167 return true;
168 }
169 }
170
171 return false;
172 }
173
174 void FileNameSearchProtocol::cleanup()
175 {
176 delete m_regExp;
177 m_regExp = 0;
178 m_iteratedDirs.clear();
179 }
180
181 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
182 {
183 KComponentData instance("kio_search");
184 QCoreApplication app(argc, argv);
185
186 if (argc != 4) {
187 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
188 exit(-1);
189 }
190
191 FileNameSearchProtocol slave(argv[2], argv[3]);
192 slave.dispatchLoop();
193
194 return 0;
195 }