File indexing completed on 2024-05-19 05:38:37

0001 /*
0002     SPDX-FileCopyrightText: 2007-2012 Alex Merry <alex.merry@kdemail.net>
0003     SPDX-FileCopyrightText: 2023 Fushan Wen <qydwhotmail@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <unordered_map>
0011 #include <vector>
0012 
0013 #include <QAbstractListModel>
0014 #include <QConcatenateTablesProxyModel>
0015 
0016 #include "kmpris_export.h"
0017 
0018 class QDBusPendingCallWatcher;
0019 class Multiplexer;
0020 class PlayerContainer;
0021 
0022 /**
0023  * The MPRIS2 data model where player information is stored and monitored
0024  */
0025 class KMPRIS_NO_EXPORT Mpris2SourceModel : public QAbstractListModel
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     enum Role {
0031         TrackRole = Qt::DisplayRole,
0032         ContainerRole = Qt::UserRole + 1,
0033         CanControlRole,
0034         CanGoNextRole,
0035         CanGoPreviousRole,
0036         CanPlayRole,
0037         CanPauseRole,
0038         CanStopRole,
0039         CanSeekRole,
0040         LoopStatusRole,
0041         PlaybackStatusRole,
0042         PositionRole,
0043         RateRole,
0044         ShuffleRole,
0045         VolumeRole,
0046         ArtUrlRole,
0047         ArtistRole,
0048         AlbumRole,
0049         LengthRole,
0050         CanQuitRole,
0051         CanRaiseRole,
0052         CanSetFullscreenRole,
0053         DesktopEntryRole,
0054         IdentityRole,
0055         IconNameRole,
0056         InstancePidRole,
0057         KDEPidRole,
0058     };
0059     Q_ENUM(Role)
0060 
0061     static std::shared_ptr<Mpris2SourceModel> self();
0062     ~Mpris2SourceModel() override;
0063 
0064     static QVariant dataFromPlayer(PlayerContainer *container, int role);
0065     QVariant data(const QModelIndex &index, int role) const override;
0066     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0067     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0068     QHash<int, QByteArray> roleNames() const override;
0069 
0070 private Q_SLOTS:
0071     void onServiceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner);
0072     void onServiceNamesFetched(QDBusPendingCallWatcher *watcher);
0073     void onInitialFetchFinished(PlayerContainer *container);
0074     void onInitialFetchFailed(PlayerContainer *container);
0075 
0076 private:
0077     Mpris2SourceModel(QObject *parent = nullptr);
0078 
0079     void fetchServiceNames();
0080     void addMediaPlayer(const QString &serviceName, const QString &sourceName);
0081     void removeMediaPlayer(const QString &sourceName);
0082 
0083     std::vector<PlayerContainer *> m_containers;
0084     std::unordered_map<QString, PlayerContainer *> m_pendingContainers;
0085 
0086     friend class Multiplexer;
0087 };