fix(desktop): expose readable removable volumes in Dolphin (#2671)

This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-17 11:19:29 -05:00
1 parent d7c03ba3ec
commit 1951bbc92a
3 files changed
+123 -7

No files matched your search

@@ -1428,6 +1428,28 @@ class SessionService : Service() {
} catch (e: Exception) {
Log.w(TAG, "steam library: the list could not be written; the card alone this session", e)
}
if (SessionState.mode == MODE_DESKTOP) {
try {
val listing = File(filesDir, "session/desktop-volumes.json").apply { parentFile?.mkdirs() }
val volumes = org.json.JSONArray()
var room = if (ProotFastPath.enabled(this)) {
ProotFastPath.MAX_BINDS - LinuxRuntime.binds(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds).size
} else Int.MAX_VALUE
for (root in com.droiddeck.launcher.files.StorageRoots.list(this)) {
if (!root.removable || !root.readable) continue
val path = root.dir.absolutePath
if (':' in path || '|' in path || room <= 0) continue
binds.add(path)
room--
volumes.put(org.json.JSONObject().put("path", path).put("label", root.label))
}
listing.writeText(volumes.toString())
val envAt = guest.indexOf(LinuxRuntime.SESSION_SCRIPT).takeIf { it >= 0 } ?: guest.size
guest.add(envAt, "BL_DESKTOP_VOLUMES=" + listing.path)
} catch (e: Exception) {
Log.w(TAG, "desktop volumes: the list could not be written", e)
}
}
return binds
}
@@ -67,7 +67,25 @@ def place_uri(uri):
return uri
def update_places(path, libraries):
def removable_volumes():
listing = os.environ.get("BL_DESKTOP_VOLUMES")
if not listing:
return {}
volumes = {}
for entry in json.loads(Path(listing).read_text()):
path = Path(entry["path"])
if not path.is_absolute():
continue
try:
with os.scandir(path):
pass
except OSError:
continue
volumes[path.as_uri()] = " ".join(str(entry["label"]).split()) or path.name
return volumes
def update_places(path, libraries, mark=MARK, icon_name="steam", id_prefix="droiddeck-steam-"):
if path.is_file():
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True, insert_pis=True))
root = ET.fromstring(path.read_bytes(), parser=parser)
@@ -80,10 +98,10 @@ def update_places(path, libraries):
before = ET.tostring(root)
seen = set()
personal = {place_uri(b.get("href")) for b in root.findall("bookmark")
if b.find("info/metadata[@owner='" + KDE_OWNER + "']/" + MARK) is None}
if b.find("info/metadata[@owner='" + KDE_OWNER + "']/" + mark) is None}
for bookmark in root.findall("bookmark"):
metadata = bookmark.find("info/metadata[@owner='" + KDE_OWNER + "']")
if metadata is None or metadata.find(MARK) is None:
if metadata is None or metadata.find(mark) is None:
continue
uri = place_uri(bookmark.get("href"))
if uri not in libraries or uri in seen or uri in personal:
@@ -101,10 +119,10 @@ def update_places(path, libraries):
ET.SubElement(bookmark, "title").text = label
info = ET.SubElement(bookmark, "info")
icon = ET.SubElement(info, "metadata", {"owner": "http://freedesktop.org"})
ET.SubElement(icon, "{" + ICON_NS + "}icon", {"name": "steam"})
ET.SubElement(icon, "{" + ICON_NS + "}icon", {"name": icon_name})
metadata = ET.SubElement(info, "metadata", {"owner": KDE_OWNER})
ET.SubElement(metadata, "ID").text = "droiddeck-steam-" + hashlib.sha256(uri.encode()).hexdigest()[:16]
ET.SubElement(metadata, MARK).text = "true"
ET.SubElement(metadata, "ID").text = id_prefix + hashlib.sha256(uri.encode()).hexdigest()[:16]
ET.SubElement(metadata, mark).text = "true"
ET.SubElement(metadata, "isSystemItem").text = "false"
if ET.tostring(root) != before:
atomic_write(path, ET.tostring(root, encoding="unicode", xml_declaration=True))
@@ -154,7 +172,9 @@ def main():
available = bool(label) and (LIBRARY / "steamapps").is_dir()
update_bookmark(bookmarks_file(home, config), label if available else None)
data = Path(os.environ.get("XDG_DATA_HOME") or home / ".local/share")
update_places(data / "user-places.xbel", steam_libraries(home, data))
places = data / "user-places.xbel"
update_places(places, steam_libraries(home, data))
update_places(places, removable_volumes(), "DroidDeckRemovableVolume", "drive-removable-media", "droiddeck-volume-")
if __name__ == "__main__":
+74
View File
@@ -162,6 +162,80 @@ class DolphinPlacesTest(unittest.TestCase):
self.assertEqual(1, len(root.findall("bookmark")))
self.assertEqual("My games", root.findtext("bookmark/title"))
def volumes(self, entries):
listing = self.home / "volumes.json"
listing.write_text(json.dumps(entries))
os.environ["BL_DESKTOP_VOLUMES"] = str(listing)
return MODULE["removable_volumes"]()
def update_volumes(self, volumes):
MODULE["update_places"](self.places, volumes, "DroidDeckRemovableVolume", "drive-removable-media", "droiddeck-volume-")
return ET.parse(self.places).getroot()
def test_all_readable_volumes_without_steam_libraries(self):
card = self.home / "ABCD-1234"
usb = self.home / "USB café & <disk>"
card.mkdir()
usb.mkdir()
missing = self.home / "unplugged"
entries = [{"path": str(card), "label": "Card"}, {"path": str(usb), "label": "USB & <disk>"},
{"path": str(missing), "label": "Gone"}, {"path": "relative", "label": "Invalid"}]
volumes = self.volumes(entries)
self.assertEqual({card.as_uri(): "Card", usb.as_uri(): "USB & <disk>"}, volumes)
root = self.update_volumes(volumes)
self.assertEqual(2, len(root.findall("bookmark")))
self.assertEqual("USB & <disk>", root.findall("bookmark")[1].findtext("title"))
self.assertFalse((card / "steamapps").exists())
self.assertEqual("drive-removable-media", root.find("bookmark/info/metadata/{" + MODULE["ICON_NS"] + "}icon").get("name"))
def test_main_adds_volumes_without_changing_gtk_or_steam_selection(self):
card = self.home / "card"
card.mkdir()
self.volumes([{"path": str(card), "label": "Card"}])
with patch.object(Path, "home", return_value=self.home), patch.dict(os.environ, {"XDG_DATA_HOME": str(self.data)}):
MODULE["main"]()
root = ET.parse(self.places).getroot()
self.assertEqual({self.steam.as_uri(), card.as_uri()}, {b.get("href") for b in root.findall("bookmark")})
self.assertFalse((self.home / ".config/gtk-3.0/bookmarks").exists())
self.volumes([])
MODULE["main"]()
root = ET.parse(self.places).getroot()
self.assertEqual([self.steam.as_uri()], [b.get("href") for b in root.findall("bookmark")])
def test_unreadable_volume_is_not_offered(self):
card = self.home / "card"
card.mkdir()
with patch("os.scandir", side_effect=PermissionError):
self.assertEqual({}, self.volumes([{"path": str(card), "label": "Card"}]))
def test_volume_refresh_preserves_steam_personal_and_hidden_places(self):
card = self.home / "card"
card.mkdir()
self.update({self.steam.as_uri(): "Steam"})
root = self.update_volumes({card.as_uri(): "Card"})
volume = root.findall("bookmark")[1]
metadata = volume.find("info/metadata[@owner='http://www.kde.org']")
ET.SubElement(metadata, "IsHidden").text = "true"
ET.SubElement(root, "bookmark", {"href": "file:///root/Documents"})
self.places.write_bytes(ET.tostring(root))
root = self.update_volumes({card.as_uri(): "Renamed"})
self.assertEqual("true", root.findall("bookmark")[1].findtext("info/metadata/IsHidden"))
before = self.places.read_bytes()
self.update_volumes({card.as_uri(): "Renamed"})
self.assertEqual(before, self.places.read_bytes())
self.update({self.steam.as_uri(): "Steam"})
root = self.update_volumes({})
self.assertEqual([self.steam.as_uri(), "file:///root/Documents"], [b.get("href") for b in root.findall("bookmark")])
def test_personal_volume_place_is_not_duplicated_or_removed(self):
card = self.home / "card"
card.mkdir()
self.places.write_text(f'<xbel><bookmark href="{card.as_uri()}/"><title>My card</title></bookmark></xbel>')
before = self.places.read_bytes()
self.update_volumes({card.as_uri(): "Card"})
self.update_volumes({})
self.assertEqual(before, self.places.read_bytes())
def test_invalid_xml_is_not_overwritten(self):
self.places.write_text("<xbel>unfinished")
with self.assertRaises(ET.ParseError):