Ticket #5355: mythnews-atom.patch

File mythnews-atom.patch, 3.3 KB (added by otto at kolsi dot fi, 16 years ago)
  • mythnews/mythnews/newsengine.cpp

     
    2323
    2424#include <qfile.h>
    2525#include <qdatastream.h>
    26 #include <qdom.h>
    2726#include <qurloperator.h>
    2827
    2928extern "C" {
     
    224223    if (m_state == RetrieveFailed)
    225224        m_errorString += tr("Showing Cached News");
    226225
     226    //Check the type of the feed
     227    QString rootName = domDoc.documentElement().nodeName();
     228    if(rootName == QString::fromLatin1("rss") ||
     229       rootName == QString::fromLatin1("rdf:RDF")) {
     230        parseRSS(domDoc);
     231        xmlFile.close();
     232        return;
     233    }
     234    else if (rootName == QString::fromLatin1("feed")) {
     235        parseAtom(domDoc);
     236        xmlFile.close();
     237        return;
     238    }
     239    else {
     240        VERBOSE(VB_IMPORTANT, "MythNews: NewsEngine: XML-file is not valid RSS-feed");
     241        m_errorString += tr("XML-file is not valid RSS-feed");
     242        return;
     243    }
     244
     245}
     246
     247void NewsSite::parseRSS(QDomDocument domDoc)
     248{
    227249    QDomNode channelNode = domDoc.documentElement().namedItem(QString::fromLatin1("channel"));
    228250
    229251    m_desc = channelNode.namedItem(QString::fromLatin1("description")).toElement().text().simplifyWhiteSpace();
     
    344366        }
    345367        new NewsArticle(this, title, description, url, thumbnail, mediaurl, enclosure);
    346368    }
     369}
    347370
    348     xmlFile.close();
     371void NewsSite::parseAtom(QDomDocument domDoc)
     372{
     373    QDomNodeList entries = domDoc.elementsByTagName(QString::fromLatin1("entry"));
    349374
     375    QDomNode itemNode;
     376    QString title, description, url, thumbnail, mediaurl, enclosure, imageURL, enclosure_type;
     377    for (unsigned int i = 0; i < entries.count(); i++) {
     378        itemNode = entries.item(i);
     379        title    = itemNode.namedItem(QString::fromLatin1("title")).toElement().text().simplifyWhiteSpace();
     380        if (!title.isNull())
     381            ReplaceHtmlChar(title);
     382
     383        QDomNode summNode = itemNode.namedItem(QString::fromLatin1("summary"));
     384        if (!summNode.isNull())
     385        {
     386            description = summNode.toElement().text().simplifyWhiteSpace();
     387            ReplaceHtmlChar(description);
     388        }           
     389        else
     390            description = QString::null;
     391       
     392        QDomNode linkNode = itemNode.namedItem(QString::fromLatin1("link"));
     393        if (!linkNode.isNull()){
     394            QDomAttr linkHref = linkNode.toElement().attributeNode("href");
     395            if(!linkHref.isNull())
     396                url = linkHref.value();
     397        }
     398        else
     399            url = QString::null;
     400
     401        new NewsArticle(this, title, description, url, QString::null, QString::null, QString::null);
     402    }
    350403}
    351404
    352405void NewsSite::slotGotData(const QByteArray& data,
  • mythnews/mythnews/newsengine.h

     
    2727#include <qobject.h>
    2828#include <qdatetime.h>
    2929#include <qcstring.h>
     30#include <qdom.h>
    3031
    3132class QUrlOperator;
    3233class QNetworkOperation;
     
    100101    void retrieve();
    101102    void stop();
    102103    void process();
     104    void parseRSS(QDomDocument domDoc);
     105    void parseAtom(QDomDocument domDoc);
    103106
    104107    bool     successful() const;
    105108    QString  errorMsg() const;