-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
138 lines (117 loc) · 5.54 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys
import os
current_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
os.chdir(current_dir)
from multiprocessing import freeze_support
from PyQt6.QtWidgets import QApplication, QMessageBox
from PyQt6.QtCore import QTimer, Qt, QSharedMemory, QEvent
from PyQt6.QtNetwork import QLocalServer, QLocalSocket
from ui.quran_interface import QuranInterface
from core_functions.athkar.athkar_scheduler import AthkarScheduler
from utils.update import UpdateManager
from utils.settings import SettingsManager
from utils.const import program_name, program_icon, user_db_path
from utils.logger import Logger
from utils.audio_player import StartupSoundEffectPlayer, VolumeController
class SingleInstanceApplication(QApplication):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.setApplicationName(program_name)
self.server_name = "Albayan"
self.local_server = QLocalServer(self)
self.shared_memory = QSharedMemory("Albayan")
self.is_running = self.shared_memory.attach()
self.volume_controller = VolumeController()
self.installEventFilter(self)
if not self.is_running:
if not self.shared_memory.create(1):
Logger.error(f"Failed to create shared memory segment: {self.shared_memory.errorString()}")
sys.exit(1)
self.setup_local_server()
else:
self.notify_existing_instance()
sys.exit(0)
def eventFilter(self, obj, event):
if event.type() == QEvent.Type.KeyPress:
key = event.key()
modifiers = event.modifiers()
if key == Qt.Key.Key_F5:
self.volume_controller.switch_category("next")
return True
elif key == Qt.Key.Key_F6:
self.volume_controller.switch_category("previous")
return True
elif key == Qt.Key.Key_F7:
if modifiers & Qt.KeyboardModifier.ControlModifier: # Ctrl+F7
self.volume_controller.adjust_volume(-10)
elif modifiers & Qt.KeyboardModifier.ShiftModifier: # Shift+F7
self.volume_controller.adjust_volume(-5)
elif modifiers & Qt.KeyboardModifier.AltModifier: # Alt+F7
self.volume_controller.adjust_volume(-100)
else: # F7
self.volume_controller.adjust_volume(-1)
return True
elif key == Qt.Key.Key_F8:
if modifiers & Qt.KeyboardModifier.ControlModifier: # Ctrl+F8
self.volume_controller.adjust_volume(10)
elif modifiers & Qt.KeyboardModifier.ShiftModifier: # Shift+F8
self.volume_controller.adjust_volume(5)
elif modifiers & Qt.KeyboardModifier.AltModifier: # Alt+F8
self.volume_controller.adjust_volume(100)
else:
self.volume_controller.adjust_volume(1) # Default behavior (no modifiers)
return True
return super().eventFilter(obj, event)
def setup_local_server(self) -> None:
if not self.local_server.listen(self.server_name):
Logger.error(f"Failed to start local server: {self.local_server.errorString()}")
sys.exit(1)
self.local_server.newConnection.connect(self.handle_new_connection)
def notify_existing_instance(self) -> None:
socket = QLocalSocket()
socket.connectToServer(self.server_name)
if socket.waitForConnected(1000):
socket.write(b"SHOW")
socket.flush()
socket.waitForBytesWritten(1000)
socket.disconnectFromServer()
else:
Logger.error("Failed to connect to existing instance.")
def handle_new_connection(self) -> None:
socket = self.local_server.nextPendingConnection()
if socket:
socket.readyRead.connect(lambda: self.read_message(socket))
def read_message(self, socket) -> None:
message = socket.readAll().data().decode()
if message == "SHOW" and hasattr(self, 'main_window'):
self.main_window.show()
self.main_window.raise_()
self.main_window.activateWindow()
socket.disconnectFromServer()
def set_main_window(self, main_window) -> None:
self.main_window = main_window
def call_after_starting(parent: QuranInterface) -> None:
basmala = StartupSoundEffectPlayer("Audio/basmala")
basmala.play()
check_update_enabled = SettingsManager.current_settings["general"].get("check_update_enabled", False)
update_manager = UpdateManager(parent, check_update_enabled)
update_manager.check_auto_update()
parent.setFocus()
QTimer.singleShot(500, parent.quran_view.setFocus)
def main():
try:
app = SingleInstanceApplication(sys.argv)
app.setLayoutDirection(Qt.LayoutDirection.RightToLeft)
main_window = QuranInterface(program_name)
app.set_main_window(main_window)
if "--minimized" not in sys.argv:
main_window.show()
call_after_starting(main_window)
sys.exit(app.exec())
except Exception as e:
print(e)
Logger.error(str(e))
QMessageBox.critical(None, "Error", "حدث خطأ، إذا استمرت المشكلة، يرجى تفعيل السجل وتكرار الإجراء الذي تسبب بالخطأ ومشاركة رمز الخطأ والسجل مع المطورين.")
if __name__ == "__main__":
freeze_support()
main()