Skip to content

Commit

Permalink
Delete books when removing a storage location
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbende committed Jan 19, 2024
1 parent 2b60139 commit 68e2924
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
33 changes: 13 additions & 20 deletions cozy/view_model/library_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from cozy.report import reporter
from cozy.ui.widgets.book_element import BookElement
from cozy.ui.import_failed_dialog import ImportFailedDialog
from cozy.view_model.settings_view_model import SettingsViewModel
from cozy.view_model.storages_view_model import StoragesViewModel

log = logging.getLogger("library_view_model")

Expand All @@ -43,7 +43,7 @@ class LibraryViewModel(Observable, EventSender):
_model = inject.attr(Library)
_importer: Importer = inject.attr(Importer)
_player: Player = inject.attr(Player)
_settings: SettingsViewModel = inject.attr(SettingsViewModel)
_storages: StoragesViewModel = inject.attr(StoragesViewModel)

def __init__(self):
super().__init__()
Expand All @@ -60,7 +60,7 @@ def _connect(self):
self._importer.add_listener(self._on_importer_event)
self._player.add_listener(self._on_player_event)
self._model.add_listener(self._on_model_event)
self._settings.add_listener(self._on_settings_event)
self._storages.add_listener(self._on_storages_event)

@property
def books(self):
Expand Down Expand Up @@ -222,24 +222,17 @@ def _on_player_event(self, event, message):
elif event == "position" or event == "book-finished":
self._notify("book-progress")

def _on_settings_event(self, event: str, message):
def _on_storages_event(self, event: str, message):
if event == "storage-removed":
self._on_external_storage_removed(message)

def _on_external_storage_removed(self, storage: Storage):
books = self.books.copy()
for book in books:
chapters_to_remove = [c for c in book.chapters if c.file.startswith(str(storage.path))]

for chapter in chapters_to_remove:
chapter.delete()

self._notify("authors")
self._notify("readers")
self._notify("books")
self._notify("books-filter")
self._notify("current_book_in_playback")
self._notify("playing")
for property in (
"authors",
"readers",
"books",
"books-filter",
"current_book_in_playback",
"playing",
):
self._notify(property)

def _on_model_event(self, event: str, message):
if event == "rebase-finished":
Expand Down
11 changes: 10 additions & 1 deletion cozy/view_model/storages_view_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,17 @@ def remove(self, model: Storage) -> None:

model.delete()
self._model.invalidate()
self.emit_event("storage-removed", model)

storage_path = str(model.path)
for book in self._library.books:
chapters_to_remove = [
c for c in book.chapters if c.file.startswith(storage_path)
]

for chapter in chapters_to_remove:
chapter.delete()

self.emit_event("storage-removed", model)
self._notify("storage_locations")

def set_default(self, model: Storage) -> None:
Expand Down

0 comments on commit 68e2924

Please sign in to comment.