File indexing completed on 2024-05-19 04:55:58

0001 /**
0002  * \file batchimporter.h
0003  * Batch importer.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 3 Jan 2013
0008  *
0009  * Copyright (C) 2013-2024  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include <QObject>
0030 #include "trackdata.h"
0031 #include "batchimportprofile.h"
0032 #include "iabortable.h"
0033 
0034 class QNetworkAccessManager;
0035 class DownloadClient;
0036 class ServerImporter;
0037 class TrackDataModel;
0038 class AlbumListModel;
0039 
0040 /**
0041  * Batch importer.
0042  */
0043 class KID3_CORE_EXPORT BatchImporter : public QObject, public IAbortable {
0044   Q_OBJECT
0045   Q_ENUMS(ImportEventType)
0046 public:
0047   /** Events occurring during batch import. */
0048   enum ImportEventType {
0049     ReadingDirectory,
0050     Started,
0051     SourceSelected,
0052     QueryingAlbumList,
0053     FetchingTrackList,
0054     TrackListReceived,
0055     FetchingCoverArt,
0056     CoverArtReceived,
0057     Finished,
0058     Aborted,
0059     Error
0060   };
0061 
0062   /**
0063    * Constructor.
0064    * @param netMgr network access manager
0065    */
0066   explicit BatchImporter(QNetworkAccessManager* netMgr);
0067 
0068   /**
0069    * Destructor.
0070    */
0071   ~BatchImporter() override = default;
0072 
0073   /**
0074    * Check if operation is aborted.
0075    *
0076    * @return true if aborted.
0077    */
0078   bool isAborted() const override;
0079 
0080   /**
0081    * Clear state which is reported by isAborted().
0082    */
0083   void clearAborted() override;
0084 
0085   /**
0086    * Set importers.
0087    * @param importers available importers
0088    * @param trackDataModel track data model used by importers
0089    */
0090   void setImporters(const QList<ServerImporter*>& importers,
0091                     TrackDataModel* trackDataModel);
0092 
0093   /**
0094    * Start batch import.
0095    * @param trackLists list of track data vectors with album tracks
0096    * @param profile batch import profile
0097    * @param tagVersion import destination tag version
0098    */
0099   void start(const QList<ImportTrackDataVector>& trackLists,
0100              const BatchImportProfile& profile,
0101              Frame::TagVersion tagVersion);
0102 
0103   /**
0104    * Set frame filter to be used when importing.
0105    * @param flt frame filter
0106    */
0107   void setFrameFilter(const FrameFilter& flt) { m_frameFilter = flt; }
0108 
0109   /**
0110    * Emit a report event.
0111    * @param type type of event
0112    * @param text additional message
0113    */
0114   void emitReportImportEvent(ImportEventType type,
0115                              const QString& text) {
0116     emit reportImportEvent(type, text);
0117   }
0118 
0119 signals:
0120   /**
0121    * Report event.
0122    * @param type type of event, enum BatchImporter::ImportEventType
0123    * @param text additional message
0124    */
0125   void reportImportEvent(int type, const QString& text);
0126 
0127   /**
0128    * Emitted when the batch import is finished.
0129    */
0130   void finished();
0131 
0132 public slots:
0133   /**
0134    * Abort batch import.
0135    */
0136   void abort() override;
0137 
0138 private slots:
0139   void onFindFinished(const QByteArray& searchStr);
0140   void onFindProgress(const QString& text, int step, int total);
0141   void onAlbumFinished(const QByteArray& albumStr);
0142   void onAlbumProgress(const QString& text, int step, int total);
0143   void onImageDownloaded(const QByteArray& data, const QString& mimeType,
0144                          const QString& url);
0145 
0146 private:
0147   enum State {
0148     Idle,
0149     CheckNextTrackList,
0150     CheckNextSource,
0151     GettingAlbumList,
0152     CheckNextAlbum,
0153     GettingTracks,
0154     GettingCover,
0155     CheckIfDone,
0156     ImportAborted
0157   };
0158 
0159   void stateTransition();
0160   ServerImporter* getImporter(const QString& name);
0161 
0162   DownloadClient* m_downloadClient;
0163   QList<ServerImporter*> m_importers;
0164   ServerImporter* m_currentImporter;
0165   TrackDataModel* m_trackDataModel;
0166   AlbumListModel* m_albumModel;
0167   QString m_albumListItemText;
0168   QString m_albumListItemCategory;
0169   QString m_albumListItemId;
0170   QList<ImportTrackDataVector> m_trackLists;
0171   BatchImportProfile m_profile;
0172   Frame::TagVersion m_tagVersion;
0173   State m_state;
0174   int m_trackListNr;
0175   int m_sourceNr;
0176   int m_albumNr;
0177   int m_requestedData;
0178   int m_importedData;
0179   QString m_currentArtist;
0180   QString m_currentAlbum;
0181   FrameFilter m_frameFilter;
0182 };