From 5589334e975ad557877f2960f2cbae20eb61711e Mon Sep 17 00:00:00 2001 From: Philip Kristoffersen Date: Sat, 30 Apr 2022 08:34:51 +0200 Subject: [PATCH] Add option to ban download of images (#105) --- src/defaultconfig.toml | 1 + src/steamgriddb/downloader.rs | 3 ++ src/steamgriddb/settings.rs | 21 +++++++++++ src/ui/ui_image_download.rs | 69 ++++++++++++++++++++++------------- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/src/defaultconfig.toml b/src/defaultconfig.toml index 8309d40..c0fe7d9 100644 --- a/src/defaultconfig.toml +++ b/src/defaultconfig.toml @@ -5,6 +5,7 @@ blacklisted_games = [] auth_key = "Write your authentication key between these quotes" enabled = true prefer_animated = false +banned_images = [] [origin] enabled = true diff --git a/src/steamgriddb/downloader.rs b/src/steamgriddb/downloader.rs index e15bfa7..19fd238 100644 --- a/src/steamgriddb/downloader.rs +++ b/src/steamgriddb/downloader.rs @@ -51,6 +51,7 @@ pub async fn download_images_for_users<'b>( client, download_animated, settings.steam.optimize_for_big_picture, + settings, ) .await; res.unwrap_or_default() @@ -133,6 +134,7 @@ async fn search_for_images_to_download( client: &Client, download_animated: bool, download_big_picture: bool, + settings: &Settings, ) -> Result, Box> { let types = { let mut types = vec![ @@ -185,6 +187,7 @@ async fn search_for_images_to_download( let images_needed = shortcuts .iter() .filter(|s| search_results.contains_key(&s.app_id)) + .filter(|s| !settings.steamgrid_db.is_image_banned(&image_type, s.app_id)) .filter(|s| !known_images.contains(&image_type.file_name(s.app_id))); let image_ids: Vec = images_needed .clone() diff --git a/src/steamgriddb/settings.rs b/src/steamgriddb/settings.rs index 4c9fb37..bc440da 100644 --- a/src/steamgriddb/settings.rs +++ b/src/steamgriddb/settings.rs @@ -1,8 +1,29 @@ use serde::{Deserialize, Serialize}; +use super::ImageType; + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct SteamGridDbSettings { pub enabled: bool, pub auth_key: Option, pub prefer_animated: bool, + pub banned_images: Vec, +} + +impl SteamGridDbSettings { + pub fn is_image_banned(&self, image_type: &ImageType, app_id: u32) -> bool { + let ban_id = format!("{}-{}", app_id, image_type.name()); + self.banned_images.contains(&ban_id) + } + + pub fn set_image_banned(&mut self, image_type: &ImageType, app_id: u32, should_ban: bool) { + let ban_id = format!("{}-{}", app_id, image_type.name()); + let images_banned = &mut self.banned_images; + let is_banned = images_banned.contains(&ban_id); + match (is_banned, should_ban) { + (true, false) => images_banned.retain(|i| !i.eq(&ban_id)), + (false, true) => images_banned.push(ban_id), + _ => {} + } + } } diff --git a/src/ui/ui_image_download.rs b/src/ui/ui_image_download.rs index cd07267..904455a 100644 --- a/src/ui/ui_image_download.rs +++ b/src/ui/ui_image_download.rs @@ -8,7 +8,7 @@ use crate::{ steamgriddb::{get_query_type, CachedSearch, ImageType, ToDownload}, }; use dashmap::DashMap; -use egui::{ImageButton, ScrollArea, TextureHandle}; +use egui::{ImageButton, ScrollArea}; use futures::executor::block_on; use steam_shortcuts_util::shortcut::ShortcutOwned; use tokio::sync::watch::{self, Receiver}; @@ -77,7 +77,7 @@ enum UserAction { UserSelected(SteamUsersInfo), ShortcutSelected(ShortcutOwned), ImageTypeSelected(ImageType), - ImageTypeCleared(ImageType), + ImageTypeCleared(ImageType, bool), ImageSelected(PossibleImage), GridIdChanged(usize), BackButton, @@ -166,10 +166,21 @@ impl MyEguiApp { ) -> Option { ui.heading(image_type.name()); - if ui.small_button("Clear image?").on_hover_text("Click here to clear the image").clicked(){ - return Some(UserAction::ImageTypeCleared(image_type.clone())); + if ui + .small_button("Clear image?") + .on_hover_text("Click here to clear the image") + .clicked() + { + return Some(UserAction::ImageTypeCleared(image_type.clone(), false)); } + if ui + .small_button("Stop downloading this image?") + .on_hover_text("Stop downloading this type of image for this shortcut at all") + .clicked() + { + return Some(UserAction::ImageTypeCleared(image_type.clone(), true)); + } match &*state.image_options.borrow() { FetcStatus::Fetched(images) => { for image in images { @@ -287,17 +298,40 @@ impl MyEguiApp { UserAction::CorrectGridId => { self.handle_correct_grid_request(); } - UserAction::ImageTypeCleared(image_type) => { + UserAction::ImageTypeCleared(image_type, should_ban) => { + let app_id = self + .image_selected_state + .selected_shortcut + .as_ref() + .unwrap() + .app_id; + self.settings + .steamgrid_db + .set_image_banned(&image_type, app_id, should_ban); self.handle_image_type_clear(image_type); - }, + } }; } fn handle_image_type_clear(&mut self, image_type: ImageType) { - let data_folder = &self.image_selected_state.steam_user.as_ref().unwrap().steam_user_data_folder; - let file_name = image_type.file_name(self.image_selected_state.selected_shortcut.as_ref().unwrap().app_id); - let path = Path::new(data_folder).join("config").join("grid").join(&file_name); - if path.exists(){ + let data_folder = &self + .image_selected_state + .steam_user + .as_ref() + .unwrap() + .steam_user_data_folder; + let file_name = image_type.file_name( + self.image_selected_state + .selected_shortcut + .as_ref() + .unwrap() + .app_id, + ); + let path = Path::new(data_folder) + .join("config") + .join("grid") + .join(&file_name); + if path.exists() { let _ = std::fs::remove_file(&path); } let key = path.to_string_lossy().to_string(); @@ -601,21 +635,6 @@ fn clamp_to_width(size: &mut egui::Vec2, max_width: f32) { size.y = y; } -fn get_image( - ui: &mut egui::Ui, - shortcut: &ShortcutOwned, - folder: &std::path::Path, - image_type: &ImageType, -) -> Option { - let file_name = ImageType::file_name(image_type, shortcut.app_id); - let file_path = folder.join(file_name); - let image = load_image_from_path(file_path.as_path()).map(|img_data| { - ui.ctx() - .load_texture(file_path.to_string_lossy().to_string(), img_data) - }); - image -} - trait HasImageKey { fn key(&self, image_type: &ImageType, user_path: &Path) -> (PathBuf, String); }