1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "PlaylistImporter.h"
28 #include <QStringList>
37 //Un-escape XML characters
43 g_xmlEscapeSequence[] =
54 const char *PlaylistImporter::supportedExtensions = "*.m3u *.m3u8 *.pls *.asx *.wpl";
57 ////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////
61 bool PlaylistImporter::importPlaylist(QStringList &fileList, const QString &playlistFile)
63 QFileInfo file(playlistFile);
64 QDir baseDir(file.canonicalPath());
66 QDir rootDir(baseDir);
67 while(rootDir.cdUp());
69 //Detect playlist type
70 playlist_t playlistType = isPlaylist(file.canonicalFilePath());
72 //Exit if not a playlist
73 if(playlistType == notPlaylist)
78 QFile data(playlistFile);
80 //Open file for reading
81 if(!data.open(QIODevice::ReadOnly))
86 //Skip very large files (parsing could take very long)
87 if((file.size() < 3) || (file.size() > 524288))
89 qWarning("File is very big. Probably not a Playlist. Rejecting...");
93 //Parse playlist depending on type
97 return parsePlaylist_m3u(data, fileList, baseDir, rootDir);
100 return parsePlaylist_pls(data, fileList, baseDir, rootDir);
103 return parsePlaylist_wpl(data, fileList, baseDir, rootDir);
111 ////////////////////////////////////////////////////////////
113 ////////////////////////////////////////////////////////////
115 PlaylistImporter::playlist_t PlaylistImporter::isPlaylist(const QString &fileName)
117 QFileInfo file (fileName);
119 if(file.suffix().compare("m3u", Qt::CaseInsensitive) == 0 || file.suffix().compare("m3u8", Qt::CaseInsensitive) == 0)
123 else if(file.suffix().compare("pls", Qt::CaseInsensitive) == 0)
127 else if(file.suffix().compare("asx", Qt::CaseInsensitive) == 0 || file.suffix().compare("wpl", Qt::CaseInsensitive) == 0)
137 bool PlaylistImporter::parsePlaylist_m3u(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
139 const QTextCodec *codec = QTextCodec::codecForName("System");
140 const bool preferUTF8 = data.fileName().endsWith(".m3u8", Qt::CaseInsensitive);
141 bool foundAtLeastOneFile = false;
148 QByteArray line = data.readLine();
152 filePath[0] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
153 filePath[1] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
154 filePath[2] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
158 filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
159 filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
160 filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
163 for(size_t i = 0; i < 3; i++)
165 if(!(filePath[i].isEmpty() || filePath[i].startsWith("#") || filePath[i].contains(QChar(QChar::ReplacementCharacter))))
167 QFileInfo filename(filePath[i]);
168 filename.setCaching(false);
169 fixFilePath(filename, baseDir, rootDir);
171 if(filename.exists() && filename.isFile())
173 qDebug("Found: \"%s\"", QUTF8(filePath[i]));
174 if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
176 fileList << filename.canonicalFilePath();
178 foundAtLeastOneFile = true;
185 //If we did not find any files yet, try UTF-16 now
186 if(!foundAtLeastOneFile)
188 const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
190 for(size_t i = 0; i < 2; i++)
192 QTextStream stream(&data);
193 stream.setAutoDetectUnicode(false);
194 stream.setCodec(codecs[i]);
197 while(!stream.atEnd())
199 QString filePath = stream.readLine().trimmed();
201 if(!(filePath.isEmpty() || filePath.startsWith("#") || filePath.contains(QChar(QChar::ReplacementCharacter))))
203 QFileInfo filename(filePath);
204 filename.setCaching(false);
205 fixFilePath(filename, baseDir, rootDir);
207 if(filename.exists() && filename.isFile())
209 if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
211 fileList << filename.canonicalFilePath();
213 foundAtLeastOneFile = true;
218 if(foundAtLeastOneFile) break;
222 return foundAtLeastOneFile;
225 bool PlaylistImporter::parsePlaylist_pls(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
227 QRegExp plsEntry("File(\\d+)=(.+)", Qt::CaseInsensitive);
228 const QTextCodec *codec = QTextCodec::codecForName("System");
229 bool foundAtLeastOneFile = false;
236 QByteArray line = data.readLine();
238 filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
239 filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
240 filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
242 for(size_t i = 0; i < 3; i++)
244 if(!filePath[i].contains(QChar(QChar::ReplacementCharacter)))
246 if(plsEntry.indexIn(filePath[i]) >= 0)
248 QFileInfo filename(QDir::fromNativeSeparators(plsEntry.cap(2)).trimmed());
249 filename.setCaching(false);
250 fixFilePath(filename, baseDir, rootDir);
252 if(filename.exists() && filename.isFile())
254 if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
256 fileList << filename.canonicalFilePath();
258 foundAtLeastOneFile = true;
266 //If we did not find any files yet, try UTF-16 now
267 if(!foundAtLeastOneFile)
269 const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
271 for(size_t i = 0; i < 2; i++)
273 QTextStream stream(&data);
274 stream.setAutoDetectUnicode(false);
275 stream.setCodec(codecs[i]);
278 while(!stream.atEnd())
280 QString filePath = stream.readLine().trimmed();
282 if(!filePath.contains(QChar(QChar::ReplacementCharacter)))
284 if(plsEntry.indexIn(filePath) >= 0)
286 QFileInfo filename(QDir::fromNativeSeparators(plsEntry.cap(2)).trimmed());
287 filename.setCaching(false);
288 fixFilePath(filename, baseDir, rootDir);
290 if(filename.exists() && filename.isFile())
292 if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
294 fileList << filename.canonicalFilePath();
296 foundAtLeastOneFile = true;
302 if(foundAtLeastOneFile) break;
306 return foundAtLeastOneFile;
309 bool PlaylistImporter::parsePlaylist_wpl(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
311 bool foundAtLeastOneFile = false;
313 QRegExp skipData("<!--(.+)-->", Qt::CaseInsensitive);
314 QRegExp wplEntry("<(media|ref)[^<>]*(src|href)=\"([^\"]+)\"[^<>]*>", Qt::CaseInsensitive);
316 skipData.setMinimal(true);
318 QByteArray buffer = data.readAll();
319 QString line = QString::fromUtf8(buffer.constData(), buffer.size()).simplified();
324 while((index = skipData.indexIn(line)) >= 0)
326 line.remove(index, skipData.matchedLength());
331 while((offset = wplEntry.indexIn(line, offset) + 1) > 0)
333 QFileInfo filename(QDir::fromNativeSeparators(unescapeXml(wplEntry.cap(3)).trimmed()));
334 filename.setCaching(false);
335 fixFilePath(filename, baseDir, rootDir);
337 if(filename.exists() && filename.isFile())
339 if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
341 fileList << filename.canonicalFilePath();
342 foundAtLeastOneFile = true;
347 return foundAtLeastOneFile;
350 void PlaylistImporter::fixFilePath(QFileInfo &filename, const QDir &baseDir, const QDir &rootDir)
352 if(filename.filePath().startsWith("/"))
354 while(filename.filePath().startsWith("/"))
356 filename.setFile(filename.filePath().mid(1));
358 filename.setFile(rootDir.filePath(filename.filePath()));
361 if(!filename.isAbsolute())
363 filename.setFile(baseDir.filePath(filename.filePath()));
367 QString &PlaylistImporter::unescapeXml(QString &str)
369 for(int i = 0; (g_xmlEscapeSequence[i].escape && g_xmlEscapeSequence[i].output); i++)
371 str.replace(g_xmlEscapeSequence[i].escape, g_xmlEscapeSequence[i].output);