Baloo

result.cpp
1 /*
2  This file is part of the KDE Baloo Project
3  SPDX-FileCopyrightText: 2013-2015 Vishesh Handa <[email protected]>
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::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.type() == QVariant::StringList) {
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.type() == QVariant::Bool) {
48  m_doc.addTerm(prefix);
49  }
50  else if (value.type() == QVariant::Int || value.type() == QVariant::UInt) {
51  const QByteArray term = prefix + value.toString().toUtf8();
52  m_doc.addTerm(term);
53  }
54  else if (value.type() == QVariant::Date) {
55  const QByteArray term = prefix + value.toDate().toString(Qt::ISODate).toUtf8();
56  m_doc.addTerm(term);
57  }
58  else if (value.type() == QVariant::DateTime) {
59  const QByteArray term = prefix + value.toDateTime().toString(Qt::ISODate).toUtf8();
60  m_doc.addTerm(term);
61  }
62  else if (value.type() == QVariant::StringList) {
63  bool shouldBeIndexed = KFileMetaData::PropertyInfo(property).shouldBeIndexed();
64  const auto valueList = value.toStringList();
65  for (const auto& val : valueList)
66  {
67  if (val.isEmpty()) {
68  continue;
69  }
70  m_termGen.indexText(val, prefix);
71  if (shouldBeIndexed) {
72  m_termGen.indexText(val);
73  }
74  }
75  }
76  else {
77  const QString val = value.toString();
78  if (val.isEmpty()) {
79  return;
80  }
81 
82  m_termGen.indexText(val, prefix);
83  KFileMetaData::PropertyInfo pi(property);
84  if (pi.shouldBeIndexed()) {
85  m_termGen.indexText(val);
86  }
87  }
88 }
89 
90 void Result::append(const QString& text)
91 {
92  m_termGenForText.indexText(text);
93 }
94 
95 void Result::addType(KFileMetaData::Type::Type type)
96 {
97  QByteArray num = QByteArray::number(static_cast<int>(type));
98  m_doc.addTerm(QByteArray("T") + num);
99 }
100 
102 {
103  if (m_map.isEmpty()) {
104  m_doc.setData(QByteArray());
105  return;
106  }
107  QJsonObject jo = Baloo::propertyMapToJson(m_map);
108  QJsonDocument jdoc;
109  jdoc.setObject(jo);
110  m_doc.setData(jdoc.toJson(QJsonDocument::JsonFormat::Compact));
111 }
112 
114 {
115  m_doc = doc;
116  // All document metadata are indexed from position 1000
117  m_termGen.setDocument(m_doc);
118  m_termGen.setPosition(1000);
119 
120  // All document plain text starts from 10000. This is done to avoid
121  // clashes with the term positions
122  m_termGenForText.setDocument(m_doc);
123  m_termGenForText.setPosition(10000);
124 }
QByteArray number(int n, int base)
QDate toDate() const const
KIOCORE_EXPORT MimetypeJob * mimetype(const QUrl &url, JobFlags flags=DefaultFlags)
QVariant::Type type() const const
bool isEmpty() const const
QByteArray toUtf8() const const
void insert(int i, const T &value)
QDateTime toDateTime() const const
void setDocument(const Baloo::Document &doc)
Can be used to add extraction results to an existing Baloo::Document.
Definition: result.cpp:113
QByteArray toJson() const const
void setObject(const QJsonObject &object)
QString toString(Qt::DateFormat format) const const
QStringList toStringList() const const
QString toString(Qt::DateFormat format) const const
A document represents an indexed file to be stored in the Baloo engine.
Definition: document.h:30
void finish()
Applies the finishing touches on the document, and makes it ready to be pushed into the db.
Definition: result.cpp:101
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.