1 /****************************************************************************************
2  * Copyright (c) 2006,2007 Nikolaj Hald Nielsen <nhn@kde.org>                           *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #ifndef AMAROKMAGNATUNESTORE_H
18 #define AMAROKMAGNATUNESTORE_H
19 
20 
21 #include "core/support/Amarok.h"
22 #include "MagnatuneDownloadHandler.h"
23 #include "MagnatuneRedownloadHandler.h"
24 #include "MagnatuneXmlParser.h"
25 #include "MagnatuneDatabaseHandler.h"
26 #include "MagnatuneSqlCollection.h"
27 
28 #include "../ServiceBase.h"
29 
30 #include <KJob>
31 
32 #include <QCheckBox>
33 #include <QComboBox>
34 #include <QPushButton>
35 
36 
37 class MagnatuneInfoParser;
38 class MagnatuneNeedUpdateWidget;
39 
40 class MagnatuneServiceFactory : public ServiceFactory
41 {
42     Q_PLUGIN_METADATA(IID AmarokPluginFactory_iid FILE "amarok_service_magnatunestore.json")
43     Q_INTERFACES(Plugins::PluginFactory)
44     Q_OBJECT
45 
46     public:
47         MagnatuneServiceFactory();
~MagnatuneServiceFactory()48         ~MagnatuneServiceFactory() override {}
49 
50         void init() override;
51         QString name() override;
52         KConfigGroup config() override;
53 
possiblyContainsTrack(const QUrl & url)54         bool possiblyContainsTrack( const QUrl &url ) const override { return url.url().contains( "magnatune.com", Qt::CaseInsensitive ); }
55 };
56 
57 
58 /**
59 Amarok browser that displays all the music available at magnatune.com and makes it available for previewing and purchasing.
60 Implemented as a singleton
61 
62 @author Nikolaj Hald Nielsen <nhn@kde.org>
63 */
64 class MagnatuneStore: public ServiceBase
65 {
66     Q_OBJECT
67 
68 public:
69      /**
70      * Constructor
71      */
72     explicit MagnatuneStore( MagnatuneServiceFactory* parent, const char *name );
73     /**
74      * Destructor
75      */
76     ~MagnatuneStore() override;
77 
78     void setMembership( int type, const QString &username,  const QString &password );
79 
80     /**
81      * OGG, MP3 or LOFI
82      */
83     void setStreamType( int );
84 
85      /**
86      * Do not do expensive initializations before we are actually shown
87      */
88     void polish() override;
89    // bool updateContextView();
90 
collection()91     Collections::Collection * collection() override { return m_collection; }
92 
93     QString messages() override;
94     QString sendMessage( const QString &message ) override;
95 
96 public Q_SLOTS:
97     /**
98     * Slot for catching cancelled list downloads
99     */
100     void listDownloadCancelled();
101 
102     void downloadTrack( Meta::MagnatuneTrack * track );
103 
104     void downloadAlbum( Meta::MagnatuneAlbum * album );
105 
106     void showFavoritesPage();
107     void showHomePage();
108     void showRecommendationsPage();
109 
110     void addToFavorites( const QString &sku );
111     void removeFromFavorites( const QString &sku );
112 
113 private Q_SLOTS:
114     /**
115      * Slot called when the download album button is clicked. Starts a download
116      */
117     void download();
118 
119     void downloadSku( const QString &sku );
120 
121     /**
122      * Slot for receiving notification that the update button has been clicked.
123      */
124     void updateButtonClicked();
125 
126 
127     /**
128      * Slot for receiving notification when the Magnatune xml file has been downloaded.
129      * Triggers a parse of the file to get the info added to the database
130      * @param downLoadJob The calling download Job
131      */
132     void listDownloadComplete( KJob* downLoadJob );
133 
134 
135     /**
136      * Slot called when the parsing of the Magnatune xml file is completed.
137      * Triggers an update of the list view and the genre combo box
138      */
139     void doneParsing();
140 
141     /**
142      * Starts the process of redownloading a previously bought album
143      */
144     void processRedownload();
145 
146     /**
147      * Slot for receiving notifications of completed download operations
148      * @param success Was the operation a success?
149      */
150     void downloadCompleted( bool success );
151 
152 
153     /**
154      * Adds all tracks with a common mood to the playlist
155      * @param mood The mood of the tracks to add
156      */
157     void addMoodyTracksToPlaylist( const QString &mood, int count );
158 
159 
160      /**
161      * Checks if download button should be enabled
162      * @param selectedItem the new selected item
163      */
164     void itemSelected( CollectionTreeItem * selectedItem );
165 
166 
167     void moodMapReady(const QMap<QString, int> &map );
168     void moodyTracksReady( const Meta::TrackList &tracks );
169 
170     void timestampDownloadComplete( KJob * job );
171     void favoritesResult( KJob* addToFavoritesJob );
172 
173 private:
174     /**
175      * Helper function that initializes the button panel below the list view
176      */
177     void initBottomPanel();
178 
179     /**
180      * Helper function that initializes the genre selection panel above the list view
181      */
182     void initTopPanel();
183 
184     /**
185      * Starts downloading an updated track list xml file from
186      * http://magnatune.com/info/album_info.xml
187      * @return Currently always returns true
188      */
189     bool updateMagnatuneList();
190 
191     void checkForUpdates();
192 
193     /**
194      * Adds a magnatune preview track to the playlist.
195      * @param item The track to add
196      */
197     //void addTrackToPlaylist ( Meta::MagnatuneTrack  *item );
198 
199     void showSignupDialog();
200 
201     static MagnatuneStore *s_instance;
202 
203     QString m_currentInfoUrl;
204     MagnatuneDownloadHandler *m_downloadHandler;
205     MagnatuneRedownloadHandler *m_redownloadHandler;
206 
207     QPushButton *m_downloadAlbumButton;
208     MagnatuneNeedUpdateWidget *m_needUpdateWidget;
209 
210     QAction * m_updateAction;
211 
212     bool m_downloadInProgress;
213 
214     Meta::MagnatuneAlbum * m_currentAlbum;
215 
216     KIO::FileCopyJob * m_listDownloadJob;
217     KIO::StoredTransferJob* m_updateTimestampDownloadJob;
218     KIO::StoredTransferJob* m_favoritesJob;
219 
220     Collections::MagnatuneSqlCollection * m_collection;
221 
222     QString m_tempFileName;
223 
224     bool m_isMember;
225     int m_membershipType;
226     QString m_username;
227     QString m_password;
228 
229     int m_streamType;
230 
231     qulonglong m_magnatuneTimestamp;
232     ServiceSqlRegistry * m_registry;
233 
234     MagnatuneInfoParser * m_magnatuneInfoParser;
235 
236     QDialog *m_signupInfoWidget;
237 };
238 
239 
240 #endif
241