PICARD-2879: macOS: Extend all paths in filebrowser with /Volumes/

This ensures that selected starting directory gets resolved correctly,
as the filebrowser only supports paths under /Volumes/, not directly under
/. On saving drop the /Volumes/ prefix for paths on the root volume.
This commit is contained in:
Philipp Wolfer
2024-04-30 08:05:43 +02:00
parent 52656c7cb5
commit 0cf178cd5d
4 changed files with 46 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
# Picard, the next-generation MusicBrainz tagger
#
# Copyright (C) 2007, 2014, 2016 Lukáš Lalinský
# Copyright (C) 2014, 2019-2022 Philipp Wolfer
# Copyright (C) 2014, 2019-2022, 2024 Philipp Wolfer
# Copyright (C) 2014-2016, 2018-2021, 2024 Laurent Monin
# Copyright (C) 2015 Ohm Patel
# Copyright (C) 2016 Rahul Raturi
@@ -77,9 +77,6 @@ DEFAULT_LOCAL_COVER_ART_REGEX = r'^(?:cover|folder|albumart)(.*)\.(?:jpe?g|png|g
DEFAULT_CURRENT_BROWSER_PATH = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.HomeLocation)
if IS_MACOS:
from picard.util.macos import extend_root_volume_path
DEFAULT_CURRENT_BROWSER_PATH = extend_root_volume_path(DEFAULT_CURRENT_BROWSER_PATH)
# Default query limit
DEFAULT_QUERY_LIMIT = 50

View File

@@ -4,7 +4,7 @@
#
# Copyright (C) 2006-2008 Lukáš Lalinský
# Copyright (C) 2008 Hendrik van Antwerpen
# Copyright (C) 2008-2009, 2019-2022 Philipp Wolfer
# Copyright (C) 2008-2009, 2019-2022, 2024 Philipp Wolfer
# Copyright (C) 2011 Andrew Barnert
# Copyright (C) 2012-2013 Michael Wiencek
# Copyright (C) 2013 Wieland Hoffmann
@@ -42,6 +42,10 @@ from picard.const.sys import IS_MACOS
from picard.formats import supported_formats
from picard.i18n import gettext as _
from picard.util import find_existing_path
from picard.util.macos import (
extend_root_volume_path,
strip_root_volume_path,
)
class FileBrowser(QtWidgets.QTreeView):
@@ -176,6 +180,8 @@ class FileBrowser(QtWidgets.QTreeView):
path = config.persist['current_browser_path']
scrolltype = QtWidgets.QAbstractItemView.ScrollHint.PositionAtCenter
if path:
if IS_MACOS:
path = extend_root_volume_path(path)
index = self.model().index(find_existing_path(path))
self.setCurrentIndex(index)
self.expand(index)
@@ -185,6 +191,8 @@ class FileBrowser(QtWidgets.QTreeView):
destination = os.path.normpath(path)
if not os.path.isdir(destination):
destination = os.path.dirname(destination)
if IS_MACOS:
destination = strip_root_volume_path(destination)
return destination
def load_file_for_item(self, index):

View File

@@ -41,3 +41,14 @@ def extend_root_volume_path(path):
path = path[1:]
path = os.path.join(root_volume, path)
return path
def strip_root_volume_path(path):
if not path.startswith("/Volumes/"):
return path
root_volume = _find_root_volume()
if root_volume:
norm_path = os.path.normpath(path)
if norm_path.startswith(root_volume):
path = os.path.join('/', norm_path[len(root_volume):])
return path

View File

@@ -24,7 +24,10 @@ from unittest.mock import patch
from test.picardtestcase import PicardTestCase
from picard.const.sys import IS_MACOS
from picard.util.macos import extend_root_volume_path
from picard.util.macos import (
extend_root_volume_path,
strip_root_volume_path,
)
def fake_os_scandir(path):
@@ -58,3 +61,24 @@ class UtilMacosExtendRootVolumeTest(PicardTestCase):
result = extend_root_volume_path(path)
self.assertEqual(result, path)
self.assertTrue(mock.called)
@unittest.skipUnless(IS_MACOS, "macOS test")
class UtilMacosStripRootVolumeTest(PicardTestCase):
def test_path_starts_not_with_volumes(self):
path = '/Users/sandra'
result = strip_root_volume_path(path)
self.assertEqual(result, path)
@patch('picard.util.macos._find_root_volume', lambda: '/Volumes/root_volume')
def test_path_starts_not_with_root_volume(self):
path = '/Volumes/other_volume/foo/bar'
result = strip_root_volume_path(path)
self.assertEqual(result, path)
@patch('picard.util.macos._find_root_volume', lambda: '/Volumes/root_volume')
def test_path_starts_with_root_volume(self):
path = '/Volumes/root_volume/foo/bar'
result = strip_root_volume_path(path)
self.assertEqual(result, '/foo/bar')