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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
use std::{
    cell::{Cell, Ref, RefCell},
    rc::{Rc, Weak},
};

use zbus::{fdo, Result};

use crate::{
    LocalPlayerInterface, LocalRootInterface, LocalServer, LocalServerRunTask, LoopStatus,
    Metadata, PlaybackRate, PlaybackStatus, Property, Signal, Time, TrackId, Volume,
};

/// Ready-to-use mutable *service*-side object that internally implements
/// [`LocalRootInterface`] and [`LocalPlayerInterface`].
///
/// This has its own internal state, automatically emits properties changed
/// signals, and allows you to connect to method and property setter calls.
///
/// If you need more control to the implementation, you can use [`LocalServer`]
/// or [`Server`] directly instead.
///
/// [`Server`]: crate::Server
#[derive(Debug)]
pub struct Player {
    server: Rc<LocalServer<State>>,
}

#[allow(clippy::type_complexity)]
struct State {
    server: RefCell<Option<Weak<LocalServer<State>>>>,

    raise_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    quit_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    set_fullscreen_cbs: RefCell<Vec<Box<dyn Fn(&Player, bool)>>>, // Property
    can_quit: Cell<bool>,
    fullscreen: Cell<bool>,
    can_set_fullscreen: Cell<bool>,
    can_raise: Cell<bool>,
    has_track_list: Cell<bool>,
    identity: RefCell<String>,
    desktop_entry: RefCell<String>,
    supported_uri_schemes: RefCell<Vec<String>>,
    supported_mime_types: RefCell<Vec<String>>,

    next_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    previous_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    pause_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    play_pause_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    stop_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    play_cbs: RefCell<Vec<Box<dyn Fn(&Player)>>>,
    seek_cbs: RefCell<Vec<Box<dyn Fn(&Player, Time)>>>,
    set_position_cbs: RefCell<Vec<Box<dyn Fn(&Player, &TrackId, Time)>>>,
    open_uri_cbs: RefCell<Vec<Box<dyn Fn(&Player, &str)>>>,
    set_loop_status_cbs: RefCell<Vec<Box<dyn Fn(&Player, LoopStatus)>>>, // Property
    set_rate_cbs: RefCell<Vec<Box<dyn Fn(&Player, PlaybackRate)>>>,      // Property
    set_shuffle_cbs: RefCell<Vec<Box<dyn Fn(&Player, bool)>>>,           // Property
    set_volume_cbs: RefCell<Vec<Box<dyn Fn(&Player, Volume)>>>,          // Property
    playback_status: Cell<PlaybackStatus>,
    loop_status: Cell<LoopStatus>,
    rate: Cell<PlaybackRate>,
    shuffle: Cell<bool>,
    metadata: RefCell<Metadata>,
    volume: Cell<Volume>,
    position: Cell<Time>,
    minimum_rate: Cell<PlaybackRate>,
    maximum_rate: Cell<PlaybackRate>,
    can_go_next: Cell<bool>,
    can_go_previous: Cell<bool>,
    can_play: Cell<bool>,
    can_pause: Cell<bool>,
    can_seek: Cell<bool>,
    can_control: Cell<bool>,
}

impl State {
    fn player(&self) -> Player {
        Player {
            server: self
                .server
                .borrow()
                .as_ref()
                .expect("server must be set up")
                .upgrade()
                .expect("server must not be dropped"),
        }
    }
}

