mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-05-04 12:41:50 +02:00
Use xdg.BaseDirectory in _find_pdf_viewers
This commit is contained in:
parent
ce95dffc06
commit
4e92a22f97
3 changed files with 18 additions and 31 deletions
|
@ -11,7 +11,7 @@ from dangerzone.gui import Application
|
||||||
from dangerzone.gui.settings import Settings
|
from dangerzone.gui.settings import Settings
|
||||||
|
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
import xdg
|
||||||
|
|
||||||
|
|
||||||
class GuiCommon(Common):
|
class GuiCommon(Common):
|
||||||
|
@ -24,6 +24,7 @@ class GuiCommon(Common):
|
||||||
|
|
||||||
# Qt app
|
# Qt app
|
||||||
self.app = app
|
self.app = app
|
||||||
|
self.settings = Settings()
|
||||||
|
|
||||||
# Preload font
|
# Preload font
|
||||||
self.fixed_font = QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)
|
self.fixed_font = QtGui.QFontDatabase.systemFont(QtGui.QFontDatabase.FixedFont)
|
||||||
|
@ -34,18 +35,13 @@ class GuiCommon(Common):
|
||||||
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
||||||
self.is_waiting_finished = False
|
self.is_waiting_finished = False
|
||||||
|
|
||||||
self.settings = Settings()
|
|
||||||
|
|
||||||
def open_pdf_viewer(self, filename: str):
|
def open_pdf_viewer(self, filename: str):
|
||||||
if platform.system() == "Darwin":
|
if platform.system() == "Darwin":
|
||||||
# Open in Preview
|
# Open in Preview
|
||||||
args = ["open", "-a", "Preview.app", filename]
|
args = ["open", "-a", "Preview.app", filename]
|
||||||
|
|
||||||
# Run
|
|
||||||
args_str = " ".join(pipes.quote(s) for s in args)
|
args_str = " ".join(pipes.quote(s) for s in args)
|
||||||
print(Fore.YELLOW + "> " + Fore.CYAN + args_str)
|
print(Fore.YELLOW + "> " + Fore.CYAN + args_str)
|
||||||
subprocess.run(args)
|
subprocess.run(args)
|
||||||
|
|
||||||
elif platform.system() == "Linux":
|
elif platform.system() == "Linux":
|
||||||
# Get the PDF reader command
|
# Get the PDF reader command
|
||||||
args = shlex.split(self.pdf_viewers[self.settings.get("open_app")])
|
args = shlex.split(self.pdf_viewers[self.settings.get("open_app")])
|
||||||
|
@ -58,39 +54,29 @@ class GuiCommon(Common):
|
||||||
or args[i] == "%U"
|
or args[i] == "%U"
|
||||||
):
|
):
|
||||||
args[i] = filename
|
args[i] = filename
|
||||||
|
|
||||||
# Open as a background process
|
# Open as a background process
|
||||||
args_str = " ".join(pipes.quote(s) for s in args)
|
args_str = " ".join(pipes.quote(s) for s in args)
|
||||||
print(Fore.YELLOW + "> " + Fore.CYAN + args_str)
|
print(Fore.YELLOW + "> " + Fore.CYAN + args_str)
|
||||||
subprocess.Popen(args)
|
subprocess.Popen(args)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _find_pdf_viewers():
|
def _find_pdf_viewers() -> dict[str, str]:
|
||||||
"""Dict of PDF viewers installed on the machine, empty if system is not Linux."""
|
"""Dict of PDF viewers installed on the machine, empty if system is not Linux."""
|
||||||
pdf_viewers = {}
|
pdf_viewers: dict[str, str] = {}
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
# Find all .desktop files
|
# Find all .desktop files
|
||||||
for search_path in [
|
paths = [path + "/applications" for path in xdg.BaseDirectory.xdg_data_dirs]
|
||||||
"/usr/share/applications",
|
for path in paths:
|
||||||
"/usr/local/share/applications",
|
try: # search the directory
|
||||||
os.path.expanduser("~/.local/share/applications"),
|
contents = os.listdir(path)
|
||||||
]:
|
|
||||||
try:
|
|
||||||
for filename in os.listdir(search_path):
|
|
||||||
full_filename = os.path.join(search_path, filename)
|
|
||||||
if os.path.splitext(filename)[1] == ".desktop":
|
|
||||||
|
|
||||||
# See which ones can open PDFs
|
|
||||||
desktop_entry = DesktopEntry(full_filename)
|
|
||||||
if (
|
|
||||||
"application/pdf" in desktop_entry.getMimeTypes()
|
|
||||||
and desktop_entry.getName() != "dangerzone"
|
|
||||||
):
|
|
||||||
pdf_viewers[
|
|
||||||
desktop_entry.getName()
|
|
||||||
] = desktop_entry.getExec()
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
for file_name in contents:
|
||||||
|
if os.path.splitext(file_name)[1] == ".desktop":
|
||||||
|
# See which ones can open PDFs
|
||||||
|
file_path = os.path.join(path, file_name)
|
||||||
|
entry = xdg.DesktopEntry(file_path)
|
||||||
|
if "application/pdf" in entry.getMimeTypes() and entry.getName() != "dangerzone":
|
||||||
|
pdf_viewers[entry.getName()] = entry.getExec()
|
||||||
return pdf_viewers
|
return pdf_viewers
|
||||||
|
|
2
poetry.lock
generated
2
poetry.lock
generated
|
@ -389,7 +389,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "1.1"
|
lock-version = "1.1"
|
||||||
python-versions = ">=3.7,<3.11"
|
python-versions = ">=3.7,<3.11"
|
||||||
content-hash = "5da26ad8f7fd76d25f1acda9cafcefa1409e1d16f23fb03030952b15f5880d10"
|
content-hash = "4d4bd8a79c0e33390185ad8d1172ecabf76e847c5274c895632cc6882016e339"
|
||||||
|
|
||||||
[metadata.files]
|
[metadata.files]
|
||||||
altgraph = [
|
altgraph = [
|
||||||
|
|
|
@ -28,6 +28,7 @@ black = "*"
|
||||||
typing-extensions = "*"
|
typing-extensions = "*"
|
||||||
types-Pillow = "*"
|
types-Pillow = "*"
|
||||||
types-appdirs = "*"
|
types-appdirs = "*"
|
||||||
|
pyxdg = "*"
|
||||||
|
|
||||||
[tool.poetry.scripts]
|
[tool.poetry.scripts]
|
||||||
dangerzone = 'dangerzone:main'
|
dangerzone = 'dangerzone:main'
|
||||||
|
|
Loading…
Reference in a new issue