1 /* AbstractStreamHandler.cpp */
2
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program 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 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "AbstractStationHandler.h"
22
23 #include "Interfaces/PlaylistInterface.h"
24 #include "Components/Playlist/Playlist.h"
25
26 #include "Utils/Parser/StreamParser.h"
27 #include "Utils/MetaData/MetaDataList.h"
28 #include "Utils/Logger/Logger.h"
29 #include "Utils/Settings/Settings.h"
30
31 struct AbstractStationHandler::Private
32 {
33 PlaylistCreator* playlistCreator;
34 StreamParser* streamParser = nullptr;
35 StationPtr parsedStation;
36
PrivateAbstractStationHandler::Private37 Private(PlaylistCreator* playlistCreator) :
38 playlistCreator(playlistCreator)
39 {}
40 };
41
AbstractStationHandler(PlaylistCreator * playlistCreator,QObject * parent)42 AbstractStationHandler::AbstractStationHandler(PlaylistCreator* playlistCreator, QObject* parent) :
43 QObject(parent)
44 {
45 m = Pimpl::make<Private>(playlistCreator);
46 }
47
48 AbstractStationHandler::~AbstractStationHandler() = default;
49
createPlaylist(StationPtr station,MetaDataList & tracks)50 void AbstractStationHandler::createPlaylist(StationPtr station, MetaDataList& tracks)
51 {
52 QString playlistName;
53
54 if(GetSetting(Set::Stream_NewTab))
55 {
56 playlistName = station->name();
57 }
58
59 const auto index = m->playlistCreator->createPlaylist(tracks, playlistName);
60 auto playlist = m->playlistCreator->playlist(index);
61
62 playlist->changeTrack(0);
63 }
64
parseStation(StationPtr station)65 bool AbstractStationHandler::parseStation(StationPtr station)
66 {
67 if(m->streamParser)
68 {
69 return false;
70 }
71
72 m->parsedStation = station;
73
74 m->streamParser = new StreamParser(this);
75
76 connect(m->streamParser, &StreamParser::sigFinished, this, &AbstractStationHandler::parserFinished);
77 connect(m->streamParser, &StreamParser::sigUrlCountExceeded, this, &AbstractStationHandler::sigUrlCountExceeded);
78 connect(m->streamParser, &StreamParser::sigStopped, this, &AbstractStationHandler::parserStopped);
79
80 m->streamParser->parse(station->name(), station->url());
81
82 return true;
83 }
84
parserFinished(bool success)85 void AbstractStationHandler::parserFinished(bool success)
86 {
87 if(!success)
88 {
89 emit sigError();
90 spLog(Log::Warning, this) << "Stream parser finished with error";
91 }
92
93 else
94 {
95 MetaDataList tracks = m->streamParser->tracks();
96
97 if(!tracks.isEmpty())
98 {
99 createPlaylist(m->parsedStation, tracks);
100 }
101
102 emit sigDataAvailable();
103 }
104
105 sender()->deleteLater(); // m->stream_parser may be nullptr here
106 m->streamParser = nullptr;
107 }
108
stop()109 void AbstractStationHandler::stop()
110 {
111 if(m->streamParser && !m->streamParser->isStopped())
112 {
113 m->streamParser->stop();
114 }
115
116 m->streamParser = nullptr;
117 emit sigStopped();
118 }
119
parserStopped()120 void AbstractStationHandler::parserStopped()
121 {
122 sender()->deleteLater(); // m->stream_parser may be nullptr here
123 m->streamParser = nullptr;
124
125 emit sigStopped();
126 }
127
save(StationPtr station)128 bool AbstractStationHandler::save(StationPtr station)
129 {
130 return addNewStream(station);
131 }
132