impl LocalRootInterface for State {
    async fn raise(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.raise_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn quit(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.quit_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn can_quit(&self) -> fdo::Result<bool> {
        Ok(self.can_quit.get())
    }

    async fn fullscreen(&self) -> fdo::Result<bool> {
        Ok(self.fullscreen.get())
    }

    async fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
        let player = self.player();
        for cb in self.set_fullscreen_cbs.borrow().iter() {
            cb(&player, fullscreen);
        }
        Ok(())
    }

    async fn can_set_fullscreen(&self) -> fdo::Result<bool> {
        Ok(self.can_set_fullscreen.get())
    }

    async fn can_raise(&self) -> fdo::Result<bool> {
        Ok(self.can_raise.get())
    }

    async fn has_track_list(&self) -> fdo::Result<bool> {
        Ok(self.has_track_list.get())
    }

    async fn identity(&self) -> fdo::Result<String> {
        Ok(self.identity.borrow().clone())
    }

    async fn desktop_entry(&self) -> fdo::Result<String> {
        Ok(self.desktop_entry.borrow().clone())
    }

    async fn supported_uri_schemes(&self) -> fdo::Result<Vec<String>> {
        Ok(self.supported_uri_schemes.borrow().clone())
    }

    async fn supported_mime_types(&self) -> fdo::Result<Vec<String>> {
        Ok(self.supported_mime_types.borrow().clone())
    }
}

impl LocalPlayerInterface for State {
    async fn next(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.next_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn previous(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.previous_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn pause(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.pause_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn play_pause(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.play_pause_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn stop(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.stop_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn play(&self) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.play_cbs.borrow().iter() {
            cb(&player);
        }
        Ok(())
    }

    async fn seek(&self, offset: Time) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.seek_cbs.borrow().iter() {
            cb(&player, offset);
        }
        Ok(())
    }

    async fn set_position(&self, track_id: TrackId, position: Time) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.set_position_cbs.borrow().iter() {
            cb(&player, &track_id, position);
        }
        Ok(())
    }

    async fn open_uri(&self, uri: String) -> fdo::Result<()> {
        let player = self.player();
        for cb in self.open_uri_cbs.borrow().iter() {
            cb(&player, &uri);
        }
        Ok(())
    }

    async fn playback_status(&self) -> fdo::Result<PlaybackStatus> {
        Ok(self.playback_status.get())
    }

    async fn loop_status(&self) -> fdo::Result<LoopStatus> {
        Ok(self.loop_status.get())
    }

    async fn set_loop_status(&self, loop_status: LoopStatus) -> Result<()> {
        let player = self.player();
        for cb in self.set_loop_status_cbs.borrow().iter() {
            cb(&player, loop_status);
        }
        Ok(())
    }

    async fn rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(self.rate.get())
    }

    async fn set_rate(&self, rate: PlaybackRate) -> Result<()> {
        let player = self.player();
        for cb in self.set_rate_cbs.borrow().iter() {
            cb(&player, rate);
        }
        Ok(())
    }

    async fn shuffle(&self) -> fdo::Result<bool> {
        Ok(self.shuffle.get())
    }

    async fn set_shuffle(&self, shuffle: bool) -> Result<()> {
        let player = self.player();
        for cb in self.set_shuffle_cbs.borrow().iter() {
            cb(&player, shuffle);
        }
        Ok(())
    }

    async fn metadata(&self) -> fdo::Result<Metadata> {
        Ok(self.metadata.borrow().clone())
    }

    async fn volume(&self) -> fdo::Result<Volume> {
        Ok(self.volume.get())
    }

    async fn set_volume(&self, volume: Volume) -> Result<()> {
        let player = self.player();
        for cb in self.set_volume_cbs.borrow().iter() {
            cb(&player, volume);
        }
        Ok(())
    }

    async fn position(&self) -> fdo::Result<Time> {
        Ok(self.position.get())
    }

    async fn minimum_rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(self.minimum_rate.get())
    }

    async fn maximum_rate(&self) -> fdo::Result<PlaybackRate> {
        Ok(self.maximum_rate.get())
    }

    async fn can_go_next(&self) -> fdo::Result<bool> {
        Ok(self.can_go_next.get())
    }

    async fn can_go_previous(&self) -> fdo::Result<bool> {
        Ok(self.can_go_previous.get())
    }

    async fn can_play(&self) -> fdo::Result<bool> {
        Ok(self.can_play.get())
    }

    async fn can_pause(&self) -> fdo::Result<bool> {
        Ok(self.can_pause.get())
    }

    async fn can_seek(&self) -> fdo::Result<bool> {
        Ok(self.can_seek.get())
    }

