add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / xmlloader.cpp
blob5aa06394a24491746a797af166016e5b6a5743f8
1 /*
2 Copyright (c) 2006 Gábor Lehel <illissius@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library 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. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "xmlloader.h"
21 #include "xmlloader_p.h"
22 #include "xmlloader_p.moc"
24 #include <QApplication>
26 MetaBundle::XmlLoader::XmlLoader(): m_aborted( false ), m_target( 0 )
28 m_reader.setContentHandler( this );
29 m_reader.setErrorHandler( this );
32 MetaBundle::XmlLoader::~XmlLoader() {}
34 bool MetaBundle::XmlLoader::load( QXmlInputSource *source, QObject *target )
36 m_target = target;
37 return m_reader.parse( source, false );
40 void MetaBundle::XmlLoader::abort()
42 m_aborted = true;
45 QString MetaBundle::XmlLoader::lastError() const
47 return m_lastError;
50 BundleList MetaBundle::XmlLoader::loadBundles( QXmlInputSource *source, bool *ok ) //static
52 return SimpleLoader( source, ok ).bundles;
55 void MetaBundle::XmlLoader::loadInThread( QXmlInputSource *source, QObject *target ) //static
57 ( new ThreadedLoader( source, target ) )->start();
60 void MetaBundle::XmlLoader::newAttribute( const QString &key, const QString &value )
62 if( key == "url" )
63 m_bundle.setUrl( value );
64 else if( key == "uniqueid" )
65 m_bundle.setUniqueId( value );
66 else if( key == "compilation" )
67 m_bundle.setCompilation( MetaBundle::CompilationYes );
68 else
69 m_attributes << QPair<QString, QString>( key, value );
72 void MetaBundle::XmlLoader::newTag( const QString &name, const QString &value )
74 static int start = 0; //most of the time, the columns should be in order
75 for( int i = start; i < NUM_COLUMNS; ++i )
76 if( name == exactColumnName( i ) )
78 switch( i )
80 case Artist:
81 case Composer:
82 case AlbumArtist:
83 case Year:
84 case Album:
85 case DiscNumber:
86 case Track:
87 case Bpm:
88 case Title:
89 case Genre:
90 case Comment:
91 case Length:
92 case Bitrate:
93 case Filesize:
94 case Type:
95 case SampleRate:
96 m_bundle.setExactText( i, value );
97 continue;
99 default:
100 continue;
102 start = i+1;
103 return;
105 for( int i = 0; i < start; ++i )
106 if( m_currentElement == exactColumnName( i ) )
108 switch( i )
110 case Artist:
111 case Composer:
112 case AlbumArtist:
113 case Year:
114 case Album:
115 case DiscNumber:
116 case Track:
117 case Bpm:
118 case Title:
119 case Genre:
120 case Comment:
121 case Length:
122 case Bitrate:
123 case Filesize:
124 case Type:
125 case SampleRate:
126 m_bundle.setExactText( i, value );
127 continue;
129 default:
130 continue;
132 start = i+1;
133 return;
137 void MetaBundle::XmlLoader::bundleLoaded()
139 m_bundle.checkExists();
140 emit newBundle( m_bundle, m_attributes );
141 if( m_target )
143 BundleLoadedEvent e( m_bundle, m_attributes );
144 QApplication::sendEvent( m_target, &e );
148 void MetaBundle::XmlLoader::errorEncountered( const QString &, int, int )
150 emit error( m_lastError );
151 if( m_target )
153 BundleLoadedEvent e( m_lastError );
154 QApplication::sendEvent( m_target, &e );
158 bool MetaBundle::XmlLoader::startElement( const QString &, const QString &localName, const QString &, const QXmlAttributes &atts )
160 if( localName == "item" )
162 m_bundle.clear();
163 m_attributes.clear();
164 for( int i = 0, n = atts.count(); i < n; ++i )
165 newAttribute( atts.localName( i ), atts.value( i ) );
167 m_currentElement.clear();
169 else
170 m_currentElement = localName;
172 return true;
175 bool MetaBundle::XmlLoader::endElement( const QString &, const QString &localName, const QString & )
177 if( localName == "item" )
179 bundleLoaded();
180 m_bundle.clear();
181 m_attributes.clear();
182 if( m_aborted )
183 return false;
186 m_currentElement.clear();
188 return true;
191 bool MetaBundle::XmlLoader::characters( const QString &ch )
193 if( m_currentElement.isNull() )
194 return true;
196 newTag( m_currentElement, ch );
198 return true;
201 bool MetaBundle::XmlLoader::endDocument()
203 if( !m_bundle.isEmpty() )
204 bundleLoaded();
206 return !m_aborted;
209 bool MetaBundle::XmlLoader::fatalError( const QXmlParseException &e )
211 if( !m_bundle.isEmpty() )
212 bundleLoaded();
214 m_lastError = QString( "Error loading XML: \"%1\", at line %2, column %3." )
215 .arg( e.message(), QString::number( e.lineNumber() ), QString::number( e.columnNumber() ) );
216 errorEncountered( e.message(), e.lineNumber(), e.columnNumber() );
218 return false;
221 #include "xmlloader.moc"