File indexing completed on 2024-05-05 04:49:23

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
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 SYNCHRONIZATIONBASEJOB_H
0018 #define SYNCHRONIZATIONBASEJOB_H
0019 
0020 #include "core/meta/forward_declarations.h"
0021 #include "core/meta/support/MetaKeys.h"
0022 
0023 #include <QHash>
0024 #include <QObject>
0025 #include <QPair>
0026 #include <QSet>
0027 #include <QString>
0028 #include <QTimer>
0029 
0030 namespace Collections {
0031     class Collection;
0032     class QueryMaker;
0033 }
0034 
0035 class SynchronizationBaseJob : public QObject
0036 {
0037     Q_OBJECT
0038     public:
0039         enum State
0040         {
0041             NotStarted,
0042             ComparingArtists,
0043             ComparingAlbums,
0044             ComparingTracks,
0045             Syncing
0046         };
0047         Q_ENUM( State )
0048 
0049         enum InSet
0050         {
0051             OnlyInA,
0052             OnlyInB,
0053             InBoth
0054         };
0055         Q_ENUM( InSet )
0056 
0057         SynchronizationBaseJob();
0058         ~SynchronizationBaseJob() override;
0059 
0060         void setFilter( const QString &filter );
0061 
0062 
0063     public Q_SLOTS:
0064         virtual void synchronize();
0065 
0066     private Q_SLOTS:
0067         void slotTracksReady( const Meta::TrackList &artists );
0068         void slotAlbumsReady( const Meta::AlbumList &albums );
0069         void slotArtistsReady( const Meta::ArtistList &tracks );
0070         void slotQueryDone();
0071         void slotSyncTracks( const Meta::TrackList &tracks );
0072         void slotSyncQueryDone();
0073 
0074         void timeout();
0075 
0076     protected:
0077         void setCollectionA( Collections::Collection *collection );
0078         void setCollectionB( Collections::Collection *collection );
0079 
0080         /**
0081           * perform the actual synchronization in this method.
0082           * SynchronizationBaseJob will delete itself afterwards.
0083           */
0084         virtual void doSynchronization( const Meta::TrackList &tracks, InSet syncDirection, Collections::Collection *collA, Collections::Collection *collB ) = 0;
0085 
0086     private:
0087         Collections::QueryMaker* createQueryMaker( Collections::Collection *collection );
0088 
0089         void handleAlbumResult();
0090         void handleArtistResult();
0091         void handleTrackResult();
0092 
0093     private:
0094         Collections::QueryMaker* setupArtistQuery( Collections::Collection *coll );
0095         Collections::QueryMaker* setupAlbumQuery( Collections::Collection *coll );
0096         Collections::QueryMaker* setupTrackQuery( Collections::Collection *coll );
0097 
0098         State m_state;
0099         int m_currentResultCount;
0100         Collections::Collection *m_collectionA;
0101         Collections::Collection *m_collectionB;
0102 
0103         /** All the query makers we created and the collection they belong to. */
0104         QHash<Collections::QueryMaker*, Collections::Collection*> m_queryMakers;
0105 
0106         QSet<QString> m_artistsA;
0107         QSet<QString> m_artistsB;
0108         QSet<Meta::AlbumKey> m_albumsA;
0109         QSet<Meta::AlbumKey> m_albumsB;
0110         QSet<Meta::TrackKey> m_tracksA;
0111         QSet<Meta::TrackKey> m_tracksB;
0112         QHash<Meta::TrackKey, Meta::TrackPtr> m_keyToTrackA;
0113         QHash<Meta::TrackKey, Meta::TrackPtr> m_keyToTrackB;
0114         QHash<QString, InSet> m_artistResult;
0115         QHash<Meta::AlbumKey, InSet> m_albumResult;
0116         Meta::TrackList m_trackResultOnlyInA;
0117         Meta::TrackList m_trackResultOnlyInB;
0118         QTimer m_timer;
0119 };
0120 
0121 #endif
0122