    async fn can_control(&self) -> fdo::Result<bool> {
        Ok(self.can_control.get())
    }
}

impl Player {
    pub fn builder(bus_name_suffix: &str) -> PlayerBuilder {
        PlayerBuilder {
            bus_name_suffix: bus_name_suffix.to_string(),
            can_quit: false,
            fullscreen: false,
            can_set_fullscreen: false,
            can_raise: false,
            has_track_list: false,
            identity: String::new(),
            desktop_entry: String::new(),
            supported_uri_schemes: Vec::new(),
            supported_mime_types: Vec::new(),
            playback_status: PlaybackStatus::Stopped,
            loop_status: LoopStatus::None,
            rate: 1.0,
            shuffle: false,
            metadata: Metadata::new(),
            volume: 1.0,
            position: Time::ZERO,
            minimum_rate: 1.0,
            maximum_rate: 1.0,
            can_go_next: false,
            can_go_previous: false,
            can_play: false,
            can_pause: false,
            can_seek: false,
            can_control: true,
        }
    }

    /// Returns a task that runs the player's server until the player and the
    /// task is dropped.
    ///
    /// The task must be awaited as soon as possible after creating the player.
    ///
    /// The returned task is no-op if the player's server has been ran before.
    pub fn run(&self) -> LocalServerRunTask {
        self.server.run()
    }

    pub fn connect_raise(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().raise_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_quit(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().quit_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_set_fullscreen(&self, cb: impl Fn(&Self, bool) + 'static) {
        self.server
            .imp()
            .set_fullscreen_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn can_quit(&self) -> bool {
        self.server.imp().can_quit.get()
    }

    pub async fn set_can_quit(&self, can_quit: bool) -> Result<()> {
        if self.can_quit() == can_quit {
            return Ok(());
        }

        self.server.imp().can_quit.set(can_quit);
        self.server
            .properties_changed([Property::CanQuit(can_quit)])
            .await
    }

    pub fn fullscreen(&self) -> bool {
        self.server.imp().fullscreen.get()
    }

    pub async fn set_fullscreen(&self, fullscreen: bool) -> Result<()> {
        if self.fullscreen() == fullscreen {
            return Ok(());
        }

        self.server.imp().fullscreen.set(fullscreen);
        self.server
            .properties_changed([Property::Fullscreen(fullscreen)])
            .await
    }

    pub fn can_set_fullscreen(&self) -> bool {
        self.server.imp().can_set_fullscreen.get()
    }

    pub async fn set_can_set_fullscreen(&self, can_set_fullscreen: bool) -> Result<()> {
        if self.can_set_fullscreen() == can_set_fullscreen {
            return Ok(());
        }

        self.server.imp().can_set_fullscreen.set(can_set_fullscreen);
        self.server
            .properties_changed([Property::CanSetFullscreen(can_set_fullscreen)])
            .await
    }

    pub fn can_raise(&self) -> bool {
        self.server.imp().can_raise.get()
    }

    pub async fn set_can_raise(&self, can_raise: bool) -> Result<()> {
        if self.can_raise() == can_raise {
            return Ok(());
        }

        self.server.imp().can_raise.set(can_raise);
        self.server
            .properties_changed([Property::CanRaise(can_raise)])
            .await
    }

    pub fn has_track_list(&self) -> bool {
        self.server.imp().has_track_list.get()
    }

    pub async fn set_has_track_list(&self, has_track_list: bool) -> Result<()> {
        if self.has_track_list() == has_track_list {
            return Ok(());
        }

        self.server.imp().has_track_list.set(has_track_list);
        self.server
            .properties_changed([Property::HasTrackList(has_track_list)])
            .await
    }

    pub fn identity(&self) -> Ref<'_, String> {
        self.server.imp().identity.borrow()
    }

    pub async fn set_identity(&self, identity: impl Into<String>) -> Result<()> {
        let identity = identity.into();

        if *self.identity() == identity {
            return Ok(());
        }

        self.server.imp().identity.replace(identity.clone());
        self.server
            .properties_changed([Property::Identity(identity)])
            .await
    }

    pub fn desktop_entry(&self) -> Ref<'_, String> {
        self.server.imp().desktop_entry.borrow()
    }

    pub async fn set_desktop_entry(&self, desktop_entry: impl Into<String>) -> Result<()> {
        let desktop_entry = desktop_entry.into();

        if *self.desktop_entry() == desktop_entry {
            return Ok(());
        }

        self.server
            .imp()
            .desktop_entry
            .replace(desktop_entry.clone());
        self.server
            .properties_changed([Property::DesktopEntry(desktop_entry)])
            .await
    }

    pub fn supported_uri_schemes(&self) -> Ref<'_, Vec<String>> {
        self.server.imp().supported_uri_schemes.borrow()
    }

