| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qm3uhandler.h" |
| 41 | #include <qmediaresource.h> |
| 42 | #include <QtCore/qiodevice.h> |
| 43 | #include <QtCore/qfileinfo.h> |
| 44 | #include <QtCore/qtextstream.h> |
| 45 | #include <QFile> |
| 46 | #include <QUrl> |
| 47 | |
| 48 | |
| 49 | class QM3uPlaylistReader : public QMediaPlaylistReader |
| 50 | { |
| 51 | public: |
| 52 | QM3uPlaylistReader(QIODevice *device) |
| 53 | :m_ownDevice(false), m_device(device), m_textStream(new QTextStream(m_device)) |
| 54 | { |
| 55 | readItem(); |
| 56 | } |
| 57 | |
| 58 | QM3uPlaylistReader(const QUrl& location) |
| 59 | :m_location(location), m_ownDevice(true) |
| 60 | { |
| 61 | QFile *f = new QFile(location.toLocalFile()); |
| 62 | if (f->open(flags: QIODevice::ReadOnly | QIODevice::Text)) { |
| 63 | m_device = f; |
| 64 | m_textStream = new QTextStream(m_device); |
| 65 | readItem(); |
| 66 | } else { |
| 67 | delete f; |
| 68 | m_device = 0; |
| 69 | m_textStream = 0; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | virtual ~QM3uPlaylistReader() |
| 74 | { |
| 75 | if (m_ownDevice) { |
| 76 | delete m_device; |
| 77 | } |
| 78 | delete m_textStream; |
| 79 | } |
| 80 | |
| 81 | bool atEnd() const override |
| 82 | { |
| 83 | //we can't just use m_textStream->atEnd(), |
| 84 | //for files with empty lines/comments at end |
| 85 | return nextResource.isNull(); |
| 86 | } |
| 87 | |
| 88 | QMediaContent readItem() override |
| 89 | { |
| 90 | QMediaContent item; |
| 91 | if (!nextResource.isNull()) |
| 92 | item = QMediaContent(nextResource); |
| 93 | |
| 94 | nextResource = QMediaContent(); |
| 95 | |
| 96 | while (m_textStream && !m_textStream->atEnd()) { |
| 97 | QString line = m_textStream->readLine().trimmed(); |
| 98 | if (line.isEmpty() || line[0] == '#' || line.size() > 4096) |
| 99 | continue; |
| 100 | |
| 101 | QUrl fileUrl = QUrl::fromLocalFile(localfile: line); |
| 102 | QUrl url(line); |
| 103 | |
| 104 | //m3u may contain url encoded entries or absolute/relative file names |
| 105 | //prefer existing file if any |
| 106 | QList<QUrl> candidates; |
| 107 | if (!m_location.isEmpty()) { |
| 108 | candidates << m_location.resolved(relative: fileUrl); |
| 109 | candidates << m_location.resolved(relative: url); |
| 110 | } |
| 111 | candidates << fileUrl; |
| 112 | candidates << url; |
| 113 | |
| 114 | for (const QUrl &candidate : qAsConst(t&: candidates)) { |
| 115 | if (QFile::exists(fileName: candidate.toLocalFile())) { |
| 116 | nextResource = candidate; |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (nextResource.isNull()) { |
| 122 | //assume the relative urls are file names, not encoded urls if m3u is local file |
| 123 | if (!m_location.isEmpty() && url.isRelative()) { |
| 124 | if (m_location.scheme() == QLatin1String("file" )) |
| 125 | nextResource = m_location.resolved(relative: fileUrl); |
| 126 | else |
| 127 | nextResource = m_location.resolved(relative: url); |
| 128 | } else { |
| 129 | nextResource = QMediaContent(QUrl::fromUserInput(userInput: line)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | return item; |
| 137 | } |
| 138 | |
| 139 | void close() override |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | QUrl m_location; |
| 145 | bool m_ownDevice; |
| 146 | QIODevice *m_device; |
| 147 | QTextStream *m_textStream; |
| 148 | QMediaContent nextResource; |
| 149 | }; |
| 150 | |
| 151 | class QM3uPlaylistWriter : public QMediaPlaylistWriter |
| 152 | { |
| 153 | public: |
| 154 | QM3uPlaylistWriter(QIODevice *device) |
| 155 | :m_device(device), m_textStream(new QTextStream(m_device)) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | virtual ~QM3uPlaylistWriter() |
| 160 | { |
| 161 | delete m_textStream; |
| 162 | } |
| 163 | |
| 164 | bool writeItem(const QMediaContent& item) override |
| 165 | { |
| 166 | *m_textStream << item.request().url().toString() << Qt::endl; |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | void close() override |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | QIODevice *m_device; |
| 176 | QTextStream *m_textStream; |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | QM3uPlaylistPlugin::QM3uPlaylistPlugin(QObject *parent) |
| 181 | :QMediaPlaylistIOPlugin(parent) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | QM3uPlaylistPlugin::~QM3uPlaylistPlugin() |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | bool QM3uPlaylistPlugin::canRead(QIODevice *device, const QByteArray &format) const |
| 190 | { |
| 191 | return device->isReadable() && (format == "m3u" || format == "m3u8" || format.isEmpty()); |
| 192 | } |
| 193 | |
| 194 | bool QM3uPlaylistPlugin::canRead(const QUrl& location, const QByteArray &format) const |
| 195 | { |
| 196 | if (!QFileInfo(location.toLocalFile()).isReadable()) |
| 197 | return false; |
| 198 | |
| 199 | if (format == "m3u" || format == "m3u8" ) |
| 200 | return true; |
| 201 | |
| 202 | if (!format.isEmpty()) |
| 203 | return false; |
| 204 | QString localFile = location.toLocalFile().toLower(); |
| 205 | return localFile.endsWith(s: QLatin1String("m3u" )) || localFile.endsWith(s: QLatin1String("m3u8" )); |
| 206 | } |
| 207 | |
| 208 | bool QM3uPlaylistPlugin::canWrite(QIODevice *device, const QByteArray &format) const |
| 209 | { |
| 210 | return device->isOpen() && device->isWritable() && (format == "m3u" || format == "m3u8" ); |
| 211 | } |
| 212 | |
| 213 | QMediaPlaylistReader *QM3uPlaylistPlugin::createReader(QIODevice *device, const QByteArray &format) |
| 214 | { |
| 215 | Q_UNUSED(format); |
| 216 | return new QM3uPlaylistReader(device); |
| 217 | } |
| 218 | |
| 219 | QMediaPlaylistReader *QM3uPlaylistPlugin::createReader(const QUrl& location, const QByteArray &format) |
| 220 | { |
| 221 | Q_UNUSED(format); |
| 222 | return new QM3uPlaylistReader(location); |
| 223 | } |
| 224 | |
| 225 | QMediaPlaylistWriter *QM3uPlaylistPlugin::createWriter(QIODevice *device, const QByteArray &format) |
| 226 | { |
| 227 | Q_UNUSED(format); |
| 228 | return new QM3uPlaylistWriter(device); |
| 229 | } |
| 230 | |
| 231 | |