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

0001 /****************************************************************************************
0002  * Copyright (c) 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 JAMENDOXMLPARSER_H
0018 #define JAMENDOXMLPARSER_H
0019 
0020 #include "JamendoDatabaseHandler.h"
0021 
0022 #include <ThreadWeaver/Job>
0023 
0024 #include <QDomElement>
0025 #include <QMap>
0026 #include <QString>
0027 #include <QStringList>
0028 #include <QXmlStreamReader>
0029 
0030 /**
0031 * Parser for the XML file from http://imgjam.com/data/dbdump_artistalbumtrack.xml.gz
0032 *
0033 * @author Nikolaj Hald Nielsen
0034 */
0035 class JamendoXmlParser : public QObject, public ThreadWeaver::Job
0036 {
0037     Q_OBJECT
0038 
0039 public:
0040 
0041     /**
0042      * Constructor
0043      * @param fileName The file to parse 
0044      */
0045     explicit JamendoXmlParser( const QString &fileName );
0046 
0047     /**
0048      * The function that starts the actual work. Inherited from ThreadWeaver::Job 
0049      * Note the work is performed in a separate thread
0050      */
0051     void run(ThreadWeaver::JobPointer self = QSharedPointer<ThreadWeaver::Job>(), ThreadWeaver::Thread *thread = 0) override;
0052 
0053     /**
0054      * Destructor
0055      */
0056     ~JamendoXmlParser();
0057 
0058     /**
0059      * Reads, and starts parsing, file. Should not be used directly.
0060      * @param filename The file to read
0061      */
0062     void readConfigFile( const QString &filename );
0063 
0064     virtual void requestAbort ();
0065 
0066 Q_SIGNALS:
0067 
0068     /** This signal is emitted when this job is being processed by a thread. */
0069     void started(ThreadWeaver::JobPointer);
0070     /** This signal is emitted when the job has been finished (no matter if it succeeded or not). */
0071     void done(ThreadWeaver::JobPointer);
0072     /** This job has failed.
0073      * This signal is emitted when success() returns false after the job is executed. */
0074     void failed(ThreadWeaver::JobPointer);
0075 
0076     /**
0077      * Signal emitted when parsing is complete.
0078      */
0079     void doneParsing();
0080 
0081 private Q_SLOTS:
0082     /**
0083      * Called when the job has completed. Is executed in the GUI thread
0084      */
0085     void completeJob();
0086 
0087 private:
0088 
0089     JamendoDatabaseHandler * m_dbHandler;
0090     QXmlStreamReader m_reader;
0091     QString m_sFileName;
0092 
0093     QMap<int, QStringList> albumTags; //used for applying genres to individual tracks
0094 
0095     int m_nNumberOfTracks;
0096     int m_nNumberOfAlbums;
0097     int m_nNumberOfArtists;
0098 
0099     /**
0100      * Read a DOM element representing an artist
0101      */
0102     void readArtist();
0103     /**
0104      * Read a DOM element representing an album
0105      */
0106     void readAlbum();
0107     /**
0108      * Read a DOM element representing a track
0109      */
0110     void readTrack();
0111 
0112     void countTransaction();
0113     int m_currentArtistId;
0114     int m_currentAlbumId;
0115 
0116     int n_numberOfTransactions;
0117     int n_maxNumberOfTransactions;
0118     QHash< int, QString > m_id3GenreHash;
0119     QMap<int, int> m_albumArtistMap;
0120 
0121     bool m_aborted;
0122 
0123 protected:
0124     void defaultBegin(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0125     void defaultEnd(const ThreadWeaver::JobPointer& job, ThreadWeaver::Thread *thread) override;
0126 };
0127 
0128 #endif