]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
DolphinTreeViewTest: Add unit test for bug 259656 (multiple file
[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 delete dirLister;
106 dirLister = 0;
107
108 // Recursively iterate all sub directories
109 foreach (const KUrl& pendingDir, pendingDirs) {
110 searchDirectory(pendingDir);
111 }
112 }
113
114 bool FileNameSearchProtocol::contentContainsPattern(const KUrl& fileName) const
115 {
116 Q_ASSERT(m_regExp != 0);
117
118 QString path;
119 KTemporaryFile tempFile;
120
121 if (fileName.isLocalFile()) {
122 path = fileName.path();
123 } else if (tempFile.open()) {
124 KIO::Job* getJob = KIO::file_copy(fileName,
125 tempFile.fileName(),
126 -1,
127 KIO::Overwrite | KIO::HideProgressInfo);
128 if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
129 // The non-local file could not be downloaded
130 return false;
131 }
132 path = tempFile.fileName();
133 } else {
134 // No temporary file could be created for downloading non-local files
135 return false;
136 }
137
138 QFile file(path);
139 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
140 return false;
141 }
142
143 QTextStream in(&file);
144 while (!in.atEnd()) {
145 const QString line = in.readLine();
146 if (line.contains(*m_regExp)) {
147 return true;
148 }
149 }
150
151 return false;
152 }
153
154 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
155 {
156 KComponentData instance("kio_search");
157 QCoreApplication app(argc, argv);
158
159 if (argc != 4) {
160 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
161 exit(-1);
162 }
163
164 FileNameSearchProtocol slave(argv[2], argv[3]);
165 slave.dispatchLoop();
166
167 return 0;
168 }