File indexing completed on 2024-05-05 04:48:34

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Sergey Ivanov <123kash@gmail.com>                                 *
0003  * Copyright (c) 2013 Alberto Villa <avilla@FreeBSD.org>                                *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #ifndef MUSICBRAINZFINDER_H
0019 #define MUSICBRAINZFINDER_H
0020 
0021 #include "Version.h"
0022 #include "core/meta/forward_declarations.h"
0023 #include "musicbrainz/MusicBrainzXmlParser.h"
0024 #include "network/NetworkAccessManagerProxy.h"
0025 
0026 typedef QPair<Meta::TrackPtr, QVariantMap> TrackInfo;
0027 
0028 class MusicBrainzFinder : public QObject
0029 {
0030     Q_OBJECT
0031 
0032     public:
0033         explicit MusicBrainzFinder( QObject *parent = nullptr,
0034                                     const QString &host = "musicbrainz.org",
0035                                     const int port = 80,
0036                                     const QString &pathPrefix = "/ws/2",
0037                                     const QString &username = QString(),
0038                                     const QString &password = QString() );
0039 
0040         bool isRunning() const;
0041 
0042     Q_SIGNALS:
0043         void progressStep();
0044         void trackFound( const Meta::TrackPtr track, const QVariantMap tags );
0045         void done();
0046 
0047     public Q_SLOTS:
0048         void run( const Meta::TrackList &tracks );
0049 
0050         void lookUpByPUID( const Meta::TrackPtr &track, const QString &puid );
0051 
0052     private Q_SLOTS:
0053         void sendNewRequest();
0054         void gotAuthenticationRequest( const QNetworkReply *reply, QAuthenticator *authenticator );
0055         void gotReplyError( QNetworkReply::NetworkError code );
0056         void gotReply( QNetworkReply *reply );
0057 
0058         void parsingDone( ThreadWeaver::JobPointer _parser );
0059 
0060     private:
0061         QVariantMap guessMetadata( const Meta::TrackPtr &track ) const;
0062 
0063         QNetworkRequest compileRequest( QUrl &url );
0064         QNetworkRequest compileTrackRequest( const Meta::TrackPtr &track );
0065         QNetworkRequest compilePUIDRequest( const QString &puid );
0066         QNetworkRequest compileReleaseGroupRequest( const QString &releaseGroupID );
0067 
0068         void sendTrack( const Meta::TrackPtr &track, QVariantMap tags );
0069         void checkDone();
0070 
0071         QTimer *m_timer;
0072 
0073         QString mb_host;
0074         int mb_port;
0075         QString mb_pathPrefix;
0076         QString mb_username;
0077         QString mb_password;
0078 
0079         QNetworkAccessManager *net;
0080         QList<QPair<Meta::TrackPtr, QNetworkRequest> > m_requests;
0081         QMap<QNetworkReply *, Meta::TrackPtr> m_replies;
0082         QMap<MusicBrainzXmlParser *, Meta::TrackPtr> m_parsers;
0083 
0084         QMap<Meta::TrackPtr, QVariantMap> m_parsedMetadata;
0085 
0086         QMap<QString, QVariantMap> mb_releaseGroups;
0087         QMap<QString, QList<TrackInfo> > mb_queuedTracks;
0088 };
0089 
0090 #endif // MUSICBRAINZFINDER_H