summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/rssparser.cpp
blob: ec81409a4d95eb8f5c75eb8b8a0d94273b3372ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/***************************************************************************
 *   Copyright (C) 2004 by ljp                                             *
 *   lpotter@trolltech.com                                                 *
 *                                                                         *
 *   This program may be distributed under the terms of the Q Public       *
 *   License as defined by Trolltech AS of Norway and appearing in the     *
 *   file LICENSE.QPL included in the packaging of this file.              *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
 ***************************************************************************/
#include "rssparser.h"

#include <qstring.h>
#include <qmessagebox.h>
#include <qlist.h>

RssParser::RssParser()
{
    isItem = false;
}

RssParser::~RssParser()
{
    int size =  channel->rssItems.size();
    for (int i = 0; i < size; i ++) {
        delete channel->rssItems.at(i);
    }
    delete channel;
}

bool RssParser::startElement( const QString &, const QString & /*localName*/,const QString & qName, const QXmlAttributes &atts)
{
   if( qName == "rss") {
      channel = new rssChannel();
      channel->rssItems.resize( 0);
      return true;
   }
//   qWarning(qName + " %d",atts.length());
   if(qName == "item") {
      isItem = true;
      tag = NewItemTag;
      Item = new rssItem();
   }

   if(qName == "image") {
      tag = NewItemTag;
      image = new rssImage();
   }

   if(qName == "title") {
      tag = TitleTag;
   }

   if (qName == "description") {
      tag = DescriptionTag;
   }

   if( qName == "link") {
      tag = LinkTag;
   }

   if( qName == "pubDate") {
      tag = pubDateTag;
   }

//    if( qName == "enclosure") {
//       tag = EnclosureTag;
//    }

   if(atts.length() > 0/* && tag == EnclosureTag*/) {
//      qWarning(qName +" attributes %d", atts.length());
//       Item->attributes << qName;
//       for(int i=0; i < atts.length(); i++) {
//          Item->attributes << atts.qName(i) << atts.value(atts.qName(i));  
//       }
      
      QStringList *sList;
      sList = new QStringList();
      sList->append(qName);
      for(int i=0; i < atts.length(); i++) {
         sList->append( atts.qName(i));
         sList->append( atts.value(atts.qName(i)));
      }
      if(isItem)
         Item->attributes.append( sList);
      else
         channel->attributes.append( sList);
   }

   return true;
}

bool RssParser::endElement( const QString &, const QString &, const QString & qName )
{
    tag = NoneTag;
    if(qName == "item") {
        isItem = false;
        int size = channel->rssItems.size();
        channel->rssItems.resize( size + 1);
        channel->rssItems.insert( channel->rssItems.size() - 1, Item);
    }
    if(qName == "channel") {
//         isItem = false;
//         int size = channel->rssItems.size();
//         channel->rssItems.resize( size + 1);
//         channel->rssItems.insert( channel->rssItems.size() - 1, Item);
    }
    return true;
}

bool RssParser::characters( const QString & ch )
{
   if(!ch.isEmpty()) {
      if(isItem) {
//       qWarning("ch "+ch);
         switch(tag) {
         case NewItemTag:
            break;
         case TitleTag:
            Item->title = ch;
            break;
         case DescriptionTag:
            Item->description = ch;
            break;
         case LinkTag:
            Item->link = ch;
            break;
         case pubDateTag:
            Item->pubdate = ch;
            break;
         case NoneTag:
            break;
         };
      } else { //channel
         switch(tag) {
         case TitleTag:
            channel->title = ch;
            break;
         case DescriptionTag:
            channel->description = ch;
            break;
         case LinkTag:
            channel->link = ch;
            break;
         case pubDateTag:
            channel->pubdate = ch;
            break;
         case NoneTag:
         case NewItemTag:
            break;
         };
      }
   }
      return true;
}

bool RssParser::warning(const QXmlParseException &e)
{
      errorMessage = e.message();
//      QMessageBox::message("Warning",tr("<p>Sorry, could not find the requested document.</p>"));
      qWarning("Warning " + errorMessage);
      return true;
}

bool RssParser::error(const QXmlParseException &e)
{
      errorMessage = e.message();
//      QMessageBox::message("Error", "<p>" + errorMessage + "</p>");
      qWarning("Error: " + errorMessage);
      return true;
}

bool RssParser::fatalError(const QXmlParseException &e)
{
      errorMessage = e.message();
//       errorMessage += " line: " + e.lineNumber();
//       errorMessage += " col: " + e.columnNumber();
       qWarning("Fatal Error: "+ errorMessage);
       qWarning("line %d, col %d\n", e.lineNumber(),  e.columnNumber());
//      QMessageBox::message("Fatal Error", errorMessage );
      return false;
}

QVector<rssItem> &RssParser::getItems()
{   
    return channel->rssItems;
}

int RssParser::getSize()
{
   return channel->rssItems.size();
}

QStringList RssParser::getChannelInfo()
{
   QStringList ch;
   ch << channel->title <<  channel->description << channel->link << channel->pubdate << channel->copyright << channel->language;
   return ch;
}