Remove downloads button

This commit is contained in:
Sumner Evans
2019-07-28 10:28:34 -06:00
parent ad0662455d
commit e08e3cb12c
2 changed files with 46 additions and 5 deletions

View File

@@ -185,6 +185,9 @@ class CacheManager(metaclass=Singleton):
self.app_config.cache_location).joinpath(*relative_paths)
def calculate_download_path(self, *relative_paths):
"""
Determine where to temporarily put the file as it is downloading.
"""
xdg_cache_home = (os.environ.get('XDG_CACHE_HOME')
or os.path.expanduser('~/.cache'))
return Path(xdg_cache_home).joinpath('libremsonic',
@@ -381,6 +384,28 @@ class CacheManager(metaclass=Singleton):
return CacheManager.executor.submit(do_get_albums)
def batch_delete_cached_songs(
self,
song_ids: List[int],
on_song_delete: Callable[[], None],
) -> Future:
def do_delete_cached_songs():
# Do the actual download.
for song_id in song_ids:
song_details_future = CacheManager.get_song_details(
song_id)
def filename_future_done(f):
relative_path = f.result().path
abs_path = self.calculate_abs_path(relative_path)
if abs_path.exists():
abs_path.unlink()
on_song_delete()
song_details_future.add_done_callback(filename_future_done)
return CacheManager.executor.submit(do_delete_cached_songs)
def batch_download_songs(
self,
song_ids: List[int],

View File

@@ -97,6 +97,12 @@ def show_song_popover(
on_song_download_complete=on_download_state_change,
)
def on_remove_downloads_click(button):
CacheManager.batch_delete_cached_songs(
song_ids,
on_song_delete=on_download_state_change,
)
def on_add_to_playlist_click(button, playlist):
CacheManager.executor.submit(
CacheManager.update_playlist,
@@ -111,13 +117,17 @@ def show_song_popover(
song_count = len(song_ids)
# Determine if we should enable the download button.
download_sensitive = False
download_sensitive, remove_download_sensitive = False, False
for song_id in song_ids:
details = CacheManager.get_song_details(song_id)
status = CacheManager.get_cached_status(details.result())
if status == SongCacheStatus.NOT_CACHED:
if download_sensitive or status == SongCacheStatus.NOT_CACHED:
download_sensitive = True
break
if (remove_download_sensitive
or status in (SongCacheStatus.CACHED,
SongCacheStatus.PERMANENTLY_CACHED)):
remove_download_sensitive = True
menu_items = [
(Gtk.ModelButton(text='Add to up next'), on_add_to_up_next_click),
@@ -134,12 +144,18 @@ def show_song_popover(
(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), None),
(
Gtk.ModelButton(
text=(f"Download {pluralize('song', song_count)}"
if song_count > 1 else 'Download Song'),
text=f"Download {pluralize('song', song_count)}",
sensitive=download_sensitive,
),
on_download_songs_click,
),
(
Gtk.ModelButton(
text=f"Demove {pluralize('download', song_count)}",
sensitive=remove_download_sensitive,
),
on_remove_downloads_click,
),
(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), None),
(
Gtk.ModelButton(