File indexing completed on 2024-04-28 03:51:47
0001 /* 0002 This file is part of the KDE Baloo Project 0003 SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <vhanda@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "result.h" 0009 #include "propertydata.h" 0010 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 0014 #include <QDateTime> 0015 #include <KFileMetaData/PropertyInfo> 0016 #include <KFileMetaData/TypeInfo> 0017 0018 // In order to use it in a vector 0019 Result::Result() 0020 : ExtractionResult(QString(), QString()) 0021 , m_termGen(m_doc) 0022 , m_termGenForText(m_doc) 0023 { 0024 } 0025 0026 Result::Result(const QString& url, const QString& mimetype, const Flags& flags) 0027 : KFileMetaData::ExtractionResult(url, mimetype, flags) 0028 , m_termGen(m_doc) 0029 , m_termGenForText(m_doc) 0030 { 0031 } 0032 0033 void Result::add(KFileMetaData::Property::Property property, const QVariant& value) 0034 { 0035 if (value.typeId() == QMetaType::QStringList) { 0036 const auto valueList = value.toStringList(); 0037 for (const auto& val : valueList) { 0038 m_map.insert(property, val); 0039 } 0040 } else { 0041 m_map.insert(property, value); 0042 } 0043 0044 int propNum = static_cast<int>(property); 0045 QByteArray prefix = 'X' + QByteArray::number(propNum) + '-'; 0046 0047 if (value.typeId() == QMetaType::Bool) { 0048 m_doc.addTerm(prefix); 0049 } else if (value.typeId() == QMetaType::Int || value.typeId() == QMetaType::UInt) { 0050 const QByteArray term = prefix + value.toString().toUtf8(); 0051 m_doc.addTerm(term); 0052 } else if (value.typeId() == QMetaType::QDate) { 0053 const QByteArray term = prefix + value.toDate().toString(Qt::ISODate).toUtf8(); 0054 m_doc.addTerm(term); 0055 } else if (value.typeId() == QMetaType::QDateTime) { 0056 const QByteArray term = prefix + value.toDateTime().toString(Qt::ISODate).toUtf8(); 0057 m_doc.addTerm(term); 0058 } else if (value.typeId() == QMetaType::QStringList) { 0059 bool shouldBeIndexed = KFileMetaData::PropertyInfo(property).shouldBeIndexed(); 0060 const auto valueList = value.toStringList(); 0061 for (const auto& val : valueList) 0062 { 0063 if (val.isEmpty()) { 0064 continue; 0065 } 0066 m_termGen.indexText(val, prefix); 0067 if (shouldBeIndexed) { 0068 m_termGen.indexText(val); 0069 } 0070 } 0071 } else { 0072 const QString val = value.toString(); 0073 if (val.isEmpty()) { 0074 return; 0075 } 0076 0077 m_termGen.indexText(val, prefix); 0078 KFileMetaData::PropertyInfo pi(property); 0079 if (pi.shouldBeIndexed()) { 0080 m_termGen.indexText(val); 0081 } 0082 } 0083 } 0084 0085 void Result::append(const QString& text) 0086 { 0087 m_termGenForText.indexText(text); 0088 } 0089 0090 void Result::addType(KFileMetaData::Type::Type type) 0091 { 0092 QByteArray num = QByteArray::number(static_cast<int>(type)); 0093 m_doc.addTerm(QByteArray("T") + num); 0094 } 0095 0096 void Result::finish() 0097 { 0098 if (m_map.isEmpty()) { 0099 m_doc.setData(QByteArray()); 0100 return; 0101 } 0102 QJsonObject jo = Baloo::propertyMapToJson(m_map); 0103 QJsonDocument jdoc; 0104 jdoc.setObject(jo); 0105 m_doc.setData(jdoc.toJson(QJsonDocument::JsonFormat::Compact)); 0106 } 0107 0108 void Result::setDocument(const Baloo::Document& doc) 0109 { 0110 m_doc = doc; 0111 // All document metadata are indexed from position 1000 0112 m_termGen.setDocument(m_doc); 0113 m_termGen.setPosition(1000); 0114 0115 // All document plain text starts from 10000. This is done to avoid 0116 // clashes with the term positions 0117 m_termGenForText.setDocument(m_doc); 0118 m_termGenForText.setPosition(10000); 0119 }