Fix broken startup symlinks (#531)

* utility: create_startup_entry check link health

* model: init always run create_startup_entry

* utility: initialize_safeeyes don't call create_startup_entry

* utility: add force option to create_startup_entry

* model: do not create autostart, only repair broken

* utility: fix comment
This commit is contained in:
Ilario Gelmetti 2023-08-18 17:15:30 +02:00 committed by GitHub
parent 068771d5fd
commit 5a993a4d60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 12 deletions

View File

@ -303,6 +303,8 @@ class Config:
# self.__force_upgrade = ['long_breaks', 'short_breaks']
if init:
# if create_startup_entry finds a broken autostart symlink, it will repair it
utility.create_startup_entry(force=False)
if self.__user_config is None:
utility.initialize_safeeyes()
self.__user_config = self.__system_config

View File

@ -391,27 +391,51 @@ def initialize_safeeyes():
shutil.copy2(SYSTEM_STYLE_SHEET_PATH, STYLE_SHEET_PATH)
os.chmod(STYLE_SHEET_PATH, 0o777)
create_startup_entry()
# initialize_safeeyes gets called when the configuration file is not present, which happens just after installation or manual deletion of .config/safeeyes/safeeyes.json file. In these cases, we want to force the creation of a startup entry
create_startup_entry(force=True)
def create_startup_entry():
def create_startup_entry(force=False):
"""
Create start up entry.
"""
startup_dir_path = os.path.join(HOME_DIRECTORY, '.config/autostart')
startup_entry = os.path.join(startup_dir_path, 'io.github.slgobinath.SafeEyes.desktop')
# until SafeEyes 2.1.5 the startup entry had another name
# https://github.com/slgobinath/SafeEyes/commit/684d16265a48794bb3fd670da67283fe4e2f591b#diff-0863348c2143a4928518a4d3661f150ba86d042bf5320b462ea2e960c36ed275L398
obsolete_entry = os.path.join(startup_dir_path, 'safeeyes.desktop')
# Create the folder if not exist
mkdir(startup_dir_path)
create_link = False
# Remove existing files
delete(startup_entry)
if force:
# if force is True, just create the link
create_link = True
else:
# if force is False, we want to avoid creating the startup symlink if it was manually deleted by the user, we want to create it only if a broken one is found
if os.path.islink(startup_entry):
# if the link exists, check if it is broken
try:
os.stat(startup_entry)
except FileNotFoundError:
# a FileNotFoundError will get thrown if the startup symlink is broken
create_link = True
# Create the new startup entry
try:
os.symlink(SYSTEM_DESKTOP_FILE, startup_entry)
except OSError:
logging.error("Failed to create startup entry at %s" % startup_entry)
if os.path.islink(obsolete_entry):
# if a link with the old naming exists, delete it and create a new one
create_link = True
delete(obsolete_entry)
if create_link:
# Create the folder if not exist
mkdir(startup_dir_path)
# Remove existing files
delete(startup_entry)
# Create the new startup entry
try:
os.symlink(SYSTEM_DESKTOP_FILE, startup_entry)
except OSError:
logging.error("Failed to create startup entry at %s" % startup_entry)
def initialize_platform():