1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/***************************************************************************
* Copyright (C) 2010 by Peter Penz <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include "filenamesearchprotocol.h"
#include <KComponentData>
#include <KDirLister>
#include <KFileItem>
#include <KIO/NetAccess>
#include <KIO/Job>
#include <KUrl>
#include <kdemacros.h>
#include <ktemporaryfile.h>
#include <QCoreApplication>
#include <QEventLoop>
#include <QRegExp>
FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) :
SlaveBase("search", pool, app),
m_checkContent(false),
m_regExp(0),
m_iteratedDirs()
{
}
FileNameSearchProtocol::~FileNameSearchProtocol()
{
cleanup();
}
void FileNameSearchProtocol::listDir(const KUrl& url)
{
cleanup();
const QString search = url.queryItem("search");
if (!search.isEmpty()) {
m_regExp = new QRegExp(search, Qt::CaseInsensitive, QRegExp::Wildcard);
}
m_checkContent = false;
const QString checkContent = url.queryItem("checkContent");
if (checkContent == QLatin1String("yes")) {
m_checkContent = true;
}
const QString urlString = url.queryItem("url");
searchDirectory(KUrl(urlString));
cleanup();
finished();
}
void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
{
if (directory.path() == QLatin1String("/proc")) {
// Don't try to iterate the /proc directory of Linux
return;
}
// Get all items of the directory
KDirLister *dirLister = new KDirLister();
dirLister->setDelayedMimeTypes(false);
dirLister->setAutoErrorHandlingEnabled(false, 0);
dirLister->openUrl(directory);
QEventLoop eventLoop;
QObject::connect(dirLister, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
QObject::connect(dirLister, SIGNAL(completed()), &eventLoop, SLOT(quit()));
eventLoop.exec();
// Visualize all items that match the search pattern
QList<KUrl> pendingDirs;
const KFileItemList items = dirLister->items();
foreach (const KFileItem& item, items) {
bool addItem = false;
if (!m_regExp || item.name().contains(*m_regExp)) {
addItem = true;
} else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) {
addItem = contentContainsPattern(item.url());
}
if (addItem) {
KIO::UDSEntry entry = item.entry();
entry.insert(KIO::UDSEntry::UDS_URL, item.url().url() );
listEntry(entry,false);
}
if (item.isDir()) {
if (item.isLink()) {
// Assure that no endless searching is done in directories that
// have already been iterated.
const KUrl linkDest(item.url(), item.linkDest());
if (!m_iteratedDirs.contains(linkDest.path())) {
pendingDirs.append(linkDest);
}
} else {
pendingDirs.append(item.url());
}
}
}
listEntry(KIO::UDSEntry(), true);
m_iteratedDirs.insert(directory.path());
delete dirLister;
dirLister = 0;
// Recursively iterate all sub directories
foreach (const KUrl& pendingDir, pendingDirs) {
searchDirectory(pendingDir);
}
}
bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
{
Q_ASSERT(m_regExp);
QString path;
KTemporaryFile tempFile;
if (fileName.isLocalFile()) {
path = fileName.path();
} else if (tempFile.open()) {
KIO::Job* getJob = KIO::file_copy(fileName,
tempFile.fileName(),
-1,
KIO::Overwrite | KIO::HideProgressInfo);
if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
// The non-local file could not be downloaded
return false;
}
path = tempFile.fileName();
} else {
// No temporary file could be created for downloading non-local files
return false;
}
QFile file(path);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}
QTextStream in(&file);
while (!in.atEnd()) {
const QString line = in.readLine();
if (line.contains(*m_regExp)) {
return true;
}
}
return false;
}
void FileNameSearchProtocol::cleanup()
{
delete m_regExp;
m_regExp = 0;
m_iteratedDirs.clear();
}
extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
{
KComponentData instance("kio_search");
QCoreApplication app(argc, argv);
if (argc != 4) {
fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
exit(-1);
}
FileNameSearchProtocol slave(argv[2], argv[3]);
slave.dispatchLoop();
return 0;
}
|