File indexing completed on 2024-05-19 04:50:19

0001 /****************************************************************************************
0002  * Copyright (c) 2006,2007 Nikolaj Hald Nielsen <nhn@kde.org>                           *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef MAGNATUNEXMLPARSER_H
0018 #define MAGNATUNEXMLPARSER_H
0019 
0020 #include "MagnatuneDatabaseHandler.h"
0021 #include "MagnatuneMeta.h"
0022 
0023 #include <qdom.h>
0024 #include <QScopedPointer>
0025 #include <QString>
0026 #include <QDomElement>
0027 #include <QMap>
0028 
0029 #include <ThreadWeaver/Job>
0030 
0031 /**
0032 * Parser for the XML file from http://magnatune.com/info/album_info.xml
0033 *
0034 * @author Nikolaj Hald Nielsen
0035 */
0036 class MagnatuneXmlParser : public QObject, public ThreadWeaver::Job
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041 
0042     /**
0043      * Constructor
0044      * @param fileName The file to parse
0045      * @return Pointer to new object
0046      */
0047     explicit MagnatuneXmlParser( const QString &fileName );
0048 
0049     /**
0050      * The function that starts the actual work. Inherited from ThreadWeaver::Job
0051      * Note the work is performed in a separate thread
0052      */
0053     void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = nullptr) override;
0054 
0055     /**
0056      * Destructor
0057      * @return none
0058      */
0059     ~MagnatuneXmlParser() override;
0060 
0061     /**
0062      * Reads, and starts parsing, file. Should not be used directly.
0063      * @param filename The file to read
0064      */
0065     void readConfigFile( const QString &filename );
0066 
0067 
0068     void setDbHandler( MagnatuneDatabaseHandler * dbHandler );
0069 
0070 Q_SIGNALS:
0071     /** This signal is emitted when this job is being processed by a thread. */
0072     void started(ThreadWeaver::JobPointer);
0073     /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0074     void done(ThreadWeaver::JobPointer);
0075     /** This job has failed.
0076      * This signal is emitted when success() returns false after the job is executed. */
0077     void failed(ThreadWeaver::JobPointer);
0078 
0079     /**
0080      * Signal emitted when parsing is complete.
0081      */
0082     void doneParsing();
0083 
0084     private Q_SLOTS:
0085         
0086     /**
0087      * Called when the job has completed. Is executed in the GUI thread
0088      */
0089     void completeJob();
0090 
0091 private:
0092 
0093     QMap<QString, int> artistNameIdMap;
0094 
0095     QString m_currentArtist;
0096     QString m_currentArtistGenre;
0097 
0098     /**
0099      * Parses a DOM element
0100      * @param e The element to parse
0101      */
0102     void parseElement( const QDomElement &e );
0103 
0104     /**
0105      * Parses all children of a DOM element
0106      * @param e The element whose children is to be parsed
0107      */
0108     void parseChildren( const QDomElement &e );
0109 
0110     /**
0111      * Parse a DOM element representing an album
0112      * @param e The album element to parse
0113      */
0114     void parseAlbum( const QDomElement &e );
0115 
0116     /**
0117      * Parse a DOM element representing a track
0118      * @param e The track element to parse
0119      */
0120     void parseTrack( const QDomElement &e );
0121 
0122     /**
0123      * Parse the moods of a track
0124      * @param e The moods element to parse
0125      */
0126     void parseMoods( const QDomElement &e );
0127 
0128     QScopedPointer<Meta::MagnatuneAlbum> m_pCurrentAlbum;
0129     QScopedPointer<Meta::MagnatuneArtist> m_pCurrentArtist;
0130     QList<Meta::MagnatuneTrack*> m_currentAlbumTracksList;
0131     QStringList m_currentTrackMoodList;
0132 
0133     QString m_sFileName;
0134 
0135     int m_nNumberOfTracks;
0136     int m_nNumberOfAlbums;
0137     int m_nNumberOfArtists;
0138 
0139     MagnatuneDatabaseHandler * m_dbHandler;
0140 
0141 protected:
0142     void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0143     void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0144 };
0145 
0146 #endif