Skip to content

Commit 9d5f55c

Browse files
Linked add_to_startup and remove_from_startup methods with their option in SettingsDialog.
1 parent 315f15c commit 9d5f55c

File tree

4 files changed

+42
-21
lines changed

4 files changed

+42
-21
lines changed

ui/dialogs/settings_dialog.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
from PyQt6.QtGui import QKeySequence
2121
from PyQt6.QtCore import Qt
2222
from core_functions.Reciters import RecitersManager
23-
from utils.const import data_folder
23+
from utils.const import data_folder, program_english_name
2424
from utils.settings import SettingsManager
2525
from utils.audio_player import AthkarPlayer, AyahPlayer, SoundEffectPlayer
26+
from utils.Startup import StartupManager
2627

2728

2829
class SettingsDialog(QDialog):
@@ -194,20 +195,23 @@ def updateBackgroundCheckboxState(self):
194195
# If 'start_on_system_start_checkbox' is checked, automatically check 'run_in_background_checkbox'
195196
self.run_in_background_checkbox.setChecked(True)
196197

197-
198198
def updateStartCheckboxState(self):
199199

200200
# Check if 'run_in_background_checkbox' is unchecked, then uncheck 'start_on_system_start_checkbox'
201201
if not self.run_in_background_checkbox.isChecked():
202202
self.start_on_system_start_checkbox.setChecked(False)
203203

204+
def save_settings(self):
204205

206+
if SettingsManager.current_settings["general"]["auto_start_enabled"] != self.start_on_system_start_checkbox.isChecked():
207+
if self.start_on_system_start_checkbox.isChecked():
208+
StartupManager.add_to_startup(program_english_name)
209+
else:
210+
StartupManager.remove_from_startup(program_english_name)
205211

206-
207-
def save_settings(self):
208212
general_settings = {
209213
"run_in_background_enabled": self.run_in_background_checkbox.isChecked(),
210-
"start_on_system_starts_enabled": self.start_on_system_start_checkbox.isChecked(),
214+
"auto_start_enabled": self.start_on_system_start_checkbox.isChecked(),
211215
"auto_save_position_enabled": self.auto_save_position_checkbox.isChecked(),
212216
"check_update_enabled": self.update_checkbox.isChecked(),
213217
"logging_enabled": self.log_checkbox.isChecked()
@@ -264,7 +268,7 @@ def set_current_settings(self):
264268
self.athkar_volume.setValue(current_settings["audio"]["athkar_volume_level"])
265269
self.ayah_volume.setValue(current_settings["audio"]["ayah_volume_level"])
266270
self.run_in_background_checkbox.setChecked(current_settings["general"]["run_in_background_enabled"])
267-
self.start_on_system_start_checkbox.setChecked(current_settings["general"]["start_on_system_starts_enabled"])
271+
self.start_on_system_start_checkbox.setChecked(current_settings["general"]["auto_start_enabled"])
268272
self.auto_save_position_checkbox.setChecked(current_settings["general"]["auto_save_position_enabled"])
269273
self.update_checkbox.setChecked(current_settings["general"]["check_update_enabled"])
270274
self.log_checkbox.setChecked(current_settings["general"]["logging_enabled"])

utils/Startup.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
import winreg as reg
22
import os
3+
import sys
4+
from utils.logger import Logger
35

46
class 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+

utils/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
# program information
2626
program_name = "البيان"
27+
program_english_name = "Albayan"
2728
program_version = "1.3.0"
2829
program_icon = "icon.webp"
2930
tray_icon: Optional[QSystemTrayIcon] = None

utils/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SettingsManager:
1111
"general": {
1212
"language": "Arabic",
1313
"run_in_background_enabled": False,
14-
"start_on_system_starts_enabled": False,
14+
"auto_start_enabled": True,
1515
"auto_save_position_enabled": False,
1616
"check_update_enabled": True,
1717
"logging_enabled": False

0 commit comments

Comments
 (0)