Skip to content

Commit

Permalink
feat: download remote images
Browse files Browse the repository at this point in the history
Signed-off-by: tsukinaha <sakuovds@gmail.com>
  • Loading branch information
tsukinaha committed Dec 23, 2024
1 parent f5501e4 commit b36c282
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 11 deletions.
5 changes: 4 additions & 1 deletion resources/ui/image_dialog_search_page.ui
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
<object class="GtkScrolledWindow">
<property name="hscrollbar-policy">never</property>
<child>
<object class="GtkGridView" id="grid" />
<object class="GtkGridView" id="grid">
<property name="single-click-activate">true</property>
<signal name="activate" handler="item_activated_cb" swapped="yes" />
</object>
</child>
</object>
</child>
Expand Down
13 changes: 13 additions & 0 deletions src/client/emby_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,19 @@ impl EmbyClient {
self.post("Items/Delete", &params, json!({})).await
}

pub async fn download_remote_images(
&self, id: &str, type_: &str, provider_name: &str, image_url: &str,
) -> Result<()> {
let path = format!("Items/{}/RemoteImages/Download", id);
let params = [
("Type", type_),
("ProviderName", provider_name),
("ImageUrl", image_url),
];
self.post(&path, &params, json!({})).await?;
Ok(())
}

pub async fn get_show_missing(
&self, id: &str, include_specials: bool, upcoming: bool,
) -> Result<MissingEpisodesList> {
Expand Down
6 changes: 4 additions & 2 deletions src/ui/widgets/identify/search_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ impl IdentifyDialogSearchPage {
#[weak]
check_button,
move |_, _| {
let id = id.clone();
let value = value.clone();
let replace = check_button.is_active();
spawn(glib::clone!(
#[weak]
obj,
#[strong]
id,
#[strong]
value,
async move {
match spawn_tokio(async move {
EMBY_CLIENT.apply_remote_search(&id, value, replace).await
Expand Down
84 changes: 79 additions & 5 deletions src/ui/widgets/image_dialog/search_page.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
use adw::subclass::prelude::*;
use gettextrs::gettext;
use gtk::{
gio,
glib,
CompositeTemplate,
StringObject,
};

use gtk::{
prelude::*,
template_callbacks,
};
use adw::prelude::*;
use gtk::template_callbacks;

use crate::{
client::{
emby_client::EMBY_CLIENT,
error::UserFacingError,
},
toast,
ui::widgets::eu_item,
ui::widgets::eu_item::{
self,
EuObject,
},
utils::{
spawn,
spawn_tokio,
Expand Down Expand Up @@ -117,6 +119,78 @@ impl ImageDialogSearchPage {
.build()
}

#[template_callback]
async fn item_activated_cb(&self, pos: u32, gridview: &gtk::GridView) {
let Some(item) = gridview.model().and_then(|m| {
m.item(pos)
.and_downcast::<EuObject>()
.and_then(|o| o.item())
}) else {
return;
};

let Some(provider_name) = item.line1() else {
return;
};

let Some(url) = item.image_original_url() else {
return;
};

let id = self.id();
let image_type = self.image_type();

let alert_dialog = adw::AlertDialog::builder()
.heading(gettext("Replace Image"))
.title("Replace Image")
.body(gettext("Are you sure you wish to continue?"))
.build();

alert_dialog.add_response("close", &gettext("Cancel"));
alert_dialog.add_response("ok", &gettext("Ok"));
alert_dialog.set_response_appearance("ok", adw::ResponseAppearance::Suggested);

alert_dialog.connect_response(
Some("ok"),
glib::clone!(
#[weak(rename_to = obj)]
self,
move |_, _| {
spawn(glib::clone!(
#[weak]
obj,
#[strong]
id,
#[strong]
image_type,
#[strong]
provider_name,
#[strong]
url,
async move {
match spawn_tokio(async move {
EMBY_CLIENT
.download_remote_images(&id, &image_type, &provider_name, &url)
.await
})
.await
{
Ok(_) => {
toast!(obj, gettext("Success"));
}
Err(e) => {
toast!(obj, e.to_user_facing());
}
}
}
))
}
),
);

alert_dialog.present(Some(self));
}

pub async fn init<const FIRST_INIT: bool>(&self) {
let id = self.id();
let type_ = self.image_type();
Expand Down
4 changes: 1 addition & 3 deletions src/ui/widgets/metadata_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ mod imp {
impl MetadataDialog {
fn init(&self) {
if IS_ADMIN.load(std::sync::atomic::Ordering::Relaxed) {
self.page.set_title("View Metadata");
self.hint
.set_subtitle("This page is READ-ONLY, because it is not finished yet.");
self.hint.set_visible(false);
}

spawn(glib::clone!(
Expand Down

0 comments on commit b36c282

Please sign in to comment.