From cfeb71724df01b53e67046964140ade42d820f12 Mon Sep 17 00:00:00 2001 From: GithubAnon0000 <160563432+GithubAnon0000@users.noreply.github.com> Date: Sat, 28 Jun 2025 18:33:17 +0000 Subject: [PATCH] CHANGE right-click in the editor to show option to open folder in linux (#4125) * CHANGE right-click in the editor to show option to open folder in linux * FIX checks * Use heuristics * ./ninja format * Use fallback from main instead of xdg-open --- qt/aqt/editor.py | 9 ++++----- qt/aqt/utils.py | 31 ++++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index 3a980145d..e824af14e 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -36,7 +36,7 @@ from anki.hooks import runFilter from anki.httpclient import HttpClient from anki.models import NotetypeDict, NotetypeId, StockNotetype from anki.notes import Note, NoteFieldsCheckResult, NoteId -from anki.utils import checksum, is_lin, is_mac, is_win, namedtmp +from anki.utils import checksum, is_lin, is_win, namedtmp from aqt import AnkiQt, colors, gui_hooks from aqt.operations import QueryOp from aqt.operations.note import update_note @@ -1735,10 +1735,9 @@ class EditorWebView(AnkiWebView): assert a is not None qconnect(a.triggered, lambda: openFolder(path)) - if is_win or is_mac: - a = menu.addAction(tr.editing_show_in_folder()) - assert a is not None - qconnect(a.triggered, lambda: show_in_folder(path)) + a = menu.addAction(tr.editing_show_in_folder()) + assert a is not None + qconnect(a.triggered, lambda: show_in_folder(path)) def _clipboard(self) -> QClipboard: clipboard = self.editor.mw.app.clipboard() diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index e17550fc0..d0252f69d 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -936,9 +936,34 @@ def show_in_folder(path: str) -> None: """ call(osascript_to_args(script)) else: - # Just open the file in any other platform - with no_bundled_libs(): - QDesktopServices.openUrl(QUrl.fromLocalFile(path)) + # For linux, there are multiple file managers. Let's test if one of the + # most common file managers is found and use it in case it is installed. + # If none of this list are installed, use a fallback. The fallback + # might open the image in a web browser, image viewer or others, + # depending on the users defaults. + file_managers = [ + "nautilus", # GNOME + "dolphin", # KDE + "pcmanfm", # LXDE + "thunar", # XFCE + "nemo", # Cinnamon + "caja", # MATE + ] + + available_file_manager = None + + # Test if a file manager is installed and use it, fallback otherwise + for file_manager in file_managers: + if shutil.which(file_manager): + available_file_manager = file_manager + break + + if available_file_manager: + subprocess.run([available_file_manager, path], check=False) + else: + # Just open the file in any other platform + with no_bundled_libs(): + QDesktopServices.openUrl(QUrl.fromLocalFile(path)) def _show_in_folder_win32(path: str) -> None: