Debian Codegraph @

podcasts-gtk/src/widgets/sheet_description.rs

raw symbols SCIP
1 // chapters_page.rs
2 //
3 // Copyright 2025 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 adw::subclass::prelude::*;
21 use async_channel::Sender;
22 use glib::clone;
23 use glib::subclass::InitializingObject;
24 use gtk::CompositeTemplate;
25 use gtk::glib;
26 use std::cell::Cell;
27
28 use crate::app::Action;
29 use crate::episode_description_parser;
30 use podcasts_data::{Episode, EpisodeId, ShowCoverModel};
31
32 #[derive(Debug, CompositeTemplate, Default)]
33 #[template(resource = "/org/gnome/Podcasts/gtk/sheet_description.ui")]
34 pub struct SheetDescriptionPriv {
35 #[template_child]
36 episode: TemplateChild<gtk::Label>,
37 #[template_child]
38 show: TemplateChild<gtk::Label>,
39 #[template_child]
40 description: TemplateChild<gtk::Label>,
41
42 id: Cell<Option<EpisodeId>>,
43 }
44
45 #[glib::object_subclass]
46 impl ObjectSubclass for SheetDescriptionPriv {
47 const NAME: &'static str = "PdSheetDescription";
48 type Type = SheetDescription;
49 type ParentType = adw::Bin;
50
51 fn class_init(klass: &mut Self::Class) {
52 klass.bind_template();
53 }
54
55 fn instance_init(obj: &InitializingObject<Self>) {
56 obj.init_template();
57 }
58 }
59
60 impl WidgetImpl for SheetDescriptionPriv {}
61 impl ObjectImpl for SheetDescriptionPriv {}
62 impl BinImpl for SheetDescriptionPriv {}
63
64 glib::wrapper! {
65 pub struct SheetDescription(ObjectSubclass<SheetDescriptionPriv>)
66 @extends adw::Bin, gtk::Widget,
67 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
68 }
69
70 impl SheetDescription {
71 pub(crate) fn init(&self, sender: &Sender<Action>) {
72 self.imp().description.connect_activate_link(clone!(
73 #[weak(rename_to=this)]
74 self,
75 #[strong]
76 sender,
77 #[upgrade_or]
78 glib::Propagation::Proceed,
79 move |_, url| {
80 if let Some(seconds_str) = url.strip_prefix("jump:") {
81 if let Ok(seconds) = seconds_str.parse() {
82 if let Some(id) = this.imp().id.get() {
83 send_blocking!(sender, Action::InitEpisodeAt(id, seconds));
84 }
85 } else {
86 error!("failed to parse jump link: {url}");
87 }
88 glib::Propagation::Stop
89 } else {
90 glib::Propagation::Proceed
91 }
92 }
93 ));
94 }
95
96 pub fn initialize_episode(&self, ep: &Episode, show: &ShowCoverModel) {
97 self.imp().id.set(Some(ep.id()));
98 self.imp().episode.set_label(ep.title());
99 self.imp().show.set_label(show.title());
100 self.set_description(ep);
101 }
102
103 fn set_description(&self, ep: &Episode) {
104 if let Some(t) = ep.description() {
105 let imp = self.imp();
106 let default_text = imp.description.text();
107
108 let markup = episode_description_parser::html2pango_markup(t);
109 imp.description.set_markup(&markup);
110 // recover from invalid markup
111 if imp.description.text() == default_text {
112 let plain = html2text::config::plain()
113 .string_from_read(t.as_bytes(), t.len())
114 .unwrap_or_else(|_| t.to_string());
115 imp.description.set_text(&plain);
116 }
117 };
118 }
119 }