Changeset View
Changeset View
Standalone View
Standalone View
src/file/basicindexingjob.cpp
Show All 22 Lines | |||||
23 | #include "basicindexingjob.h" | 23 | #include "basicindexingjob.h" | ||
24 | #include "termgenerator.h" | 24 | #include "termgenerator.h" | ||
25 | #include "idutils.h" | 25 | #include "idutils.h" | ||
26 | 26 | | |||
27 | #include <QFileInfo> | 27 | #include <QFileInfo> | ||
28 | #include <QDateTime> | 28 | #include <QDateTime> | ||
29 | #include <QStringList> | 29 | #include <QStringList> | ||
30 | 30 | | |||
31 | #include <KFileMetaData/Types> | ||||
31 | #include <KFileMetaData/TypeInfo> | 32 | #include <KFileMetaData/TypeInfo> | ||
32 | #include <KFileMetaData/UserMetaData> | 33 | #include <KFileMetaData/UserMetaData> | ||
33 | 34 | | |||
34 | using namespace Baloo; | 35 | using namespace Baloo; | ||
35 | 36 | | |||
36 | BasicIndexingJob::BasicIndexingJob(const QString& filePath, const QString& mimetype, | 37 | BasicIndexingJob::BasicIndexingJob(const QString& filePath, const QString& mimetype, | ||
37 | IndexingLevel level) | 38 | IndexingLevel level) | ||
38 | : m_filePath(filePath) | 39 | : m_filePath(filePath) | ||
Show All 20 Lines | 59 | if (rating) { | |||
59 | doc.addXattrTerm(QByteArray("R") + QByteArray::number(rating)); | 60 | doc.addXattrTerm(QByteArray("R") + QByteArray::number(rating)); | ||
60 | } | 61 | } | ||
61 | 62 | | |||
62 | QString comment = userMetaData.userComment(); | 63 | QString comment = userMetaData.userComment(); | ||
63 | if (!comment.isEmpty()) { | 64 | if (!comment.isEmpty()) { | ||
64 | tg.indexXattrText(comment, QByteArray("C")); | 65 | tg.indexXattrText(comment, QByteArray("C")); | ||
65 | } | 66 | } | ||
66 | } | 67 | } | ||
67 | } // namespace | | |||
68 | | ||||
69 | BasicIndexingJob::~BasicIndexingJob() | | |||
70 | { | | |||
71 | } | | |||
72 | | ||||
73 | bool BasicIndexingJob::index() | | |||
74 | { | | |||
75 | const QByteArray url = QFile::encodeName(m_filePath); | | |||
76 | | ||||
77 | QT_STATBUF statBuf; | | |||
78 | if (filePathToStat(url, statBuf) != 0) { | | |||
79 | return false; | | |||
80 | } | | |||
81 | | ||||
82 | Document doc; | | |||
83 | doc.setId(statBufToId(statBuf)); | | |||
84 | doc.setUrl(url); | | |||
85 | | ||||
86 | QString fileName = url.mid(url.lastIndexOf('/') + 1); | | |||
87 | | ||||
88 | TermGenerator tg(doc); | | |||
89 | tg.indexFileNameText(fileName); | | |||
90 | tg.indexFileNameText(fileName, QByteArray("F")); | | |||
91 | tg.indexText(m_mimetype, QByteArray("M")); | | |||
92 | | ||||
93 | // (Content) Modification time, Metadata (e.g. XAttr) change time | | |||
94 | doc.setMTime(statBuf.st_mtime); | | |||
95 | doc.setCTime(statBuf.st_ctime); | | |||
96 | | ||||
97 | // Types | | |||
98 | QVector<KFileMetaData::Type::Type> tList = typesForMimeType(m_mimetype); | | |||
99 | for (KFileMetaData::Type::Type type : tList) { | | |||
100 | QByteArray num = QByteArray::number(static_cast<int>(type)); | | |||
101 | doc.addTerm(QByteArray("T") + num); | | |||
102 | } | | |||
103 | | ||||
104 | if (S_ISDIR(statBuf.st_mode)) { | | |||
105 | static const QByteArray type = QByteArray("T") + QByteArray::number(static_cast<int>(KFileMetaData::Type::Folder)); | | |||
106 | doc.addTerm(type); | | |||
107 | // For folders we do not need to go through file indexing, so we do not set contentIndexing | | |||
108 | } | | |||
109 | else if (m_indexingLevel == MarkForContentIndexing) { | | |||
110 | doc.setContentIndexing(true); | | |||
111 | } | | |||
112 | | ||||
113 | indexXAttr(m_filePath, doc); | | |||
114 | | ||||
115 | m_doc = doc; | | |||
116 | return true; | | |||
117 | } | | |||
118 | | ||||
119 | 68 | | |||
120 | QVector<KFileMetaData::Type::Type> BasicIndexingJob::typesForMimeType(const QString& mimeType) | 69 | QVector<KFileMetaData::Type::Type> typesForMimeType(const QString& mimeType) | ||
121 | { | 70 | { | ||
122 | using namespace KFileMetaData; | 71 | using namespace KFileMetaData; | ||
123 | QVector<Type::Type> types; | 72 | QVector<Type::Type> types; | ||
73 | types.reserve(2); | ||||
124 | 74 | | |||
125 | // Basic types | 75 | // Basic types | ||
126 | if (mimeType.startsWith(QLatin1String("audio/"))) | 76 | if (mimeType.startsWith(QLatin1String("audio/"))) | ||
127 | types << Type::Audio; | 77 | types << Type::Audio; | ||
128 | if (mimeType.startsWith(QLatin1String("video/"))) | 78 | if (mimeType.startsWith(QLatin1String("video/"))) | ||
129 | types << Type::Video; | 79 | types << Type::Video; | ||
130 | if (mimeType.startsWith(QLatin1String("image/"))) | 80 | if (mimeType.startsWith(QLatin1String("image/"))) | ||
131 | types << Type::Image; | 81 | types << Type::Image; | ||
▲ Show 20 Lines • Show All 77 Lines • ▼ Show 20 Line(s) | 96 | static QMultiHash<QString, Type::Type> typeMapper = { | |||
209 | {"application/wps-office.pptx", Type::Document}, | 159 | {"application/wps-office.pptx", Type::Document}, | ||
210 | {"application/wps-office.pptx", Type::Presentation}, | 160 | {"application/wps-office.pptx", Type::Presentation}, | ||
211 | // Other | 161 | // Other | ||
212 | {"text/markdown", Type::Document}, | 162 | {"text/markdown", Type::Document}, | ||
213 | {"image/vnd.djvu+multipage", Type::Document}, | 163 | {"image/vnd.djvu+multipage", Type::Document}, | ||
214 | {"application/x-lyx", Type::Document} | 164 | {"application/x-lyx", Type::Document} | ||
215 | }; | 165 | }; | ||
216 | 166 | | |||
167 | auto hashIt = typeMapper.find(mimeType); | ||||
168 | while (hashIt != typeMapper.end() && hashIt.key() == mimeType) { | ||||
169 | types.append(hashIt.value()); | ||||
170 | ++hashIt; | ||||
171 | } | ||||
217 | 172 | | |||
218 | types << typeMapper.values(mimeType).toVector(); | | |||
219 | return types; | 173 | return types; | ||
220 | } | 174 | } | ||
175 | } // namespace | ||||
176 | | ||||
177 | BasicIndexingJob::~BasicIndexingJob() | ||||
178 | { | ||||
179 | } | ||||
180 | | ||||
181 | bool BasicIndexingJob::index() | ||||
182 | { | ||||
183 | const QByteArray url = QFile::encodeName(m_filePath); | ||||
184 | | ||||
185 | QT_STATBUF statBuf; | ||||
186 | if (filePathToStat(url, statBuf) != 0) { | ||||
187 | return false; | ||||
188 | } | ||||
189 | | ||||
190 | Document doc; | ||||
191 | doc.setId(statBufToId(statBuf)); | ||||
192 | doc.setUrl(url); | ||||
193 | | ||||
194 | QString fileName = url.mid(url.lastIndexOf('/') + 1); | ||||
195 | | ||||
196 | TermGenerator tg(doc); | ||||
197 | tg.indexFileNameText(fileName); | ||||
198 | tg.indexFileNameText(fileName, QByteArray("F")); | ||||
199 | tg.indexText(m_mimetype, QByteArray("M")); | ||||
221 | 200 | | |||
201 | // (Content) Modification time, Metadata (e.g. XAttr) change time | ||||
202 | doc.setMTime(statBuf.st_mtime); | ||||
203 | doc.setCTime(statBuf.st_ctime); | ||||
204 | | ||||
205 | // Types | ||||
206 | QVector<KFileMetaData::Type::Type> tList = typesForMimeType(m_mimetype); | ||||
207 | for (KFileMetaData::Type::Type type : tList) { | ||||
208 | QByteArray num = QByteArray::number(static_cast<int>(type)); | ||||
209 | doc.addTerm(QByteArray("T") + num); | ||||
210 | } | ||||
211 | | ||||
212 | if (S_ISDIR(statBuf.st_mode)) { | ||||
213 | static const QByteArray type = QByteArray("T") + QByteArray::number(static_cast<int>(KFileMetaData::Type::Folder)); | ||||
214 | doc.addTerm(type); | ||||
215 | // For folders we do not need to go through file indexing, so we do not set contentIndexing | ||||
216 | } | ||||
217 | else if (m_indexingLevel == MarkForContentIndexing) { | ||||
218 | doc.setContentIndexing(true); | ||||
219 | } | ||||
220 | | ||||
221 | indexXAttr(m_filePath, doc); | ||||
222 | | ||||
223 | m_doc = doc; | ||||
224 | return true; | ||||
225 | } |