1 /**
2  * \file musicbrainzclient.h
3  * MusicBrainz client.
4  *
5  * \b Project: Kid3
6  * \author Urs Fleisch
7  * \date 15 Sep 2005
8  *
9  * Copyright (C) 2005-2018  Urs Fleisch
10  *
11  * This file is part of Kid3.
12  *
13  * Kid3 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * Kid3 is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #pragma once
28 
29 #include <QObject>
30 #include "servertrackimporter.h"
31 #include "trackdata.h"
32 
33 class QByteArray;
34 class FingerprintCalculator;
35 
36 /**
37  * MusicBrainz client.
38  */
39 class MusicBrainzClient : public ServerTrackImporter {
40   Q_OBJECT
41 public:
42   /**
43    * Constructor.
44    *
45    * @param netMgr network access manager
46    * @param trackDataModel track data to be filled with imported values,
47    *                       is passed with filenames set
48    */
49   MusicBrainzClient(QNetworkAccessManager* netMgr,
50                     TrackDataModel* trackDataModel);
51 
52   /**
53    * Destructor.
54    */
55   virtual ~MusicBrainzClient() override = default;
56 
57   /**
58    * Name of import source.
59    * @return name.
60    */
61   virtual const char* name() const override;
62 
63   /** NULL-terminated array of server strings, 0 if not used */
64   virtual const char** serverList() const override;
65 
66   /** default server, 0 to disable */
67   virtual const char* defaultServer() const override;
68 
69   /** anchor to online help, 0 to disable */
70   virtual const char* helpAnchor() const override;
71 
72   /** configuration, 0 if not used */
73   virtual ServerImporterConfig* config() const override;
74 
75   /**
76    * Set configuration.
77    *
78    * @param cfg import server configuration, 0 if not used
79    */
80   virtual void setConfig(const ServerImporterConfig* cfg) override;
81 
82   /**
83    * Add the files in the file list.
84    */
85   virtual void start() override;
86 
87   /**
88    * Reset the client state.
89    */
90   virtual void stop() override;
91 
92 private slots:
93   void receiveBytes(const QByteArray& bytes);
94 
95   void receiveFingerprint(const QString& fingerprint, int duration, int error);
96 
97 private:
98   enum State {
99     Idle,
100     CalculatingFingerprint,
101     GettingIds,
102     GettingMetadata
103   };
104 
105   bool verifyIdIndex();
106   bool verifyTrackIndex();
107   void processNextStep();
108   void processNextTrack();
109 
110   FingerprintCalculator* m_fingerprintCalculator;
111   State m_state;
112   QVector<QString> m_filenameOfTrack;
113   QVector<QStringList> m_idsOfTrack;
114   int m_currentIndex;
115   ImportTrackDataVector m_currentTrackData;
116   QMap<QByteArray, QByteArray> m_headers;
117 };
118