diff --git a/src/file/extractor/CMakeLists.txt b/src/file/extractor/CMakeLists.txt index 95072c9d..69c5d31a 100644 --- a/src/file/extractor/CMakeLists.txt +++ b/src/file/extractor/CMakeLists.txt @@ -1,36 +1,37 @@ remove_definitions(-DTRANSLATION_DOMAIN=\"baloo_file5\") add_definitions(-DTRANSLATION_DOMAIN=\"baloo_file_extractor5\") set(EXTRACTOR_SRCS main.cpp app.cpp result.cpp idlestatemonitor.cpp ../priority.cpp ../basicindexingjob.cpp ../fileindexerconfig.cpp ../storagedevices.cpp ../regexpcache.cpp ../fileexcludefilters.cpp + ../propertydata.cpp ) ecm_qt_declare_logging_category(EXTRACTOR_SRCS HEADER baloodebug.h IDENTIFIER BALOO CATEGORY_NAME org.kde.baloo) add_executable(baloo_file_extractor ${EXTRACTOR_SRCS}) ecm_mark_nongui_executable(baloo_file_extractor) target_compile_definitions(baloo_file_extractor PRIVATE -DPROJECT_VERSION="${PROJECT_VERSION}") target_link_libraries(baloo_file_extractor Qt5::DBus Qt5::Widgets KF5::FileMetaData KF5::I18n KF5::ConfigCore KF5::Solid KF5::BalooEngine KF5::Crash KF5::IdleTime ) install(TARGETS baloo_file_extractor DESTINATION ${BIN_INSTALL_DIR}) diff --git a/src/file/extractor/result.cpp b/src/file/extractor/result.cpp index 27761d16..b0e4c9d7 100644 --- a/src/file/extractor/result.cpp +++ b/src/file/extractor/result.cpp @@ -1,145 +1,136 @@ /* * 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 "propertydata.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)); + if (value.type() == QVariant::StringList) { + const auto valueList = value.toStringList(); + for (const auto& val : valueList) { + m_map.insertMulti(property, val); } + } else { + m_map.insertMulti(property, value); } + int propNum = static_cast(property); 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); + QJsonObject jo = Baloo::propertyMapToJson(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/file/extractor/result.h b/src/file/extractor/result.h index c808c814..ca1472dc 100644 --- a/src/file/extractor/result.h +++ b/src/file/extractor/result.h @@ -1,92 +1,92 @@ /* * 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 . * */ #ifndef EXTRACTIONRESULT_H #define EXTRACTIONRESULT_H #include #include "document.h" #include "termgenerator.h" /** * \class Result result.h * * \brief The result class is where all the data extracted by * the KFileMetaData extractors is saved to. * It implements the KFileMetaData::ExtractionResult interface. * The results can be retrieved as a Baloo::Document using * \c document() and stored in the database. * */ class Result : public KFileMetaData::ExtractionResult { public: Result(); Result(const QString& url, const QString& mimetype, const Flags& flags = ExtractEverything); void add(KFileMetaData::Property::Property property, const QVariant& value) override; void append(const QString& text) override; void addType(KFileMetaData::Type::Type type) override; /** * Can be used to add extraction results to an existing * Baloo::Document. Has to be called before passing the * Result to KFileMetaData::Extractor::extract(). * \param doc The Baloo::Document */ void setDocument(const Baloo::Document& doc); /** * Returns the Baloo document to which the results from the extractors * are saved. */ Baloo::Document& document() { return m_doc; } /** * Applies the finishing touches on the document, and makes * it ready to be pushed into the db. */ void finish(); private: /** * The document that represents the file that is to be indexed. */ Baloo::Document m_doc; /** * Contains the position state of the metadata and adds it to the document. */ Baloo::TermGenerator m_termGen; /** * Contains the position state of plain text and adds it to the document. */ Baloo::TermGenerator m_termGenForText; /** * Contains all indexed property data from the extractors. */ - QVariantMap m_map; + KFileMetaData::PropertyMap m_map; }; #endif // EXTRACTIONRESULT_H diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 5be61b02..d1c9317f 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -1,135 +1,136 @@ set(DBUS_INTERFACES ${CMAKE_BINARY_DIR}/src/dbus/maininterface.cpp ) # CMake <= 3.9 skipped automoc for generated files # CMake >= 3.10 requires SKIP_AUTOMOC to actually skip automoc SET_SOURCE_FILES_PROPERTIES(${DBUS_INTERFACES} PROPERTIES GENERATED 1 SKIP_AUTOMOC 1) set(BALOO_LIB_SRCS term.cpp query.cpp queryrunnable.cpp resultiterator.cpp advancedqueryparser.cpp file.cpp filemonitor.cpp taglistjob.cpp indexerconfig.cpp ../file/fileindexerconfig.cpp ../file/storagedevices.cpp ../file/regexpcache.cpp ../file/fileexcludefilters.cpp + ../file/propertydata.cpp searchstore.cpp ${DBUS_INTERFACES} ) ecm_qt_declare_logging_category(BALOO_LIB_SRCS HEADER baloodebug.h IDENTIFIER BALOO CATEGORY_NAME org.kde.baloo) add_library(KF5Baloo ${BALOO_LIB_SRCS}) add_dependencies(KF5Baloo BalooDBusInterfaces) add_library(KF5::Baloo ALIAS KF5Baloo) target_link_libraries(KF5Baloo PUBLIC Qt5::Core KF5::CoreAddons KF5::FileMetaData PRIVATE KF5::ConfigCore Qt5::DBus KF5::Solid KF5::BalooEngine ) set_target_properties(KF5Baloo PROPERTIES VERSION ${BALOO_VERSION_STRING} SOVERSION ${BALOO_SOVERSION} EXPORT_NAME Baloo ) target_include_directories(KF5Baloo INTERFACE "$") generate_export_header(KF5Baloo BASE_NAME BALOO_CORE EXPORT_FILE_NAME core_export.h) ecm_generate_headers(KF5Baloo_CamelCase_HEADERS HEADER_NAMES Query QueryRunnable ResultIterator File FileMonitor TagListJob IndexerConfig PREFIX baloo REQUIRED_HEADERS KF5Baloo_HEADERS ) install(TARGETS KF5Baloo EXPORT KF5BalooTargets ${KF5_INSTALL_TARGETS_DEFAULT_ARGS}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/core_export.h ${KF5Baloo_HEADERS} DESTINATION ${KF5_INCLUDE_INSTALL_DIR}/Baloo/baloo COMPONENT Devel ) install(FILES ${KF5Baloo_CamelCase_HEADERS} DESTINATION ${KF5_INCLUDE_INSTALL_DIR}/Baloo/Baloo COMPONENT Devel ) if(BUILD_QCH) ecm_add_qch( KF5Baloo_QCH NAME Baloo BASE_NAME KF5Baloo VERSION ${KF5_VERSION} ORG_DOMAIN org.kde SOURCES # using only public headers, to cover only public API ${KF5Baloo_HEADERS} "${CMAKE_SOURCE_DIR}/docs/distributing.md" "${CMAKE_SOURCE_DIR}/docs/development/build-instructions.md" "${CMAKE_SOURCE_DIR}/docs/user/searching.md" MD_MAINPAGE "${CMAKE_SOURCE_DIR}/README.md" LINK_QCHS Qt5Core_QCH KF5CoreAddons_QCH KF5FileMetaData_QCH BLANK_MACROS BALOO_CORE_EXPORT BALOO_CORE_DEPRECATED BALOO_CORE_DEPRECATED_EXPORT TAGFILE_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} QCH_INSTALL_DESTINATION ${KDE_INSTALL_QTQCHDIR} COMPONENT Devel ) endif() if (NOT WIN32) configure_file ( ${CMAKE_CURRENT_SOURCE_DIR}/Baloo.pc.cmake ${CMAKE_CURRENT_BINARY_DIR}/Baloo.pc ) install ( FILES ${CMAKE_CURRENT_BINARY_DIR}/Baloo.pc DESTINATION ${KDE_INSTALL_LIBDIR}/pkgconfig ) endif () include(ECMGeneratePriFile) ecm_generate_pri_file( BASE_NAME Baloo LIB_NAME KF5Baloo DEPS "core KCoreAddons KFileMetaData" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KF5_INCLUDE_INSTALL_DIR}/Baloo ) install(FILES ${PRI_FILENAME} DESTINATION ${ECM_MKSPECS_INSTALL_DIR}) diff --git a/src/lib/file.cpp b/src/lib/file.cpp index d02c70ad..b6bba47a 100644 --- a/src/lib/file.cpp +++ b/src/lib/file.cpp @@ -1,124 +1,124 @@ /* * 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 "propertydata.h" #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); } // 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); + d->propertyMap = Baloo::jsonToPropertyMap(jdoc.object()); return true; }