]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
Allow to search for filenames + textcontent also for non-local files.
[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.h>
23 #include <kdirlister.h>
24 #include <kfileitem.h>
25 #include <kio/netaccess.h>
26 #include <kio/job.h>
27 #include <kurl.h>
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 {
39 }
40
41 FileNameSearchProtocol::~FileNameSearchProtocol()
42 {
43 delete m_regExp;
44 m_regExp = 0;
45 }
46
47 void FileNameSearchProtocol::listDir(const KUrl& url)
48 {
49 delete m_regExp;
50 m_regExp = 0;
51
52 const QString search = url.queryItem("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.queryItem("checkContent");
59 if (checkContent == QLatin1String("yes")) {
60 m_checkContent = true;
61 }
62
63 const QString urlString = url.queryItem("url");
64 searchDirectory(KUrl(urlString));
65
66 finished();
67 }
68
69 void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
70 {
71 // Get all items of the directory
72 KDirLister *dirLister = new KDirLister();
73 dirLister->setDelayedMimeTypes(false);
74 dirLister->setAutoErrorHandlingEnabled(false, 0);
75 dirLister->openUrl(directory);
76
77 QEventLoop eventLoop;
78 QObject::connect(dirLister, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
79 QObject::connect(dirLister, SIGNAL(completed()), &eventLoop, SLOT(quit()));
80 eventLoop.exec();
81
82 // Visualize all items that match the search pattern
83 QList<KUrl> pendingDirs;
84 const KFileItemList items = dirLister->items();
85 foreach (const KFileItem& item, items) {
86 bool addItem = false;
87 if ((m_regExp == 0) || item.name().contains(*m_regExp)) {
88 addItem = true;
89 } else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) {
90 addItem = contentContainsPattern(item.url());
91 }
92
93 if (addItem) {
94 KIO::UDSEntry entry = item.entry();
95 entry.insert(KIO::UDSEntry::UDS_URL, item.url().url() );
96 listEntry(entry,false);
97 }
98
99 if (item.isDir()) {
100 pendingDirs.append(item.url());
101 }
102 }
103 listEntry(KIO::UDSEntry(), true);
104
105 // Recursively iterate all sub directories
106 foreach (const KUrl& pendingDir, pendingDirs) {
107 searchDirectory(pendingDir);
108 }
109 }
110
111 bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
112 {
113 Q_ASSERT(m_regExp != 0);
114
115 QString path;
116 KTemporaryFile tempFile;
117
118 if (fileName.isLocalFile()) {
119 path = fileName.path();
120 } else if (tempFile.open()) {
121 KIO::Job* getJob = KIO::file_copy(fileName,
122 tempFile.fileName(),
123 -1,
124 KIO::Overwrite | KIO::HideProgressInfo);
125 if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
126 // The non-local file could not be downloaded
127 return false;
128 }
129 path = tempFile.fileName();
130 } else {
131 // No temporary file could be created for downloading non-local files
132 return false;
133 }
134
135 QFile file(path);
136 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
137 return false;
138 }
139
140 QTextStream in(&file);
141 while (!in.atEnd()) {
142 const QString line = in.readLine();
143 if (line.contains(*m_regExp)) {
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
152 {
153 KComponentData instance("kio_search");
154 QCoreApplication app(argc, argv);
155
156 if (argc != 4) {
157 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
158 exit(-1);
159 }
160
161 FileNameSearchProtocol slave(argv[2], argv[3]);
162 slave.dispatchLoop();
163
164 return 0;
165 }