Krita Source Code Documentation
Loading...
Searching...
No Matches
KisMultiFeedRSSModel.cpp
Go to the documentation of this file.
1/**************************************************************************
2**
3** This file is part of Qt Creator
4**
5** SPDX-FileCopyrightText: 2011 Nokia Corporation and /or its subsidiary(-ies).
6**
7** Contact: Nokia Corporation (qt-info@nokia.com)
8**
9**
10** SPDX-License-Identifier: LGPL-2.1-only
11**
12** In addition, as a special exception, Nokia gives you certain additional
13** rights. These rights are described in the Nokia Qt LGPL Exception
14** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
15**
16** Other Usage
17**
18** Alternatively, this file may be used in accordance with the terms and
19** conditions contained in a signed written agreement between you and Nokia.
20**
21** If you have questions regarding the use of this file, please contact
22** Nokia at qt-info@nokia.com.
23**
24**************************************************************************/
25
27
28#include <QThread>
29#include <QXmlStreamReader>
30#include <QCoreApplication>
31#include <QLocale>
32#include <QFile>
33#include <QTextBlock>
34#include <QTextDocument>
35
36#include <QNetworkRequest>
37#include <QNetworkReply>
39
40#include <KisRssReader.h>
41#include <QApplication>
42
44 QAbstractListModel(parent),
45 m_networkAccessManager(new KisNetworkAccessManager),
46 m_articleCount(0)
47{
48 initialize();
49}
50
52 : QAbstractListModel(parent),
53 m_networkAccessManager(nam),
54 m_articleCount(0)
55{
56 initialize();
57}
58
59
64
77
78void MultiFeedRssModel::addFeed(const QString& feed)
79{
80 if (m_sites.contains(feed)) {
81 // do not add the feed twice
82 return;
83 }
84
85 m_sites << feed;
86 const QUrl feedUrl(feed);
88}
89
90bool sortForPubDate(const RssItem& item1, const RssItem& item2)
91{
92 return item1.pubDate > item2.pubDate;
93}
94
95void MultiFeedRssModel::appendFeedData(QNetworkReply *reply)
96{
97 beginResetModel();
98 KisRssReader reader;
99 m_aggregatedFeed.append(reader.parse(reply));
102 endResetModel();
103
104 Q_EMIT feedDataChanged();
105}
106
111
113{
114 connect(m_networkAccessManager, SIGNAL(finished(QNetworkReply*)),
115 SLOT(appendFeedData(QNetworkReply*)), Qt::QueuedConnection);
116}
117
118void MultiFeedRssModel::removeFeed(const QString &feed)
119{
120 bool isRemoved = m_sites.removeOne(feed);
121 if (isRemoved) {
122 beginResetModel();
123 QMutableListIterator<RssItem> it(m_aggregatedFeed);
124 while (it.hasNext()) {
125 RssItem item = it.next();
126 if (item.source == feed)
127 it.remove();
128 }
130 endResetModel();
131
132 Q_EMIT feedDataChanged();
133 }
134}
135
136int MultiFeedRssModel::rowCount(const QModelIndex &) const
137{
138 return m_aggregatedFeed.size();
139}
140
141QVariant MultiFeedRssModel::data(const QModelIndex &index, int role) const
142{
143 if (index.row() < m_aggregatedFeed.size()) {
144 RssItem item = m_aggregatedFeed.at(index.row());
145
146 switch (role) {
147 case Qt::DisplayRole:
148 {
149 QTextDocument doc;
150 doc.setHtml(item.description);
151 // Extract the first text block, which is the `<p>` element containing
152 // the shortened post text, excluding the "This post [...] appeared
153 // first on [...]" text.
154 QString text = doc.firstBlock().text();
155 if (text.length() > 292) {
156 text.truncate(290);
157 text.append("...");
158 }
159 return QString("<b><a href=\"" + item.link + "\">" + item.title + "</a></b>"
160 "<br><small>(" + QLocale().toString(item.pubDate.toLocalTime(), QLocale::ShortFormat) + ") "
161 "<p style=\"margin-top: 4px\">" + text + "</p></small>");
162 }
164 return item.title;
166 return item.description;
168 return item.pubDate.toString("dd-MM-yyyy hh:mm");
170 return item.link;
172 return item.category;
174 return item.blogName;
176 return item.blogIcon;
177 }
178 }
179
180 return QVariant();
181}
bool sortForPubDate(const RssItem &item1, const RssItem &item2)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
Network Access Manager for use with Krita.
RssItemList parse(QNetworkReply *reply)
KisNetworkAccessManager * m_networkAccessManager
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QHash< int, QByteArray > roleNames() const override
virtual void addFeed(const QString &feed)
void removeFeed(const QString &feed)
void appendFeedData(QNetworkReply *reply)
MultiFeedRssModel(QObject *parent=0)
void setArticleCount(int arg)
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QString title
QString category
QString blogName
QString blogIcon
QDateTime pubDate
QString source
QString description
QString link