Baloo

result.cpp
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
19Result::Result()
20 : ExtractionResult(QString(), QString())
21 , m_termGen(m_doc)
22 , m_termGenForText(m_doc)
23{
24}
25
26Result::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
33void 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(property, val);
39 }
40 } else {
41 m_map.insert(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(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(Qt::ISODate).toUtf8();
54 m_doc.addTerm(term);
55 } else if (value.typeId() == QMetaType::QDateTime) {
56 const QByteArray term = prefix + value.toDateTime().toString(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(val, prefix);
67 if (shouldBeIndexed) {
68 m_termGen.indexText(val);
69 }
70 }
71 } else {
72 const QString val = value.toString();
73 if (val.isEmpty()) {
74 return;
75 }
76
77 m_termGen.indexText(val, prefix);
78 KFileMetaData::PropertyInfo pi(property);
79 if (pi.shouldBeIndexed()) {
80 m_termGen.indexText(val);
81 }
82 }
83}
84
85void Result::append(const QString& text)
86{
87 m_termGenForText.indexText(text);
88}
89
90void Result::addType(KFileMetaData::Type::Type type)
91{
92 QByteArray num = QByteArray::number(static_cast<int>(type));
93 m_doc.addTerm(QByteArray("T") + num);
94}
95
97{
98 if (m_map.isEmpty()) {
99 m_doc.setData(QByteArray());
100 return;
101 }
102 QJsonObject jo = Baloo::propertyMapToJson(m_map);
103 QJsonDocument jdoc;
104 jdoc.setObject(jo);
105 m_doc.setData(jdoc.toJson(QJsonDocument::JsonFormat::Compact));
106}
107
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}
A document represents an indexed file to be stored in the Baloo engine.
Definition document.h:31
void setDocument(const Baloo::Document &doc)
Can be used to add extraction results to an existing Baloo::Document.
Definition result.cpp:108
void finish()
Applies the finishing touches on the document, and makes it ready to be pushed into the db.
Definition result.cpp:96
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
QByteArray number(double n, char format, int precision)
QString toString(QStringView format, QCalendar cal) const const
QString toString(QStringView format, QCalendar cal) const const
void setObject(const QJsonObject &object)
QByteArray toJson(JsonFormat format) const const
bool isEmpty() const const
QByteArray toUtf8() const const
QDate toDate() const const
QDateTime toDateTime() const const
QString toString() const const
QStringList toStringList() const const
int typeId() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:48:46 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.