2222"""
2323
2424import copy
25+ import logging
2526from packaging .version import parse
27+ import os
28+ import shutil
2629import typing
2730
2831from safeeyes import utility
@@ -44,11 +47,11 @@ def load(cls) -> "Config":
4447 force_upgrade_keys : list [str ] = []
4548 # force_upgrade_keys = ['long_breaks', 'short_breaks']
4649
47- # if create_startup_entry finds a broken autostart symlink, it will repair
50+ # if _create_startup_entry finds a broken autostart symlink, it will repair
4851 # it
49- utility . create_startup_entry (force = False )
52+ cls . _create_startup_entry (force = False )
5053 if user_config is None :
51- utility . initialize_safeeyes ()
54+ cls . _initialize_config ()
5255 user_config = copy .deepcopy (system_config )
5356 cfg = cls (user_config , system_config )
5457 cfg .save ()
@@ -126,3 +129,86 @@ def __eq__(self, config):
126129
127130 def __ne__ (self , config ):
128131 return self .__user_config != config .__user_config
132+
133+ @classmethod
134+ def reset_config (cls ) -> None :
135+ # Remove the ~/.config/safeeyes/safeeyes.json and safeeyes_style.css
136+ utility .delete (utility .CONFIG_FILE_PATH )
137+
138+ # Copy the safeeyes.json and safeeyes_style.css
139+ shutil .copy2 (utility .SYSTEM_CONFIG_FILE_PATH , utility .CONFIG_FILE_PATH )
140+
141+ # Add write permission (e.g. if original file was stored in /nix/store)
142+ os .chmod (utility .CONFIG_FILE_PATH , 0o600 )
143+
144+ cls ._create_startup_entry ()
145+
146+ @classmethod
147+ def _initialize_config (cls ) -> None :
148+ """Create the config file in XDG_CONFIG_HOME(or
149+ ~/.config)/safeeyes directory.
150+ """
151+ logging .info ("Copy the config files to XDG_CONFIG_HOME(or ~/.config)/safeeyes" )
152+
153+ # Remove the ~/.config/safeeyes/safeeyes.json file
154+ utility .delete (utility .CONFIG_FILE_PATH )
155+
156+ if not os .path .isdir (utility .CONFIG_DIRECTORY ):
157+ utility .mkdir (utility .CONFIG_DIRECTORY )
158+
159+ # Copy the safeeyes.json
160+ shutil .copy2 (utility .SYSTEM_CONFIG_FILE_PATH , utility .CONFIG_FILE_PATH )
161+ os .chmod (utility .CONFIG_FILE_PATH , 0o600 )
162+
163+ # This method gets called when the configuration file is not present,
164+ # which happens just after installation or manual deletion of
165+ # .config/safeeyes/safeeyes.json file. In these cases, we want to force the
166+ # creation of a startup entry
167+ cls ._create_startup_entry (force = True )
168+
169+ @classmethod
170+ def _create_startup_entry (cls , force : bool = False ) -> None :
171+ """Create start up entry."""
172+ startup_dir_path = os .path .join (utility .HOME_DIRECTORY , ".config/autostart" )
173+ startup_entry = os .path .join (
174+ startup_dir_path , "io.github.slgobinath.SafeEyes.desktop"
175+ )
176+ # until Safe Eyes 2.1.5 the startup entry had another name
177+ # https://github.com/slgobinath/safeeyes/commit/684d16265a48794bb3fd670da67283fe4e2f591b#diff-0863348c2143a4928518a4d3661f150ba86d042bf5320b462ea2e960c36ed275L398
178+ obsolete_entry = os .path .join (startup_dir_path , "safeeyes.desktop" )
179+
180+ create_link = False
181+
182+ if force :
183+ # if force is True, just create the link
184+ create_link = True
185+ else :
186+ # if force is False, we want to avoid creating the startup symlink if it was
187+ # manually deleted by the user, we want to create it only if a broken one is
188+ # found
189+ if os .path .islink (startup_entry ):
190+ # if the link exists, check if it is broken
191+ try :
192+ os .stat (startup_entry )
193+ except FileNotFoundError :
194+ # a FileNotFoundError will get thrown if the startup symlink is
195+ # broken
196+ create_link = True
197+
198+ if os .path .islink (obsolete_entry ):
199+ # if a link with the old naming exists, delete it and create a new one
200+ create_link = True
201+ utility .delete (obsolete_entry )
202+
203+ if create_link :
204+ # Create the folder if not exist
205+ utility .mkdir (startup_dir_path )
206+
207+ # Remove existing files
208+ utility .delete (startup_entry )
209+
210+ # Create the new startup entry
211+ try :
212+ os .symlink (utility .SYSTEM_DESKTOP_FILE , startup_entry )
213+ except OSError :
214+ logging .error ("Failed to create startup entry at %s" % startup_entry )
0 commit comments