File indexing completed on 2024-05-12 16:01:54

0001 /**************************************************************************
0002 **
0003 ** This file is part of Qt Creator
0004 **
0005 ** SPDX-FileCopyrightText: 2011 Nokia Corporation and /or its subsidiary(-ies).
0006 **
0007 ** Contact: Nokia Corporation (qt-info@nokia.com)
0008 **
0009 **
0010 ** SPDX-License-Identifier: LGPL-2.1-only
0011 **
0012 ** In addition, as a special exception, Nokia gives you certain additional
0013 ** rights. These rights are described in the Nokia Qt LGPL Exception
0014 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
0015 **
0016 ** Other Usage
0017 **
0018 ** Alternatively, this file may be used in accordance with the terms and
0019 ** conditions contained in a signed written agreement between you and Nokia.
0020 **
0021 ** If you have questions regarding the use of this file, please contact
0022 ** Nokia at qt-info@nokia.com.
0023 **
0024 **************************************************************************/
0025 
0026 #include "KisRssReader.h"
0027 
0028 #include <QString>
0029 #include <QFile>
0030 #include <QXmlStreamReader>
0031 #include <QUrl>
0032 
0033 QString shortenHtml(QString html)
0034 {
0035     html.replace(QLatin1String("<a"), QLatin1String("<i"));
0036     html.replace(QLatin1String("</a"), QLatin1String("</i"));
0037     uint firstParaEndXhtml = (uint) html.indexOf(QLatin1String("</p>"));
0038     uint firstParaEndHtml = (uint) html.indexOf(QLatin1String("<p>"), html.indexOf(QLatin1String("<p>"))+1);
0039     uint firstParaEndBr = (uint) html.indexOf(QLatin1String("<br"));
0040     uint firstParaEnd = qMin(firstParaEndXhtml, firstParaEndHtml);
0041     firstParaEnd = qMin(firstParaEnd, firstParaEndBr);
0042     return html.left(firstParaEnd);
0043 }
0044 
0045 
0046 KisRssReader::KisRssReader()
0047 {
0048 
0049 }
0050 
0051 RssItem KisRssReader::parseItem() {
0052     RssItem item;
0053     item.source = requestUrl;
0054     item.blogIcon = blogIcon;
0055     item.blogName = blogName;
0056     while (!m_streamReader.atEnd()) {
0057         switch (m_streamReader.readNext()) {
0058         case QXmlStreamReader::StartElement:
0059             if (m_streamReader.name() == QLatin1String("title"))
0060                 item.title = m_streamReader.readElementText();
0061             else if (m_streamReader.name() == QLatin1String("link"))
0062                 item.link = m_streamReader.readElementText();
0063             else if (m_streamReader.name() == QLatin1String("pubDate")) {
0064                 QString dateStr = m_streamReader.readElementText();
0065                 item.pubDate = QDateTime::fromString(dateStr, Qt::RFC2822Date);
0066             }
0067             else if (m_streamReader.name() == QLatin1String("category"))
0068                 item.category = m_streamReader.readElementText();
0069             else if (m_streamReader.name() == QLatin1String("description"))
0070                 item.description = m_streamReader.readElementText(); //shortenHtml(streamReader.readElementText());
0071             break;
0072         case QXmlStreamReader::EndElement:
0073             if (m_streamReader.name() == QLatin1String("item"))
0074                 return item;
0075             break;
0076         default:
0077             break;
0078 
0079         }
0080     }
0081     return RssItem();
0082 }
0083 
0084 RssItemList KisRssReader::parseStream(QXmlStreamReader &streamReader) {
0085     RssItemList list;
0086     while (!streamReader.atEnd()) {
0087         switch (streamReader.readNext()) {
0088         case QXmlStreamReader::StartElement:
0089             if (streamReader.name() == QLatin1String("item"))
0090                 list.append(parseItem());
0091             else if (streamReader.name() == QLatin1String("title"))
0092                 blogName = streamReader.readElementText();
0093             else if (streamReader.name() == QLatin1String("link")) {
0094                 if (!streamReader.namespaceUri().isEmpty())
0095                     break;
0096                 QString favIconString(streamReader.readElementText());
0097                 QUrl favIconUrl(favIconString);
0098                 favIconUrl.setPath(QLatin1String("favicon.ico"));
0099                 blogIcon = favIconUrl.toString();
0100                 blogIcon = QString(); // XXX: fix the favicon on krita.org!
0101             }
0102             break;
0103         default:
0104             break;
0105         }
0106     }
0107     return list;
0108 }
0109 
0110 RssItemList KisRssReader::parse(QNetworkReply *reply) {
0111     QUrl source = reply->request().url();
0112     requestUrl = source.toString();
0113     m_streamReader.setDevice(reply);
0114 
0115     return parseStream(m_streamReader);
0116 }
0117 
0118 RssItemList KisRssReader::parse(QFile &file) {
0119     requestUrl = file.fileName();
0120     file.open(QIODevice::ReadOnly);
0121     m_streamReader.setDevice(&file);
0122 
0123     RssItemList itemList(parseStream(m_streamReader));
0124 
0125     file.close();
0126     return itemList;
0127 }