| 1 | /* |
| 2 | This file is part of the KDE Baloo Project |
| 3 | SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <vhanda@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "result.h" |
| 9 | #include "propertydata.h" |
| 10 | |
| 11 | #include <QJsonDocument> |
| 12 | #include <QJsonObject> |
| 13 | |
| 14 | #include <QDateTime> |
| 15 | #include <KFileMetaData/PropertyInfo> |
| 16 | #include <KFileMetaData/TypeInfo> |
| 17 | |
| 18 | // In order to use it in a vector |
| 19 | Result::Result() |
| 20 | : ExtractionResult(QString(), QString()) |
| 21 | , m_termGen(m_doc) |
| 22 | , m_termGenForText(m_doc) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | Result::(const QString& url, const QString& mimetype, const Flags& flags) |
| 27 | : KFileMetaData::ExtractionResult(url, mimetype, flags) |
| 28 | , m_termGen(m_doc) |
| 29 | , m_termGenForText(m_doc) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | void Result::add(KFileMetaData::Property::Property property, const QVariant& value) |
| 34 | { |
| 35 | if (value.typeId() == QMetaType::QStringList) { |
| 36 | const auto valueList = value.toStringList(); |
| 37 | for (const auto& val : valueList) { |
| 38 | m_map.insert(key: property, value: val); |
| 39 | } |
| 40 | } else { |
| 41 | m_map.insert(key: property, value); |
| 42 | } |
| 43 | |
| 44 | int propNum = static_cast<int>(property); |
| 45 | QByteArray prefix = 'X' + QByteArray::number(propNum) + '-'; |
| 46 | |
| 47 | if (value.typeId() == QMetaType::Bool) { |
| 48 | m_doc.addTerm(term: prefix); |
| 49 | } else if (value.typeId() == QMetaType::Int || value.typeId() == QMetaType::UInt) { |
| 50 | const QByteArray term = prefix + value.toString().toUtf8(); |
| 51 | m_doc.addTerm(term); |
| 52 | } else if (value.typeId() == QMetaType::QDate) { |
| 53 | const QByteArray term = prefix + value.toDate().toString(format: Qt::ISODate).toUtf8(); |
| 54 | m_doc.addTerm(term); |
| 55 | } else if (value.typeId() == QMetaType::QDateTime) { |
| 56 | const QByteArray term = prefix + value.toDateTime().toString(format: Qt::ISODate).toUtf8(); |
| 57 | m_doc.addTerm(term); |
| 58 | } else if (value.typeId() == QMetaType::QStringList) { |
| 59 | bool shouldBeIndexed = KFileMetaData::PropertyInfo(property).shouldBeIndexed(); |
| 60 | const auto valueList = value.toStringList(); |
| 61 | for (const auto& val : valueList) |
| 62 | { |
| 63 | if (val.isEmpty()) { |
| 64 | continue; |
| 65 | } |
| 66 | m_termGen.indexText(text: val, prefix); |
| 67 | if (shouldBeIndexed) { |
| 68 | m_termGen.indexText(text: val); |
| 69 | } |
| 70 | } |
| 71 | } else { |
| 72 | const QString val = value.toString(); |
| 73 | if (val.isEmpty()) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | m_termGen.indexText(text: val, prefix); |
| 78 | KFileMetaData::PropertyInfo pi(property); |
| 79 | if (pi.shouldBeIndexed()) { |
| 80 | m_termGen.indexText(text: val); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | void Result::append(const QString& text) |
| 86 | { |
| 87 | m_termGenForText.indexText(text); |
| 88 | } |
| 89 | |
| 90 | void Result::addType(KFileMetaData::Type::Type type) |
| 91 | { |
| 92 | QByteArray num = QByteArray::number(static_cast<int>(type)); |
| 93 | m_doc.addTerm(term: QByteArray("T" ) + num); |
| 94 | } |
| 95 | |
| 96 | void Result::finish() |
| 97 | { |
| 98 | if (m_map.isEmpty()) { |
| 99 | m_doc.setData(QByteArray()); |
| 100 | return; |
| 101 | } |
| 102 | QJsonObject jo = Baloo::propertyMapToJson(properties: m_map); |
| 103 | QJsonDocument jdoc; |
| 104 | jdoc.setObject(jo); |
| 105 | m_doc.setData(jdoc.toJson(format: QJsonDocument::JsonFormat::Compact)); |
| 106 | } |
| 107 | |
| 108 | void Result::setDocument(const Baloo::Document& doc) |
| 109 | { |
| 110 | m_doc = doc; |
| 111 | // All document metadata are indexed from position 1000 |
| 112 | m_termGen.setDocument(m_doc); |
| 113 | m_termGen.setPosition(1000); |
| 114 | |
| 115 | // All document plain text starts from 10000. This is done to avoid |
| 116 | // clashes with the term positions |
| 117 | m_termGenForText.setDocument(m_doc); |
| 118 | m_termGenForText.setPosition(10000); |
| 119 | } |
| 120 | |