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
146
147
148
149
150
151
use futures_channel::{mpsc, oneshot};
use futures_util::StreamExt;
use zbus::{dbus_interface, ConnectionBuilder, Result, SignalContext};

use super::{
    player::{RawPlayerInterface, RawRootInterface},
    utils::{changed_delegate, signal_delegate},
    Action, Server, OBJECT_PATH,
};
use crate::{MaybePlaylist, Playlist, PlaylistId, PlaylistOrdering, PlaylistsInterface};

pub(super) enum PlaylistsAction {
    // Methods
    ActivatePlaylist(PlaylistId),
    GetPlaylists(
        u32,
        u32,
        PlaylistOrdering,
        bool,
        oneshot::Sender<Vec<Playlist>>,
    ),

    // Properties
    PlaylistCount(oneshot::Sender<u32>),
    Orderings(oneshot::Sender<Vec<PlaylistOrdering>>),
    ActivePlaylist(oneshot::Sender<MaybePlaylist>),
}

pub(super) struct RawPlaylistsInterface {
    pub(super) tx: mpsc::UnboundedSender<Action>,
}

impl RawPlaylistsInterface {
    fn send(&self, action: PlaylistsAction) {
        self.tx.unbounded_send(Action::Playlists(action)).unwrap();
    }
}

#[dbus_interface(name = "org.mpris.MediaPlayer2.Playlists")]
impl RawPlaylistsInterface {
    fn activate_playlist(&self, playlist_id: PlaylistId) {
        self.send(PlaylistsAction::ActivatePlaylist(playlist_id));
    }

    async fn get_playlists(
        &self,
        index: u32,
        max_count: u32,
        order: PlaylistOrdering,
        reverse_order: bool,
    ) -> Vec<Playlist> {
        let (tx, rx) = oneshot::channel();
        self.send(PlaylistsAction::GetPlaylists(
            index,
            max_count,
            order,
            reverse_order,
            tx,
        ));
        rx.await.unwrap()
    }

    #[dbus_interface(signal)]
    async fn playlist_changed(ctxt: &SignalContext<'_>, playlist: Playlist) -> Result<()>;

    #[dbus_interface(property)]
    async fn playlist_count(&self) -> u32 {
        let (tx, rx) = oneshot::channel();
        self.send(PlaylistsAction::PlaylistCount(tx));
        rx.await.unwrap()
    }

    #[dbus_interface(property)]
    async fn orderings(&self) -> Vec<PlaylistOrdering> {
        let (tx, rx) = oneshot::channel();
        self.send(PlaylistsAction::Orderings(tx));
        rx.await.unwrap()
    }

    #[dbus_interface(property)]
    async fn active_playlist(&self) -> MaybePlaylist {
        let (tx, rx) = oneshot::channel();
        self.send(PlaylistsAction::ActivePlaylist(tx));
        rx.await.unwrap()
    }
}

impl<T> Server<T>
where
    T: PlaylistsInterface + 'static,
{
    pub async fn run_with_playlists(&self) -> Result<()> {
        let (tx, mut rx) = mpsc::unbounded::<Action>();

        let connection = ConnectionBuilder::session()?
            .name(&self.bus_name)?
            .serve_at(OBJECT_PATH, RawRootInterface { tx: tx.clone() })?
            .serve_at(OBJECT_PATH, RawPlayerInterface { tx: tx.clone() })?
            .serve_at(OBJECT_PATH, RawPlaylistsInterface { tx })?
            .build()
            .await?;

        self.connection
            .set(connection)
            .expect("server must only be ran once");

        // FIXME Spawn tasks so we can handle calls concurrently
        while let Some(action) = rx.next().await {
            match action {
                Action::Root(action) => self.handle_interface_action(action).await,
                Action::Player(action) => self.handle_player_interface_action(action).await,
                Action::Playlists(action) => self.handle_playlists_interface_action(action).await,
                Action::TrackList(_) => unreachable!(),
            }
        }

        Ok(())
    }

    pub(super) async fn handle_playlists_interface_action(&self, action: PlaylistsAction) {
        match action {
            PlaylistsAction::ActivatePlaylist(playlist_id) => {
                self.imp.activate_playlist(playlist_id).await
            }
            PlaylistsAction::GetPlaylists(index, max_count, order, reverse_order, sender) => {
                sender
                    .send(
                        self.imp
                            .get_playlists(index, max_count, order, reverse_order)
                            .await,
                    )
                    .unwrap();
            }
            PlaylistsAction::PlaylistCount(sender) => {
                sender.send(self.imp.playlist_count().await).unwrap();
            }
            PlaylistsAction::Orderings(sender) => {
                sender.send(self.imp.orderings().await).unwrap();
            }
            PlaylistsAction::ActivePlaylist(sender) => {
                sender.send(self.imp.active_playlist().await).unwrap();
            }
        }
    }

    // org.mpris.MediaPlayer2.Playlists
    signal_delegate!(RawPlaylistsInterface, playlist_changed(playlist: Playlist));
    changed_delegate!(RawPlaylistsInterface, playlist_count_changed);
    changed_delegate!(RawPlaylistsInterface, orderings_changed);
    changed_delegate!(RawPlaylistsInterface, active_playlist_changed);
}