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