]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
Merge remote-tracking branch 'origin/master' into frameworks
[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 addItem = contentContainsPattern(item.url());
97 }
98
99 if (addItem) {
100 KIO::UDSEntry entry = item.entry();
101 entry.insert(KIO::UDSEntry::UDS_URL, item.url().url() );
102 listEntry(entry,false);
103 }
104
105 if (item.isDir()) {
106 if (item.isLink()) {
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);
112 }
113 } else {
114 pendingDirs.append(item.url());
115 }
116 }
117 }
118 listEntry(KIO::UDSEntry(), true);
119
120 m_iteratedDirs.insert(directory.path());
121
122 delete dirLister;
123 dirLister = 0;
124
125 // Recursively iterate all sub directories
126 foreach (const KUrl& pendingDir, pendingDirs) {
127 searchDirectory(pendingDir);
128 }
129 }
130
131 bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
132 {
133 Q_ASSERT(m_regExp);
134
135 QString path;
136 KTemporaryFile tempFile;
137
138 if (fileName.isLocalFile()) {
139 path = fileName.path();
140 } else if (tempFile.open()) {
141 KIO::Job* getJob = KIO::file_copy(fileName,
142 tempFile.fileName(),
143 -1,
144 KIO::Overwrite | KIO::HideProgressInfo);
145 if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
146 // The non-local file could not be downloaded
147 return false;
148 }
149 path = tempFile.fileName();
150 } else {
151 // No temporary file could be created for downloading non-local files
152 return false;
153 }
154
155 QFile file(path);
156 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
157 return false;
158 }
159
160 QTextStream in(&file);
161 while (!in.atEnd()) {
162 const QString line = in.readLine();
163 if (line.contains(*m_regExp)) {
164 return true;
165 }
166 }
167
168 return false;
169 }
170
171 void FileNameSearchProtocol::cleanup()
172 {
173 delete m_regExp;
174 m_regExp = 0;
175 m_iteratedDirs.clear();
176 }
177
178 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
179 {
180 KComponentData instance("kio_search");
181 QCoreApplication app(argc, argv);
182
183 if (argc != 4) {
184 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
185 exit(-1);
186 }
187
188 FileNameSearchProtocol slave(argv[2], argv[3]);
189 slave.dispatchLoop();
190
191 return 0;
192 }