diff --git a/autotests/unit/file/CMakeLists.txt b/autotests/unit/file/CMakeLists.txt --- a/autotests/unit/file/CMakeLists.txt +++ b/autotests/unit/file/CMakeLists.txt @@ -41,3 +41,11 @@ LINK_LIBRARIES Qt5::Test Qt5::DBus KF5::Baloo baloofilecommon ) +# +# Property Serialization +# +ecm_add_test(propertyserializationtest.cpp + TEST_NAME "propertyserializationtest" + LINK_LIBRARIES Qt5::Test KF5::FileMetaData baloofilecommon +) + diff --git a/autotests/unit/file/propertyserializationtest.cpp b/autotests/unit/file/propertyserializationtest.cpp new file mode 100644 --- /dev/null +++ b/autotests/unit/file/propertyserializationtest.cpp @@ -0,0 +1,107 @@ +/* + This file is part of the KDE Baloo project. + * Copyright (C) 2019 Stefan Brüns + * + * 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) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "propertydata.h" + +#include + +// #include +// #include + +using namespace Baloo; + +class PropertySerializationTest : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testEmpty(); + void testSingleProp(); + void testMultiProp(); + void testMultiValue(); +}; + +// Test empty property map +void PropertySerializationTest::testEmpty() +{ + KFileMetaData::PropertyMap properties; + + auto json = propertyMapToJson(properties); + // QJsonDocument jdoc(json); + // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); + + auto res = jsonToPropertyMap(json); + QCOMPARE(res, properties); +} + +// Test empty property map +void PropertySerializationTest::testSingleProp() +{ + namespace KFMProp = KFileMetaData::Property; + + KFileMetaData::PropertyMap properties; + properties.insertMulti(KFMProp::Subject, "subject"); + + auto json = propertyMapToJson(properties); + // QJsonDocument jdoc(json); + // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); + + auto res = jsonToPropertyMap(json); + QCOMPARE(res, properties); +} + +// Test empty property map +void PropertySerializationTest::testMultiProp() +{ + namespace KFMProp = KFileMetaData::Property; + + KFileMetaData::PropertyMap properties; + properties.insertMulti(KFMProp::Subject, "subject"); + properties.insertMulti(KFMProp::ReleaseYear, 2019); + + auto json = propertyMapToJson(properties); + // QJsonDocument jdoc(json); + // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); + + auto res = jsonToPropertyMap(json); + QCOMPARE(res, properties); +} + +// Test empty property map +void PropertySerializationTest::testMultiValue() +{ + namespace KFMProp = KFileMetaData::Property; + + KFileMetaData::PropertyMap properties; + properties.insertMulti(KFMProp::Genre, "genre1"); + properties.insertMulti(KFMProp::Genre, "genre2"); + + auto json = propertyMapToJson(properties); + // QJsonDocument jdoc(json); + // qDebug() << __func__ << jdoc.toJson(QJsonDocument::JsonFormat::Compact); + + auto res = jsonToPropertyMap(json); + QCOMPARE(res, properties); +} + + +QTEST_MAIN(PropertySerializationTest) + +#include "propertyserializationtest.moc" diff --git a/src/file/CMakeLists.txt b/src/file/CMakeLists.txt --- a/src/file/CMakeLists.txt +++ b/src/file/CMakeLists.txt @@ -41,6 +41,8 @@ org.kde.BalooWatcherApplication.xml pendingfile.cpp kinotify.cpp + + propertydata.cpp ) ecm_qt_declare_logging_category(file_static_lib_SRCS HEADER baloodebug.h IDENTIFIER BALOO CATEGORY_NAME org.kde.baloo) diff --git a/src/file/propertydata.h b/src/file/propertydata.h new file mode 100644 --- /dev/null +++ b/src/file/propertydata.h @@ -0,0 +1,35 @@ +/* + This file is part of the KDE Baloo project. + * Copyright (C) 2019 Stefan Brüns + * + * 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) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef BALOO_PROPERTYDATA_H +#define BALOO_PROPERTYDATA_H + +#include +#include + +namespace Baloo +{ + +const QJsonObject propertyMapToJson(const KFileMetaData::PropertyMap& properties); +const KFileMetaData::PropertyMap jsonToPropertyMap(const QJsonObject& properties); + +} // namespace Baloo + +#endif // BALOO_PROPERTYDATA_H diff --git a/src/file/propertydata.cpp b/src/file/propertydata.cpp new file mode 100644 --- /dev/null +++ b/src/file/propertydata.cpp @@ -0,0 +1,93 @@ +/* + This file is part of the KDE Baloo project. + * Copyright (C) 2019 Stefan Brüns + * + * 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) any later version. + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include "propertydata.h" +#include +#include +#include + +namespace Baloo +{ + +const QJsonObject propertyMapToJson(const KFileMetaData::PropertyMap& properties) +{ + auto it = properties.cbegin(); + QJsonObject jsonDict; + + while (it != properties.cend()) { + + auto property = it.key(); + QString keyString = QString::number(static_cast(property)); + + auto rangeEnd = properties.upperBound(property); + + QJsonValue value; + // In case a key has multiple values, convert to QJsonArray + if (std::distance(it, rangeEnd) > 1) { + QJsonArray values; + + // Last inserted is first element, for backwards compatible + // ordering prepend earlier elements + while (it != rangeEnd) { + values.insert(0, QJsonValue::fromVariant(it.value())); + ++it; + }; + + value = values; + } else { + value = QJsonValue::fromVariant(it.value()); + } + + jsonDict.insert(keyString, value); + + // pivot to next key + it = rangeEnd; + } + + return jsonDict; +} + +const KFileMetaData::PropertyMap jsonToPropertyMap(const QJsonObject& properties) +{ + KFileMetaData::PropertyMap propertyMap; + + auto it = properties.begin(); + while (it != properties.end()) { + int propNum = it.key().toInt(); + auto prop = static_cast(propNum); + + if (it.value().isArray()) { + const auto values = it.value().toArray(); + for (const auto val : values) { + propertyMap.insertMulti(prop, val.toString()); + } + + } else if (it.value().isDouble()) { + propertyMap.insertMulti(prop, it.value().toInt()); + } else { + propertyMap.insertMulti(prop, it.value().toString()); + } + ++it; + } + + return propertyMap; +} + +} // namespace Baloo