Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ find_package(KF6 6.23 REQUIRED COMPONENTS I18n
ColorScheme
IconThemes
BreezeIcons
Syndication
)

if(UNIX)
Expand Down
1 change: 1 addition & 0 deletions launcher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ target_link_libraries(MeshMC_logic
KF6::ColorScheme
KF6::IconThemes
KF6::BreezeIcons
KF6::Syndication
LibArchive::LibArchive
cmark::cmark
LocalPeer::LocalPeer
Expand Down
2 changes: 2 additions & 0 deletions launcher/news/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ target_sources(MeshMC_logic PRIVATE
NewsChecker.cpp
NewsEntry.h
NewsEntry.cpp
NewsFeed.h
NewsFeed.cpp
)
47 changes: 12 additions & 35 deletions launcher/news/NewsChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/

#include "NewsChecker.h"
#include "NewsFeed.h"
#include "Logging.h"

#include <QByteArray>
#include <QDomDocument>

#include <QDebug>

Expand Down Expand Up @@ -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<NewsEntryPtr> 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();
}
Expand Down
40 changes: 0 additions & 40 deletions launcher/news/NewsEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

#include "NewsEntry.h"

#include <QDomNodeList>
#include <QVariant>
#include <KLocalizedString>

NewsEntry::NewsEntry(QObject* parent) : QObject(parent)
Expand All @@ -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;
}
15 changes: 3 additions & 12 deletions launcher/news/NewsEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include <QObject>
#include <QString>
#include <QDomElement>
#include <QDateTime>

#include <memory>
Expand All @@ -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;

Expand All @@ -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;
};
Expand Down
82 changes: 82 additions & 0 deletions launcher/news/NewsFeed.cpp
Original file line number Diff line number Diff line change
@@ -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 <QDateTime>

#include <KLocalizedString>

#include <syndication/syndication.h>

namespace News
{
bool parseFeed(const QByteArray& data, const QString& url, int feedIndex,
QList<NewsEntryPtr>* 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<NewsEntry>();

// The title of an item is HTML as far as Syndication is
// concerned ("Fish &amp; 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
46 changes: 46 additions & 0 deletions launcher/news/NewsFeed.h
Original file line number Diff line number Diff line change
@@ -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 <QByteArray>
#include <QList>
#include <QString>

#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<NewsEntryPtr>* entries, QString* error);
} // namespace News
Loading
Loading