Debian Codegraph @

podcasts-gtk/src/widgets/discovery_search_results.rs

raw symbols SCIP
1 // discovery_search_result.rs
2 //
3 // Copyright 2022-2024 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::prelude::*;
21 use adw::subclass::prelude::*;
22 use async_channel::Sender;
23 use glib::clone;
24 use glib::subclass::InitializingObject;
25 use gtk::CompositeTemplate;
26 use gtk::glib;
27
28 use crate::app::Action;
29 use crate::i18n::i18n;
30 use podcasts_data::discovery::FoundPodcast;
31
32 #[derive(Debug, CompositeTemplate, Default)]
33 #[template(resource = "/org/gnome/Podcasts/gtk/discovery_search_results.ui")]
34 pub struct SearchResultsPriv {
35 #[template_child]
36 list: TemplateChild<gtk::ListBox>,
37 #[template_child]
38 no_results: TemplateChild<gtk::Label>,
39 }
40
41 impl SearchResultsPriv {
42 pub(crate) fn init(&self, entries: &Vec<FoundPodcast>, sender: &Sender<Action>) {
43 for e in entries {
44 let entry_widget = Podcast::new(e, sender);
45 self.list.append(&entry_widget);
46 }
47 if entries.is_empty() {
48 self.no_results.set_visible(true);
49 self.list.set_visible(false);
50 }
51 }
52 }
53
54 #[glib::object_subclass]
55 impl ObjectSubclass for SearchResultsPriv {
56 const NAME: &'static str = "PdDiscoverySearchResults";
57 type Type = SearchResults;
58 type ParentType = adw::NavigationPage;
59
60 fn class_init(klass: &mut Self::Class) {
61 klass.bind_template();
62 }
63
64 fn instance_init(obj: &InitializingObject<Self>) {
65 obj.init_template();
66 }
67 }
68 impl WidgetImpl for SearchResultsPriv {}
69 impl ObjectImpl for SearchResultsPriv {}
70 impl NavigationPageImpl for SearchResultsPriv {}
71 glib::wrapper! {
72 pub struct SearchResults(ObjectSubclass<SearchResultsPriv>)
73 @extends adw::NavigationPage, gtk::Widget,
74 @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
75 }
76
77 impl SearchResults {
78 pub(crate) fn new(entries: &Vec<FoundPodcast>, sender: &Sender<Action>) -> Self {
79 let widget: Self = glib::Object::new();
80 widget.imp().init(entries, sender);
81
82 widget
83 }
84 }
85
86 #[derive(Debug, CompositeTemplate, Default)]
87 #[template(resource = "/org/gnome/Podcasts/gtk/discovery_found_podcast.ui")]
88 pub struct PodcastPriv {
89 #[template_child]
90 subscribe: TemplateChild<gtk::Button>,
91 #[template_child]
92 cover: TemplateChild<gtk::Image>,
93 #[template_child]
94 description: TemplateChild<gtk::Label>,
95 #[template_child]
96 feed_url: TemplateChild<gtk::Label>,
97 #[template_child]
98 title: TemplateChild<gtk::Label>,
99 #[template_child]
100 author: TemplateChild<gtk::Label>,
101 #[template_child]
102 episode_count: TemplateChild<gtk::Box>,
103 #[template_child]
104 episode_count_label: TemplateChild<gtk::Label>,
105 #[template_child]
106 last_publication: TemplateChild<gtk::Label>,
107 #[template_child]
108 loading_spinner: TemplateChild<adw::Spinner>,
109 #[template_child]
110 subscribe_stack: TemplateChild<gtk::Stack>,
111 }
112
113 impl PodcastPriv {
114 fn init(&self, p: &FoundPodcast, sender: &Sender<Action>) {
115 self.title.set_text(&p.title);
116 self.feed_url.set_text(&p.feed);
117 self.author.set_text(&p.author);
118
119 let description = p.description.trim();
120 if !description.is_empty() {
121 self.description.set_text(description);
122 self.description.set_tooltip_text(Some(description));
123 self.description.set_visible(true);
124 }
125 if let Some(ep_count) = p.episode_count {
126 self.episode_count_label.set_text(&format!("{}", ep_count));
127 self.episode_count_label.set_visible(true);
128 }
129 if let Some(last_publication) = p.last_publication {
130 let date = last_publication
131 .format_localized("%e %b %Y", *crate::CHRONO_LOCALE)
132 .to_string();
133 self.last_publication.set_text(&date);
134 self.last_publication.set_visible(true);
135 }
136
137 let url = p.feed.clone();
138 self.subscribe.connect_clicked(clone!(
139 #[weak(rename_to = this)]
140 self,
141 #[strong]
142 sender,
143 move |_| {
144 let (loading_done, receiver) = async_channel::bounded(1);
145
146 this.subscribe_stack
147 .set_visible_child(&this.loading_spinner.get());
148 this.loading_spinner.announce(
149 &this
150 .loading_spinner
151 .tooltip_text()
152 .unwrap_or(i18n("Subscribing to feed…").into()),
153 gtk::AccessibleAnnouncementPriority::High,
154 );
155 crate::RUNTIME.spawn(clone!(
156 #[strong]
157 sender,
158 #[strong]
159 url,
160 async move {
161 crate::utils::subscribe(&sender, url).await;
162 send!(loading_done, ());
163 }
164 ));
165
166 crate::MAINCONTEXT.spawn_local(clone!(
167 #[weak]
168 this,
169 async move {
170 while receiver.recv().await.is_ok() {
171 this.subscribe_stack
172 .set_visible_child(&this.subscribe.get());
173 }
174 }
175 ));
176 }
177 ));
178
179 let art = p.art.clone();
180 let (sender, receiver) = async_channel::bounded(1);
181 crate::RUNTIME.spawn(async move {
182 if let Err(e) = async {
183 let response = reqwest::get(&art).await?;
184 let bytes = response.bytes().await?;
185 let texture = {
186 let strm = gtk::gio::MemoryInputStream::from_bytes(&glib::Bytes::from(&bytes));
187 let pixbuf =
188 gtk::gdk_pixbuf::Pixbuf::from_stream(&strm, gtk::gio::Cancellable::NONE)?;
189 gtk::gdk::Texture::for_pixbuf(&pixbuf)
190 };
191 sender
192 .send(texture)
193 .await
194 .expect("failed to send img to main thread");
195 Ok::<(), anyhow::Error>(())
196 }
197 .await
198 {
199 error!("failed to load image for search result: {art} {e}");
200 }
201 });
202
203 crate::MAINCONTEXT.spawn_local(clone!(
204 #[weak(rename_to = this)]
205 self,
206 async move {
207 if let Ok(texture) = receiver.recv().await {
208 this.cover.set_paintable(Some(&texture));
209 }
210 }
211 ));
212 }
213 }
214
215 #[glib::object_subclass]
216 impl ObjectSubclass for PodcastPriv {
217 const NAME: &'static str = "PdDiscoveryFoundPodcast";
218 type Type = Podcast;
219 type ParentType = adw::PreferencesRow;
220
221 fn class_init(klass: &mut Self::Class) {
222 klass.bind_template();
223 }
224
225 fn instance_init(obj: &InitializingObject<Self>) {
226 obj.init_template();
227 }
228 }
229 impl ObjectImpl for PodcastPriv {}
230 impl WidgetImpl for PodcastPriv {}
231 impl ListBoxRowImpl for PodcastPriv {}
232 impl PreferencesRowImpl for PodcastPriv {}
233
234 glib::wrapper! {
235 pub struct Podcast(ObjectSubclass<PodcastPriv>)
236 @extends adw::PreferencesRow, gtk::ListBoxRow, gtk::Widget,
237 @implements gtk::Actionable, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
238 }
239
240 impl Podcast {
241 pub(crate) fn new(p: &FoundPodcast, sender: &Sender<Action>) -> Self {
242 let widget: Self = glib::Object::new();
243 widget.imp().init(p, sender);
244 widget
245 }
246 }