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