File indexing completed on 2024-04-28 04:48:38

0001 /*
0002     SPDX-FileCopyrightText: 2005 Max Howell <max.howell@methylblue.com>
0003     SPDX-FileCopyrightText: 2007 Ian Monroe <ian@monroe.nu>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 // TODO error messages that vary depending on if the file is remote or not
0009 
0010 #include "playlistFile.h"
0011 #include "codeine.h"
0012 
0013 #include <KIO/StoredTransferJob>
0014 #include <KJobWidgets>
0015 #include <KLocalizedString>
0016 
0017 #include <QApplication>
0018 #include <QDebug>
0019 #include <QEvent>
0020 #include <QFile>
0021 #include <QGraphicsItem>
0022 
0023 PlaylistFile::PlaylistFile(const QUrl &url)
0024     : m_url(url)
0025     , m_isValid(false)
0026 {
0027     QApplication::setOverrideCursor(Qt::WaitCursor);
0028 
0029     const QString filename = m_url.fileName();
0030 
0031     if (filename.endsWith(QLatin1String(".pls"), Qt::CaseInsensitive))
0032         m_type = PLS;
0033     else if (filename.endsWith(QLatin1String(".m3u"), Qt::CaseInsensitive))
0034         m_type = M3U;
0035     else {
0036         m_type = Unknown;
0037         m_error = i18n("The file is not a playlist");
0038         QApplication::restoreOverrideCursor();
0039         return;
0040     }
0041 
0042     KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, KIO::HideProgressInfo | KIO::Overwrite);
0043     KJobWidgets::setWindow(job, Dragon::mainWindow());
0044     if (!job->exec()) {
0045         m_error = i18n("Dragon Player could not download the remote playlist: %1", url.toDisplayString());
0046         QApplication::restoreOverrideCursor();
0047         job->deleteLater();
0048         return;
0049     }
0050 
0051     QByteArray data = job->data();
0052 
0053     if (!data.isEmpty()) {
0054         QTextStream stream(&data);
0055         switch (m_type) {
0056         case M3U:
0057             parseM3uFile(stream);
0058             break;
0059         case PLS:
0060             parsePlsFile(stream);
0061             break;
0062         default:;
0063         }
0064 
0065         if (m_contents.isEmpty()) {
0066             m_error = i18n("<qt>The playlist, <i>'%1'</i>, could not be interpreted. Perhaps it is empty?</qt>", filename);
0067             m_isValid = false;
0068         }
0069     } else
0070         m_error = i18n("Dragon Player could not open the file: %1", filename);
0071 
0072     QApplication::restoreOverrideCursor();
0073 }
0074 
0075 PlaylistFile::~PlaylistFile()
0076 {
0077 }
0078 
0079 void PlaylistFile::parsePlsFile(QTextStream &stream)
0080 {
0081     QString line;
0082     while (!stream.atEnd()) {
0083         line = stream.readLine();
0084         if (line.startsWith(QLatin1String("File"))) {
0085             const QString tmp = line.section(QLatin1Char('='), -1);
0086             addToPlaylist(tmp);
0087         }
0088     }
0089     m_isValid = !m_contents.isEmpty();
0090 }
0091 
0092 void PlaylistFile::parseM3uFile(QTextStream &stream)
0093 {
0094     QString line;
0095     while (!stream.atEnd()) {
0096         line = stream.readLine();
0097 
0098         if (line.startsWith(QLatin1String("#EXTINF"), Qt::CaseInsensitive)) {
0099             continue;
0100         } else if (!line.startsWith(QLatin1Char('#')) && !line.isEmpty()) {
0101             addToPlaylist(line);
0102         }
0103     }
0104     m_isValid = !m_contents.isEmpty();
0105 }
0106 
0107 void PlaylistFile::addToPlaylist(const QString &line)
0108 {
0109     QUrl url;
0110 
0111     if (line.startsWith(QLatin1Char('/'))) { // absolute local file
0112         url = QUrl::fromLocalFile(line);
0113     } else { // relative file or other protocol
0114         const QUrl tmp = QUrl(line);
0115         if (tmp.scheme().isEmpty()) {
0116             url = QUrl::fromLocalFile(m_url.adjusted(QUrl::RemoveFilename).path() + QLatin1Char('/') + line);
0117         } else {
0118             url = tmp;
0119         }
0120     }
0121     m_contents += url;
0122 }