From 3d7d97fc40a913cf708f432916ae28fa894e7029 Mon Sep 17 00:00:00 2001 From: Samuel Woon Date: Mon, 10 Aug 2020 13:39:51 -0500 Subject: [PATCH] Refactor Linux autostart (#568) --- src/vorta/autostart.py | 54 +++++++++++++++++++++++------------------- tests/test_misc.py | 31 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 tests/test_misc.py diff --git a/src/vorta/autostart.py b/src/vorta/autostart.py index 1f7094f9..c512f9b4 100644 --- a/src/vorta/autostart.py +++ b/src/vorta/autostart.py @@ -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() diff --git a/tests/test_misc.py b/tests/test_misc.py new file mode 100644 index 00000000..58a433e1 --- /dev/null +++ b/tests/test_misc.py @@ -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]"))