diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -7,6 +7,7 @@ ecm_add_tests( filemetadatawidgettest.cpp filemetadataitemcounttest.cpp + filemetadatadatedisplaytest.cpp LINK_LIBRARIES KF5::KIOCore KF5::KIOWidgets KF5::KIOFileWidgets diff --git a/autotests/filemetadatadatedisplaytest.h b/autotests/filemetadatadatedisplaytest.h new file mode 100644 --- /dev/null +++ b/autotests/filemetadatadatedisplaytest.h @@ -0,0 +1,38 @@ +/* + * This file is part of the KDE Baloo Project + * Copyright 2018 Michael Heidelbach + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 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 14 of version 3 of the license. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef FILEMETADATADATEDISPLAYTEST_H +#define FILEMETADATADATEDISPLAYTEST_H + +#include + +class FileMetadataDateDisplayTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + void initTestCase(); + + void shouldDisplayLongAndShortDates(); + void shouldDisplayLongAndShortDates_data(); + +}; + +#endif // FILEMETADATADATEDISPLAYTEST_H diff --git a/autotests/filemetadatadatedisplaytest.cpp b/autotests/filemetadatadatedisplaytest.cpp new file mode 100644 --- /dev/null +++ b/autotests/filemetadatadatedisplaytest.cpp @@ -0,0 +1,110 @@ +/* + * This file is part of the KDE Baloo Project + * Copyright 2018 Michael Heidelbach + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 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 14 of version 3 of the license. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "filemetadatadatedisplaytest.h" +#include + + +#include +#include +#include +#include +#include +#include +#include +#include + +void initLocale() +{ + QLocale::setDefault(QLocale("en_US")); +} +Q_CONSTRUCTOR_FUNCTION(initLocale) + +QTEST_MAIN(FileMetadataDateDisplayTest) + +void FileMetadataDateDisplayTest::initTestCase() +{ + qRegisterMetaType("KFileItemList"); + QStandardPaths::setTestModeEnabled(true); + + auto yesterday = QDateTime::currentDateTime().addDays(-1); + QFile* file(new QFile(QFINDTESTDATA("samplefiles/testtagged.m4a"))); + file->open(QIODevice::ReadOnly); + QVERIFY(file->setFileTime(yesterday, QFileDevice::FileModificationTime)); + file->close(); + delete file; + auto ancient = QDateTime::currentDateTime().addYears(-10); + file = new QFile(QFINDTESTDATA("samplefiles/testtagged.mp3")); + file->open(QIODevice::ReadOnly); + QVERIFY(file->setFileTime(ancient, QFileDevice::FileModificationTime)); + file->close(); +} + +void FileMetadataDateDisplayTest::shouldDisplayLongAndShortDates_data() +{ + QTest::addColumn("format"); + QTest::addColumn("file"); + QTest::addColumn("regex"); + + QTest::addRow("Short date, long ago") + << Baloo::DateFormats::ShortFormat + << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.mp3")) + << QRegularExpression("(?:[1-3][0-9]|[1-9]) (?:[1-2][0-9]|[1-9]):[0-5][0-9] [AP]M") + ; + + QTest::addRow("Short date, yesterday") + << Baloo::DateFormats::ShortFormat + << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.m4a")) + << QRegularExpression("Yesterday, (?:[1-2][0-9]|[1-9]):[0-5][0-9] [AP]M") + ; + + QTest::addRow("Long date, long ago") + << Baloo::DateFormats::LongFormat + << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.mp3")) + << QRegularExpression("[A-Z][a-z]+, [A-Z][a-z]+ (?:[1-3][0-9]|[1-9]), 20[0-9]{2} (?:1[0-2]|[1-9]):[0-5][0-9]:[0-5][0-9] [AP]M [A-Z]{3,4}") + ; + + QTest::addRow("Long date, yesterday") + << Baloo::DateFormats::LongFormat + << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.m4a")) + << QRegularExpression("Yesterday, (?:1[0-2]|[1-9]):[0-5][0-9]:[0-5][0-9] [AP]M [A-Z]{3,4}") + ; + +} + +void FileMetadataDateDisplayTest::shouldDisplayLongAndShortDates() +{ + QFETCH(Baloo::DateFormats, format); + QFETCH(QUrl, file); + QFETCH(QRegularExpression, regex); + + const auto widget = new Baloo::FileMetaDataWidget(); + widget->setDateFormat(format); + QSignalSpy spy(widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); + widget->setItems(KFileItemList() << file); + QVERIFY(spy.wait()); + + QLabel* dateWidget = widget->findChild(QStringLiteral("kfileitem#modified"), Qt::FindDirectChildrenOnly); + QVERIFY2(dateWidget, "Date widget not found"); + QVERIFY2(regex.match(dateWidget->text()).hasMatch(), + qPrintable(QStringLiteral("\"%1\" did not match %2").arg(dateWidget->text(), regex.pattern())) + ); +} diff --git a/autotests/filemetadatawidgettest.cpp b/autotests/filemetadatawidgettest.cpp --- a/autotests/filemetadatawidgettest.cpp +++ b/autotests/filemetadatawidgettest.cpp @@ -1,20 +1,20 @@ /* * This file is part of the KDE Baloo Project * Copyright 2018 Michael Heidelbach - * + * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 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 14 of version 3 of the license. - * + * * This program 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 General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ @@ -47,14 +47,14 @@ void FileMetadataWidgetTest::initTestCase() { qRegisterMetaType("KFileItemList"); - + QStandardPaths::setTestModeEnabled(true); KConfig balooConfig("baloofilerc", KConfig::NoGlobals); KConfigGroup balooSettings = balooConfig.group("General"); // If we use .writePathEntry here, the test will fail. balooSettings.writeEntry(QStringLiteral("folders"), QString()); - + // Ensure show configuration KConfig config("baloofileinformationrc", KConfig::NoGlobals); KConfigGroup settings = config.group("Show"); @@ -68,7 +68,7 @@ if (exe.isEmpty()) { return; } - + const QStringList args = {QStringLiteral("--name=user.baloo.rating"), QStringLiteral("--value=5") , QFINDTESTDATA("samplefiles/testtagged.mp3"), @@ -117,19 +117,19 @@ void FileMetadataWidgetTest::shouldSignalOnceFile() { QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); - m_widget->setItems(KFileItemList() + m_widget->setItems(KFileItemList() << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.m4a")) ); QVERIFY(spy.wait()); QCOMPARE(spy.count(), 1); QCOMPARE(m_widget->items().count(), 1); - + } void FileMetadataWidgetTest::shouldSignalOnceFiles() { QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); - m_widget->setItems(KFileItemList() + m_widget->setItems(KFileItemList() << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/test.mp3")) << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.mp3")) << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.m4a")) @@ -142,18 +142,18 @@ void FileMetadataWidgetTest::shouldShowProperties() { QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); - m_widget->setItems(KFileItemList() + m_widget->setItems(KFileItemList() << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.mp3")) ); - + QVERIFY(spy.wait()); QCOMPARE(spy.count(), 1); - + // simple property QLabel* valueWidget = m_widget->findChild("kfileitem#type"); QVERIFY2(valueWidget, "Type data missing"); QCOMPARE(valueWidget->text(), QLatin1String("MP3 audio")); - + if (m_mayTestRating) { // editable property KRatingWidget* ratingWidget = m_widget->findChild("rating"); @@ -166,27 +166,27 @@ valueWidget = m_widget->findChild("albumArtist"); QVERIFY2(valueWidget, "albumArtist data was not found"); QCOMPARE(valueWidget->text(), QLatin1String("Bill Laswell")); - + } void FileMetadataWidgetTest::shouldShowCommonProperties() { QSignalSpy spy(m_widget, &Baloo::FileMetaDataWidget::metaDataRequestFinished); - m_widget->setItems(KFileItemList() + m_widget->setItems(KFileItemList() << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.mp3")) << QUrl::fromLocalFile(QFINDTESTDATA("samplefiles/testtagged.m4a")) ); QVERIFY(spy.wait()); QCOMPARE(spy.count(), 1); - + // simple property QLabel* valueWidget = m_widget->findChild("kfileitem#type"); QVERIFY(!valueWidget); - + valueWidget = m_widget->findChild("kfileitem#totalSize"); // circumvent i18n formatting QCOMPARE(valueWidget->text().left(3), QLatin1String("153")); - + // editable property if (m_mayTestRating) { KRatingWidget* ratingWidget = m_widget->findChild("rating"); diff --git a/src/filemetadataprovider.cpp b/src/filemetadataprovider.cpp --- a/src/filemetadataprovider.cpp +++ b/src/filemetadataprovider.cpp @@ -176,8 +176,7 @@ } m_data.insert("kfileitem#type", item.mimeComment()); - m_data.insert("kfileitem#modified", - format.formatRelativeDateTime(item.time(KFileItem::ModificationTime), QLocale::LongFormat)); + m_data.insert("kfileitem#modified", item.time(KFileItem::ModificationTime)); m_data.insert("kfileitem#owner", item.user()); m_data.insert("kfileitem#group", item.group()); m_data.insert("kfileitem#permissions", item.permissionsString()); diff --git a/src/filemetadatawidget.h b/src/filemetadatawidget.h --- a/src/filemetadatawidget.h +++ b/src/filemetadatawidget.h @@ -29,15 +29,25 @@ #include #include +#include #include "widgets_export.h" namespace Baloo { +/** + * Modify format of date display + */ +enum class DateFormats { + LongFormat = QLocale::LongFormat, ///< @see QLocale::LongFormat + ShortFormat = QLocale::ShortFormat ///< @see QLocale::ShortFormat +}; + class BALOO_WIDGETS_EXPORT FileMetaDataWidget : public QWidget { Q_OBJECT Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) + Q_PROPERTY(DateFormats dateFormat READ dateFormat WRITE setDateFormat) public: explicit FileMetaDataWidget(QWidget* parent = 0); @@ -58,6 +68,13 @@ void setReadOnly(bool readOnly); bool isReadOnly() const; + /** + * Set date display format. + * Per Default format is Long = @see QLocale::LongFormat + */ + void setDateFormat(const DateFormats format); + DateFormats dateFormat() const; + /** @see QWidget::sizeHint() */ virtual QSize sizeHint() const Q_DECL_OVERRIDE; @@ -86,4 +103,6 @@ }; } +Q_DECLARE_METATYPE(Baloo::DateFormats) + #endif // _BALOO_FILE_METADATAWIDGET_H diff --git a/src/filemetadatawidget.cpp b/src/filemetadatawidget.cpp --- a/src/filemetadatawidget.cpp +++ b/src/filemetadatawidget.cpp @@ -261,6 +261,15 @@ { return d->m_provider->isReadOnly(); } +void FileMetaDataWidget::setDateFormat(const DateFormats format) +{ + d->m_widgetFactory->setDateFormat(format); +} + +DateFormats FileMetaDataWidget::dateFormat() const +{ + return d->m_widgetFactory->dateFormat(); +} QSize FileMetaDataWidget::sizeHint() const { diff --git a/src/widgetfactory.h b/src/widgetfactory.h --- a/src/widgetfactory.h +++ b/src/widgetfactory.h @@ -21,6 +21,8 @@ #ifndef WIDGETFACTORY_H #define WIDGETFACTORY_H +#include "filemetadatawidget.h" + #include #include @@ -46,6 +48,9 @@ void setReadOnly(bool value); void setNoLinks(bool value); + void setDateFormat(const DateFormats format); + DateFormats dateFormat() const; + QWidget* createWidget(const QString& prop, const QVariant& value, QWidget* parent); Q_SIGNALS: @@ -75,6 +80,7 @@ QStringList m_prevTags; bool m_readOnly; bool m_noLinks; + QLocale::FormatType m_dateFormat; }; } diff --git a/src/widgetfactory.cpp b/src/widgetfactory.cpp --- a/src/widgetfactory.cpp +++ b/src/widgetfactory.cpp @@ -65,6 +65,7 @@ : QObject(parent) , m_readOnly( false ) , m_noLinks( false ) + , m_dateFormat(QLocale::LongFormat) { } @@ -135,14 +136,13 @@ } } else { // Check if Date/DateTime - QDateTime dt = QDateTime::fromString(value.toString(), Qt::ISODate); if (dt.isValid()) { QTime time = dt.time(); if (!time.hour() && !time.minute() && !time.second()){ - valueString = form.formatRelativeDate(dt.date(), QLocale::LongFormat); + valueString = form.formatRelativeDate(dt.date(), m_dateFormat); } else { - valueString = form.formatRelativeDateTime(dt, QLocale::LongFormat); + valueString = form.formatRelativeDateTime(dt, m_dateFormat); } } else { @@ -329,3 +329,13 @@ m_items = items; } +Baloo::DateFormats WidgetFactory::dateFormat() const +{ + return static_cast(m_dateFormat); +} + +void Baloo::WidgetFactory::setDateFormat(const Baloo::DateFormats format) +{ + m_dateFormat = static_cast(format); +} +