| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use adw::subclass::prelude::*; |
| 21 |
use anyhow::Result; |
| 22 |
use async_channel::Sender; |
| 23 |
use chrono::prelude::*; |
| 24 |
use glib::clone; |
| 25 |
use glib::subclass::InitializingObject; |
| 26 |
use gtk::CompositeTemplate; |
| 27 |
use gtk::prelude::*; |
| 28 |
use gtk::{gio, glib}; |
| 29 |
use std::borrow::Borrow; |
| 30 |
use std::cell::Cell; |
| 31 |
use std::sync::Arc; |
| 32 |
|
| 33 |
use crate::app::Action; |
| 34 |
use crate::episode_description_parser; |
| 35 |
use crate::widgets::DownloadProgressBar; |
| 36 |
use crate::widgets::EpisodeMenu; |
| 37 |
use podcasts_data::EpisodeWidgetModel; |
| 38 |
use podcasts_data::{Episode, EpisodeId, EpisodeModel, Show, ShowId}; |
| 39 |
use podcasts_data::{dbqueries, downloader}; |
| 40 |
|
| 41 |
pub enum EpisodeDescriptionAction { |
| 42 |
EpisodeSpecificImage(gtk::gdk::Texture), |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Debug, CompositeTemplate, Default)] |
| 46 |
#[template(resource = "/org/gnome/Podcasts/gtk/episode_description.ui")] |
| 47 |
pub struct EpisodeDescriptionPriv { |
| 48 |
#[template_child] |
| 49 |
menu_button: TemplateChild<gtk::MenuButton>, |
| 50 |
#[template_child] |
| 51 |
cover: TemplateChild<gtk::Image>, |
| 52 |
#[template_child] |
| 53 |
podcast_title: TemplateChild<gtk::Label>, |
| 54 |
#[template_child] |
| 55 |
episode_title: TemplateChild<gtk::Label>, |
| 56 |
#[template_child] |
| 57 |
episode_duration: TemplateChild<gtk::Label>, |
| 58 |
#[template_child] |
| 59 |
description: TemplateChild<gtk::Label>, |
| 60 |
#[template_child] |
| 61 |
episode_specific_cover: TemplateChild<gtk::Picture>, |
| 62 |
|
| 63 |
#[template_child] |
| 64 |
progressbar: TemplateChild<DownloadProgressBar>, |
| 65 |
|
| 66 |
#[template_child] |
| 67 |
stream_button: TemplateChild<gtk::Button>, |
| 68 |
#[template_child] |
| 69 |
download_button: TemplateChild<gtk::Button>, |
| 70 |
#[template_child] |
| 71 |
cancel_button: TemplateChild<gtk::Button>, |
| 72 |
#[template_child] |
| 73 |
play_button: TemplateChild<gtk::Button>, |
| 74 |
#[template_child] |
| 75 |
pause_button: TemplateChild<gtk::Button>, |
| 76 |
#[template_child] |
| 77 |
delete_button: TemplateChild<gtk::Button>, |
| 78 |
|
| 79 |
episode: Cell<Option<EpisodeId>>, |
| 80 |
} |
| 81 |
|
| 82 |
impl EpisodeDescriptionPriv { |
| 83 |
fn init(&self, sender: Sender<Action>, ep: Arc<Episode>, show: Arc<Show>) { |
| 84 |
let (ed_sender, r) = async_channel::unbounded(); |
| 85 |
crate::MAINCONTEXT.spawn_local(clone!( |
| 86 |
#[weak(rename_to = this)] |
| 87 |
self, |
| 88 |
async move { |
| 89 |
while let Ok(action) = r.recv().await { |
| 90 |
this.do_action(action); |
| 91 |
} |
| 92 |
} |
| 93 |
)); |
| 94 |
|
| 95 |
self.set_description(&ep); |
| 96 |
self.set_duration(&ep); |
| 97 |
self.episode_title.set_text(ep.title()); |
| 98 |
self.podcast_title.set_text(show.title()); |
| 99 |
self.set_cover(ep.show_id()); |
| 100 |
if let Some(uri) = ep.image_uri().as_ref() { |
| 101 |
|
| 102 |
if *uri != show.image_uri().unwrap_or("") { |
| 103 |
let _ = self.set_episode_specific_cover(ed_sender, ep.show_id(), uri); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
let id = ep.id(); |
| 108 |
self.episode.set(Some(id)); |
| 109 |
self.description.connect_activate_link(clone!( |
| 110 |
#[strong] |
| 111 |
sender, |
| 112 |
move |_, url| { |
| 113 |
if let Some(seconds_str) = url.strip_prefix("jump:") { |
| 114 |
if let Ok(seconds) = seconds_str.parse() { |
| 115 |
send_blocking!(sender, Action::InitEpisodeAt(id, seconds)); |
| 116 |
} else { |
| 117 |
error!("failed to parse jump link: {}", url); |
| 118 |
} |
| 119 |
glib::Propagation::Stop |
| 120 |
} else { |
| 121 |
glib::Propagation::Proceed |
| 122 |
} |
| 123 |
} |
| 124 |
)); |
| 125 |
|
| 126 |
let ep: &Episode = ep.borrow(); |
| 127 |
if ep.uri().is_some() { |
| 128 |
self.init_buttons(sender, ep, id); |
| 129 |
self.determine_button_state(&ep.clone().into()); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
fn init_buttons(&self, sender: Sender<Action>, ep: &Episode, id: EpisodeId) { |
| 134 |
self.stream_button.connect_clicked(clone!( |
| 135 |
#[strong] |
| 136 |
sender, |
| 137 |
move |_| { |
| 138 |
send_blocking!(sender, Action::StreamEpisode(id)); |
| 139 |
} |
| 140 |
)); |
| 141 |
|
| 142 |
self.play_button.connect_clicked(clone!( |
| 143 |
#[strong] |
| 144 |
sender, |
| 145 |
move |_| { |
| 146 |
send_blocking!(sender, Action::InitEpisode(id)); |
| 147 |
} |
| 148 |
)); |
| 149 |
|
| 150 |
let show_id = ep.show_id(); |
| 151 |
self.download_button.connect_clicked(clone!( |
| 152 |
#[weak(rename_to = this)] |
| 153 |
self, |
| 154 |
#[strong] |
| 155 |
sender, |
| 156 |
move |_| { |
| 157 |
use podcasts_data::utils::get_download_dir; |
| 158 |
if let Err(e) = (|| { |
| 159 |
let pd = dbqueries::get_podcast_from_id(show_id)?; |
| 160 |
let download_dir = get_download_dir(pd.title())?; |
| 161 |
crate::manager::add(sender.clone(), id, download_dir)?; |
| 162 |
Ok::<(), anyhow::Error>(()) |
| 163 |
})() { |
| 164 |
error!("failed to start download {e}"); |
| 165 |
} |
| 166 |
this.refresh_buttons(id); |
| 167 |
this.progressbar.grab_focus(); |
| 168 |
send_blocking!(sender, Action::RefreshEpisode(id)); |
| 169 |
} |
| 170 |
)); |
| 171 |
|
| 172 |
self.delete_button.connect_clicked(clone!( |
| 173 |
#[weak(rename_to = this)] |
| 174 |
self, |
| 175 |
#[strong] |
| 176 |
sender, |
| 177 |
move |_| { |
| 178 |
if let Ok(ep) = dbqueries::get_episode_from_id(id) { |
| 179 |
let mut cleaner_ep = podcasts_data::EpisodeCleanerModel::from(ep); |
| 180 |
if let Err(e) = podcasts_data::utils::delete_local_content(&mut cleaner_ep) { |
| 181 |
error!("failed to delete ep {e}"); |
| 182 |
} |
| 183 |
} |
| 184 |
this.refresh_buttons(id); |
| 185 |
send_blocking!(sender, Action::RefreshEpisode(id)); |
| 186 |
} |
| 187 |
)); |
| 188 |
|
| 189 |
self.progressbar.init(ep.id()); |
| 190 |
self.progressbar.connect_state_change(clone!( |
| 191 |
#[weak(rename_to = this)] |
| 192 |
self, |
| 193 |
move |_| { |
| 194 |
this.refresh_buttons(id); |
| 195 |
} |
| 196 |
)); |
| 197 |
self.cancel_button.connect_clicked(clone!( |
| 198 |
#[weak(rename_to = this)] |
| 199 |
self, |
| 200 |
move |_| { |
| 201 |
if let Err(e) = this.progressbar.cancel() { |
| 202 |
error!("failed to cancel download {e}"); |
| 203 |
} |
| 204 |
} |
| 205 |
)); |
| 206 |
} |
| 207 |
|
| 208 |
fn refresh_buttons(&self, id: EpisodeId) { |
| 209 |
match dbqueries::get_episode_widget_from_id(id) { |
| 210 |
Ok(ep) => self.determine_button_state(&ep), |
| 211 |
Err(e) => error!("failed to fetch episode for description refresh {e}"), |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
fn determine_button_state(&self, ep: &EpisodeWidgetModel) { |
| 216 |
let app = gio::Application::default() |
| 217 |
.expect("Could not get default application") |
| 218 |
.downcast::<crate::PdApplication>() |
| 219 |
.unwrap(); |
| 220 |
let is_playing = app.is_playing(ep.id()); |
| 221 |
self.pause_button.set_visible(is_playing); |
| 222 |
|
| 223 |
let is_downloading = self.progressbar.check_if_downloading().unwrap_or(false); |
| 224 |
self.cancel_button.set_visible(is_downloading); |
| 225 |
let is_downloaded = ep.local_uri().is_some(); |
| 226 |
self.download_button |
| 227 |
.set_visible(!is_downloaded && !is_downloading); |
| 228 |
self.stream_button |
| 229 |
.set_visible(!is_playing && !is_downloaded); |
| 230 |
self.delete_button.set_visible(is_downloaded); |
| 231 |
self.play_button.set_visible(!is_playing && is_downloaded); |
| 232 |
} |
| 233 |
|
| 234 |
fn set_description(&self, ep: &Episode) { |
| 235 |
if let Some(t) = ep.description() { |
| 236 |
let default_text = self.description.text(); |
| 237 |
|
| 238 |
let markup = episode_description_parser::html2pango_markup(t); |
| 239 |
self.description.set_markup(&markup); |
| 240 |
|
| 241 |
if self.description.text() == default_text { |
| 242 |
let plain = html2text::config::plain() |
| 243 |
.string_from_read(t.as_bytes(), t.len()) |
| 244 |
.unwrap_or_else(|_| t.to_string()); |
| 245 |
self.description.set_text(&plain); |
| 246 |
} |
| 247 |
}; |
| 248 |
} |
| 249 |
|
| 250 |
fn set_duration(&self, ep: &Episode) { |
| 251 |
let duration = ep.duration().map(|s| { |
| 252 |
let seconds = s % 60; |
| 253 |
let minutes = (s / 60) % 60; |
| 254 |
let hours = (s / 60) / 60; |
| 255 |
format!("{:02}:{:02}:{:02}", hours, minutes, seconds) |
| 256 |
}); |
| 257 |
let now = Local::now(); |
| 258 |
let ep_local = DateTime::<Local>::from(ep.epoch().and_utc()); |
| 259 |
|
| 260 |
let date = if now.year() != ep_local.year() { |
| 261 |
ep_local |
| 262 |
.format_localized("%e %b %Y", *crate::CHRONO_LOCALE) |
| 263 |
.to_string() |
| 264 |
|
| 265 |
} else { |
| 266 |
ep_local |
| 267 |
.format_localized("%e %b", *crate::CHRONO_LOCALE) |
| 268 |
.to_string() |
| 269 |
}; |
| 270 |
|
| 271 |
let duration_date = match duration { |
| 272 |
Some(duration) => format!("{} ยท {}", duration, date), |
| 273 |
None => date, |
| 274 |
}; |
| 275 |
self.episode_duration.set_text(&duration_date); |
| 276 |
} |
| 277 |
|
| 278 |
fn set_cover(&self, show_id: ShowId) { |
| 279 |
crate::download_covers::load_widget_texture( |
| 280 |
&self.cover.get(), |
| 281 |
show_id, |
| 282 |
crate::Thumb64, |
| 283 |
true, |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
fn set_episode_specific_cover( |
| 288 |
&self, |
| 289 |
sender: Sender<EpisodeDescriptionAction>, |
| 290 |
show_id: ShowId, |
| 291 |
uri: &str, |
| 292 |
) -> Result<()> { |
| 293 |
let pd = dbqueries::get_podcast_cover_from_id(show_id)?; |
| 294 |
let uri = uri.to_owned(); |
| 295 |
crate::RUNTIME.spawn(clone!( |
| 296 |
#[strong] |
| 297 |
pd, |
| 298 |
async move { |
| 299 |
if let Err(e) = async move { |
| 300 |
let path = downloader::cache_episode_image(&pd, &uri, true).await?; |
| 301 |
let texture = gtk::gdk::Texture::from_filename(path)?; |
| 302 |
send!( |
| 303 |
sender, |
| 304 |
EpisodeDescriptionAction::EpisodeSpecificImage(texture) |
| 305 |
); |
| 306 |
Ok::<(), anyhow::Error>(()) |
| 307 |
} |
| 308 |
.await |
| 309 |
{ |
| 310 |
error!("failed to get episode specific cover: {e}"); |
| 311 |
} |
| 312 |
} |
| 313 |
)); |
| 314 |
Ok(()) |
| 315 |
} |
| 316 |
|
| 317 |
fn do_action(&self, action: EpisodeDescriptionAction) -> glib::ControlFlow { |
| 318 |
match action { |
| 319 |
EpisodeDescriptionAction::EpisodeSpecificImage(texture) => { |
| 320 |
self.episode_specific_cover.set_paintable(Some(&texture)); |
| 321 |
self.episode_specific_cover.set_visible(true); |
| 322 |
} |
| 323 |
} |
| 324 |
glib::ControlFlow::Continue |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
#[glib::object_subclass] |
| 329 |
impl ObjectSubclass for EpisodeDescriptionPriv { |
| 330 |
const NAME: &'static str = "PdEpisodeDescription"; |
| 331 |
type Type = EpisodeDescription; |
| 332 |
type ParentType = adw::NavigationPage; |
| 333 |
|
| 334 |
fn class_init(klass: &mut Self::Class) { |
| 335 |
klass.bind_template(); |
| 336 |
} |
| 337 |
|
| 338 |
fn instance_init(obj: &InitializingObject<Self>) { |
| 339 |
obj.init_template(); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
impl WidgetImpl for EpisodeDescriptionPriv {} |
| 344 |
impl ObjectImpl for EpisodeDescriptionPriv {} |
| 345 |
impl NavigationPageImpl for EpisodeDescriptionPriv { |
| 346 |
fn shown(&self) { |
| 347 |
self.description.set_selectable(true); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
glib::wrapper! { |
| 352 |
pub struct EpisodeDescription(ObjectSubclass<EpisodeDescriptionPriv>) |
| 353 |
@extends adw::NavigationPage, gtk::Widget, |
| 354 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 355 |
} |
| 356 |
|
| 357 |
impl EpisodeDescription { |
| 358 |
pub(crate) fn new(ep: Arc<Episode>, show: Arc<Show>, sender: Sender<Action>) -> Self { |
| 359 |
let widget: Self = glib::Object::new(); |
| 360 |
widget.update_episode_menu(&sender, ep.as_ref(), show.clone()); |
| 361 |
widget.imp().init(sender, ep, show); |
| 362 |
|
| 363 |
widget |
| 364 |
} |
| 365 |
|
| 366 |
pub(crate) fn update_episode_menu( |
| 367 |
&self, |
| 368 |
sender: &Sender<Action>, |
| 369 |
ep: &dyn EpisodeModel, |
| 370 |
show: Arc<Show>, |
| 371 |
) { |
| 372 |
let menu = EpisodeMenu::new(sender, ep, Some(show.id())); |
| 373 |
self.imp().menu_button.set_menu_model(Some(&menu.menu)); |
| 374 |
self.insert_action_group("episode", Some(&menu.group)); |
| 375 |
} |
| 376 |
|
| 377 |
pub(crate) fn id(&self) -> EpisodeId { |
| 378 |
self.imp().episode.get().unwrap() |
| 379 |
} |
| 380 |
|
| 381 |
pub(crate) fn update_episode(&self, ep: &EpisodeWidgetModel) { |
| 382 |
self.imp().determine_button_state(ep); |
| 383 |
} |
| 384 |
} |