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