1 /*
2  * Strawberry Music Player
3  * This file was part of Clementine.
4  * Copyright 2010, David Sansome <me@davidsansome.com>
5  * Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
6  *
7  * Strawberry 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  * Strawberry 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 Strawberry.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef URLHANDLER_H
23 #define URLHANDLER_H
24 
25 #include "config.h"
26 
27 #include <QtGlobal>
28 #include <QObject>
29 #include <QString>
30 #include <QUrl>
31 
32 #include "song.h"
33 
34 class UrlHandler : public QObject {
35   Q_OBJECT
36 
37  public:
38   explicit UrlHandler(QObject *parent = nullptr);
39 
40   // The URL scheme that this handler handles.
41   virtual QString scheme() const = 0;
42 
43   // Returned by StartLoading() and LoadNext(), indicates what the player should do when it wants to load a URL.
44   struct LoadResult {
45     enum Type {
46       // There wasn't a track available, and the player should move on to the next playlist item.
47       NoMoreTracks,
48 
49       // There might be another track available but the handler needs to do some work (eg. fetching a remote playlist) to find out.
50       // AsyncLoadComplete will be emitted later with the same original_url.
51       WillLoadAsynchronously,
52 
53       // There was a track available.  Its url is in stream_url.
54       TrackAvailable,
55 
56       // There was a error
57       Error,
58     };
59 
60     explicit LoadResult(const QUrl &original_url = QUrl(), const Type type = NoMoreTracks, const QUrl &stream_url = QUrl(), const Song::FileType filetype = Song::FileType_Stream, const int samplerate = -1, const int bit_depth = -1, const qint64 length_nanosec = -1, const QString &error = QString());
61 
62     explicit LoadResult(const QUrl &original_url, const Type type, const QString &error);
63 
64     // The url that the playlist item has in Url().
65     // Might be something unplayable like lastfm://...
66     QUrl original_url_;
67 
68     // Result type
69     Type type_;
70 
71     // The actual url to something that gstreamer can play.
72     QUrl stream_url_;
73 
74     // The type of the stream
75     Song::FileType filetype_;
76 
77     // Track sample rate
78     int samplerate_;
79 
80     // Track bit depth
81     int bit_depth_;
82 
83     // Track length
84     qint64 length_nanosec_;
85 
86     // Error message, if any
87     QString error_;
88   };
89 
90   // Called by the Player when a song starts loading - gives the handler a chance to do something clever to get a playable track.
91   virtual LoadResult StartLoading(const QUrl &url) { return LoadResult(url); }
92 
93  signals:
94   void AsyncLoadComplete(UrlHandler::LoadResult result);
95 
96 };
97 
98 #endif  // URLHANDLER_H
99