File indexing completed on 2024-05-19 04:53:22

0001 /*
0002  * playlistmodel.h
0003  *
0004  * Copyright (C) 2009-2011 Christoph Pfister <christophpfister@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License along
0017  * with this program; if not, write to the Free Software Foundation, Inc.,
0018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0019  */
0020 
0021 #ifndef PLAYLISTMODEL_H
0022 #define PLAYLISTMODEL_H
0023 
0024 #include <QAbstractTableModel>
0025 #include <QTime>
0026 #include "../mediawidget.h"
0027 
0028 class PlaylistTrack
0029 {
0030 public:
0031     PlaylistTrack() : trackNumber(-1), currentSubtitle(-1) { }
0032     ~PlaylistTrack() { }
0033 
0034     QUrl url;
0035     QString title;
0036     QString artist;
0037     QString album;
0038     int trackNumber;
0039     QTime length;
0040     QList<QUrl> subtitles;
0041     int currentSubtitle;
0042 };
0043 
0044 class Playlist
0045 {
0046 public:
0047     Playlist() : currentTrack(-1) { }
0048     ~Playlist() { }
0049 
0050     const PlaylistTrack &at(int index) const
0051     {
0052         return tracks.at(index);
0053     }
0054 
0055     enum Format {
0056         Invalid,
0057         Kaffeine, // read-only
0058         M3U,
0059         PLS,
0060         XSPF
0061     };
0062 
0063     bool load(const QUrl &url_, Format format);
0064     bool save(Format format) const;
0065 
0066     QUrl url;
0067     QString title;
0068     QList<PlaylistTrack> tracks;
0069     int currentTrack;
0070 
0071 private:
0072     void appendTrack(PlaylistTrack &track);
0073     QUrl fromFileOrUrl(const QString &fileOrUrl) const;
0074     QUrl fromRelativeUrl(const QString &trackUrlString) const;
0075     QString toFileOrUrl(const QUrl &trackUrl) const;
0076     QString toRelativeUrl(const QUrl &trackUrl) const;
0077 
0078     bool loadKaffeinePlaylist(QIODevice *device);
0079     bool loadM3UPlaylist(QIODevice *device);
0080     void saveM3UPlaylist(QIODevice *device) const;
0081     bool loadPLSPlaylist(QIODevice *device);
0082     void savePLSPlaylist(QIODevice *device) const;
0083     bool loadXSPFPlaylist(QIODevice *device);
0084     void saveXSPFPlaylist(QIODevice *device) const;
0085 };
0086 
0087 class PlaylistModel : public QAbstractTableModel
0088 {
0089     Q_OBJECT
0090 public:
0091     PlaylistModel(Playlist *visiblePlaylist_, QObject *parent);
0092     ~PlaylistModel();
0093 
0094     void setVisiblePlaylist(Playlist *visiblePlaylist_);
0095     Playlist *getVisiblePlaylist() const;
0096 
0097     void appendUrls(Playlist *playlist, const QList<QUrl> &urls, bool playImmediately);
0098     void removeRows(Playlist *playlist, int row, int count);
0099     void setCurrentTrack(Playlist *playlist, int track);
0100     void updateTrackLength(Playlist *playlist, int length);
0101     void updateTrackMetadata(Playlist *playlist,
0102         const QMap<MediaWidget::MetadataType, QString> &metadata);
0103 
0104 public slots:
0105     void clearVisiblePlaylist();
0106 
0107 signals:
0108     void appendPlaylist(Playlist *playlist, bool playImmediately);
0109     void playTrack(Playlist *playlist, int track);
0110 
0111 private:
0112     void insertUrls(Playlist *playlist, int row, const QList<QUrl> &urls,
0113         bool playImmediately);
0114 
0115     int columnCount(const QModelIndex &parent) const override;
0116     int rowCount(const QModelIndex &parent) const override;
0117     QVariant data(const QModelIndex &index, int role) const override;
0118     QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
0119     bool removeRows(int row, int count, const QModelIndex &parent) override;
0120     void sort(int column, Qt::SortOrder order) override;
0121 
0122     Qt::ItemFlags flags(const QModelIndex &index) const override;
0123     QStringList mimeTypes() const override;
0124     Qt::DropActions supportedDropActions() const override;
0125     QMimeData *mimeData(const QModelIndexList &indexes) const override;
0126     bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
0127         const QModelIndex &parent) override;
0128 
0129     Playlist *visiblePlaylist;
0130 };
0131 
0132 #endif /* PLAYLISTMODEL_H */