Refactor Linux autostart (#568)

This commit is contained in:
Samuel Woon 2020-08-10 13:39:51 -05:00 committed by GitHub
parent 976e3dae83
commit 3d7d97fc40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 25 deletions

View File

@ -1,19 +1,10 @@
import sys
import os
from pathlib import Path
LINUX_STARTUP_FILE = """\
[Desktop Entry]
Name=Vorta
GenericName=Backup Software
Exec={} --daemonize
Terminal=false
Icon=com.borgbase.Vorta
Categories=Utility
Type=Application
StartupNotify=false
AUTOSTART_DELAY = """StartupNotify=false
X-GNOME-Autostart-enabled=true
X-GNOME-Autostart-Delay=20
"""
X-GNOME-Autostart-Delay=20"""
def open_app_at_startup(enabled=True):
@ -42,20 +33,33 @@ def open_app_at_startup(enabled=True):
LSSharedFileListItemRemove(login_items, new_item)
elif sys.platform.startswith('linux'):
autostart_path = Path.home() / '.config' / 'autostart'
is_flatpak = Path('/.flatpak-info').exists()
if not autostart_path.exists():
autostart_path.mkdir()
with open(os.path.join(os.path.dirname(__file__),
"assets/metadata/com.borgbase.Vorta.desktop")) as desktop_file:
desktop_file_text = desktop_file.read()
autostart_file_path = autostart_path / 'vorta.desktop'
if enabled:
if Path('/.flatpak-info').exists():
# Vorta runs as flatpak
autostart_file_path.write_text(LINUX_STARTUP_FILE.format('flatpak run com.borgbase.Vorta'))
# Find XDG_CONFIG_HOME unless when running in flatpak
if is_flatpak:
autostart_path = Path.home() / '.config' / 'autostart'
else:
autostart_file_path.write_text(LINUX_STARTUP_FILE.format('vorta'))
autostart_path = Path(os.environ.get(
"XDG_CONFIG_HOME", os.path.expanduser("~") + '/.config') + "/autostart")
else:
if autostart_file_path.exists():
autostart_file_path.unlink()
if not autostart_path.exists():
autostart_path.mkdir()
autostart_file_path = autostart_path / 'vorta.desktop'
if enabled:
# Replace to for flatpak if appropriate and start in background
desktop_file_text = desktop_file_text.replace(
"Exec=vorta", "Exec=flatpak run com.borgbase.Vorta --daemonize" if is_flatpak
else "Exec=vorta --daemonize")
# Add autostart delay
desktop_file_text += (AUTOSTART_DELAY)
autostart_file_path.write_text(desktop_file_text)
else:
if autostart_file_path.exists():
autostart_file_path.unlink()

31
tests/test_misc.py Normal file
View File

@ -0,0 +1,31 @@
from PyQt5 import QtCore
from PyQt5.QtWidgets import QCheckBox
from pathlib import Path
import pytest
import os
import sys
@pytest.mark.skipif(sys.platform != 'linux', reason="Autostart files only generated in Linux")
def test_linux_autostart(qapp, qtbot):
main = qapp.main_window
main.tabWidget.setCurrentIndex(4)
tab = main.miscTab
for x in range(0, tab.checkboxLayout.count()):
checkbox = tab.checkboxLayout.itemAt(x).widget()
checkbox.__class__ = QCheckBox
if checkbox.text().startswith("Automatically"):
# Have to use pos to click checkbox correctly
# https://stackoverflow.com/questions/19418125/pysides-qtest-not-checking-box/24070484#24070484
qtbot.mouseClick(checkbox, QtCore.Qt.LeftButton, pos=QtCore.QPoint(2, checkbox.height() / 2))
break
autostart_path = Path(os.environ.get(
"XDG_CONFIG_HOME", os.path.expanduser("~") + '/.config') + "/autostart") / "vorta.desktop"
qtbot.waitUntil(lambda: autostart_path.exists(), timeout=5000)
with open(autostart_path) as desktop_file:
desktop_file_text = desktop_file.read()
assert(desktop_file_text.startswith("[Desktop Entry]"))