create a desktop file when running the binary distribution on Linux

This commit is contained in:
2022-08-28 11:54:19 +02:00
parent 554220148f
commit fd840a2c95
3 changed files with 95 additions and 13 deletions

View File

@@ -29,18 +29,6 @@
z"/> z"/>
<rect x="8" y="8" width="112" height="112" <rect x="8" y="8" width="112" height="112"
style="fill:none; stroke:url(#bg1); stroke-width:14" rx="10"/> style="fill:none; stroke:url(#bg1); stroke-width:14" rx="10"/>
<circle cx="65" cy="40" r="6" fill="url(#rg1)"/> <circle cx="65" cy="40" r="6" fill="url(#rg1)"/>
<circle cx="65" cy="40" r="3" fill="white"/> <circle cx="65" cy="40" r="3" fill="white"/>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@@ -5,6 +5,7 @@ from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QTimer from PySide6.QtCore import QTimer
import sys import sys
import constants import constants
from src import install
from src.pluginregistry import PluginRegistry from src.pluginregistry import PluginRegistry
import gettext import gettext
from src.ui.icon import Icon from src.ui.icon import Icon
@@ -37,8 +38,12 @@ if __name__ == "__main__":
if sys.platform == 'win32' or sys.platform == 'cygwin': if sys.platform == 'win32' or sys.platform == 'cygwin':
myappid = 'krowlog' # arbitrary string myappid = 'krowlog' # arbitrary string
import ctypes import ctypes
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
# install stuff, e.g. a desktop file
install.install()
# workaround to make signals work in QT apps. # workaround to make signals work in QT apps.
# They do not work out of the box, because the main thread # They do not work out of the box, because the main thread
# is running in C++ code once app.exec() is executed # is running in C++ code once app.exec() is executed

89
src/install.py Normal file
View File

@@ -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("""<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="64" height="64" viewBox="0 0 128 128" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="bg1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#9f8700"/>
<stop offset="100%" style="stop-color:#ffdb00"/>
</linearGradient>
<linearGradient id="fg1" x1="0%" y1="100%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:#8caec7"/>
<stop offset="100%" style="stop-color:#6289a3"/>
</linearGradient>
<radialGradient id="rg1">
<stop offset="0%" stop-color="white"/>
<stop offset="100%" stop-color="black"/>
</radialGradient>
</defs>
<rect x="8" y="8" width="112" height="112" style="fill:url(#fg1)" rx="10"/>
<path style="fill:black"
d="M76,113
L15,113
L15,70
C15,70 25,55 45,36
C53,29 60,26 78,32
C85,34 90,32 90,32
C95,30 108,35 110,40
L110,40
C83,48 80,60 76,71
C74,80 76,113 76,113
z"/>
<rect x="8" y="8" width="112" height="112"
style="fill:none; stroke:url(#bg1); stroke-width:14" rx="10"/>
<circle cx="65" cy="40" r="6" fill="url(#rg1)"/>
<circle cx="65" cy="40" r="3" fill="white"/>
</svg>""")
path.write_text(svg, "utf8")
pass