11import winreg as reg
22import os
3+ import sys
4+ from utils .logger import Logger
35
46class StartupManager :
57 STARTUP_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
8+ app_path = f'"{ os .path .abspath (sys .argv [0 ])} " --minimized'
9+ Logger .info (app_path )
610
711 @staticmethod
8- def add_to_startup (app_name , app_path ) :
12+ def add_to_startup (app_name : str ) -> None :
913 """
1014 Adds the application to the Windows startup registry.
1115
1216 :param app_name: Name of the application (Registry key name).
13- :param app_path: Full path to the executable with arguments if necessary.
1417 """
18+ if not sys .argv [0 ].endswith (".exe" ):
19+ return
20+
1521 try :
1622 with reg .OpenKey (reg .HKEY_CURRENT_USER , StartupManager .STARTUP_KEY , 0 , reg .KEY_SET_VALUE ) as registry_key :
17- reg .SetValueEx (registry_key , app_name , 0 , reg .REG_SZ , app_path )
18- print (f"{ app_name } added to startup successfully." )
23+ reg .SetValueEx (registry_key , app_name , 0 , reg .REG_SZ , StartupManager . app_path )
24+ Logger . info (f"{ app_name } added to startup successfully." )
1925 except WindowsError as e :
20- print (f"Failed to add { app_name } to startup: { e } " )
26+ Logger . error (f"Failed to add { app_name } to startup: { e } " )
2127
2228 @staticmethod
23- def remove_from_startup (app_name ):
24- """
25- Removes the application from the Windows startup registry.
26-
27- :param app_name: Name of the application (Registry key name).
28- """
29+ def remove_from_startup (app_name : str ) -> None :
30+ """Removes the application from the Windows startup registry."""
2931 try :
3032 with reg .OpenKey (reg .HKEY_CURRENT_USER , StartupManager .STARTUP_KEY , 0 , reg .KEY_SET_VALUE ) as registry_key :
3133 reg .DeleteValue (registry_key , app_name )
32- print (f"{ app_name } removed from startup successfully." )
34+ Logger .info (f"{ app_name } removed from startup successfully." )
35+ except FileNotFoundError :
36+ Logger .error (f"{ app_name } not found in startup." )
37+ except WindowsError as e :
38+ Logger .error (f"Failed to remove { app_name } from startup: { e } " )
39+
40+ @staticmethod
41+ def is_in_startup (app_name : str ) -> bool :
42+ """Checks if the application is registered in the Windows startup registry."""
43+ try :
44+ with reg .OpenKey (reg .HKEY_CURRENT_USER , StartupManager .STARTUP_KEY , 0 , reg .KEY_READ ) as registry_key :
45+ reg .QueryValueEx (registry_key , app_name )
46+ return True
3347 except FileNotFoundError :
34- print ( f" { app_name } not found in startup." )
48+ return False
3549 except WindowsError as e :
36- print (f"Failed to remove { app_name } from startup: { e } " )
50+ Logger .error (f"Failed to check startup status: { e } " )
51+ return False
52+
0 commit comments