1 /* AbstractPlaylistParser.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 "AbstractPlaylistParser.h"
22 #include "PlaylistParser.h"
23 #include "Utils/FileUtils.h"
24 #include "Utils/WebAccess/AsyncWebAccess.h"
25 #include "Utils/MetaData/MetaData.h"
26 #include "Utils/MetaData/MetaDataList.h"
27 
28 #include <QDir>
29 
30 struct AbstractPlaylistParser::Private
31 {
32 	MetaDataList		tracks;
33 	QString				fileContent;
34 	QString				directory;
35 	bool				parsed;
36 
PrivateAbstractPlaylistParser::Private37 	Private() :
38 		parsed(false)
39 	{}
40 };
41 
AbstractPlaylistParser(const QString & filename)42 AbstractPlaylistParser::AbstractPlaylistParser(const QString& filename)
43 {
44 	m = Pimpl::make<AbstractPlaylistParser::Private>();
45 
46 	QString pureFile;
47 
48 	Util::File::splitFilename(filename, m->directory, pureFile);
49 	Util::File::readFileIntoString(filename, m->fileContent);
50 }
51 
52 AbstractPlaylistParser::~AbstractPlaylistParser() = default;
53 
tracks(bool forceParse)54 MetaDataList AbstractPlaylistParser::tracks(bool forceParse)
55 {
56 	if(forceParse){
57 		m->parsed = false;
58 	}
59 
60 	if(!m->parsed){
61 		m->tracks.clear();
62 		parse();
63 	}
64 
65 	m->parsed = true;
66 
67 	return m->tracks;
68 }
69 
addTrack(const MetaData & md)70 void AbstractPlaylistParser::addTrack(const MetaData& md)
71 {
72 	m->tracks << md;
73 }
74 
addTracks(const MetaDataList & tracks)75 void AbstractPlaylistParser::addTracks(const MetaDataList& tracks)
76 {
77 	m->tracks << tracks;
78 }
79 
content() const80 const QString& AbstractPlaylistParser::content() const
81 {
82 	return m->fileContent;
83 }
84 
getAbsoluteFilename(const QString & filename) const85 QString AbstractPlaylistParser::getAbsoluteFilename(const QString& filename) const
86 {
87 	QString ret;
88 
89 	if(filename.isEmpty()){
90 		return "";
91 	}
92 
93 	if(Util::File::isWWW(filename)){
94 		return filename;
95 	}
96 
97 	if(!Util::File::isAbsolute(filename)){
98 		ret = m->directory + "/" + filename;
99 	}
100 	else{
101 		ret = filename;
102 	}
103 
104 	if(!Util::File::exists(ret)){
105 		ret = "";
106 	}
107 
108 	return Util::File::cleanFilename(ret);
109 }
110