plugins/multimedia/m3u/qm3uhandler.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qm3uhandler.h"
       
    43 #include <qmediaresource.h>
       
    44 #include <QtCore/qiodevice.h>
       
    45 #include <QtCore/qfileinfo.h>
       
    46 #include <QtCore/qtextstream.h>
       
    47 #include <QFile>
       
    48 #include <QUrl>
       
    49 
       
    50 
       
    51 class QM3uPlaylistReader : public QMediaPlaylistReader
       
    52 {
       
    53 public:
       
    54     QM3uPlaylistReader(QIODevice *device)
       
    55         :m_ownDevice(false), m_device(device), m_textStream(new QTextStream(m_device))
       
    56     {
       
    57         readItem();
       
    58     }
       
    59 
       
    60     QM3uPlaylistReader(const QUrl& location)
       
    61         :m_ownDevice(true)
       
    62     {
       
    63         QFile *f = new QFile(location.toLocalFile());
       
    64         if (f->open(QIODevice::ReadOnly | QIODevice::Text)) {
       
    65             m_device = f;
       
    66             m_textStream = new QTextStream(m_device);
       
    67             readItem();
       
    68         } else {
       
    69             delete f;
       
    70             m_device = 0;
       
    71             m_textStream = 0;
       
    72         }
       
    73     }
       
    74 
       
    75     virtual ~QM3uPlaylistReader()
       
    76     {
       
    77         if (m_ownDevice) {
       
    78             delete m_device;
       
    79         }
       
    80         delete m_textStream;
       
    81     }
       
    82 
       
    83     virtual bool atEnd() const
       
    84     {
       
    85         //we can't just use m_textStream->atEnd(),
       
    86         //for files with empty lines/comments at end
       
    87         return nextResource.isNull();
       
    88     }
       
    89 
       
    90     virtual QMediaContent readItem()
       
    91     {
       
    92         QMediaContent item;
       
    93         if (!nextResource.isNull())
       
    94         item = QMediaContent(nextResource);
       
    95 
       
    96         nextResource = QMediaContent();
       
    97 
       
    98         while (m_textStream && !m_textStream->atEnd()) {
       
    99             QString line = m_textStream->readLine();
       
   100             if (line.isEmpty() || line[0] == '#')
       
   101                 continue;
       
   102 
       
   103             nextResource = QMediaContent(QUrl(line));
       
   104             break;
       
   105         }
       
   106 
       
   107         return item;
       
   108     }
       
   109 
       
   110     virtual void close()
       
   111     {
       
   112     }
       
   113 
       
   114 private:
       
   115     bool m_ownDevice;
       
   116     QIODevice *m_device;
       
   117     QTextStream *m_textStream;
       
   118     QMediaContent nextResource;
       
   119 };
       
   120 
       
   121 class QM3uPlaylistWriter : public QMediaPlaylistWriter
       
   122 {
       
   123 public:
       
   124     QM3uPlaylistWriter(QIODevice *device)
       
   125         :m_device(device), m_textStream(new QTextStream(m_device))
       
   126     {
       
   127     }
       
   128 
       
   129     virtual ~QM3uPlaylistWriter()
       
   130     {
       
   131         delete m_textStream;
       
   132     }
       
   133 
       
   134     virtual bool writeItem(const QMediaContent& item)
       
   135     {
       
   136         *m_textStream << item.canonicalUrl().toString() << endl;
       
   137         return true;
       
   138     }
       
   139 
       
   140     virtual void close()
       
   141     {
       
   142     }
       
   143 
       
   144 private:
       
   145     QIODevice *m_device;
       
   146     QTextStream *m_textStream;
       
   147 };
       
   148 
       
   149 
       
   150 QM3uPlaylistPlugin::QM3uPlaylistPlugin(QObject *parent)
       
   151     :QMediaPlaylistIOPlugin(parent)
       
   152 {
       
   153 }
       
   154 
       
   155 QM3uPlaylistPlugin::~QM3uPlaylistPlugin()
       
   156 {
       
   157 }
       
   158 
       
   159 bool QM3uPlaylistPlugin::canRead(QIODevice *device, const QByteArray &format) const
       
   160 {
       
   161     return device->isReadable() && (format == "m3u" || format.isEmpty());
       
   162 }
       
   163 
       
   164 bool QM3uPlaylistPlugin::canRead(const QUrl& location, const QByteArray &format) const
       
   165 {
       
   166     if (!QFileInfo(location.toLocalFile()).isReadable())
       
   167         return false;
       
   168 
       
   169     if (format == "m3u")
       
   170         return true;
       
   171 
       
   172     if (!format.isEmpty())
       
   173         return false;
       
   174     else
       
   175         return location.toLocalFile().toLower().endsWith(QLatin1String("m3u"));
       
   176 }
       
   177 
       
   178 bool QM3uPlaylistPlugin::canWrite(QIODevice *device, const QByteArray &format) const
       
   179 {
       
   180     return device->isOpen() && device->isWritable() && format == "m3u";
       
   181 }
       
   182 
       
   183 QStringList QM3uPlaylistPlugin::keys() const
       
   184 {
       
   185     return QStringList() << QLatin1String("m3u");
       
   186 }
       
   187 
       
   188 QMediaPlaylistReader *QM3uPlaylistPlugin::createReader(QIODevice *device, const QByteArray &format)
       
   189 {
       
   190     Q_UNUSED(format);
       
   191     return new QM3uPlaylistReader(device);
       
   192 }
       
   193 
       
   194 QMediaPlaylistReader *QM3uPlaylistPlugin::createReader(const QUrl& location, const QByteArray &format)
       
   195 {
       
   196     Q_UNUSED(format);
       
   197     return new QM3uPlaylistReader(location);
       
   198 }
       
   199 
       
   200 QMediaPlaylistWriter *QM3uPlaylistPlugin::createWriter(QIODevice *device, const QByteArray &format)
       
   201 {
       
   202     Q_UNUSED(format);
       
   203     return new QM3uPlaylistWriter(device);
       
   204 }
       
   205