Debian Codegraph @

podcasts-gtk/src/widgets/show_menu.rs

raw symbols SCIP
1 // show_menu.rs
2 //
3 // Copyright 2017 Jordan Petridis <[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 anyhow::Result;
21 use async_channel::Sender;
22 use glib::clone;
23 use gtk::prelude::*;
24 use gtk::{gio, glib};
25 use std::sync::Arc;
26
27 use crate::app::Action;
28 use crate::i18n::{i18n, i18n_f};
29 use crate::utils;
30 use podcasts_data::Show;
31 use podcasts_data::dbqueries;
32 use podcasts_data::utils::delete_show;
33
34 #[derive(Debug, Clone)]
35 pub(crate) struct ShowMenu {
36 pub(crate) menu: gio::MenuModel,
37 website: gio::SimpleAction,
38 played: gio::SimpleAction,
39 unsub: gio::SimpleAction,
40 group: gio::SimpleActionGroup,
41 }
42
43 impl Default for ShowMenu {
44 fn default() -> Self {
45 let builder = gtk::Builder::from_resource("/org/gnome/Podcasts/gtk/show_menu.ui");
46 let menu = builder.object("show_menu").unwrap();
47 let website = gio::SimpleAction::new("open-website", None);
48 let played = gio::SimpleAction::new("mark-played", None);
49 let unsub = gio::SimpleAction::new("unsubscribe", None);
50 let group = gio::SimpleActionGroup::new();
51
52 group.add_action(&website);
53 group.add_action(&played);
54 group.add_action(&unsub);
55
56 ShowMenu {
57 menu,
58 website,
59 played,
60 unsub,
61 group,
62 }
63 }
64 }
65
66 impl ShowMenu {
67 pub(crate) fn new(pd: &Arc<Show>, episodes: &gtk::ListBox, sender: &Sender<Action>) -> Self {
68 let s = Self::default();
69 s.init(pd, episodes, sender);
70 s
71 }
72
73 fn init(&self, pd: &Arc<Show>, episodes: &gtk::ListBox, sender: &Sender<Action>) {
74 self.connect_website(pd);
75 self.connect_played(pd, episodes, sender);
76 self.connect_unsub(pd, sender);
77
78 let app = gio::Application::default()
79 .expect("Could not get default application")
80 .downcast::<gtk::Application>()
81 .unwrap();
82 let win = app.active_window().expect("No active window");
83 win.insert_action_group("show", Some(&self.group));
84 }
85
86 fn connect_website(&self, pd: &Arc<Show>) {
87 // TODO: tooltips for actions?
88 self.website.connect_activate(clone!(
89 #[strong]
90 pd,
91 move |_, _| {
92 let link = pd.link();
93 info!("Opening link: {}", link);
94 let res = open::that(link);
95 debug_assert!(res.is_ok());
96 }
97 ));
98 }
99
100 fn connect_played(&self, pd: &Arc<Show>, episodes: &gtk::ListBox, sender: &Sender<Action>) {
101 self.played.connect_activate(clone!(
102 #[strong]
103 pd,
104 #[strong]
105 sender,
106 #[weak]
107 episodes,
108 move |_, _| {
109 let res = dim_titles(&episodes);
110 debug_assert!(res.is_some());
111 send_blocking!(sender, Action::MarkAllPlayerNotification(pd.clone()));
112 }
113 ));
114 }
115
116 fn connect_unsub(&self, pd: &Arc<Show>, sender: &Sender<Action>) {
117 self.unsub.connect_activate(clone!(
118 #[strong]
119 pd,
120 #[strong]
121 sender,
122 move |unsub, _| {
123 unsub.set_enabled(false);
124 send_blocking!(sender, Action::RemoveShow(pd.clone()));
125 // Queue a refresh after the switch to avoid blocking the db.
126 send_blocking!(sender, Action::RefreshShowsView);
127 send_blocking!(sender, Action::RefreshEpisodesView);
128 unsub.set_enabled(true);
129 }
130 ));
131 }
132 }
133
134 // Ideally if we had a custom widget this would have been as simple as:
135 // `for row in listbox { ep = row.get_episode(); ep.dim_title(); }`
136 // But now I can't think of a better way to do it than hardcoding the title
137 // position relative to the EpisodeWidget container gtk::Box.
138 fn dim_titles(episodes: &gtk::ListBox) -> Option<()> {
139 // FIXME This api should only be used for widget implementations.
140 let listmodel = episodes.observe_children();
141 for i in 0..listmodel.n_items() {
142 let obj = listmodel.item(i)?;
143 let row = obj.downcast_ref::<gtk::ListBoxRow>()?;
144 dim_row_title(row)?;
145 }
146 Some(())
147 }
148
149 fn dim_row_title(row: &gtk::ListBoxRow) -> Option<()> {
150 // FIXME first_child should only be used for widget implementations.
151 let container = row.first_child().and_downcast::<gtk::Box>()?;
152 let container_child = container.first_child().and_downcast::<gtk::Box>()?;
153 let container_gradchild = container_child.first_child().and_downcast::<gtk::Box>()?;
154 let container_great_gradchild = container_gradchild
155 .first_child()
156 .and_downcast::<gtk::Box>()?;
157 let title = container_great_gradchild
158 .first_child()
159 .and_downcast::<gtk::Label>()?;
160
161 title.add_css_class("dim-label");
162
163 // FIXME next_sibling should only be used for widget implementations.
164 let checkmark = title.next_sibling().and_downcast::<gtk::Image>()?;
165 checkmark.set_visible(true);
166 Some(())
167 }
168
169 fn mark_all_watched(pd: &Show, sender: &Sender<Action>) -> Result<()> {
170 // TODO: If this fails for whatever reason, it should be impossible, show an error
171 dbqueries::update_none_to_played_now(pd)?;
172 // Not all widgets might have been loaded when the mark_all is hit
173 // So we will need to refresh again after it's done.
174 send_blocking!(sender, Action::RefreshWidgetIfSame(pd.id()));
175 send_blocking!(sender, Action::RefreshEpisodesView);
176 Ok(())
177 }
178
179 pub(crate) fn mark_all_notif(pd: Arc<Show>, sender: &Sender<Action>) -> adw::Toast {
180 let id = pd.id();
181 let toast = adw::Toast::new(&i18n("Marked all episodes as listened"));
182 toast.set_button_label(Some(&i18n("Undo")));
183 toast.set_action_target_value(Some(&id.0.to_variant()));
184 toast.set_action_name(Some("app.undo-mark-all"));
185
186 toast.connect_dismissed(clone!(
187 #[strong]
188 sender,
189 move |_| {
190 let app = gio::Application::default()
191 .expect("Could not get default application")
192 .downcast::<crate::PdApplication>()
193 .unwrap();
194 if app.is_show_marked_mark(&pd) {
195 let res = mark_all_watched(&pd, &sender);
196 debug_assert!(res.is_ok());
197 }
198 }
199 ));
200
201 toast
202 }
203
204 pub(crate) fn remove_show_notif(pd: Arc<Show>) -> adw::Toast {
205 let text = i18n_f("Unsubscribed from {}", &[pd.title()]);
206 let id = pd.id();
207
208 let toast = adw::Toast::new(&text);
209 toast.set_button_label(Some(&i18n("Undo")));
210 toast.set_action_target_value(Some(&id.0.to_variant()));
211 toast.set_action_name(Some("app.undo-remove-show"));
212
213 let res = utils::ignore_show(id);
214 debug_assert!(res.is_ok());
215
216 toast.connect_dismissed(move |_args| {
217 let res = utils::unignore_show(id);
218 debug_assert!(res.is_ok());
219
220 // Spawn a thread so it won't block the ui.
221 gio::spawn_blocking(clone!(
222 #[strong]
223 pd,
224 move || {
225 let app = gio::Application::default()
226 .expect("Could not get default application")
227 .downcast::<crate::PdApplication>()
228 .unwrap();
229 if app.is_show_marked_delete(&pd) {
230 if let Err(err) = delete_show(&pd) {
231 error!("Error: {}", err);
232 error!("Failed to delete {}", pd.title());
233 }
234 }
235 // No need to update the UI after remove.
236 // The "unsubscribe" action already updated the UI after ignoring the show.
237 }
238 ));
239 });
240
241 toast
242 }