Line data Source code
1 : #include "OPMLParser.h"
2 :
3 : #include <QByteArray>
4 :
5 : #include "../utilities/FangLogging.h"
6 :
7 4 : OPMLParser::OPMLParser(QObject *parent) :
8 : FangObject(parent),
9 4 : file(),
10 4 : feedList(),
11 8 : result(ParserInterface::IN_PROGRESS)
12 : {
13 4 : }
14 :
15 4 : OPMLParser::~OPMLParser()
16 : {
17 4 : }
18 :
19 4 : void OPMLParser::parseFile(QString filename)
20 : {
21 4 : result = ParserInterface::IN_PROGRESS;
22 4 : QFile file(filename);
23 :
24 : // Open the file or die with an error.
25 4 : if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
26 0 : result = ParserInterface::FILE_ERROR;
27 0 : qCDebug(logParser) << "Couldn't read file: " << file.fileName();
28 :
29 0 : emit done();
30 :
31 0 : return;
32 : }
33 :
34 : // Read file!
35 4 : QByteArray data = file.readAll();
36 4 : xml.addData(data);
37 :
38 128 : while (!xml.atEnd()) {
39 120 : xml.readNext();
40 120 : if (xml.isStartElement()) {
41 : //qDebug() << "XML node: " << xml.name().toString() << " " << xml.prefix().toString();
42 :
43 : // Look for start of entries.
44 32 : QString name = xml.name().toString().toLower();
45 :
46 : // Start of outline.
47 32 : if ("outline" == name) {
48 66 : if (xml.attributes().hasAttribute("xmlUrl") &&
49 43 : (xml.attributes().hasAttribute("title") || xml.attributes().hasAttribute("text") ))
50 : {
51 24 : QString xmlUrl = xml.attributes().value("xmlUrl").toString();
52 24 : QString htmlUrl = xml.attributes().value("htmlUrl").toString();
53 12 : QString title;
54 :
55 24 : title = xml.attributes().value("title").toString();
56 12 : if (title.isEmpty()) {
57 6 : title = xml.attributes().value("text").toString();
58 : }
59 :
60 : // Oh shit! We got one!
61 12 : if (!xmlUrl.isEmpty() && !title.isEmpty()) {
62 12 : RawFeed* feed = new RawFeed();
63 :
64 12 : feed->title = title;
65 12 : feed->url = QUrl(xmlUrl);
66 12 : feed->siteURL = QUrl(htmlUrl);
67 :
68 12 : feedList.append(feed);
69 : }
70 12 : }
71 : }
72 32 : }
73 : }
74 :
75 : // Handle errors.
76 4 : if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError &&
77 0 : xml.error() != QXmlStreamReader::NotWellFormedError) {
78 0 : result = ParserInterface::PARSE_ERROR;
79 4 : } else if (feedList.size() > 0) {
80 4 : result = ParserInterface::OK;
81 : } else {
82 0 : result = ParserInterface::EMPTY_DOCUMENT;
83 : }
84 :
85 : //qDebug() << "Parser result (0 is good): " << result;
86 :
87 4 : emit done();
88 4 : }
|