1 /* This file is part of Clementine.
2    Copyright 2011, Arnaud Bienner <arnaud.bienner@gmail.com>
3    Copyright 2011, David Sansome <me@davidsansome.com>
4    Copyright 2012, 2014, John Maguire <john.maguire@gmail.com>
5    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
6 
7    Clementine is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11 
12    Clementine is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef CORE_URLHANDLER_H_
22 #define CORE_URLHANDLER_H_
23 
24 #include <QIcon>
25 #include <QObject>
26 #include <QUrl>
27 
28 class UrlHandler : public QObject {
29   Q_OBJECT
30 
31  public:
32   explicit UrlHandler(QObject* parent = nullptr);
33 
34   // The URL scheme that this handler handles.
35   virtual QString scheme() const = 0;
36   virtual QIcon icon() const;
37 
38   // Returned by StartLoading() and LoadNext(), indicates what the player
39   // should do when it wants to load a URL.
40   struct LoadResult {
41     enum Type {
42       // There wasn't a track available, and the player should move on to the
43       // next playlist item.
44       NoMoreTracks,
45 
46       // There might be another track available but the handler needs to do some
47       // work (eg. fetching a remote playlist) to find out.  AsyncLoadComplete
48       // will be emitted later with the same original_url.
49       WillLoadAsynchronously,
50 
51       // There was a track available.  Its url is in media_url.
52       TrackAvailable,
53     };
54 
55     LoadResult(const QUrl& original_url, Type type = NoMoreTracks,
56                const QUrl& media_url = QUrl(), qint64 length_nanosec_ = -1);
57 
58     // The url that the playlist item has in Url().
59     // Might be something unplayable like lastfm://...
60     QUrl original_url_;
61 
62     Type type_;
63 
64     // The actual url to something that gstreamer can play.
65     QUrl media_url_;
66 
67     // Track length, if we are able to get it only now
68     qint64 length_nanosec_;
69   };
70 
71   // Called by the Player when a song starts loading - gives the handler
72   // a chance to do something clever to get a playable track.
StartLoading(const QUrl & url)73   virtual LoadResult StartLoading(const QUrl& url) { return LoadResult(url); }
74 
75   // Called by the player when a song finishes - gives the handler a chance to
76   // get another track to play.
LoadNext(const QUrl & url)77   virtual LoadResult LoadNext(const QUrl& url) { return LoadResult(url); }
78 
79   // Functions to be warned when something happen to a track handled by
80   // UrlHandler.
TrackAboutToEnd()81   virtual void TrackAboutToEnd() {}
TrackSkipped()82   virtual void TrackSkipped() {}
83 
84  signals:
85   void AsyncLoadComplete(const UrlHandler::LoadResult& result);
86 };
87 
88 #endif  // CORE_URLHANDLER_H_
89