From fd840a2c959533f7e8b753d57cb223e0c969aebe Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Sun, 28 Aug 2022 11:54:19 +0200 Subject: [PATCH] create a desktop file when running the binary distribution on Linux --- icons/krowlog.svg | 14 +------- krowlog.py | 5 +++ src/install.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 src/install.py diff --git a/icons/krowlog.svg b/icons/krowlog.svg index 64d2c34..c10be71 100644 --- a/icons/krowlog.svg +++ b/icons/krowlog.svg @@ -29,18 +29,6 @@ z"/> - - - - - - - - - - - - - + \ No newline at end of file diff --git a/krowlog.py b/krowlog.py index 137cfe4..339bc84 100644 --- a/krowlog.py +++ b/krowlog.py @@ -5,6 +5,7 @@ from PySide6.QtWidgets import QApplication from PySide6.QtCore import QTimer import sys import constants +from src import install from src.pluginregistry import PluginRegistry import gettext from src.ui.icon import Icon @@ -37,8 +38,12 @@ if __name__ == "__main__": if sys.platform == 'win32' or sys.platform == 'cygwin': myappid = 'krowlog' # arbitrary string import ctypes + ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) + # install stuff, e.g. a desktop file + install.install() + # workaround to make signals work in QT apps. # They do not work out of the box, because the main thread # is running in C++ code once app.exec() is executed diff --git a/src/install.py b/src/install.py new file mode 100644 index 0000000..0d92c2a --- /dev/null +++ b/src/install.py @@ -0,0 +1,89 @@ +import sys +import textwrap +from os import PathLike +from pathlib import Path + + +def install(): + if sys.platform == 'win32' or sys.platform == 'cygwin': + # nothing to do + pass + else: + if _is_executable(): + _linux_install_desktop_file() + _linux_install_icon() + + +def _is_executable() -> bool: + """ + Checks if we run from an executable (as opposed to from source by calling 'python krowlog.py') + :return: True if this process is run from a distribution binary + """ + return Path(sys.executable).name == 'krowlog' + + +def _linux_install_desktop_file(): + apps_dir = Path.home().joinpath(".local").joinpath("share").joinpath("applications") + if apps_dir.exists() and apps_dir.is_dir(): + krowlog_desktop_file = apps_dir.joinpath("krowlog.desktop") + if not krowlog_desktop_file.exists(): + desktop_file_data = textwrap.dedent(""" + [Desktop Entry] + Type=Application + Terminal=false + Name=KrowLog + Comment=A viewer for files of arbitrary size. + Categories=Development;Utility;TextTools;IDE; + MimeType=text/plain;text/x-log + Icon=krowlog + Exec={binary} + """.format(binary=Path(sys.executable).resolve())) + krowlog_desktop_file.write_text(desktop_file_data, "utf8") + + +def _linux_install_icon(): + local_share = Path.home().joinpath(".local").joinpath("share") + if local_share.is_dir(): + icon_dir = local_share / "icons" / "hicolor" / "scalable" / "apps" + icon_dir.mkdir(parents=True, exist_ok=True) + _linux_install_icon_to_path(icon_dir / "krowlog.svg") + + +def _linux_install_icon_to_path(path: Path): + svg = textwrap.dedent(""" + + + + + + + + + + + + + + + + + + + + + +""") + path.write_text(svg, "utf8") + pass