mirror of
https://github.com/ankitects/anki.git
synced 2026-01-13 22:13:58 -05:00
- Introduced a new transact() method that wraps the return value in a separate struct that describes the changes that were made. - Changes are now gathered from the undo log, so we don't need to guess at what was changed - eg if update_note() is called with identical note contents, no changes are returned. Card changes will only be set if cards were actually generated by the update_note() call, and tag will only be set if a new tag was added. - mw.perform_op() has been updated to expect the op to return the changes, or a structure with the changes in it, and it will use them to fire the change hook, instead of fetching the changes from undo_status(), so there is no risk of race conditions. - the various calls to mw.perform_op() have been split into separate files like card_ops.py. Aside from making the code cleaner, this works around a rather annoying issue with mypy. Because we run it with no_strict_optional, mypy is happy to accept an operation that returns None, despite the type signature saying it requires changes to be returned. Turning no_strict_optional on for the whole codebase is not practical at the moment, but we can enable it for individual files. Still todo: - The cursor keeps moving back to the start of a field when typing - we need to ignore the refresh hook when we are the initiator. - The busy cursor icon should probably be delayed a few hundreds ms. - Still need to think about a nicer way of handling saveNow() - op_made_changes(), op_affects_study_queue() might be better embedded as properties in the object instead
89 lines
3.1 KiB
Rust
89 lines
3.1 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use super::{progress::Progress, Backend};
|
|
use crate::{
|
|
backend_proto as pb,
|
|
media::{check::MediaChecker, MediaManager},
|
|
prelude::*,
|
|
};
|
|
pub(super) use pb::media_service::Service as MediaService;
|
|
|
|
impl MediaService for Backend {
|
|
// media
|
|
//-----------------------------------------------
|
|
|
|
fn check_media(&self, _input: pb::Empty) -> Result<pb::CheckMediaOut> {
|
|
let mut handler = self.new_progress_handler();
|
|
let progress_fn =
|
|
move |progress| handler.update(Progress::MediaCheck(progress as u32), true);
|
|
self.with_col(|col| {
|
|
let mgr = MediaManager::new(&col.media_folder, &col.media_db)?;
|
|
col.transact_no_undo(|ctx| {
|
|
let mut checker = MediaChecker::new(ctx, &mgr, progress_fn);
|
|
let mut output = checker.check()?;
|
|
|
|
let report = checker.summarize_output(&mut output);
|
|
|
|
Ok(pb::CheckMediaOut {
|
|
unused: output.unused,
|
|
missing: output.missing,
|
|
report,
|
|
have_trash: output.trash_count > 0,
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
fn trash_media_files(&self, input: pb::TrashMediaFilesIn) -> Result<pb::Empty> {
|
|
self.with_col(|col| {
|
|
let mgr = MediaManager::new(&col.media_folder, &col.media_db)?;
|
|
let mut ctx = mgr.dbctx();
|
|
mgr.remove_files(&mut ctx, &input.fnames)
|
|
})
|
|
.map(Into::into)
|
|
}
|
|
|
|
fn add_media_file(&self, input: pb::AddMediaFileIn) -> Result<pb::String> {
|
|
self.with_col(|col| {
|
|
let mgr = MediaManager::new(&col.media_folder, &col.media_db)?;
|
|
let mut ctx = mgr.dbctx();
|
|
Ok(mgr
|
|
.add_file(&mut ctx, &input.desired_name, &input.data)?
|
|
.to_string()
|
|
.into())
|
|
})
|
|
}
|
|
|
|
fn empty_trash(&self, _input: pb::Empty) -> Result<pb::Empty> {
|
|
let mut handler = self.new_progress_handler();
|
|
let progress_fn =
|
|
move |progress| handler.update(Progress::MediaCheck(progress as u32), true);
|
|
|
|
self.with_col(|col| {
|
|
let mgr = MediaManager::new(&col.media_folder, &col.media_db)?;
|
|
col.transact_no_undo(|ctx| {
|
|
let mut checker = MediaChecker::new(ctx, &mgr, progress_fn);
|
|
|
|
checker.empty_trash()
|
|
})
|
|
})
|
|
.map(Into::into)
|
|
}
|
|
|
|
fn restore_trash(&self, _input: pb::Empty) -> Result<pb::Empty> {
|
|
let mut handler = self.new_progress_handler();
|
|
let progress_fn =
|
|
move |progress| handler.update(Progress::MediaCheck(progress as u32), true);
|
|
self.with_col(|col| {
|
|
let mgr = MediaManager::new(&col.media_folder, &col.media_db)?;
|
|
|
|
col.transact_no_undo(|ctx| {
|
|
let mut checker = MediaChecker::new(ctx, &mgr, progress_fn);
|
|
|
|
checker.restore_trash()
|
|
})
|
|
})
|
|
.map(Into::into)
|
|
}
|
|
}
|