Changeset View
Changeset View
Standalone View
Standalone View
krusader/FileSystem/filesearcher.cpp
- This file was moved from krusader/Search/krsearchmod.cpp.
Show All 13 Lines | |||||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
16 | * GNU General Public License for more details. * | 16 | * GNU General Public License for more details. * | ||
17 | * * | 17 | * * | ||
18 | * You should have received a copy of the GNU General Public License * | 18 | * You should have received a copy of the GNU General Public License * | ||
19 | * along with Krusader. If not, see [http://www.gnu.org/licenses/]. * | 19 | * along with Krusader. If not, see [http://www.gnu.org/licenses/]. * | ||
20 | *****************************************************************************/ | 20 | *****************************************************************************/ | ||
21 | 21 | | |||
22 | #include "krsearchmod.h" | 22 | #include "filesearcher.h" | ||
23 | 23 | | |||
24 | // QtCore | 24 | // QtCore | ||
25 | #include <QDir> | 25 | #include <QDir> | ||
26 | #include <QMimeDatabase> | 26 | #include <QMimeDatabase> | ||
27 | #include <QMimeType> | 27 | #include <QMimeType> | ||
28 | #include <QRegExp> | 28 | #include <QRegExp> | ||
29 | // QtWidgets | 29 | // QtWidgets | ||
30 | #include <QApplication> | 30 | #include <QApplication> | ||
Show All 9 Lines | |||||
40 | #include "../FileSystem/virtualfilesystem.h" | 40 | #include "../FileSystem/virtualfilesystem.h" | ||
41 | 41 | | |||
42 | #define EVENT_PROCESS_DELAY 250 // milliseconds | 42 | #define EVENT_PROCESS_DELAY 250 // milliseconds | ||
43 | 43 | | |||
44 | static const QStringList TAR_TYPES = QStringList() << "tbz" << "tgz" << "tarz" << "tar" << "tlz"; | 44 | static const QStringList TAR_TYPES = QStringList() << "tbz" << "tgz" << "tarz" << "tar" << "tlz"; | ||
45 | 45 | | |||
46 | extern KRarcHandler arcHandler; | 46 | extern KRarcHandler arcHandler; | ||
47 | 47 | | |||
48 | KRSearchMod::KRSearchMod(const KRQuery *query) | 48 | FileSearcher::FileSearcher(const KRQuery &query) | ||
49 | : m_defaultFileSystem(nullptr), m_virtualFileSystem(nullptr), m_stopSearch(false) | 49 | : m_query(query), m_defaultFileSystem(nullptr), m_virtualFileSystem(nullptr) | ||
50 | { | 50 | { | ||
51 | m_query = new KRQuery(*query); | 51 | connect(&m_query, &KRQuery::status, this, &FileSearcher::searching); | ||
52 | connect(m_query, &KRQuery::status, this, &KRSearchMod::searching); | 52 | connect(&m_query, &KRQuery::processEvents, this, &FileSearcher::slotProcessEvents); | ||
53 | connect(m_query, &KRQuery::processEvents, this, &KRSearchMod::slotProcessEvents); | | |||
54 | } | 53 | } | ||
55 | 54 | | |||
56 | KRSearchMod::~KRSearchMod() | 55 | FileSearcher::~FileSearcher() | ||
57 | { | 56 | { | ||
58 | delete m_query; | | |||
59 | if (m_defaultFileSystem) | 57 | if (m_defaultFileSystem) | ||
60 | delete m_defaultFileSystem; | 58 | delete m_defaultFileSystem; | ||
61 | if (m_virtualFileSystem) | 59 | if (m_virtualFileSystem) | ||
62 | delete m_virtualFileSystem; | 60 | delete m_virtualFileSystem; | ||
63 | } | 61 | } | ||
64 | 62 | | |||
65 | void KRSearchMod::start() | 63 | void FileSearcher::start(const QUrl &url) | ||
66 | { | 64 | { | ||
65 | m_foundFiles.clear(); | ||||
67 | m_unScannedUrls.clear(); | 66 | m_unScannedUrls.clear(); | ||
68 | m_scannedUrls.clear(); | 67 | m_scannedUrls.clear(); | ||
69 | m_timer.start(); | 68 | m_timer.start(); | ||
70 | 69 | m_stopSearch = false; | |||
71 | const QList<QUrl> whereToSearch = m_query->searchInDirs(); | | |||
72 | 70 | | |||
73 | // search every dir that needs to be searched | 71 | // search every dir that needs to be searched | ||
74 | for (int i = 0; i < whereToSearch.count(); ++i) | 72 | const QList<QUrl> urls = url.isEmpty() ? m_query.searchInDirs() : QList<QUrl>() << url; | ||
75 | scanUrl(whereToSearch[i]); | 73 | for (const QUrl url : urls) { | ||
74 | if (m_stopSearch) | ||||
75 | break; | ||||
76 | | ||||
77 | scanUrl(url); | ||||
78 | } | ||||
76 | 79 | | |||
77 | emit finished(); | 80 | emit finished(); | ||
78 | } | 81 | } | ||
79 | 82 | | |||
80 | void KRSearchMod::stop() { m_stopSearch = true; } | 83 | void FileSearcher::stop() { m_stopSearch = true; } | ||
81 | 84 | | |||
82 | void KRSearchMod::scanUrl(const QUrl &url) | 85 | void FileSearcher::scanUrl(const QUrl &url) | ||
83 | { | 86 | { | ||
84 | if (m_stopSearch) | | |||
85 | return; | | |||
86 | 87 | | |||
87 | m_unScannedUrls.push(url); | 88 | m_unScannedUrls.push(url); | ||
88 | while (!m_unScannedUrls.isEmpty()) { | 89 | while (!m_unScannedUrls.isEmpty()) { | ||
89 | const QUrl url = m_unScannedUrls.pop(); | | |||
90 | | ||||
91 | if (m_stopSearch) | 90 | if (m_stopSearch) | ||
92 | return; | 91 | return; | ||
93 | 92 | | |||
94 | if (m_query->isExcluded(url)) { | 93 | const QUrl url = m_unScannedUrls.pop(); | ||
95 | if (!m_query->searchInDirs().contains(url)) | 94 | | ||
95 | if (m_query.isExcluded(url)) { | ||||
96 | if (!m_query.searchInDirs().contains(url)) | ||||
96 | continue; | 97 | continue; | ||
97 | } | 98 | } | ||
98 | 99 | | |||
99 | if (m_scannedUrls.contains(url)) | 100 | if (m_scannedUrls.contains(url)) | ||
100 | // avoid endless loop | 101 | // avoid endless loop | ||
101 | continue; | 102 | continue; | ||
102 | 103 | | |||
103 | m_scannedUrls.push(url); | 104 | m_scannedUrls.push(url); | ||
104 | 105 | | |||
105 | emit searching(url.toDisplayString(QUrl::PreferLocalFile)); | 106 | emit searching(url.toDisplayString(QUrl::PreferLocalFile)); | ||
106 | 107 | | |||
107 | scanDirectory(url); | 108 | scanDirectory(url); | ||
108 | } | 109 | } | ||
109 | } | 110 | } | ||
110 | 111 | | |||
111 | void KRSearchMod::scanDirectory(const QUrl &url) | 112 | void FileSearcher::scanDirectory(const QUrl &url) | ||
112 | { | 113 | { | ||
113 | FileSystem *fileSystem = getFileSystem(url); | 114 | FileSystem *fileSystem = getFileSystem(url); | ||
114 | 115 | | |||
115 | // create file items | 116 | // create file items, deletes old ones | ||
116 | const bool refreshed = fileSystem->scanDir(url); | 117 | const bool refreshed = fileSystem->scanDir(url); | ||
117 | if (!refreshed) { | 118 | if (!refreshed) { | ||
118 | emit error(url); | 119 | emit error(url); | ||
119 | return; | 120 | return; | ||
120 | } | 121 | } | ||
121 | 122 | | |||
122 | for (FileItem *fileItem : fileSystem->fileItems()) { | 123 | for (FileItem *fileItem : fileSystem->fileItems()) { | ||
123 | const QUrl fileUrl = fileItem->getUrl(); | 124 | const QUrl fileUrl = fileItem->getUrl(); | ||
124 | 125 | | |||
125 | if (m_query->isRecursive() && | 126 | if (m_query.ignoreHidden() && fileUrl.fileName().startsWith('.')) | ||
126 | (fileItem->isDir() || (fileItem->isSymLink() && m_query->followLinks()))) { | 127 | // ignore hidden files and directories | ||
128 | continue; | ||||
129 | | ||||
130 | if (m_query.isRecursive() && | ||||
131 | (fileItem->isDir() || (fileItem->isSymLink() && m_query.followLinks()))) { | ||||
127 | // query search in subdirectory | 132 | // query search in subdirectory | ||
128 | m_unScannedUrls.push(fileUrl); | 133 | m_unScannedUrls.push(fileUrl); | ||
129 | } | 134 | } | ||
130 | 135 | | |||
131 | if (m_query->searchInArchives() && fileUrl.isLocalFile() && | 136 | if (m_query.searchInArchives() && fileUrl.isLocalFile() && | ||
132 | KRarcHandler::arcSupported(fileItem->getMime())) { | 137 | KRarcHandler::arcSupported(fileItem->getMime())) { | ||
133 | // query search in archive; NOTE: only supported for local files | 138 | // query search in archive; NOTE: only supported for local files | ||
134 | QUrl archiveURL = fileUrl; | 139 | QUrl archiveURL = fileUrl; | ||
135 | bool encrypted; | 140 | bool encrypted; | ||
136 | const QString type = arcHandler.getType(encrypted, fileUrl.path(), fileItem->getMime()); | 141 | const QString type = arcHandler.getType(encrypted, fileUrl.path(), fileItem->getMime()); | ||
137 | 142 | | |||
138 | if (!encrypted) { | 143 | if (!encrypted) { | ||
139 | archiveURL.setScheme(TAR_TYPES.contains(type) ? "tar" : "krarc"); | 144 | archiveURL.setScheme(TAR_TYPES.contains(type) ? "tar" : "krarc"); | ||
140 | m_unScannedUrls.push(archiveURL); | 145 | m_unScannedUrls.push(archiveURL); | ||
141 | } | 146 | } | ||
142 | } | 147 | } | ||
143 | 148 | | |||
144 | if (m_query->match(fileItem)) { | 149 | if (m_query.match(fileItem)) { | ||
145 | // found! | 150 | // found! | ||
146 | emit found(*fileItem, m_query->foundText()); // emitting copy of file item | 151 | m_foundFiles.append(fileItem); | ||
152 | emit found(*fileItem, m_query.foundText()); // emitting copy of file item | ||||
147 | } | 153 | } | ||
148 | 154 | | |||
149 | if (m_timer.elapsed() >= EVENT_PROCESS_DELAY) { | 155 | if (m_timer.elapsed() >= EVENT_PROCESS_DELAY) { | ||
150 | qApp->processEvents(); | 156 | qApp->processEvents(); | ||
151 | m_timer.start(); | 157 | m_timer.start(); | ||
152 | if (m_stopSearch) | 158 | if (m_stopSearch) | ||
153 | return; | 159 | return; | ||
154 | } | 160 | } | ||
155 | } | 161 | } | ||
156 | } | 162 | } | ||
157 | 163 | | |||
158 | FileSystem *KRSearchMod::getFileSystem(const QUrl &url) | 164 | FileSystem *FileSearcher::getFileSystem(const QUrl &url) | ||
159 | { | 165 | { | ||
160 | FileSystem *fileSystem; | 166 | FileSystem *fileSystem; | ||
161 | if (url.scheme() == QStringLiteral("virt")) { | 167 | if (url.scheme() == QStringLiteral("virt")) { | ||
162 | if (!m_virtualFileSystem) | 168 | if (!m_virtualFileSystem) | ||
163 | m_virtualFileSystem = new VirtualFileSystem(); | 169 | m_virtualFileSystem = new VirtualFileSystem(); | ||
164 | fileSystem = m_virtualFileSystem; | 170 | fileSystem = m_virtualFileSystem; | ||
165 | } else { | 171 | } else { | ||
166 | if (!m_defaultFileSystem) | 172 | if (!m_defaultFileSystem) | ||
167 | m_defaultFileSystem = new DefaultFileSystem(); | 173 | m_defaultFileSystem = new DefaultFileSystem(); | ||
168 | fileSystem = m_defaultFileSystem; | 174 | fileSystem = m_defaultFileSystem; | ||
169 | } | 175 | } | ||
170 | return fileSystem; | 176 | return fileSystem; | ||
171 | } | 177 | } | ||
172 | 178 | | |||
173 | void KRSearchMod::slotProcessEvents(bool &stopped) | 179 | void FileSearcher::slotProcessEvents(bool &stopped) | ||
174 | { | 180 | { | ||
175 | qApp->processEvents(); | 181 | qApp->processEvents(); | ||
176 | stopped = m_stopSearch; | 182 | stopped = m_stopSearch; | ||
177 | } | 183 | } |