Debian Codegraph @

podcasts-gtk/src/widgets/show.rs

raw symbols SCIP
1 // show.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 adw::prelude::*;
21 use adw::subclass::prelude::*;
22 use anyhow::Result;
23 use async_channel::Sender;
24 use glib::clone;
25 use gtk::CompositeTemplate;
26 use gtk::gio;
27 use gtk::glib;
28 use std::cell::Cell;
29 use std::sync::Arc;
30
31 use crate::app::Action;
32 use crate::utils::lazy_load;
33 use crate::widgets::{EmptyShow, EpisodeWidget, ReadMoreLabel, ShowMenu};
34 use podcasts_data::dbqueries;
35 use podcasts_data::{EpisodeModel, EpisodeWidgetModel, Show, ShowId};
36
37 #[derive(Debug, Default, CompositeTemplate, glib::Properties)]
38 #[template(resource = "/org/gnome/Podcasts/gtk/show_widget.ui")]
39 #[properties(wrapper_type = ShowWidget)]
40 pub struct ShowWidgetPriv {
41 #[template_child]
42 pub cover: TemplateChild<gtk::Image>,
43 #[template_child]
44 pub read_more_label: TemplateChild<ReadMoreLabel>,
45 #[template_child]
46 pub episodes: TemplateChild<gtk::ListBox>,
47 #[template_child]
48 pub(crate) view: TemplateChild<gtk::Widget>,
49 #[template_child]
50 pub(crate) secondary_menu: TemplateChild<gtk::MenuButton>,
51
52 pub show_id: Cell<Option<ShowId>>,
53
54 #[property(set, get)]
55 pub(crate) is_mobile_layout: Cell<bool>,
56 #[property(set, get)]
57 pub(crate) description_width: Cell<i32>,
58 }
59
60 impl ShowWidgetPriv {
61 fn init(&self) {
62 self.read_more_label.init();
63 self.read_more_label
64 .update_property(&[gtk::accessible::Property::Description(
65 "Podcast Description",
66 )]);
67 }
68
69 /// Set the description text.
70 fn set_description(&self, text: &str) {
71 let markup = html2text::config::plain()
72 .string_from_read(text.as_bytes(), text.len())
73 .unwrap_or_else(|_| text.to_string());
74 let markup = markup.trim();
75 if !markup.is_empty() {
76 self.read_more_label.set_label(markup);
77 }
78 }
79 }
80
81 #[glib::object_subclass]
82 impl ObjectSubclass for ShowWidgetPriv {
83 const NAME: &'static str = "PdShowWidget";
84 type Type = super::ShowWidget;
85 type ParentType = gtk::Widget;
86
87 fn class_init(klass: &mut Self::Class) {
88 Self::bind_template(klass);
89 klass.set_layout_manager_type::<gtk::BinLayout>();
90 }
91
92 fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
93 obj.init_template();
94 }
95 }
96
97 #[glib::derived_properties]
98 impl ObjectImpl for ShowWidgetPriv {
99 fn dispose(&self) {
100 self.view.unparent();
101 }
102 }
103
104 impl WidgetImpl for ShowWidgetPriv {}
105
106 glib::wrapper! {
107 pub struct ShowWidget(ObjectSubclass<ShowWidgetPriv>)
108 @extends gtk::Widget,
109 @implements gtk::ConstraintTarget, gtk::Buildable, gtk::Accessible;
110 }
111
112 impl Default for ShowWidget {
113 fn default() -> Self {
114 glib::Object::new()
115 }
116 }
117
118 impl ShowWidget {
119 pub(crate) fn new(pd: Arc<Show>, sender: &Sender<Action>) -> ShowWidget {
120 let widget = ShowWidget::default();
121 widget.init(&pd);
122
123 let menu = ShowMenu::new(&pd, &widget.imp().episodes, sender);
124 widget.imp().secondary_menu.set_menu_model(Some(&menu.menu));
125
126 let res = populate_listbox(&widget, pd, sender);
127 debug_assert!(res.is_ok());
128
129 widget
130 }
131
132 pub(crate) fn init(&self, pd: &Arc<Show>) {
133 let self_ = self.imp();
134 self_.init();
135 self_.set_description(pd.description());
136 self_.show_id.set(Some(pd.id()));
137 self.set_cover(pd);
138
139 self.bind_property("is_mobile_layout", self, "description_width")
140 .transform_to(move |_, is_mobile: bool| Some(if is_mobile { 320 } else { 600 }))
141 .flags(glib::BindingFlags::SYNC_CREATE)
142 .build();
143 }
144
145 /// Set the show cover.
146 fn set_cover(&self, pd: &Arc<Show>) {
147 crate::download_covers::load_widget_texture(
148 &self.imp().cover.get(),
149 pd.id(),
150 crate::Thumb256,
151 true,
152 );
153 }
154
155 pub(crate) fn show_id(&self) -> Option<ShowId> {
156 self.imp().show_id.get()
157 }
158
159 pub(crate) fn update_episode(&self, ep: &EpisodeWidgetModel) {
160 let imp = self.imp();
161 let id = ep.id();
162 let mut i = 0;
163 while let Some(row) = imp.episodes.row_at_index(i) {
164 if let Some(Ok(episode)) = row.child().map(|w| w.downcast::<EpisodeWidget>()) {
165 if episode.id() == id {
166 episode.update_episode_state(ep);
167 return;
168 }
169 }
170 i += 1;
171 }
172 }
173 }
174
175 /// Populate the listbox with the shows episodes.
176 fn populate_listbox(show: &ShowWidget, pd: Arc<Show>, sender: &Sender<Action>) -> Result<()> {
177 let count = dbqueries::get_pd_episodes_count(&pd)?;
178
179 if count == 0 {
180 let empty = EmptyShow::default();
181 show.imp().episodes.append(&empty);
182 return Ok(());
183 }
184
185 let constructor = clone!(
186 #[strong]
187 sender,
188 move |ep: EpisodeWidgetModel| {
189 let id = ep.id();
190 let episode_widget = EpisodeWidget::new(&sender, ep, false);
191 let row = gtk::ListBoxRow::new();
192 row.set_child(Some(&episode_widget));
193 row.set_action_name(Some("app.go-to-episode"));
194 row.set_action_target_value(Some(&id.0.to_variant()));
195 row.upcast()
196 }
197 );
198
199 let listbox = show.imp().episodes.upcast_ref::<gtk::Widget>().downgrade();
200 crate::MAINCONTEXT.spawn_local_with_priority(
201 glib::source::Priority::DEFAULT_IDLE,
202 async move {
203 let episodes = gio::spawn_blocking(clone!(
204 #[strong]
205 pd,
206 move || dbqueries::get_pd_episodeswidgets(&pd),
207 ));
208
209 if let Ok(Ok(episodes)) = episodes.await {
210 let results = lazy_load(episodes, listbox, constructor).await;
211 for result in results {
212 if let Err(e) = result {
213 log::error!("Error: {:?}", e);
214 }
215 }
216 }
217 },
218 );
219
220 Ok(())
221 }