2011-01-31 21:02:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2016-01-03 15:53:42 +01:00
|
|
|
// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
|
2011-01-31 21:02:47 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2011-01-31 21:02:47 +01:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "PlaylistImporter.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
2013-10-21 15:00:55 +02:00
|
|
|
#include "Global.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
|
|
|
|
|
|
|
//Qt
|
2011-01-31 21:02:47 +01:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QDate>
|
|
|
|
#include <QTime>
|
|
|
|
#include <QDebug>
|
2011-12-10 01:59:45 +01:00
|
|
|
#include <QTextCodec>
|
2011-01-31 21:02:47 +01:00
|
|
|
|
|
|
|
//Un-escape XML characters
|
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
char *escape;
|
|
|
|
char *output;
|
|
|
|
}
|
|
|
|
g_xmlEscapeSequence[] =
|
|
|
|
{
|
|
|
|
{"&", "&"},
|
|
|
|
{"<", "<"},
|
|
|
|
{">", ">"},
|
|
|
|
{"'", "'"},
|
|
|
|
{" ", " "},
|
|
|
|
{""", "\""},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool PlaylistImporter::importPlaylist(QStringList &fileList, const QString &playlistFile)
|
|
|
|
{
|
|
|
|
QFileInfo file(playlistFile);
|
|
|
|
QDir baseDir(file.canonicalPath());
|
|
|
|
|
|
|
|
QDir rootDir(baseDir);
|
|
|
|
while(rootDir.cdUp());
|
|
|
|
|
|
|
|
//Detect playlist type
|
|
|
|
playlist_t playlistType = isPlaylist(file.canonicalFilePath());
|
|
|
|
|
|
|
|
//Exit if not a playlist
|
|
|
|
if(playlistType == notPlaylist)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile data(playlistFile);
|
|
|
|
|
|
|
|
//Open file for reading
|
|
|
|
if(!data.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
//Skip very large files (parsing could take very long)
|
2013-10-06 19:28:12 +02:00
|
|
|
if((file.size() < 3) || (file.size() > 524288))
|
2011-12-13 23:33:21 +01:00
|
|
|
{
|
|
|
|
qWarning("File is very big. Probably not a Playlist. Rejecting...");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-31 21:02:47 +01:00
|
|
|
//Parse playlist depending on type
|
|
|
|
switch(playlistType)
|
|
|
|
{
|
|
|
|
case m3uPlaylist:
|
|
|
|
return parsePlaylist_m3u(data, fileList, baseDir, rootDir);
|
|
|
|
break;
|
|
|
|
case plsPlaylist:
|
|
|
|
return parsePlaylist_pls(data, fileList, baseDir, rootDir);
|
|
|
|
break;
|
|
|
|
case wplPlaylist:
|
|
|
|
return parsePlaylist_wpl(data, fileList, baseDir, rootDir);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Private Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
PlaylistImporter::playlist_t PlaylistImporter::isPlaylist(const QString &fileName)
|
|
|
|
{
|
|
|
|
QFileInfo file (fileName);
|
|
|
|
|
|
|
|
if(file.suffix().compare("m3u", Qt::CaseInsensitive) == 0 || file.suffix().compare("m3u8", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return m3uPlaylist;
|
|
|
|
}
|
|
|
|
else if(file.suffix().compare("pls", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return plsPlaylist;
|
|
|
|
}
|
|
|
|
else if(file.suffix().compare("asx", Qt::CaseInsensitive) == 0 || file.suffix().compare("wpl", Qt::CaseInsensitive) == 0)
|
|
|
|
{
|
|
|
|
return wplPlaylist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return notPlaylist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaylistImporter::parsePlaylist_m3u(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
|
|
|
|
{
|
2011-12-10 01:59:45 +01:00
|
|
|
const QTextCodec *codec = QTextCodec::codecForName("System");
|
2011-12-13 23:33:21 +01:00
|
|
|
const bool preferUTF8 = data.fileName().endsWith(".m3u8", Qt::CaseInsensitive);
|
|
|
|
bool foundAtLeastOneFile = false;
|
|
|
|
|
|
|
|
data.reset();
|
2011-12-10 01:59:45 +01:00
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
while(!data.atEnd())
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
QString filePath[3];
|
2011-12-13 23:33:21 +01:00
|
|
|
QByteArray line = data.readLine();
|
2011-01-31 21:02:47 +01:00
|
|
|
|
2011-12-09 00:34:07 +01:00
|
|
|
if(preferUTF8)
|
|
|
|
{
|
|
|
|
filePath[0] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
|
2011-12-10 01:59:45 +01:00
|
|
|
filePath[1] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
|
2011-12-09 00:34:07 +01:00
|
|
|
filePath[2] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
|
|
|
|
}
|
|
|
|
else
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-10 01:59:45 +01:00
|
|
|
filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
|
2011-12-09 00:34:07 +01:00
|
|
|
filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
|
|
|
|
filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
|
|
|
|
}
|
2011-01-31 21:02:47 +01:00
|
|
|
|
2011-12-09 00:34:07 +01:00
|
|
|
for(size_t i = 0; i < 3; i++)
|
|
|
|
{
|
2011-12-13 23:33:21 +01:00
|
|
|
if(!(filePath[i].isEmpty() || filePath[i].startsWith("#") || filePath[i].contains(QChar(QChar::ReplacementCharacter))))
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
QFileInfo filename(filePath[i]);
|
|
|
|
filename.setCaching(false);
|
|
|
|
fixFilePath(filename, baseDir, rootDir);
|
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
if(filename.exists() && filename.isFile())
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
qDebug("Found: \"%s\"", MUTILS_UTF8(filePath[i]));
|
2011-12-09 00:34:07 +01:00
|
|
|
if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
|
|
|
|
{
|
|
|
|
fileList << filename.canonicalFilePath();
|
|
|
|
}
|
2011-12-13 23:33:21 +01:00
|
|
|
foundAtLeastOneFile = true;
|
2011-12-09 00:34:07 +01:00
|
|
|
break;
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 23:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//If we did not find any files yet, try UTF-16 now
|
|
|
|
if(!foundAtLeastOneFile)
|
|
|
|
{
|
|
|
|
const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
QTextStream stream(&data);
|
|
|
|
stream.setAutoDetectUnicode(false);
|
|
|
|
stream.setCodec(codecs[i]);
|
|
|
|
stream.seek(0i64);
|
2011-01-31 21:02:47 +01:00
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
while(!stream.atEnd())
|
|
|
|
{
|
|
|
|
QString filePath = stream.readLine().trimmed();
|
|
|
|
|
|
|
|
if(!(filePath.isEmpty() || filePath.startsWith("#") || filePath.contains(QChar(QChar::ReplacementCharacter))))
|
|
|
|
{
|
|
|
|
QFileInfo filename(filePath);
|
|
|
|
filename.setCaching(false);
|
|
|
|
fixFilePath(filename, baseDir, rootDir);
|
|
|
|
|
|
|
|
if(filename.exists() && filename.isFile())
|
|
|
|
{
|
|
|
|
if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
|
|
|
|
{
|
|
|
|
fileList << filename.canonicalFilePath();
|
|
|
|
}
|
|
|
|
foundAtLeastOneFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundAtLeastOneFile) break;
|
|
|
|
}
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
2013-10-06 19:28:12 +02:00
|
|
|
return foundAtLeastOneFile;
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaylistImporter::parsePlaylist_pls(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
|
|
|
|
{
|
|
|
|
QRegExp plsEntry("File(\\d+)=(.+)", Qt::CaseInsensitive);
|
2011-12-10 01:59:45 +01:00
|
|
|
const QTextCodec *codec = QTextCodec::codecForName("System");
|
2011-12-13 23:33:21 +01:00
|
|
|
bool foundAtLeastOneFile = false;
|
|
|
|
|
|
|
|
data.reset();
|
|
|
|
|
|
|
|
while(!data.atEnd())
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
QString filePath[3];
|
2011-12-13 23:33:21 +01:00
|
|
|
QByteArray line = data.readLine();
|
2011-01-31 21:02:47 +01:00
|
|
|
|
2011-12-10 01:59:45 +01:00
|
|
|
filePath[0] = QString(QDir::fromNativeSeparators(codec->toUnicode(line.constData(), line.size()).trimmed()));
|
|
|
|
filePath[1] = QString(QDir::fromNativeSeparators(QString::fromLatin1(line.constData(), line.size()).trimmed()));
|
|
|
|
filePath[2] = QString(QDir::fromNativeSeparators(QString::fromUtf8(line.constData(), line.size()).trimmed()));
|
|
|
|
|
2011-12-09 00:34:07 +01:00
|
|
|
for(size_t i = 0; i < 3; i++)
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
if(!filePath[i].contains(QChar(QChar::ReplacementCharacter)))
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
if(plsEntry.indexIn(filePath[i]) >= 0)
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
2011-12-09 00:34:07 +01:00
|
|
|
QFileInfo filename(QDir::fromNativeSeparators(plsEntry.cap(2)).trimmed());
|
|
|
|
filename.setCaching(false);
|
|
|
|
fixFilePath(filename, baseDir, rootDir);
|
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
if(filename.exists() && filename.isFile())
|
2011-12-09 00:34:07 +01:00
|
|
|
{
|
|
|
|
if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
|
|
|
|
{
|
|
|
|
fileList << filename.canonicalFilePath();
|
|
|
|
}
|
2011-12-13 23:33:21 +01:00
|
|
|
foundAtLeastOneFile = true;
|
2011-12-09 00:34:07 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 23:33:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//If we did not find any files yet, try UTF-16 now
|
|
|
|
if(!foundAtLeastOneFile)
|
|
|
|
{
|
|
|
|
const char* codecs[2] = {"UTF-16LE", "UTF-16BE"};
|
|
|
|
|
|
|
|
for(size_t i = 0; i < 2; i++)
|
|
|
|
{
|
|
|
|
QTextStream stream(&data);
|
|
|
|
stream.setAutoDetectUnicode(false);
|
|
|
|
stream.setCodec(codecs[i]);
|
|
|
|
stream.seek(0i64);
|
2011-01-31 21:02:47 +01:00
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
while(!stream.atEnd())
|
|
|
|
{
|
|
|
|
QString filePath = stream.readLine().trimmed();
|
|
|
|
|
|
|
|
if(!filePath.contains(QChar(QChar::ReplacementCharacter)))
|
|
|
|
{
|
|
|
|
if(plsEntry.indexIn(filePath) >= 0)
|
|
|
|
{
|
|
|
|
QFileInfo filename(QDir::fromNativeSeparators(plsEntry.cap(2)).trimmed());
|
|
|
|
filename.setCaching(false);
|
|
|
|
fixFilePath(filename, baseDir, rootDir);
|
|
|
|
|
|
|
|
if(filename.exists() && filename.isFile())
|
|
|
|
{
|
|
|
|
if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
|
|
|
|
{
|
|
|
|
fileList << filename.canonicalFilePath();
|
|
|
|
}
|
|
|
|
foundAtLeastOneFile = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundAtLeastOneFile) break;
|
|
|
|
}
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
2013-10-06 19:28:12 +02:00
|
|
|
return foundAtLeastOneFile;
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaylistImporter::parsePlaylist_wpl(QFile &data, QStringList &fileList, const QDir &baseDir, const QDir &rootDir)
|
|
|
|
{
|
2013-10-06 19:28:12 +02:00
|
|
|
bool foundAtLeastOneFile = false;
|
|
|
|
|
2011-01-31 21:02:47 +01:00
|
|
|
QRegExp skipData("<!--(.+)-->", Qt::CaseInsensitive);
|
|
|
|
QRegExp wplEntry("<(media|ref)[^<>]*(src|href)=\"([^\"]+)\"[^<>]*>", Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
skipData.setMinimal(true);
|
|
|
|
|
|
|
|
QByteArray buffer = data.readAll();
|
|
|
|
QString line = QString::fromUtf8(buffer.constData(), buffer.size()).simplified();
|
|
|
|
buffer.clear();
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
while((index = skipData.indexIn(line)) >= 0)
|
|
|
|
{
|
|
|
|
line.remove(index, skipData.matchedLength());
|
|
|
|
}
|
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
while((offset = wplEntry.indexIn(line, offset) + 1) > 0)
|
|
|
|
{
|
|
|
|
QFileInfo filename(QDir::fromNativeSeparators(unescapeXml(wplEntry.cap(3)).trimmed()));
|
|
|
|
filename.setCaching(false);
|
|
|
|
fixFilePath(filename, baseDir, rootDir);
|
|
|
|
|
2011-12-13 23:33:21 +01:00
|
|
|
if(filename.exists() && filename.isFile())
|
2011-01-31 21:02:47 +01:00
|
|
|
{
|
|
|
|
if(isPlaylist(filename.canonicalFilePath()) == notPlaylist)
|
|
|
|
{
|
|
|
|
fileList << filename.canonicalFilePath();
|
2013-10-06 19:28:12 +02:00
|
|
|
foundAtLeastOneFile = true;
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-06 19:28:12 +02:00
|
|
|
return foundAtLeastOneFile;
|
2011-01-31 21:02:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlaylistImporter::fixFilePath(QFileInfo &filename, const QDir &baseDir, const QDir &rootDir)
|
|
|
|
{
|
|
|
|
if(filename.filePath().startsWith("/"))
|
|
|
|
{
|
|
|
|
while(filename.filePath().startsWith("/"))
|
|
|
|
{
|
|
|
|
filename.setFile(filename.filePath().mid(1));
|
|
|
|
}
|
|
|
|
filename.setFile(rootDir.filePath(filename.filePath()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!filename.isAbsolute())
|
|
|
|
{
|
|
|
|
filename.setFile(baseDir.filePath(filename.filePath()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString &PlaylistImporter::unescapeXml(QString &str)
|
|
|
|
{
|
|
|
|
for(int i = 0; (g_xmlEscapeSequence[i].escape && g_xmlEscapeSequence[i].output); i++)
|
|
|
|
{
|
|
|
|
str.replace(g_xmlEscapeSequence[i].escape, g_xmlEscapeSequence[i].output);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
2015-05-18 18:56:33 +02:00
|
|
|
|
|
|
|
const char *const *const PlaylistImporter::getSupportedExtensions(void)
|
|
|
|
{
|
|
|
|
static const char *const s_supportedExtensions[] =
|
|
|
|
{
|
|
|
|
"m3u", "m3u8", "pls", "asx", "wpl", NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
return s_supportedExtensions;
|
|
|
|
}
|