| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
use anyhow::Result; |
| 21 |
use async_channel::Sender; |
| 22 |
use glib::clone; |
| 23 |
use gtk::prelude::*; |
| 24 |
use gtk::{gio, glib}; |
| 25 |
use std::sync::Arc; |
| 26 |
|
| 27 |
use crate::app::Action; |
| 28 |
use crate::i18n::{i18n, i18n_f}; |
| 29 |
use crate::utils; |
| 30 |
use podcasts_data::Show; |
| 31 |
use podcasts_data::dbqueries; |
| 32 |
use podcasts_data::utils::delete_show; |
| 33 |
|
| 34 |
#[derive(Debug, Clone)] |
| 35 |
pub(crate) struct ShowMenu { |
| 36 |
pub(crate) menu: gio::MenuModel, |
| 37 |
website: gio::SimpleAction, |
| 38 |
played: gio::SimpleAction, |
| 39 |
unsub: gio::SimpleAction, |
| 40 |
group: gio::SimpleActionGroup, |
| 41 |
} |
| 42 |
|
| 43 |
impl Default for ShowMenu { |
| 44 |
fn default() -> Self { |
| 45 |
let builder = gtk::Builder::from_resource("/org/gnome/Podcasts/gtk/show_menu.ui"); |
| 46 |
let menu = builder.object("show_menu").unwrap(); |
| 47 |
let website = gio::SimpleAction::new("open-website", None); |
| 48 |
let played = gio::SimpleAction::new("mark-played", None); |
| 49 |
let unsub = gio::SimpleAction::new("unsubscribe", None); |
| 50 |
let group = gio::SimpleActionGroup::new(); |
| 51 |
|
| 52 |
group.add_action(&website); |
| 53 |
group.add_action(&played); |
| 54 |
group.add_action(&unsub); |
| 55 |
|
| 56 |
ShowMenu { |
| 57 |
menu, |
| 58 |
website, |
| 59 |
played, |
| 60 |
unsub, |
| 61 |
group, |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
impl ShowMenu { |
| 67 |
pub(crate) fn new(pd: &Arc<Show>, episodes: >k::ListBox, sender: &Sender<Action>) -> Self { |
| 68 |
let s = Self::default(); |
| 69 |
s.init(pd, episodes, sender); |
| 70 |
s |
| 71 |
} |
| 72 |
|
| 73 |
fn init(&self, pd: &Arc<Show>, episodes: >k::ListBox, sender: &Sender<Action>) { |
| 74 |
self.connect_website(pd); |
| 75 |
self.connect_played(pd, episodes, sender); |
| 76 |
self.connect_unsub(pd, sender); |
| 77 |
|
| 78 |
let app = gio::Application::default() |
| 79 |
.expect("Could not get default application") |
| 80 |
.downcast::<gtk::Application>() |
| 81 |
.unwrap(); |
| 82 |
let win = app.active_window().expect("No active window"); |
| 83 |
win.insert_action_group("show", Some(&self.group)); |
| 84 |
} |
| 85 |
|
| 86 |
fn connect_website(&self, pd: &Arc<Show>) { |
| 87 |
|
| 88 |
self.website.connect_activate(clone!( |
| 89 |
#[strong] |
| 90 |
pd, |
| 91 |
move |_, _| { |
| 92 |
let link = pd.link(); |
| 93 |
info!("Opening link: {}", link); |
| 94 |
let res = open::that(link); |
| 95 |
debug_assert!(res.is_ok()); |
| 96 |
} |
| 97 |
)); |
| 98 |
} |
| 99 |
|
| 100 |
fn connect_played(&self, pd: &Arc<Show>, episodes: >k::ListBox, sender: &Sender<Action>) { |
| 101 |
self.played.connect_activate(clone!( |
| 102 |
#[strong] |
| 103 |
pd, |
| 104 |
#[strong] |
| 105 |
sender, |
| 106 |
#[weak] |
| 107 |
episodes, |
| 108 |
move |_, _| { |
| 109 |
let res = dim_titles(&episodes); |
| 110 |
debug_assert!(res.is_some()); |
| 111 |
send_blocking!(sender, Action::MarkAllPlayerNotification(pd.clone())); |
| 112 |
} |
| 113 |
)); |
| 114 |
} |
| 115 |
|
| 116 |
fn connect_unsub(&self, pd: &Arc<Show>, sender: &Sender<Action>) { |
| 117 |
self.unsub.connect_activate(clone!( |
| 118 |
#[strong] |
| 119 |
pd, |
| 120 |
#[strong] |
| 121 |
sender, |
| 122 |
move |unsub, _| { |
| 123 |
unsub.set_enabled(false); |
| 124 |
send_blocking!(sender, Action::RemoveShow(pd.clone())); |
| 125 |
|
| 126 |
send_blocking!(sender, Action::RefreshShowsView); |
| 127 |
send_blocking!(sender, Action::RefreshEpisodesView); |
| 128 |
unsub.set_enabled(true); |
| 129 |
} |
| 130 |
)); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
fn dim_titles(episodes: >k::ListBox) -> Option<()> { |
| 139 |
|
| 140 |
let listmodel = episodes.observe_children(); |
| 141 |
for i in 0..listmodel.n_items() { |
| 142 |
let obj = listmodel.item(i)?; |
| 143 |
let row = obj.downcast_ref::<gtk::ListBoxRow>()?; |
| 144 |
dim_row_title(row)?; |
| 145 |
} |
| 146 |
Some(()) |
| 147 |
} |
| 148 |
|
| 149 |
fn dim_row_title(row: >k::ListBoxRow) -> Option<()> { |
| 150 |
|
| 151 |
let container = row.first_child().and_downcast::<gtk::Box>()?; |
| 152 |
let container_child = container.first_child().and_downcast::<gtk::Box>()?; |
| 153 |
let container_gradchild = container_child.first_child().and_downcast::<gtk::Box>()?; |
| 154 |
let container_great_gradchild = container_gradchild |
| 155 |
.first_child() |
| 156 |
.and_downcast::<gtk::Box>()?; |
| 157 |
let title = container_great_gradchild |
| 158 |
.first_child() |
| 159 |
.and_downcast::<gtk::Label>()?; |
| 160 |
|
| 161 |
title.add_css_class("dim-label"); |
| 162 |
|
| 163 |
|
| 164 |
let checkmark = title.next_sibling().and_downcast::<gtk::Image>()?; |
| 165 |
checkmark.set_visible(true); |
| 166 |
Some(()) |
| 167 |
} |
| 168 |
|
| 169 |
fn mark_all_watched(pd: &Show, sender: &Sender<Action>) -> Result<()> { |
| 170 |
|
| 171 |
dbqueries::update_none_to_played_now(pd)?; |
| 172 |
|
| 173 |
|
| 174 |
send_blocking!(sender, Action::RefreshWidgetIfSame(pd.id())); |
| 175 |
send_blocking!(sender, Action::RefreshEpisodesView); |
| 176 |
Ok(()) |
| 177 |
} |
| 178 |
|
| 179 |
pub(crate) fn mark_all_notif(pd: Arc<Show>, sender: &Sender<Action>) -> adw::Toast { |
| 180 |
let id = pd.id(); |
| 181 |
let toast = adw::Toast::new(&i18n("Marked all episodes as listened")); |
| 182 |
toast.set_button_label(Some(&i18n("Undo"))); |
| 183 |
toast.set_action_target_value(Some(&id.0.to_variant())); |
| 184 |
toast.set_action_name(Some("app.undo-mark-all")); |
| 185 |
|
| 186 |
toast.connect_dismissed(clone!( |
| 187 |
#[strong] |
| 188 |
sender, |
| 189 |
move |_| { |
| 190 |
let app = gio::Application::default() |
| 191 |
.expect("Could not get default application") |
| 192 |
.downcast::<crate::PdApplication>() |
| 193 |
.unwrap(); |
| 194 |
if app.is_show_marked_mark(&pd) { |
| 195 |
let res = mark_all_watched(&pd, &sender); |
| 196 |
debug_assert!(res.is_ok()); |
| 197 |
} |
| 198 |
} |
| 199 |
)); |
| 200 |
|
| 201 |
toast |
| 202 |
} |
| 203 |
|
| 204 |
pub(crate) fn remove_show_notif(pd: Arc<Show>) -> adw::Toast { |
| 205 |
let text = i18n_f("Unsubscribed from {}", &[pd.title()]); |
| 206 |
let id = pd.id(); |
| 207 |
|
| 208 |
let toast = adw::Toast::new(&text); |
| 209 |
toast.set_button_label(Some(&i18n("Undo"))); |
| 210 |
toast.set_action_target_value(Some(&id.0.to_variant())); |
| 211 |
toast.set_action_name(Some("app.undo-remove-show")); |
| 212 |
|
| 213 |
let res = utils::ignore_show(id); |
| 214 |
debug_assert!(res.is_ok()); |
| 215 |
|
| 216 |
toast.connect_dismissed(move |_args| { |
| 217 |
let res = utils::unignore_show(id); |
| 218 |
debug_assert!(res.is_ok()); |
| 219 |
|
| 220 |
|
| 221 |
gio::spawn_blocking(clone!( |
| 222 |
#[strong] |
| 223 |
pd, |
| 224 |
move || { |
| 225 |
let app = gio::Application::default() |
| 226 |
.expect("Could not get default application") |
| 227 |
.downcast::<crate::PdApplication>() |
| 228 |
.unwrap(); |
| 229 |
if app.is_show_marked_delete(&pd) { |
| 230 |
if let Err(err) = delete_show(&pd) { |
| 231 |
error!("Error: {}", err); |
| 232 |
error!("Failed to delete {}", pd.title()); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
} |
| 238 |
)); |
| 239 |
}); |
| 240 |
|
| 241 |
toast |
| 242 |
} |