File indexing completed on 2024-05-12 11:45:19
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.type() == QVariant::StringList) { 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.type() == QVariant::Bool) { 0048 m_doc.addTerm(prefix); 0049 } 0050 else if (value.type() == QVariant::Int || value.type() == QVariant::UInt) { 0051 const QByteArray term = prefix + value.toString().toUtf8(); 0052 m_doc.addTerm(term); 0053 } 0054 else if (value.type() == QVariant::Date) { 0055 const QByteArray term = prefix + value.toDate().toString(Qt::ISODate).toUtf8(); 0056 m_doc.addTerm(term); 0057 } 0058 else if (value.type() == QVariant::DateTime) { 0059 const QByteArray term = prefix + value.toDateTime().toString(Qt::ISODate).toUtf8(); 0060 m_doc.addTerm(term); 0061 } 0062 else if (value.type() == QVariant::StringList) { 0063 bool shouldBeIndexed = KFileMetaData::PropertyInfo(property).shouldBeIndexed(); 0064 const auto valueList = value.toStringList(); 0065 for (const auto& val : valueList) 0066 { 0067 if (val.isEmpty()) { 0068 continue; 0069 } 0070 m_termGen.indexText(val, prefix); 0071 if (shouldBeIndexed) { 0072 m_termGen.indexText(val); 0073 } 0074 } 0075 } 0076 else { 0077 const QString val = value.toString(); 0078 if (val.isEmpty()) { 0079 return; 0080 } 0081 0082 m_termGen.indexText(val, prefix); 0083 KFileMetaData::PropertyInfo pi(property); 0084 if (pi.shouldBeIndexed()) { 0085 m_termGen.indexText(val); 0086 } 0087 } 0088 } 0089 0090 void Result::append(const QString& text) 0091 { 0092 m_termGenForText.indexText(text); 0093 } 0094 0095 void Result::addType(KFileMetaData::Type::Type type) 0096 { 0097 QByteArray num = QByteArray::number(static_cast<int>(type)); 0098 m_doc.addTerm(QByteArray("T") + num); 0099 } 0100 0101 void Result::finish() 0102 { 0103 if (m_map.isEmpty()) { 0104 m_doc.setData(QByteArray()); 0105 return; 0106 } 0107 QJsonObject jo = Baloo::propertyMapToJson(m_map); 0108 QJsonDocument jdoc; 0109 jdoc.setObject(jo); 0110 m_doc.setData(jdoc.toJson(QJsonDocument::JsonFormat::Compact)); 0111 } 0112 0113 void Result::setDocument(const Baloo::Document& doc) 0114 { 0115 m_doc = doc; 0116 // All document metadata are indexed from position 1000 0117 m_termGen.setDocument(m_doc); 0118 m_termGen.setPosition(1000); 0119 0120 // All document plain text starts from 10000. This is done to avoid 0121 // clashes with the term positions 0122 m_termGenForText.setDocument(m_doc); 0123 m_termGenForText.setPosition(10000); 0124 }