]> cloud.milkyroute.net Git - dolphin.git/blob - src/search/filenamesearchprotocol.cpp
Unify the search interface for non-indexed and indexed folders
[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
30 FileNameSearchProtocol::FileNameSearchProtocol( const QByteArray &pool, const QByteArray &app ) :
31 SlaveBase("search", pool, app),
32 m_checkContent(false),
33 m_searchPattern()
34 {
35 }
36
37 FileNameSearchProtocol::~FileNameSearchProtocol()
38 {
39 }
40
41 void FileNameSearchProtocol::listDir(const KUrl& url)
42 {
43 const QStringList searchValues = url.allQueryItemValues("search");
44 m_searchPattern.clear();
45 if (!searchValues.isEmpty()) {
46 m_searchPattern = searchValues.first();
47 }
48
49 m_checkContent = false;
50 const QStringList checkContentValues = url.allQueryItemValues("checkContent");
51 if (!checkContentValues.isEmpty() && (checkContentValues.first() == QLatin1String("yes"))) {
52 m_checkContent = true;
53 }
54
55 KUrl directory = url;
56 directory.setProtocol("file");
57 directory.setEncodedQuery(QByteArray());
58
59 searchDirectory(directory);
60
61 finished();
62 }
63
64 void FileNameSearchProtocol::searchDirectory(const KUrl& directory)
65 {
66 // Get all items of the directory
67 KDirLister *dirLister = new KDirLister();
68 dirLister->setDelayedMimeTypes(false);
69 dirLister->setAutoErrorHandlingEnabled(false, 0);
70 dirLister->openUrl(directory);
71
72 QEventLoop eventLoop;
73 QObject::connect(dirLister, SIGNAL(canceled()), &eventLoop, SLOT(quit()));
74 QObject::connect(dirLister, SIGNAL(completed()), &eventLoop, SLOT(quit()));
75 eventLoop.exec();
76
77 // Visualize all items that match the search pattern
78 QList<KUrl> pendingDirs;
79 const KFileItemList items = dirLister->items();
80 foreach (const KFileItem& item, items) {
81 bool addItem = false;
82 if (m_searchPattern.isEmpty() || item.name().contains(m_searchPattern, Qt::CaseInsensitive)) {
83 addItem = true;
84 } else if (m_checkContent && item.mimetype().startsWith(QLatin1String("text/"))) {
85 addItem = containsPattern(item.url());
86 }
87
88 if (addItem) {
89 KIO::UDSEntry entry = item.entry();
90 entry.insert(KIO::UDSEntry::UDS_URL, item.url().url() );
91 listEntry(entry,false);
92 }
93
94 if (item.isDir()) {
95 pendingDirs.append(item.url());
96 }
97 }
98 listEntry(KIO::UDSEntry(), true);
99
100 // Recursively iterate all sub directories
101 foreach (const KUrl& pendingDir, pendingDirs) {
102 searchDirectory(pendingDir);
103 }
104 }
105
106 bool FileNameSearchProtocol::containsPattern(const KUrl& fileName) const
107 {
108 QFile file(fileName.path());
109 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
110 return false;
111 }
112
113 QTextStream in(&file);
114 while (!in.atEnd()) {
115 const QString line = in.readLine();
116 if (line.contains(m_searchPattern)) {
117 return true;
118 }
119 }
120
121 return false;
122 }
123
124 extern "C" int KDE_EXPORT kdemain( int argc, char **argv )
125 {
126 KComponentData instance("kio_search");
127 QCoreApplication app(argc, argv);
128
129 if (argc != 4) {
130 fprintf(stderr, "Usage: kio_filenamesearch protocol domain-socket1 domain-socket2\n");
131 exit(-1);
132 }
133
134 FileNameSearchProtocol slave(argv[2], argv[3]);
135 slave.dispatchLoop();
136
137 return 0;
138 }