]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
Allow to use wildcards when searching filenames or simple text files with the filenam...
[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 <kurl.h>
26
27 #include <QCoreApplication>
28 #include <QEventLoop>
29 #include <QRegExp>
30
31 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) :
32 SlaveBase("search", pool, app),
33 m_checkContent(false),
34 m_regExp(0)
35 {
36 }
37
38 FileNameSearchProtocol::~FileNameSearchProtocol()
39 {
40 delete m_regExp;
41 m_regExp = 0;
42 }
43
44 void FileNameSearchProtocol::listDir(const KUrl& url)
45 {
46 delete m_regExp;
47 m_regExp = 0;
48
49 const QStringList searchValues = url.allQueryItemValues("search");
50 if (!searchValues.isEmpty()) {
51 m_regExp = new QRegExp(searchValues.first(), Qt::CaseInsensitive, QRegExp::Wildcard);
52 }
53
54 m_checkContent = false;
55 const QStringList checkContentValues = url.allQueryItemValues("checkContent");
56 if (!checkContentValues.isEmpty() && (checkContentValues.first() == QLatin1String("yes"))) {
57 m_checkContent = true;
58 }
59
60 KUrl directory = url;
61 directory.setProtocol("file");
62 directory.setEncodedQuery(QByteArray());
63
64 searchDirectory(directory);
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 = containsPattern(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::containsPattern(const KUrl& fileName) const
112 {
113 Q_ASSERT(m_regExp != 0);
114
115 QFile file(fileName.path());
116 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
117 return false;
118 }
119
120 QTextStream in(&file);
121 while (!in.atEnd()) {
122 const QString line = in.readLine();
123 if (line.contains(*m_regExp)) {
124 return true;
125 }
126 }
127
128 return false;
129 }
130
131 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
132 {
133 KComponentData instance("kio_search");
134 QCoreApplication app(argc, argv);
135
136 if (argc != 4) {
137 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
138 exit(-1);
139 }
140
141 FileNameSearchProtocol slave(argv[2], argv[3]);
142 slave.dispatchLoop();
143
144 return 0;
145 }