From 3edf2b355a5f0ccfb6b2dfed32f4363657347c78 Mon Sep 17 00:00:00 2001 From: Mehmet Samet Duman Date: Sun, 8 Nov 2026 22:36:50 +0300 Subject: [PATCH] The MeshMC News section is now displayed via KF6 Syndication Signed-off-by: Mehmet Samet Duman --- CHANGELOG.md | 1 + CMakeLists.txt | 1 + launcher/CMakeLists.txt | 1 + launcher/news/CMakeLists.txt | 2 + launcher/news/NewsChecker.cpp | 47 +++------ launcher/news/NewsEntry.cpp | 40 -------- launcher/news/NewsEntry.h | 15 +-- launcher/news/NewsFeed.cpp | 82 +++++++++++++++ launcher/news/NewsFeed.h | 46 +++++++++ launcher/news/NewsFeed_test.cpp | 170 ++++++++++++++++++++++++++++++++ 10 files changed, 318 insertions(+), 87 deletions(-) create mode 100644 launcher/news/NewsFeed.cpp create mode 100644 launcher/news/NewsFeed.h create mode 100644 launcher/news/NewsFeed_test.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index e27d79fe5..cd1c3573a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * KColorUtils migration has been completed. * KColorScheme migration has been completed. * KIconThemes migration has been completed. +* The MeshMC News section is now displayed via KF6 Syndication. ## Removed diff --git a/CMakeLists.txt b/CMakeLists.txt index 241d10354..30804d61d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -171,6 +171,7 @@ find_package(KF6 6.23 REQUIRED COMPONENTS I18n ColorScheme IconThemes BreezeIcons + Syndication ) if(UNIX) diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 6bfe1b518..3efa871a0 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -415,6 +415,7 @@ target_link_libraries(MeshMC_logic KF6::ColorScheme KF6::IconThemes KF6::BreezeIcons + KF6::Syndication LibArchive::LibArchive cmark::cmark LocalPeer::LocalPeer diff --git a/launcher/news/CMakeLists.txt b/launcher/news/CMakeLists.txt index 3367ac327..0ea59e58b 100644 --- a/launcher/news/CMakeLists.txt +++ b/launcher/news/CMakeLists.txt @@ -5,4 +5,6 @@ target_sources(MeshMC_logic PRIVATE NewsChecker.cpp NewsEntry.h NewsEntry.cpp + NewsFeed.h + NewsFeed.cpp ) diff --git a/launcher/news/NewsChecker.cpp b/launcher/news/NewsChecker.cpp index ac0a751b9..1919cc556 100644 --- a/launcher/news/NewsChecker.cpp +++ b/launcher/news/NewsChecker.cpp @@ -19,10 +19,10 @@ */ #include "NewsChecker.h" +#include "NewsFeed.h" #include "Logging.h" #include -#include #include @@ -130,44 +130,21 @@ void NewsChecker::rssDownloadFinished(int feedIndex) feed.job.reset(); feed.entries.clear(); - QDomDocument doc; - { - // Stuff to store error info in. - QString errorMsg = "Unknown error."; - int errorLine = -1; - int errorCol = -1; - - // Parse the XML. - if (!doc.setContent(feed.data, false, &errorMsg, &errorLine, - &errorCol)) { - QString fullErrorMsg = - QString("Error parsing RSS feed XML. %1 at %2:%3.") - .arg(errorMsg) - .arg(errorLine) - .arg(errorCol); - feed.data.clear(); - rssDownloadFailed(feedIndex, fullErrorMsg); - return; - } + QList parsed; + QString errorMsg; + if (!News::parseFeed(feed.data, feed.url, feedIndex, &parsed, &errorMsg)) { feed.data.clear(); + rssDownloadFailed( + feedIndex, + QStringLiteral("Error reading the feed. %1.").arg(errorMsg)); + return; } + feed.data.clear(); - // If the parsing succeeded, read it. - QDomNodeList items = doc.elementsByTagName("item"); - for (int i = 0; i < items.length(); i++) { - QDomElement element = items.at(i).toElement(); - NewsEntryPtr entry; - entry.reset(new NewsEntry()); - QString errorMsg = "An unknown error occurred."; - if (NewsEntry::fromXmlElement(element, entry.get(), &errorMsg)) { - entry->feedIndex = feedIndex; - qCDebug(newsLog) << "Loaded news entry" << entry->title; - feed.entries.append(entry); - } else { - qCWarning(newsLog) << "Failed to load news entry at index" << i << ":" - << errorMsg; - } + for (const auto& entry : parsed) { + qCDebug(newsLog) << "Loaded news entry" << entry->title; } + feed.entries = parsed; feedSettled(); } diff --git a/launcher/news/NewsEntry.cpp b/launcher/news/NewsEntry.cpp index 57780cbc0..48150e298 100644 --- a/launcher/news/NewsEntry.cpp +++ b/launcher/news/NewsEntry.cpp @@ -20,8 +20,6 @@ #include "NewsEntry.h" -#include -#include #include NewsEntry::NewsEntry(QObject* parent) : QObject(parent) @@ -44,41 +42,3 @@ NewsEntry::NewsEntry(const QString& title, const QString& content, this->author = author; this->pubDate = pubDate; } - -/*! - * Gets the text content of the given child element as a QVariant. - */ -inline QString childValue(const QDomElement& element, const QString& childName, - QString defaultVal = "") -{ - QDomNodeList nodes = element.elementsByTagName(childName); - if (nodes.count() > 0) { - QDomElement childElement = nodes.at(0).toElement(); - return childElement.text(); - } else { - return defaultVal; - } -} - -bool NewsEntry::fromXmlElement(const QDomElement& element, NewsEntry* entry, - QString* errorMsg) -{ - (void)errorMsg; - QString title = childValue(element, "title", i18n("Untitled")); - QString content = childValue(element, "description", i18n("No content.")); - QString link = childValue(element, "link"); - QString author = childValue(element, "dc:creator", i18n("Unknown Author")); - QString pubDateStr = childValue(element, "pubDate"); - - // FIXME: For now, we're just ignoring timezones. We assume that all time - // zones in the RSS feed are the same. - QString dateFormat("ddd, dd MMM yyyy hh:mm:ss"); - QDateTime pubDate = QDateTime::fromString(pubDateStr, dateFormat); - - entry->title = title; - entry->content = content; - entry->link = link; - entry->author = author; - entry->pubDate = pubDate; - return true; -} diff --git a/launcher/news/NewsEntry.h b/launcher/news/NewsEntry.h index 789b25e8a..0940bc07e 100644 --- a/launcher/news/NewsEntry.h +++ b/launcher/news/NewsEntry.h @@ -22,7 +22,6 @@ #include #include -#include #include #include @@ -45,14 +44,6 @@ class NewsEntry : public QObject const QString& author, const QDateTime& pubDate, QObject* parent = 0); - /*! - * Attempts to load information from the given XML element into the given - * news entry pointer. If this fails, the function will return false and - * store an error message in the errorMsg pointer. - */ - static bool fromXmlElement(const QDomElement& element, NewsEntry* entry, - QString* errorMsg = 0); - //! The post title. QString title; @@ -71,9 +62,9 @@ class NewsEntry : public QObject /*! * Which of NewsChecker's feeds this entry came from. 0 is the * launcher's own feed; anything higher is an extra feed configured - * at build time via MeshMC_NEWS_EXTRA_FEEDS. Set by NewsChecker - * after parsing, not by fromXmlElement() — the XML says nothing - * about where it was downloaded from. + * at build time via MeshMC_NEWS_EXTRA_FEEDS. Set by News::parseFeed() + * from the index NewsChecker gives it — the XML says nothing about + * where it was downloaded from. */ int feedIndex = 0; }; diff --git a/launcher/news/NewsFeed.cpp b/launcher/news/NewsFeed.cpp new file mode 100644 index 000000000..49cec225e --- /dev/null +++ b/launcher/news/NewsFeed.cpp @@ -0,0 +1,82 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2013-2021 MultiMC Contributors + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "NewsFeed.h" + +#include + +#include + +#include + +namespace News +{ + bool parseFeed(const QByteArray& data, const QString& url, int feedIndex, + QList* entries, QString* error) + { + const Syndication::FeedPtr feed = + Syndication::parse(Syndication::DocumentSource(data, url)); + if (!feed) { + *error = QStringLiteral("this is not an RSS or an Atom feed"); + return false; + } + + entries->clear(); + for (const Syndication::ItemPtr& item : feed->items()) { + auto entry = std::make_shared(); + + // The title of an item is HTML as far as Syndication is + // concerned ("Fish & Chips"); a NewsEntry has plain text + // there, which the news dialog escapes itself. + const QString title = Syndication::resolveEntities(item->title()); + if (!title.isEmpty()) { + entry->title = title; + } + + // An RSS item says its text in the description, unless it has a + // content of its own; an Atom entry, the other way round. + const QString content = item->content().isEmpty() + ? item->description() + : item->content(); + if (!content.isEmpty()) { + entry->content = content; + } + + entry->link = item->link(); + + const auto authors = item->authors(); + if (!authors.isEmpty() && !authors.first()->name().isEmpty()) { + entry->author = + Syndication::resolveEntities(authors.first()->name()); + } + + const time_t published = item->datePublished() != 0 + ? item->datePublished() + : item->dateUpdated(); + entry->pubDate = published != 0 + ? QDateTime::fromSecsSinceEpoch(published) + : QDateTime(); + + entry->feedIndex = feedIndex; + entries->append(entry); + } + return true; + } +} // namespace News diff --git a/launcher/news/NewsFeed.h b/launcher/news/NewsFeed.h new file mode 100644 index 000000000..bea648f78 --- /dev/null +++ b/launcher/news/NewsFeed.h @@ -0,0 +1,46 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2013-2021 MultiMC Contributors + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#include "NewsEntry.h" + +namespace News +{ + /*! + * Reads the entries of a news feed, which is an RSS or an Atom document + * (KDE Frameworks' Syndication tells which, and reads either). + * + * Every entry gets \p feedIndex. A date is taken as it is given, with its + * time zone; an entry without a date has an invalid one, which makes it + * the oldest. An entry that has no title, content or author gets the + * words NewsEntry has for that. + * + * \param url where the document came from, which a feed may use to + * resolve addresses that are relative to it. + * \return false if \p data is not a feed; \p error then says what is wrong. + */ + bool parseFeed(const QByteArray& data, const QString& url, int feedIndex, + QList* entries, QString* error); +} // namespace News diff --git a/launcher/news/NewsFeed_test.cpp b/launcher/news/NewsFeed_test.cpp new file mode 100644 index 000000000..cb7a32180 --- /dev/null +++ b/launcher/news/NewsFeed_test.cpp @@ -0,0 +1,170 @@ +/* SPDX-FileCopyrightText: 2026 Project Tick + * SPDX-FileContributor: Project Tick + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2013-2021 MultiMC Contributors + * Copyright (C) 2026 Project Tick + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +#include + +#include "news/NewsFeed.h" + +// What the news bar gets out of a feed: the RSS one that MeshMC's site +// publishes, and Atom, which Syndication reads as well. +class NewsFeedTest : public QObject +{ + Q_OBJECT + + private slots: + void initTestCase() + { + KLocalizedString::setApplicationDomain("meshmc"); + } + + void test_AnRssFeedIsRead() + { + const QByteArray rss = QByteArray( + "\n" + "\n" + " \n" + " MeshMC\n" + " https://example.org/\n" + " News\n" + " \n" + " Fish & Chips\n" + " https://example.org/fish\n" + " Hello world

]]>
\n" + " Alice\n" + " Mon, 02 Jan 2006 15:04:05 +0000\n" + "
\n" + " \n" + " Second\n" + " https://example.org/second\n" + " Plain\n" + " Tue, 03 Jan 2006 10:00:00 +0200\n" + " \n" + "
\n" + "
"); + QList entries; + QString error; + QVERIFY2(News::parseFeed(rss, "https://example.org/feed.xml", 3, + &entries, &error), + qPrintable(error)); + QCOMPARE(entries.size(), 2); + + QCOMPARE(entries[0]->title, QString("Fish & Chips")); + QCOMPARE(entries[0]->link, QString("https://example.org/fish")); + QCOMPARE(entries[0]->author, QString("Alice")); + QVERIFY2(entries[0]->content.contains("world"), + qPrintable(entries[0]->content)); + QCOMPARE(entries[0]->feedIndex, 3); + // the time zone is taken into account + QCOMPARE(entries[0]->pubDate.toUTC(), + QDateTime(QDate(2006, 1, 2), QTime(15, 4, 5), QTimeZone::UTC)); + QCOMPARE(entries[1]->pubDate.toUTC(), + QDateTime(QDate(2006, 1, 3), QTime(8, 0, 0), QTimeZone::UTC)); + } + + void test_AnAtomFeedIsRead() + { + const QByteArray atom = QByteArray( + "\n" + "\n" + " MeshMC\n" + " urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6\n" + " 2006-01-02T15:04:05Z\n" + " \n" + " An entry\n" + " \n" + " urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a\n" + " 2006-01-02T15:04:05Z\n" + " Bob\n" + " <p>Body</p>\n" + " \n" + ""); + QList entries; + QString error; + QVERIFY2(News::parseFeed(atom, QString(), 0, &entries, &error), + qPrintable(error)); + QCOMPARE(entries.size(), 1); + QCOMPARE(entries[0]->title, QString("An entry")); + QCOMPARE(entries[0]->link, QString("https://example.org/entry")); + QCOMPARE(entries[0]->author, QString("Bob")); + QVERIFY2(entries[0]->content.contains("Body"), + qPrintable(entries[0]->content)); + QCOMPARE(entries[0]->pubDate.toUTC(), + QDateTime(QDate(2006, 1, 2), QTime(15, 4, 5), QTimeZone::UTC)); + } + + void test_AMissingDateAndAuthorAreHandled() + { + const QByteArray rss = QByteArray( + "\n" + "thttps://example.org/d\n" + "Undatedhttps://example.org/u\n" + "text\n" + ""); + QList entries; + QString error; + QVERIFY2(News::parseFeed(rss, QString(), 0, &entries, &error), + qPrintable(error)); + QCOMPARE(entries.size(), 1); + // no date is an invalid one, which sorts last in NewsChecker + QVERIFY(!entries[0]->pubDate.isValid()); + // the words NewsEntry has for a missing author + QVERIFY(!entries[0]->author.isEmpty()); + } + + void test_AFeedWithoutEntriesIsAFeed() + { + const QByteArray rss = QByteArray( + "\n" + "thttps://example.org/d\n" + ""); + QList entries; + QString error; + QVERIFY2(News::parseFeed(rss, QString(), 0, &entries, &error), + qPrintable(error)); + QVERIFY(entries.isEmpty()); + } + + void test_ThingsThatAreNotFeedsAreRejected_data() + { + QTest::addColumn("data"); + QTest::newRow("empty") << QByteArray(); + QTest::newRow("text") << QByteArray("hello"); + QTest::newRow("html") << QByteArray("no"); + QTest::newRow("broken xml") << QByteArray(""); + } + + void test_ThingsThatAreNotFeedsAreRejected() + { + QFETCH(QByteArray, data); + QList entries; + QString error; + QVERIFY(!News::parseFeed(data, QString(), 0, &entries, &error)); + QVERIFY(!error.isEmpty()); + } +}; + +QTEST_MAIN(NewsFeedTest) + +#include "NewsFeed_test.moc"