| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 glib::subclass::InitializingObject; |
| 26 |
use gtk::CompositeTemplate; |
| 27 |
use gtk::glib; |
| 28 |
use std::sync::Arc; |
| 29 |
use url::Url; |
| 30 |
|
| 31 |
use crate::app::Action; |
| 32 |
use crate::i18n::i18n; |
| 33 |
use crate::utils::{itunes_to_rss, soundcloud_to_rss}; |
| 34 |
use podcasts_data::dbqueries; |
| 35 |
use podcasts_data::discovery::SearchError::NoSearchPlatformsSelected; |
| 36 |
use podcasts_data::discovery::{ALL_PLATFORM_IDS, SearchError, search}; |
| 37 |
|
| 38 |
#[derive(Debug, CompositeTemplate, Default)] |
| 39 |
#[template(resource = "/org/gnome/Podcasts/gtk/discovery_page.ui")] |
| 40 |
pub struct DiscoveryPagePriv { |
| 41 |
#[template_child] |
| 42 |
list: TemplateChild<adw::PreferencesGroup>, |
| 43 |
#[template_child] |
| 44 |
entry: TemplateChild<gtk::Entry>, |
| 45 |
#[template_child] |
| 46 |
search_button: TemplateChild<gtk::Button>, |
| 47 |
#[template_child] |
| 48 |
loading_spinner: TemplateChild<adw::Spinner>, |
| 49 |
#[template_child] |
| 50 |
no_platforms_selected_label: TemplateChild<gtk::Label>, |
| 51 |
} |
| 52 |
|
| 53 |
impl DiscoveryPagePriv { |
| 54 |
fn init(&self, sender: &Sender<Action>) { |
| 55 |
let (loading_done, receiver) = async_channel::bounded(1); |
| 56 |
crate::MAINCONTEXT.spawn_local(clone!( |
| 57 |
#[weak(rename_to = this)] |
| 58 |
self, |
| 59 |
async move { |
| 60 |
while let Ok(result) = receiver.recv().await { |
| 61 |
if let Err(NoSearchPlatformsSelected) = result { |
| 62 |
this.entry.add_css_class("error"); |
| 63 |
this.no_platforms_selected_label.set_visible(true); |
| 64 |
this.no_platforms_selected_label.announce( |
| 65 |
&this.no_platforms_selected_label.text(), |
| 66 |
gtk::AccessibleAnnouncementPriority::High, |
| 67 |
); |
| 68 |
} |
| 69 |
this.search_button.set_visible(true); |
| 70 |
this.loading_spinner.set_visible(false); |
| 71 |
} |
| 72 |
} |
| 73 |
)); |
| 74 |
|
| 75 |
|
| 76 |
let settings = dbqueries::get_discovery_settings(); |
| 77 |
for id in ALL_PLATFORM_IDS { |
| 78 |
let switch = adw::SwitchRow::new(); |
| 79 |
let active = *settings.get(id).unwrap_or(&false); |
| 80 |
switch.set_active(active); |
| 81 |
switch.set_title(id); |
| 82 |
switch.set_selectable(false); |
| 83 |
switch.connect_active_notify(clone!( |
| 84 |
#[weak(rename_to = this)] |
| 85 |
self, |
| 86 |
move |s| { |
| 87 |
if let Err(e) = dbqueries::set_discovery_setting(id, s.is_active()) { |
| 88 |
error!("failed setting search preference: {e}"); |
| 89 |
} else if s.is_active() { |
| 90 |
this.entry.remove_css_class("error"); |
| 91 |
this.no_platforms_selected_label.set_visible(false); |
| 92 |
} |
| 93 |
} |
| 94 |
)); |
| 95 |
self.list.add(&switch); |
| 96 |
} |
| 97 |
|
| 98 |
self.entry.connect_activate(clone!( |
| 99 |
#[weak(rename_to = this)] |
| 100 |
self, |
| 101 |
#[strong] |
| 102 |
sender, |
| 103 |
move |entry| { |
| 104 |
let entry_text = entry.text().to_string(); |
| 105 |
let url = Url::parse(&entry_text); |
| 106 |
let this = this.clone(); |
| 107 |
this.search_button.set_visible(false); |
| 108 |
this.loading_spinner.set_visible(true); |
| 109 |
this.loading_spinner.announce( |
| 110 |
&this |
| 111 |
.loading_spinner |
| 112 |
.tooltip_text() |
| 113 |
.unwrap_or(i18n("Loading…").into()), |
| 114 |
gtk::AccessibleAnnouncementPriority::High, |
| 115 |
); |
| 116 |
this.entry.remove_css_class("error"); |
| 117 |
this.no_platforms_selected_label.set_visible(false); |
| 118 |
let loading_done = loading_done.clone(); |
| 119 |
crate::RUNTIME.spawn(clone!( |
| 120 |
#[strong] |
| 121 |
sender, |
| 122 |
async move { |
| 123 |
if let Err(e) = match url { |
| 124 |
Ok(url) => add_podcast_from_url(url.to_string(), &sender) |
| 125 |
.await |
| 126 |
.map_err(SearchError::from), |
| 127 |
Err(_) => search_podcasts(entry_text, &sender).await, |
| 128 |
} { |
| 129 |
match e { |
| 130 |
NoSearchPlatformsSelected => (), |
| 131 |
_ => send!(sender, Action::ErrorNotification(format!("{e}"))), |
| 132 |
}; |
| 133 |
send!(loading_done, Err(e)); |
| 134 |
} else { |
| 135 |
send!(loading_done, Ok(())) |
| 136 |
} |
| 137 |
} |
| 138 |
)); |
| 139 |
} |
| 140 |
)); |
| 141 |
|
| 142 |
self.search_button.connect_clicked(clone!( |
| 143 |
#[weak(rename_to = this)] |
| 144 |
self, |
| 145 |
move |_| { |
| 146 |
this.entry.emit_activate(); |
| 147 |
} |
| 148 |
)); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
#[glib::object_subclass] |
| 153 |
impl ObjectSubclass for DiscoveryPagePriv { |
| 154 |
const NAME: &'static str = "PdDiscoveryPage"; |
| 155 |
type Type = DiscoveryPage; |
| 156 |
type ParentType = adw::NavigationPage; |
| 157 |
|
| 158 |
fn class_init(klass: &mut Self::Class) { |
| 159 |
klass.bind_template(); |
| 160 |
} |
| 161 |
|
| 162 |
fn instance_init(obj: &InitializingObject<Self>) { |
| 163 |
obj.init_template(); |
| 164 |
} |
| 165 |
} |
| 166 |
impl WidgetImpl for DiscoveryPagePriv {} |
| 167 |
impl ObjectImpl for DiscoveryPagePriv {} |
| 168 |
impl NavigationPageImpl for DiscoveryPagePriv {} |
| 169 |
glib::wrapper! { |
| 170 |
pub struct DiscoveryPage(ObjectSubclass<DiscoveryPagePriv>) |
| 171 |
@extends adw::NavigationPage, gtk::Widget, |
| 172 |
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 173 |
} |
| 174 |
impl DiscoveryPage { |
| 175 |
pub(crate) fn new(sender: &Sender<Action>) -> Self { |
| 176 |
let widget: Self = glib::Object::new(); |
| 177 |
widget.imp().init(sender); |
| 178 |
|
| 179 |
widget |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
async fn add_podcast_from_url(url_input: String, sender: &Sender<Action>) -> Result<()> { |
| 184 |
let mut url = url_input; |
| 185 |
if !(url.starts_with("https://") || url.starts_with("http://")) { |
| 186 |
url = format!("http://{}", url); |
| 187 |
}; |
| 188 |
|
| 189 |
debug!("Url: {}", url); |
| 190 |
let url = if url.contains("itunes.com") || url.contains("apple.com") { |
| 191 |
info!("Detected itunes url."); |
| 192 |
let itunes_url = itunes_to_rss(&url).await?; |
| 193 |
info!("Resolved to {}", itunes_url); |
| 194 |
itunes_url |
| 195 |
} else if url.contains("soundcloud.com") && !url.contains("feeds.soundcloud.com") { |
| 196 |
info!("Detected soundcloud url."); |
| 197 |
let soundcloud_url = soundcloud_to_rss(&Url::parse(&url)?).await?; |
| 198 |
info!("Resolved to {}", soundcloud_url); |
| 199 |
soundcloud_url.to_string() |
| 200 |
} else { |
| 201 |
url.to_owned() |
| 202 |
}; |
| 203 |
|
| 204 |
crate::utils::subscribe(sender, url).await; |
| 205 |
Ok(()) |
| 206 |
} |
| 207 |
|
| 208 |
async fn search_podcasts(text: String, sender: &Sender<Action>) -> Result<(), SearchError> { |
| 209 |
let results = search(&text).await; |
| 210 |
send!(sender, Action::GoToFoundPodcasts(Arc::new(results?))); |
| 211 |
Ok(()) |
| 212 |
} |