diff --git a/src/file/extractor/result.cpp b/src/file/extractor/result.cpp index d7d2c464..27761d16 100644 --- a/src/file/extractor/result.cpp +++ b/src/file/extractor/result.cpp @@ -1,141 +1,145 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2013-2015 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "result.h" #include #include #include #include #include // In order to use it in a vector Result::Result() : ExtractionResult(QString(), QString()) , m_termGen(m_doc) , m_termGenForText(m_doc) { } Result::Result(const QString& url, const QString& mimetype, const Flags& flags) : KFileMetaData::ExtractionResult(url, mimetype, flags) , m_termGen(m_doc) , m_termGenForText(m_doc) { } void Result::add(KFileMetaData::Property::Property property, const QVariant& value) { int propNum = static_cast(property); if (!value.isNull()) { QString p = QString::number(propNum); if (!m_map.contains(p)) { m_map.insert(p, value); } else { QVariant prev = m_map.value(p); QVariantList list; if (prev.type() == QVariant::List) { list = prev.toList(); } else { list << prev; } list << value; m_map.insert(p, QVariant(list)); } } QByteArray prefix = 'X' + QByteArray::number(propNum) + '-'; if (value.type() == QVariant::Bool) { m_doc.addTerm(prefix); } else if (value.type() == QVariant::Int || value.type() == QVariant::UInt) { const QByteArray term = prefix + value.toString().toUtf8(); m_doc.addTerm(term); } else if (value.type() == QVariant::Date) { const QByteArray term = prefix + value.toDate().toString(Qt::ISODate).toUtf8(); m_doc.addTerm(term); } else if (value.type() == QVariant::DateTime) { const QByteArray term = prefix + value.toDateTime().toString(Qt::ISODate).toUtf8(); m_doc.addTerm(term); } else if (value.type() == QVariant::StringList) { bool shouldBeIndexed = KFileMetaData::PropertyInfo(property).shouldBeIndexed(); const auto valueList = value.toStringList(); for (const auto& val : valueList) { if (val.isEmpty()) continue; m_termGen.indexText(val, prefix); if (shouldBeIndexed) m_termGen.indexText(val); } } else { const QString val = value.toString(); if (val.isEmpty()) return; m_termGen.indexText(val, prefix); KFileMetaData::PropertyInfo pi(property); if (pi.shouldBeIndexed()) m_termGen.indexText(val); } } void Result::append(const QString& text) { m_termGenForText.indexText(text); } void Result::addType(KFileMetaData::Type::Type type) { KFileMetaData::TypeInfo ti(type); const QString t = QLatin1Char('T') + ti.name().toLower(); m_doc.addTerm(t.toUtf8()); } void Result::finish() { + if (m_map.isEmpty()) { + m_doc.setData(QByteArray()); + return; + } QJsonObject jo = QJsonObject::fromVariantMap(m_map); QJsonDocument jdoc; jdoc.setObject(jo); m_doc.setData(jdoc.toJson(QJsonDocument::JsonFormat::Compact)); } void Result::setDocument(const Baloo::Document& doc) { m_doc = doc; // All document metadata are indexed from position 1000 m_termGen.setDocument(m_doc); m_termGen.setPosition(1000); // All document plain text starts from 10000. This is done to avoid // clashes with the term positions m_termGenForText.setDocument(m_doc); m_termGenForText.setPosition(10000); } diff --git a/src/lib/file.cpp b/src/lib/file.cpp index 2e79c0eb..5be986ca 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -1,124 +1,125 @@ /* * This file is part of the KDE Baloo Project * Copyright (C) 2013 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . * */ #include "file.h" #include "global.h" #include "database.h" #include "transaction.h" #include "idutils.h" #include #include #include #include using namespace Baloo; class File::Private { public: QString url; KFileMetaData::PropertyMap propertyMap; }; File::File() : d(new Private) { } File::File(const File& f) : d(new Private(*f.d)) { } File::File(const QString& url) : d(new Private) { d->url = QFileInfo(url).canonicalFilePath(); } File::~File() { delete d; } const File& File::operator=(const File& f) { if (&f != this) { *d = *f.d; } return *this; } QString File::path() const { return d->url; } KFileMetaData::PropertyMap File::properties() const { return d->propertyMap; } QVariant File::property(KFileMetaData::Property::Property property) const { return d->propertyMap.value(property); } bool File::load(const QString& url) { d->url = QFileInfo(url).canonicalFilePath(); d->propertyMap.clear(); return load(); } bool File::load() { const QString& url = d->url; if (url.isEmpty() || !QFile::exists(url)) { return false; } Database *db = globalDatabaseInstance(); if (!db->open(Database::ReadOnlyDatabase)) { return false; } quint64 id = filePathToId(QFile::encodeName(d->url)); if (!id) { return false; } QByteArray arr; { Transaction tr(db, Transaction::ReadOnly); arr = tr.documentData(id); } - if (arr.isEmpty()) { + // Ignore empty JSON documents, i.e. "" or "{}" + if (arr.isEmpty() || arr.size() <= 2) { return false; } const QJsonDocument jdoc = QJsonDocument::fromJson(arr); const QVariantMap varMap = jdoc.object().toVariantMap(); d->propertyMap = KFileMetaData::toPropertyMap(varMap); return true; }