| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use anyhow::{Context, Result, anyhow, bail}; |
| 21 |
use async_channel::Sender; |
| 22 |
use async_channel::unbounded; |
| 23 |
use chrono::prelude::*; |
| 24 |
use futures_util::StreamExt; |
| 25 |
use glib::clone; |
| 26 |
use glib::object::WeakRef; |
| 27 |
use gtk::FileFilter; |
| 28 |
use gtk::Widget; |
| 29 |
use gtk::prelude::*; |
| 30 |
use gtk::{gio, glib}; |
| 31 |
use regex::Regex; |
| 32 |
use serde_json::Value; |
| 33 |
use std::collections::HashSet; |
| 34 |
use std::sync::{Arc, LazyLock, Mutex}; |
| 35 |
use std::time::{Duration, Instant}; |
| 36 |
use url::Url; |
| 37 |
|
| 38 |
use crate::app::Action; |
| 39 |
use crate::feed_manager::FEED_MANAGER; |
| 40 |
use crate::i18n::{i18n, i18n_f}; |
| 41 |
use podcasts_data::dbqueries; |
| 42 |
use podcasts_data::downloader::client_builder; |
| 43 |
use podcasts_data::opml; |
| 44 |
use podcasts_data::utils::checkup; |
| 45 |
use podcasts_data::{ShowId, Source}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[macro_export] |
| 67 |
macro_rules! send { |
| 68 |
($sender:expr, $action:expr) => { |
| 69 |
if let Err(err) = $sender.send($action).await { |
| 70 |
panic!( |
| 71 |
"Failed to send \"{}\" action due to {}", |
| 72 |
stringify!($action), |
| 73 |
err |
| 74 |
); |
| 75 |
} |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
#[macro_export] |
| 82 |
macro_rules! send_blocking { |
| 83 |
($sender:expr, $action:expr) => { |
| 84 |
if let Err(err) = $sender.send_blocking($action) { |
| 85 |
panic!( |
| 86 |
"Failed to send \"{}\" action due to {}", |
| 87 |
stringify!($action), |
| 88 |
err |
| 89 |
); |
| 90 |
} |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
pub(crate) async fn lazy_load<C, W, T>( |
| 128 |
data: Vec<T>, |
| 129 |
container: WeakRef<W>, |
| 130 |
constructor: C, |
| 131 |
) -> Vec<Result<(), glib::JoinError>> |
| 132 |
where |
| 133 |
T: 'static, |
| 134 |
W: IsA<Widget> + Sized + 'static, |
| 135 |
C: Fn(T) -> W + 'static, |
| 136 |
{ |
| 137 |
let (sender, receiver) = unbounded::<W>(); |
| 138 |
|
| 139 |
let h1 = |
| 140 |
crate::MAINCONTEXT.spawn_local_with_priority(glib::source::Priority::LOW, async move { |
| 141 |
let mut total_duration = Duration::default(); |
| 142 |
let mut count = 0; |
| 143 |
|
| 144 |
for item in data { |
| 145 |
let start = Instant::now(); |
| 146 |
let widget = constructor(item); |
| 147 |
|
| 148 |
let duration = start.elapsed(); |
| 149 |
trace!("Created single widget in: {:?}", duration); |
| 150 |
total_duration += duration; |
| 151 |
count += 1; |
| 152 |
|
| 153 |
if let Err(err) = sender.send(widget).await { |
| 154 |
debug!("Got SendError, Channel is closed: {}", err); |
| 155 |
return; |
| 156 |
}; |
| 157 |
|
| 158 |
tokio::task::yield_now().await; |
| 159 |
} |
| 160 |
|
| 161 |
debug!("Created {} widgets in: {:?}", count, total_duration); |
| 162 |
}); |
| 163 |
|
| 164 |
let h2 = crate::MAINCONTEXT.spawn_local_with_priority( |
| 165 |
glib::source::Priority::DEFAULT_IDLE, |
| 166 |
async move { |
| 167 |
receiver |
| 168 |
.chunks(25) |
| 169 |
.for_each(move |widgets| { |
| 170 |
trace!("Received {} widgets", &widgets.len()); |
| 171 |
insert_widgets_idle(widgets, container.clone()) |
| 172 |
}) |
| 173 |
.await |
| 174 |
}, |
| 175 |
); |
| 176 |
|
| 177 |
futures_util::future::join_all([h1, h2]).await |
| 178 |
} |
| 179 |
|
| 180 |
async fn insert_widgets_idle<W>(data: Vec<W>, container: WeakRef<W>) |
| 181 |
where |
| 182 |
W: IsA<Widget> + Sized + 'static, |
| 183 |
{ |
| 184 |
let widget_count = data.len(); |
| 185 |
|
| 186 |
let mut count = 0; |
| 187 |
let mut start = Instant::now(); |
| 188 |
|
| 189 |
let mut batch_construction_time_total = Duration::default(); |
| 190 |
|
| 191 |
for widget in data { |
| 192 |
let w_start = Instant::now(); |
| 193 |
insert_widget_dynamic(widget, &container); |
| 194 |
let w_duration = w_start.elapsed(); |
| 195 |
trace!("Inserted single widget in: {:?}", w_duration); |
| 196 |
count += 1; |
| 197 |
batch_construction_time_total += w_duration; |
| 198 |
|
| 199 |
let duration = start.elapsed(); |
| 200 |
if duration > Duration::from_millis(1) { |
| 201 |
trace!("Inserted batch of {} widgets in: {:?}", count, duration); |
| 202 |
tokio::task::yield_now().await; |
| 203 |
count = 0; |
| 204 |
start = Instant::now(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
debug!( |
| 209 |
"Inserted {} widgets in: {:?}", |
| 210 |
widget_count, batch_construction_time_total |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
fn insert_widget_dynamic<W: IsA<Widget> + Sized>(widget: W, container: &WeakRef<W>) { |
| 215 |
let container = match container.upgrade() { |
| 216 |
Some(c) => c, |
| 217 |
None => return, |
| 218 |
}; |
| 219 |
|
| 220 |
if let Some(listbox) = container.dynamic_cast_ref::<gtk::ListBox>() { |
| 221 |
listbox.append(&widget); |
| 222 |
} else if let Some(flowbox) = container.dynamic_cast_ref::<gtk::FlowBox>() { |
| 223 |
flowbox.append(&widget); |
| 224 |
} else { |
| 225 |
unreachable!("Failed to downcast widget. {}", container.value_type()); |
| 226 |
} |
| 227 |
|
| 228 |
widget.set_visible(true); |
| 229 |
} |
| 230 |
|
| 231 |
static IGNORESHOWS: LazyLock<Arc<Mutex<HashSet<ShowId>>>> = |
| 232 |
LazyLock::new(|| Arc::new(Mutex::new(HashSet::new()))); |
| 233 |
|
| 234 |
pub(crate) fn ignore_show(id: ShowId) -> Result<bool> { |
| 235 |
IGNORESHOWS |
| 236 |
.lock() |
| 237 |
.map(|mut guard| guard.insert(id)) |
| 238 |
.map_err(|err| anyhow!("{err}")) |
| 239 |
} |
| 240 |
|
| 241 |
pub(crate) fn unignore_show(id: ShowId) -> Result<bool> { |
| 242 |
IGNORESHOWS |
| 243 |
.lock() |
| 244 |
.map(|mut guard| guard.remove(&id)) |
| 245 |
.map_err(|err| anyhow!("{err}")) |
| 246 |
} |
| 247 |
|
| 248 |
pub(crate) fn get_ignored_shows() -> Result<Vec<ShowId>> { |
| 249 |
IGNORESHOWS |
| 250 |
.lock() |
| 251 |
.map(|guard| guard.iter().cloned().collect::<Vec<_>>()) |
| 252 |
.map_err(|err| anyhow!("{err}")) |
| 253 |
} |
| 254 |
|
| 255 |
pub(crate) fn cleanup(cleanup_date: DateTime<Utc>) { |
| 256 |
if let Err(err) = checkup(cleanup_date) { |
| 257 |
error!("Check up failed: {err}"); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
pub(crate) async fn subscribe(sender: &Sender<Action>, feed: String) { |
| 262 |
let mut error_source = None; |
| 263 |
if let Err(e) = async { |
| 264 |
let source = dbqueries::get_source_from_uri(&feed).or_else(|_| Source::from_url(&feed))?; |
| 265 |
error_source = Some(source.clone()); |
| 266 |
let source_id = source.id(); |
| 267 |
info!("Subscribing to {feed}"); |
| 268 |
FEED_MANAGER.refresh(sender, vec![source]).await; |
| 269 |
let show = dbqueries::get_podcast_from_source_id(source_id)?; |
| 270 |
send!(sender, Action::RefreshAllViews); |
| 271 |
send!(sender, Action::GoToShow(Arc::new(show.clone()))); |
| 272 |
Ok::<(), anyhow::Error>(()) |
| 273 |
} |
| 274 |
.await |
| 275 |
{ |
| 276 |
error!("Failed to subscribe: {feed} {e}"); |
| 277 |
|
| 278 |
if let Some(error_source) = error_source { |
| 279 |
|
| 280 |
if dbqueries::get_podcast_from_source_id(error_source.id()).is_err() { |
| 281 |
if let Err(remove_err) = dbqueries::remove_source(&error_source) { |
| 282 |
error!("failed to remove failed source! {remove_err} {feed}"); |
| 283 |
} else { |
| 284 |
info!("auto removed source that failed to import {feed}"); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
send!( |
| 291 |
sender, |
| 292 |
Action::ErrorNotification(i18n_f( |
| 293 |
"Failed to subscribe to feed: {}", |
| 294 |
&[&feed.to_string()] |
| 295 |
)) |
| 296 |
); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
pub(crate) async fn itunes_to_rss(url: &str) -> Result<String> { |
| 302 |
let id = itunes_id_from_url(url).ok_or_else(|| anyhow!("Failed to find an iTunes ID."))?; |
| 303 |
itunes_lookup_id(id).await |
| 304 |
} |
| 305 |
|
| 306 |
fn itunes_id_from_url(url: &str) -> Option<u32> { |
| 307 |
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"/id([0-9]+)").unwrap()); |
| 308 |
|
| 309 |
|
| 310 |
let itunes_id = RE.captures_iter(url).next()?.get(1)?.as_str(); |
| 311 |
|
| 312 |
itunes_id.parse::<u32>().ok() |
| 313 |
} |
| 314 |
|
| 315 |
async fn itunes_lookup_id(id: u32) -> Result<String> { |
| 316 |
let url = format!("https://itunes.apple.com/lookup?id={}&entity=podcast", id); |
| 317 |
let req: Value = client_builder() |
| 318 |
.build()? |
| 319 |
.get(&url) |
| 320 |
.send() |
| 321 |
.await? |
| 322 |
.json() |
| 323 |
.await?; |
| 324 |
let rssurl = || -> Option<&str> { req.get("results")?.get(0)?.get("feedUrl")?.as_str() }; |
| 325 |
rssurl() |
| 326 |
.map(From::from) |
| 327 |
.ok_or_else(|| anyhow!("Failed to get url from itunes response")) |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
pub(crate) async fn soundcloud_to_rss(url: &Url) -> Result<Url> { |
| 333 |
|
| 334 |
|
| 335 |
let (user_id, playlist_id) = soundcloud_lookup_id(url) |
| 336 |
.await |
| 337 |
.ok_or_else(|| anyhow!("Failed to find a soundcloud ID."))?; |
| 338 |
if playlist_id != 0 { |
| 339 |
let url = format!( |
| 340 |
"https://feeds.soundcloud.com/playlists/soundcloud:playlists:{}/sounds.rss", |
| 341 |
playlist_id |
| 342 |
); |
| 343 |
Ok(Url::parse(&url)?) |
| 344 |
} else if user_id != 0 { |
| 345 |
let url = format!( |
| 346 |
"https://feeds.soundcloud.com/users/soundcloud:users:{}/sounds.rss", |
| 347 |
user_id |
| 348 |
); |
| 349 |
Ok(Url::parse(&url)?) |
| 350 |
} else { |
| 351 |
Err(anyhow!("No valid id's in soundcloud page.")) |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
async fn soundcloud_lookup_id(url: &Url) -> Option<(u64, u64)> { |
| 359 |
static RE_U: LazyLock<Regex> = |
| 360 |
LazyLock::new(|| Regex::new(r"soundcloud://users:([0-9]+)").unwrap()); |
| 361 |
static RE_P: LazyLock<Regex> = |
| 362 |
LazyLock::new(|| Regex::new(r"soundcloud://playlists:([0-9]+)").unwrap()); |
| 363 |
|
| 364 |
let url_str = url.to_string(); |
| 365 |
let client = client_builder().build().ok()?; |
| 366 |
let response = client.get(&url_str).send(); |
| 367 |
let response_text = response.await.ok()?.text().await.ok()?; |
| 368 |
let user_id = RE_U |
| 369 |
.captures_iter(&response_text) |
| 370 |
.next() |
| 371 |
.and_then(|r| r.get(1).map(|u| u.as_str())); |
| 372 |
let playlist_id = RE_P |
| 373 |
.captures_iter(&response_text) |
| 374 |
.next() |
| 375 |
.and_then(|r| r.get(1).map(|u| u.as_str())); |
| 376 |
|
| 377 |
Some(( |
| 378 |
user_id.and_then(|id| id.parse::<u64>().ok()).unwrap_or(0), |
| 379 |
playlist_id |
| 380 |
.and_then(|id| id.parse::<u64>().ok()) |
| 381 |
.unwrap_or(0), |
| 382 |
)) |
| 383 |
} |
| 384 |
|
| 385 |
pub(crate) async fn on_import_clicked(window: >k::ApplicationWindow, sender: &Sender<Action>) { |
| 386 |
|
| 387 |
let filter = FileFilter::new(); |
| 388 |
FileFilter::set_name(&filter, Some(i18n("OPML file").as_str())); |
| 389 |
filter.add_mime_type("application/xml"); |
| 390 |
filter.add_mime_type("text/xml"); |
| 391 |
filter.add_mime_type("text/x-opml"); |
| 392 |
|
| 393 |
let filters = gio::ListStore::new::<gtk::FileFilter>(); |
| 394 |
filters.append(&filter); |
| 395 |
|
| 396 |
|
| 397 |
let dialog = gtk::FileDialog::builder() |
| 398 |
.title(i18n( |
| 399 |
"Select the file from which to you want to import shows.", |
| 400 |
)) |
| 401 |
.accept_label(i18n("_Import")) |
| 402 |
.filters(&filters) |
| 403 |
.build(); |
| 404 |
|
| 405 |
if let Ok(file) = dialog.open_future(Some(window)).await { |
| 406 |
if let Some(path) = file.peek_path() { |
| 407 |
|
| 408 |
let result = gio::spawn_blocking(clone!( |
| 409 |
#[strong] |
| 410 |
sender, |
| 411 |
move || { |
| 412 |
|
| 413 |
opml::import_from_file(path) |
| 414 |
.map(|sources| { |
| 415 |
|
| 416 |
FEED_MANAGER.schedule_refresh(&sender, sources); |
| 417 |
}) |
| 418 |
.context("PARSE") |
| 419 |
} |
| 420 |
)) |
| 421 |
.await; |
| 422 |
|
| 423 |
if let Err(err) = result.unwrap_or_else(|e| bail!("Import Thread Error {e:#?}")) { |
| 424 |
let text = i18n_f("Failed to parse the imported file {}", &[&err.to_string()]); |
| 425 |
send!(sender, Action::ErrorNotification(text)); |
| 426 |
} |
| 427 |
} |
| 428 |
}; |
| 429 |
} |
| 430 |
|
| 431 |
pub(crate) async fn on_export_clicked(window: >k::ApplicationWindow, sender: &Sender<Action>) { |
| 432 |
|
| 433 |
let filter = FileFilter::new(); |
| 434 |
FileFilter::set_name(&filter, Some(i18n("OPML file").as_str())); |
| 435 |
filter.add_mime_type("application/xml"); |
| 436 |
filter.add_mime_type("text/xml"); |
| 437 |
filter.add_mime_type("text/x-opml"); |
| 438 |
|
| 439 |
let filters = gio::ListStore::new::<gtk::FileFilter>(); |
| 440 |
filters.append(&filter); |
| 441 |
|
| 442 |
|
| 443 |
let dialog = gtk::FileDialog::builder() |
| 444 |
|
| 445 |
.title(i18n("Export shows to…")) |
| 446 |
.accept_label(i18n("_Export")) |
| 447 |
.initial_name(format!( |
| 448 |
"{}.opml", |
| 449 |
|
| 450 |
i18n("gnome-podcasts-exported-shows") |
| 451 |
)) |
| 452 |
.filters(&filters) |
| 453 |
.build(); |
| 454 |
|
| 455 |
if let Ok(file) = dialog.save_future(Some(window)).await { |
| 456 |
if let Some(path) = file.peek_path() { |
| 457 |
debug!("File selected: {:?}", path); |
| 458 |
let result = gio::spawn_blocking(move || { |
| 459 |
opml::export_from_db(path, &i18n("GNOME Podcasts Subscriptions")) |
| 460 |
}) |
| 461 |
.await; |
| 462 |
if let Ok(Err(err)) = result { |
| 463 |
let text = i18n("Failed to export podcasts"); |
| 464 |
error!("Failed to export podcasts: {err}"); |
| 465 |
send!(sender, Action::ErrorNotification(text)); |
| 466 |
} |
| 467 |
} |
| 468 |
}; |
| 469 |
} |
| 470 |
|
| •471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| •650 |
|