Debian Codegraph @

podcasts-gtk/src/widgets/episode_menu.rs

raw symbols SCIP
1 // episode_menu.rs
2 //
3 // Copyright 2021 nee <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use async_channel::Sender;
21 use glib::clone;
22 use gtk::prelude::*;
23 use gtk::{gio, glib};
24
25 use crate::app::Action;
26 use podcasts_data::ShowId;
27 use podcasts_data::{EpisodeId, EpisodeModel};
28
29 #[derive(Debug, Clone)]
30 pub(crate) struct EpisodeMenu {
31 pub(crate) menu: gio::MenuModel,
32 go_to_show: gio::SimpleAction,
33 copy_episode_url: gio::SimpleAction,
34 mark_as_played: gio::SimpleAction,
35 mark_as_unplayed: gio::SimpleAction,
36 pub(crate) group: gio::SimpleActionGroup,
37 }
38
39 impl Default for EpisodeMenu {
40 fn default() -> Self {
41 let builder = gtk::Builder::from_resource("/org/gnome/Podcasts/gtk/episode_menu.ui");
42 let menu = builder.object("episode_menu").unwrap();
43 let go_to_show = gio::SimpleAction::new("go-to-show", None);
44 let copy_episode_url = gio::SimpleAction::new("copy-episode-url", None);
45 let mark_as_played = gio::SimpleAction::new("mark-as-played", None);
46 let mark_as_unplayed = gio::SimpleAction::new("mark-as-unplayed", None);
47 let group = gio::SimpleActionGroup::new();
48
49 EpisodeMenu {
50 menu,
51 go_to_show,
52 copy_episode_url,
53 mark_as_played,
54 mark_as_unplayed,
55 group,
56 }
57 }
58 }
59
60 impl EpisodeMenu {
61 pub fn new(sender: &Sender<Action>, ep: &dyn EpisodeModel, show: Option<ShowId>) -> Self {
62 let s = Self::default();
63 s.init(sender, ep, show);
64 s
65 }
66
67 fn init(&self, sender: &Sender<Action>, ep: &dyn EpisodeModel, show: Option<ShowId>) {
68 if let Some(show_id) = show {
69 self.connect_go_to_show(show_id);
70 }
71 self.connect_mark_as_played(sender, ep.id());
72 self.update_played_state(ep);
73 self.connect_copy_episode_url(sender, ep);
74 }
75
76 fn update_played_state(&self, ep: &dyn EpisodeModel) {
77 let played = ep.played();
78 self.mark_as_played.set_enabled(played.is_none());
79 self.mark_as_unplayed.set_enabled(played.is_some());
80 }
81
82 fn connect_go_to_show(&self, id: ShowId) {
83 self.go_to_show.connect_activate(move |_, _| {
84 if let Some(app) = gio::Application::default() {
85 app.activate_action("go-to-show", Some(&id.0.into()));
86 }
87 });
88 self.group.add_action(&self.go_to_show);
89 }
90
91 fn connect_copy_episode_url(&self, sender: &Sender<Action>, ep: &dyn EpisodeModel) {
92 let ep_id = ep.id();
93 if ep.uri().is_some() {
94 self.copy_episode_url.connect_activate(clone!(
95 #[strong]
96 sender,
97 move |_, _| {
98 send_blocking!(sender, Action::CopyUrl(ep_id));
99 }
100 ));
101 self.group.add_action(&self.copy_episode_url);
102 }
103 }
104
105 fn connect_mark_as_played(&self, sender: &Sender<Action>, ep_id: EpisodeId) {
106 self.mark_as_played.connect_activate(clone!(
107 #[strong]
108 sender,
109 move |_, _| {
110 send_blocking!(sender, Action::MarkAsPlayed(true, ep_id));
111 }
112 ));
113 self.group.add_action(&self.mark_as_played);
114 self.mark_as_unplayed.connect_activate(clone!(
115 #[strong]
116 sender,
117 move |_, _| {
118 send_blocking!(sender, Action::MarkAsPlayed(false, ep_id));
119 }
120 ));
121 self.group.add_action(&self.mark_as_unplayed);
122 }
123 }