kasts/src/entry.cpp

155 lines
4.8 KiB
C++
Raw Normal View History

2020-05-26 16:32:07 +02:00
/**
* Copyright 2020 Tobias Fella <fella@posteo.de>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "entry.h"
2020-06-06 00:05:32 +02:00
#include <QRegularExpression>
2020-06-06 00:05:32 +02:00
#include <QSqlQuery>
2020-05-26 16:32:07 +02:00
#include <QUrl>
2020-06-06 00:05:32 +02:00
#include "database.h"
2020-06-10 00:07:08 +02:00
Entry::Entry(Feed *feed, int index)
: QObject(nullptr)
2020-06-06 00:05:32 +02:00
, m_feed(feed)
2020-05-26 16:32:07 +02:00
{
2020-06-10 00:07:08 +02:00
QSqlQuery entryQuery;
entryQuery.prepare(QStringLiteral("SELECT * FROM Entries WHERE feed=:feed ORDER BY updated DESC LIMIT 1 OFFSET :index;"));
entryQuery.bindValue(QStringLiteral(":feed"), m_feed->url());
entryQuery.bindValue(QStringLiteral(":index"), index);
Database::instance().execute(entryQuery);
if (!entryQuery.next())
qWarning() << "No element with index" << index << "found in feed" << m_feed->url();
QSqlQuery authorQuery;
authorQuery.prepare(QStringLiteral("SELECT * FROM Authors WHERE id=:id"));
authorQuery.bindValue(QStringLiteral(":id"), entryQuery.value(QStringLiteral("id")).toString());
Database::instance().execute(authorQuery);
while (authorQuery.next()) {
m_authors += new Author(authorQuery.value(QStringLiteral("name")).toString(), authorQuery.value(QStringLiteral("email")).toString(), authorQuery.value(QStringLiteral("uri")).toString(), nullptr);
}
m_created.setSecsSinceEpoch(entryQuery.value(QStringLiteral("created")).toInt());
m_updated.setSecsSinceEpoch(entryQuery.value(QStringLiteral("updated")).toInt());
m_id = entryQuery.value(QStringLiteral("id")).toString();
m_title = entryQuery.value(QStringLiteral("title")).toString();
m_content = entryQuery.value(QStringLiteral("content")).toString();
m_link = entryQuery.value(QStringLiteral("link")).toString();
m_read = entryQuery.value(QStringLiteral("read")).toBool();
2020-05-26 16:32:07 +02:00
}
Entry::~Entry()
{
}
2020-06-06 00:05:32 +02:00
QString Entry::id() const
{
return m_id;
}
2020-05-26 16:32:07 +02:00
QString Entry::title() const
{
return m_title;
}
QString Entry::content() const
{
return m_content;
}
QVector<Author *> Entry::authors() const
{
return m_authors;
}
QDateTime Entry::created() const
{
return m_created;
}
QDateTime Entry::updated() const
{
return m_updated;
}
QString Entry::link() const
{
return m_link;
}
2020-06-06 00:05:32 +02:00
bool Entry::read() const
{
return m_read;
}
2020-05-26 16:32:07 +02:00
QString Entry::baseUrl() const
{
return QUrl(m_link).adjusted(QUrl::RemovePath).toString();
}
2020-06-06 00:05:32 +02:00
void Entry::setRead(bool read)
{
m_read = read;
Q_EMIT readChanged(m_read);
QSqlQuery query;
query.prepare(QStringLiteral("UPDATE Entries SET read=:read WHERE id=:id AND feed=:feed"));
query.bindValue(QStringLiteral(":id"), m_id);
query.bindValue(QStringLiteral(":feed"), m_feed->url());
query.bindValue(QStringLiteral(":read"), m_read);
Database::instance().execute(query);
2020-07-29 23:41:11 +02:00
Q_EMIT m_feed->unreadEntryCountChanged();
2020-06-06 00:05:32 +02:00
}
QString Entry::adjustedContent(int width, int fontSize)
{
QString ret(m_content);
QRegularExpression imgRegex(QStringLiteral("<img ((?!width=\"[0-9]+(px)?\").)*(width=\"([0-9]+)(px)?\")?[^>]*>"));
QRegularExpressionMatchIterator i = imgRegex.globalMatch(ret);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString imgTag(match.captured());
if (imgTag.contains(QStringLiteral("wp-smiley")))
imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(fontSize));
QString widthParameter = match.captured(4);
if (widthParameter.length() != 0) {
if (widthParameter.toInt() > width)
imgTag.replace(match.captured(3), QStringLiteral("width=\"%1\"").arg(width));
} else {
imgTag.insert(4, QStringLiteral(" width=\"%1\"").arg(width));
}
ret.replace(match.captured(), imgTag);
}
ret.replace(QRegularExpression(QStringLiteral("<ul[^>]*>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("</ul>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("<li[^>]*>")), QLatin1String(""));
ret.replace(QRegularExpression(QStringLiteral("</li>")), QLatin1String(""));
ret.replace(QStringLiteral("<img"), QStringLiteral("<br /> <img"));
return ret;
}