diff --git a/libremsonic/ui/configure_servers.py b/libremsonic/ui/configure_servers.py index 6a94b08..7126869 100644 --- a/libremsonic/ui/configure_servers.py +++ b/libremsonic/ui/configure_servers.py @@ -5,93 +5,35 @@ from gi.repository import Gtk, GObject from libremsonic.server import Server from libremsonic.config import ServerConfiguration +from libremsonic.ui import util -class EditServerDialog(Gtk.Dialog): - """ - The Add New/Edit Server Dialog. The dialogs are the same, but when editing, - an ``existing_config`` will be specified. - """ - def __init__(self, parent, existing_config=None): - editing = existing_config is not None - Gtk.Dialog.__init__( - self, - f'Edit {existing_config.name}' if editing else 'Add New Server', - parent, 0, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, - Gtk.STOCK_EDIT if editing else Gtk.STOCK_ADD, - Gtk.ResponseType.OK)) - - if not existing_config: - existing_config = ServerConfiguration() - - # Create the two columns for the labels and corresponding entry fields. - self.set_default_size(450, 250) - content_area = self.get_content_area() - flowbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) - label_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) - entry_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) - flowbox.pack_start(label_box, False, True, 10) - flowbox.pack_start(entry_box, True, True, 10) - - # Store a map of field label to GTK component. - self.data = {} - - # Create all of the text entry fields for the server configuration. - text_fields = [ - ('Name', existing_config.name, False), - ('Server address', existing_config.server_address, False), - ('Local network address', existing_config.local_network_address, - False), - ('Local network SSID', existing_config.local_network_ssid, False), - ('Username', existing_config.username, False), - ('Password', existing_config.password, True), - ] - for label, value, is_password in text_fields: - # Put the label in the left box. - entry_label = Gtk.Label(label + ':') - entry_label.set_halign(Gtk.Align.START) - label_box.pack_start(entry_label, True, True, 0) - - # Put the text entry in the right box. - entry = Gtk.Entry(text=value) - if is_password: - entry.set_visibility(False) - entry_box.pack_start(entry, True, True, 0) - self.data[label] = entry - - # Create all of the check box fields for the server configuration. - boolean_fields = [ - ('Browse by tags', existing_config.browse_by_tags), - ('Sync enabled', existing_config.sync_enabled), - ] - for label, value in boolean_fields: - # Put the label in the left box. - entry_label = Gtk.Label(label + ':') - entry_label.set_halign(Gtk.Align.START) - label_box.pack_start(entry_label, True, True, 0) - - # Put the checkbox in the right box. Note we have to pad here since - # the checkboxes are smaller than the text fields. - checkbox = Gtk.CheckButton(active=value) - entry_box.pack_start(checkbox, True, True, 5) - self.data[label] = checkbox - - content_area.pack_start(flowbox, True, True, 10) - - # Create a box for buttons. - button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) +class EditServerDialog(util.EditFormDialog): + entity_name: str = 'Server' + initial_size = (450, 250) + text_fields = [ + ('Name', 'name', False), + ('Server address', 'server_address', False), + ('Local network address', 'local_network_address', False), + ('Local network SSID', 'local_network_ssid', False), + ('Username', 'username', False), + ('Password', 'password', True), + ] + boolean_fields = [ + ('Browse by tags', 'browse_by_tags'), + ('Sync enabled', 'sync_enabled'), + ] + def __init__(self, *args, **kwargs): test_server = Gtk.Button('Test Connection to Server') test_server.connect('clicked', self.on_test_server_clicked) - button_box.pack_start(test_server, False, True, 5) open_in_browser = Gtk.Button('Open in Browser') open_in_browser.connect('clicked', self.on_open_in_browser_clicked) - button_box.pack_start(open_in_browser, False, True, 5) - content_area.pack_start(button_box, True, True, 10) + self.extra_buttons = [test_server, open_in_browser] - self.show_all() + super().__init__(*args, **kwargs) def on_test_server_clicked(self, event): # Instantiate the server. diff --git a/libremsonic/ui/playlists.py b/libremsonic/ui/playlists.py index 2be5f11..2aa9b26 100644 --- a/libremsonic/ui/playlists.py +++ b/libremsonic/ui/playlists.py @@ -11,6 +11,13 @@ from libremsonic.cache_manager import CacheManager, SongCacheStatus from libremsonic.ui import util +class EditPlaylistDialog(util.EditFormDialog): + entity_name: str = 'Playlist' + initial_size = (350, 120) + text_fields = [('Name', 'name', False), ('Comment', 'comment', False)] + boolean_fields = [('Public', 'public')] + + class PlaylistsPanel(Gtk.Paned): """Defines the playlists panel.""" __gsignals__ = { @@ -126,14 +133,20 @@ class PlaylistsPanel(Gtk.Paned): # Action buttons, name, comment, number of songs, etc. playlist_details_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) - # Action buttons + # Action buttons (note we are packing end here, so we have to put them + # in right-to-left). # TODO hide this if there is no selected playlist action_button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) - view_refresh_button = util.button_with_icon('view-refresh') + view_refresh_button = util.button_with_icon('view-refresh-symbolic') view_refresh_button.connect('clicked', self.on_view_refresh_click) action_button_box.pack_end(view_refresh_button, False, False, 5) + playlist_edit_button = util.button_with_icon('document-edit-symbolic') + playlist_edit_button.connect('clicked', + self.on_playlist_edit_button_click) + action_button_box.pack_end(playlist_edit_button, False, False, 5) + playlist_details_box.pack_start(action_button_box, False, False, 5) playlist_details_box.pack_start(Gtk.Box(), True, False, 0) @@ -237,6 +250,24 @@ class PlaylistsPanel(Gtk.Paned): def on_list_refresh_click(self, button): self.update_playlist_list(force=True) + def on_playlist_edit_button_click(self, button): + selected = self.playlist_list.get_selected_row() + playlist_id = self.playlist_map[selected.get_index()].id + dialog = EditPlaylistDialog( + self.get_toplevel(), + CacheManager.get_playlist(playlist_id, + before_download=lambda: None).result()) + result = dialog.run() + if result == Gtk.ResponseType.OK: + CacheManager.update_playlist( + playlist_id, + name=dialog.data['Name'].get_text(), + comment=dialog.data['Comment'].get_text(), + public=dialog.data['Public'].get_active(), + ) + self.update_playlist_view(playlist_id, force=True) + dialog.destroy() + def on_view_refresh_click(self, button): playlist_id = self.playlist_map[ self.playlist_list.get_selected_row().get_index()] @@ -385,12 +416,18 @@ class PlaylistsPanel(Gtk.Paned): def format_stats(self, playlist): created_date = playlist.created.strftime('%B %d, %Y') - return ' • '.join([ - f'Created by {playlist.owner} on {created_date}', - '{} {}'.format(playlist.songCount, - util.pluralize("song", playlist.songCount)), - self.format_playlist_duration(playlist.duration) - ]) + lines = [ + ' • '.join([ + f'Created by {playlist.owner} on {created_date}', + f"{'Not v' if not playlist.public else 'V'}isible with others", + ]), + ' • '.join([ + '{} {}'.format(playlist.songCount, + util.pluralize("song", playlist.songCount)), + self.format_playlist_duration(playlist.duration) + ]), + ] + return '\n'.join(lines) def format_playlist_duration(self, duration_secs) -> str: duration_mins = (duration_secs // 60) % 60 diff --git a/libremsonic/ui/util.py b/libremsonic/ui/util.py index a0b1a53..52d40d9 100644 --- a/libremsonic/ui/util.py +++ b/libremsonic/ui/util.py @@ -1,4 +1,5 @@ import functools +from typing import List, Tuple from concurrent.futures import Future @@ -37,6 +38,93 @@ def esc(string): return string.replace('&', '&') +class EditFormDialog(Gtk.Dialog): + entity_name: str + initial_size: Tuple[int, int] + text_fields: List[Tuple[str, str, bool]] = [] + boolean_fields: List[Tuple[str, str]] = [] + extra_buttons: List[Gtk.Button] = [] + + def get_object_name(self, obj): + """ + Gets the friendly object name. Can be overridden. + """ + return obj.name if obj else '' + + def get_default_object(self): + return None + + def __init__(self, parent, existing_object=None): + editing = existing_object is not None + Gtk.Dialog.__init__( + self, + f'Edit {self.get_object_name(existing_object)}' + if editing else f'Create New {self.entity_name}', + parent, + 0, + (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, + Gtk.STOCK_EDIT if editing else Gtk.STOCK_ADD, + Gtk.ResponseType.OK), + ) + + if not existing_object: + existing_object = self.get_default_object() + + self.set_default_size(*self.initial_size) + + if len(self.text_fields) + len(self.boolean_fields) > 0: + # Create two columns for the labels and corresponding entry fields. + content_area = self.get_content_area() + flowbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) + label_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) + entry_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) + flowbox.pack_start(label_box, False, True, 10) + flowbox.pack_start(entry_box, True, True, 10) + + # Store a map of field label to GTK component. + self.data = {} + + # Create all of the text entry fields. + for label, value_field_name, is_password in self.text_fields: + # Put the label in the left box. + entry_label = Gtk.Label(label + ':') + entry_label.set_halign(Gtk.Align.START) + label_box.pack_start(entry_label, True, True, 0) + + # Put the text entry in the right box. + entry = Gtk.Entry( + text=getattr(existing_object, value_field_name, '')) + if is_password: + entry.set_visibility(False) + entry_box.pack_start(entry, True, True, 0) + self.data[label] = entry + + # Create all of the check box fields. + for label, value_field_name in self.boolean_fields: + # Put the label in the left box. + entry_label = Gtk.Label(label + ':') + entry_label.set_halign(Gtk.Align.START) + label_box.pack_start(entry_label, True, True, 0) + + # Put the checkbox in the right box. Note we have to pad here + # since the checkboxes are smaller than the text fields. + checkbox = Gtk.CheckButton( + active=getattr(existing_object, value_field_name, False)) + entry_box.pack_start(checkbox, True, True, 5) + self.data[label] = checkbox + + content_area.pack_start(flowbox, True, True, 10) + + # Create a box for buttons. + if len(self.extra_buttons) > 0: + button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) + for button in self.extra_buttons: + button_box.pack_start(button, False, True, 5) + content_area.pack_start(button_box, True, True, 10) + + self.show_all() + + def async_callback(future_fn, before_download=None, on_failure=None): """ Defines the ``async_callback`` decorator.