    pub async fn set_supported_uri_schemes(
        &self,
        supported_uri_schemes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<()> {
        let supported_uri_schemes = supported_uri_schemes
            .into_iter()
            .map(|i| i.into())
            .collect::<Vec<_>>();

        if *self.supported_uri_schemes() == supported_uri_schemes {
            return Ok(());
        }

        self.server
            .imp()
            .supported_uri_schemes
            .replace(supported_uri_schemes.clone());
        self.server
            .properties_changed([Property::SupportedUriSchemes(supported_uri_schemes)])
            .await
    }

    pub fn supported_mime_types(&self) -> Ref<'_, Vec<String>> {
        self.server.imp().supported_mime_types.borrow()
    }

    pub async fn set_supported_mime_types(
        &self,
        supported_mime_types: impl IntoIterator<Item = impl Into<String>>,
    ) -> Result<()> {
        let supported_mime_types = supported_mime_types
            .into_iter()
            .map(|i| i.into())
            .collect::<Vec<_>>();

        if *self.supported_mime_types() == supported_mime_types {
            return Ok(());
        }

        self.server
            .imp()
            .supported_mime_types
            .replace(supported_mime_types.clone());
        self.server
            .properties_changed([Property::SupportedMimeTypes(supported_mime_types)])
            .await
    }

