Diffstat (limited to 'core/multimedia/opieplayer/rssparser.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | core/multimedia/opieplayer/rssparser.cpp | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/core/multimedia/opieplayer/rssparser.cpp b/core/multimedia/opieplayer/rssparser.cpp new file mode 100644 index 0000000..ec81409 --- a/dev/null +++ b/core/multimedia/opieplayer/rssparser.cpp | |||
@@ -0,0 +1,201 @@ | |||
1 | /*************************************************************************** | ||
2 | * Copyright (C) 2004 by ljp * | ||
3 | * lpotter@trolltech.com * | ||
4 | * * | ||
5 | * This program may be distributed under the terms of the Q Public * | ||
6 | * License as defined by Trolltech AS of Norway and appearing in the * | ||
7 | * file LICENSE.QPL included in the packaging of this file. * | ||
8 | * * | ||
9 | * This program is distributed in the hope that it will be useful, * | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * | ||
12 | ***************************************************************************/ | ||
13 | #include "rssparser.h" | ||
14 | |||
15 | #include <qstring.h> | ||
16 | #include <qmessagebox.h> | ||
17 | #include <qlist.h> | ||
18 | |||
19 | RssParser::RssParser() | ||
20 | { | ||
21 | isItem = false; | ||
22 | } | ||
23 | |||
24 | RssParser::~RssParser() | ||
25 | { | ||
26 | int size = channel->rssItems.size(); | ||
27 | for (int i = 0; i < size; i ++) { | ||
28 | delete channel->rssItems.at(i); | ||
29 | } | ||
30 | delete channel; | ||
31 | } | ||
32 | |||
33 | bool RssParser::startElement( const QString &, const QString & /*localName*/,const QString & qName, const QXmlAttributes &atts) | ||
34 | { | ||
35 | if( qName == "rss") { | ||
36 | channel = new rssChannel(); | ||
37 | channel->rssItems.resize( 0); | ||
38 | return true; | ||
39 | } | ||
40 | // qWarning(qName + " %d",atts.length()); | ||
41 | if(qName == "item") { | ||
42 | isItem = true; | ||
43 | tag = NewItemTag; | ||
44 | Item = new rssItem(); | ||
45 | } | ||
46 | |||
47 | if(qName == "image") { | ||
48 | tag = NewItemTag; | ||
49 | image = new rssImage(); | ||
50 | } | ||
51 | |||
52 | if(qName == "title") { | ||
53 | tag = TitleTag; | ||
54 | } | ||
55 | |||
56 | if (qName == "description") { | ||
57 | tag = DescriptionTag; | ||
58 | } | ||
59 | |||
60 | if( qName == "link") { | ||
61 | tag = LinkTag; | ||
62 | } | ||
63 | |||
64 | if( qName == "pubDate") { | ||
65 | tag = pubDateTag; | ||
66 | } | ||
67 | |||
68 | // if( qName == "enclosure") { | ||
69 | // tag = EnclosureTag; | ||
70 | // } | ||
71 | |||
72 | if(atts.length() > 0/* && tag == EnclosureTag*/) { | ||
73 | // qWarning(qName +" attributes %d", atts.length()); | ||
74 | // Item->attributes << qName; | ||
75 | // for(int i=0; i < atts.length(); i++) { | ||
76 | // Item->attributes << atts.qName(i) << atts.value(atts.qName(i)); | ||
77 | // } | ||
78 | |||
79 | QStringList *sList; | ||
80 | sList = new QStringList(); | ||
81 | sList->append(qName); | ||
82 | for(int i=0; i < atts.length(); i++) { | ||
83 | sList->append( atts.qName(i)); | ||
84 | sList->append( atts.value(atts.qName(i))); | ||
85 | } | ||
86 | if(isItem) | ||
87 | Item->attributes.append( sList); | ||
88 | else | ||
89 | channel->attributes.append( sList); | ||
90 | } | ||
91 | |||
92 | return true; | ||
93 | } | ||
94 | |||
95 | bool RssParser::endElement( const QString &, const QString &, const QString & qName ) | ||
96 | { | ||
97 | tag = NoneTag; | ||
98 | if(qName == "item") { | ||
99 | isItem = false; | ||
100 | int size = channel->rssItems.size(); | ||
101 | channel->rssItems.resize( size + 1); | ||
102 | channel->rssItems.insert( channel->rssItems.size() - 1, Item); | ||
103 | } | ||
104 | if(qName == "channel") { | ||
105 | // isItem = false; | ||
106 | // int size = channel->rssItems.size(); | ||
107 | // channel->rssItems.resize( size + 1); | ||
108 | // channel->rssItems.insert( channel->rssItems.size() - 1, Item); | ||
109 | } | ||
110 | return true; | ||
111 | } | ||
112 | |||
113 | bool RssParser::characters( const QString & ch ) | ||
114 | { | ||
115 | if(!ch.isEmpty()) { | ||
116 | if(isItem) { | ||
117 | // qWarning("ch "+ch); | ||
118 | switch(tag) { | ||
119 | case NewItemTag: | ||
120 | break; | ||
121 | case TitleTag: | ||
122 | Item->title = ch; | ||
123 | break; | ||
124 | case DescriptionTag: | ||
125 | Item->description = ch; | ||
126 | break; | ||
127 | case LinkTag: | ||
128 | Item->link = ch; | ||
129 | break; | ||
130 | case pubDateTag: | ||
131 | Item->pubdate = ch; | ||
132 | break; | ||
133 | case NoneTag: | ||
134 | break; | ||
135 | }; | ||
136 | } else { //channel | ||
137 | switch(tag) { | ||
138 | case TitleTag: | ||
139 | channel->title = ch; | ||
140 | break; | ||
141 | case DescriptionTag: | ||
142 | channel->description = ch; | ||
143 | break; | ||
144 | case LinkTag: | ||
145 | channel->link = ch; | ||
146 | break; | ||
147 | case pubDateTag: | ||
148 | channel->pubdate = ch; | ||
149 | break; | ||
150 | case NoneTag: | ||
151 | case NewItemTag: | ||
152 | break; | ||
153 | }; | ||
154 | } | ||
155 | } | ||
156 | return true; | ||
157 | } | ||
158 | |||
159 | bool RssParser::warning(const QXmlParseException &e) | ||
160 | { | ||
161 | errorMessage = e.message(); | ||
162 | // QMessageBox::message("Warning",tr("<p>Sorry, could not find the requested document.</p>")); | ||
163 | qWarning("Warning " + errorMessage); | ||
164 | return true; | ||
165 | } | ||
166 | |||
167 | bool RssParser::error(const QXmlParseException &e) | ||
168 | { | ||
169 | errorMessage = e.message(); | ||
170 | // QMessageBox::message("Error", "<p>" + errorMessage + "</p>"); | ||
171 | qWarning("Error: " + errorMessage); | ||
172 | return true; | ||
173 | } | ||
174 | |||
175 | bool RssParser::fatalError(const QXmlParseException &e) | ||
176 | { | ||
177 | errorMessage = e.message(); | ||
178 | // errorMessage += " line: " + e.lineNumber(); | ||
179 | // errorMessage += " col: " + e.columnNumber(); | ||
180 | qWarning("Fatal Error: "+ errorMessage); | ||
181 | qWarning("line %d, col %d\n", e.lineNumber(), e.columnNumber()); | ||
182 | // QMessageBox::message("Fatal Error", errorMessage ); | ||
183 | return false; | ||
184 | } | ||
185 | |||
186 | QVector<rssItem> &RssParser::getItems() | ||
187 | { | ||
188 | return channel->rssItems; | ||
189 | } | ||
190 | |||
191 | int RssParser::getSize() | ||
192 | { | ||
193 | return channel->rssItems.size(); | ||
194 | } | ||
195 | |||
196 | QStringList RssParser::getChannelInfo() | ||
197 | { | ||
198 | QStringList ch; | ||
199 | ch << channel->title << channel->description << channel->link << channel->pubdate << channel->copyright << channel->language; | ||
200 | return ch; | ||
201 | } | ||