1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2012-2015 Vishesh Handa <me@vhanda.in> |
3 | |
4 | SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL |
5 | */ |
6 | |
7 | #include <QFile> |
8 | |
9 | #include <KLocalizedString> |
10 | #include <QFileInfo> |
11 | #include <QTextStream> |
12 | |
13 | #include "database.h" |
14 | #include "global.h" |
15 | #include "indexer.h" |
16 | #include "transaction.h" |
17 | |
18 | #include "indexentry.h" |
19 | |
20 | using namespace Baloo; |
21 | |
22 | // IndexEntry Constructors |
23 | // Wraps Transaction, presuming ReadWrite access |
24 | |
25 | IndexEntry::IndexEntry(const Database &db, QTextStream &out) |
26 | : Transaction(db, TransactionType::ReadWrite) |
27 | , m_out(out) {}; |
28 | |
29 | IndexEntry::IndexEntry(Database *db, QTextStream &out) |
30 | : IndexEntry(*db, out) {}; |
31 | |
32 | // Index the given file. |
33 | // Done in the foreground, the user will have to wait until any content |
34 | // indexing done (which for a big file can take a while) |
35 | |
36 | bool IndexEntry::indexFileNow(const QString &fileName) |
37 | { |
38 | const QFileInfo fileInfo = QFileInfo(fileName); |
39 | if (!fileInfo.exists()) { |
40 | m_out << "Could not stat file: "<< fileName << '\n'; |
41 | return false; |
42 | } |
43 | |
44 | const QString canonicalPath = fileInfo.canonicalFilePath(); |
45 | quint64 id = filePathToId(filePath: QFile::encodeName(fileName: canonicalPath)); |
46 | if (id == 0) { |
47 | m_out << "Skipping: "<< canonicalPath << " Reason: Bad Document Id for file\n"; |
48 | return false; |
49 | } |
50 | |
51 | if (this->inPhaseOne(id)) { |
52 | m_out << "Skipping: "<< canonicalPath << " Reason: Already scheduled for indexing\n"; |
53 | return false; |
54 | } |
55 | |
56 | if (!this->documentData(id).isEmpty()) { |
57 | m_out << "Skipping: "<< canonicalPath << " Reason: Already indexed\n"; |
58 | return false; |
59 | } |
60 | |
61 | Indexer indexer(canonicalPath, this); |
62 | m_out << "Indexing "<< canonicalPath << '\n'; |
63 | indexer.index(); |
64 | return true; |
65 | } |
66 |