    pub fn connect_next(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().next_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_previous(&self, cb: impl Fn(&Self) + 'static) {
        self.server
            .imp()
            .previous_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_pause(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().pause_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_play_pause(&self, cb: impl Fn(&Self) + 'static) {
        self.server
            .imp()
            .play_pause_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_stop(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().stop_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_play(&self, cb: impl Fn(&Self) + 'static) {
        self.server.imp().play_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_seek(&self, cb: impl Fn(&Self, Time) + 'static) {
        self.server.imp().seek_cbs.borrow_mut().push(Box::new(cb));
    }

    pub fn connect_set_position(&self, cb: impl Fn(&Self, &TrackId, Time) + 'static) {
        self.server
            .imp()
            .set_position_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_open_uri(&self, cb: impl Fn(&Self, &str) + 'static) {
        self.server
            .imp()
            .open_uri_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_set_loop_status(&self, cb: impl Fn(&Self, LoopStatus) + 'static) {
        self.server
            .imp()
            .set_loop_status_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_set_rate(&self, cb: impl Fn(&Self, PlaybackRate) + 'static) {
        self.server
            .imp()
            .set_rate_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_set_shuffle(&self, cb: impl Fn(&Self, bool) + 'static) {
        self.server
            .imp()
            .set_shuffle_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub fn connect_set_volume(&self, cb: impl Fn(&Self, Volume) + 'static) {
        self.server
            .imp()
            .set_volume_cbs
            .borrow_mut()
            .push(Box::new(cb));
    }

    pub async fn seeked(&self, position: Time) -> Result<()> {
        self.server.emit(Signal::Seeked { position }).await
    }

    pub fn playback_status(&self) -> PlaybackStatus {
        self.server.imp().playback_status.get()
    }

    pub async fn set_playback_status(&self, playback_status: PlaybackStatus) -> Result<()> {
        if self.playback_status() == playback_status {
            return Ok(());
        }

        self.server.imp().playback_status.set(playback_status);
        self.server
            .properties_changed([Property::PlaybackStatus(playback_status)])
            .await
    }

    pub fn loop_status(&self) -> LoopStatus {
        self.server.imp().loop_status.get()
    }

    pub async fn set_loop_status(&self, loop_status: LoopStatus) -> Result<()> {
        if self.loop_status() == loop_status {
            return Ok(());
        }

        self.server.imp().loop_status.set(loop_status);
        self.server
            .properties_changed([Property::LoopStatus(loop_status)])
            .await
    }

    pub fn rate(&self) -> PlaybackRate {
        self.server.imp().rate.get()
    }

    pub async fn set_rate(&self, rate: PlaybackRate) -> Result<()> {
        if self.rate() == rate {
            return Ok(());
        }

        self.server.imp().rate.set(rate);
        self.server.properties_changed([Property::Rate(rate)]).await
    }

    pub fn shuffle(&self) -> bool {
        self.server.imp().shuffle.get()
    }

    pub async fn set_shuffle(&self, shuffle: bool) -> Result<()> {
        if self.shuffle() == shuffle {
            return Ok(());
        }

        self.server.imp().shuffle.set(shuffle);
        self.server
            .properties_changed([Property::Shuffle(shuffle)])
            .await
    }

    pub fn metadata(&self) -> Ref<'_, Metadata> {
        self.server.imp().metadata.borrow()
    }

    pub async fn set_metadata(&self, metadata: Metadata) -> Result<()> {
        if *self.metadata() == metadata {
            return Ok(());
        }

        self.server.imp().metadata.replace(metadata.clone());
        self.server
            .properties_changed([Property::Metadata(metadata)])
            .await
    }

    pub fn volume(&self) -> Volume {
        self.server.imp().volume.get()
    }

    pub async fn set_volume(&self, volume: Volume) -> Result<()> {
        if self.volume() == volume {
            return Ok(());
        }

        self.server.imp().volume.set(volume);
        self.server
            .properties_changed([Property::Volume(volume)])
            .await
    }

    pub fn position(&self) -> Time {
        self.server.imp().position.get()
    }

    /// This does not emit `PropertiesChanged` signal.
    pub fn set_position(&self, position: Time) {
        self.server.imp().position.set(position);
    }

    pub fn minimum_rate(&self) -> PlaybackRate {
        self.server.imp().minimum_rate.get()
    }

    pub async fn set_minimum_rate(&self, minimum_rate: PlaybackRate) -> Result<()> {
        if self.minimum_rate() == minimum_rate {
            return Ok(());
        }

        self.server.imp().minimum_rate.set(minimum_rate);
        self.server
            .properties_changed([Property::MinimumRate(minimum_rate)])
            .await
    }

    pub fn maximum_rate(&self) -> PlaybackRate {
        self.server.imp().maximum_rate.get()
    }

    pub async fn set_maximum_rate(&self, maximum_rate: PlaybackRate) -> Result<()> {
        if self.maximum_rate() == maximum_rate {
            return Ok(());
        }

        self.server.imp().maximum_rate.set(maximum_rate);
        self.server
            .properties_changed([Property::MaximumRate(maximum_rate)])
            .await
    }

    pub fn can_go_next(&self) -> bool {
        self.server.imp().can_go_next.get()
    }

    pub async fn set_can_go_next(&self, can_go_next: bool) -> Result<()> {
        if self.can_go_next() == can_go_next {
            return Ok(());
        }

        self.server.imp().can_go_next.set(can_go_next);
        self.server
            .properties_changed([Property::CanGoNext(can_go_next)])
            .await
    }

    pub fn can_go_previous(&self) -> bool {
        self.server.imp().can_go_previous.get()
    }

    pub async fn set_can_go_previous(&self, can_go_previous: bool) -> Result<()> {
        if self.can_go_previous() == can_go_previous {
            return Ok(());
        }

        self.server.imp().can_go_previous.set(can_go_previous);
        self.server
            .properties_changed([Property::CanGoPrevious(can_go_previous)])
            .await
    }

    pub fn can_play(&self) -> bool {
        self.server.imp().can_play.get()
    }

    pub async fn set_can_play(&self, can_play: bool) -> Result<()> {
        if self.can_play() == can_play {
            return Ok(());
        }

        self.server.imp().can_play.set(can_play);
        self.server
            .properties_changed([Property::CanPlay(can_play)])
            .await
    }

    pub fn can_pause(&self) -> bool {
        self.server.imp().can_pause.get()
    }

    pub async fn set_can_pause(&self, can_pause: bool) -> Result<()> {
        if self.can_pause() == can_pause {
            return Ok(());
        }

        self.server.imp().can_pause.set(can_pause);
        self.server
            .properties_changed([Property::CanPause(can_pause)])
            .await
    }

    pub fn can_seek(&self) -> bool {
        self.server.imp().can_seek.get()
    }

    pub async fn set_can_seek(&self, can_seek: bool) -> Result<()> {
        if self.can_seek() == can_seek {
            return Ok(());
        }

        self.server.imp().can_seek.set(can_seek);
        self.server
            .properties_changed([Property::CanSeek(can_seek)])
            .await
    }

    /// This can only be set on construct.
    pub fn can_control(&self) -> bool {
        self.server.imp().can_control.get()
    }
}

/// A builder used to create [`Player`].
#[derive(Debug)]
#[must_use = "must call `build()` to finish building the player"]
pub struct PlayerBuilder {
    bus_name_suffix: String,
    can_quit: bool,
    fullscreen: bool,
    can_set_fullscreen: bool,
    can_raise: bool,
    has_track_list: bool,
    identity: String,
    desktop_entry: String,
    supported_uri_schemes: Vec<String>,
    supported_mime_types: Vec<String>,
    playback_status: PlaybackStatus,
    loop_status: LoopStatus,
    rate: PlaybackRate,
    shuffle: bool,
    metadata: Metadata,
    volume: Volume,
    position: Time,
    minimum_rate: PlaybackRate,
    maximum_rate: PlaybackRate,
    can_go_next: bool,
    can_go_previous: bool,
    can_play: bool,
    can_pause: bool,
    can_seek: bool,
    can_control: bool,
}

impl PlayerBuilder {
    pub fn can_quit(mut self, can_quit: bool) -> Self {
        self.can_quit = can_quit;
        self
    }

    pub fn fullscreen(mut self, fullscreen: bool) -> Self {
        self.fullscreen = fullscreen;
        self
    }

    pub fn can_set_fullscreen(mut self, can_set_fullscreen: bool) -> Self {
        self.can_set_fullscreen = can_set_fullscreen;
        self
    }

    pub fn can_raise(mut self, can_raise: bool) -> Self {
        self.can_raise = can_raise;
        self
    }

    pub fn has_track_list(mut self, has_track_list: bool) -> Self {
        self.has_track_list = has_track_list;
        self
    }

    pub fn identity(mut self, identity: impl Into<String>) -> Self {
        self.identity = identity.into();
        self
    }

    pub fn desktop_entry(mut self, desktop_entry: impl Into<String>) -> Self {
        self.desktop_entry = desktop_entry.into();
        self
    }

    pub fn supported_uri_schemes(
        mut self,
        supported_uri_schemes: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.supported_uri_schemes = supported_uri_schemes
            .into_iter()
            .map(|i| i.into())
            .collect();
        self
    }

    pub fn supported_mime_types(
        mut self,
        supported_mime_types: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        self.supported_mime_types = supported_mime_types.into_iter().map(|i| i.into()).collect();
        self
    }

    pub fn playback_status(mut self, playback_status: PlaybackStatus) -> Self {
        self.playback_status = playback_status;
        self
    }

    pub fn loop_status(mut self, loop_status: LoopStatus) -> Self {
        self.loop_status = loop_status;
        self
    }

    pub fn rate(mut self, rate: PlaybackRate) -> Self {
        self.rate = rate;
        self
    }

    pub fn shuffle(mut self, shuffle: bool) -> Self {
        self.shuffle = shuffle;
        self
    }

    pub fn metadata(mut self, metadata: Metadata) -> Self {
        self.metadata = metadata;
        self
    }

    pub fn volume(mut self, volume: Volume) -> Self {
        self.volume = volume;
        self
    }

    pub fn position(mut self, position: Time) -> Self {
        self.position = position;
        self
    }

    pub fn minimum_rate(mut self, minimum_rate: PlaybackRate) -> Self {
        self.minimum_rate = minimum_rate;
        self
    }

    pub fn maximum_rate(mut self, maximum_rate: PlaybackRate) -> Self {
        self.maximum_rate = maximum_rate;
        self
    }

    pub fn can_go_next(mut self, can_go_next: bool) -> Self {
        self.can_go_next = can_go_next;
        self
    }

    pub fn can_go_previous(mut self, can_go_previous: bool) -> Self {
        self.can_go_previous = can_go_previous;
        self
    }

    pub fn can_play(mut self, can_play: bool) -> Self {
        self.can_play = can_play;
        self
    }

    pub fn can_pause(mut self, can_pause: bool) -> Self {
        self.can_pause = can_pause;
        self
    }

    pub fn can_seek(mut self, can_seek: bool) -> Self {
        self.can_seek = can_seek;
        self
    }

    pub fn can_control(mut self, can_control: bool) -> Self {
        self.can_control = can_control;
        self
    }

    #[must_use = "building player is usually expensive and is not expected to have side effects"]
    pub async fn build(self) -> Result<Player> {
        let server = Rc::new(
            LocalServer::new(
                &self.bus_name_suffix,
                State {
                    server: RefCell::new(None),
                    raise_cbs: RefCell::new(Vec::new()),
                    quit_cbs: RefCell::new(Vec::new()),
                    set_fullscreen_cbs: RefCell::new(Vec::new()),
                    can_quit: Cell::new(self.can_quit),
                    fullscreen: Cell::new(self.fullscreen),
                    can_set_fullscreen: Cell::new(self.can_set_fullscreen),
                    can_raise: Cell::new(self.can_raise),
                    has_track_list: Cell::new(self.has_track_list),
                    identity: RefCell::new(self.identity),
                    desktop_entry: RefCell::new(self.desktop_entry),
                    supported_uri_schemes: RefCell::new(self.supported_uri_schemes),
                    supported_mime_types: RefCell::new(self.supported_mime_types),
                    next_cbs: RefCell::new(Vec::new()),
                    previous_cbs: RefCell::new(Vec::new()),
                    pause_cbs: RefCell::new(Vec::new()),
                    play_pause_cbs: RefCell::new(Vec::new()),
                    stop_cbs: RefCell::new(Vec::new()),
                    play_cbs: RefCell::new(Vec::new()),
                    seek_cbs: RefCell::new(Vec::new()),
                    set_position_cbs: RefCell::new(Vec::new()),
                    open_uri_cbs: RefCell::new(Vec::new()),
                    set_loop_status_cbs: RefCell::new(Vec::new()),
                    set_rate_cbs: RefCell::new(Vec::new()),
                    set_shuffle_cbs: RefCell::new(Vec::new()),
                    set_volume_cbs: RefCell::new(Vec::new()),
                    playback_status: Cell::new(self.playback_status),
                    loop_status: Cell::new(self.loop_status),
                    rate: Cell::new(self.rate),
                    shuffle: Cell::new(self.shuffle),
                    metadata: RefCell::new(self.metadata),
                    volume: Cell::new(self.volume),
                    position: Cell::new(self.position),
                    minimum_rate: Cell::new(self.minimum_rate),
                    maximum_rate: Cell::new(self.maximum_rate),
                    can_go_next: Cell::new(self.can_go_next),
                    can_go_previous: Cell::new(self.can_go_previous),
                    can_play: Cell::new(self.can_play),
                    can_pause: Cell::new(self.can_pause),
                    can_seek: Cell::new(self.can_seek),
                    can_control: Cell::new(self.can_control),
                },
            )
            .await?,
        );

        server.imp().server.replace(Some(Rc::downgrade(&server)));

        Ok(Player { server })
    }
}