File: mpris_player_manager.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,412 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (145 lines) | stat: -rw-r--r-- 4,993 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "mpris_player_manager.h"
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>

class MprisPlayerManagerPrivate : public QObject
{
    Q_OBJECT
public:
    explicit MprisPlayerManagerPrivate(QObject* parent = nullptr);
    ~MprisPlayerManagerPrivate(){qDebug() << "MprisPlayerManagerPrivate" << "析构";}
private Q_SLOTS:
    void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
private:
    void addPlayer(const QString& service);
    void removePlayer(const QString& service);
public:
    QHash<QString, MprisPlayerPtr> m_players;
    //service to pid
    QHash<QString, uint> m_services;
};

MprisPlayerManager::MprisPlayerManager(QObject *parent)
    : QObject(parent)
    , d(new MprisPlayerManagerPrivate(this))
{

}

MprisPlayerManager::~MprisPlayerManager()
{
    qDebug() << "MprisPlayerManager" << "析构";
}

MprisPlayerWeakPtr MprisPlayerManager::player(const QString &service) const
{
    return d->m_players.value(service);
}

MprisPlayerWeakPtr MprisPlayerManager::player(const uint &pid) const
{
    return d->m_players.value(d->m_services.key(pid));
}

MprisPlayerWeakPtr MprisPlayerManager::activePlayer()
{
    if (d->m_players.count() == 0) {
        //如果没有player,构建空对象
        return std::make_shared<MprisPlayer>("nullplayer");
    }
    return d->m_players.begin().value();
}

//private
MprisPlayerManagerPrivate::MprisPlayerManagerPrivate(QObject *parent)
    : QObject(parent)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
    auto watcher = new QDBusServiceWatcher(QStringLiteral("org.mpris.MediaPlayer2*"), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this);
    connect(watcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &MprisPlayerManagerPrivate::serviceOwnerChanged);
#else
    connect(QDBusConnection::sessionBus().interface(), &QDBusConnectionInterface::serviceOwnerChanged, this, &MprisPlayerManagerPrivate::serviceOwnerChanged);
#endif
    //获取列表
    QDBusPendingCall async = QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames"));
    QDBusPendingCallWatcher *asyncWatcher = new QDBusPendingCallWatcher(async, this);
    connect(asyncWatcher, &QDBusPendingCallWatcher::finished, [=](QDBusPendingCallWatcher* watcher){
        QDBusPendingReply<QStringList> reply = *watcher;
        watcher->deleteLater();
        if (reply.isError()) {
            //error
            qWarning()  << "Error message was" << reply.error().name() << reply.error().message();

        } else {
            for (const QString& serviceName : reply.value()) {
                if (serviceName.startsWith(QLatin1String("org.mpris.MediaPlayer2."))) {
                    addPlayer(serviceName);
                    qDebug() << serviceName;
                }
            }
        }
    });
}

void MprisPlayerManagerPrivate::serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner)
{
    if (!name.startsWith("org.mpris.MediaPlayer2.")) {
        //过滤不符合mpris的seivice
        return;
    }
    if (!oldOwner.isEmpty()) {
        //dbus 注销 remove player
        removePlayer(name);
    }
    if (!newOwner.isEmpty()) {
        //dbus 注册 add player
        addPlayer(name);
        qDebug() << "add service" << name;
    }
}

void MprisPlayerManagerPrivate::addPlayer(const QString &service)
{
    MprisPlayerPtr player = std::make_shared<MprisPlayer>(service);
    if (player.get()) {
        connect(player.get(), &MprisPlayer::initialFetchFinished, this, [=] () {
            disconnect(player.get(), &MprisPlayer::initialFetchFinished, this, nullptr);
            //add service pid
            m_services.insert(player->objectName(), player->pid());
            //add service player
            m_players.insert(player->objectName(), player);
        });
    }
}

void MprisPlayerManagerPrivate::removePlayer(const QString &service)
{
    m_players.take(service);
    m_services.take(service);
    qDebug() << "remove service" << service;
}

#include "mpris_player_manager.moc"