File indexing completed on 2024-05-05 04:50:49

0001 /*
0002  * SPDX-FileCopyrightText: 2020 George Florea Bănuș <georgefb899@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #ifndef PLAYLISTMODEL_H
0008 #define PLAYLISTMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QSortFilterProxyModel>
0012 #include <QUrl>
0013 #include <QtQml/qqmlregistration.h>
0014 
0015 struct PlaylistItem {
0016     QUrl url;
0017     QString filename;
0018     QString mediaTitle;
0019     QString folderPath;
0020     QString duration;
0021 };
0022 
0023 class PlaylistProxyModel : public QSortFilterProxyModel
0024 {
0025     Q_OBJECT
0026     QML_NAMED_ELEMENT(PlaylistProxyModel)
0027 
0028 public:
0029     explicit PlaylistProxyModel(QObject *parent = nullptr);
0030 
0031     enum class Sort {
0032         NameAscending,
0033         NameDescending,
0034         DurationAscending,
0035         DurationDescending,
0036     };
0037     Q_ENUM(Sort)
0038 
0039     Q_INVOKABLE void sortItems(Sort sortMode);
0040     Q_INVOKABLE int getPlayingItem();
0041     Q_INVOKABLE void setPlayingItem(int i);
0042     Q_INVOKABLE void playNext();
0043     Q_INVOKABLE void playPrevious();
0044     Q_INVOKABLE void saveM3uFile(const QString &path);
0045     Q_INVOKABLE void highlightInFileManager(int row);
0046     Q_INVOKABLE void removeItem(int row);
0047     Q_INVOKABLE void renameFile(int row);
0048     Q_INVOKABLE void trashFile(int row);
0049     Q_INVOKABLE void copyFileName(int row);
0050     Q_INVOKABLE void copyFilePath(int row);
0051     Q_INVOKABLE QString getFilePath(int row);
0052 };
0053 
0054 class PlaylistModel : public QAbstractListModel
0055 {
0056     Q_OBJECT
0057     QML_NAMED_ELEMENT(PlaylistModel)
0058 
0059 public:
0060     explicit PlaylistModel(QObject *parent = nullptr);
0061     friend class PlaylistProxyModel;
0062     friend class MpvItem;
0063 
0064     enum Roles {
0065         NameRole = Qt::UserRole,
0066         TitleRole,
0067         DurationRole,
0068         PathRole,
0069         FolderPathRole,
0070         PlayingRole,
0071         IsLocalRole,
0072     };
0073     Q_ENUM(Roles)
0074 
0075     enum Behaviour {
0076         Append,
0077         Clear,
0078     };
0079     Q_ENUM(Behaviour)
0080 
0081     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0082     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0083     virtual QHash<int, QByteArray> roleNames() const override;
0084 
0085     Q_INVOKABLE void clear();
0086     Q_INVOKABLE void addItem(const QString &path, PlaylistModel::Behaviour behaviour);
0087     Q_INVOKABLE void addItem(const QUrl &url, PlaylistModel::Behaviour behaviour);
0088 
0089 Q_SIGNALS:
0090     void itemAdded(int index, const QString &path);
0091     void playingItemChanged();
0092 
0093 private:
0094     void appendItem(const QUrl &url);
0095     void getSiblingItems(const QUrl &url);
0096     void addM3uItems(const QUrl &url);
0097     void getYouTubePlaylist(const QUrl &url, PlaylistModel::Behaviour behaviour);
0098     void getHttpItemInfo(const QUrl &url, int row);
0099     bool isVideoOrAudioMimeType(const QString &mimeType);
0100     void setPlayingItem(int i);
0101 
0102     QList<PlaylistItem> m_playlist;
0103     int m_playingItem{-1};
0104     QString m_playlistPath;
0105     int m_httpItemCounter{0};
0106 };
0107 
0108 Q_DECLARE_METATYPE(PlaylistModel::Behaviour)
0109 #endif // PLAYLISTMODEL_H