236 lines
7.1 KiB
Python
236 lines
7.1 KiB
Python
import functools
|
|
from typing import Callable, List, Tuple, Any
|
|
|
|
from concurrent.futures import Future
|
|
|
|
import gi
|
|
gi.require_version('Gtk', '3.0')
|
|
from gi.repository import Gio, Gtk, GObject, GLib, Gdk
|
|
|
|
from libremsonic.cache_manager import CacheManager, SongCacheStatus
|
|
|
|
|
|
def button_with_icon(
|
|
icon_name,
|
|
relief=False,
|
|
icon_size=Gtk.IconSize.BUTTON,
|
|
) -> Gtk.Button:
|
|
button = Gtk.Button()
|
|
icon = Gio.ThemedIcon(name=icon_name)
|
|
image = Gtk.Image.new_from_gicon(icon, icon_size)
|
|
button.add(image)
|
|
|
|
if not relief:
|
|
button.props.relief = Gtk.ReliefStyle.NONE
|
|
|
|
return button
|
|
|
|
|
|
def format_song_duration(duration_secs) -> str:
|
|
return f'{duration_secs // 60}:{duration_secs % 60:02}'
|
|
|
|
|
|
def pluralize(string: str, number: int, pluralized_form=None):
|
|
if number != 1:
|
|
return pluralized_form or f'{string}s'
|
|
return string
|
|
|
|
|
|
def format_sequence_duration(duration_secs) -> str:
|
|
duration_mins = (duration_secs // 60) % 60
|
|
duration_hrs = duration_secs // 60 // 60
|
|
duration_secs = duration_secs % 60
|
|
|
|
format_components = []
|
|
if duration_hrs > 0:
|
|
hrs = '{} {}'.format(duration_hrs, pluralize('hour', duration_hrs))
|
|
format_components.append(hrs)
|
|
|
|
if duration_mins > 0:
|
|
mins = '{} {}'.format(duration_mins, pluralize('minute',
|
|
duration_mins))
|
|
format_components.append(mins)
|
|
|
|
# Show seconds if there are no hours.
|
|
if duration_hrs == 0:
|
|
secs = '{} {}'.format(duration_secs, pluralize('second',
|
|
duration_secs))
|
|
format_components.append(secs)
|
|
|
|
return ', '.join(format_components)
|
|
|
|
|
|
def esc(string):
|
|
return string.replace('&', '&').replace(" target='_blank'", '')
|
|
|
|
|
|
def dot_join(*items):
|
|
return ' • '.join(map(str, items))
|
|
|
|
|
|
def show_song_popover(
|
|
song_ids,
|
|
x: int,
|
|
y: int,
|
|
relative_to: Any,
|
|
position: Gtk.PositionType = Gtk.PositionType.BOTTOM,
|
|
on_download_state_change: Callable[[int], None] = lambda x: None,
|
|
show_remove_from_playlist_button: bool = False,
|
|
extra_menu_items: List[Tuple[Gtk.ModelButton, Any]] = [],
|
|
):
|
|
def on_add_to_up_next_click(button):
|
|
print('up next click')
|
|
|
|
def on_add_to_queue_click(button):
|
|
print('add to queue click')
|
|
|
|
def on_go_to_album_click(button):
|
|
print('go to album click')
|
|
|
|
def on_go_to_artist_click(button):
|
|
print('go to artist click')
|
|
|
|
def on_download_songs_click(button):
|
|
CacheManager.batch_download_songs(
|
|
song_ids,
|
|
before_download=on_download_state_change,
|
|
on_song_download_complete=on_download_state_change,
|
|
)
|
|
|
|
def on_add_to_playlist_click(button, playlist):
|
|
CacheManager.executor.submit(
|
|
CacheManager.update_playlist,
|
|
playlist_id=playlist.id,
|
|
song_id_to_add=song_ids,
|
|
)
|
|
|
|
popover = Gtk.PopoverMenu()
|
|
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
|
|
# Add all of the menu items to the popover.
|
|
song_count = len(song_ids)
|
|
|
|
# Determine if we should enable the download button.
|
|
download_sensitive = 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:
|
|
download_sensitive = True
|
|
break
|
|
|
|
menu_items = [
|
|
(Gtk.ModelButton(text='Add to up next'), on_add_to_up_next_click),
|
|
(Gtk.ModelButton(text='Add to queue'), on_add_to_queue_click),
|
|
(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), None),
|
|
(
|
|
Gtk.ModelButton(text='Go to album', sensitive=len(song_ids) == 1),
|
|
on_go_to_album_click,
|
|
),
|
|
(
|
|
Gtk.ModelButton(text='Go to artist', sensitive=len(song_ids) == 1),
|
|
on_go_to_artist_click,
|
|
),
|
|
(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), None),
|
|
(
|
|
Gtk.ModelButton(
|
|
text=(f"Download {pluralize('song', song_count)}"
|
|
if song_count > 1 else 'Download Song'),
|
|
sensitive=download_sensitive,
|
|
),
|
|
on_download_songs_click,
|
|
),
|
|
(Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL), None),
|
|
(
|
|
Gtk.ModelButton(
|
|
text=f"Add {pluralize('song', song_count)} to playlist",
|
|
menu_name='add-to-playlist',
|
|
),
|
|
None,
|
|
),
|
|
*extra_menu_items,
|
|
]
|
|
|
|
for item, action in menu_items:
|
|
if action:
|
|
item.connect('clicked', action)
|
|
if type(item) == Gtk.ModelButton:
|
|
item.get_style_context().add_class('menu-button')
|
|
vbox.pack_start(item, False, True, 0)
|
|
|
|
popover.add(vbox)
|
|
|
|
# Create the "Add song(s) to playlist" sub-menu.
|
|
playlists_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
|
|
# Back button
|
|
playlists_vbox.add(
|
|
Gtk.ModelButton(
|
|
inverted=True,
|
|
centered=True,
|
|
menu_name='main',
|
|
))
|
|
|
|
# The playlist buttons
|
|
for playlist in CacheManager.get_playlists().result():
|
|
button = Gtk.ModelButton(text=playlist.name)
|
|
button.get_style_context().add_class('menu-button')
|
|
button.connect('clicked', on_add_to_playlist_click, playlist)
|
|
playlists_vbox.pack_start(button, False, True, 0)
|
|
|
|
popover.add(playlists_vbox)
|
|
popover.child_set_property(playlists_vbox, 'submenu', 'add-to-playlist')
|
|
|
|
# Positioning of the popover.
|
|
rect = Gdk.Rectangle()
|
|
rect.x, rect.y, rect.width, rect.height = x, y, 1, 1
|
|
popover.set_pointing_to(rect)
|
|
popover.set_position(position)
|
|
popover.set_relative_to(relative_to)
|
|
|
|
popover.popup()
|
|
popover.show_all()
|
|
|
|
|
|
def async_callback(future_fn, before_download=None, on_failure=None):
|
|
"""
|
|
Defines the ``async_callback`` decorator.
|
|
|
|
When a function is annotated with this decorator, the function becomes the
|
|
done callback for the given future-generating lambda function. The
|
|
annotated function will be called with the result of the future generated
|
|
by said lambda function.
|
|
|
|
:param future_fn: a function which generates a
|
|
``concurrent.futures.Future``.
|
|
"""
|
|
def decorator(callback_fn):
|
|
@functools.wraps(callback_fn)
|
|
def wrapper(self, *args, **kwargs):
|
|
if before_download:
|
|
on_before_download = (
|
|
lambda: GLib.idle_add(before_download, self))
|
|
else:
|
|
on_before_download = (lambda: None)
|
|
|
|
def future_callback(f):
|
|
try:
|
|
result = f.result()
|
|
except Exception as e:
|
|
if on_failure:
|
|
on_failure(self, e)
|
|
return
|
|
|
|
return GLib.idle_add(callback_fn, self, result)
|
|
|
|
future: Future = future_fn(
|
|
*args,
|
|
before_download=on_before_download,
|
|
**kwargs,
|
|
)
|
|
future.add_done_callback(future_callback)
|
|
|
|
return wrapper
|
|
|
|
return decorator
|