1 /****************************************************************************************
2  * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
3  * Copyright (c) 2008 Jason A. Donenfeld <Jason@zx2c4.com>                              *
4  * Copyright (c) 2010 Casey Link <unnamedrambler@gmail.com>                             *
5  * Copyright (c) 2010 Teo Mrnjavac <teo@kde.org>                                        *
6  *                                                                                      *
7  * This program is free software; you can redistribute it and/or modify it under        *
8  * the terms of the GNU General Public License as published by the Free Software        *
9  * Foundation; either version 2 of the License, or (at your option) any later           *
10  * version.                                                                             *
11  *                                                                                      *
12  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
14  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
15  *                                                                                      *
16  * You should have received a copy of the GNU General Public License along with         *
17  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
18  ****************************************************************************************/
19 
20 #ifndef AMAROK_SQLCOLLECTIONLOCATION_H
21 #define AMAROK_SQLCOLLECTIONLOCATION_H
22 
23 #include "amarok_sqlcollection_export.h"
24 #include "core/collections/CollectionLocation.h"
25 
26 #include <KJob>
27 #include <KCompositeJob>
28 #include <QSet>
29 #include <QMap>
30 #include <QString>
31 
32 class OrganizeCollectionDelegateFactory;
33 
34 namespace Collections {
35 
36 class SqlCollection;
37 
38 class SqlCollectionLocation;
39 
40 /**
41  * @class TransferJob
42  * A simple class that provides KJob functionality (progress reporting, aborting, etc) for sqlcollectionlocation.
43  * It calls SqlCollectionLocation::startNextJob()
44  */
45 class TransferJob : public KCompositeJob
46 {
47     Q_OBJECT
48     public:
49         TransferJob( SqlCollectionLocation * location, const Transcoding::Configuration & configuration );
50 
51         void start() override;
52         bool addSubjob( KJob* job ) override;
53 
54         void emitInfo( const QString &message );
55     public Q_SLOTS:
56         /**
57          * A move or copy job finished
58          */
59         void slotJobFinished( KJob *job );
60     protected Q_SLOTS:
61         void slotResult( KJob *job ) override;
62         void doWork();
63         void propagateProcessedAmount( KJob *job, KJob::Unit unit, qulonglong amount);
64     protected:
65         bool doKill() override;
66     private:
67         SqlCollectionLocation* m_location;
68         bool m_killed;
69         Transcoding::Configuration m_transcodeFormat;
70 };
71 
72 class AMAROK_SQLCOLLECTION_EXPORT SqlCollectionLocation : public CollectionLocation
73 {
74     Q_OBJECT
75 
76     public:
77         explicit SqlCollectionLocation( SqlCollection *collection );
78         ~SqlCollectionLocation() override;
79 
80         QString prettyLocation() const override;
81         QStringList actualLocation() const override;
82         bool isWritable() const override;
83         bool isOrganizable() const override;
84 
85         bool remove( const Meta::TrackPtr &track );
86         bool insert( const Meta::TrackPtr &track, const QString &path ) override;
87 
88         //dependency injectors
89         void setOrganizeCollectionDelegateFactory( OrganizeCollectionDelegateFactory *fac );
90 
91     protected:
92         void showDestinationDialog( const Meta::TrackList &tracks,
93                                             bool removeSources,
94                                             const Transcoding::Configuration &configuration ) override;
95         void copyUrlsToCollection( const QMap<Meta::TrackPtr, QUrl> &sources,
96                                            const Transcoding::Configuration & configuration ) override;
97         void removeUrlsFromCollection( const Meta::TrackList &sources ) override;
98 
99     private Q_SLOTS:
100         void slotDialogAccepted();
101         void slotDialogRejected();
102         void slotJobFinished( KJob *job );
103         void slotRemoveJobFinished( KJob *job );
104         void slotTransferJobFinished( KJob *job );
105         void slotTransferJobAborted();
106 
107     private:
108         QUrl moodFile( const QUrl &track ) const;
109         void migrateLabels( const QMap<Meta::TrackPtr, QString> &trackMap );
110         bool startNextJob(const Transcoding::Configuration &configuration );
111         bool startNextRemoveJob();
112 
113         Collections::SqlCollection *m_collection;
114         OrganizeCollectionDelegateFactory *m_delegateFactory;
115         QMap<Meta::TrackPtr, QString> m_destinations;
116         QMap<Meta::TrackPtr, QUrl> m_sources;
117         Meta::TrackList m_removetracks;
118         QHash<Meta::TrackPtr, QUrl> m_originalUrls;
119         bool m_overwriteFiles;
120         QMap<KJob*, Meta::TrackPtr> m_jobs;
121         QMap<KJob*, Meta::TrackPtr> m_removejobs;
122         TransferJob* m_transferjob;
123         friend class TransferJob; // so the transfer job can run the jobs
124 };
125 
126 class SqlCollectionLocationFactory
127 {
128     public:
129         virtual SqlCollectionLocation* createSqlCollectionLocation() const = 0;
~SqlCollectionLocationFactory()130         virtual ~SqlCollectionLocationFactory() {}
131 };
132 
133 } //namespace Collections
134 
135 class AMAROK_SQLCOLLECTION_EXPORT OrganizeCollectionDelegate : public QObject
136 {
137     Q_OBJECT
138 public:
OrganizeCollectionDelegate()139     OrganizeCollectionDelegate() : QObject() {}
~OrganizeCollectionDelegate()140     ~ OrganizeCollectionDelegate() override {}
141 
142     virtual void setTracks( const Meta::TrackList &tracks ) = 0;
143     virtual void setFolders( const QStringList &folders ) = 0;
144     virtual void setIsOrganizing( bool organizing ) = 0;
145     virtual void setTranscodingConfiguration( const Transcoding::Configuration &configuration ) = 0;
146     virtual void setCaption( const QString &caption ) = 0;
147 
148     virtual void show() = 0;
149 
150     virtual bool overwriteDestinations() const = 0;
151     virtual QMap<Meta::TrackPtr, QString> destinations() const = 0;
152 
153 Q_SIGNALS:
154     void accepted();
155     void rejected();
156 };
157 
158 class OrganizeCollectionDelegateFactory
159 {
160 public:
161     virtual OrganizeCollectionDelegate* createDelegate() = 0;
~OrganizeCollectionDelegateFactory()162     virtual ~OrganizeCollectionDelegateFactory() {}
163 };
164 
165 #endif
166